Skip to content

Commit c150ced

Browse files
authored
Merge pull request #102 from jennmald/xrt_demo
Add XRT demo to blop tutorials
2 parents 6e9b0f6 + 8b820f2 commit c150ced

File tree

7 files changed

+631
-2
lines changed

7 files changed

+631
-2
lines changed

.github/workflows/docs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ jobs:
7777
set -vxeo pipefail
7878
7979
pip install --upgrade pip wheel
80-
pip install -v .[dev]
80+
pip install -v .[dev,xrt]
8181
pip list
8282
8383
- name: Build Docs

docs/source/tutorials.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ Tutorials
99
tutorials/pareto-fronts.ipynb
1010
tutorials/passive-dofs.ipynb
1111
tutorials/kb-mirrors.ipynb
12+
tutorials/xrt-blop-demo.ipynb

docs/source/tutorials/kb-mirrors.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"id": "0",
66
"metadata": {},
77
"source": [
8-
"# Hyperparameters\n",
8+
"# KB Mirrors\n",
99
"\n",
1010
"This example simulates the alignment of a KB mirror endstation (with four degrees of freedom)."
1111
]
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "0",
6+
"metadata": {},
7+
"source": [
8+
"# XRT Blop Demo"
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"id": "1",
14+
"metadata": {},
15+
"source": [
16+
"For ophyd beamline setup see: https://github.com/NSLS-II/blop/blob/main/src/blop/sim/xrt_beamline.py and https://github.com/NSLS-II/blop/blob/main/src/blop/sim/xrt_kb_model.py"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": null,
22+
"id": "2",
23+
"metadata": {},
24+
"outputs": [],
25+
"source": [
26+
"from blop.utils import prepare_re_env # noqa\n",
27+
"%run -i $prepare_re_env.__file__ --db-type=temp\n",
28+
"bec.disable_plots()"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": null,
34+
"id": "3",
35+
"metadata": {},
36+
"outputs": [],
37+
"source": [
38+
"import sys, os\n",
39+
"from matplotlib import pyplot as plt\n",
40+
"from blop.sim.xrt_beamline import Beamline\n",
41+
"\n",
42+
"from blop import DOF, Objective, Agent\n",
43+
"from blop.digestion import beam_stats_digestion\n",
44+
"import time"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": null,
50+
"id": "4",
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
54+
"plt.ion()\n",
55+
"\n",
56+
"h_opt = 0\n",
57+
"dh = 5\n",
58+
"\n",
59+
"R1, dR1 = 40000, 10000\n",
60+
"R2, dR2 = 20000, 10000"
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": null,
66+
"id": "5",
67+
"metadata": {},
68+
"outputs": [],
69+
"source": [
70+
"beamline = Beamline(name=\"bl\")\n",
71+
"time.sleep(1)\n",
72+
"dofs = [\n",
73+
" DOF(description=\"KBV R\",\n",
74+
" device=beamline.kbv_dsv,\n",
75+
" search_domain=(R1-dR1, R1+dR1)),\n",
76+
" DOF(description=\"KBH R\",\n",
77+
" device=beamline.kbh_dsh,\n",
78+
" search_domain=(R2-dR2, R2+dR2)),\n",
79+
"\n",
80+
"]"
81+
]
82+
},
83+
{
84+
"cell_type": "code",
85+
"execution_count": null,
86+
"id": "6",
87+
"metadata": {},
88+
"outputs": [],
89+
"source": [
90+
"objectives = [\n",
91+
" Objective(name=\"bl_det_sum\", \n",
92+
" target=\"max\",\n",
93+
" transform=\"log\",\n",
94+
" trust_domain=(20, 1e12)),\n",
95+
"\n",
96+
" Objective(name=\"bl_det_wid_x\",\n",
97+
" target=\"min\",\n",
98+
" transform=\"log\",\n",
99+
" # trust_domain=(0, 1e12),\n",
100+
" latent_groups=[(\"bl_kbh_dsh\", \"bl_kbv_dsv\")]),\n",
101+
" Objective(name=\"bl_det_wid_y\",\n",
102+
" target=\"min\",\n",
103+
" transform=\"log\",\n",
104+
" # trust_domain=(0, 1e12),\n",
105+
" latent_groups=[(\"bl_kbh_dsh\", \"bl_kbv_dsv\")]),\n",
106+
"]"
107+
]
108+
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": null,
112+
"id": "7",
113+
"metadata": {},
114+
"outputs": [],
115+
"source": [
116+
"agent = Agent(\n",
117+
" dofs=dofs,\n",
118+
" objectives=objectives,\n",
119+
" detectors=[beamline.det],\n",
120+
" digestion=beam_stats_digestion,\n",
121+
" digestion_kwargs={\"image_key\": \"bl_det_image\"},\n",
122+
" verbose=True,\n",
123+
" db=db,\n",
124+
" tolerate_acquisition_errors=False,\n",
125+
" enforce_all_objectives_valid=True,\n",
126+
" train_every=3,\n",
127+
")"
128+
]
129+
},
130+
{
131+
"cell_type": "code",
132+
"execution_count": null,
133+
"id": "8",
134+
"metadata": {},
135+
"outputs": [],
136+
"source": [
137+
"RE(agent.learn(\"qr\", n=16))\n",
138+
"RE(agent.learn(\"qei\", n=16, iterations=4))\n"
139+
]
140+
},
141+
{
142+
"cell_type": "code",
143+
"execution_count": null,
144+
"id": "9",
145+
"metadata": {},
146+
"outputs": [],
147+
"source": [
148+
"agent.plot_objectives(axes=(0, 1))"
149+
]
150+
}
151+
],
152+
"metadata": {
153+
"kernelspec": {
154+
"display_name": "xrt-blop",
155+
"language": "python",
156+
"name": "python3"
157+
},
158+
"language_info": {
159+
"codemirror_mode": {
160+
"name": "ipython",
161+
"version": 3
162+
},
163+
"file_extension": ".py",
164+
"mimetype": "text/x-python",
165+
"name": "python",
166+
"nbconvert_exporter": "python",
167+
"pygments_lexer": "ipython3",
168+
"version": "3.11.0"
169+
}
170+
},
171+
"nbformat": 4,
172+
"nbformat_minor": 5
173+
}

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pre-commit = [
5050
"nbstripout",
5151
]
5252
adaptive = ["bluesky-adaptive"]
53+
xrt = ["xrt"]
5354

5455
dev = [
5556
"pytest-codecov",

0 commit comments

Comments
 (0)