Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bloptools example notebook to documentation #149

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
166 changes: 166 additions & 0 deletions docs/source/notebooks/optimization.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
{
"cells": [
{
"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": {
"tags": []
},
"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",
" limits=(-5e-2, +5e-2),\n",
" tags=[\"kb\", \"kbv\"],\n",
" units=\"deg\",\n",
" ),\n",
" DOF(\n",
" device=kbv.x_rot,\n",
" limits=(-5e-2, +5e-2),\n",
" tags=[\"kb\", \"kbv\"],\n",
" units=\"deg\",\n",
" ),\n",
"]\n",
"\n",
"# composite\n",
"objectives = [\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",
"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": {
"tags": []
},
"outputs": [],
"source": [
"RE(agent.learn(acq_func=\"qr\", n=16))\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": {
"tags": []
},
"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": {
"tags": []
},
"outputs": [],
"source": [
"agent.plot_history()"
]
}
],
"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.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ pandoc
sphinx
sphinx-copybutton
tabulate>=0.9.0
bloptools>=0.5.0
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
Loading