Skip to content

Commit

Permalink
added bloptools example to documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaswmorris committed Oct 26, 2023
1 parent 39a7d09 commit 0773698
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/source/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ Examples
notebooks/srw.ipynb
notebooks/shadow.ipynb
notebooks/madx.ipynb
notebooks/optimization.ipynb
168 changes: 168 additions & 0 deletions docs/source/notebooks/optimization.ipynb
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion sirepo_bluesky/sirepo_ophyd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 0773698

Please sign in to comment.