Skip to content

Commit 5ea0306

Browse files
committed
enh: improve upgrade guide
1 parent 22f1938 commit 5ea0306

File tree

2 files changed

+148
-26
lines changed

2 files changed

+148
-26
lines changed

_website/tutorials/introduction/upgrade_guide_020.ipynb

+74-13
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"def maximum(x, y):\n",
4040
" return znp.maximum(x, y)\n",
4141
"\n",
42+
"\n",
4243
"print(f\"sqrt with np:{np.sqrt(maximum(1., 2.))}, with z:{znp.sqrt(maximum(1., 2.))}\")\n",
4344
"print(f\"vectorized: {np.sqrt(maximum(znp.array([1., 2.]), znp.array([3., 4.])))}\")"
4445
]
@@ -306,9 +307,46 @@
306307
"id": "24",
307308
"metadata": {},
308309
"source": [
309-
"## Result and serialization\n",
310+
"## Result\n",
311+
"\n",
312+
"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",
310313
"\n",
311-
"The result stays similar but can now be pickled like any object in zfit!\n",
314+
"It can also be used in a context manager to temporarily set the parameters to the minimum and restore them afterwards.\n"
315+
]
316+
},
317+
{
318+
"cell_type": "code",
319+
"execution_count": null,
320+
"id": "25",
321+
"metadata": {},
322+
"outputs": [],
323+
"source": [
324+
"param1again.set_value(1.5)\n",
325+
"with result:\n",
326+
" print(f\"param1 set temporarily to {param1again}\")\n",
327+
"print(f\"param1 is now {param1again} again\")"
328+
]
329+
},
330+
{
331+
"cell_type": "code",
332+
"execution_count": null,
333+
"id": "26",
334+
"metadata": {},
335+
"outputs": [],
336+
"source": [
337+
"# or to set the parameters to the minimum\n",
338+
"zfit.param.set_values(result) # supports also a dict of {param: value}!\n",
339+
"print(param1again)"
340+
]
341+
},
342+
{
343+
"cell_type": "markdown",
344+
"id": "27",
345+
"metadata": {},
346+
"source": [
347+
"## Serialization\n",
348+
"\n",
349+
"The result can now be pickled like any object in zfit!\n",
312350
"(this was not possible before, only after calling `freeze` on the result)\n",
313351
" \n",
314352
"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",
@@ -319,18 +357,30 @@
319357
{
320358
"cell_type": "code",
321359
"execution_count": null,
322-
"id": "25",
360+
"id": "28",
323361
"metadata": {},
324362
"outputs": [],
325363
"source": [
326364
"result_serialized = zfit.dill.dumps(result)\n",
327-
"result_deserialized = zfit.dill.loads(result_serialized)\n",
328-
"result_deserialized.errors()"
365+
"result_deserialized = zfit.dill.loads(result_serialized)"
366+
]
367+
},
368+
{
369+
"cell_type": "code",
370+
"execution_count": null,
371+
"id": "29",
372+
"metadata": {},
373+
"outputs": [],
374+
"source": [
375+
"# the result can be used as before\n",
376+
"result_deserialized.hesse() # the default name is now \"hesse\" and not \"minuit_hesse\"\n",
377+
"result_deserialized.errors() # the default name is now \"errors\" and not \"minuit_minos\"\n",
378+
"print(result_deserialized)"
329379
]
330380
},
331381
{
332382
"cell_type": "markdown",
333-
"id": "26",
383+
"id": "30",
334384
"metadata": {},
335385
"source": [
336386
"## Parameters as arguments\n",
@@ -343,7 +393,7 @@
343393
{
344394
"cell_type": "code",
345395
"execution_count": null,
346-
"id": "27",
396+
"id": "31",
347397
"metadata": {},
348398
"outputs": [],
349399
"source": [
@@ -358,7 +408,7 @@
358408
{
359409
"cell_type": "code",
360410
"execution_count": null,
361-
"id": "28",
411+
"id": "32",
362412
"metadata": {},
363413
"outputs": [],
364414
"source": [
@@ -381,13 +431,24 @@
381431
{
382432
"cell_type": "code",
383433
"execution_count": null,
384-
"id": "29",
434+
"id": "33",
435+
"metadata": {},
436+
"outputs": [],
437+
"source": [
438+
"# a result can also be used as argument for PDFs or, here, for losses\n",
439+
"loss_before = loss.value()\n",
440+
"loss_min = loss.value(params=result_deserialized) # evaluate at minimum\n",
441+
"print(f\"loss before (random from before): {loss_before:.7} minimum value: {loss_min:.7} vs {result_deserialized.fmin:.7}\")"
442+
]
443+
},
444+
{
445+
"cell_type": "code",
446+
"execution_count": null,
447+
"id": "34",
385448
"metadata": {},
386449
"outputs": [],
387450
"source": [
388451
"# creating a PDF looks also different, but here we use the name of the parametrization and the axis (integers)\n",
389-
"\n",
390-
"\n",
391452
"class MyGauss2D(zfit.pdf.ZPDF):\n",
392453
" _PARAMS = (\"mu\", \"sigma\")\n",
393454
" _N_OBS = 2\n",
@@ -404,7 +465,7 @@
404465
{
405466
"cell_type": "code",
406467
"execution_count": null,
407-
"id": "30",
468+
"id": "35",
408469
"metadata": {},
409470
"outputs": [],
410471
"source": [
@@ -415,7 +476,7 @@
415476
{
416477
"cell_type": "code",
417478
"execution_count": null,
418-
"id": "31",
479+
"id": "36",
419480
"metadata": {},
420481
"outputs": [],
421482
"source": []

introduction/upgrade_guide_020.ipynb

+74-13
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"def maximum(x, y):\n",
4040
" return znp.maximum(x, y)\n",
4141
"\n",
42+
"\n",
4243
"print(f\"sqrt with np:{np.sqrt(maximum(1., 2.))}, with z:{znp.sqrt(maximum(1., 2.))}\")\n",
4344
"print(f\"vectorized: {np.sqrt(maximum(znp.array([1., 2.]), znp.array([3., 4.])))}\")"
4445
]
@@ -306,9 +307,46 @@
306307
"id": "24",
307308
"metadata": {},
308309
"source": [
309-
"## Result and serialization\n",
310+
"## Result\n",
311+
"\n",
312+
"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",
310313
"\n",
311-
"The result stays similar but can now be pickled like any object in zfit!\n",
314+
"It can also be used in a context manager to temporarily set the parameters to the minimum and restore them afterwards.\n"
315+
]
316+
},
317+
{
318+
"cell_type": "code",
319+
"execution_count": null,
320+
"id": "25",
321+
"metadata": {},
322+
"outputs": [],
323+
"source": [
324+
"param1again.set_value(1.5)\n",
325+
"with result:\n",
326+
" print(f\"param1 set temporarily to {param1again}\")\n",
327+
"print(f\"param1 is now {param1again} again\")"
328+
]
329+
},
330+
{
331+
"cell_type": "code",
332+
"execution_count": null,
333+
"id": "26",
334+
"metadata": {},
335+
"outputs": [],
336+
"source": [
337+
"# or to set the parameters to the minimum\n",
338+
"zfit.param.set_values(result) # supports also a dict of {param: value}!\n",
339+
"print(param1again)"
340+
]
341+
},
342+
{
343+
"cell_type": "markdown",
344+
"id": "27",
345+
"metadata": {},
346+
"source": [
347+
"## Serialization\n",
348+
"\n",
349+
"The result can now be pickled like any object in zfit!\n",
312350
"(this was not possible before, only after calling `freeze` on the result)\n",
313351
" \n",
314352
"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",
@@ -319,18 +357,30 @@
319357
{
320358
"cell_type": "code",
321359
"execution_count": null,
322-
"id": "25",
360+
"id": "28",
323361
"metadata": {},
324362
"outputs": [],
325363
"source": [
326364
"result_serialized = zfit.dill.dumps(result)\n",
327-
"result_deserialized = zfit.dill.loads(result_serialized)\n",
328-
"result_deserialized.errors()"
365+
"result_deserialized = zfit.dill.loads(result_serialized)"
366+
]
367+
},
368+
{
369+
"cell_type": "code",
370+
"execution_count": null,
371+
"id": "29",
372+
"metadata": {},
373+
"outputs": [],
374+
"source": [
375+
"# the result can be used as before\n",
376+
"result_deserialized.hesse() # the default name is now \"hesse\" and not \"minuit_hesse\"\n",
377+
"result_deserialized.errors() # the default name is now \"errors\" and not \"minuit_minos\"\n",
378+
"print(result_deserialized)"
329379
]
330380
},
331381
{
332382
"cell_type": "markdown",
333-
"id": "26",
383+
"id": "30",
334384
"metadata": {},
335385
"source": [
336386
"## Parameters as arguments\n",
@@ -343,7 +393,7 @@
343393
{
344394
"cell_type": "code",
345395
"execution_count": null,
346-
"id": "27",
396+
"id": "31",
347397
"metadata": {},
348398
"outputs": [],
349399
"source": [
@@ -358,7 +408,7 @@
358408
{
359409
"cell_type": "code",
360410
"execution_count": null,
361-
"id": "28",
411+
"id": "32",
362412
"metadata": {},
363413
"outputs": [],
364414
"source": [
@@ -381,13 +431,24 @@
381431
{
382432
"cell_type": "code",
383433
"execution_count": null,
384-
"id": "29",
434+
"id": "33",
435+
"metadata": {},
436+
"outputs": [],
437+
"source": [
438+
"# a result can also be used as argument for PDFs or, here, for losses\n",
439+
"loss_before = loss.value()\n",
440+
"loss_min = loss.value(params=result_deserialized) # evaluate at minimum\n",
441+
"print(f\"loss before (random from before): {loss_before:.7} minimum value: {loss_min:.7} vs {result_deserialized.fmin:.7}\")"
442+
]
443+
},
444+
{
445+
"cell_type": "code",
446+
"execution_count": null,
447+
"id": "34",
385448
"metadata": {},
386449
"outputs": [],
387450
"source": [
388451
"# creating a PDF looks also different, but here we use the name of the parametrization and the axis (integers)\n",
389-
"\n",
390-
"\n",
391452
"class MyGauss2D(zfit.pdf.ZPDF):\n",
392453
" _PARAMS = (\"mu\", \"sigma\")\n",
393454
" _N_OBS = 2\n",
@@ -404,7 +465,7 @@
404465
{
405466
"cell_type": "code",
406467
"execution_count": null,
407-
"id": "30",
468+
"id": "35",
408469
"metadata": {},
409470
"outputs": [],
410471
"source": [
@@ -415,7 +476,7 @@
415476
{
416477
"cell_type": "code",
417478
"execution_count": null,
418-
"id": "31",
479+
"id": "36",
419480
"metadata": {},
420481
"outputs": [],
421482
"source": []

0 commit comments

Comments
 (0)