|
1 | 1 | """Reflectometry detector-mapping alignment plans.""" |
2 | 2 |
|
| 3 | +import functools |
3 | 4 | import logging |
4 | 5 | from collections.abc import Generator |
5 | 6 | from typing import Any, TypedDict |
|
40 | 41 | logger = logging.getLogger(__name__) |
41 | 42 |
|
42 | 43 |
|
| 44 | +def _set_title_to_fit_result( |
| 45 | + name: str, doc: dict[str, Any], *, fit_callback: LiveFit, ax: Axes |
| 46 | +) -> None: |
| 47 | + fit_result = fit_callback.result |
| 48 | + if fit_result is not None: |
| 49 | + ax.set_title( |
| 50 | + f"Best x0: {fit_result.params['x0'].value:.4f} +/- {fit_result.params['x0'].stderr:.4f}" |
| 51 | + ) |
| 52 | + else: |
| 53 | + ax.set_title("Fit failed") |
| 54 | + plt.draw() |
| 55 | + |
| 56 | + |
43 | 57 | def _height_scan_callback_and_fit( |
44 | 58 | reducer: PeriodSpecIntegralsReducer, |
45 | 59 | height: NamedMovable[float], |
@@ -68,18 +82,12 @@ def _height_scan_callback_and_fit( |
68 | 82 | for cb in height_scan_callbacks.subs: |
69 | 83 | height_scan_ld.subscribe(cb) |
70 | 84 |
|
71 | | - def set_title_to_height_fit_result(name: str, doc: dict[str, Any]) -> None: |
72 | | - fit_result = height_scan_callbacks.live_fit.result |
73 | | - if fit_result is not None: |
74 | | - ax.set_title( |
75 | | - f"Best x0: {fit_result.params['x0'].value:.4f} " |
76 | | - f"+/- {fit_result.params['x0'].stderr:.4f}" |
77 | | - ) |
78 | | - else: |
79 | | - ax.set_title("Fit failed") # pragma: no cover |
80 | | - plt.draw() |
81 | | - |
82 | | - height_scan_ld.subscribe(set_title_to_height_fit_result, "stop") |
| 85 | + height_scan_ld.subscribe( |
| 86 | + functools.partial( |
| 87 | + _set_title_to_fit_result, fit_callback=height_scan_callbacks.live_fit, ax=ax |
| 88 | + ), |
| 89 | + "stop", |
| 90 | + ) |
83 | 91 |
|
84 | 92 | return height_scan_ld, height_scan_callbacks.live_fit |
85 | 93 |
|
@@ -135,24 +143,16 @@ def gaussian_max_y_guess( |
135 | 143 | for cb in angle_scan_callbacks.subs: |
136 | 144 | angle_scan_ld.subscribe(cb) |
137 | 145 |
|
138 | | - def set_title_to_angle_fit_result(name: str, doc: dict[str, Any]) -> None: |
139 | | - fit_result = angle_scan_callbacks.live_fit.result |
140 | | - if fit_result is not None: |
141 | | - ax.set_title( |
142 | | - f"Best x0: {fit_result.params['x0'].value:.4f} " |
143 | | - f"+/- {fit_result.params['x0'].stderr:.4f}" |
144 | | - ) |
145 | | - else: |
146 | | - ax.set_title("Fit failed") # pragma: no cover |
147 | | - plt.draw() |
148 | | - |
149 | | - angle_scan_ld.subscribe(set_title_to_angle_fit_result, "stop") |
150 | | - |
151 | | - # Make sure the Plot PNG saving happens *after* setting plot title to fit result... |
152 | 146 | angle_scan_ld.subscribe( |
153 | | - PlotPNGSaver(x=angle_name, y=counts_name, ax=ax, postfix="", output_dir=None) |
| 147 | + functools.partial( |
| 148 | + _set_title_to_fit_result, fit_callback=angle_scan_callbacks.live_fit, ax=ax |
| 149 | + ), |
| 150 | + "stop", |
154 | 151 | ) |
155 | 152 |
|
| 153 | + # Make sure the Plot PNG saving happens *after* setting plot title to fit result... |
| 154 | + angle_scan_ld.subscribe(PlotPNGSaver(x=angle_name, y=counts_name, ax=ax, postfix="")) |
| 155 | + |
156 | 156 | return angle_scan_ld, angle_scan_callbacks.live_fit |
157 | 157 |
|
158 | 158 |
|
|
0 commit comments