Add optimize parameter example
Browse files
app.py
CHANGED
@@ -1,469 +1,822 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import marimo
|
2 |
|
3 |
-
__generated_with = "0.
|
4 |
-
app = marimo.App()
|
5 |
|
6 |
|
7 |
@app.cell
|
8 |
-
def
|
|
|
|
|
|
|
|
|
9 |
import marimo as mo
|
10 |
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
|
15 |
@app.cell
|
16 |
-
def
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
19 |
|
20 |
|
21 |
@app.cell
|
22 |
-
def
|
23 |
-
mo.
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
This means that unlike traditional notebooks, marimo notebooks **run
|
28 |
-
automatically** when you modify them or
|
29 |
-
interact with UI elements, like this slider: {slider}.
|
30 |
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
)
|
34 |
-
return
|
35 |
|
36 |
|
37 |
-
@app.cell
|
38 |
-
def
|
39 |
-
mo.
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
-
"Runtime > On Cell Change" to "lazy".
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
)
|
56 |
-
return
|
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
|
60 |
-
def __(mo):
|
61 |
-
mo.md(
|
62 |
-
"""
|
63 |
-
Tip: This is a tutorial notebook. You can create your own notebooks
|
64 |
-
by entering `marimo edit` at the command line.
|
65 |
-
"""
|
66 |
-
).callout()
|
67 |
-
return
|
68 |
|
|
|
|
|
69 |
|
70 |
-
@app.cell(hide_code=True)
|
71 |
-
def __(mo):
|
72 |
-
mo.md(
|
73 |
-
"""
|
74 |
-
## 1. Reactive execution
|
75 |
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
-
marimo reads your cells and models the dependencies among them: whenever
|
80 |
-
a cell that defines a global variable is run, marimo
|
81 |
-
**automatically runs** all cells that reference that variable.
|
82 |
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
)
|
88 |
-
return
|
89 |
|
90 |
|
91 |
-
@app.cell
|
92 |
-
def
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
)
|
115 |
-
return
|
116 |
|
117 |
|
118 |
@app.cell
|
119 |
-
def
|
120 |
-
|
121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
|
|
|
|
|
123 |
|
124 |
-
@app.cell(hide_code=True)
|
125 |
-
def __(mo):
|
126 |
-
mo.accordion(
|
127 |
-
{
|
128 |
-
"Tip: execution order": (
|
129 |
-
"""
|
130 |
-
The order of cells on the page has no bearing on
|
131 |
-
the order in which cells are executed: marimo knows that a cell
|
132 |
-
reading a variable must run after the cell that defines it. This
|
133 |
-
frees you to organize your code in the way that makes the most
|
134 |
-
sense for you.
|
135 |
-
"""
|
136 |
-
)
|
137 |
-
}
|
138 |
-
)
|
139 |
-
return
|
140 |
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
)
|
151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
|
153 |
|
154 |
-
@app.cell
|
155 |
-
def
|
156 |
-
mo.
|
157 |
-
{
|
158 |
-
"Tip: encapsulation": (
|
159 |
-
"""
|
160 |
-
By encapsulating logic in functions, classes, or Python modules,
|
161 |
-
you can minimize the number of global variables in your notebook.
|
162 |
-
"""
|
163 |
-
)
|
164 |
-
}
|
165 |
-
)
|
166 |
-
return
|
167 |
-
|
168 |
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
"Tip: private variables": (
|
174 |
-
"""
|
175 |
-
Variables prefixed with an underscore are "private" to a cell, so
|
176 |
-
they can be defined by multiple cells.
|
177 |
-
"""
|
178 |
-
)
|
179 |
-
}
|
180 |
)
|
181 |
return
|
182 |
|
183 |
|
184 |
-
@app.cell
|
185 |
-
def
|
186 |
-
|
187 |
-
|
188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
|
190 |
-
Cells can output interactive UI elements. Interacting with a UI
|
191 |
-
element **automatically triggers notebook execution**: when
|
192 |
-
you interact with a UI element, its value is sent back to Python, and
|
193 |
-
every cell that references that element is re-run.
|
194 |
|
195 |
-
|
196 |
-
|
197 |
-
|
|
|
|
|
|
|
198 |
)
|
199 |
return
|
200 |
|
201 |
|
202 |
@app.cell
|
203 |
-
def
|
204 |
-
mo.
|
205 |
-
|
|
|
206 |
|
207 |
|
208 |
@app.cell
|
209 |
-
def
|
210 |
-
|
211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
212 |
|
213 |
|
214 |
@app.cell
|
215 |
-
def
|
216 |
-
|
217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
218 |
|
219 |
|
220 |
@app.cell
|
221 |
-
def
|
222 |
-
|
223 |
-
|
|
|
|
|
|
|
|
|
224 |
|
225 |
|
226 |
@app.cell
|
227 |
-
def
|
228 |
-
|
|
|
229 |
return
|
230 |
|
231 |
|
232 |
-
@app.cell
|
233 |
-
def
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
251 |
)
|
252 |
-
return
|
253 |
|
254 |
|
255 |
-
@app.cell
|
256 |
-
def
|
257 |
-
|
258 |
-
|
259 |
-
|
|
|
|
|
|
|
|
|
|
|
260 |
|
261 |
-
marimo notebooks can double as apps. Click the app window icon in the
|
262 |
-
bottom-right to see this notebook in "app view."
|
263 |
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
268 |
)
|
269 |
-
return
|
270 |
-
|
271 |
|
272 |
-
@app.cell(hide_code=True)
|
273 |
-
def __(mo):
|
274 |
-
mo.md(
|
275 |
-
"""
|
276 |
-
## 5. The `marimo` command-line tool
|
277 |
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
286 |
|
287 |
|
288 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
289 |
|
290 |
-
```
|
291 |
-
marimo run notebook.py
|
292 |
-
```
|
293 |
|
294 |
-
|
295 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
296 |
|
297 |
-
**Convert a Jupyter notebook.** Convert a Jupyter notebook to a marimo
|
298 |
-
notebook using `marimo convert`:
|
299 |
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
|
304 |
-
|
|
|
|
|
|
|
305 |
|
306 |
-
- `dataflow`: more on marimo's automatic execution
|
307 |
-
- `ui`: how to use UI elements
|
308 |
-
- `markdown`: how to write markdown, with interpolated values and
|
309 |
-
LaTeX
|
310 |
-
- `plots`: how plotting works in marimo
|
311 |
-
- `sql`: how to use SQL
|
312 |
-
- `layout`: layout elements in marimo
|
313 |
-
- `fileformat`: how marimo's file format works
|
314 |
-
- `markdown-format`: for using `.md` files in marimo
|
315 |
-
- `for-jupyter-users`: if you are coming from Jupyter
|
316 |
|
317 |
-
|
|
|
|
|
|
|
318 |
|
319 |
-
```
|
320 |
-
marimo tutorial dataflow
|
321 |
-
```
|
322 |
|
323 |
-
|
324 |
-
|
325 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
326 |
)
|
327 |
-
return
|
328 |
|
329 |
|
330 |
-
@app.cell
|
331 |
-
def
|
332 |
-
|
333 |
-
"""
|
334 |
-
## 6. The marimo editor
|
335 |
|
336 |
-
|
337 |
-
|
338 |
-
)
|
339 |
-
return
|
340 |
|
341 |
|
342 |
@app.cell
|
343 |
-
def
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
@app.cell(hide_code=True)
|
349 |
-
def __(mo):
|
350 |
-
mo.md("""## Finally, a fun fact""")
|
351 |
-
return
|
352 |
|
353 |
|
354 |
-
@app.cell
|
355 |
-
def
|
356 |
-
mo.
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
beloved assemblages are greater than the sum of their parts.
|
362 |
-
"""
|
363 |
)
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
@app.cell(hide_code=True)
|
368 |
-
def __():
|
369 |
-
tips = {
|
370 |
-
"Saving": (
|
371 |
-
"""
|
372 |
-
**Saving**
|
373 |
-
|
374 |
-
- _Name_ your app using the box at the top of the screen, or
|
375 |
-
with `Ctrl/Cmd+s`. You can also create a named app at the
|
376 |
-
command line, e.g., `marimo edit app_name.py`.
|
377 |
-
|
378 |
-
- _Save_ by clicking the save icon on the bottom right, or by
|
379 |
-
inputting `Ctrl/Cmd+s`. By default marimo is configured
|
380 |
-
to autosave.
|
381 |
-
"""
|
382 |
-
),
|
383 |
-
"Running": (
|
384 |
-
"""
|
385 |
-
1. _Run a cell_ by clicking the play ( β· ) button on the top
|
386 |
-
right of a cell, or by inputting `Ctrl/Cmd+Enter`.
|
387 |
-
|
388 |
-
2. _Run a stale cell_ by clicking the yellow run button on the
|
389 |
-
right of the cell, or by inputting `Ctrl/Cmd+Enter`. A cell is
|
390 |
-
stale when its code has been modified but not run.
|
391 |
-
|
392 |
-
3. _Run all stale cells_ by clicking the play ( β· ) button on
|
393 |
-
the bottom right of the screen, or input `Ctrl/Cmd+Shift+r`.
|
394 |
-
"""
|
395 |
-
),
|
396 |
-
"Console Output": (
|
397 |
-
"""
|
398 |
-
Console output (e.g., `print()` statements) is shown below a
|
399 |
-
cell.
|
400 |
-
"""
|
401 |
-
),
|
402 |
-
"Creating, Moving, and Deleting Cells": (
|
403 |
-
"""
|
404 |
-
1. _Create_ a new cell above or below a given one by clicking
|
405 |
-
the plus button to the left of the cell, which appears on
|
406 |
-
mouse hover.
|
407 |
-
|
408 |
-
2. _Move_ a cell up or down by dragging on the handle to the
|
409 |
-
right of the cell, which appears on mouse hover.
|
410 |
-
|
411 |
-
3. _Delete_ a cell by clicking the trash bin icon. Bring it
|
412 |
-
back by clicking the undo button on the bottom right of the
|
413 |
-
screen, or with `Ctrl/Cmd+Shift+z`.
|
414 |
-
"""
|
415 |
-
),
|
416 |
-
"Disabling Automatic Execution": (
|
417 |
-
"""
|
418 |
-
Via the notebook settings (gear icon) or footer panel, you
|
419 |
-
can disable automatic execution. This is helpful when
|
420 |
-
working with expensive notebooks or notebooks that have
|
421 |
-
side-effects like database transactions.
|
422 |
-
"""
|
423 |
-
),
|
424 |
-
"Disabling Cells": (
|
425 |
-
"""
|
426 |
-
You can disable a cell via the cell context menu.
|
427 |
-
marimo will never run a disabled cell or any cells that depend on it.
|
428 |
-
This can help prevent accidental execution of expensive computations
|
429 |
-
when editing a notebook.
|
430 |
-
"""
|
431 |
-
),
|
432 |
-
"Code Folding": (
|
433 |
-
"""
|
434 |
-
You can collapse or fold the code in a cell by clicking the arrow
|
435 |
-
icons in the line number column to the left, or by using keyboard
|
436 |
-
shortcuts.
|
437 |
-
|
438 |
-
Use the command palette (`Ctrl/Cmd+k`) or a keyboard shortcut to
|
439 |
-
quickly fold or unfold all cells.
|
440 |
-
"""
|
441 |
-
),
|
442 |
-
"Code Formatting": (
|
443 |
-
"""
|
444 |
-
If you have [ruff](https://github.com/astral-sh/ruff) installed,
|
445 |
-
you can format a cell with the keyboard shortcut `Ctrl/Cmd+b`.
|
446 |
-
"""
|
447 |
-
),
|
448 |
-
"Command Palette": (
|
449 |
-
"""
|
450 |
-
Use `Ctrl/Cmd+k` to open the command palette.
|
451 |
-
"""
|
452 |
-
),
|
453 |
-
"Keyboard Shortcuts": (
|
454 |
-
"""
|
455 |
-
Open the notebook menu (top-right) or input `Ctrl/Cmd+Shift+h` to
|
456 |
-
view a list of all keyboard shortcuts.
|
457 |
-
"""
|
458 |
-
),
|
459 |
-
"Configuration": (
|
460 |
-
"""
|
461 |
-
Configure the editor by clicking the gears icon near the top-right
|
462 |
-
of the screen.
|
463 |
-
"""
|
464 |
-
),
|
465 |
-
}
|
466 |
-
return (tips,)
|
467 |
|
468 |
|
469 |
if __name__ == "__main__":
|
|
|
1 |
+
"""
|
2 |
+
Copyright (c) 2024, UChicago Argonne, LLC. All rights reserved.
|
3 |
+
|
4 |
+
Copyright 2024. UChicago Argonne, LLC. This software was produced
|
5 |
+
under U.S. Government contract DE-AC02-06CH11357 for Argonne National
|
6 |
+
Laboratory (ANL), which is operated by UChicago Argonne, LLC for the
|
7 |
+
U.S. Department of Energy. The U.S. Government has rights to use,
|
8 |
+
reproduce, and distribute this software. NEITHER THE GOVERNMENT NOR
|
9 |
+
UChicago Argonne, LLC MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR
|
10 |
+
ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE. If software is
|
11 |
+
modified to produce derivative works, such modified software should
|
12 |
+
be clearly marked, so as not to confuse it with the version available
|
13 |
+
from ANL.
|
14 |
+
|
15 |
+
Additionally, redistribution and use in source and binary forms, with
|
16 |
+
or without modification, are permitted provided that the following
|
17 |
+
conditions are met:
|
18 |
+
|
19 |
+
* Redistributions of source code must retain the above copyright
|
20 |
+
notice, this list of conditions and the following disclaimer.
|
21 |
+
|
22 |
+
* Redistributions in binary form must reproduce the above copyright
|
23 |
+
notice, this list of conditions and the following disclaimer in
|
24 |
+
the documentation and/or other materials provided with the
|
25 |
+
distribution.
|
26 |
+
|
27 |
+
* Neither the name of UChicago Argonne, LLC, Argonne National
|
28 |
+
Laboratory, ANL, the U.S. Government, nor the names of its
|
29 |
+
contributors may be used to endorse or promote products derived
|
30 |
+
from this software without specific prior written permission.
|
31 |
+
|
32 |
+
THIS SOFTWARE IS PROVIDED BY UChicago Argonne, LLC AND CONTRIBUTORS
|
33 |
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
34 |
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
35 |
+
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL UChicago
|
36 |
+
Argonne, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
37 |
+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
38 |
+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
39 |
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
40 |
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
41 |
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
42 |
+
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
43 |
+
POSSIBILITY OF SUCH DAMAGE.
|
44 |
+
"""
|
45 |
+
|
46 |
+
### Initial Author <2024>: Xiangyu Yin
|
47 |
+
|
48 |
import marimo
|
49 |
|
50 |
+
__generated_with = "0.10.2"
|
51 |
+
app = marimo.App(width="medium")
|
52 |
|
53 |
|
54 |
@app.cell
|
55 |
+
def _(__file__):
|
56 |
+
import sys
|
57 |
+
from math import floor, ceil, acos, pi
|
58 |
+
import numpy as np
|
59 |
+
import plotly.express as px
|
60 |
import marimo as mo
|
61 |
|
62 |
+
from mapstorch.io import read_dataset
|
63 |
+
from mapstorch.util import PeriodicTableWidget
|
64 |
+
from mapstorch.default import (
|
65 |
+
default_fitting_elems,
|
66 |
+
unsupported_elements,
|
67 |
+
supported_elements_mapping,
|
68 |
+
)
|
69 |
+
|
70 |
+
return (
|
71 |
+
PeriodicTableWidget,
|
72 |
+
acos,
|
73 |
+
ceil,
|
74 |
+
default_fitting_elems,
|
75 |
+
floor,
|
76 |
+
mo,
|
77 |
+
np,
|
78 |
+
pi,
|
79 |
+
px,
|
80 |
+
read_dataset,
|
81 |
+
supported_elements_mapping,
|
82 |
+
sys,
|
83 |
+
unsupported_elements,
|
84 |
+
)
|
85 |
|
86 |
|
87 |
@app.cell
|
88 |
+
def _(mo):
|
89 |
+
dataset = mo.ui.file_browser(
|
90 |
+
filetypes=[".h5", ".h50", ".h51", ".h52", ".h53", ".h54", ".h55"],
|
91 |
+
multiple=False,
|
92 |
+
)
|
93 |
+
mo.md(f"Please select the dataset file (h5 file) \n{dataset}")
|
94 |
+
return (dataset,)
|
95 |
|
96 |
|
97 |
@app.cell
|
98 |
+
def _(mo):
|
99 |
+
int_spec_path = mo.ui.dropdown(
|
100 |
+
["MAPS/int_spec", "MAPS/Spectra/Integrateds_Spectra/Spectra"],
|
101 |
+
value="MAPS/int_spec",
|
102 |
+
label="Integrated spectrum location",
|
103 |
+
)
|
104 |
+
elem_path = mo.ui.dropdown(
|
105 |
+
["MAPS/channel_names"],
|
106 |
+
value="MAPS/channel_names",
|
107 |
+
label="Energy channel names location",
|
108 |
+
)
|
109 |
+
dataset_button = mo.ui.run_button(label="Load")
|
110 |
+
mo.hstack(
|
111 |
+
[int_spec_path, elem_path, dataset_button], justify="start", gap=1
|
112 |
+
).right()
|
113 |
+
return dataset_button, elem_path, int_spec_path
|
114 |
|
|
|
|
|
|
|
115 |
|
116 |
+
@app.cell
|
117 |
+
def _(int_spec_og, mo):
|
118 |
+
energy_range = mo.ui.range_slider(
|
119 |
+
start=0,
|
120 |
+
stop=int_spec_og.shape[-1] - 1,
|
121 |
+
step=1,
|
122 |
+
label="Energy range",
|
123 |
+
value=[50, 1450],
|
124 |
+
full_width=True,
|
125 |
)
|
126 |
+
return (energy_range,)
|
127 |
|
128 |
|
129 |
+
@app.cell
|
130 |
+
def _(energy_range, int_spec_og, mo, peaks):
|
131 |
+
incident_energy_slider = mo.ui.slider(
|
132 |
+
start=6,
|
133 |
+
stop=18,
|
134 |
+
step=0.01,
|
135 |
+
value=12,
|
136 |
+
label="Incident Energy (keV)",
|
137 |
+
full_width=True,
|
138 |
+
)
|
139 |
+
compton_peak_value = (
|
140 |
+
(int_spec_og.shape[-1] - 1) // 2 if len(peaks) < 8 else peaks[-2]
|
141 |
+
)
|
142 |
+
compton_peak_slider = mo.ui.slider(
|
143 |
+
start=0,
|
144 |
+
stop=int_spec_og.shape[-1] - 1,
|
145 |
+
step=1,
|
146 |
+
value=compton_peak_value,
|
147 |
+
label="Compton Peak Position",
|
148 |
+
full_width=True,
|
149 |
+
)
|
150 |
+
elastic_peak_value = (
|
151 |
+
(int_spec_og.shape[-1] - 1) // 1.9 if len(peaks) < 8 else peaks[-1]
|
152 |
+
)
|
153 |
+
elastic_peak_slider = mo.ui.slider(
|
154 |
+
start=0,
|
155 |
+
stop=int_spec_og.shape[-1] - 1,
|
156 |
+
step=1,
|
157 |
+
value=elastic_peak_value,
|
158 |
+
label="Elastic Peak Position",
|
159 |
+
full_width=True,
|
160 |
+
)
|
161 |
+
mo.vstack(
|
162 |
+
[incident_energy_slider, energy_range, compton_peak_slider, elastic_peak_slider]
|
163 |
+
)
|
164 |
+
return (
|
165 |
+
compton_peak_slider,
|
166 |
+
compton_peak_value,
|
167 |
+
elastic_peak_slider,
|
168 |
+
elastic_peak_value,
|
169 |
+
incident_energy_slider,
|
170 |
+
)
|
171 |
|
|
|
172 |
|
173 |
+
@app.cell
|
174 |
+
def _(
|
175 |
+
compton_peak_slider,
|
176 |
+
elastic_peak_slider,
|
177 |
+
int_spec,
|
178 |
+
int_spec_log,
|
179 |
+
mo,
|
180 |
+
np,
|
181 |
+
peaks,
|
182 |
+
):
|
183 |
+
from plotly.subplots import make_subplots
|
184 |
+
import plotly.graph_objects as go
|
185 |
+
|
186 |
+
int_spec_fig = make_subplots(rows=2, cols=1)
|
187 |
+
|
188 |
+
# Add trace for the 1D data
|
189 |
+
int_spec_fig.append_trace(
|
190 |
+
go.Scatter(
|
191 |
+
x=np.arange(len(int_spec)), y=int_spec, mode="lines", name="Photon counts"
|
192 |
+
),
|
193 |
+
row=1,
|
194 |
+
col=1,
|
195 |
+
)
|
196 |
+
int_spec_fig.append_trace(
|
197 |
+
go.Scatter(
|
198 |
+
x=np.arange(len(int_spec)), y=int_spec_log, mode="lines", name="Log scale"
|
199 |
+
),
|
200 |
+
row=2,
|
201 |
+
col=1,
|
202 |
+
)
|
203 |
+
int_spec_fig.append_trace(
|
204 |
+
go.Scatter(
|
205 |
+
x=peaks,
|
206 |
+
y=int_spec[peaks],
|
207 |
+
mode="markers",
|
208 |
+
marker_color="#00cc96",
|
209 |
+
showlegend=False,
|
210 |
+
),
|
211 |
+
row=1,
|
212 |
+
col=1,
|
213 |
+
)
|
214 |
+
int_spec_fig.append_trace(
|
215 |
+
go.Scatter(
|
216 |
+
x=peaks,
|
217 |
+
y=int_spec_log[peaks],
|
218 |
+
mode="markers",
|
219 |
+
name="Peaks",
|
220 |
+
marker_color="#00cc96",
|
221 |
+
),
|
222 |
+
row=2,
|
223 |
+
col=1,
|
224 |
)
|
|
|
225 |
|
226 |
+
# Add a vertical line to mark a position within the range
|
227 |
+
int_spec_fig.add_vline(
|
228 |
+
x=compton_peak_slider.value, line_width=1, line_color="#ab63fa"
|
229 |
+
)
|
230 |
+
int_spec_fig.add_vline(
|
231 |
+
x=elastic_peak_slider.value, line_width=1, line_color="#ffa15a"
|
232 |
+
)
|
233 |
|
234 |
+
int_spec_fig.update_layout(showlegend=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
235 |
|
236 |
+
mo.ui.plotly(int_spec_fig)
|
237 |
+
return go, int_spec_fig, make_subplots
|
238 |
|
|
|
|
|
|
|
|
|
|
|
239 |
|
240 |
+
@app.cell
|
241 |
+
def _(configs, elem_selection, mo, param_selection):
|
242 |
+
control_panel = mo.accordion(
|
243 |
+
{
|
244 |
+
"Elements": elem_selection.center(),
|
245 |
+
"Parameters": param_selection,
|
246 |
+
"Configs": configs,
|
247 |
+
},
|
248 |
+
multiple=True,
|
249 |
+
)
|
250 |
+
control_panel_shown = True
|
251 |
+
control_panel
|
252 |
+
return control_panel, control_panel_shown
|
253 |
|
|
|
|
|
|
|
254 |
|
255 |
+
@app.cell
|
256 |
+
def _(control_panel_shown, mo):
|
257 |
+
run_button = mo.ui.run_button()
|
258 |
+
run_button.right() if control_panel_shown else None
|
259 |
+
return (run_button,)
|
|
|
260 |
|
261 |
|
262 |
+
@app.cell
|
263 |
+
def _(
|
264 |
+
device_selection,
|
265 |
+
elem_checkboxes,
|
266 |
+
energy_range,
|
267 |
+
fit_indices_list,
|
268 |
+
init_amp_checkbox,
|
269 |
+
int_spec_og,
|
270 |
+
iter_slider,
|
271 |
+
loss_selection,
|
272 |
+
mo,
|
273 |
+
optimizer_selection,
|
274 |
+
param_checkboxes,
|
275 |
+
run_button,
|
276 |
+
use_snip_checkbox,
|
277 |
+
use_step_checkbox,
|
278 |
+
):
|
279 |
+
mo.stop(not run_button.value)
|
280 |
+
from mapstorch.opt import fit_spec
|
281 |
+
|
282 |
+
n_iter = iter_slider.value
|
283 |
+
with mo.status.progress_bar(total=n_iter) as bar:
|
284 |
+
fitted_tensors, fitted_spec, fitted_bkg, loss_trace = fit_spec(
|
285 |
+
int_spec_og,
|
286 |
+
energy_range.value,
|
287 |
+
elements_to_fit=[k for k, v in elem_checkboxes.items() if v.value],
|
288 |
+
fitting_params=[k for k, v in param_checkboxes.items() if v[0].value],
|
289 |
+
init_param_vals={
|
290 |
+
k: float(v[2].value) for k, v in param_checkboxes.items() if v[0].value
|
291 |
+
},
|
292 |
+
fixed_param_vals={
|
293 |
+
k: float(v[2].value)
|
294 |
+
for k, v in param_checkboxes.items()
|
295 |
+
if v[0].value and v[1].value
|
296 |
+
},
|
297 |
+
indices=fit_indices_list[-1],
|
298 |
+
tune_params=True,
|
299 |
+
init_amp=init_amp_checkbox.value,
|
300 |
+
use_snip=use_snip_checkbox.value,
|
301 |
+
use_step=use_step_checkbox.value,
|
302 |
+
use_tail=False,
|
303 |
+
loss=loss_selection.value,
|
304 |
+
optimizer=optimizer_selection.value,
|
305 |
+
n_iter=n_iter,
|
306 |
+
progress_bar=False,
|
307 |
+
device=device_selection.value,
|
308 |
+
status_updator=bar,
|
309 |
)
|
310 |
+
return (
|
311 |
+
bar,
|
312 |
+
fit_spec,
|
313 |
+
fitted_bkg,
|
314 |
+
fitted_spec,
|
315 |
+
fitted_tensors,
|
316 |
+
loss_trace,
|
317 |
+
n_iter,
|
318 |
)
|
|
|
319 |
|
320 |
|
321 |
@app.cell
|
322 |
+
def _(fitted_bkg, fitted_spec, go, int_spec, make_subplots, mo, np, px):
|
323 |
+
fit_labels = ["experiment", "background", "fitted"]
|
324 |
+
fit_fig = make_subplots(rows=2, cols=1)
|
325 |
+
spec_x = np.linspace(0, int_spec.size - 1, int_spec.size)
|
326 |
+
|
327 |
+
for i, spec in enumerate([int_spec, fitted_bkg, fitted_spec + fitted_bkg]):
|
328 |
+
fit_fig.add_trace(
|
329 |
+
go.Scatter(
|
330 |
+
x=spec_x,
|
331 |
+
y=spec,
|
332 |
+
mode="lines",
|
333 |
+
name=fit_labels[i],
|
334 |
+
line=dict(color=px.colors.qualitative.Plotly[i]),
|
335 |
+
),
|
336 |
+
row=1,
|
337 |
+
col=1,
|
338 |
+
)
|
339 |
+
spec_log = np.log10(np.clip(spec, 0, None) + 1)
|
340 |
+
fit_fig.add_trace(
|
341 |
+
go.Scatter(
|
342 |
+
x=spec_x,
|
343 |
+
y=spec_log,
|
344 |
+
mode="lines",
|
345 |
+
showlegend=False,
|
346 |
+
line=dict(color=px.colors.qualitative.Plotly[i]),
|
347 |
+
),
|
348 |
+
row=2,
|
349 |
+
col=1,
|
350 |
+
)
|
351 |
|
352 |
+
mo.ui.plotly(fit_fig)
|
353 |
+
return fit_fig, fit_labels, i, spec, spec_log, spec_x
|
354 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
355 |
|
356 |
+
@app.cell
|
357 |
+
def _(elem_checkboxes, fitted_tensors, go, make_subplots, mo):
|
358 |
+
target_elems = [k for k, v in elem_checkboxes.items() if v.value]
|
359 |
+
amps = {p: fitted_tensors[p].item() for p in target_elems}
|
360 |
+
amps = dict(sorted(amps.items(), key=lambda item: item[1]))
|
361 |
+
|
362 |
+
amp_fig = make_subplots(rows=1, cols=2)
|
363 |
+
|
364 |
+
amp_fig.add_trace(
|
365 |
+
go.Bar(
|
366 |
+
x=[10**v for v in amps.values()],
|
367 |
+
y=list(amps.keys()),
|
368 |
+
orientation="h",
|
369 |
+
name="Photon counts",
|
370 |
+
),
|
371 |
+
row=1,
|
372 |
+
col=1,
|
373 |
)
|
374 |
+
amp_fig.add_trace(go.Bar(x=list(amps.values()), name="Log scale"), row=1, col=2)
|
375 |
+
amp_fig.update_yaxes(showticklabels=False, row=1, col=2)
|
376 |
+
amp_fig.update_layout(showlegend=False)
|
377 |
+
|
378 |
+
results_shown = True
|
379 |
+
|
380 |
+
mo.ui.plotly(amp_fig)
|
381 |
+
return amp_fig, amps, results_shown, target_elems
|
382 |
+
|
383 |
+
|
384 |
+
# @app.cell
|
385 |
+
# def _(
|
386 |
+
# confirm_range_button,
|
387 |
+
# energy_level_slider,
|
388 |
+
# focus_target_switch,
|
389 |
+
# mo,
|
390 |
+
# range_fig,
|
391 |
+
# results_shown,
|
392 |
+
# ):
|
393 |
+
# (
|
394 |
+
# mo.accordion(
|
395 |
+
# {
|
396 |
+
# "Target ranges": mo.vstack(
|
397 |
+
# [
|
398 |
+
# range_fig,
|
399 |
+
# mo.hstack(
|
400 |
+
# [
|
401 |
+
# focus_target_switch,
|
402 |
+
# energy_level_slider,
|
403 |
+
# confirm_range_button,
|
404 |
+
# ]
|
405 |
+
# ),
|
406 |
+
# ]
|
407 |
+
# )
|
408 |
+
# }
|
409 |
+
# )
|
410 |
+
# if results_shown
|
411 |
+
# else None
|
412 |
+
# )
|
413 |
+
# return
|
414 |
|
415 |
|
416 |
+
@app.cell
|
417 |
+
def _(confirm_range_button, elem_peak_indices, fit_indices_list, mo):
|
418 |
+
mo.stop(not confirm_range_button.value)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
419 |
|
420 |
+
fit_indices_list[-1] = elem_peak_indices
|
421 |
+
mo.callout(
|
422 |
+
"Target ranges have been updated. Please select parameters to re-run the fitting process.",
|
423 |
+
kind="success",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
424 |
)
|
425 |
return
|
426 |
|
427 |
|
428 |
+
@app.cell
|
429 |
+
def _(dataset, elem_checkboxes, fitted_tensors, mo, params_record):
|
430 |
+
import pandas as pd
|
431 |
+
import datetime
|
432 |
+
|
433 |
+
for par, l in params_record.items():
|
434 |
+
if par == "elements":
|
435 |
+
l.append(",".join([k for k, v in elem_checkboxes.items() if v.value]))
|
436 |
+
else:
|
437 |
+
l.append(fitted_tensors[par].item())
|
438 |
+
today = datetime.date.today()
|
439 |
+
today_string = today.strftime("%Y-%m-%d")
|
440 |
+
table_label = dataset.value[0].name + " parameter tuning record " + today_string
|
441 |
+
params_table = mo.ui.table(
|
442 |
+
pd.DataFrame(params_record),
|
443 |
+
selection="single",
|
444 |
+
label=table_label,
|
445 |
+
show_download=False,
|
446 |
+
)
|
447 |
+
params_table
|
448 |
+
return (
|
449 |
+
datetime,
|
450 |
+
l,
|
451 |
+
par,
|
452 |
+
params_table,
|
453 |
+
pd,
|
454 |
+
table_label,
|
455 |
+
today,
|
456 |
+
today_string,
|
457 |
+
)
|
458 |
|
|
|
|
|
|
|
|
|
459 |
|
460 |
+
@app.cell
|
461 |
+
def _(load_params_button, mo, params_table, save_params_button):
|
462 |
+
(
|
463 |
+
mo.hstack([save_params_button, load_params_button]).right()
|
464 |
+
if len(params_table.value) > 0
|
465 |
+
else None
|
466 |
)
|
467 |
return
|
468 |
|
469 |
|
470 |
@app.cell
|
471 |
+
def _(mo):
|
472 |
+
load_params_button = mo.ui.button(label="Load selected parameters and re-run")
|
473 |
+
save_params_button = mo.ui.run_button(label="Generate override params file")
|
474 |
+
return load_params_button, save_params_button
|
475 |
|
476 |
|
477 |
@app.cell
|
478 |
+
def _(mo, params_table, save_params_button):
|
479 |
+
mo.stop(not save_params_button.value)
|
480 |
+
from mapstorch.io import write_override_params_file
|
481 |
+
|
482 |
+
write_override_params_file(
|
483 |
+
"maps_fit_parameters_override.txt",
|
484 |
+
param_values={
|
485 |
+
"COHERENT_SCT_ENERGY": params_table.value.iloc[0][
|
486 |
+
"COHERENT_SCT_ENERGY"
|
487 |
+
].item(),
|
488 |
+
"ENERGY_OFFSET": params_table.value.iloc[0]["ENERGY_OFFSET"].item(),
|
489 |
+
"ENERGY_SLOPE": params_table.value.iloc[0]["ENERGY_SLOPE"].item(),
|
490 |
+
"ENERGY_QUADRATIC": params_table.value.iloc[0]["ENERGY_QUADRATIC"].item(),
|
491 |
+
"COMPTON_ANGLE": params_table.value.iloc[0]["COMPTON_ANGLE"].item(),
|
492 |
+
"COMPTON_FWHM_CORR": params_table.value.iloc[0]["COMPTON_FWHM_CORR"].item(),
|
493 |
+
"COMPTON_HI_F_TAIL": params_table.value.iloc[0]["COMPTON_HI_F_TAIL"].item(),
|
494 |
+
"COMPTON_F_TAIL": params_table.value.iloc[0]["COMPTON_F_TAIL"].item(),
|
495 |
+
"FWHM_FANOPRIME": params_table.value.iloc[0]["FWHM_FANOPRIME"].item(),
|
496 |
+
"FWHM_OFFSET": params_table.value.iloc[0]["FWHM_OFFSET"].item(),
|
497 |
+
"F_TAIL_OFFSET": params_table.value.iloc[0]["F_TAIL_OFFSET"].item(),
|
498 |
+
"KB_F_TAIL_OFFSET": params_table.value.iloc[0]["KB_F_TAIL_OFFSET"].item(),
|
499 |
+
},
|
500 |
+
elements=params_table.value.iloc[0]["elements"].split(","),
|
501 |
+
)
|
502 |
+
return (write_override_params_file,)
|
503 |
|
504 |
|
505 |
@app.cell
|
506 |
+
def _(
|
507 |
+
elem_peak_shapes,
|
508 |
+
fit_labels,
|
509 |
+
fitted_bkg,
|
510 |
+
fitted_spec,
|
511 |
+
focus_target_switch,
|
512 |
+
go,
|
513 |
+
int_spec,
|
514 |
+
make_subplots,
|
515 |
+
np,
|
516 |
+
px,
|
517 |
+
spec_x,
|
518 |
+
):
|
519 |
+
range_fig = make_subplots(rows=2, cols=1)
|
520 |
+
|
521 |
+
for iii, specc in enumerate([int_spec, fitted_bkg, fitted_spec + fitted_bkg]):
|
522 |
+
range_fig.add_trace(
|
523 |
+
go.Scatter(
|
524 |
+
x=spec_x,
|
525 |
+
y=specc,
|
526 |
+
mode="lines",
|
527 |
+
name=fit_labels[iii],
|
528 |
+
line=dict(color=px.colors.qualitative.Plotly[iii]),
|
529 |
+
),
|
530 |
+
row=1,
|
531 |
+
col=1,
|
532 |
+
)
|
533 |
+
specc_log = np.log10(np.clip(specc, 0, None) + 1)
|
534 |
+
range_fig.add_trace(
|
535 |
+
go.Scatter(
|
536 |
+
x=spec_x,
|
537 |
+
y=specc_log,
|
538 |
+
mode="lines",
|
539 |
+
showlegend=False,
|
540 |
+
line=dict(color=px.colors.qualitative.Plotly[iii]),
|
541 |
+
),
|
542 |
+
row=2,
|
543 |
+
col=1,
|
544 |
+
)
|
545 |
+
|
546 |
+
if focus_target_switch.value:
|
547 |
+
range_fig.update_layout(shapes=elem_peak_shapes, overwrite=True)
|
548 |
+
return iii, range_fig, specc, specc_log
|
549 |
|
550 |
|
551 |
@app.cell
|
552 |
+
def _(mo):
|
553 |
+
focus_target_switch = mo.ui.switch(label="Focus on target elements", value=False)
|
554 |
+
energy_level_slider = mo.ui.slider(
|
555 |
+
start=1, stop=6, step=1, value=1, label="Energy levels"
|
556 |
+
)
|
557 |
+
confirm_range_button = mo.ui.run_button(label="Load target ranges")
|
558 |
+
return confirm_range_button, energy_level_slider, focus_target_switch
|
559 |
|
560 |
|
561 |
@app.cell
|
562 |
+
def _(fit_indices_list, focus_target_switch):
|
563 |
+
if not focus_target_switch.value:
|
564 |
+
fit_indices_list[-1] = None
|
565 |
return
|
566 |
|
567 |
|
568 |
+
@app.cell
|
569 |
+
def _(elem_checkboxes, energy_level_slider, energy_range, fitted_tensors):
|
570 |
+
from mapstorch.util import get_peak_ranges
|
571 |
+
|
572 |
+
plot_elems = [k for k, v in elem_checkboxes.items() if v.value]
|
573 |
+
elem_peak_indices = []
|
574 |
+
elem_peak_shapes = []
|
575 |
+
for ii, ee in enumerate(plot_elems):
|
576 |
+
peak_rg = get_peak_ranges(
|
577 |
+
[ee],
|
578 |
+
fitted_tensors["COHERENT_SCT_ENERGY"].item(),
|
579 |
+
fitted_tensors["COMPTON_ANGLE"].item(),
|
580 |
+
fitted_tensors["ENERGY_OFFSET"].item(),
|
581 |
+
fitted_tensors["ENERGY_SLOPE"].item(),
|
582 |
+
fitted_tensors["ENERGY_QUADRATIC"].item(),
|
583 |
+
energy_range.value,
|
584 |
+
)
|
585 |
+
alpha = 0.2
|
586 |
+
for p_n, r in peak_rg.items():
|
587 |
+
if (
|
588 |
+
p_n in ["COMPTON_AMPLITUDE", "COHERENT_SCT_AMPLITUDE"]
|
589 |
+
or int(p_n[-1]) < energy_level_slider.value
|
590 |
+
):
|
591 |
+
elem_peak_indices += list(range(*r))
|
592 |
+
elem_peak_shapes.append(
|
593 |
+
dict(
|
594 |
+
type="rect",
|
595 |
+
x0=r[0],
|
596 |
+
x1=r[1],
|
597 |
+
y0=0,
|
598 |
+
y1=1,
|
599 |
+
xref="x",
|
600 |
+
yref="paper",
|
601 |
+
fillcolor="yellow",
|
602 |
+
opacity=alpha,
|
603 |
+
layer="below",
|
604 |
+
line_width=0,
|
605 |
+
)
|
606 |
+
)
|
607 |
+
return (
|
608 |
+
alpha,
|
609 |
+
ee,
|
610 |
+
elem_peak_indices,
|
611 |
+
elem_peak_shapes,
|
612 |
+
get_peak_ranges,
|
613 |
+
ii,
|
614 |
+
p_n,
|
615 |
+
peak_rg,
|
616 |
+
plot_elems,
|
617 |
+
r,
|
618 |
)
|
|
|
619 |
|
620 |
|
621 |
+
@app.cell
|
622 |
+
def _(param_checkbox_vals, param_default_vals, params_table):
|
623 |
+
if len(params_table.value) > 0:
|
624 |
+
for pp in params_table.value:
|
625 |
+
if pp in param_checkbox_vals:
|
626 |
+
param_checkbox_vals[pp] = float(params_table.value[pp].item())
|
627 |
+
else:
|
628 |
+
for pp in param_checkbox_vals:
|
629 |
+
param_checkbox_vals[pp] = float(param_default_vals[pp])
|
630 |
+
return (pp,)
|
631 |
|
|
|
|
|
632 |
|
633 |
+
@app.cell
|
634 |
+
def _(
|
635 |
+
PeriodicTableWidget,
|
636 |
+
default_fitting_elems,
|
637 |
+
elems,
|
638 |
+
mo,
|
639 |
+
unsupported_elements,
|
640 |
+
):
|
641 |
+
initial_selected_elems = set()
|
642 |
+
for e in default_fitting_elems:
|
643 |
+
if e in elems and not e in ["COHERENT_SCT_AMPLITUDE", "COMPTON_AMPLITUDE"]:
|
644 |
+
initial_selected_elems.add(e.split("_")[0])
|
645 |
+
|
646 |
+
elem_selection = mo.ui.anywidget(
|
647 |
+
PeriodicTableWidget(
|
648 |
+
states=1,
|
649 |
+
initial_selected={se: 0 for se in initial_selected_elems},
|
650 |
+
initial_disabled=unsupported_elements,
|
651 |
+
)
|
652 |
)
|
653 |
+
return e, elem_selection, initial_selected_elems
|
|
|
654 |
|
|
|
|
|
|
|
|
|
|
|
655 |
|
656 |
+
@app.cell
|
657 |
+
def _(
|
658 |
+
default_fitting_elems,
|
659 |
+
elem_selection,
|
660 |
+
mo,
|
661 |
+
supported_elements_mapping,
|
662 |
+
):
|
663 |
+
selected_lines = ["COHERENT_SCT_AMPLITUDE", "COMPTON_AMPLITUDE", "Si_Si"]
|
664 |
+
for select_e in elem_selection.selected_elements:
|
665 |
+
for l_ in supported_elements_mapping[select_e]:
|
666 |
+
if l_ == "K":
|
667 |
+
selected_lines.append(select_e)
|
668 |
+
else:
|
669 |
+
selected_lines.append(select_e + "_" + l_)
|
670 |
+
elem_checkboxes = {}
|
671 |
+
for edf in default_fitting_elems:
|
672 |
+
if edf in selected_lines:
|
673 |
+
elem_checkboxes[edf] = mo.ui.checkbox(label=edf, value=True)
|
674 |
+
else:
|
675 |
+
elem_checkboxes[edf] = mo.ui.checkbox(label=edf, value=False)
|
676 |
+
return edf, elem_checkboxes, l_, select_e, selected_lines
|
677 |
|
678 |
|
679 |
+
@app.cell
|
680 |
+
def _(default_fitting_params, load_params_button, mo, param_checkbox_vals):
|
681 |
+
load_params_button
|
682 |
+
|
683 |
+
param_checkboxes = {}
|
684 |
+
for p in default_fitting_params:
|
685 |
+
param_checkboxes[p] = [
|
686 |
+
mo.ui.checkbox(label=p, value=True),
|
687 |
+
mo.ui.checkbox(label="Fix"),
|
688 |
+
mo.ui.text(value=str(param_checkbox_vals[p])),
|
689 |
+
]
|
690 |
+
|
691 |
+
param_selection = mo.vstack(
|
692 |
+
[
|
693 |
+
mo.hstack(param_checkboxes[p], justify="start", gap=0)
|
694 |
+
for p in default_fitting_params
|
695 |
+
]
|
696 |
+
)
|
697 |
+
return p, param_checkboxes, param_selection
|
698 |
|
|
|
|
|
|
|
699 |
|
700 |
+
@app.cell
|
701 |
+
def _(device_list, mo):
|
702 |
+
init_amp_checkbox = mo.ui.checkbox(label="Initialize amplitudes", value=True)
|
703 |
+
use_snip_checkbox = mo.ui.checkbox(label="Use SNIP background", value=True)
|
704 |
+
use_step_checkbox = mo.ui.checkbox(label="Modify pearks with step", value=True)
|
705 |
+
model_options = mo.hstack(
|
706 |
+
[init_amp_checkbox, use_snip_checkbox, use_step_checkbox],
|
707 |
+
justify="start",
|
708 |
+
gap=5,
|
709 |
+
)
|
710 |
+
iter_slider = mo.ui.slider(
|
711 |
+
value=500, start=100, stop=3000, step=50, label="number of iterations"
|
712 |
+
)
|
713 |
+
loss_selection = mo.ui.dropdown(["mse", "l1"], value="mse", label="loss")
|
714 |
+
optimizer_selection = mo.ui.dropdown(
|
715 |
+
["adam", "adamw"], value="adam", label="optimizer"
|
716 |
+
)
|
717 |
+
device_selection = mo.ui.dropdown(device_list, value="cpu", label="device")
|
718 |
+
opt_options = mo.hstack(
|
719 |
+
[device_selection, loss_selection, optimizer_selection, iter_slider],
|
720 |
+
justify="start",
|
721 |
+
gap=2,
|
722 |
+
)
|
723 |
+
configs = mo.vstack([model_options, opt_options])
|
724 |
+
return (
|
725 |
+
configs,
|
726 |
+
device_selection,
|
727 |
+
init_amp_checkbox,
|
728 |
+
iter_slider,
|
729 |
+
loss_selection,
|
730 |
+
model_options,
|
731 |
+
opt_options,
|
732 |
+
optimizer_selection,
|
733 |
+
use_snip_checkbox,
|
734 |
+
use_step_checkbox,
|
735 |
+
)
|
736 |
|
|
|
|
|
737 |
|
738 |
+
@app.cell
|
739 |
+
def _():
|
740 |
+
import torch
|
741 |
|
742 |
+
device_list = ["cpu"]
|
743 |
+
if torch.cuda.is_available():
|
744 |
+
device_list.append("cuda")
|
745 |
+
return device_list, torch
|
746 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
747 |
|
748 |
+
@app.cell
|
749 |
+
def _():
|
750 |
+
fit_indices_list = [None]
|
751 |
+
return (fit_indices_list,)
|
752 |
|
|
|
|
|
|
|
753 |
|
754 |
+
@app.cell
|
755 |
+
def _(
|
756 |
+
acos,
|
757 |
+
compton_peak_slider,
|
758 |
+
elastic_peak_slider,
|
759 |
+
incident_energy_slider,
|
760 |
+
pi,
|
761 |
+
):
|
762 |
+
from mapstorch.default import default_param_vals, default_fitting_params
|
763 |
+
from copy import copy
|
764 |
+
|
765 |
+
coherent_sct_energy = incident_energy_slider.value
|
766 |
+
energy_slope = coherent_sct_energy / elastic_peak_slider.value
|
767 |
+
compton_energy = energy_slope * compton_peak_slider.value
|
768 |
+
try:
|
769 |
+
compton_angle = (
|
770 |
+
acos(1 - 511 * (1 / compton_energy - 1 / coherent_sct_energy)) * 180 / pi
|
771 |
+
)
|
772 |
+
except:
|
773 |
+
compton_angle = default_param_vals["COMPTON_ANGLE"]
|
774 |
+
param_default_vals = copy(default_param_vals)
|
775 |
+
param_default_vals["COHERENT_SCT_ENERGY"] = coherent_sct_energy
|
776 |
+
param_default_vals["ENERGY_SLOPE"] = energy_slope
|
777 |
+
param_default_vals["COMPTON_ANGLE"] = compton_angle
|
778 |
+
param_checkbox_vals = copy(param_default_vals)
|
779 |
+
params_record = {p: [] for p in default_fitting_params + ["elements"]}
|
780 |
+
return (
|
781 |
+
coherent_sct_energy,
|
782 |
+
compton_angle,
|
783 |
+
compton_energy,
|
784 |
+
copy,
|
785 |
+
default_fitting_params,
|
786 |
+
default_param_vals,
|
787 |
+
energy_slope,
|
788 |
+
param_checkbox_vals,
|
789 |
+
param_default_vals,
|
790 |
+
params_record,
|
791 |
)
|
|
|
792 |
|
793 |
|
794 |
+
@app.cell
|
795 |
+
def _(int_spec):
|
796 |
+
from scipy.signal import find_peaks
|
|
|
|
|
797 |
|
798 |
+
peaks, _ = find_peaks(int_spec, prominence=int_spec.max() / 200)
|
799 |
+
return find_peaks, peaks
|
|
|
|
|
800 |
|
801 |
|
802 |
@app.cell
|
803 |
+
def _(energy_range, int_spec_og, np):
|
804 |
+
int_spec = int_spec_og[energy_range.value[0] : energy_range.value[1] + 1]
|
805 |
+
int_spec_log = np.log10(np.clip(int_spec, 0, None) + 1)
|
806 |
+
return int_spec, int_spec_log
|
|
|
|
|
|
|
|
|
|
|
807 |
|
808 |
|
809 |
+
@app.cell
|
810 |
+
def _(dataset, dataset_button, elem_path, int_spec_path, mo, read_dataset):
|
811 |
+
mo.stop(not dataset_button.value)
|
812 |
+
dataset_dict = read_dataset(
|
813 |
+
dataset.value[0].path,
|
814 |
+
fit_elem_key=elem_path.value,
|
815 |
+
int_spec_key=int_spec_path.value,
|
|
|
|
|
816 |
)
|
817 |
+
int_spec_og = dataset_dict["int_spec"]
|
818 |
+
elems = dataset_dict["elems"]
|
819 |
+
return dataset_dict, elems, int_spec_og
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
820 |
|
821 |
|
822 |
if __name__ == "__main__":
|