diff --git a/_website/tutorials/introduction/upgrade_guide_020.ipynb b/_website/tutorials/introduction/upgrade_guide_020.ipynb index 9961b29..5a1b103 100644 --- a/_website/tutorials/introduction/upgrade_guide_020.ipynb +++ b/_website/tutorials/introduction/upgrade_guide_020.ipynb @@ -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.])))}\")" ] @@ -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", @@ -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", @@ -343,7 +393,7 @@ { "cell_type": "code", "execution_count": null, - "id": "27", + "id": "31", "metadata": {}, "outputs": [], "source": [ @@ -358,7 +408,7 @@ { "cell_type": "code", "execution_count": null, - "id": "28", + "id": "32", "metadata": {}, "outputs": [], "source": [ @@ -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", @@ -404,7 +465,7 @@ { "cell_type": "code", "execution_count": null, - "id": "30", + "id": "35", "metadata": {}, "outputs": [], "source": [ @@ -415,7 +476,7 @@ { "cell_type": "code", "execution_count": null, - "id": "31", + "id": "36", "metadata": {}, "outputs": [], "source": [] diff --git a/introduction/upgrade_guide_020.ipynb b/introduction/upgrade_guide_020.ipynb index 9961b29..5a1b103 100644 --- a/introduction/upgrade_guide_020.ipynb +++ b/introduction/upgrade_guide_020.ipynb @@ -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.])))}\")" ] @@ -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", @@ -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", @@ -343,7 +393,7 @@ { "cell_type": "code", "execution_count": null, - "id": "27", + "id": "31", "metadata": {}, "outputs": [], "source": [ @@ -358,7 +408,7 @@ { "cell_type": "code", "execution_count": null, - "id": "28", + "id": "32", "metadata": {}, "outputs": [], "source": [ @@ -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", @@ -404,7 +465,7 @@ { "cell_type": "code", "execution_count": null, - "id": "30", + "id": "35", "metadata": {}, "outputs": [], "source": [ @@ -415,7 +476,7 @@ { "cell_type": "code", "execution_count": null, - "id": "31", + "id": "36", "metadata": {}, "outputs": [], "source": []