Skip to content

Commit

Permalink
enh: improve upgrade guide
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-eschle committed Apr 15, 2024
1 parent 22f1938 commit 5ea0306
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 26 deletions.
87 changes: 74 additions & 13 deletions _website/tutorials/introduction/upgrade_guide_020.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"def maximum(x, y):\n",
" return znp.maximum(x, y)\n",
"\n",
"\n",
"print(f\"sqrt with np:{np.sqrt(maximum(1., 2.))}, with z:{znp.sqrt(maximum(1., 2.))}\")\n",
"print(f\"vectorized: {np.sqrt(maximum(znp.array([1., 2.]), znp.array([3., 4.])))}\")"
]
Expand Down Expand Up @@ -306,9 +307,46 @@
"id": "24",
"metadata": {},
"source": [
"## Result and serialization\n",
"## Result\n",
"\n",
"The result has more usage for setting parameters, the `update_params` method is now available and can be used to set the parameters to the minimum as seen above.\n",
"\n",
"The result stays similar but can now be pickled like any object in zfit!\n",
"It can also be used in a context manager to temporarily set the parameters to the minimum and restore them afterwards.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "25",
"metadata": {},
"outputs": [],
"source": [
"param1again.set_value(1.5)\n",
"with result:\n",
" print(f\"param1 set temporarily to {param1again}\")\n",
"print(f\"param1 is now {param1again} again\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "26",
"metadata": {},
"outputs": [],
"source": [
"# or to set the parameters to the minimum\n",
"zfit.param.set_values(result) # supports also a dict of {param: value}!\n",
"print(param1again)"
]
},
{
"cell_type": "markdown",
"id": "27",
"metadata": {},
"source": [
"## Serialization\n",
"\n",
"The result can now be pickled like any object in zfit!\n",
"(this was not possible before, only after calling `freeze` on the result)\n",
" \n",
"This works directly using `dill` (a library that extends `pickle`), but can fail if the garbage collector is not run. Therefore, zfit provides a slightly modified `dill` that can work as a drop-in replacement.\n",
Expand All @@ -319,18 +357,30 @@
{
"cell_type": "code",
"execution_count": null,
"id": "25",
"id": "28",
"metadata": {},
"outputs": [],
"source": [
"result_serialized = zfit.dill.dumps(result)\n",
"result_deserialized = zfit.dill.loads(result_serialized)\n",
"result_deserialized.errors()"
"result_deserialized = zfit.dill.loads(result_serialized)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "29",
"metadata": {},
"outputs": [],
"source": [
"# the result can be used as before\n",
"result_deserialized.hesse() # the default name is now \"hesse\" and not \"minuit_hesse\"\n",
"result_deserialized.errors() # the default name is now \"errors\" and not \"minuit_minos\"\n",
"print(result_deserialized)"
]
},
{
"cell_type": "markdown",
"id": "26",
"id": "30",
"metadata": {},
"source": [
"## Parameters as arguments\n",
Expand All @@ -343,7 +393,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "27",
"id": "31",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -358,7 +408,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "28",
"id": "32",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -381,13 +431,24 @@
{
"cell_type": "code",
"execution_count": null,
"id": "29",
"id": "33",
"metadata": {},
"outputs": [],
"source": [
"# a result can also be used as argument for PDFs or, here, for losses\n",
"loss_before = loss.value()\n",
"loss_min = loss.value(params=result_deserialized) # evaluate at minimum\n",
"print(f\"loss before (random from before): {loss_before:.7} minimum value: {loss_min:.7} vs {result_deserialized.fmin:.7}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "34",
"metadata": {},
"outputs": [],
"source": [
"# creating a PDF looks also different, but here we use the name of the parametrization and the axis (integers)\n",
"\n",
"\n",
"class MyGauss2D(zfit.pdf.ZPDF):\n",
" _PARAMS = (\"mu\", \"sigma\")\n",
" _N_OBS = 2\n",
Expand All @@ -404,7 +465,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "30",
"id": "35",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -415,7 +476,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "31",
"id": "36",
"metadata": {},
"outputs": [],
"source": []
Expand Down
87 changes: 74 additions & 13 deletions introduction/upgrade_guide_020.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"def maximum(x, y):\n",
" return znp.maximum(x, y)\n",
"\n",
"\n",
"print(f\"sqrt with np:{np.sqrt(maximum(1., 2.))}, with z:{znp.sqrt(maximum(1., 2.))}\")\n",
"print(f\"vectorized: {np.sqrt(maximum(znp.array([1., 2.]), znp.array([3., 4.])))}\")"
]
Expand Down Expand Up @@ -306,9 +307,46 @@
"id": "24",
"metadata": {},
"source": [
"## Result and serialization\n",
"## Result\n",
"\n",
"The result has more usage for setting parameters, the `update_params` method is now available and can be used to set the parameters to the minimum as seen above.\n",
"\n",
"The result stays similar but can now be pickled like any object in zfit!\n",
"It can also be used in a context manager to temporarily set the parameters to the minimum and restore them afterwards.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "25",
"metadata": {},
"outputs": [],
"source": [
"param1again.set_value(1.5)\n",
"with result:\n",
" print(f\"param1 set temporarily to {param1again}\")\n",
"print(f\"param1 is now {param1again} again\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "26",
"metadata": {},
"outputs": [],
"source": [
"# or to set the parameters to the minimum\n",
"zfit.param.set_values(result) # supports also a dict of {param: value}!\n",
"print(param1again)"
]
},
{
"cell_type": "markdown",
"id": "27",
"metadata": {},
"source": [
"## Serialization\n",
"\n",
"The result can now be pickled like any object in zfit!\n",
"(this was not possible before, only after calling `freeze` on the result)\n",
" \n",
"This works directly using `dill` (a library that extends `pickle`), but can fail if the garbage collector is not run. Therefore, zfit provides a slightly modified `dill` that can work as a drop-in replacement.\n",
Expand All @@ -319,18 +357,30 @@
{
"cell_type": "code",
"execution_count": null,
"id": "25",
"id": "28",
"metadata": {},
"outputs": [],
"source": [
"result_serialized = zfit.dill.dumps(result)\n",
"result_deserialized = zfit.dill.loads(result_serialized)\n",
"result_deserialized.errors()"
"result_deserialized = zfit.dill.loads(result_serialized)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "29",
"metadata": {},
"outputs": [],
"source": [
"# the result can be used as before\n",
"result_deserialized.hesse() # the default name is now \"hesse\" and not \"minuit_hesse\"\n",
"result_deserialized.errors() # the default name is now \"errors\" and not \"minuit_minos\"\n",
"print(result_deserialized)"
]
},
{
"cell_type": "markdown",
"id": "26",
"id": "30",
"metadata": {},
"source": [
"## Parameters as arguments\n",
Expand All @@ -343,7 +393,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "27",
"id": "31",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -358,7 +408,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "28",
"id": "32",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -381,13 +431,24 @@
{
"cell_type": "code",
"execution_count": null,
"id": "29",
"id": "33",
"metadata": {},
"outputs": [],
"source": [
"# a result can also be used as argument for PDFs or, here, for losses\n",
"loss_before = loss.value()\n",
"loss_min = loss.value(params=result_deserialized) # evaluate at minimum\n",
"print(f\"loss before (random from before): {loss_before:.7} minimum value: {loss_min:.7} vs {result_deserialized.fmin:.7}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "34",
"metadata": {},
"outputs": [],
"source": [
"# creating a PDF looks also different, but here we use the name of the parametrization and the axis (integers)\n",
"\n",
"\n",
"class MyGauss2D(zfit.pdf.ZPDF):\n",
" _PARAMS = (\"mu\", \"sigma\")\n",
" _N_OBS = 2\n",
Expand All @@ -404,7 +465,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "30",
"id": "35",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -415,7 +476,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "31",
"id": "36",
"metadata": {},
"outputs": [],
"source": []
Expand Down

0 comments on commit 5ea0306

Please sign in to comment.