From d7ac5002fe70120e2f80326fe2b208a727ef5fc6 Mon Sep 17 00:00:00 2001 From: x10sa Date: Mon, 21 Sep 2026 09:02:46 +0200 Subject: [PATCH] restructured common mx utility files --- pxii_bec/macros/mxcomm_analysis.py | 5 +- pxii_bec/macros/mxcomm_data.py | 6 +-- pxii_bec/macros/mxcomm_scan.py | 79 +++++++----------------------- 3 files changed, 24 insertions(+), 66 deletions(-) diff --git a/pxii_bec/macros/mxcomm_analysis.py b/pxii_bec/macros/mxcomm_analysis.py index 5bd2805..f12d466 100644 --- a/pxii_bec/macros/mxcomm_analysis.py +++ b/pxii_bec/macros/mxcomm_analysis.py @@ -176,9 +176,8 @@ def check_peak_fit(x_data, result, min_fwhm_steps=3): if fwhm < min_fwhm_steps * step: problems.append( - f"Peak FWHM spans only {fwhm_steps:.2f} scan steps " - f"(minimum {min_fwhm_steps})" - ) + f"Peak FWHM spans only {fwhm_steps:.2f} scan steps " f"(minimum {min_fwhm_steps})" + ) if fwhm > 0.7 * scan_range: problems.append("peak is too broad for scan range") diff --git a/pxii_bec/macros/mxcomm_data.py b/pxii_bec/macros/mxcomm_data.py index 73c1bb3..b01b6ba 100644 --- a/pxii_bec/macros/mxcomm_data.py +++ b/pxii_bec/macros/mxcomm_data.py @@ -81,17 +81,17 @@ def get_value(dev_name, details=False): root = "scam" live_root = "scam" name = "scam_contrast" - value = dev_name.read()[name]["value"] + value = dev_name.read(cached=True)[name]["value"] else: try: name = live_root = root = dev_name.name - value = dev_name.read()[name]["value"] + value = dev_name.read(cached=True)[name]["value"] except: print("non-standard device") root = dev_name.dotted_name name = dev_name.full_name live_root = dev_name.parent.name - value = dev_name.read()[name]["value"] + value = dev_name.read(cached=True)[name]["value"] if details: return {"value": value, "root": root, "name": name, "live_root": live_root} diff --git a/pxii_bec/macros/mxcomm_scan.py b/pxii_bec/macros/mxcomm_scan.py index c007e2f..8232dda 100755 --- a/pxii_bec/macros/mxcomm_scan.py +++ b/pxii_bec/macros/mxcomm_scan.py @@ -6,7 +6,6 @@ from dataclasses import dataclass import numpy as np - # from mxcomm_analysis import ( # create_fit_parameters, # get_data_from_history, @@ -26,6 +25,7 @@ import numpy as np # dev # ) + @dataclass(frozen=True) class ScanDefaults: """Default values for fitting routines""" @@ -100,9 +100,11 @@ def move_to_scan_position(motor_device, motor_name: str, position: float, data: scans.umv(motor_device, motor_position, relative=False) print(f"\n Moving {motor_name} to position {motor_position: .3f}") + def restore_position(motor_device, position): """Return a motor to its position before the scan.""" - scans.umv(motor_device, position, relative=False) + scans.umv(motor_device, position, relative=False) + def go_to_peak( motor_device, @@ -256,21 +258,12 @@ def go_to_peak( # ------------------------------------------------------------ scan_result = scans.line_scan( - motor_device, - start, - stop, - steps=steps, - relative=relative, - settling_time=settle, + motor_device, start, stop, steps=steps, relative=relative, settling_time=settle ) - motor_data = ( - scan_result.scan.live_data[mres["live_root"]][mres["name"]].val - ) + motor_data = scan_result.scan.live_data[mres["live_root"]][mres["name"]].val - signal_data = ( - scan_result.scan.live_data[sres["root"]][sres["name"]].val - ) + signal_data = scan_result.scan.live_data[sres["root"]][sres["name"]].val scan_number = "Current" @@ -321,12 +314,7 @@ def go_to_peak( ) # Move to measured maximum - move_to_scan_position( - data["motor_device"], - data["motor_name"], - x_max, - data, - ) + move_to_scan_position(data["motor_device"], data["motor_name"], x_max, data) return { "success": True, @@ -342,10 +330,7 @@ def go_to_peak( # ============================================================ fit_params = create_fit_parameters( - deriv=False, - negative=negative, - model=FitDefaults.MODEL, - baseline=FitDefaults.BASELINE, + deriv=False, negative=negative, model=FitDefaults.MODEL, baseline=FitDefaults.BASELINE ) fit_result = fit(data, fit_params) @@ -354,11 +339,7 @@ def go_to_peak( # Validate fitted peak # ------------------------------------------------------------ - check = check_peak_fit( - data["x_data"], - fit_result, - min_fwhm_steps=min_fwhm_steps, - ) + check = check_peak_fit(data["x_data"], fit_result, min_fwhm_steps=min_fwhm_steps) if not check["success"]: @@ -375,32 +356,18 @@ def go_to_peak( title=f"{motor_name} scan - FIT REJECTED", ) - print( - f"Returning motor to start position, " - f"{starting_position:.5g}" - ) + print(f"Returning motor to start position, " f"{starting_position:.5g}") - restore_position( - motor_device, - starting_position, - ) + restore_position(motor_device, starting_position) - return { - "success": False, - "problems": check["problems"], - } + return {"success": False, "problems": check["problems"]} # ------------------------------------------------------------ # Plot accepted fit # ------------------------------------------------------------ if plot: - plot_fitted_data( - data, - fit_result, - window_name=window_name, - plot_widget=plot_widget, - ) + plot_fitted_data(data, fit_result, window_name=window_name, plot_widget=plot_widget) # ------------------------------------------------------------ # Move to fitted peak centre @@ -409,19 +376,11 @@ def go_to_peak( centre = fit_result["centre"] if gap and centre < 4.5: - print( - f"Fitted gap position {centre:.5g} mm is below " - "minimum gap of 4.5 mm" - ) + print(f"Fitted gap position {centre:.5g} mm is below " "minimum gap of 4.5 mm") umv(dev.id_gap, 4.5) else: - move_to_scan_position( - data["motor_device"], - data["motor_name"], - centre, - data, - ) + move_to_scan_position(data["motor_device"], data["motor_name"], centre, data) # ------------------------------------------------------------ # Return successful fit information @@ -546,9 +505,9 @@ def find_feature( plot_live_data( motor_name, signal_name, - window_name = window_name, - plot_widget = plot_widget, - title = f"{motor_name} scans" + window_name=window_name, + plot_widget=plot_widget, + title=f"{motor_name} scans", ) # Perform scan scan_result = scans.line_scan(