From 077369869ad4274127567007f831f2e465502dab Mon Sep 17 00:00:00 2001 From: Thomas Morris Date: Thu, 26 Oct 2023 11:34:26 -0400 Subject: [PATCH 1/3] added bloptools example to documentation --- docs/source/examples.rst | 1 + docs/source/notebooks/optimization.ipynb | 168 +++++++++++++++++++++++ sirepo_bluesky/sirepo_ophyd.py | 2 +- 3 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 docs/source/notebooks/optimization.ipynb diff --git a/docs/source/examples.rst b/docs/source/examples.rst index 0a0073f..daa6b9c 100644 --- a/docs/source/examples.rst +++ b/docs/source/examples.rst @@ -10,3 +10,4 @@ Examples notebooks/srw.ipynb notebooks/shadow.ipynb notebooks/madx.ipynb + notebooks/optimization.ipynb diff --git a/docs/source/notebooks/optimization.ipynb b/docs/source/notebooks/optimization.ipynb new file mode 100644 index 0000000..1908c52 --- /dev/null +++ b/docs/source/notebooks/optimization.ipynb @@ -0,0 +1,168 @@ +{ + "cells": [ + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Optimize the beamline\n", + "\n", + "In this example, we use the `bloptools` package to optimize the beamline.\n", + "\n", + "We set up the simulation as we normally do:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from sirepo_bluesky import prepare_re_env\n", + "\n", + "%run -i $prepare_re_env.__file__\n", + "\n", + "from sirepo_bluesky.sirepo_bluesky import SirepoBluesky\n", + "from sirepo_bluesky.sirepo_ophyd import create_classes\n", + "\n", + "connection = SirepoBluesky(\"http://localhost:8000\")\n", + "\n", + "data, schema = connection.auth(\"shadow\", sim_id=\"00000002\")\n", + "classes, objects = create_classes(connection=connection)\n", + "globals().update(**objects)\n", + "\n", + "bec.disable_plots()\n", + "\n", + "aperture.horizontalSize.kind = \"hinted\"\n", + "w9.duration.kind = \"hinted\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can then instantiate an `Agent` from the `bloptools` package; because it can run any experiment defined with a Bluesky plan, we can give it any of our Sirepo components to use as degrees of freedom. We define the objectives as to maximize the flux density of the beamline." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "from bloptools.bayesian import Agent, DOF, Objective\n", + "\n", + "dofs = [\n", + " DOF(\n", + " device=kbh.x_rot,\n", + " name=\"KBH rotation offset\",\n", + " limits=(-5e-2, +5e-2),\n", + " tags=[\"kb\", \"kbv\"],\n", + " units=\"deg\",\n", + " ),\n", + " DOF(\n", + " device=kbv.x_rot,\n", + " name=\"KBV rotation offset\",\n", + " limits=(-5e-2, +5e-2),\n", + " tags=[\"kb\", \"kbv\"],\n", + " units=\"deg\",\n", + " ),\n", + "]\n", + "\n", + "# composite\n", + "objectives = [\n", + " Objective(key=\"w9_flux\", name=\"total flux\", limits=(100, np.inf), log=True),\n", + " Objective(key=\"w9_fwhm_x\", name=\"horizontal FWHM\", minimize=True, log=True),\n", + " Objective(key=\"w9_fwhm_y\", name=\"vertical FWHM\", minimize=True, log=True),\n", + "]\n", + "\n", + "\n", + "agent = Agent(\n", + " dofs=dofs,\n", + " dets=[w9],\n", + " objectives=objectives,\n", + " db=db,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We start by running a scan over a quasi-random sample of input parameters, and plot the result:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "RE(agent.learn(acq_func=\"qr\", n=24))\n", + "agent.plot_objectives()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We then use a more intelligent strategy (\"$q$-expected mean\") to narrow in on the optimum." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "RE(agent.learn(\"qem\", iterations=4))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can see the convergence of our model:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "agent.plot_history()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.12" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/sirepo_bluesky/sirepo_ophyd.py b/sirepo_bluesky/sirepo_ophyd.py index 289fd2e..3fb79f1 100644 --- a/sirepo_bluesky/sirepo_ophyd.py +++ b/sirepo_bluesky/sirepo_ophyd.py @@ -336,7 +336,7 @@ def set(self, value): # want to make sure the crl element is updated properly when parameters are changed. ret.pop("state") # Update crl element - for cpt in ["absoluteFocusPosition", "focalDistance"]: + for cpt in ["absoluteFocusPosition", "focalDistance", "photonEnergy"]: getattr(self.parent, cpt).put(ret[cpt]) return NullStatus() From eecec22ea5af90b4ce7f3600eb649900dd3afc36 Mon Sep 17 00:00:00 2001 From: Max Rakitin Date: Thu, 9 Nov 2023 12:02:41 -0500 Subject: [PATCH 2/3] Add `bloptools` to the dev requirements --- requirements-dev.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements-dev.txt b/requirements-dev.txt index 667eb4a..e788819 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -19,3 +19,4 @@ pandoc sphinx sphinx-copybutton tabulate>=0.9.0 +bloptools>=0.5.0 From 89054cbdfe033aed7a2273040226affe45f36d42 Mon Sep 17 00:00:00 2001 From: Thomas Morris Date: Mon, 27 Nov 2023 13:04:33 -0500 Subject: [PATCH 3/3] make docs/source/notebooks compatible with blop --- docs/source/notebooks/optimization.ipynb | 36 +++++++++++------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/docs/source/notebooks/optimization.ipynb b/docs/source/notebooks/optimization.ipynb index 1908c52..974d0cc 100644 --- a/docs/source/notebooks/optimization.ipynb +++ b/docs/source/notebooks/optimization.ipynb @@ -1,7 +1,6 @@ { "cells": [ { - "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -15,7 +14,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [], "source": [ "from sirepo_bluesky import prepare_re_env\n", @@ -57,14 +58,12 @@ "dofs = [\n", " DOF(\n", " device=kbh.x_rot,\n", - " name=\"KBH rotation offset\",\n", " limits=(-5e-2, +5e-2),\n", " tags=[\"kb\", \"kbv\"],\n", " units=\"deg\",\n", " ),\n", " DOF(\n", " device=kbv.x_rot,\n", - " name=\"KBV rotation offset\",\n", " limits=(-5e-2, +5e-2),\n", " tags=[\"kb\", \"kbv\"],\n", " units=\"deg\",\n", @@ -73,9 +72,9 @@ "\n", "# composite\n", "objectives = [\n", - " Objective(key=\"w9_flux\", name=\"total flux\", limits=(100, np.inf), log=True),\n", - " Objective(key=\"w9_fwhm_x\", name=\"horizontal FWHM\", minimize=True, log=True),\n", - " Objective(key=\"w9_fwhm_y\", name=\"vertical FWHM\", minimize=True, log=True),\n", + " Objective(name=\"w9_flux\", description=\"total flux\", target=\"max\", log=True),\n", + " Objective(name=\"w9_fwhm_x\", description=\"horizontal FWHM\", target=\"min\", log=True),\n", + " Objective(name=\"w9_fwhm_y\", description=\"vertical FWHM\", target=\"min\", log=True),\n", "]\n", "\n", "\n", @@ -97,10 +96,12 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [], "source": [ - "RE(agent.learn(acq_func=\"qr\", n=24))\n", + "RE(agent.learn(acq_func=\"qr\", n=16))\n", "agent.plot_objectives()" ] }, @@ -114,7 +115,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [], "source": [ "RE(agent.learn(\"qem\", iterations=4))" @@ -130,18 +133,13 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [], "source": [ "agent.plot_history()" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { @@ -160,7 +158,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.12" + "version": "3.10.0" } }, "nbformat": 4,