|
5 | 5 | .. autosummary::
|
6 | 6 |
|
7 | 7 | ~lineup
|
| 8 | + ~lineup2 |
8 | 9 | ~tune_axes
|
9 | 10 | ~TuneAxis
|
10 | 11 | ~TuneResults
|
@@ -42,68 +43,59 @@ def lineup(
|
42 | 43 | # fmt: on
|
43 | 44 | ):
|
44 | 45 | """
|
45 |
| - Lineup and center a given axis, relative to current position. |
| 46 | + (use ``lineup2()`` now) Lineup and center a given axis, relative to current position. |
46 | 47 |
|
47 | 48 | If first run identifies a peak, makes a second run to fine tune the result.
|
48 | 49 |
|
| 50 | + ..caution:: ``lineup()`` does not work in the queueserver. Use |
| 51 | + :func:`~apstools.plans.alignment.lineup2()` instead. |
| 52 | +
|
49 | 53 | .. index:: Bluesky Plan; lineup
|
50 | 54 |
|
51 | 55 | PARAMETERS
|
52 | 56 |
|
53 | 57 | detectors
|
54 |
| - *[object]* or *object* : |
55 |
| - Instance(s) of ophyd.Signal (or subclass such as ophyd.scaler.ScalerChannel) |
56 |
| - dependent measurement to be maximized. If a list, the first signal in the |
57 |
| - list will be used. |
| 58 | + *[object]* or *object* : Instance(s) of ophyd.Signal (or subclass such |
| 59 | + as ophyd.scaler.ScalerChannel) dependent measurement to be maximized. |
| 60 | + If a list, the first signal in the list will be used. |
58 | 61 |
|
59 | 62 | axis
|
60 |
| - movable *object* : |
61 |
| - instance of ophyd.Signal (or subclass such as EpicsMotor) |
62 |
| - independent axis to use for alignment |
| 63 | + movable *object* : instance of ophyd.Signal (or subclass such as |
| 64 | + EpicsMotor) independent axis to use for alignment |
63 | 65 |
|
64 | 66 | minus
|
65 |
| - *float* : |
66 |
| - first point of scan at this offset from starting position |
| 67 | + *float* : first point of scan at this offset from starting position |
67 | 68 |
|
68 | 69 | plus
|
69 |
| - *float* : |
70 |
| - last point of scan at this offset from starting position |
| 70 | + *float* : last point of scan at this offset from starting position |
71 | 71 |
|
72 | 72 | npts
|
73 |
| - *int* : |
74 |
| - number of data points in the scan |
| 73 | + *int* : number of data points in the scan |
75 | 74 |
|
76 | 75 | time_s
|
77 |
| - *float* : |
78 |
| - Count time per step (if detectors[0] is ScalerChannel object), |
79 |
| - other object types not yet supported. |
80 |
| - (default: 0.1) |
| 76 | + *float* : Count time per step (if detectors[0] is ScalerChannel object), |
| 77 | + other object types not yet supported. (default: 0.1) |
81 | 78 |
|
82 | 79 | peak_factor
|
83 |
| - *float* : |
84 |
| - peak maximum must be greater than ``peak_factor*minimum`` |
| 80 | + *float* : peak maximum must be greater than ``peak_factor*minimum`` |
85 | 81 | (default: 4)
|
86 | 82 |
|
87 | 83 | width_factor
|
88 |
| - *float* : |
89 |
| - fwhm must be less than ``width_factor*plot_range`` |
90 |
| - (default: 0.8) |
| 84 | + *float* : fwhm must be less than ``width_factor*plot_range`` (default: |
| 85 | + 0.8) |
91 | 86 |
|
92 | 87 | feature
|
93 |
| - *str* : |
94 |
| - One of the parameters returned by BestEffortCallback peak stats. |
95 |
| - (``cen``, ``com``, ``max``, ``min``) |
96 |
| - (default: ``cen``) |
| 88 | + *str* : One of the parameters returned by BestEffortCallback peak stats. |
| 89 | + (``cen``, ``com``, ``max``, ``min``) (default: ``cen``) |
97 | 90 |
|
98 | 91 | rescan
|
99 |
| - *bool* : |
100 |
| - If first scan indicates a peak, should a second scan refine the result? |
101 |
| - (default: ``True``) |
| 92 | + *bool* : If first scan indicates a peak, should a second scan refine the |
| 93 | + result? (default: ``True``) |
102 | 94 |
|
103 | 95 | bec
|
104 |
| - *object* : |
105 |
| - Instance of ``bluesky.callbacks.best_effort.BestEffortCallback``. |
106 |
| - (default: ``None``, meaning look for it from global namespace) |
| 96 | + *object* : Instance of |
| 97 | + ``bluesky.callbacks.best_effort.BestEffortCallback``. (default: |
| 98 | + ``None``, meaning look for it from global namespace) |
107 | 99 |
|
108 | 100 | EXAMPLE::
|
109 | 101 |
|
@@ -221,6 +213,165 @@ def peak_analysis():
|
221 | 213 | scaler.stage_sigs = old_sigs
|
222 | 214 |
|
223 | 215 |
|
| 216 | +def lineup2( |
| 217 | + # fmt: off |
| 218 | + detectors, mover, rel_start, rel_end, points, |
| 219 | + peak_factor=2.5, width_factor=0.8, |
| 220 | + feature="centroid", |
| 221 | + nscans=2, |
| 222 | + signal_stats=None, |
| 223 | + md={}, |
| 224 | + # fmt: on |
| 225 | +): |
| 226 | + """ |
| 227 | + Lineup and center a given mover, relative to current position. |
| 228 | +
|
| 229 | + This plan can be used in the queueserver, Jupyter notebooks, and IPython |
| 230 | + consoles. It does not require the bluesky BestEffortCallback. Instead, it |
| 231 | + uses *PySumReg* [#pysumreg]_ to compute statistics for each signal in a 1-D |
| 232 | + scan. |
| 233 | +
|
| 234 | + New in release 1.6.18 |
| 235 | +
|
| 236 | + .. caution:: This is an early draft and is subject to change! |
| 237 | +
|
| 238 | + .. index:: Bluesky Plan; lineup2; lineup |
| 239 | +
|
| 240 | + PARAMETERS |
| 241 | +
|
| 242 | + detectors *Readable* or [*Readable*]: |
| 243 | + Detector object or list of detector objects (each is a Device or |
| 244 | + Signal). If a list, the first Signal will be used for alignment. |
| 245 | +
|
| 246 | + mover *Movable*: |
| 247 | + Mover object, such as motor or other positioner. |
| 248 | +
|
| 249 | + rel_start *float*: |
| 250 | + Starting point for the scan, relative to the current mover position. |
| 251 | +
|
| 252 | + rel_end *float*: |
| 253 | + Ending point for the scan, relative to the current mover position. |
| 254 | +
|
| 255 | + points *int*: |
| 256 | + Number of points in the scan. |
| 257 | +
|
| 258 | + peak_factor *float* : |
| 259 | + peak maximum must be greater than ``peak_factor*minimum`` (default: 2.5) |
| 260 | +
|
| 261 | + width_factor *float* : |
| 262 | + fwhm must be less than ``width_factor*plot_range`` (default: 0.8) |
| 263 | +
|
| 264 | + feature *str*: |
| 265 | + Use this statistical measure (default: centroid) to set the mover |
| 266 | + position after a peak has been found. Must be one of these values: |
| 267 | +
|
| 268 | + ========== ==================== |
| 269 | + feature description |
| 270 | + ========== ==================== |
| 271 | + centroid center of mass |
| 272 | + x_at_max_y x location of y maximum |
| 273 | + x_at_min_y x location of y minimum |
| 274 | + ========== ==================== |
| 275 | +
|
| 276 | + Statistical analysis provided by *PySumReg*. [#pysumreg]_ |
| 277 | +
|
| 278 | + .. [#pysumreg] https://prjemian.github.io/pysumreg/latest/ |
| 279 | +
|
| 280 | + nscans *int*: |
| 281 | + Number of scans. (default: 2) Scanning will stop if any scan cannot |
| 282 | + find a peak. |
| 283 | +
|
| 284 | + signal_stats *object*: |
| 285 | + Caller could provide an object of |
| 286 | + :class:`~apstools.callbacks.scan_signal_statistics.SignalStatsCallback`. |
| 287 | + If ``None``, this function will search for the ``signal_stats`` signal |
| 288 | + in the global namespace. If not still found, a new one will be created |
| 289 | + for the brief lifetime of this function. |
| 290 | +
|
| 291 | + md *dict*: |
| 292 | + User-supplied metadata for this scan. |
| 293 | + """ |
| 294 | + from ..callbacks import SignalStatsCallback |
| 295 | + from ..callbacks import factor_fwhm |
| 296 | + |
| 297 | + if not isinstance(detectors, (tuple, list)): |
| 298 | + detectors = [detectors] |
| 299 | + |
| 300 | + _md = dict(purpose="alignment") |
| 301 | + _md.update(md or {}) |
| 302 | + |
| 303 | + if signal_stats is None: |
| 304 | + signal_stats = utils.ipython_shell_namespace().get("signal_stats") |
| 305 | + if signal_stats is None: |
| 306 | + signal_stats = SignalStatsCallback() |
| 307 | + signal_stats.stop_report = True |
| 308 | + else: |
| 309 | + signal_stats.stop_report = False # Turn this automation off. |
| 310 | + |
| 311 | + # Allow for feature to be defined using a name from PeakStats. |
| 312 | + xref_PeakStats = { |
| 313 | + "com": "centroid", |
| 314 | + "cen": "x_at_max_y", |
| 315 | + "max": "x_at_max_y", |
| 316 | + "min": "x_at_min_y", |
| 317 | + } |
| 318 | + # translate from PeakStats feature to SignalStats |
| 319 | + feature = xref_PeakStats.get(feature, feature) |
| 320 | + |
| 321 | + def get_x_by_feature(): |
| 322 | + """Return the X value of the specified ``feature``.""" |
| 323 | + stats = principal_signal_stats() |
| 324 | + if strong_peak(stats) and not too_wide(stats): |
| 325 | + return getattr(stats, feature) |
| 326 | + |
| 327 | + def principal_signal_stats() -> str: |
| 328 | + """Return the name of the first detector Signal.""" |
| 329 | + return signal_stats._registers[signal_stats._y_names[0]] |
| 330 | + |
| 331 | + def strong_peak(stats) -> bool: |
| 332 | + """Determine if the peak is strong.""" |
| 333 | + try: |
| 334 | + value = (stats.max_y - stats.min_y) / stats.sigma |
| 335 | + return value > peak_factor |
| 336 | + except ZeroDivisionError: # not enough samples |
| 337 | + try: |
| 338 | + value = abs(stats.max_y / stats.min_y) |
| 339 | + return value > peak_factor |
| 340 | + except ZeroDivisionError: |
| 341 | + return False |
| 342 | + |
| 343 | + def too_wide(stats): |
| 344 | + """Does the measured peak width fill the full range of X?""" |
| 345 | + try: |
| 346 | + x_range = stats.max_x - stats.min_x |
| 347 | + fwhm = stats.sigma * factor_fwhm |
| 348 | + return fwhm > width_factor * x_range |
| 349 | + except ZeroDivisionError: # not enough samples |
| 350 | + return True |
| 351 | + |
| 352 | + @bpp.subs_decorator(signal_stats.receiver) |
| 353 | + def _inner(): |
| 354 | + """Run the scan, collecting statistics at each step.""" |
| 355 | + # TODO: save signal stats into separate stream |
| 356 | + yield from bp.rel_scan(detectors, mover, rel_start, rel_end, points, md=_md) |
| 357 | + |
| 358 | + while nscans > 0: # allow for repeated scans |
| 359 | + yield from _inner() # Run the scan. |
| 360 | + nscans -= 1 |
| 361 | + |
| 362 | + target = get_x_by_feature() |
| 363 | + if target is None: |
| 364 | + nscans = 0 # Nothing found, no point scanning again. |
| 365 | + else: |
| 366 | + yield from bps.mv(mover, target) # Move to the feature position. |
| 367 | + logger.info("Moved %s to %s: %f", mover.name, feature, target) |
| 368 | + |
| 369 | + if nscans > 0: |
| 370 | + # move the end points for the next scan |
| 371 | + rel_end = principal_signal_stats().sigma * factor_fwhm |
| 372 | + rel_start = -rel_end |
| 373 | + |
| 374 | + |
224 | 375 | class TuneAxis(object):
|
225 | 376 | """
|
226 | 377 | tune an axis with a signal
|
|
0 commit comments