Feat/flomni tomo from angles file #327
No files matched your search
@@ -81,6 +81,7 @@ class LamniWebpageGenerator(WebpageGeneratorBase):
|
||||
("lamni_piezo_range_y", "Piezo range y (µm)"),
|
||||
("manual_shift_x", "Manual shift x (mm)"),
|
||||
("manual_shift_y", "Manual shift y (mm)"),
|
||||
("tomo_angle_file", "Angle file"),
|
||||
("tomo_angle_stepsize", "Angle step (°)"),
|
||||
("tomo_shellstep", "Shell step (µm)"),
|
||||
("lamni_stitch_x", "Stitch x"),
|
||||
|
||||
@@ -23,6 +23,14 @@ from csaxs_bec.bec_ipython_client.plugins.OMNY_shared.omny_general_tools import
|
||||
PtychoReconstructor,
|
||||
TomoIDManager,
|
||||
)
|
||||
from csaxs_bec.bec_ipython_client.plugins.OMNY_shared.angle_file import (
|
||||
AngleFileError,
|
||||
read_angle_file,
|
||||
summarize_angles,
|
||||
)
|
||||
from csaxs_bec.bec_ipython_client.plugins.OMNY_shared.tomo_angle_file_mixin import (
|
||||
TomoAngleFileMixin,
|
||||
)
|
||||
from csaxs_bec.bec_ipython_client.plugins.OMNY_shared.tomo_queue_mixin import TomoQueueMixin
|
||||
|
||||
from .lamni_optics_mixin import LaMNIInitStages, LamNIOpticsMixin
|
||||
@@ -129,9 +137,16 @@ class LamNIError(Exception):
|
||||
precondition, ...) that should never be retried by @scan_repeat."""
|
||||
|
||||
|
||||
class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools):
|
||||
class LamNI(
|
||||
TomoQueueMixin, TomoAngleFileMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
):
|
||||
_TOMO_PARAMS_WINDOW_NAME = "lamni_tomo_params"
|
||||
_TOMO_PARAMS_ERROR = LamNIError
|
||||
# tomo_type 4: laminography -- 180-deg-opposite projections are not
|
||||
# redundant, so do not warn about them. No legacy at-each-angle builtin
|
||||
# is consulted by _at_each_angle() (only at_each_angle_hook).
|
||||
_ANGLE_FILE_OPPOSITE_WARNING = False
|
||||
_AT_EACH_ANGLE_BUILTIN = None
|
||||
|
||||
# Ordered set of all global-var-backed tomo scan parameters snapshotted
|
||||
# by tomo_queue_add()/restored by tomo_queue_execute(). Lamni's own
|
||||
@@ -153,6 +168,7 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
"lamni_piezo_range_y",
|
||||
"frames_per_trigger",
|
||||
"tomo_type",
|
||||
"tomo_angle_file",
|
||||
"tomo_angle_stepsize",
|
||||
"golden_ratio_bunch_size",
|
||||
"golden_max_number_of_projections",
|
||||
@@ -175,9 +191,11 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
}
|
||||
|
||||
# Named-action registry for tomo-queue command jobs -- identical to
|
||||
# flomni's, since both actions (move, optimize_idgap) are beamline-wide,
|
||||
# not flomni-specific hardware. See TomoQueueMixin._queue_action_move()/
|
||||
# optimize_idgap() for the implementations.
|
||||
# flomni's plain "move" action, since it's beamline-wide, not
|
||||
# flomni-specific hardware. flomni also has a "move_foptz_with_shift"
|
||||
# action for its own FZP optics-Z stage; lamni has no equivalent
|
||||
# hardware, so it isn't mirrored here. See
|
||||
# TomoQueueMixin._queue_action_move() for the implementation.
|
||||
_TOMO_QUEUE_ACTIONS = {
|
||||
"move": {
|
||||
"func": "_queue_action_move",
|
||||
@@ -185,21 +203,6 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
"help": "Move device(s) to absolute position(s).",
|
||||
"params": {"positions": {"type": "device_positions"}},
|
||||
},
|
||||
"optimize_idgap": {
|
||||
"func": "optimize_idgap",
|
||||
"idempotent": True,
|
||||
"help": "Scan idgap over a search range and move to the peak. STUB: no-op.",
|
||||
"params": {
|
||||
"search_range": {
|
||||
"type": "float",
|
||||
"default": 0.5,
|
||||
"min": 0.0,
|
||||
"max": 2.0,
|
||||
"unit": "mm",
|
||||
"label": "Search range",
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
def _describe_tomo_job_line(self, job: dict) -> str:
|
||||
@@ -212,8 +215,11 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
missing key.
|
||||
"""
|
||||
p = job["params"]
|
||||
angles_str = ""
|
||||
if p.get("tomo_type") == 4:
|
||||
angles_str = f" angles={os.path.basename(p.get('tomo_angle_file') or '') or '?'}"
|
||||
return (
|
||||
f"type={p.get('tomo_type')} circfov={p.get('tomo_circfov')}um "
|
||||
f"type={p.get('tomo_type')}{angles_str} circfov={p.get('tomo_circfov')}um "
|
||||
f"step={p.get('tomo_shellstep')}um ctime={p.get('tomo_countingtime')}s"
|
||||
)
|
||||
|
||||
@@ -338,6 +344,7 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
self.manual_shift_y = 0.0
|
||||
self.tomo_circfov = 0.0
|
||||
self.tomo_type = 1
|
||||
self.tomo_angle_file = ""
|
||||
self.corridor_size = -1
|
||||
self.lamni_stitch_x = 0
|
||||
self.lamni_stitch_y = 0
|
||||
@@ -687,10 +694,14 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
raise exc
|
||||
|
||||
def xrayeye_rotation_center_calibration_smear_experimental(
|
||||
self, keep_shutter_open: bool = False, sweep_deg: float = 360.0
|
||||
self,
|
||||
keep_shutter_open: bool = False,
|
||||
sweep_deg: float = 360.0,
|
||||
projection: str = "min",
|
||||
opaque_level: float = 0.25,
|
||||
):
|
||||
"""EXPERIMENTAL: extended-sample rotation-center calibration aided by
|
||||
a continuous-rotation max-projection ("star-trail") composite image,
|
||||
a continuous-rotation min- or max-projection ("star-trail") composite image,
|
||||
instead of a single instant of live rotation.
|
||||
|
||||
Off-axis features smear into circular arcs while lsamrot rotates
|
||||
@@ -706,6 +717,15 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
sweep_deg: angular range of the continuous smear sweep. Defaults
|
||||
to a full 360 deg (complete arcs), but a smaller range may
|
||||
already show enough curvature to locate the center.
|
||||
projection: "min" (default) for absorption contrast, i.e. dark
|
||||
features on a bright background; "max" for bright features
|
||||
on a dark background.
|
||||
opaque_level: with projection="min", pixels darker than this
|
||||
fraction of a frame's bright level are treated as opaque
|
||||
(sample holder) and ignored, so the holder rotating through
|
||||
the view does not black out the field of view. Raise it if
|
||||
the holder still blocks too much, lower it if faint sample
|
||||
features disappear; 0 = plain minimum.
|
||||
|
||||
Returns:
|
||||
tuple: (new_lsamx_center, new_lsamy_center) in mm.
|
||||
@@ -713,7 +733,10 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
aligner = XrayEyeAlignGUI(self.client, self)
|
||||
try:
|
||||
return aligner.find_rotation_center_smear_experimental(
|
||||
keep_shutter_open=keep_shutter_open, sweep_deg=sweep_deg
|
||||
keep_shutter_open=keep_shutter_open,
|
||||
sweep_deg=sweep_deg,
|
||||
projection=projection,
|
||||
opaque_level=opaque_level,
|
||||
)
|
||||
except KeyboardInterrupt as exc:
|
||||
print("Smear rotation-center calibration interrupted by user.")
|
||||
@@ -771,7 +794,7 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
self.device_manager.devices.rtx.controller.show_feedback_status()
|
||||
|
||||
def show_interferometer_positions(self):
|
||||
self.device_manager.devices.rtx.controller.show_feedback_status()
|
||||
return self.device_manager.devices.rtx.controller.show_interferometer_positions()
|
||||
|
||||
def show_signal_strength(self):
|
||||
self.device_manager.devices.rtx.controller.show_signal_strength_interferometer()
|
||||
@@ -780,10 +803,10 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
return self.device_manager.devices.rtx.controller.show_analog_signals()
|
||||
|
||||
def lights_off(self):
|
||||
self.device_manager.devices.lsamx.controller.lights_off()
|
||||
self.device_manager.devices.lsamx.controller.lamni_lights_off()
|
||||
|
||||
def lights_on(self):
|
||||
self.device_manager.devices.lsamx.controller.lights_on()
|
||||
self.device_manager.devices.lsamx.controller.lamni_lights_on()
|
||||
|
||||
def lamni_move_to_scan_center(self, shift_x: float, shift_y: float, angle: float):
|
||||
"""
|
||||
@@ -835,8 +858,8 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
|
||||
@tomo_type.setter
|
||||
def tomo_type(self, val: int):
|
||||
if val not in (1, 2, 3):
|
||||
raise ValueError("Unknown tomo_type. Must be 1, 2 or 3.")
|
||||
if val not in (1, 2, 3, 4):
|
||||
raise ValueError("Unknown tomo_type. Must be 1, 2, 3 or 4.")
|
||||
self.client.set_global_var("tomo_type", val)
|
||||
|
||||
@property
|
||||
@@ -1320,7 +1343,13 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def tomo_scan_projection(
|
||||
self, angle: float, _internal: bool = False, _reconstruct: bool = True
|
||||
self,
|
||||
angle: float,
|
||||
_internal: bool = False,
|
||||
_reconstruct: bool = True,
|
||||
step: float | None = None,
|
||||
exp_time: float | None = None,
|
||||
frames_per_trigger: int | None = None,
|
||||
):
|
||||
"""Acquire one fermat-scan projection at `angle` and add it to the
|
||||
reconstruction queue (tomo_reconstruct()), like
|
||||
@@ -1339,11 +1368,20 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
projection. Direct/standalone calls (the default) are checked every
|
||||
time, mirroring Flomni.tomo_scan_projection()'s _internal convention.
|
||||
|
||||
step/exp_time/frames_per_trigger (tomo_type 4 per-line overrides)
|
||||
replace tomo_shellstep/tomo_countingtime/frames_per_trigger for this
|
||||
projection only; None falls back to the current global values.
|
||||
|
||||
_reconstruct=False skips the reconstruction-queue entry; used by
|
||||
tomo_alignment_scan(), whose quick projections were never queued for
|
||||
reconstruction on lamni.
|
||||
"""
|
||||
scans = builtins.__dict__.get("scans")
|
||||
step = self.tomo_shellstep if step is None else step
|
||||
exp_time = self.tomo_countingtime if exp_time is None else exp_time
|
||||
frames_per_trigger = (
|
||||
self.frames_per_trigger if frames_per_trigger is None else frames_per_trigger
|
||||
)
|
||||
|
||||
if not _internal:
|
||||
dev = builtins.__dict__.get("dev")
|
||||
@@ -1378,7 +1416,7 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
scans.lamni_fermat_scan(
|
||||
fovx=self.lamni_piezo_range_x,
|
||||
fovy=self.lamni_piezo_range_y,
|
||||
step=self.tomo_shellstep,
|
||||
step=step,
|
||||
stitch_x=stitch_x,
|
||||
stitch_y=stitch_y,
|
||||
stitch_overlap=self.tomo_stitch_overlap,
|
||||
@@ -1398,8 +1436,8 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
),
|
||||
fov_circular=self.tomo_circfov,
|
||||
angle=angle,
|
||||
exp_time=self.tomo_countingtime,
|
||||
frames_per_trigger=self.frames_per_trigger,
|
||||
exp_time=exp_time,
|
||||
frames_per_trigger=frames_per_trigger,
|
||||
corridor_size=corridor_size,
|
||||
)
|
||||
|
||||
@@ -1565,16 +1603,31 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
scan_list=scan_list, next_scan_number=next_scan_number, base_path=base_path
|
||||
)
|
||||
|
||||
def _at_each_angle(self, angle: float) -> None:
|
||||
def _at_each_angle(
|
||||
self,
|
||||
angle: float,
|
||||
*,
|
||||
step: float | None = None,
|
||||
exp_time: float | None = None,
|
||||
frames_per_trigger: int | None = None,
|
||||
) -> None:
|
||||
hook_name = self.at_each_angle_hook
|
||||
if hook_name:
|
||||
# Hooks own the whole acquisition (fixed 2-arg contract) and never
|
||||
# see the tomo_type 4 per-line overrides.
|
||||
hook = self._resolve_at_each_angle_hook(hook_name, LamNIError)
|
||||
hook(self, angle)
|
||||
return
|
||||
|
||||
# tomo_scan_projection() itself adds the projection to the
|
||||
# reconstruction queue -- do not call tomo_reconstruct() again here.
|
||||
self.tomo_scan_projection(angle, _internal=True)
|
||||
self.tomo_scan_projection(
|
||||
angle,
|
||||
_internal=True,
|
||||
step=step,
|
||||
exp_time=exp_time,
|
||||
frames_per_trigger=frames_per_trigger,
|
||||
)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Progress reporting
|
||||
@@ -1641,6 +1694,12 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
tomo_type_name = "Equally spaced, golden ratio starting angles"
|
||||
subtomo_total = 360 / self.tomo_angle_stepsize
|
||||
total = self.golden_max_number_of_projections
|
||||
elif self.tomo_type == 4:
|
||||
# Real totals come from the angle file once _run_angle_file_loop()
|
||||
# has parsed it (and only if the file is marked complete).
|
||||
tomo_type_name = "Angles from file"
|
||||
subtomo_total = 1
|
||||
total = 1
|
||||
else:
|
||||
raise ValueError(f"Unknown tomo_type: {self.tomo_type}.")
|
||||
|
||||
@@ -1825,8 +1884,10 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
return not isinstance(exc, LamNIError)
|
||||
|
||||
@scan_repeat(max_repeats=10, default=True, exc_handler=_retry_unless_lamni_error)
|
||||
def _tomo_scan_at_angle(self, angle, subtomo_number):
|
||||
if 0 <= angle < 360.05:
|
||||
def _tomo_scan_at_angle(
|
||||
self, angle, subtomo_number, *, step=None, exp_time=None, frames_per_trigger=None
|
||||
):
|
||||
if self._min_scan_angle() <= angle < self._max_scan_angle() + 0.05:
|
||||
now = datetime.datetime.now()
|
||||
prev_heartbeat_str = self.progress.get("heartbeat")
|
||||
if prev_heartbeat_str is not None:
|
||||
@@ -1842,7 +1903,11 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
# tune if it over/under-triggers in practice.
|
||||
n_tiles = (2 * self.lamni_stitch_x + 1) * (2 * self.lamni_stitch_y + 1)
|
||||
normal_cadence = max(
|
||||
60.0, 5 * self.tomo_countingtime * self.frames_per_trigger * n_tiles
|
||||
60.0,
|
||||
5
|
||||
* (self.tomo_countingtime if exp_time is None else exp_time)
|
||||
* (self.frames_per_trigger if frames_per_trigger is None else frames_per_trigger)
|
||||
* n_tiles,
|
||||
)
|
||||
if gap > normal_cadence:
|
||||
idle = gap - normal_cadence
|
||||
@@ -1874,9 +1939,10 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
|
||||
start_scan_number = bec.queue.next_scan_number
|
||||
projection_start = time.perf_counter()
|
||||
overrides = self._angle_overrides_kwargs(step, exp_time, frames_per_trigger)
|
||||
try:
|
||||
for _ in range(num_repeats):
|
||||
self._at_each_angle(angle)
|
||||
self._at_each_angle(angle, **overrides)
|
||||
except AlarmBase as exc:
|
||||
if exc.alarm_type == "TimeoutError":
|
||||
# Clear the stuck queue, then re-raise so @scan_repeat
|
||||
@@ -1979,6 +2045,26 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
check instead prints a bold warning, waits 10s, and always
|
||||
proceeds -- it never aborts or raises.
|
||||
"""
|
||||
is_new_scan = (
|
||||
(self.tomo_type == 1 and subtomo_start == 1 and start_angle is None)
|
||||
or (self.tomo_type == 2 and projection_number == None)
|
||||
or (self.tomo_type == 3 and projection_number == None)
|
||||
or (self.tomo_type == 4 and projection_number is None)
|
||||
)
|
||||
if self.tomo_type == 4 and is_new_scan:
|
||||
# Clear stale progress from a previous scan immediately, before
|
||||
# the angle-file check and the gates below -- otherwise a
|
||||
# declined/raising gate leaves the GUI showing the last scan's
|
||||
# projection/angle/start time as if it were still running.
|
||||
self._reset_progress_for_new_scan()
|
||||
|
||||
# tomo_type 4: (re-)read the angle file before anything else, so a
|
||||
# missing/malformed file fails immediately (see
|
||||
# TomoAngleFileMixin._start_angle_file_scan).
|
||||
file_angles = None
|
||||
if self.tomo_type == 4:
|
||||
file_angles = self._start_angle_file_scan(projection_number)
|
||||
|
||||
if not self._check_eye_out_and_optics_in():
|
||||
warning = (
|
||||
"The setup is not in measurement configuration -- the X-ray "
|
||||
@@ -2013,11 +2099,7 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
|
||||
self._current_special_angles = self.special_angles.copy()
|
||||
|
||||
if (
|
||||
(self.tomo_type == 1 and subtomo_start == 1 and start_angle is None)
|
||||
or (self.tomo_type == 2 and projection_number is None)
|
||||
or (self.tomo_type == 3 and projection_number is None)
|
||||
):
|
||||
if is_new_scan:
|
||||
if not self.corr_pos_x and not force:
|
||||
warning = (
|
||||
"No fine (ptycho) alignment correction is loaded -- the "
|
||||
@@ -2054,6 +2136,8 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
)
|
||||
self.write_pdf_report()
|
||||
self._reset_progress_for_new_scan()
|
||||
if file_angles is not None:
|
||||
self._init_angle_file_progress(file_angles, projection_number)
|
||||
# Unconditional single 0-degree reference projection at the very
|
||||
# start of a new tomogram, independent of
|
||||
# zero_deg_reference_at_each_subtomo /
|
||||
@@ -2185,6 +2269,8 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
f" {self.golden_max_number_of_projections} projections."
|
||||
)
|
||||
break
|
||||
elif self.tomo_type == 4:
|
||||
self._run_angle_file_loop(file_angles, projection_number)
|
||||
else:
|
||||
raise ValueError(f"Unknown tomo_type: {self.tomo_type}.")
|
||||
finally:
|
||||
@@ -2214,7 +2300,7 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
)
|
||||
self.write_to_scilog(timing_content, ["tomoscan"])
|
||||
|
||||
def tomo_scan_resume(self) -> None:
|
||||
def tomo_scan_resume(self, from_queue: bool = False) -> None:
|
||||
"""Resume a tomo_scan() that crashed or was interrupted, picking up
|
||||
automatically from wherever ``progress`` last reported -- no need to
|
||||
read the last subtomo/angle/projection off the progress printout by
|
||||
@@ -2231,11 +2317,20 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
internally when resuming a job that previously failed partway
|
||||
through, so a failed queue job picks up mid-scan rather than
|
||||
restarting from subtomo 1 / angle 0.
|
||||
|
||||
If the tomo queue holds an unfinished ("incomplete"/"running") job,
|
||||
a manual call warns and asks for confirmation, since resuming by
|
||||
hand bypasses the queue (use tomo_queue_resume() instead).
|
||||
``from_queue`` is set by tomo_queue_execute() to skip that prompt.
|
||||
"""
|
||||
if self.progress.get("tomo_start_time") is None:
|
||||
print("No tomo scan in progress to resume -- nothing to do.")
|
||||
return
|
||||
|
||||
if not from_queue and not self._confirm_resume_outside_queue():
|
||||
print("Aborted.")
|
||||
return
|
||||
|
||||
if self.tomo_type == 1:
|
||||
subtomo_start = self.progress["subtomo"]
|
||||
start_angle = self.progress["angle"]
|
||||
@@ -2244,9 +2339,14 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
return
|
||||
print(f"Resuming tomo scan at subtomo {subtomo_start}, angle {start_angle:.3f} deg.")
|
||||
self.tomo_scan(subtomo_start=subtomo_start, start_angle=start_angle)
|
||||
elif self.tomo_type in (2, 3):
|
||||
elif self.tomo_type in (2, 3, 4):
|
||||
projection_number = self.progress["projection"]
|
||||
print(f"Resuming tomo scan at projection {projection_number}.")
|
||||
if self.tomo_type == 4:
|
||||
print(
|
||||
f"Note: the angle file {self.tomo_angle_file} is re-read now -- its "
|
||||
"already-acquired lines must be unchanged."
|
||||
)
|
||||
self.tomo_scan(projection_number=projection_number)
|
||||
else:
|
||||
raise ValueError(f"Unknown tomo_type: {self.tomo_type}.")
|
||||
@@ -2371,6 +2471,9 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
print("Ending by manual interruption.")
|
||||
if self.golden_projections_at_0_deg_for_damage_estimation == 1:
|
||||
print("Repeating projections at 0 deg at start of every second subtomogram.")
|
||||
elif self.tomo_type == 4:
|
||||
print("\x1b[1mTomo type 4:\x1b[0m Angles read from a file")
|
||||
self._print_angle_file_summary()
|
||||
print(f"\nSample name: {self.sample_name}\n")
|
||||
|
||||
if self.OMNYTools.yesno("Are these parameters correctly set for your scan?", "y"):
|
||||
@@ -2414,7 +2517,8 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
print(" 1: 8 equally spaced sub-tomograms (360 deg)")
|
||||
print(" 2: Golden ratio tomography")
|
||||
print(" 3: Equally spaced tomography, golden ratio starting angle")
|
||||
self.tomo_type = self._get_val("Tomography type", self.tomo_type, int)
|
||||
print(" 4: Angles (and sub-tomogram numbers) read from a file")
|
||||
self.tomo_type = self._get_val("Tomography type", self.tomo_type, int, min_=1, max_=4)
|
||||
|
||||
if self.tomo_type == 1:
|
||||
_, _, current_total = self._tomo_type1_actual_grid()
|
||||
@@ -2490,6 +2594,15 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
int,
|
||||
)
|
||||
|
||||
elif self.tomo_type == 4:
|
||||
print(
|
||||
"Angle file: one angle (0 to 360 deg) per line, optional 2nd column ="
|
||||
" sub-tomogram number, '#' comments. Read at scan start, and again"
|
||||
" while the scan runs -- without a final 'END' line the scan waits"
|
||||
" for more angles to be appended rather than stopping."
|
||||
)
|
||||
self._prompt_angle_file()
|
||||
|
||||
@staticmethod
|
||||
def _get_val(msg: str, default_value, data_type, min_=None, max_=None):
|
||||
"""Prompt for a value, casting to data_type. When min_/max_ are
|
||||
@@ -2562,6 +2675,7 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
# total left over in the persistent tomo_angle_stepsize global var
|
||||
# instead of golden_max_number_of_projections, plus a hardcoded "8
|
||||
# sub-tomograms".
|
||||
angle_step_label = "Angular step within sub-tomogram:"
|
||||
if self.tomo_type == 1:
|
||||
_, achievable_step, total_projections = self._tomo_type1_actual_grid()
|
||||
subtomo_count_label = "Number of individual sub-tomograms:"
|
||||
@@ -2576,6 +2690,19 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
subtomo_count_label = "Number of projections per sub-tomogram:"
|
||||
subtomo_count_value = f"{360 / self.tomo_angle_stepsize:.1f}"
|
||||
angle_step_value = f"{self.tomo_angle_stepsize:.2f}"
|
||||
elif self.tomo_type == 4:
|
||||
try:
|
||||
file_angles = read_angle_file(self.tomo_angle_file)
|
||||
total_projections = len(file_angles.angles)
|
||||
subtomo_count_value = f"{summarize_angles(file_angles.angles)['n_subtomos']}"
|
||||
if not file_angles.is_complete:
|
||||
subtomo_count_value += " (file incomplete, no 'END' marker yet)"
|
||||
except AngleFileError:
|
||||
total_projections = 0
|
||||
subtomo_count_value = "N/A (angle file not readable)"
|
||||
subtomo_count_label = "Number of individual sub-tomograms:"
|
||||
angle_step_label = "Angle file:"
|
||||
angle_step_value = self.tomo_angle_file or "N/A"
|
||||
else:
|
||||
# tomo_type == 2: golden ratio tomography -- there is no fixed set
|
||||
# of sub-tomograms or a fixed angular step, so report the bunch
|
||||
@@ -2663,7 +2790,7 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
|
||||
f"{'Restriction to circular FOV:':<{padding}}{self.tomo_circfov:.2f}\n",
|
||||
f"{'Stitching:':<{padding}}{stitching}\n",
|
||||
f"{subtomo_count_label:<{padding}}{subtomo_count_value}\n",
|
||||
f"{'Angular step within sub-tomogram:':<{padding}}{angle_step_value}\n",
|
||||
f"{angle_step_label:<{padding}}{angle_step_value}\n",
|
||||
f"{'Tomo type:':<{padding}}{self.tomo_type}\n",
|
||||
f"{'FZP diameter:':<{padding}}{fzp_diameter_um} microns\n",
|
||||
f"{'FZP outermost zone width:':<{padding}}{fzp_zone_width_nm} nm\n",
|
||||
|
||||
@@ -403,9 +403,10 @@ class LamNIOpticsMixin:
|
||||
)
|
||||
|
||||
def loptics_in(self):
|
||||
"""Move in the LamNI optics (FZP + OSA)."""
|
||||
"""Move in the LamNI optics (FZP + OSA + central stop)."""
|
||||
self.lfzp_in()
|
||||
self.losa_in()
|
||||
self.lcs_in()
|
||||
|
||||
def _check_eye_out_and_optics_in(self, tol=0.003):
|
||||
"""True if the setup is in measurement configuration: X-ray eye OUT,
|
||||
@@ -452,10 +453,11 @@ class LamNIOpticsMixin:
|
||||
return eye_out and optics_in and osa_in
|
||||
|
||||
def loptics_out(self):
|
||||
"""Move out the LamNI optics."""
|
||||
"""Move out the LamNI optics (central stop + OSA + FZP)."""
|
||||
if "rtx" in dev and dev.rtx.enabled:
|
||||
dev.rtx.controller.feedback_disable()
|
||||
|
||||
self.lcs_out()
|
||||
self.losa_out()
|
||||
loptx_out = self._get_user_param_safe("loptx", "out")
|
||||
lopty_out = self._get_user_param_safe("lopty", "out")
|
||||
@@ -469,10 +471,31 @@ class LamNIOpticsMixin:
|
||||
print(f"Interferometer feedback re-established ({time.time() - _t0:.1f} s).")
|
||||
|
||||
def lcs_in(self):
|
||||
pass
|
||||
lcsx_in = self._get_user_param_safe("lcsx", "in")
|
||||
lcsy_in = self._get_user_param_safe("lcsy", "in")
|
||||
|
||||
current_lcsx = dev.lcsx.readback.get()
|
||||
current_lcsy = dev.lcsy.readback.get()
|
||||
|
||||
tol = 0.003
|
||||
|
||||
need_move_cs = not np.isclose(current_lcsx, lcsx_in, atol=tol) or not np.isclose(
|
||||
current_lcsy, lcsy_in, atol=tol
|
||||
)
|
||||
|
||||
if need_move_cs:
|
||||
umv(dev.lcsx, lcsx_in, dev.lcsy, lcsy_in)
|
||||
else:
|
||||
print("Central stop is already at the IN position.")
|
||||
|
||||
def lcs_out(self):
|
||||
umv(dev.lcsy, 3)
|
||||
# only lcsy has an "out" position; lcsx stays where it is
|
||||
lcsy_out = self._get_user_param_safe("lcsy", "out")
|
||||
|
||||
if not np.isclose(dev.lcsy.readback.get(), lcsy_out, atol=0.003):
|
||||
umv(dev.lcsy, lcsy_out)
|
||||
else:
|
||||
print("Central stop is already at the OUT position.")
|
||||
|
||||
def losa_in(self):
|
||||
losax_in = self._get_user_param_safe("losax", "in")
|
||||
@@ -537,19 +560,18 @@ class LamNIOpticsMixin:
|
||||
distance = -loptz_val + 85.6 + 52
|
||||
print(f"The sample is in a distance of {distance:.1f} mm from the FZP.")
|
||||
|
||||
print(f"At the current energy of {mokev_val:.4f} keV we have following options:\n")
|
||||
|
||||
wavelength = 1.2398e-9 / mokev_val
|
||||
|
||||
diameters = [80e-6, 100e-6, 120e-6, 150e-6, 170e-6, 200e-6, 220e-6, 250e-6]
|
||||
|
||||
console = Console()
|
||||
table = Table(
|
||||
title=f"At the current energy of {mokev_val:.4f} keV we have following options:",
|
||||
box=box.SQUARE,
|
||||
)
|
||||
table = Table(title="Outermost zone width \033[1m60 nm\033[0m", box=box.SQUARE)
|
||||
table.add_column("Diameter", justify="center")
|
||||
table.add_column("Focal distance", justify="center")
|
||||
table.add_column("Current beam size", justify="center")
|
||||
|
||||
wavelength = 1.2398e-9 / mokev_val
|
||||
|
||||
for diameter in diameters:
|
||||
outermost_zonewidth = 60e-9
|
||||
focal_distance = diameter * outermost_zonewidth / wavelength
|
||||
@@ -562,6 +584,31 @@ class LamNIOpticsMixin:
|
||||
f"{beam_size:.2f} microns",
|
||||
)
|
||||
|
||||
console.print(table)
|
||||
|
||||
# Outermost zone width 30 nm -- diameters match flomni's 30nm FZP
|
||||
# (ffzp_info(), flomni_optics_mixin.py) now that the zone plate
|
||||
# mounted in LamNI is the same one mounted in Flomni.
|
||||
diameters = [140e-6, 170e-6, 200e-6, 250e-6]
|
||||
|
||||
console = Console()
|
||||
table = Table(title="Outermost zone width \033[1m30 nm\033[0m", box=box.SQUARE)
|
||||
table.add_column("Diameter", justify="center")
|
||||
table.add_column("Focal distance", justify="center")
|
||||
table.add_column("Current beam size", justify="center")
|
||||
|
||||
for diameter in diameters:
|
||||
outermost_zonewidth = 30e-9
|
||||
focal_distance = diameter * outermost_zonewidth / wavelength
|
||||
beam_size = (
|
||||
-diameter / (focal_distance * 1000) * (focal_distance * 1000 - distance) * 1e6
|
||||
)
|
||||
table.add_row(
|
||||
f"{diameter*1e6:.2f} microns",
|
||||
f"{focal_distance*1000:.2f} mm",
|
||||
f"{beam_size:.2f} microns",
|
||||
)
|
||||
|
||||
console.print(table)
|
||||
print(
|
||||
"The numbers presented here are for a sample in the plane of the lamni sample holder.\n"
|
||||
|
||||
@@ -29,6 +29,62 @@ if TYPE_CHECKING:
|
||||
from csaxs_bec.bec_ipython_client.plugins.LamNI.lamni import LamNI
|
||||
|
||||
|
||||
class SmearComposite:
|
||||
"""Running per-pixel projection of camera frames for the smear aid.
|
||||
|
||||
projection="max" keeps the brightest value per pixel. projection="min"
|
||||
keeps the darkest value per pixel, but skips pixels that are opaque in a
|
||||
given frame: darker than ``opaque_level`` times that frame's bright
|
||||
level (its 99th-percentile value). Without that, the sample holder,
|
||||
which is opaque and rotates through the field of view, would blacken
|
||||
every pixel it passes at any angle. A pixel that was opaque in every
|
||||
frame falls back to its plain minimum. opaque_level=0 gives the plain
|
||||
minimum everywhere.
|
||||
"""
|
||||
|
||||
def __init__(self, projection: str = "min", opaque_level: float = 0.25):
|
||||
if projection not in ("min", "max"):
|
||||
raise ValueError(f"projection must be 'min' or 'max', got {projection!r}")
|
||||
if not 0 <= opaque_level < 1:
|
||||
raise ValueError(f"opaque_level must be in [0, 1), got {opaque_level!r}")
|
||||
self.projection = projection
|
||||
self.opaque_level = opaque_level
|
||||
self._plain = None # plain min or max over all frames
|
||||
self._masked = None # min over non-opaque pixels only
|
||||
self._seen = None # True where a pixel was non-opaque at least once
|
||||
|
||||
@property
|
||||
def shape(self):
|
||||
return None if self._plain is None else self._plain.shape
|
||||
|
||||
@property
|
||||
def _plain_only(self) -> bool:
|
||||
return self.projection == "max" or self.opaque_level == 0
|
||||
|
||||
def add(self, frame: np.ndarray) -> None:
|
||||
if self._plain_only:
|
||||
reduce = np.maximum if self.projection == "max" else np.minimum
|
||||
self._plain = frame.copy() if self._plain is None else reduce(self._plain, frame)
|
||||
return
|
||||
open_px = frame > self.opaque_level * np.percentile(frame, 99)
|
||||
if self._plain is None:
|
||||
self._plain = frame.copy()
|
||||
self._masked = frame.copy()
|
||||
self._seen = open_px
|
||||
return
|
||||
self._plain = np.minimum(self._plain, frame)
|
||||
# first non-opaque sighting of a pixel takes its value; after that, min
|
||||
first = open_px & ~self._seen
|
||||
self._masked = np.where(first, frame, self._masked)
|
||||
self._masked = np.where(open_px, np.minimum(self._masked, frame), self._masked)
|
||||
self._seen |= open_px
|
||||
|
||||
def image(self) -> np.ndarray | None:
|
||||
if self._plain_only or self._plain is None:
|
||||
return self._plain
|
||||
return np.where(self._seen, self._masked, self._plain)
|
||||
|
||||
|
||||
# Laminography alignment angles: full 360° in 45° steps
|
||||
LAMNI_ALIGNMENT_ANGLES = [k * 45 for k in range(8)] # [0, 45, 90, 135, 180, 225, 270, 315]
|
||||
|
||||
@@ -336,6 +392,8 @@ class XrayEyeAlign:
|
||||
# __init__ for why this can't live there.
|
||||
self.lamni.reset_correction()
|
||||
self.lamni.reset_xray_eye_correction()
|
||||
self.lamni.manual_shift_x = 0
|
||||
self.lamni.manual_shift_y = 0
|
||||
|
||||
self.gui.enable_submit_button(False)
|
||||
|
||||
@@ -706,16 +764,61 @@ class XrayEyeAlign:
|
||||
|
||||
ROTATION_CENTER_SAMPLE_TYPES = ("isolated", "extended")
|
||||
|
||||
def _collect_click(self, k: int, message: str, label: str = "") -> tuple[float, float]:
|
||||
def _handle_rotation_center_arrows(self, keep_shutter_open: bool = False):
|
||||
"""Apply a pending GUI arrow-button move during rotation-center
|
||||
calibration.
|
||||
|
||||
mvx/mvy (um) are accumulated into self._rc_shift (mm, lamni coords,
|
||||
absolute offset from the configured lsamx/lsamy center) and applied
|
||||
via lamni_move_to_scan_center with angle=0.0: lsamx/lsamy sit below
|
||||
the rotation stage, so the move is a plain image translation at any
|
||||
lsamrot angle (see the math note above). The caller must map each
|
||||
click back to the configured-center frame using the shift active at
|
||||
click time.
|
||||
"""
|
||||
_mvx = dev.omny_xray_gui.mvx.get()
|
||||
_mvy = dev.omny_xray_gui.mvy.get()
|
||||
if _mvx == 0 and _mvy == 0:
|
||||
return
|
||||
self._rc_shift[0] += _mvx / 1000
|
||||
self._rc_shift[1] += _mvy / 1000
|
||||
self.scans.lamni_move_to_scan_center(
|
||||
shift_x=self._rc_shift[0], shift_y=self._rc_shift[1], angle=0.0
|
||||
).wait()
|
||||
print(
|
||||
f"[rotation-center] arrow move -- shift from configured center now "
|
||||
f"({self._rc_shift[0]:.4f}, {self._rc_shift[1]:.4f}) mm"
|
||||
)
|
||||
dev.omny_xray_gui.mvx.set(0)
|
||||
dev.omny_xray_gui.mvy.set(0)
|
||||
self.update_frame(keep_shutter_open)
|
||||
|
||||
def _collect_click(
|
||||
self,
|
||||
k: int,
|
||||
message: str,
|
||||
label: str = "",
|
||||
arrows: bool = False,
|
||||
keep_shutter_open: bool = False,
|
||||
) -> tuple[float, float]:
|
||||
"""Wait for the operator to submit a click at GUI step *k*.
|
||||
|
||||
If *arrows* is True, GUI arrow-button presses are applied while
|
||||
waiting (see _handle_rotation_center_arrows()).
|
||||
|
||||
Returns the click position in mm (converted via pixel_calibration).
|
||||
"""
|
||||
print(f"[rotation-center] step {k}: waiting for submit -- {message}")
|
||||
if arrows:
|
||||
# ignore any stale press from before this step
|
||||
dev.omny_xray_gui.mvx.set(0)
|
||||
dev.omny_xray_gui.mvy.set(0)
|
||||
dev.omny_xray_gui.step.set(k)
|
||||
self.send_message(message)
|
||||
self.gui.enable_submit_button(True)
|
||||
while True:
|
||||
if arrows:
|
||||
self._handle_rotation_center_arrows(keep_shutter_open)
|
||||
if dev.omny_xray_gui.submit.get() == 1:
|
||||
raw_x = getattr(dev.omny_xray_gui, f"xval_x_{k}").get()
|
||||
raw_y = getattr(dev.omny_xray_gui, f"yval_y_{k}").get()
|
||||
@@ -755,7 +858,7 @@ class XrayEyeAlign:
|
||||
print("[rotation-center] live sweep done")
|
||||
|
||||
# --- EXPERIMENTAL: rotation-center smear mode -------------------------
|
||||
# Max-projection "star-trail" composite aid for the extended-sample
|
||||
# Min- or max-projection "star-trail" composite aid for the extended-sample
|
||||
# rotation-center click, built while lsamrot rotates continuously.
|
||||
# Operator-visual only -- no automatic circle fitting. See
|
||||
# find_rotation_center_smear_experimental() below for the entry point.
|
||||
@@ -783,9 +886,21 @@ class XrayEyeAlign:
|
||||
keep_shutter_open: bool = False,
|
||||
display_update_interval_s: float = 1.0,
|
||||
poll_interval_s: float = 0.15,
|
||||
projection: str = "min",
|
||||
opaque_level: float = 0.25,
|
||||
) -> np.ndarray:
|
||||
"""Rotate lsamrot continuously through sweep_deg while accumulating
|
||||
a max-projection composite of camera frames grabbed along the way.
|
||||
a min- or max-projection composite of camera frames grabbed along the way.
|
||||
|
||||
projection="min" (default) keeps the darkest value per pixel and is
|
||||
meant for absorption contrast (dark features on a bright background):
|
||||
a max-projection would let the bright background that passes every
|
||||
pixel at some point wash those features out. projection="max" keeps
|
||||
the brightest value, for bright features on a dark background.
|
||||
With "min", pixels darker than opaque_level times a frame's bright
|
||||
level are treated as opaque (sample holder) in that frame and
|
||||
skipped, so the rotating holder does not black out the field of
|
||||
view; see SmearComposite. opaque_level=0 gives the plain minimum.
|
||||
|
||||
Off-axis features smear into circular arcs as the sample rotates;
|
||||
the common center of curvature of those arcs is the rotation axis
|
||||
@@ -816,9 +931,10 @@ class XrayEyeAlign:
|
||||
The shutter is closed unless keep_shutter_open, so the operator
|
||||
isn't exposing the sample while deciding where to click.
|
||||
"""
|
||||
accumulator = SmearComposite(projection=projection, opaque_level=opaque_level)
|
||||
print(
|
||||
f"[rotation-center][smear] EXPERIMENTAL feature: max-projection composite aid, "
|
||||
"operator-visual only -- no automatic circle fitting."
|
||||
f"[rotation-center][smear] EXPERIMENTAL feature: {projection}-projection composite "
|
||||
"aid, operator-visual only -- no automatic circle fitting."
|
||||
)
|
||||
if not dev.cam_xeye.live_mode_enabled.get():
|
||||
dev.cam_xeye.live_mode_enabled.put(True)
|
||||
@@ -836,7 +952,6 @@ class XrayEyeAlign:
|
||||
)
|
||||
report = mv(self.device_manager.devices.lsamrot, target)
|
||||
|
||||
composite = None
|
||||
last_frame = None
|
||||
frames_seen = 0
|
||||
last_push = time.time()
|
||||
@@ -848,22 +963,22 @@ class XrayEyeAlign:
|
||||
try:
|
||||
while report.status != "COMPLETED":
|
||||
frame = dev.cam_xeye.get_last_image()
|
||||
if composite is None:
|
||||
composite = frame.copy()
|
||||
if accumulator.shape is None:
|
||||
accumulator.add(frame)
|
||||
first_frame_time = time.time()
|
||||
elif frame.shape != composite.shape:
|
||||
elif frame.shape != accumulator.shape:
|
||||
print(
|
||||
f"[rotation-center][smear] skipping frame with mismatched shape "
|
||||
f"{frame.shape} != {composite.shape}"
|
||||
f"{frame.shape} != {accumulator.shape}"
|
||||
)
|
||||
else:
|
||||
if last_frame is None or not np.array_equal(frame, last_frame):
|
||||
frames_seen += 1
|
||||
last_distinct_time = time.time()
|
||||
composite = np.maximum(composite, frame)
|
||||
accumulator.add(frame)
|
||||
last_frame = frame
|
||||
if time.time() - last_push > display_update_interval_s:
|
||||
self._push_smear_composite(composite)
|
||||
self._push_smear_composite(accumulator.image())
|
||||
last_push = time.time()
|
||||
time.sleep(poll_interval_s)
|
||||
except KeyboardInterrupt:
|
||||
@@ -873,6 +988,7 @@ class XrayEyeAlign:
|
||||
self.gui.set_live_view_signal("image")
|
||||
raise
|
||||
|
||||
composite = accumulator.image()
|
||||
self._push_smear_composite(composite)
|
||||
self.gui.set_smear_active(False)
|
||||
if not keep_shutter_open:
|
||||
@@ -896,10 +1012,15 @@ class XrayEyeAlign:
|
||||
return composite
|
||||
|
||||
def find_rotation_center_smear_experimental(
|
||||
self, keep_shutter_open: bool = False, apply: bool = True, sweep_deg: float = 360.0
|
||||
self,
|
||||
keep_shutter_open: bool = False,
|
||||
apply: bool = True,
|
||||
sweep_deg: float = 360.0,
|
||||
projection: str = "min",
|
||||
opaque_level: float = 0.25,
|
||||
):
|
||||
"""EXPERIMENTAL: extended-sample rotation-center calibration aided
|
||||
by a continuous-rotation max-projection composite (see
|
||||
by a continuous-rotation min- or max-projection composite (see
|
||||
_smear_sweep()), instead of a single instant of live rotation.
|
||||
|
||||
Otherwise identical in structure to find_rotation_center(sample_type="extended"):
|
||||
@@ -915,10 +1036,19 @@ class XrayEyeAlign:
|
||||
sweep_deg: angular range of the continuous smear sweep. Defaults
|
||||
to a full 360 deg (complete arcs), but a smaller range may
|
||||
already show enough curvature to locate the center.
|
||||
projection: "min" (default) for absorption contrast, i.e. dark
|
||||
features on a bright background; "max" for bright features
|
||||
on a dark background. See _smear_sweep().
|
||||
opaque_level: with projection="min", pixels darker than this
|
||||
fraction of a frame's bright level are treated as opaque
|
||||
(sample holder) and skipped; 0 = plain minimum. See
|
||||
SmearComposite.
|
||||
|
||||
Returns:
|
||||
tuple: (new_lsamx_center, new_lsamy_center) in mm.
|
||||
"""
|
||||
# validate before anything moves
|
||||
SmearComposite(projection=projection, opaque_level=opaque_level)
|
||||
self._prompt_sample_name()
|
||||
self.lamni.lamnigui_show_xeyealign()
|
||||
self._sync_sample_name()
|
||||
@@ -935,7 +1065,8 @@ class XrayEyeAlign:
|
||||
|
||||
print(
|
||||
"[rotation-center][smear] starting find_rotation_center_smear_experimental"
|
||||
f"(sweep_deg={sweep_deg!r})"
|
||||
f"(sweep_deg={sweep_deg!r}, projection={projection!r}, "
|
||||
f"opaque_level={opaque_level!r})"
|
||||
)
|
||||
|
||||
self.send_message("Getting things ready. Please wait...")
|
||||
@@ -961,7 +1092,12 @@ class XrayEyeAlign:
|
||||
self.send_message(
|
||||
"Watch the sample rotate and locate the stationary rotation centre..."
|
||||
)
|
||||
composite = self._smear_sweep(sweep_deg=sweep_deg, keep_shutter_open=keep_shutter_open)
|
||||
composite = self._smear_sweep(
|
||||
sweep_deg=sweep_deg,
|
||||
keep_shutter_open=keep_shutter_open,
|
||||
projection=projection,
|
||||
opaque_level=opaque_level,
|
||||
)
|
||||
self.movement_buttons_enabled(True, True)
|
||||
center_x, center_y = self._collect_click(
|
||||
1,
|
||||
@@ -1046,6 +1182,8 @@ class XrayEyeAlign:
|
||||
fzp_xy=(fzp_x, fzp_y),
|
||||
new_center=(new_lsamx, new_lsamy),
|
||||
sweep_deg=sweep_deg,
|
||||
projection=projection,
|
||||
opaque_level=opaque_level,
|
||||
)
|
||||
|
||||
return (new_lsamx, new_lsamy)
|
||||
@@ -1056,6 +1194,8 @@ class XrayEyeAlign:
|
||||
fzp_xy: tuple[float, float],
|
||||
new_center: tuple[float, float],
|
||||
sweep_deg: float,
|
||||
projection: str = "min",
|
||||
opaque_level: float = 0.25,
|
||||
):
|
||||
"""Archive the raw record of a smear calibration run: one composite
|
||||
image and one clicked centre position per iteration, plus the FZP
|
||||
@@ -1074,6 +1214,8 @@ class XrayEyeAlign:
|
||||
ds.attrs["columns"] = ["center_x_mm", "center_y_mm"]
|
||||
f.create_dataset("new_center_mm", data=np.array(new_center))
|
||||
f.attrs["sweep_deg"] = sweep_deg
|
||||
f.attrs["projection"] = projection
|
||||
f.attrs["opaque_level"] = opaque_level
|
||||
print(f"[rotation-center][smear] saved calibration record to {expanded}")
|
||||
|
||||
# --- end EXPERIMENTAL: rotation-center smear mode ---------------------
|
||||
@@ -1204,7 +1346,9 @@ class XrayEyeAlign:
|
||||
submitted at 0 deg and again at 180 deg; the midpoint of the two
|
||||
is the rotation axis' projected position, independent of the
|
||||
axis tilt (see the module-level math note above). No visual
|
||||
judgement call and no iteration.
|
||||
judgement call and no iteration. The GUI arrow buttons work at
|
||||
both angles (e.g. if the particle starts outside the FOV); their
|
||||
lsamx/lsamy moves are tracked and subtracted from the clicks.
|
||||
|
||||
"extended" -- for a non-isolated/textured sample where the rotation
|
||||
centre can be visually identified directly. The sample is swept
|
||||
@@ -1266,19 +1410,47 @@ class XrayEyeAlign:
|
||||
self.gui.show_crosshair()
|
||||
|
||||
if sample_type == "isolated":
|
||||
# Running arrow-button shift from the configured center (stage is
|
||||
# there now, see _ensure_at_configured_center above).
|
||||
self._rc_shift = [0.0, 0.0]
|
||||
self.movement_buttons_enabled(True, True)
|
||||
p0 = self._collect_click(1, "Submit particle centre (0 deg).", label="particle @0deg")
|
||||
p0 = self._collect_click(
|
||||
1,
|
||||
"Submit particle centre (0 deg).",
|
||||
label="particle @0deg",
|
||||
arrows=True,
|
||||
keep_shutter_open=keep_shutter_open,
|
||||
)
|
||||
s0 = tuple(self._rc_shift)
|
||||
self._disable_rt_feedback()
|
||||
self.tomo_rotate(180)
|
||||
self.update_frame(keep_shutter_open)
|
||||
self.movement_buttons_enabled(True, True)
|
||||
p180 = self._collect_click(
|
||||
2, "Submit the SAME particle's centre (180 deg).", label="particle @180deg"
|
||||
2,
|
||||
"Submit the SAME particle's centre (180 deg).",
|
||||
label="particle @180deg",
|
||||
arrows=True,
|
||||
keep_shutter_open=keep_shutter_open,
|
||||
)
|
||||
center_x, center_y = (p0[0] + p180[0]) / 2, (p0[1] + p180[1]) / 2
|
||||
s180 = tuple(self._rc_shift)
|
||||
self.movement_buttons_enabled(False, False)
|
||||
# Map both clicks back to the configured-center frame: a shift s
|
||||
# moves the image by +s_x in x and -s_y in y (same convention as
|
||||
# _compute_shift_to_fzp). The midpoint is then the axis position
|
||||
# with the stage at the configured center, so the target below is
|
||||
# directly the absolute shift from that center.
|
||||
p0_ref = (p0[0] - s0[0], p0[1] + s0[1])
|
||||
p180_ref = (p180[0] - s180[0], p180[1] + s180[1])
|
||||
print(
|
||||
f"[rotation-center] midpoint of ({p0[0]:.4f},{p0[1]:.4f}) and "
|
||||
f"({p180[0]:.4f},{p180[1]:.4f}) = ({center_x:.4f},{center_y:.4f}) mm "
|
||||
"-- this is the rotation axis' current projected position"
|
||||
f"[rotation-center] arrow shift at 0 deg click: ({s0[0]:.4f},{s0[1]:.4f}) mm, "
|
||||
f"at 180 deg click: ({s180[0]:.4f},{s180[1]:.4f}) mm"
|
||||
)
|
||||
center_x, center_y = (p0_ref[0] + p180_ref[0]) / 2, (p0_ref[1] + p180_ref[1]) / 2
|
||||
print(
|
||||
f"[rotation-center] midpoint of ({p0_ref[0]:.4f},{p0_ref[1]:.4f}) and "
|
||||
f"({p180_ref[0]:.4f},{p180_ref[1]:.4f}) = ({center_x:.4f},{center_y:.4f}) mm "
|
||||
"-- this is the rotation axis' projected position at the configured center"
|
||||
)
|
||||
self._disable_rt_feedback()
|
||||
self.tomo_rotate(0)
|
||||
@@ -1295,7 +1467,7 @@ class XrayEyeAlign:
|
||||
)
|
||||
self._live_sweep([180, 0])
|
||||
self.update_frame(keep_shutter_open)
|
||||
self.movement_buttons_enabled(True, True)
|
||||
self.movement_buttons_enabled(False, False)
|
||||
center_x, center_y = self._collect_click(
|
||||
1,
|
||||
"Submit the rotation centre.",
|
||||
|
||||
@@ -0,0 +1,323 @@
|
||||
"""
|
||||
Parser for user-supplied tomography angle files (flomni tomo_type 4).
|
||||
|
||||
Pure Python, no BEC imports -- shared by the flomni CLI (tomo_scan(),
|
||||
tomo_parameters(), the tomo queue) and the TomoParams widget's live preview,
|
||||
so both validate a file identically.
|
||||
|
||||
File format: plain text, one projection per line.
|
||||
- column 1: angle in degrees, -2 <= angle <= 362 -- matches the physical
|
||||
overtravel range of the rotation stage, not just the nominal 0-360
|
||||
sweep
|
||||
- column 2 (optional): sub-tomogram number, integer >= 1 (default 1)
|
||||
- further, optional 'key=value' tokens overriding acquisition parameters
|
||||
for this projection and (sticky) every one after it -- see below
|
||||
Columns are separated by whitespace and/or commas. Everything after a '#' is a
|
||||
comment; blank lines are ignored. Projections are acquired in file order.
|
||||
|
||||
A line that, after comment-stripping and trimming, is 'END' (case-insensitive)
|
||||
marks the file as complete: no more angles will ever be appended, and
|
||||
tomo_scan() may stop once it reaches that point instead of waiting for more.
|
||||
A file without this marker is read as *incomplete* -- tomo_scan() waits and
|
||||
polls for more lines rather than stopping at the current end of file, so an
|
||||
angle file can be grown by appending lines while a scan is running. A
|
||||
commented-out '# END' is an ordinary comment, not the sentinel, which lets a
|
||||
marker be staged ahead of time and uncommented when the file is truly done.
|
||||
Any further non-blank/non-comment content after the sentinel line is an error.
|
||||
|
||||
Per-line acquisition overrides: after the angle and optional subtomo number,
|
||||
any number of 'key=value' tokens may override acquisition parameters that
|
||||
would otherwise come from the live tomo_parameters() configuration:
|
||||
- step=<um> Fermat scan step size, 0.025-20.0
|
||||
- exptime=<s> exposure/counting time per frame, 0.001-100.0
|
||||
- frames=<n> burst frame count, 1-100 (integer)
|
||||
Each of these is *sticky*: once set on a line, it applies to that line and
|
||||
every line after it, until changed again by a later 'key=value' of the same
|
||||
key -- not just the one line it appears on. A field that is never set on any
|
||||
line uses whatever the live tomo_shellstep/tomo_countingtime/frames_per_trigger
|
||||
configuration is at the moment that projection is actually acquired. The
|
||||
sub-tomogram number is NOT sticky (unchanged from today: per-line, default 1).
|
||||
|
||||
Example::
|
||||
|
||||
# angle subtomo
|
||||
0 1
|
||||
45 1 # still the default step/exptime/frames
|
||||
90 1 step=0.5 # step=0.5 from here on...
|
||||
135 1 # ...still in effect here
|
||||
180 2 step=2 exptime=5 frames=3
|
||||
225 2 # step/exptime/frames all still as set above
|
||||
270, 2
|
||||
END
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
|
||||
# Matches the physical overtravel range of the fsamroy rotation stage, not
|
||||
# just the nominal 0-360 sweep (see Flomni._min_scan_angle()/_max_scan_angle()
|
||||
# in flomni.py, which gate tomo_scan()'s actual acquisition to the same range).
|
||||
ANGLE_MIN = -2.0
|
||||
ANGLE_MAX = 362.0
|
||||
|
||||
# Two angles closer than this (mod 180) are reported as a 180-degree-opposite
|
||||
# pair: in the parallel-beam approximation they carry the same information.
|
||||
OPPOSITE_TOLERANCE_DEG = 0.05
|
||||
|
||||
# Per-line override ranges -- match the bounds tomo_parameters_cli() already
|
||||
# enforces interactively on tomo_shellstep/tomo_countingtime/frames_per_trigger
|
||||
# (flomni.py), so a value rejected here would also be rejected there.
|
||||
_STEP_RANGE = (0.025, 20.0)
|
||||
_EXPTIME_RANGE = (0.001, 100.0)
|
||||
_FRAMES_RANGE = (1, 100)
|
||||
_OVERRIDE_KEYS = ("exptime", "frames", "step")
|
||||
|
||||
_SPLIT_RE = re.compile(r"[\s,]+")
|
||||
_END_SENTINEL = "END"
|
||||
|
||||
|
||||
class AngleFileError(ValueError):
|
||||
"""Angle file missing, unreadable, or malformed."""
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class AngleEntry:
|
||||
"""One parsed projection: an angle/sub-tomogram pair, plus whichever
|
||||
acquisition parameters are overridden (sticky) at this point in the file.
|
||||
|
||||
step/exp_time/frames_per_trigger are None when not overridden by this or
|
||||
any earlier line -- callers should fall back to the live
|
||||
tomo_shellstep/tomo_countingtime/frames_per_trigger configuration in that
|
||||
case. Field names match the kwargs already used for these values
|
||||
elsewhere in flomni.py (the file's own 'exptime' token is the only name
|
||||
that differs, spelled out as exp_time here).
|
||||
"""
|
||||
|
||||
angle: float
|
||||
subtomo: int
|
||||
step: float | None = None
|
||||
exp_time: float | None = None
|
||||
frames_per_trigger: int | None = None
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class AngleFileResult:
|
||||
"""Result of parsing a tomo_type-4 angle file.
|
||||
|
||||
is_complete is True iff the file ends with an 'END' sentinel line, i.e.
|
||||
no more angles will ever be appended to it.
|
||||
"""
|
||||
|
||||
angles: list[AngleEntry]
|
||||
is_complete: bool
|
||||
|
||||
|
||||
def read_angle_file(path: str) -> AngleFileResult:
|
||||
"""Parse an angle file into an AngleFileResult, angles in file order.
|
||||
|
||||
Raises:
|
||||
AngleFileError: file missing/unreadable, a malformed line, an angle
|
||||
outside [0, 360], a sub-tomogram number < 1, no angles at all, or
|
||||
content found after the 'END' sentinel line.
|
||||
"""
|
||||
if not path:
|
||||
raise AngleFileError("No angle file set.")
|
||||
full_path = os.path.abspath(os.path.expanduser(path))
|
||||
try:
|
||||
with open(full_path, "r", encoding="utf-8") as f:
|
||||
lines = f.readlines()
|
||||
except OSError as exc:
|
||||
raise AngleFileError(f"Cannot read angle file {full_path}: {exc}") from exc
|
||||
|
||||
entries: list[AngleEntry] = []
|
||||
is_complete = False
|
||||
end_lineno = None
|
||||
# Sticky override state, carried forward top-to-bottom across the whole
|
||||
# file -- None means "no override in effect, use the live global".
|
||||
sticky_step: float | None = None
|
||||
sticky_exp_time: float | None = None
|
||||
sticky_frames: int | None = None
|
||||
for lineno, raw in enumerate(lines, start=1):
|
||||
line = raw.split("#", 1)[0].strip()
|
||||
if not line:
|
||||
continue
|
||||
if is_complete:
|
||||
raise AngleFileError(
|
||||
f"{full_path}, line {lineno}: content found after the 'END' "
|
||||
f"marker (line {end_lineno}) -- remove it or move it above 'END'."
|
||||
)
|
||||
if line.upper() == _END_SENTINEL:
|
||||
is_complete = True
|
||||
end_lineno = lineno
|
||||
continue
|
||||
fields = [f for f in _SPLIT_RE.split(line) if f]
|
||||
try:
|
||||
angle = float(fields[0])
|
||||
except ValueError as exc:
|
||||
raise AngleFileError(
|
||||
f"{full_path}, line {lineno}: invalid angle {fields[0]!r}"
|
||||
) from exc
|
||||
if not ANGLE_MIN <= angle <= ANGLE_MAX:
|
||||
raise AngleFileError(
|
||||
f"{full_path}, line {lineno}: angle {angle} outside "
|
||||
f"[{ANGLE_MIN:g}, {ANGLE_MAX:g}] degrees"
|
||||
)
|
||||
|
||||
rest = fields[1:]
|
||||
subtomo = 1
|
||||
if rest and "=" not in rest[0]:
|
||||
# positional subtomo -- must come immediately after the angle,
|
||||
# before any key=value tokens
|
||||
token = rest[0]
|
||||
try:
|
||||
subtomo = int(token)
|
||||
except ValueError as exc:
|
||||
raise AngleFileError(
|
||||
f"{full_path}, line {lineno}: invalid sub-tomogram number {token!r}"
|
||||
) from exc
|
||||
if subtomo < 1:
|
||||
raise AngleFileError(
|
||||
f"{full_path}, line {lineno}: sub-tomogram number must be >= 1, got {subtomo}"
|
||||
)
|
||||
rest = rest[1:]
|
||||
|
||||
seen_keys: set[str] = set()
|
||||
for token in rest:
|
||||
if "=" not in token:
|
||||
raise AngleFileError(
|
||||
f"{full_path}, line {lineno}: expected 'key=value', got {token!r}"
|
||||
)
|
||||
key, _, value_str = token.partition("=")
|
||||
key, value_str = key.strip(), value_str.strip()
|
||||
if not key or not value_str:
|
||||
raise AngleFileError(
|
||||
f"{full_path}, line {lineno}: malformed 'key=value' token {token!r}"
|
||||
)
|
||||
if key not in _OVERRIDE_KEYS:
|
||||
raise AngleFileError(
|
||||
f"{full_path}, line {lineno}: unknown key {key!r} "
|
||||
f"(expected one of {', '.join(_OVERRIDE_KEYS)})"
|
||||
)
|
||||
if key in seen_keys:
|
||||
raise AngleFileError(
|
||||
f"{full_path}, line {lineno}: duplicate key {key!r} on the same line"
|
||||
)
|
||||
seen_keys.add(key)
|
||||
|
||||
if key == "frames":
|
||||
try:
|
||||
frames_val = int(value_str)
|
||||
except ValueError as exc:
|
||||
raise AngleFileError(
|
||||
f"{full_path}, line {lineno}: invalid frames value {value_str!r}"
|
||||
) from exc
|
||||
lo, hi = _FRAMES_RANGE
|
||||
if not lo <= frames_val <= hi:
|
||||
raise AngleFileError(
|
||||
f"{full_path}, line {lineno}: frames={frames_val} outside [{lo}, {hi}]"
|
||||
)
|
||||
sticky_frames = frames_val
|
||||
else:
|
||||
try:
|
||||
num_val = float(value_str)
|
||||
except ValueError as exc:
|
||||
raise AngleFileError(
|
||||
f"{full_path}, line {lineno}: invalid {key} value {value_str!r}"
|
||||
) from exc
|
||||
lo, hi = _STEP_RANGE if key == "step" else _EXPTIME_RANGE
|
||||
if not lo <= num_val <= hi:
|
||||
raise AngleFileError(
|
||||
f"{full_path}, line {lineno}: {key}={num_val:g} outside [{lo:g}, {hi:g}]"
|
||||
)
|
||||
if key == "step":
|
||||
sticky_step = num_val
|
||||
else:
|
||||
sticky_exp_time = num_val
|
||||
|
||||
entries.append(
|
||||
AngleEntry(
|
||||
angle=angle,
|
||||
subtomo=subtomo,
|
||||
step=sticky_step,
|
||||
exp_time=sticky_exp_time,
|
||||
frames_per_trigger=sticky_frames,
|
||||
)
|
||||
)
|
||||
|
||||
if not entries:
|
||||
raise AngleFileError(f"Angle file {full_path} contains no angles.")
|
||||
return AngleFileResult(angles=entries, is_complete=is_complete)
|
||||
|
||||
|
||||
def summarize_angles(entries: list[AngleEntry]) -> dict:
|
||||
"""Summary of a parsed angle list for display/warnings.
|
||||
|
||||
Returns a dict with n, min, max, n_subtomos, n_overridden, and
|
||||
opposite_pairs -- a list of (index_a, index_b, angle_a, angle_b) for
|
||||
projections that are (nearly) 180 degrees apart or identical mod 180,
|
||||
i.e. redundant in the parallel-beam approximation. n_overridden is the
|
||||
number of entries carrying a non-default step/exp_time/frames_per_trigger.
|
||||
"""
|
||||
angles = [e.angle for e in entries]
|
||||
subtomos = {e.subtomo for e in entries}
|
||||
n_overridden = sum(
|
||||
1
|
||||
for e in entries
|
||||
if e.step is not None or e.exp_time is not None or e.frames_per_trigger is not None
|
||||
)
|
||||
|
||||
# Sort by angle mod 180 so near-duplicates end up adjacent (plus the
|
||||
# wrap-around pair between last and first): O(n log n) instead of O(n^2).
|
||||
folded = sorted((a % 180.0, i) for i, a in enumerate(angles))
|
||||
opposite_pairs = []
|
||||
n = len(folded)
|
||||
for k in range(n):
|
||||
f_a, i_a = folded[k]
|
||||
for m in range(k + 1, n + k):
|
||||
f_b, i_b = folded[m % n]
|
||||
gap = f_b - f_a if m < n else f_b + 180.0 - f_a
|
||||
if gap >= OPPOSITE_TOLERANCE_DEG:
|
||||
break
|
||||
pair = (min(i_a, i_b), max(i_a, i_b))
|
||||
opposite_pairs.append((pair[0], pair[1], angles[pair[0]], angles[pair[1]]))
|
||||
opposite_pairs = sorted(set(opposite_pairs))
|
||||
|
||||
return {
|
||||
"n": len(angles),
|
||||
"min": min(angles),
|
||||
"max": max(angles),
|
||||
"n_subtomos": len(subtomos),
|
||||
"n_overridden": n_overridden,
|
||||
"opposite_pairs": opposite_pairs,
|
||||
}
|
||||
|
||||
|
||||
def describe_summary(summary: dict) -> str:
|
||||
"""One-line human-readable summary, e.g. '120 angles, 0.0-359.0 deg, 4 sub-tomograms'.
|
||||
|
||||
Appends ', N with per-line overrides' only when N > 0, so a file with no
|
||||
step/exptime/frames overrides prints exactly as before this feature.
|
||||
"""
|
||||
text = (
|
||||
f"{summary['n']} angles, {summary['min']:.2f}-{summary['max']:.2f} deg, "
|
||||
f"{summary['n_subtomos']} sub-tomogram{'s' if summary['n_subtomos'] != 1 else ''}"
|
||||
)
|
||||
if summary.get("n_overridden"):
|
||||
text += f", {summary['n_overridden']} with per-line overrides"
|
||||
return text
|
||||
|
||||
|
||||
def describe_opposite_pairs(summary: dict, max_listed: int = 5) -> str | None:
|
||||
"""Warning text for 180-degree-opposite/duplicate pairs, or None if there are none."""
|
||||
pairs = summary["opposite_pairs"]
|
||||
if not pairs:
|
||||
return None
|
||||
listed = ", ".join(f"{a:g}/{b:g}" for _, _, a, b in pairs[:max_listed])
|
||||
more = f" (+{len(pairs) - max_listed} more)" if len(pairs) > max_listed else ""
|
||||
return (
|
||||
f"{len(pairs)} pair(s) of angles are identical or 180 deg apart "
|
||||
f"(redundant in parallel-beam geometry): {listed}{more}"
|
||||
)
|
||||
@@ -15,12 +15,12 @@ Two browser pages backed by one JSON file:
|
||||
1. **Machine status** — ring current, lifetime, light delivery, undulator gap,
|
||||
beamline status, orbit feedback, injection rate, ring vacuum.
|
||||
2. **Operator messages** — the SLS control-room message board.
|
||||
3. **Alarm status** — the EPS alarm banner (green when `AlarmCnt_EPS == 0`,
|
||||
red with the alarm text otherwise), plus front-end alarms.
|
||||
4. **Mono cryocooler** — run / alarm / inhibit / interlock / heartbeat.
|
||||
5. **Beamline synoptic** — a link to the full synoptic page.
|
||||
6. **Zone detail** — FE / OP / ES / EH1 cards, each a severity-colored summary
|
||||
3. **Mono cryocooler** — run / alarm / inhibit / interlock / heartbeat.
|
||||
4. **Beamline synoptic** — a link to the full synoptic page.
|
||||
5. **Zone detail** — FE / OP / ES / EH1 cards, each a severity-colored summary
|
||||
with an expandable table of every channel.
|
||||
6. **Alarm status** — the EPS alarm banner (green when `AlarmCnt_EPS == 0`,
|
||||
red with the alarm text otherwise), plus front-end alarms.
|
||||
7. **Recent history** — the last 5 EPS + machine events.
|
||||
|
||||
**`eps_synoptic.html`** — a faithful replica of the caQtDM EPS expert panel
|
||||
@@ -294,7 +294,7 @@ IP-whitelisted upload all apply unchanged.
|
||||
|
||||
- **`--verify` on a running beamline** (this was done during shutdown). Re-run
|
||||
when the machine is up to confirm the FE photon-shutter PVs
|
||||
(`X12SA-EPS-PH:SYSTEM`, `X12SA-FE-PH1:OPEN_EPS/CLOSE_EPS`) come back.
|
||||
(`X12SA-EPS-PH:SYSTEM`) comes back (the FE-PH1 OPEN/CLOSE_EPS PVs were dropped; FE shutter state now comes from `X12SA-FE-PSH1-EMLS:STATE`).
|
||||
- **Cryocooler detail.** The card shows run/alarm/inhibit/heartbeat. Full
|
||||
temperature/pressure detail needs `XDS_CRYOCOOLER_OVERVIEW.ui` (a sub-template
|
||||
not yet supplied).
|
||||
|
||||
@@ -28,8 +28,6 @@ HOT_PVS = [
|
||||
("X12SA-EH1-PSYS:SH-A-OK", "sh_eh1_a_ok", "EH1 shutter A OK"),
|
||||
("X12SA-OP-PSYS:ALARM-COLLECTIVE", "psys_op_alarm", "OP PSYS collective alarm"),
|
||||
("X12SA-EH1-PSYS:ALARM-COLLECTIVE", "psys_eh1_alarm", "EH1 PSYS collective alarm"),
|
||||
("X12SA-FE-PH1:OPEN_EPS", "fe_ph1_open", "FE photon shutter open"),
|
||||
("X12SA-FE-PH1:CLOSE_EPS", "fe_ph1_closed", "FE photon shutter closed"),
|
||||
("AGEBD-PARAMS:CURRENT", "ring_current", "SLS ring current"),
|
||||
("AGEBD-PARAMS:LIFETIME", "lifetime", "Beam lifetime"),
|
||||
("AGEOP-STATUS:LIGHT-DELIVERY", "light_delivery", "Light delivery state"),
|
||||
@@ -362,6 +360,9 @@ def all_bulk_pvs():
|
||||
if pv not in seen: seen.add(pv); out.append(pv)
|
||||
for pv, _ in MACHINE_PVS:
|
||||
if pv not in seen: seen.add(pv); out.append(pv)
|
||||
for pvs in OPMSG_PVS:
|
||||
for pv in pvs:
|
||||
if pv not in seen: seen.add(pv); out.append(pv)
|
||||
return out
|
||||
|
||||
def all_pvs():
|
||||
|
||||
@@ -107,9 +107,6 @@
|
||||
<h2>Operator Messages</h2>
|
||||
<div class="panel"><div id="messages"></div></div>
|
||||
|
||||
<h2>Alarm Status</h2>
|
||||
<div id="hero" class="hero unknown"><div class="hero-title">—</div></div>
|
||||
|
||||
<h2>Mono Cryocooler</h2>
|
||||
<div class="panel"><div class="cryo" id="cryo"></div></div>
|
||||
|
||||
@@ -127,6 +124,9 @@
|
||||
<h2>Zone Detail</h2>
|
||||
<div class="zones" id="zones"></div>
|
||||
|
||||
<h2>Alarm Status</h2>
|
||||
<div id="hero" class="hero unknown"><div class="hero-title">—</div></div>
|
||||
|
||||
<h2>Recent History</h2>
|
||||
<div class="panel" id="history"></div>
|
||||
|
||||
|
||||
@@ -875,9 +875,15 @@ class EpsStatusGenerator:
|
||||
"b_closed": hv("X12SA-OP-PSYS:SH-B-CLOSE"),
|
||||
"b_ok": hv("X12SA-OP-PSYS:SH-B-OK"),
|
||||
})
|
||||
# The old X12SA-FE-PH1:OPEN_EPS/CLOSE_EPS PVs no longer exist; derive
|
||||
# open/closed from the front-end shutter end-switch state (enum
|
||||
# MOVING/OPEN/CLOSED, may arrive as index or string).
|
||||
fe_state = (self._bulk_cache.get("X12SA-FE-PSH1-EMLS:STATE") or {}).get("value")
|
||||
fe_state = {1: "OPEN", 2: "CLOSED"}.get(fe_state, fe_state)
|
||||
fe_known = isinstance(fe_state, str) and fe_state.upper() in ("OPEN", "CLOSED", "MOVING")
|
||||
shutters["FE"] = {
|
||||
"a_open": hv("X12SA-FE-PH1:OPEN_EPS"),
|
||||
"a_closed": hv("X12SA-FE-PH1:CLOSE_EPS"),
|
||||
"a_open": (fe_state.upper() == "OPEN") if fe_known else None,
|
||||
"a_closed": (fe_state.upper() == "CLOSED") if fe_known else None,
|
||||
"a_ok": None,
|
||||
"collective_alarm": None,
|
||||
}
|
||||
|
||||
@@ -0,0 +1,321 @@
|
||||
"""
|
||||
Shared tomo_type 4 ("angles from file") support for the flomni and LamNI
|
||||
tomography classes.
|
||||
|
||||
The file format and parser live in angle_file.py. This mixin holds everything
|
||||
that is identical between the setups: the tomo_angle_file global-var property,
|
||||
parsing/summary helpers, the per-line override plumbing and the acquisition
|
||||
loop that follows (and waits for) a growing angle file.
|
||||
|
||||
Host-class contract:
|
||||
* _TOMO_PARAMS_ERROR exception class raised on any problem
|
||||
* _ANGLE_FILE_OPPOSITE_WARNING bool, report 180-deg-opposite/duplicate angles
|
||||
(parallel-beam redundancy -- off for laminography)
|
||||
* _tomo_scan_at_angle(angle, subtomo_number, **overrides)
|
||||
* progress, OMNYTools, client, at_each_angle_hook
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import builtins
|
||||
import datetime
|
||||
import os
|
||||
import time
|
||||
|
||||
from csaxs_bec.bec_ipython_client.plugins.OMNY_shared.angle_file import (
|
||||
AngleEntry,
|
||||
AngleFileError,
|
||||
AngleFileResult,
|
||||
describe_opposite_pairs,
|
||||
describe_summary,
|
||||
read_angle_file,
|
||||
summarize_angles,
|
||||
)
|
||||
|
||||
|
||||
class TomoAngleFileMixin:
|
||||
# tomo_type 4: how often to re-read tomo_angle_file while waiting for
|
||||
# more lines to be appended, once the scan has caught up to its end.
|
||||
_TOMO_ANGLE_FILE_POLL_INTERVAL_S = 5.0
|
||||
_ANGLE_FILE_OPPOSITE_WARNING = True
|
||||
# Name of the setup's legacy at-each-angle builtin, or None (ignored-overrides warning).
|
||||
_AT_EACH_ANGLE_BUILTIN = "flomni_at_each_angle"
|
||||
# Angle range accepted by tomo_type 4 (mechanical overtravel); the
|
||||
# parser enforces the same limits.
|
||||
_ANGLE_FILE_MIN = -2
|
||||
_ANGLE_FILE_MAX = 362
|
||||
|
||||
@property
|
||||
def tomo_angle_file(self):
|
||||
"""Path of the angle file used by tomo_type 4 (format: see
|
||||
OMNY_shared/angle_file.py). Only the path is stored -- the file is
|
||||
read at tomo_scan() start, including resume and queued jobs, so an
|
||||
edited file always takes effect on the next (re)start. While a scan
|
||||
is running, it is also polled and re-read whenever the scan catches
|
||||
up to the current end of file, so new lines appended during a scan
|
||||
are picked up automatically -- unless the file ends with an 'END'
|
||||
sentinel line, the scan waits indefinitely for more lines once it
|
||||
reaches the end rather than stopping."""
|
||||
val = self.client.get_global_var("tomo_angle_file")
|
||||
if val is None:
|
||||
return ""
|
||||
return val
|
||||
|
||||
@tomo_angle_file.setter
|
||||
def tomo_angle_file(self, val: str):
|
||||
# Deliberately no existence/format check here: tomo_queue_execute()
|
||||
# setattr()s every snapshotted param for every job, so validating here
|
||||
# would make e.g. a type-1 job fail because of a stale, irrelevant
|
||||
# angle file path. Validation happens where type 4 actually uses it
|
||||
# (tomo_scan(), tomo_parameters(), tomo_queue_reacquire()).
|
||||
val = str(val or "").strip()
|
||||
if val:
|
||||
val = os.path.abspath(os.path.expanduser(val))
|
||||
self.client.set_global_var("tomo_angle_file", val)
|
||||
|
||||
def _read_tomo_angle_file(self) -> AngleFileResult:
|
||||
"""Parse self.tomo_angle_file, raising _TOMO_PARAMS_ERROR on any problem."""
|
||||
try:
|
||||
return read_angle_file(self.tomo_angle_file)
|
||||
except AngleFileError as exc:
|
||||
raise self._TOMO_PARAMS_ERROR(str(exc)) from exc
|
||||
|
||||
@staticmethod
|
||||
def _tomo_angle_subtomo_layout(entries: list[AngleEntry]):
|
||||
"""subtomo_sizes (label -> count) and index_in_subtomo (per-entry
|
||||
position within its own sub-tomogram), for the progress display."""
|
||||
subtomo_sizes: dict[int, int] = {}
|
||||
index_in_subtomo: list[int] = []
|
||||
for entry in entries:
|
||||
index_in_subtomo.append(subtomo_sizes.get(entry.subtomo, 0))
|
||||
subtomo_sizes[entry.subtomo] = subtomo_sizes.get(entry.subtomo, 0) + 1
|
||||
return subtomo_sizes, index_in_subtomo
|
||||
|
||||
@staticmethod
|
||||
def _angle_overrides_kwargs(step=None, exp_time=None, frames_per_trigger=None) -> dict:
|
||||
"""Only the non-None overrides, as kwargs to splat into
|
||||
_at_each_angle()/_tomo_scan_at_angle() -- when nothing is overridden
|
||||
this is {}, so the call is byte-identical to the pre-override-feature
|
||||
call (no extra kwargs reach fixed-arity stubs/hooks)."""
|
||||
return {
|
||||
k: v
|
||||
for k, v in (
|
||||
("step", step),
|
||||
("exp_time", exp_time),
|
||||
("frames_per_trigger", frames_per_trigger),
|
||||
)
|
||||
if v is not None
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _format_angle_entry(entry: AngleEntry) -> str:
|
||||
"""'angle subtomo [step=.. exptime=.. frames=..]' for scilog/logging."""
|
||||
text = f"{entry.angle:g} {entry.subtomo}"
|
||||
if entry.step is not None:
|
||||
text += f" step={entry.step:g}"
|
||||
if entry.exp_time is not None:
|
||||
text += f" exptime={entry.exp_time:g}"
|
||||
if entry.frames_per_trigger is not None:
|
||||
text += f" frames={entry.frames_per_trigger}"
|
||||
return text
|
||||
|
||||
def _warn_if_hook_ignores_overrides(self, entries: list[AngleEntry]) -> None:
|
||||
"""Print a one-time warning if a custom at_each_angle_hook/
|
||||
<setup>_at_each_angle is active while entries carry step/exp_time/
|
||||
frames_per_trigger overrides -- those hooks own the whole
|
||||
acquisition (fixed 2-arg contract) and never see the overrides."""
|
||||
hook_active = bool(self.at_each_angle_hook) or bool(
|
||||
self._AT_EACH_ANGLE_BUILTIN and self._AT_EACH_ANGLE_BUILTIN in builtins.__dict__
|
||||
)
|
||||
if hook_active and any(
|
||||
e.step is not None or e.exp_time is not None or e.frames_per_trigger is not None
|
||||
for e in entries
|
||||
):
|
||||
self.OMNYTools.printredbold(
|
||||
"Warning: an at_each_angle hook is active -- per-line step/exptime/frames "
|
||||
"overrides in the angle file are ignored (the hook owns the whole acquisition)."
|
||||
)
|
||||
|
||||
def _print_opposite_pairs_warning(self, summary) -> None:
|
||||
if not self._ANGLE_FILE_OPPOSITE_WARNING:
|
||||
return
|
||||
warning = describe_opposite_pairs(summary)
|
||||
if warning:
|
||||
self.OMNYTools.printredbold(f"Warning: {warning}")
|
||||
|
||||
def _min_scan_angle(self) -> float:
|
||||
"""Lower angle limit (inclusive) accepted by _tomo_scan_at_angle().
|
||||
tomo_type 4 files may contain any angle in the mechanical overtravel
|
||||
range, not just the nominal 0-360 sweep."""
|
||||
if self.tomo_type == 4:
|
||||
return self._ANGLE_FILE_MIN
|
||||
return 0
|
||||
|
||||
def _max_scan_angle(self) -> float:
|
||||
"""Upper angle limit (inclusive) accepted by _tomo_scan_at_angle()."""
|
||||
if self.tomo_type == 4:
|
||||
return self._ANGLE_FILE_MAX
|
||||
return self._default_max_scan_angle()
|
||||
|
||||
def _default_max_scan_angle(self) -> float:
|
||||
"""Upper limit for tomo types 1-3; flomni overrides (tomo_angle_range)."""
|
||||
return 360
|
||||
|
||||
def _start_angle_file_scan(self, projection_number) -> AngleFileResult:
|
||||
"""Read + report the angle file at tomo_scan() start (before any gate
|
||||
or motion), so a missing/malformed file fails immediately -- and, in
|
||||
the tomo queue, marks the job incomplete with this message. The file
|
||||
need not be complete (end with 'END') yet -- that is the normal
|
||||
starting state for a file meant to be appended to while the scan runs."""
|
||||
file_angles = self._read_tomo_angle_file()
|
||||
summary = summarize_angles(file_angles.angles)
|
||||
print(f"Angle file {self.tomo_angle_file}: {describe_summary(summary)}")
|
||||
if not file_angles.is_complete:
|
||||
print(
|
||||
"Angle file has no 'END' marker yet -- the scan will wait for "
|
||||
"more angles to be appended once it reaches the end of the file."
|
||||
)
|
||||
self._print_opposite_pairs_warning(summary)
|
||||
self._warn_if_hook_ignores_overrides(file_angles.angles)
|
||||
if projection_number is not None and projection_number >= len(file_angles.angles):
|
||||
raise self._TOMO_PARAMS_ERROR(
|
||||
f"Cannot resume at projection {projection_number}: the angle file now "
|
||||
f"contains only {len(file_angles.angles)} angles."
|
||||
)
|
||||
return file_angles
|
||||
|
||||
def _init_angle_file_progress(self, file_angles: AngleFileResult, projection_number) -> None:
|
||||
"""Fill the progress fields that are already known from the file, so the
|
||||
GUI shows the real tomo type/total (and sub-tomogram 1) during the
|
||||
preparation steps and the 0-deg reference shot that precede the loop,
|
||||
instead of the blank reset defaults (tomo type 0, projection 0)."""
|
||||
self.progress["tomo_type"] = "Angles from file"
|
||||
self.progress["total_projections"] = (
|
||||
len(file_angles.angles) if file_angles.is_complete else 0
|
||||
)
|
||||
if projection_number is None:
|
||||
self.progress["subtomo"] = 1
|
||||
|
||||
def _run_angle_file_loop(self, file_angles: AngleFileResult, projection_number) -> None:
|
||||
"""Acquire the angles of tomo_type 4 in file order (no extra 0-deg
|
||||
damage-estimation shots: sub-tomogram numbers in a user file are just
|
||||
labels, put 0-deg lines in the file instead). Waits for lines to be
|
||||
appended while the file has no 'END' marker."""
|
||||
error = self._TOMO_PARAMS_ERROR
|
||||
self.progress["tomo_type"] = "Angles from file"
|
||||
angles = file_angles.angles
|
||||
is_complete = file_angles.is_complete
|
||||
ii = 0 if projection_number is None else projection_number
|
||||
subtomo_sizes, index_in_subtomo = self._tomo_angle_subtomo_layout(angles)
|
||||
self.progress["total_projections"] = len(angles) if is_complete else 0
|
||||
waiting = False
|
||||
|
||||
while True:
|
||||
if ii >= len(angles):
|
||||
if is_complete:
|
||||
break
|
||||
# Caught up to the current end of file, which isn't marked
|
||||
# complete ('END') -- wait and poll instead of stopping, so
|
||||
# lines appended while this scan is running are picked up
|
||||
# without needing tomo_scan_resume().
|
||||
if not waiting:
|
||||
print(
|
||||
f"Reached the end of {self.tomo_angle_file} "
|
||||
f"({len(angles)} angle(s), no 'END' marker yet) -- "
|
||||
"waiting for more lines to be appended. Ctrl-C to abort."
|
||||
)
|
||||
waiting = True
|
||||
tick_start = time.perf_counter()
|
||||
try:
|
||||
time.sleep(self._TOMO_ANGLE_FILE_POLL_INTERVAL_S)
|
||||
except KeyboardInterrupt:
|
||||
raise error(
|
||||
"tomo_scan() aborted while waiting for the angle "
|
||||
f"file {self.tomo_angle_file} to grow past "
|
||||
f"projection {ii}. Resume with tomo_scan_resume() "
|
||||
"once more angles (or an 'END' marker) have been "
|
||||
"added."
|
||||
) from None
|
||||
# Keep the heartbeat fresh so the GUI/webpage staleness
|
||||
# detectors (120s/90s windows) don't flag this scan as dead
|
||||
# while it's intentionally idle -- and since that keeps the
|
||||
# gap _tomo_scan_at_angle() would otherwise see small,
|
||||
# account the wait as idle time here explicitly.
|
||||
self.progress["heartbeat"] = datetime.datetime.now().isoformat()
|
||||
self.progress["accumulated_idle_time"] = self.progress.get(
|
||||
"accumulated_idle_time", 0.0
|
||||
) + (time.perf_counter() - tick_start)
|
||||
new_result = self._read_tomo_angle_file()
|
||||
if new_result.angles[: len(angles)] != angles:
|
||||
raise error(
|
||||
f"{self.tomo_angle_file} changed: an already-"
|
||||
f"acquired line (index < {len(angles)}) was edited "
|
||||
"or removed. Fix the file (only append new lines "
|
||||
"after the last acquired one) and resume with "
|
||||
"tomo_scan_resume()."
|
||||
)
|
||||
if len(new_result.angles) > len(angles):
|
||||
print(
|
||||
f"Picked up {len(new_result.angles) - len(angles)} "
|
||||
f"new angle(s) from {self.tomo_angle_file} "
|
||||
f"(total now {len(new_result.angles)})."
|
||||
)
|
||||
self._warn_if_hook_ignores_overrides(new_result.angles[len(angles) :])
|
||||
waiting = False
|
||||
angles = new_result.angles
|
||||
subtomo_sizes, index_in_subtomo = self._tomo_angle_subtomo_layout(angles)
|
||||
is_complete = new_result.is_complete
|
||||
self.progress["total_projections"] = len(angles) if is_complete else 0
|
||||
if is_complete:
|
||||
summary = summarize_angles(angles)
|
||||
print(f"Angle file complete: {describe_summary(summary)}")
|
||||
self._print_opposite_pairs_warning(summary)
|
||||
continue
|
||||
|
||||
entry = angles[ii]
|
||||
self.progress["subtomo"] = entry.subtomo
|
||||
self.progress["projection"] = ii
|
||||
self.progress["angle"] = entry.angle
|
||||
self.progress["subtomo_total_projections"] = subtomo_sizes[entry.subtomo]
|
||||
self.progress["subtomo_projection"] = index_in_subtomo[ii]
|
||||
overrides = self._angle_overrides_kwargs(
|
||||
entry.step, entry.exp_time, entry.frames_per_trigger
|
||||
)
|
||||
self._tomo_scan_at_angle(entry.angle, entry.subtomo, **overrides)
|
||||
ii += 1
|
||||
|
||||
def _print_angle_file_summary(self) -> bool:
|
||||
"""Print path + parsed summary (+ 180-deg-pair warning if enabled) of
|
||||
tomo_angle_file. Returns False (after printing the error) if the file
|
||||
can't be used, instead of raising."""
|
||||
print(f"Angle file: {self.tomo_angle_file or '(not set)'}")
|
||||
try:
|
||||
result = read_angle_file(self.tomo_angle_file)
|
||||
except AngleFileError as exc:
|
||||
self.OMNYTools.printredbold(f"Angle file not usable: {exc}")
|
||||
return False
|
||||
summary = summarize_angles(result.angles)
|
||||
print(describe_summary(summary))
|
||||
if not result.is_complete:
|
||||
print("No 'END' marker yet -- a scan will wait for more angles once started.")
|
||||
self._print_opposite_pairs_warning(summary)
|
||||
return True
|
||||
|
||||
def _prompt_angle_file(self) -> None:
|
||||
"""Ask for tomo_angle_file until it parses, or the user presses
|
||||
enter to keep the current value."""
|
||||
while True:
|
||||
val = input(f"Angle file ({self.tomo_angle_file}): ").strip()
|
||||
if not val:
|
||||
if not self._print_angle_file_summary():
|
||||
print("Keeping it anyway -- the scan will refuse to start until it is fixed.")
|
||||
return
|
||||
try:
|
||||
read_angle_file(val)
|
||||
except AngleFileError as exc:
|
||||
self.OMNYTools.printredbold(str(exc))
|
||||
print("Please try again (enter keeps the current value).")
|
||||
continue
|
||||
self.tomo_angle_file = val
|
||||
self._print_angle_file_summary()
|
||||
return
|
||||
@@ -47,6 +47,8 @@ from typing import Callable
|
||||
|
||||
from typeguard import check_type
|
||||
|
||||
from csaxs_bec.bec_ipython_client.plugins.OMNY_shared.angle_file import read_angle_file
|
||||
|
||||
if builtins.__dict__.get("bec") is not None:
|
||||
dev = builtins.__dict__.get("dev")
|
||||
umv = builtins.__dict__.get("umv")
|
||||
@@ -216,6 +218,9 @@ class TomoQueueMixin:
|
||||
_TOMO_PARAMS_WINDOW_NAME: str = ""
|
||||
_TOMO_PARAMS_ERROR: type[Exception] = RuntimeError
|
||||
_TOMO_PARAMS_GUI_TIMEOUT = 20
|
||||
# (pos_x, pos_y, width, height) of the tomo parameter window when it is
|
||||
# created: tall and narrow, so the whole parameter column fits on screen.
|
||||
_TOMO_PARAMS_WINDOW_GEOMETRY = (50, 50, 730, 1170)
|
||||
|
||||
# Name of a registered at_each_angle hook (see register_at_each_angle_hook()),
|
||||
# or None for the default per-projection behaviour.
|
||||
@@ -270,7 +275,7 @@ class TomoQueueMixin:
|
||||
print("The tomo parameter window is already open.")
|
||||
return
|
||||
else:
|
||||
gui.new(name, timeout=timeout)
|
||||
gui.new(name, geometry=self._TOMO_PARAMS_WINDOW_GEOMETRY, timeout=timeout)
|
||||
time.sleep(1)
|
||||
window = getattr(gui, name)
|
||||
window.new("OMNY_TomoParams", object_name="tomo_params", timeout=timeout)
|
||||
@@ -584,6 +589,11 @@ class TomoQueueMixin:
|
||||
if not isinstance(value, (int, float)) or isinstance(value, bool):
|
||||
raise ValueError(f"{action_name}.{name} must be a number, got {value!r}.")
|
||||
lo, hi = field.get("min"), field.get("max")
|
||||
device_name = field.get("device_limits_from")
|
||||
if device_name is not None:
|
||||
device = self.device_manager.devices.get(device_name)
|
||||
if device is not None:
|
||||
lo, hi = device.limits
|
||||
if lo is not None and value < lo:
|
||||
raise ValueError(f"{action_name}.{name}={value} is below the minimum {lo}.")
|
||||
if hi is not None and value > hi:
|
||||
@@ -616,23 +626,6 @@ class TomoQueueMixin:
|
||||
args += [dev[name], target]
|
||||
umv(*args)
|
||||
|
||||
def optimize_idgap(self, search_range: float = 0.5) -> None:
|
||||
"""Scan idgap over +/- search_range around its current position and
|
||||
move to the peak of <signal not yet decided>.
|
||||
|
||||
STUB -- not implemented. The peak-detection signal/device is not yet
|
||||
decided (see flomni's AI_docs/TOMO_QUEUE_COMMAND_JOBS_PLAN.md section
|
||||
10). This is the ONLY place that needs to change to make this a real
|
||||
action: the registry entry, resume policy, and (eventually) GUI
|
||||
wiring already treat it as a normal idempotent compound action, so
|
||||
dropping in the real scan-and-move-to-peak logic here (or swapping
|
||||
which device/signal it reads) is a self-contained change.
|
||||
"""
|
||||
print(
|
||||
f"optimize_idgap(search_range={search_range}): not yet implemented -- "
|
||||
"peak-detection signal not yet decided. No-op."
|
||||
)
|
||||
|
||||
def _describe_tomo_queue_actions(self) -> dict:
|
||||
"""Serializable description of the action registry + move allow-list.
|
||||
|
||||
@@ -941,7 +934,9 @@ class TomoQueueMixin:
|
||||
for name, value in job["params"].items():
|
||||
setattr(self, name, value)
|
||||
if resume_job:
|
||||
self.tomo_scan_resume()
|
||||
# from_queue=True: skip tomo_scan_resume()'s
|
||||
# "unfinished queue job" prompt -- this *is* the queue.
|
||||
self.tomo_scan_resume(from_queue=True)
|
||||
else:
|
||||
# interactive=False: an unattended queued run must
|
||||
# never block on input() -- see LamNI.tomo_scan()'s
|
||||
@@ -1012,6 +1007,35 @@ class TomoQueueMixin:
|
||||
"tomo_queue_reacquire() on a tomo_type-1 job."
|
||||
)
|
||||
|
||||
def _confirm_resume_outside_queue(self) -> bool:
|
||||
"""Guard for a manual ``tomo_scan_resume()`` call.
|
||||
|
||||
If the queue holds an "incomplete"/"running" job (e.g. after a
|
||||
Ctrl-C abort, which leaves it "running"), resuming by hand bypasses
|
||||
the queue: the job stays unfinished there, so a later
|
||||
``tomo_queue_resume()`` would resume it again and the jobs after it
|
||||
would not run. Warn, print the hint, and ask. Returns True if the
|
||||
resume should go ahead (nothing unfinished in the queue, or the
|
||||
operator confirmed).
|
||||
"""
|
||||
unfinished = [
|
||||
j for j in self._tomo_queue_proxy.as_list() if j["status"] in ("incomplete", "running")
|
||||
]
|
||||
if not unfinished:
|
||||
return True
|
||||
print("\nWARNING: the tomo queue has an unfinished job:")
|
||||
for job in unfinished:
|
||||
print(
|
||||
f" {job['status']:>10s} {job['label']:<20s} {self._describe_tomo_job_line(job)}"
|
||||
)
|
||||
print(
|
||||
"Use tomo_queue_resume() to resume it and continue with the rest of the queue.\n"
|
||||
"If you resume manually instead, the queue is not told -- mark the job done "
|
||||
"afterwards, or it will be resumed again:\n"
|
||||
" self._tomo_queue_proxy.update_by_id(<job id>, status='done')"
|
||||
)
|
||||
return self.OMNYTools.yesno("Run tomo_scan_resume() anyway, bypassing the queue?", "n")
|
||||
|
||||
def tomo_queue_resume(self, start_index: int = 0) -> None:
|
||||
"""Continue running the tomo queue from wherever it currently
|
||||
stands -- an alias for ``tomo_queue_execute()``, added purely so
|
||||
@@ -1056,7 +1080,7 @@ class TomoQueueMixin:
|
||||
queue order (same indexing as ``tomo_queue_show()``/
|
||||
``tomo_queue_delete()``).
|
||||
projection_number: Flat 0-indexed projection to resume from,
|
||||
for any tomo_type (1, 2, or 3) -- see
|
||||
for any tomo_type (1, 2, 3, or 4 = angle-file index) -- see
|
||||
``_resolve_type1_projection()`` for how this maps onto a
|
||||
tomo_type-1 job's (subtomo, angle).
|
||||
|
||||
@@ -1096,6 +1120,18 @@ class TomoQueueMixin:
|
||||
subtomo_start, start_angle = self._resolve_type1_projection(projection_number)
|
||||
self.progress["subtomo"] = subtomo_start
|
||||
self.progress["angle"] = start_angle
|
||||
elif self.tomo_type == 4:
|
||||
# angles from file (flomni) -- bound by the file as it reads now,
|
||||
# since the resumed tomo_scan() re-reads it too.
|
||||
if projection_number < 0:
|
||||
raise ValueError("tomo_queue_reacquire: projection_number must be >= 0.")
|
||||
n_angles = len(read_angle_file(self.tomo_angle_file).angles)
|
||||
if projection_number >= n_angles:
|
||||
raise ValueError(
|
||||
f"projection_number must be < {n_angles} (number of angles in "
|
||||
f"{self.tomo_angle_file}) for this job."
|
||||
)
|
||||
self.progress["projection"] = projection_number
|
||||
elif self.tomo_type in (2, 3):
|
||||
if projection_number < 0:
|
||||
raise ValueError("tomo_queue_reacquire: projection_number must be >= 0.")
|
||||
|
||||
@@ -41,8 +41,10 @@ support on top of it:
|
||||
registry `Flomni.__init__` publishes) so its action dropdown and
|
||||
per-action fields always match whatever's actually in `flomni.py` — no
|
||||
hardcoded mirror. Lets you add steps in order (action + its own fields
|
||||
only — a device dropdown and target for `move`, a bounded spinbox for
|
||||
`optimize_idgap`'s `search_range`), remove a step, reorder steps within
|
||||
only — a device dropdown and target for `move`, three bounded spinboxes
|
||||
for `move_foptz_with_shift`'s `foptz_target`/`manual_shift_x`/
|
||||
`manual_shift_y`, the first of which ranges over `foptz`'s live device
|
||||
limits rather than a static number), remove a step, reorder steps within
|
||||
the job (move up/down), and set a label + idempotent flag (defaults to
|
||||
"every step idempotent" ANDed, same as the CLI, until you touch the
|
||||
checkbox yourself). On confirm it produces a job dict byte-for-byte
|
||||
@@ -50,7 +52,8 @@ support on top of it:
|
||||
2. **Command-job-aware queue table.** `TomoQueueDialog`'s table gained a
|
||||
"Details" column; a command job shows `CMD` in the Type column and its
|
||||
step summary + idempotency marker in Details (e.g.
|
||||
`move{'positions': {'mokev': 6.2}} > optimize_idgap{'search_range': 0.5} [idem]`
|
||||
`move{'positions': {'ccm_energy': 6.2}} > move_foptz_with_shift{'foptz_target':
|
||||
15.0, 'manual_shift_x': 1.0, 'manual_shift_y': -2.0} [idem]`
|
||||
— literally the same string `flomni.tomo_queue_show()` prints).
|
||||
3. **Self-describing tomo jobs.** "Add current params to queue" now stamps
|
||||
`"kind": "tomo"` and a fresh `"id"` on the job it writes, matching what
|
||||
@@ -201,19 +204,22 @@ file is exactly how a regression sneaks in.
|
||||
## 4. Test plan — new: command-job builder
|
||||
|
||||
6. Click **"Add command…"**. Confirm the dialog opens with an **action
|
||||
dropdown showing `move` and `optimize_idgap`** (or whatever's currently
|
||||
in `flomni.py`'s `_TOMO_QUEUE_ACTIONS` — if you've added a third action
|
||||
since this was written, it should appear automatically with no GUI
|
||||
change needed; that's the point of reading the live registry).
|
||||
dropdown showing `move` and `move_foptz_with_shift`** (or whatever's
|
||||
currently in `flomni.py`'s `_TOMO_QUEUE_ACTIONS` — if you've added a
|
||||
third action since this was written, it should appear automatically with
|
||||
no GUI change needed; that's the point of reading the live registry).
|
||||
7. With `move` selected, confirm the field area shows a **device dropdown**
|
||||
(should list `mokev`, `idgap`, `ftray` per the current allow-list) and a
|
||||
**numeric target field**. Pick a device, set a target, click **"Add
|
||||
step"**. Confirm a line appears in the step list above, formatted like
|
||||
`move{'positions': {'mokev': 6.2}}`.
|
||||
8. Switch the action dropdown to `optimize_idgap`. Confirm the field area
|
||||
now shows a single **search_range** spinbox pre-filled with `0.5`,
|
||||
range-limited (try typing outside 0–2, confirm the widget clamps it
|
||||
rather than accepting it). Click **"Add step"**.
|
||||
(should list `ccm_energy`, `idgap` per the current allow-list -- `foptz`
|
||||
is deliberately absent, see point 8) and a **numeric target field**. Pick
|
||||
a device, set a target, click **"Add step"**. Confirm a line appears in
|
||||
the step list above, formatted like `move{'positions': {'ccm_energy': 6.2}}`.
|
||||
8. Switch the action dropdown to `move_foptz_with_shift`. Confirm the field
|
||||
area now shows three spinboxes: **foptz target (absolute)**,
|
||||
**New manual_shift_x**, **New manual_shift_y**. Confirm
|
||||
the foptz-target spinbox's range reflects `foptz`'s *live* device limits
|
||||
(try typing a value past the real limit, confirm the widget clamps it
|
||||
rather than accepting it -- this is the live-limit lookup, not a static
|
||||
number). Fill in all three and click **"Add step"**.
|
||||
9. Confirm the **idempotent checkbox auto-checked itself** after both adds
|
||||
(both current actions default to idempotent=True). Uncheck it by hand,
|
||||
then add another step — confirm it **stays unchecked** (your override
|
||||
@@ -250,9 +256,10 @@ file is exactly how a regression sneaks in.
|
||||
## 6. Test plan — mixed queue, end to end
|
||||
|
||||
17. Build a small realistic sequence via the GUI only: a tomo job ("Add
|
||||
current params to queue"), then a command job (e.g. a `move` on
|
||||
`ftray` if you're on the sim — `mokev`/`idgap` only exist on the real
|
||||
front-end, see plan section 3.1), then another tomo job.
|
||||
current params to queue"), then a command job (e.g. a `move_foptz_with_shift`
|
||||
step if you're on the sim — `ccm_energy`/`idgap` only exist on the real
|
||||
front-end, `foptz` is the only queue-movable device in the sim config),
|
||||
then another tomo job.
|
||||
18. From the CLI, run `flomni.tomo_queue_execute()`. Confirm: the tomo job
|
||||
runs, the command job actually moves the device (check
|
||||
`dev.<device>.readback.get()` before/after), and the second tomo job
|
||||
@@ -405,8 +412,12 @@ GUI bug and reporting gaps for the hook feature.
|
||||
|
||||
42. Queue a `done` tomo job (run one, or add one via CLI with `status`
|
||||
hand-edited to `done`). Select it in the queue dialog and confirm
|
||||
**"Load into editor"** is enabled. Select a command job (or multiple
|
||||
rows, or nothing) and confirm it's **disabled** in each case.
|
||||
**"Load into editor"** is enabled. Select multiple rows (or nothing)
|
||||
and confirm it's **disabled**. Select a *pending* command job and
|
||||
confirm the button text instead switches to **"Edit command"**
|
||||
(enabled) — see 6f below for that path; select a `running`/
|
||||
`incomplete`/`done` command job and confirm it's **disabled** (text
|
||||
reverts to "Load into editor").
|
||||
43. Click **"Load into editor"** with nothing else in progress. Confirm the
|
||||
params panel enters edit mode (Edit button hides, Submit/"Add to
|
||||
queue"/Cancel appear) with every field populated from the *job's*
|
||||
@@ -481,6 +492,28 @@ buttons living on two different windows.
|
||||
to queue" — confirm it queues normally with **no** warning (only
|
||||
fires when an edit is actually in progress).
|
||||
|
||||
## 6g. Test plan — new: "Edit command" (same button slot as "Load into editor")
|
||||
|
||||
48. Add a multi-step command job via **"Add command…"** (e.g. a `move` step
|
||||
plus a `move_foptz_with_shift` step), with a custom label and the
|
||||
idempotent checkbox left at its default. Select it in the queue table
|
||||
and confirm the button reads **"Edit command"**.
|
||||
49. Click it. Confirm `CommandJobBuilderDialog` opens titled **"Edit
|
||||
command job"**, with the step list showing both steps exactly as the
|
||||
queue table's Details column renders them, the label field pre-filled
|
||||
with your custom label, and the idempotent checkbox matching what you
|
||||
set when adding it.
|
||||
50. Click **Cancel**. Confirm the queue is completely unchanged (same job,
|
||||
same position, same `id` — check via `flomni.tomo_queue_show()` or
|
||||
`flomni._tomo_queue_proxy.as_list()` before/after).
|
||||
51. Reopen it, remove a step, add a different one, change the label, click
|
||||
**OK**. Confirm the row updates **in place** (same position in the
|
||||
table, same `id` via the CLI check above) with the new steps/label —
|
||||
no new row is appended, no old row left behind.
|
||||
52. Confirm the button is **disabled** for a `running`/`incomplete`/`done`
|
||||
command job (only `pending` is editable), and also disabled while
|
||||
**Sort queue…** mode is active.
|
||||
|
||||
## 7. Known limitations of this first iteration (by design, not bugs)
|
||||
|
||||
Worth knowing before you file something as broken:
|
||||
@@ -501,13 +534,16 @@ Worth knowing before you file something as broken:
|
||||
(`tomo_queue_add_command` with a `positions` dict of more than one
|
||||
device); the GUI builder just doesn't expose it yet. Add extra `move`
|
||||
steps instead if you need to move more than one device.
|
||||
- **No live device-limit validation.** The plan (section 6.5) mentions
|
||||
validating against `dev.<name>.limits` in addition to the schema's
|
||||
min/max; this iteration only enforces the schema bounds (via the spinbox
|
||||
range), not real hardware limits. The execution-time re-check in
|
||||
`_queue_action_move()` is still the actual safety boundary regardless
|
||||
(two-layer validation, plan section 3.3), so this is a UX gap, not a
|
||||
safety gap.
|
||||
- **Resolved, for fields that opt in: live device-limit validation.** A
|
||||
float param schema can now declare `"device_limits_from": "<device>"`
|
||||
(see `move_foptz_with_shift`'s `foptz_target`), which makes both the GUI
|
||||
spinbox's range and the CLI's add-time check (`_validate_action_kwargs`)
|
||||
use `dev.<name>.limits` instead of the schema's static `min`/`max`. The
|
||||
plain `move` action's devices (`ccm_energy`/`idgap`) don't use this --
|
||||
still just the schema bounds via the spinbox range there. The
|
||||
execution-time re-check in `_queue_action_move()`/
|
||||
`_queue_action_move_foptz_with_shift()` remains the actual safety
|
||||
boundary regardless (two-layer validation, plan section 3.3).
|
||||
- **Resolved: `Submit` is now a hard block while the beamline is busy**,
|
||||
matching plan section 6.1 literally instead of the earlier soft-warn
|
||||
confirmation dialog. The gap that made the soft warn tempting in the first
|
||||
|
||||
@@ -264,8 +264,8 @@ interruptible mid-scan for the crash-resume tests (§4, B5).
|
||||
10. **✅ Add-time validation rejects bad steps, and rejects them without landing
|
||||
anything in the queue.** `test_add_command_validation_errors`. Unknown action,
|
||||
a device not on the `move` allow-list, an out-of-range/wrong-type
|
||||
`optimize_idgap.search_range`, and an unknown kwarg are each rejected with the
|
||||
expected `ValueError`; afterward the queue is confirmed still empty.
|
||||
`move_foptz_with_shift.foptz_target`, and an unknown kwarg are each rejected
|
||||
with the expected `ValueError`; afterward the queue is confirmed still empty.
|
||||
11. **✅ Execution-time allow-list re-check is the real safety boundary.**
|
||||
`test_move_execution_time_allowlist_recheck`. A job with a disallowed device
|
||||
appended directly (bypassing `tomo_queue_add_command`) is refused by
|
||||
@@ -278,10 +278,13 @@ interruptible mid-scan for the crash-resume tests (§4, B5).
|
||||
`test_show_and_webpage_survive_command_job`. A command job (no `params` key)
|
||||
renders its step summary + idempotency marker in both, instead of `KeyError`ing.
|
||||
14. **✅ Back-compat + real dispatch, combined.**
|
||||
`test_legacy_job_no_kind_defaults_to_tomo_and_command_moves_ftray`. A `kind`-less
|
||||
`test_legacy_job_no_kind_defaults_to_tomo_and_command_moves_foptz`. A `kind`-less
|
||||
legacy queue entry still runs as a tomogram; in the same mixed-queue run, a
|
||||
command job actually moves a device (`ftray`, the sim-testable stand-in for
|
||||
`mokev`/`idgap` — see the plan doc §3.1) via the real `move` action path.
|
||||
command job actually moves `foptz` and sets new `manual_shift_x`/
|
||||
`manual_shift_y` values via the real `move_foptz_with_shift` action
|
||||
path (the only sim-movable path now that `ftray` is no longer in the move
|
||||
allow-list; `foptz` is also the real device this action exists for, unlike
|
||||
`ftray`'s old sim-only stand-in role).
|
||||
15. **✅ Non-idempotent resume prompts exactly once, and respects "no".**
|
||||
`test_non_idempotent_command_resume_prompts`. A command job flagged
|
||||
`idempotent=False`, left `incomplete`, triggers the resume prompt (distinct from
|
||||
@@ -294,13 +297,14 @@ interruptible mid-scan for the crash-resume tests (§4, B5).
|
||||
16. One queued tomogram end-to-end on the real instrument, to confirm the sim's
|
||||
timing/progress semantics match reality closely enough that resume lands on the
|
||||
right angle.
|
||||
17. `move` on `mokev`/`idgap` end-to-end — neither device is in the flomni sim
|
||||
config (see the plan doc §3.1), so Step 2's `move` action itself has only been
|
||||
sim-exercised via `ftray`, never against the real devices it's actually for.
|
||||
17. `move` on `ccm_energy`/`idgap` end-to-end — neither device is in the flomni
|
||||
sim config, so Step 2's `move` action itself has only been sim-exercised via
|
||||
add-time validation (which doesn't require real movement), never a real
|
||||
execute() against these two devices.
|
||||
|
||||
**Gate:** A–D passed on the sim (16/16 tests total). **Steps 1 and 2 are verified
|
||||
enough to build Step 3 (the GUI) on.** E remains open, and blocks calling `move` on
|
||||
`mokev`/`idgap` production-ready.
|
||||
**Gate:** A–D passed on the sim. **Steps 1 and 2 are verified enough to build
|
||||
Step 3 (the GUI) on.** E remains open, and blocks calling `move` on
|
||||
`ccm_energy`/`idgap` production-ready.
|
||||
|
||||
### A gotcha found testing Step 2 (not a Step-2 bug)
|
||||
|
||||
@@ -348,11 +352,14 @@ still just its design.
|
||||
- **Registry entry = 4 things:** bound method name, `idempotent` default, help string,
|
||||
and a **params schema** (possibly empty).
|
||||
- **Result-dependent logic lives INSIDE a named action, never across queue steps.**
|
||||
E.g. "scan idgap, move to peak" is ONE action (`optimize_idgap`) whose method does
|
||||
the scan, finds the peak, and moves — *not* three queue steps. The queue therefore
|
||||
never has to carry runtime values between steps; `kwargs` stays plain JSON. A
|
||||
compound action may take parameters (e.g. `search_range`), declared in its params
|
||||
schema like any other.
|
||||
E.g. a hypothetical "scan idgap, move to peak" action would be ONE action whose
|
||||
method does the scan, finds the peak, and moves — *not* three queue steps. The
|
||||
queue therefore never has to carry runtime values between steps; `kwargs` stays
|
||||
plain JSON. The same "bundle, don't split" idea also applies to a compound action
|
||||
with no runtime-dependent result but a correctness-critical pairing of writes --
|
||||
`move_foptz_with_shift` (moves `foptz` + sets new `manual_shift_x`/
|
||||
`manual_shift_y` values atomically, deliberately not reachable as two
|
||||
separate steps) is the concrete example currently in the registry.
|
||||
- **Publish the registry via a global var** (`tomo_queue_actions`) on `Flomni.__init__`
|
||||
so the GUI builds its dropdowns/forms from it — no hardcoded mirror to drift (the
|
||||
`QUEUE_PARAM_NAMES` mirror between `flomni.py` and `tomo_params.py` already has this
|
||||
@@ -399,9 +406,10 @@ Guarded against command jobs (`job.get("kind", "tomo") == "tomo"` before touchin
|
||||
## 6. Open questions for Mirko — all resolved
|
||||
|
||||
- **The final action set and device allow-list**, see `TOMO_QUEUE_COMMAND_JOBS_PLAN.md`
|
||||
§10 — `move` + `optimize_idgap` only, `_TOMO_QUEUE_MOVE_DEVICES = {mokev, idgap}`.
|
||||
One detail (`optimize_idgap`'s peak-detection signal) deferred to the
|
||||
implementation session, not blocking.
|
||||
§10 — originally `move` + `optimize_idgap`, `_TOMO_QUEUE_MOVE_DEVICES = {mokev,
|
||||
idgap}`. Superseded since: `optimize_idgap` was removed (never commissioned, stayed
|
||||
a no-op stub), `mokev` was fixed to the real device name `ccm_energy`, and a new
|
||||
`move_foptz_with_shift` action was added (see §4D10/§4D14 above for its tests).
|
||||
- **`_TOMO_QUEUE_PARAM_NAMES` renamed to `_TOMO_SCAN_PARAM_NAMES`** (it reads live
|
||||
properties, not queue state — the old name was misleading). Done in `flomni.py`;
|
||||
the `tomo_params.py` comment referencing it by name was updated too (the
|
||||
|
||||
Binary file not shown.
@@ -19,10 +19,18 @@ from csaxs_bec.bec_ipython_client.plugins.cSAXS import cSAXSBeamlineChecks
|
||||
from csaxs_bec.bec_ipython_client.plugins.flomni.flomni_optics_mixin import FlomniOpticsMixin
|
||||
from csaxs_bec.bec_ipython_client.plugins.flomni.gui_tools import flomniGuiTools
|
||||
from csaxs_bec.bec_ipython_client.plugins.flomni.x_ray_eye_align import XrayEyeAlign
|
||||
from csaxs_bec.bec_ipython_client.plugins.OMNY_shared.angle_file import (
|
||||
AngleFileError,
|
||||
read_angle_file,
|
||||
summarize_angles,
|
||||
)
|
||||
from csaxs_bec.bec_ipython_client.plugins.OMNY_shared.filter_check import (
|
||||
filters_out_of_beam,
|
||||
warn_and_confirm,
|
||||
)
|
||||
from csaxs_bec.bec_ipython_client.plugins.OMNY_shared.tomo_angle_file_mixin import (
|
||||
TomoAngleFileMixin,
|
||||
)
|
||||
from csaxs_bec.bec_ipython_client.plugins.OMNY_shared.tomo_queue_mixin import (
|
||||
TomoQueueMixin,
|
||||
_GlobalVarParam,
|
||||
@@ -1001,6 +1009,10 @@ class FlomniSampleTransferMixin:
|
||||
self.ftransfer_get_sample(new_sample_position)
|
||||
self.ftransfer_put_sample(0)
|
||||
|
||||
# The manual shifts belong to the previous sample; start the new one clean.
|
||||
self.manual_shift_x = 0.0
|
||||
self.manual_shift_y = 0.0
|
||||
|
||||
def ftransfer_modify_storage(self, position: int, used: int):
|
||||
if used:
|
||||
name = input("What's the name of this sample? ")
|
||||
@@ -1777,6 +1789,7 @@ class _MeasuredSampleLog:
|
||||
|
||||
class Flomni(
|
||||
TomoQueueMixin,
|
||||
TomoAngleFileMixin,
|
||||
FlomniInitStagesMixin,
|
||||
FlomniSampleTransferMixin,
|
||||
FlomniAlignmentMixin,
|
||||
@@ -1786,6 +1799,7 @@ class Flomni(
|
||||
):
|
||||
_TOMO_PARAMS_WINDOW_NAME = "flomni_tomo_params"
|
||||
_TOMO_PARAMS_ERROR = FlomniError
|
||||
_ANGLE_FILE_OPPOSITE_WARNING = True # parallel-beam: 180-deg opposites are redundant
|
||||
|
||||
# Backed by a BEC global var (Redis) so the pre-open slit position
|
||||
# survives a kernel restart while the eye is still in.
|
||||
@@ -1896,6 +1910,7 @@ class Flomni(
|
||||
self.tomo_shellstep = 1
|
||||
self.fermat_asymmetry = 1.0
|
||||
self.tomo_countingtime = 0.1
|
||||
self.manual_shift_x = 0.0
|
||||
self.manual_shift_y = 0.0
|
||||
self.single_point_random_shift_max = 0.0
|
||||
self.fovx = 20
|
||||
@@ -1911,7 +1926,9 @@ class Flomni(
|
||||
self.tomo_angle_range = 180
|
||||
self.golden_projections_at_0_deg_for_damage_estimation = 0
|
||||
self.zero_deg_reference_at_each_subtomo = False
|
||||
self.tomo_type1_always_forward = False
|
||||
self.golden_ratio_bunch_size = 20
|
||||
self.tomo_angle_file = ""
|
||||
self.frames_per_trigger = 1
|
||||
self.single_point_instead_of_fermat_scan = False
|
||||
|
||||
@@ -2044,6 +2061,8 @@ class Flomni(
|
||||
|
||||
tomo_countingtime = _GlobalVarParam(0.1)
|
||||
|
||||
manual_shift_x = _GlobalVarParam(0.0)
|
||||
|
||||
manual_shift_y = _GlobalVarParam(0.0)
|
||||
|
||||
# at_each_angle_hook is inherited from TomoQueueMixin: name of a
|
||||
@@ -2121,6 +2140,9 @@ class Flomni(
|
||||
elif val == 3:
|
||||
# equally spaced tomography with starting angles shifted by golden ratio
|
||||
self.client.set_global_var("tomo_type", val)
|
||||
elif val == 4:
|
||||
# angles (and optional sub-tomogram numbers) read from tomo_angle_file
|
||||
self.client.set_global_var("tomo_type", val)
|
||||
else:
|
||||
raise ValueError("Unknown tomo_type.")
|
||||
|
||||
@@ -2166,11 +2188,15 @@ class Flomni(
|
||||
|
||||
golden_projections_at_0_deg_for_damage_estimation = _GlobalVarParam(0)
|
||||
|
||||
def _default_max_scan_angle(self) -> float:
|
||||
"""Upper limit for tomo types 1-3 (type 4 uses the mixin's fixed range)."""
|
||||
return self.tomo_angle_range
|
||||
|
||||
@property
|
||||
def zero_deg_reference_at_each_subtomo(self):
|
||||
"""If True (tomo_type == 1 only), an additional projection at exactly
|
||||
0 degrees is acquired at the start of every odd (forward)
|
||||
sub-tomogram - i.e. every time the rotation passes back through
|
||||
sub-tomogram (every sub-tomogram if tomo_type1_always_forward) - i.e. every time the rotation passes back through
|
||||
0 degrees - and once more after the final (8th) sub-tomogram
|
||||
completes. Together with sub-tomogram 1's own first projection
|
||||
(which already lands exactly on 0 degrees), this gives a 0-degree
|
||||
@@ -2187,6 +2213,24 @@ class Flomni(
|
||||
def zero_deg_reference_at_each_subtomo(self, val: bool):
|
||||
self.client.set_global_var("zero_deg_reference_at_each_subtomo", val)
|
||||
|
||||
@property
|
||||
def tomo_type1_always_forward(self):
|
||||
"""If True (tomo_type == 1, tomo_angle_range == 180 only), every
|
||||
sub-tomogram sweeps forward (0 -> 180) instead of alternating
|
||||
forward/reverse. Each sub-tomogram covers the same angle set either
|
||||
way, so the combined grid is unchanged; the rotation just returns to
|
||||
~0 between sub-tomograms. Because every sub-tomogram then starts at
|
||||
0 degrees, zero_deg_reference_at_each_subtomo takes its reference
|
||||
shot before every sub-tomogram instead of every second one."""
|
||||
val = self.client.get_global_var("tomo_type1_always_forward")
|
||||
if val is None:
|
||||
return False
|
||||
return val
|
||||
|
||||
@tomo_type1_always_forward.setter
|
||||
def tomo_type1_always_forward(self, val: bool):
|
||||
self.client.set_global_var("tomo_type1_always_forward", bool(val))
|
||||
|
||||
golden_ratio_bunch_size = _GlobalVarParam(20)
|
||||
|
||||
@property
|
||||
@@ -2348,7 +2392,9 @@ class Flomni(
|
||||
f.write(" ".join(map(str, zeros)) + "\n")
|
||||
|
||||
@staticmethod
|
||||
def _subtomo_angle_plan(subtomo_number, tomo_angle_range, tomo_angle_stepsize, start_angle=None):
|
||||
def _subtomo_angle_plan(
|
||||
subtomo_number, tomo_angle_range, tomo_angle_stepsize, start_angle=None, always_forward=False
|
||||
):
|
||||
"""
|
||||
Pure angle-generation logic for one sub-tomogram of an equally
|
||||
spaced 8-sub-tomogram tomography scan (tomo_type == 1). No device
|
||||
@@ -2393,6 +2439,10 @@ class Flomni(
|
||||
scan would produce, at the same total projection count, with zero
|
||||
redundant measurements anywhere.
|
||||
|
||||
always_forward (bool): 180 mode only - sweep every sub-tomogram
|
||||
forward instead of alternating direction (same angle set per
|
||||
sub-tomogram, only the order changes). Ignored in 360 mode.
|
||||
|
||||
Returns:
|
||||
angles (np.ndarray): the N angles (degrees) for this sub-tomogram.
|
||||
subtomo_offset (int): index of this sub-tomogram's first angle
|
||||
@@ -2422,7 +2472,7 @@ class Flomni(
|
||||
|
||||
if tomo_angle_range == 180:
|
||||
base = 0.0
|
||||
forward = bool(subtomo_number % 2)
|
||||
forward = True if always_forward else bool(subtomo_number % 2)
|
||||
else:
|
||||
mod4 = subtomo_number % 4
|
||||
base = 0.0 if mod4 in (1, 0) else 180.0
|
||||
@@ -2503,7 +2553,10 @@ class Flomni(
|
||||
)
|
||||
subtomo_number = projection_number // N + 1
|
||||
angles, _, _, _ = self._subtomo_angle_plan(
|
||||
subtomo_number, self.tomo_angle_range, self.tomo_angle_stepsize
|
||||
subtomo_number,
|
||||
self.tomo_angle_range,
|
||||
self.tomo_angle_stepsize,
|
||||
always_forward=self.tomo_type1_always_forward,
|
||||
)
|
||||
return subtomo_number, float(angles[projection_number % N])
|
||||
|
||||
@@ -2519,7 +2572,11 @@ class Flomni(
|
||||
print(f"Sub tomo scan with start angle {start_angle} requested.")
|
||||
|
||||
angles, subtomo_offset, N, step = self._subtomo_angle_plan(
|
||||
subtomo_number, self.tomo_angle_range, self.tomo_angle_stepsize, start_angle=start_angle
|
||||
subtomo_number,
|
||||
self.tomo_angle_range,
|
||||
self.tomo_angle_stepsize,
|
||||
start_angle=start_angle,
|
||||
always_forward=self.tomo_type1_always_forward,
|
||||
)
|
||||
|
||||
for i, angle in enumerate(angles):
|
||||
@@ -2552,7 +2609,7 @@ class Flomni(
|
||||
shot: forcing that shot before a sub-tomogram that doesn't actually
|
||||
start near 0 would mean a large, wasted detour."""
|
||||
if self.tomo_angle_range == 180:
|
||||
return bool(subtomo_number % 2)
|
||||
return self.tomo_type1_always_forward or bool(subtomo_number % 2)
|
||||
return subtomo_number % 4 == 1
|
||||
|
||||
@staticmethod
|
||||
@@ -2569,9 +2626,10 @@ class Flomni(
|
||||
return not isinstance(exc, FlomniError)
|
||||
|
||||
@scan_repeat(max_repeats=10, default=True, exc_handler=_retry_unless_flomni_error)
|
||||
def _tomo_scan_at_angle(self, angle, subtomo_number):
|
||||
def _tomo_scan_at_angle(self, angle, subtomo_number, *, step=None, exp_time=None,
|
||||
frames_per_trigger=None):
|
||||
|
||||
if 0 <= angle < self.tomo_angle_range + 0.05:
|
||||
if self._min_scan_angle() <= angle < self._max_scan_angle() + 0.05:
|
||||
now = datetime.datetime.now()
|
||||
prev_heartbeat_str = self.progress.get("heartbeat")
|
||||
if prev_heartbeat_str is not None:
|
||||
@@ -2615,8 +2673,9 @@ class Flomni(
|
||||
num_repeats = self.special_angle_repeats
|
||||
start_scan_number = bec.queue.next_scan_number
|
||||
projection_start = time.perf_counter()
|
||||
overrides = self._angle_overrides_kwargs(step, exp_time, frames_per_trigger)
|
||||
for i in range(num_repeats):
|
||||
self._at_each_angle(angle)
|
||||
self._at_each_angle(angle, **overrides)
|
||||
projection_duration = time.perf_counter() - projection_start
|
||||
if is_special_angle:
|
||||
self._current_special_angles.pop(0)
|
||||
@@ -2707,9 +2766,41 @@ class Flomni(
|
||||
LamNI.tomo_scan() (tomo_queue_execute() calls both the same
|
||||
way). FlOMNI has no fine-alignment gate, but it is used by
|
||||
the filter-out-of-beam check below.
|
||||
projection_number: For tomo_types 2, 3 and 4, resume from this
|
||||
(0-indexed) projection. For type 4 this is an index into the
|
||||
angle file as it reads *now* -- the file is re-read on every
|
||||
call, including resume, and again during the scan itself
|
||||
whenever it catches up to the current end of file.
|
||||
"""
|
||||
dev = builtins.__dict__.get("dev")
|
||||
|
||||
is_new_scan = (
|
||||
(self.tomo_type == 1 and subtomo_start == 1 and start_angle is None)
|
||||
or (self.tomo_type == 2 and projection_number == None)
|
||||
or (self.tomo_type == 3 and projection_number == None)
|
||||
or (self.tomo_type == 4 and projection_number is None)
|
||||
)
|
||||
if is_new_scan:
|
||||
# Clear any stale progress from a previous scan immediately, before
|
||||
# the angle-file check and the eye/optics-in / filter-out-of-beam
|
||||
# gates below -- otherwise, if any of those decline or raise (e.g.
|
||||
# the setup isn't in measurement condition, which disabling rt
|
||||
# feedback can easily cause), tomo_scan() returns/raises having
|
||||
# never touched `progress`, and the GUI/webpage keep showing
|
||||
# projection/angle/start-time from whatever scan last completed --
|
||||
# arbitrarily old, and easy to mistake for a scan still running.
|
||||
self.progress.reset()
|
||||
|
||||
# tomo_type 4: (re-)read the angle file before anything else, so a
|
||||
# missing/malformed file fails immediately -- before sample DB
|
||||
# registration, empty frames, or any motion (and, in the tomo queue,
|
||||
# marks the job incomplete with this message). The file need not be
|
||||
# complete (end with 'END') yet -- that's the normal starting state
|
||||
# for a file meant to be appended to while the scan runs.
|
||||
file_angles = None
|
||||
if self.tomo_type == 4:
|
||||
file_angles = self._start_angle_file_scan(projection_number)
|
||||
|
||||
if not self._check_eye_out_and_optics_in():
|
||||
print(
|
||||
"Attention: The setup is not in measurement condition.\nXray eye might be IN or the Xray optics OUT."
|
||||
@@ -2736,13 +2827,7 @@ class Flomni(
|
||||
bec.builtin_actors.scan_interlock.enabled = True
|
||||
|
||||
self._current_special_angles = self.special_angles.copy()
|
||||
# a new tomo scan was started
|
||||
if (
|
||||
(self.tomo_type == 1 and subtomo_start == 1 and start_angle is None)
|
||||
or (self.tomo_type == 2 and projection_number == None)
|
||||
or (self.tomo_type == 3 and projection_number == None)
|
||||
):
|
||||
|
||||
if is_new_scan:
|
||||
# pylint: disable=undefined-variable
|
||||
# Always attempt registration (even for an empty/test account)
|
||||
# and let add_sample_database() -> TomoIDManager.register()
|
||||
@@ -2764,14 +2849,13 @@ class Flomni(
|
||||
status="started",
|
||||
)
|
||||
self.write_pdf_report()
|
||||
# estimated_remaining_time/estimated_finish_time/accumulated_idle_time/
|
||||
# heartbeat/subtomo/projection/angle/... were already cleared by the
|
||||
# self.progress.reset() above, before the gates -- only the actual
|
||||
# start time is set here, once the scan is genuinely committed to.
|
||||
self.progress["tomo_start_time"] = datetime.datetime.now().isoformat()
|
||||
# reset stale estimates from any previous scan, otherwise the GUI
|
||||
# would keep showing a leftover ETA from before this scan has
|
||||
# accumulated enough projections to compute a fresh one
|
||||
self.progress["estimated_remaining_time"] = None
|
||||
self.progress["estimated_finish_time"] = None
|
||||
self.progress["accumulated_idle_time"] = 0.0
|
||||
self.progress["heartbeat"] = None
|
||||
if file_angles is not None:
|
||||
self._init_angle_file_progress(file_angles, projection_number)
|
||||
self.collect_empty_frames()
|
||||
# Unconditional single 0-degree reference projection at the very
|
||||
# start of a new tomogram, independent of
|
||||
@@ -2933,6 +3017,8 @@ class Flomni(
|
||||
f"Golden ratio tomography stopped automatically after the requested {self.golden_max_number_of_projections} projections"
|
||||
)
|
||||
break
|
||||
elif self.tomo_type == 4:
|
||||
self._run_angle_file_loop(file_angles, projection_number)
|
||||
else:
|
||||
raise FlomniError("undefined tomo type")
|
||||
finally:
|
||||
@@ -2966,7 +3052,7 @@ class Flomni(
|
||||
)
|
||||
self._scilog_write(timing_content, "tomoscan")
|
||||
|
||||
def tomo_scan_resume(self) -> None:
|
||||
def tomo_scan_resume(self, from_queue: bool = False) -> None:
|
||||
"""Resume a tomo_scan() that crashed or was interrupted, picking up
|
||||
automatically from wherever ``progress`` last reported -- no need to
|
||||
read the last subtomo/angle/projection off the progress printout by
|
||||
@@ -2982,11 +3068,25 @@ class Flomni(
|
||||
queue. tomo_queue_execute() also calls this internally when resuming
|
||||
a job that previously failed partway through, so a failed queue job
|
||||
picks up mid-scan rather than restarting from subtomo 1 / angle 0.
|
||||
|
||||
For tomo_type 4 specifically, this is for recovering from a crash or
|
||||
an explicit abort (e.g. Ctrl-C) -- it is not needed just to pick up
|
||||
angle lines appended to the file while a scan is running, since
|
||||
tomo_scan() already waits for and picks those up on its own.
|
||||
|
||||
If the tomo queue holds an unfinished ("incomplete"/"running") job,
|
||||
a manual call warns and asks for confirmation, since resuming by
|
||||
hand bypasses the queue (use tomo_queue_resume() instead).
|
||||
``from_queue`` is set by tomo_queue_execute() to skip that prompt.
|
||||
"""
|
||||
if self.progress.get("tomo_start_time") is None:
|
||||
print("No tomo scan in progress to resume -- nothing to do.")
|
||||
return
|
||||
|
||||
if not from_queue and not self._confirm_resume_outside_queue():
|
||||
print("Aborted.")
|
||||
return
|
||||
|
||||
if self.tomo_type == 1:
|
||||
subtomo_start = self.progress["subtomo"]
|
||||
start_angle = self.progress["angle"]
|
||||
@@ -2995,9 +3095,14 @@ class Flomni(
|
||||
return
|
||||
print(f"Resuming tomo scan at subtomo {subtomo_start}, angle {start_angle:.3f} deg.")
|
||||
self.tomo_scan(subtomo_start=subtomo_start, start_angle=start_angle)
|
||||
elif self.tomo_type in (2, 3):
|
||||
elif self.tomo_type in (2, 3, 4):
|
||||
projection_number = self.progress["projection"]
|
||||
print(f"Resuming tomo scan at projection {projection_number}.")
|
||||
if self.tomo_type == 4:
|
||||
print(
|
||||
f"Note: the angle file {self.tomo_angle_file} is re-read now -- its "
|
||||
f"current content is used from index {projection_number} on."
|
||||
)
|
||||
self.tomo_scan(projection_number=projection_number)
|
||||
else:
|
||||
raise FlomniError("undefined tomo type")
|
||||
@@ -3154,23 +3259,33 @@ class Flomni(
|
||||
# add_sample_database() moved to TomoQueueMixin (OMNY_shared) -- was
|
||||
# byte-identical to LamNI's own copy.
|
||||
|
||||
def _at_each_angle(self, angle: float) -> None:
|
||||
def _at_each_angle(
|
||||
self, angle: float, *, step: float | None = None, exp_time: float | None = None,
|
||||
frames_per_trigger: int | None = None,
|
||||
) -> None:
|
||||
hook_name = self.at_each_angle_hook
|
||||
if hook_name:
|
||||
# Fixed 2-arg contract for a user-pluggable hook -- overrides are
|
||||
# not applicable here (the hook owns the whole acquisition);
|
||||
# tomo_scan() warns up front when a hook is active and the angle
|
||||
# file carries overrides.
|
||||
hook = self._resolve_at_each_angle_hook(hook_name, FlomniError)
|
||||
hook(self, angle)
|
||||
return
|
||||
|
||||
if "flomni_at_each_angle" in builtins.__dict__:
|
||||
# pylint: disable=undefined-variable
|
||||
flomni_at_each_angle(self, angle)
|
||||
flomni_at_each_angle(self, angle) # same fixed 2-arg contract
|
||||
return
|
||||
|
||||
if self.single_point_instead_of_fermat_scan:
|
||||
self.tomo_acquire_at_angle(angle)
|
||||
self.tomo_acquire_at_angle(angle, frames_per_trigger=frames_per_trigger, exp_time=exp_time)
|
||||
return
|
||||
|
||||
self.tomo_scan_projection(angle, _internal=True)
|
||||
self.tomo_scan_projection(
|
||||
angle, _internal=True, step=step, exp_time=exp_time,
|
||||
frames_per_trigger=frames_per_trigger,
|
||||
)
|
||||
|
||||
def _golden(self, ii, howmany_sorted, maxangle, reverse=False):
|
||||
"""returns the iis golden ratio angle of sorted bunches of howmany_sorted and its subtomo number"""
|
||||
@@ -3475,7 +3590,10 @@ class Flomni(
|
||||
f"{scan_number} {angle} {dev.fsamroy.read()['fsamroy']['value']:.5f} {self.tomo_id} {subtomo_number} {0} {sample_name}\n"
|
||||
)
|
||||
|
||||
def tomo_scan_projection(self, angle: float, _internal: bool = False):
|
||||
def tomo_scan_projection(
|
||||
self, angle: float, _internal: bool = False, step: float | None = None,
|
||||
exp_time: float | None = None, frames_per_trigger: int | None = None,
|
||||
):
|
||||
"""Acquire one fermat-scan projection at `angle`.
|
||||
|
||||
Note: this ALWAYS runs a fermat scan, regardless of
|
||||
@@ -3488,6 +3606,12 @@ class Flomni(
|
||||
want the fermat path regardless (the fermat branch of
|
||||
_at_each_angle, and tomo_alignment_scan, which needs real ptycho
|
||||
data) pass _internal=True to skip the prompt.
|
||||
|
||||
step/exp_time/frames_per_trigger: optional per-call overrides for
|
||||
the Fermat step size / exposure time / burst frame count, each
|
||||
defaulting to self.tomo_shellstep/self.tomo_countingtime/
|
||||
self.frames_per_trigger when None (the tomo_type-4 angle-file path
|
||||
is the only caller that passes these).
|
||||
"""
|
||||
dev = builtins.__dict__.get("dev")
|
||||
if not _internal:
|
||||
@@ -3543,14 +3667,14 @@ class Flomni(
|
||||
# correction_xeye_mu = self.align.lamni_compute_additional_correction_xeye_mu(angle)
|
||||
|
||||
offsets = self.get_alignment_offset(angle)
|
||||
sum_offset_x = offsets[0]
|
||||
sum_offset_x = offsets[0] + self.manual_shift_x
|
||||
sum_offset_y = (
|
||||
offsets[1]
|
||||
- self.compute_additional_correction_y(angle)
|
||||
- self.compute_additional_correction_y_2(angle)
|
||||
+ self.manual_shift_y
|
||||
)
|
||||
print(f"Constant shift: y={self.manual_shift_y}")
|
||||
print(f"Constant shift: x={self.manual_shift_x}, y={self.manual_shift_y}")
|
||||
sum_offset_z = offsets[2]
|
||||
# TODO this fix is while the tracker z is broken
|
||||
probe_propagation = -sum_offset_z * 1e-6
|
||||
@@ -3558,6 +3682,10 @@ class Flomni(
|
||||
|
||||
self._current_scan_list = []
|
||||
|
||||
n_step = step if step is not None else self.tomo_shellstep
|
||||
n_exp_time = exp_time if exp_time is not None else self.tomo_countingtime
|
||||
n_frames = frames_per_trigger if frames_per_trigger is not None else self.frames_per_trigger
|
||||
|
||||
start_scan_number = bec.queue.next_scan_number
|
||||
projection_start = time.perf_counter()
|
||||
for stitch_x in range(-self.stitch_x, self.stitch_x + 1):
|
||||
@@ -3569,10 +3697,11 @@ class Flomni(
|
||||
ceny = sum_offset_y + stitch_y * (self.fovy - self.tomo_stitch_overlap)
|
||||
logger.info(
|
||||
f"scans.flomni_fermat_scan(fovx={self.fovx}, fovy={self.fovy},"
|
||||
f" step={self.tomo_shellstep}, fermat_asymmetry={self.fermat_asymmetry},"
|
||||
f" step={n_step}, fermat_asymmetry={self.fermat_asymmetry},"
|
||||
f" cenx={cenx}, ceny={ceny},"
|
||||
f" zshift={sum_offset_z}, angle={angle},"
|
||||
f" exp_time={self.tomo_countingtime}, corridor_size={corridor_size})"
|
||||
f" exp_time={n_exp_time}, frames_per_trigger={n_frames},"
|
||||
f" corridor_size={corridor_size})"
|
||||
)
|
||||
log_message = (
|
||||
f"{str(datetime.datetime.now())}: flomni scan projection at angle {angle}, scan"
|
||||
@@ -3582,14 +3711,14 @@ class Flomni(
|
||||
scan_kwargs = dict(
|
||||
fovx=self.fovx,
|
||||
fovy=self.fovy,
|
||||
step=self.tomo_shellstep,
|
||||
step=n_step,
|
||||
fermat_asymmetry=self.fermat_asymmetry,
|
||||
cenx=cenx,
|
||||
ceny=ceny,
|
||||
zshift=sum_offset_z,
|
||||
angle=angle,
|
||||
exp_time=self.tomo_countingtime,
|
||||
frames_per_trigger=self.frames_per_trigger,
|
||||
exp_time=n_exp_time,
|
||||
frames_per_trigger=n_frames,
|
||||
)
|
||||
|
||||
if self.corridor_size > 0:
|
||||
@@ -3614,7 +3743,10 @@ class Flomni(
|
||||
|
||||
self.tomo_reconstruct(probe_propagation=probe_propagation)
|
||||
|
||||
def tomo_acquire_at_angle(self, angle: float, frames_per_trigger: int | None = None):
|
||||
def tomo_acquire_at_angle(
|
||||
self, angle: float, frames_per_trigger: int | None = None,
|
||||
exp_time: float | None = None,
|
||||
):
|
||||
"""
|
||||
Move fsamroy to `angle`, then move rtx/rty/rtz to the alignment-corrected
|
||||
scan center (same alignment-offset logic as tomo_scan_projection, but
|
||||
@@ -3631,6 +3763,10 @@ class Flomni(
|
||||
angle (float): rotation angle [deg] to move fsamroy to.
|
||||
frames_per_trigger (int, optional): number of burst frames for
|
||||
this acquisition. Defaults to self.frames_per_trigger.
|
||||
exp_time (float, optional): exposure/counting time per frame for
|
||||
this acquisition. Defaults to self.tomo_countingtime. (No
|
||||
`step` parameter: single-point acquisition runs no Fermat
|
||||
scan, so a step-size override has nothing to apply to.)
|
||||
"""
|
||||
scans = builtins.__dict__.get("scans")
|
||||
|
||||
@@ -3655,7 +3791,7 @@ class Flomni(
|
||||
|
||||
# --- alignment offset (same as tomo_scan_projection, no stitching) ---
|
||||
offsets = self.get_alignment_offset(angle)
|
||||
sum_offset_x = offsets[0] + random_shift_x
|
||||
sum_offset_x = offsets[0] + self.manual_shift_x + random_shift_x
|
||||
sum_offset_y = (
|
||||
offsets[1]
|
||||
- self.compute_additional_correction_y(angle)
|
||||
@@ -3663,7 +3799,7 @@ class Flomni(
|
||||
+ self.manual_shift_y
|
||||
+ random_shift_y
|
||||
)
|
||||
print(f"Constant shift: y={self.manual_shift_y}")
|
||||
print(f"Constant shift: x={self.manual_shift_x}, y={self.manual_shift_y}")
|
||||
sum_offset_z = offsets[2]
|
||||
|
||||
# TODO this fix is while the tracker z is broken
|
||||
@@ -3688,8 +3824,9 @@ class Flomni(
|
||||
|
||||
# --- acquire ---
|
||||
n_frames = frames_per_trigger if frames_per_trigger is not None else self.frames_per_trigger
|
||||
n_exp_time = exp_time if exp_time is not None else self.tomo_countingtime
|
||||
self._current_scan_list = [bec.queue.next_scan_number]
|
||||
scans.acquire(exp_time=self.tomo_countingtime, frames_per_trigger=n_frames)
|
||||
scans.acquire(exp_time=n_exp_time, frames_per_trigger=n_frames)
|
||||
self.tomo_reconstruct(
|
||||
probe_propagation=probe_propagation,
|
||||
random_offset_x=random_shift_x,
|
||||
@@ -3790,6 +3927,7 @@ class Flomni(
|
||||
else:
|
||||
print(f"Estimated Fermat scan points per projection: {expected_positions}")
|
||||
print(f"Reconstruction queue name = {self.ptycho_reconstruct_foldername}")
|
||||
print(f" _manual_shift_x <um> = {self.manual_shift_x}")
|
||||
print(f" _manual_shift_y <um> = {self.manual_shift_y}")
|
||||
print(f"Frames per trigger (burst) = {self.frames_per_trigger}")
|
||||
print(f"Single point instead of fermat = {self.single_point_instead_of_fermat_scan}")
|
||||
@@ -3827,8 +3965,15 @@ class Flomni(
|
||||
)
|
||||
if self.tomo_angle_range == 360:
|
||||
print("There are no duplicate angles.")
|
||||
if self.tomo_angle_range == 180:
|
||||
print(f"All sub-tomos forward (0->180) = {self.tomo_type1_always_forward}")
|
||||
zero_deg_where = (
|
||||
"every sub-tomo start + end"
|
||||
if self.tomo_angle_range == 180 and self.tomo_type1_always_forward
|
||||
else "odd sub-tomo start + end"
|
||||
)
|
||||
print(
|
||||
"0-deg reference shots (odd sub-tomo start + end) ="
|
||||
f"0-deg reference shots ({zero_deg_where}) ="
|
||||
f" {self.zero_deg_reference_at_each_subtomo}"
|
||||
)
|
||||
if self.tomo_type == 2:
|
||||
@@ -3856,6 +4001,9 @@ class Flomni(
|
||||
print(
|
||||
"repeating prjections at 0 degrees at the beginning of every second subtomogram"
|
||||
)
|
||||
if self.tomo_type == 4:
|
||||
print("\x1b[1mTomo type 4:\x1b[0m Angles read from file")
|
||||
self._print_angle_file_summary()
|
||||
print(f"\nSample name: {self.sample_name}\n")
|
||||
|
||||
if self.OMNYTools.yesno("Are these parameters correctly set for your scan?", "y"):
|
||||
@@ -3891,6 +4039,9 @@ class Flomni(
|
||||
self.ptycho_reconstruct_foldername = self._get_val(
|
||||
"Reconstruction queue ", self.ptycho_reconstruct_foldername, str
|
||||
)
|
||||
self.manual_shift_x = self._get_val(
|
||||
"<manual_shift_x> um", self.manual_shift_x, float, min_=-10.0, max_=10.0
|
||||
)
|
||||
self.manual_shift_y = self._get_val(
|
||||
"<manual_shift_y> um", self.manual_shift_y, float, min_=-50.0, max_=50.0
|
||||
)
|
||||
@@ -3926,7 +4077,8 @@ class Flomni(
|
||||
print(" 1: 8 equally spaced sub-tomograms")
|
||||
print(" 2: Golden ratio tomography")
|
||||
print(" 3: Equally spaced tomography, golden ratio starting angle")
|
||||
self.tomo_type = self._get_val("Tomography type", self.tomo_type, int)
|
||||
print(" 4: Angles read from file")
|
||||
self.tomo_type = self._get_val("Tomography type", self.tomo_type, int, min_=1, max_=4)
|
||||
|
||||
if self.tomo_type == 1:
|
||||
self.tomo_angle_range = self._get_val(
|
||||
@@ -3976,9 +4128,18 @@ class Flomni(
|
||||
)
|
||||
if self.tomo_angle_range == 360:
|
||||
print("There are no duplicate angles.")
|
||||
if self.tomo_angle_range == 180:
|
||||
self.tomo_type1_always_forward = bool(
|
||||
self._get_val(
|
||||
"Run all sub-tomos forward (0->180) instead of alternating 1/0?",
|
||||
int(self.tomo_type1_always_forward),
|
||||
int,
|
||||
)
|
||||
)
|
||||
self.zero_deg_reference_at_each_subtomo = bool(
|
||||
self._get_val(
|
||||
"Take 0-deg reference shots (start of each odd sub-tomo + end) for"
|
||||
"Take 0-deg reference shots (start of each odd sub-tomo, or every"
|
||||
" sub-tomo if run forward, + end) for"
|
||||
" damage estimation 1/0?",
|
||||
int(self.zero_deg_reference_at_each_subtomo),
|
||||
int,
|
||||
@@ -4028,6 +4189,15 @@ class Flomni(
|
||||
int,
|
||||
)
|
||||
|
||||
if self.tomo_type == 4:
|
||||
print(
|
||||
"Angle file: one angle (-2 to 362 deg) per line, optional 2nd column ="
|
||||
" sub-tomogram number, '#' comments. Read at scan start, and again"
|
||||
" while the scan runs -- without a final 'END' line the scan waits"
|
||||
" for more angles to be appended rather than stopping."
|
||||
)
|
||||
self._prompt_angle_file()
|
||||
|
||||
@staticmethod
|
||||
def _get_val(msg: str, default_value, data_type, min_=None, max_=None):
|
||||
"""Prompt for a value, casting to data_type. When min_/max_ are
|
||||
@@ -4046,8 +4216,9 @@ class Flomni(
|
||||
return val
|
||||
|
||||
# Ordered set of all global-var-backed tomo scan parameters: exactly the
|
||||
# settings shown by tomo_parameters() (including manual_shift_y, which
|
||||
# is prompted there too), plus tomo_stitch_overlap/corridor_size/
|
||||
# settings shown by tomo_parameters() (including manual_shift_x/
|
||||
# manual_shift_y, which are prompted there too), plus tomo_stitch_overlap/
|
||||
# corridor_size/
|
||||
# at_each_angle_hook, which also affect the scan but are only ever set
|
||||
# directly as properties -- tomo_parameters() never prompts for them.
|
||||
# This is the full "parameter set" that tomo_queue_add()/
|
||||
@@ -4062,6 +4233,7 @@ class Flomni(
|
||||
"stitch_y",
|
||||
"tomo_stitch_overlap",
|
||||
"ptycho_reconstruct_foldername",
|
||||
"manual_shift_x",
|
||||
"manual_shift_y",
|
||||
"frames_per_trigger",
|
||||
"single_point_instead_of_fermat_scan",
|
||||
@@ -4073,6 +4245,8 @@ class Flomni(
|
||||
"golden_max_number_of_projections",
|
||||
"golden_projections_at_0_deg_for_damage_estimation",
|
||||
"zero_deg_reference_at_each_subtomo",
|
||||
"tomo_type1_always_forward",
|
||||
"tomo_angle_file",
|
||||
"corridor_size",
|
||||
"at_each_angle_hook",
|
||||
)
|
||||
@@ -4080,14 +4254,14 @@ class Flomni(
|
||||
# Curated allow-list for the "move" queue action -- NOT "any device in
|
||||
# dev". Only devices actually reconfigured between tomograms belong here;
|
||||
# this is the difference between a reconfiguration tool and a remote
|
||||
# control for the whole endstation. mokev/idgap are real front-end
|
||||
# devices (bl_frontend.yaml, not the flomni sim config); ftray (sample
|
||||
# transfer tray, harmless to move, untouched by tomo-scan code) is
|
||||
# included so the move action has a sim-testable target.
|
||||
# control for the whole endstation. ccm_energy/idgap are real front-end
|
||||
# devices (bl_frontend.yaml/bl_optics_hutch.yaml, not the flomni sim
|
||||
# config). foptz is deliberately NOT here -- it has its own dedicated
|
||||
# "move_foptz_with_shift" action below so the new manual_shift_x/y
|
||||
# correction can never be forgotten (see that action's docstring).
|
||||
_TOMO_QUEUE_MOVE_DEVICES = {
|
||||
"mokev": {"label": "Energy", "unit": "keV"},
|
||||
"ccm_energy": {"label": "Energy", "unit": "keV"},
|
||||
"idgap": {"label": "Undulator gap", "unit": "mm"},
|
||||
"ftray": {"label": "Sample transfer tray (sim-testable)", "unit": "mm"},
|
||||
}
|
||||
|
||||
# Named-action registry for tomo-queue command jobs. Each entry maps a
|
||||
@@ -4109,23 +4283,85 @@ class Flomni(
|
||||
"help": "Move device(s) to absolute position(s).",
|
||||
"params": {"positions": {"type": "device_positions"}},
|
||||
},
|
||||
"optimize_idgap": {
|
||||
"func": "optimize_idgap",
|
||||
"idempotent": True,
|
||||
"help": "Scan idgap over a search range and move to the peak. STUB: no-op.",
|
||||
"move_foptz_with_shift": {
|
||||
"func": "_queue_action_move_foptz_with_shift",
|
||||
"idempotent": True, # absolute move + absolute param set, re-runs harmlessly
|
||||
"help": (
|
||||
"Move foptz (FZP optics-Z) to an absolute target and set new "
|
||||
"manual_shift_x/manual_shift_y values (replacing the current "
|
||||
"ones) in the same step -- changing foptz shifts the aligned "
|
||||
"scan center, so this is the only way to queue a foptz move."
|
||||
),
|
||||
"params": {
|
||||
"search_range": {
|
||||
"foptz_target": {
|
||||
"type": "float",
|
||||
"default": 0.5,
|
||||
"default": 17.0,
|
||||
"min": 0.0,
|
||||
"max": 2.0,
|
||||
"max": 27.0,
|
||||
"unit": "mm",
|
||||
"label": "Search range",
|
||||
}
|
||||
"label": "foptz target (absolute)",
|
||||
# Live ophyd limits (dev.foptz.limits) are checked at add
|
||||
# time (CommandJobBuilderDialog._build_field_widget /
|
||||
# TomoQueueMixin._validate_action_kwargs) and again at
|
||||
# execution time (_queue_action_move_foptz_with_shift) --
|
||||
# min/max above are only the static fallback if the live
|
||||
# device can't be reached.
|
||||
"device_limits_from": "foptz",
|
||||
},
|
||||
"manual_shift_x": {
|
||||
"type": "float",
|
||||
"default": 0.0,
|
||||
"min": -10.0,
|
||||
"max": 10.0,
|
||||
"unit": "um",
|
||||
"label": "New manual_shift_x",
|
||||
},
|
||||
"manual_shift_y": {
|
||||
"type": "float",
|
||||
"default": 0.0,
|
||||
"min": -50.0,
|
||||
"max": 50.0,
|
||||
"unit": "um",
|
||||
"label": "New manual_shift_y",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
def _queue_action_move_foptz_with_shift(
|
||||
self, foptz_target: float, manual_shift_x: float, manual_shift_y: float
|
||||
) -> None:
|
||||
"""Move foptz and set new manual_shift_x/y values together.
|
||||
|
||||
manual_shift_x/manual_shift_y are replaced outright by the values
|
||||
given here, not adjusted relative to whatever they were before.
|
||||
|
||||
foptz changes the FZP-to-sample distance, which shifts the aligned
|
||||
scan center (interferometer mirrors aren't perfectly parallel to the
|
||||
beam) -- bundling the new shift into this one action is deliberate:
|
||||
foptz is NOT in _TOMO_QUEUE_MOVE_DEVICES, so this is the only way to
|
||||
queue a foptz move, precisely so the shift can't be forgotten.
|
||||
|
||||
FUTURE: manual_shift_x/y could be auto-suggested from a calibration
|
||||
proportional to the change in foptz, instead of typed in by hand --
|
||||
deliberately out of scope here.
|
||||
|
||||
Re-checks foptz's live limits at execution time too (not just add
|
||||
time, see TomoQueueMixin._validate_action_kwargs) -- the actual
|
||||
safety boundary, since a value could have arrived via the CLI,
|
||||
bypassing the add-time check, or the limits could have changed
|
||||
since the job was queued.
|
||||
"""
|
||||
lo, hi = dev.foptz.limits
|
||||
if not (lo <= foptz_target <= hi):
|
||||
raise ValueError(
|
||||
f"Queue move_foptz_with_shift: foptz_target={foptz_target} is "
|
||||
f"outside foptz's current limits ({lo}, {hi})."
|
||||
)
|
||||
umv(dev.foptz, foptz_target)
|
||||
self.manual_shift_x = manual_shift_x
|
||||
self.manual_shift_y = manual_shift_y
|
||||
|
||||
def _describe_tomo_job_line(self, job: dict) -> str:
|
||||
"""flomni's tomo_queue_show() one-line summary: fov/type/step/ctime/range.
|
||||
|
||||
@@ -4137,10 +4373,14 @@ class Flomni(
|
||||
p = job["params"]
|
||||
fovx, fovy = p.get("fovx"), p.get("fovy")
|
||||
fov_str = f"{fovx}/{fovy}um" if fovx is not None and fovy is not None else "?"
|
||||
if p.get("tomo_type") == 4:
|
||||
angles_str = f"angles={os.path.basename(p.get('tomo_angle_file') or '') or '?'}"
|
||||
else:
|
||||
angles_str = f"range={p.get('tomo_angle_range')}deg"
|
||||
return (
|
||||
f"type={p.get('tomo_type')} fov={fov_str} "
|
||||
f"step={p.get('tomo_shellstep')}um ctime={p.get('tomo_countingtime')}s "
|
||||
f"range={p.get('tomo_angle_range')}deg"
|
||||
f"{angles_str}"
|
||||
)
|
||||
|
||||
def rt_off(self):
|
||||
@@ -4204,6 +4444,19 @@ class Flomni(
|
||||
subtomo_count_value = f"{180 / self.tomo_angle_stepsize:.1f}"
|
||||
angle_step_label = "Angular step within sub-tomogram:"
|
||||
angle_step_value = f"{self.tomo_angle_stepsize:.2f}"
|
||||
elif self.tomo_type == 4:
|
||||
try:
|
||||
file_angles = read_angle_file(self.tomo_angle_file)
|
||||
total_projections = len(file_angles.angles)
|
||||
subtomo_count_value = f"{summarize_angles(file_angles.angles)['n_subtomos']}"
|
||||
if not file_angles.is_complete:
|
||||
subtomo_count_value += " (file incomplete, no 'END' marker yet)"
|
||||
except AngleFileError:
|
||||
total_projections = 0
|
||||
subtomo_count_value = "N/A (angle file not readable)"
|
||||
subtomo_count_label = "Number of individual sub-tomograms:"
|
||||
angle_step_label = "Angle file:"
|
||||
angle_step_value = self.tomo_angle_file or "N/A"
|
||||
else:
|
||||
# tomo_type == 2: golden ratio tomography -- there is no fixed
|
||||
# set of sub-tomograms or a fixed angular step, so report the
|
||||
|
||||
@@ -9,6 +9,22 @@ class FlomniOpticsError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def compute_osa_remaining_space(foptz_val: float, fosaz_val: float) -> float:
|
||||
"""Clearance (mm) between the OSA and a normal OMNY pin for a given
|
||||
foptz/fosaz pair. <=0 means collision.
|
||||
|
||||
Module-level (not a method) so it can be imported and reused outside a
|
||||
live Flomni session -- e.g. by the tomo-queue GUI (tomo_params.py),
|
||||
which only has RPC device access (dev.foptz.readback.get()/
|
||||
dev.fosaz.readback.get()), not a Flomni instance to call a bound method
|
||||
on. FlomniOpticsMixin's own methods below are thin wrappers around this
|
||||
for the two directions actually used in a live session (varying fosaz
|
||||
at the current foptz, and vice versa) -- single source of truth for the
|
||||
formula/constant either way.
|
||||
"""
|
||||
return 33 - foptz_val - fosaz_val
|
||||
|
||||
|
||||
class FlomniOpticsMixin:
|
||||
@staticmethod
|
||||
def _get_user_param_safe(device, var):
|
||||
@@ -21,7 +37,7 @@ class FlomniOpticsMixin:
|
||||
"""Clearance (mm) between the OSA and a normal OMNY pin if fosaz were
|
||||
at `fosaz_target`, at the *current* foptz reading. <=0 means collision."""
|
||||
foptz_val = dev.foptz.readback.get()
|
||||
return -fosaz_target + (33 - foptz_val)
|
||||
return compute_osa_remaining_space(foptz_val, fosaz_target)
|
||||
|
||||
def _fheater_is_up(self) -> bool:
|
||||
axis_id = dev.fheater._config["deviceConfig"].get("axis_Id")
|
||||
|
||||
@@ -73,6 +73,7 @@ class FlomniWebpageGenerator(WebpageGeneratorBase):
|
||||
"single_point_instead_of_fermat_scan",
|
||||
"ptycho_reconstruct_foldername",
|
||||
"at_each_angle_hook",
|
||||
"tomo_angle_file",
|
||||
]
|
||||
|
||||
# label -> dotpath under device_manager.devices
|
||||
@@ -86,7 +87,13 @@ class FlomniWebpageGenerator(WebpageGeneratorBase):
|
||||
# Explicitly set (not just inherited) so flomni's page keeps rendering
|
||||
# its own param names/labels even if WebpageGeneratorBase's default ever
|
||||
# changes for another setup.
|
||||
TQ_PARAM_DISPLAY = WebpageGeneratorBase.TQ_PARAM_DISPLAY
|
||||
# tomo_angle_file: tomo_type 4 (angles from file) is flomni-only. The
|
||||
# page can't read the file itself, so a type-4 job's projection count
|
||||
# stays blank (not in TOMO_TYPES) -- the live progress card still shows
|
||||
# the true total, which tomo_scan() sets from the parsed file.
|
||||
TQ_PARAM_DISPLAY = WebpageGeneratorBase.TQ_PARAM_DISPLAY + [
|
||||
("tomo_angle_file", "Angle file"),
|
||||
]
|
||||
|
||||
def _logo_path(self):
|
||||
return Path(__file__).parent / "flOMNI.png"
|
||||
|
||||
@@ -265,6 +265,17 @@ class flomniGuiTools:
|
||||
except Exception as e:
|
||||
logger.warning(f"Error in flomnigui_show_progress: {e}")
|
||||
|
||||
@staticmethod
|
||||
def _percent(numerator, denominator) -> float:
|
||||
"""numerator/denominator in percent; 0 when the denominator is not
|
||||
positive -- e.g. the total of a tomo_type 4 scan whose angle file has
|
||||
no 'END' marker yet, or an endless golden-ratio scan (0 = unknown).
|
||||
Without this the division raised, the error was swallowed, and the
|
||||
GUI stayed frozen on the reset defaults for the whole scan."""
|
||||
if not denominator or denominator <= 0:
|
||||
return 0.0
|
||||
return numerator / denominator * 100
|
||||
|
||||
def _flomnigui_update_progress(self):
|
||||
"""Update the progress ring bar and center label from the current progress state.
|
||||
|
||||
@@ -281,11 +292,9 @@ class flomniGuiTools:
|
||||
|
||||
main_progress_ring = self.progressbar.rings[0]
|
||||
subtomo_progress_ring = self.progressbar.rings[1]
|
||||
progress = self.progress["projection"] / self.progress["total_projections"] * 100
|
||||
subtomo_progress = (
|
||||
self.progress["subtomo_projection"]
|
||||
/ self.progress["subtomo_total_projections"]
|
||||
* 100
|
||||
progress = self._percent(self.progress["projection"], self.progress["total_projections"])
|
||||
subtomo_progress = self._percent(
|
||||
self.progress["subtomo_projection"], self.progress["subtomo_total_projections"]
|
||||
)
|
||||
main_progress_ring.set_value(progress)
|
||||
subtomo_progress_ring.set_value(subtomo_progress)
|
||||
|
||||
@@ -260,6 +260,7 @@ class XrayEyeAlign:
|
||||
# there.
|
||||
self.flomni.reset_correction()
|
||||
self.flomni.reset_tomo_alignment_fit()
|
||||
self.flomni.manual_shift_x = 0
|
||||
self.flomni.manual_shift_y = 0
|
||||
|
||||
self.flomni.lights_off()
|
||||
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
# TODO: port the "unfinished queue job" guard on `tomo_scan_resume()` to OMNY
|
||||
|
||||
**Status: implemented on Flomni and LamNI (2026-10), not applicable to OMNY yet** --
|
||||
OMNY has no `TomoQueueMixin`, no tomo queue and no `tomo_scan_resume()`.
|
||||
|
||||
## What landed on Flomni/LamNI
|
||||
- Problem: a Ctrl-C abort of `tomo_queue_execute()` leaves the job "running" (only
|
||||
`Exception` is caught -> "incomplete"). Calling `tomo_scan_resume()` by hand then finishes
|
||||
the scan but the queue still has the job unfinished, so a later `tomo_queue_resume()`
|
||||
resumes it again (at the last projection) and the jobs after it never ran.
|
||||
- `TomoQueueMixin._confirm_resume_outside_queue()`
|
||||
(`OMNY_shared/tomo_queue_mixin.py`): if the queue has an "incomplete"/"running" job, print a
|
||||
warning + hint (use `tomo_queue_resume()`, or mark the job done via
|
||||
`_tomo_queue_proxy.update_by_id(id, status="done")`) and ask yes/no (default no).
|
||||
- `tomo_scan_resume(from_queue=False)` in `flomni/flomni.py` and `LamNI/lamni.py` calls it
|
||||
after the "nothing to resume" check; `tomo_queue_execute()` passes `from_queue=True` so the
|
||||
unattended queue never prompts.
|
||||
|
||||
## To do for OMNY
|
||||
When the tomo queue (`TomoQueueMixin`) and a `tomo_scan_resume()` are ported to OMNY, give
|
||||
`tomo_scan_resume()` the same `from_queue` parameter and guard call, and make sure
|
||||
`tomo_queue_execute()` calls it with `from_queue=True`.
|
||||
|
||||
Related, not done anywhere: catching `KeyboardInterrupt` in `tomo_queue_execute()` so an abort
|
||||
marks the job "incomplete" instead of leaving it "running".
|
||||
@@ -21,7 +21,9 @@ Two independent "something is running" signals gate parameter submission and
|
||||
drive an advisory banner:
|
||||
|
||||
* ``tomo_progress["heartbeat"]`` — set by ``_tomo_scan_at_angle()`` at every
|
||||
projection, whether the scan was queue- or CLI-started.
|
||||
projection, whether the scan was queue- or CLI-started. For tomo_type 4,
|
||||
also refreshed while a scan is waiting at the end of an incomplete angle
|
||||
file (no projection in progress, but not idle either).
|
||||
* the BEC scan queue — catches a single ptycho projection (or any other scan)
|
||||
run manually outside a tomo run, which writes no heartbeat. The busy logic
|
||||
mirrors bec_widgets' ``BECQueue`` widget so it stays consistent with the
|
||||
@@ -43,6 +45,7 @@ from __future__ import annotations
|
||||
|
||||
import copy
|
||||
import datetime
|
||||
import os
|
||||
import uuid
|
||||
from typing import Any, Optional
|
||||
|
||||
@@ -65,6 +68,7 @@ from qtpy.QtWidgets import (
|
||||
QLabel,
|
||||
QLineEdit,
|
||||
QListWidget,
|
||||
QFileDialog,
|
||||
QMessageBox,
|
||||
QPushButton,
|
||||
QScrollArea,
|
||||
@@ -83,20 +87,27 @@ TOMO_TYPES = {
|
||||
1: "8 equally spaced sub-tomograms",
|
||||
2: "Golden ratio (sorted in bunches)",
|
||||
3: "Equally spaced, golden ratio start",
|
||||
4: "Angles from file",
|
||||
}
|
||||
|
||||
# Mirrors _update_type_visibility()'s section/field gating (_sec_type1 shown
|
||||
# only for type 1, golden_ratio_bunch_size shown only for type 2 within
|
||||
# _sec_type23, the rest of _sec_type23 shown for type 2/3) -- reused by
|
||||
# _sec_type23, the rest of _sec_type23 shown for type 2/3, _sec_type4 only
|
||||
# for type 4) -- reused by
|
||||
# _job_tooltip() so a queued job's hover details don't show fields that are
|
||||
# meaningless for its own tomo_type (e.g. golden-ratio settings on a type-1
|
||||
# job), on both flomni and lamni.
|
||||
_TYPE1_ONLY_PARAMS = {"tomo_angle_range", "zero_deg_reference_at_each_subtomo"}
|
||||
_TYPE1_ONLY_PARAMS = {
|
||||
"tomo_angle_range",
|
||||
"zero_deg_reference_at_each_subtomo",
|
||||
"tomo_type1_always_forward",
|
||||
}
|
||||
_TYPE2_ONLY_PARAMS = {"golden_ratio_bunch_size"}
|
||||
_TYPE23_PARAMS = {
|
||||
"golden_max_number_of_projections",
|
||||
"golden_projections_at_0_deg_for_damage_estimation",
|
||||
}
|
||||
_TYPE4_ONLY_PARAMS = {"tomo_angle_file"}
|
||||
|
||||
|
||||
def _irrelevant_params_for_type(tomo_type: Any) -> set[str]:
|
||||
@@ -109,9 +120,42 @@ def _irrelevant_params_for_type(tomo_type: Any) -> set[str]:
|
||||
irrelevant |= _TYPE23_PARAMS
|
||||
if tomo_type != 2:
|
||||
irrelevant |= _TYPE2_ONLY_PARAMS
|
||||
if tomo_type != 4:
|
||||
irrelevant |= _TYPE4_ONLY_PARAMS
|
||||
return irrelevant
|
||||
|
||||
|
||||
def _angle_file_status(
|
||||
path: str, opposite_warning: bool = True
|
||||
) -> tuple[Optional[str], Optional[str]]:
|
||||
"""(summary, error) for a tomo_type-4 angle file -- exactly one is None.
|
||||
The summary includes the 180-degree-pair warning, if any (only when
|
||||
``opposite_warning`` -- off for laminography), and a note if
|
||||
the file has no 'END' marker yet (not an error: a scan started against
|
||||
an incomplete file waits for more lines to be appended rather than
|
||||
failing)."""
|
||||
from csaxs_bec.bec_ipython_client.plugins.OMNY_shared.angle_file import (
|
||||
AngleFileError,
|
||||
describe_opposite_pairs,
|
||||
describe_summary,
|
||||
read_angle_file,
|
||||
summarize_angles,
|
||||
)
|
||||
|
||||
try:
|
||||
result = read_angle_file(path)
|
||||
except AngleFileError as exc:
|
||||
return None, str(exc)
|
||||
summary = summarize_angles(result.angles)
|
||||
text = describe_summary(summary)
|
||||
warning = describe_opposite_pairs(summary) if opposite_warning else None
|
||||
if warning:
|
||||
text += f"\n⚠ {warning}"
|
||||
if not result.is_complete:
|
||||
text += "\n⚠ No 'END' marker yet -- a scan will wait for more angles once started."
|
||||
return text, None
|
||||
|
||||
|
||||
STATUS_COLORS = {
|
||||
"pending": "#888888",
|
||||
"running": "#2196F3",
|
||||
@@ -134,6 +178,7 @@ QUEUE_PARAM_NAMES = (
|
||||
"stitch_y",
|
||||
"tomo_stitch_overlap",
|
||||
"ptycho_reconstruct_foldername",
|
||||
"manual_shift_x",
|
||||
"manual_shift_y",
|
||||
"frames_per_trigger",
|
||||
"single_point_instead_of_fermat_scan",
|
||||
@@ -145,6 +190,8 @@ QUEUE_PARAM_NAMES = (
|
||||
"golden_max_number_of_projections",
|
||||
"golden_projections_at_0_deg_for_damage_estimation",
|
||||
"zero_deg_reference_at_each_subtomo",
|
||||
"tomo_type1_always_forward",
|
||||
"tomo_angle_file",
|
||||
"corridor_size",
|
||||
"at_each_angle_hook",
|
||||
)
|
||||
@@ -159,6 +206,7 @@ DEFAULTS: dict[str, Any] = {
|
||||
"stitch_y": 0,
|
||||
"tomo_stitch_overlap": 0.2,
|
||||
"ptycho_reconstruct_foldername": "ptycho_reconstruct",
|
||||
"manual_shift_x": 0.0,
|
||||
"manual_shift_y": 0.0,
|
||||
"frames_per_trigger": 1,
|
||||
"single_point_instead_of_fermat_scan": False,
|
||||
@@ -168,16 +216,20 @@ DEFAULTS: dict[str, Any] = {
|
||||
"tomo_angle_range": 180,
|
||||
"tomo_angle_stepsize": 10.0,
|
||||
"zero_deg_reference_at_each_subtomo": False,
|
||||
"tomo_type1_always_forward": False,
|
||||
"golden_ratio_bunch_size": 20,
|
||||
"golden_max_number_of_projections": 1000.0,
|
||||
"golden_projections_at_0_deg_for_damage_estimation": 0,
|
||||
"tomo_angle_file": "",
|
||||
"at_each_angle_hook": None,
|
||||
}
|
||||
|
||||
# Exact tuple from LamNI._TOMO_SCAN_PARAM_NAMES (source of truth in lamni.py).
|
||||
# No tomo_angle_range/single_point_* -- lamni is laminography: always 360
|
||||
# degrees, no single-point acquisition. lamni_piezo_range_x/y have no flomni
|
||||
# equivalent; manual_shift_x is lamni-only (flomni only exposes shift_y).
|
||||
# equivalent. manual_shift_x now exists on both setups, but with a narrower
|
||||
# +/-10um range on flomni vs. lamni's own bounds (see manual_shift_fields
|
||||
# below).
|
||||
# zero_deg_reference_at_each_subtomo (tomo_type 1 only) mirrors flomni's --
|
||||
# ported to lamni as bool(subtomo_number % 2) (every odd sub-tomogram),
|
||||
# confirmed against lamni's own hardware behavior -- not flomni's
|
||||
@@ -197,6 +249,7 @@ LAMNI_QUEUE_PARAM_NAMES = (
|
||||
"lamni_piezo_range_y",
|
||||
"frames_per_trigger",
|
||||
"tomo_type",
|
||||
"tomo_angle_file",
|
||||
"tomo_angle_stepsize",
|
||||
"golden_ratio_bunch_size",
|
||||
"golden_max_number_of_projections",
|
||||
@@ -221,6 +274,7 @@ LAMNI_DEFAULTS: dict[str, Any] = {
|
||||
"lamni_piezo_range_y": 20,
|
||||
"frames_per_trigger": 1,
|
||||
"tomo_type": 1,
|
||||
"tomo_angle_file": "",
|
||||
"tomo_angle_stepsize": 10.0,
|
||||
"golden_ratio_bunch_size": 20,
|
||||
"golden_max_number_of_projections": 1000.0,
|
||||
@@ -440,8 +494,8 @@ class OMNY_TomoParams(BECWidget, QWidget):
|
||||
type_row = QHBoxLayout()
|
||||
type_row.addWidget(QLabel("Tomo type:"))
|
||||
self._pw["tomo_type"] = QComboBox()
|
||||
for tid, label in TOMO_TYPES.items():
|
||||
self._pw["tomo_type"].addItem(f"{tid} – {label}", tid)
|
||||
for tid in self._profile["tomo_types"]:
|
||||
self._pw["tomo_type"].addItem(f"{tid} – {TOMO_TYPES[tid]}", tid)
|
||||
self._pw["tomo_type"].currentIndexChanged.connect(self._on_type_changed)
|
||||
type_row.addWidget(self._pw["tomo_type"], stretch=1)
|
||||
vbox.addLayout(type_row)
|
||||
@@ -485,7 +539,9 @@ class OMNY_TomoParams(BECWidget, QWidget):
|
||||
common_form, "ptycho_reconstruct_foldername", "Reconstruct folder"
|
||||
),
|
||||
"manual_shift": lambda: self._build_double_group(
|
||||
common_form, self._profile["manual_shift_fields"]
|
||||
common_form,
|
||||
self._profile["manual_shift_fields"],
|
||||
help_texts=self._profile.get("manual_shift_help"),
|
||||
),
|
||||
"piezo_range": lambda: self._build_double_group(
|
||||
common_form, self._profile["piezo_range_fields"]
|
||||
@@ -520,13 +576,25 @@ class OMNY_TomoParams(BECWidget, QWidget):
|
||||
self._sec_type23 = self._build_type23_section()
|
||||
vbox.addWidget(self._sec_type23)
|
||||
|
||||
# type-4 section (angles from file) -- only for setups supporting it
|
||||
self._sec_type4 = None
|
||||
if 4 in self._profile["tomo_types"]:
|
||||
self._sec_type4 = self._build_type4_section()
|
||||
vbox.addWidget(self._sec_type4)
|
||||
|
||||
return box
|
||||
|
||||
def _build_double_group(
|
||||
self, form: QFormLayout, fields: list[tuple[str, str, float, float, int]]
|
||||
self,
|
||||
form: QFormLayout,
|
||||
fields: list[tuple[str, str, float, float, int]],
|
||||
help_texts: dict[str, str] | None = None,
|
||||
) -> None:
|
||||
for key, label, min_, max_, decimals in fields:
|
||||
self._add_double(form, key, label, min_=min_, max_=max_, decimals=decimals)
|
||||
help_text = help_texts.get(key) if help_texts else None
|
||||
self._add_double(
|
||||
form, key, label, min_=min_, max_=max_, decimals=decimals, help_text=help_text
|
||||
)
|
||||
|
||||
def _build_int_group(self, form: QFormLayout, fields: list[tuple[str, str, int, int]]) -> None:
|
||||
for key, label, min_, max_ in fields:
|
||||
@@ -577,7 +645,9 @@ class OMNY_TomoParams(BECWidget, QWidget):
|
||||
form.addRow(header_row)
|
||||
for key, label, min_, max_, decimals, _axis in self._profile["offset_fields"]:
|
||||
self._add_double(form, key, label, min_=min_, max_=max_, decimals=decimals)
|
||||
self._build_double_group(form, self._profile["manual_shift_fields"])
|
||||
self._build_double_group(
|
||||
form, self._profile["manual_shift_fields"], help_texts=self._profile.get("manual_shift_help")
|
||||
)
|
||||
|
||||
def _show_offsets_help(self) -> None:
|
||||
QMessageBox.information(
|
||||
@@ -648,6 +718,9 @@ class OMNY_TomoParams(BECWidget, QWidget):
|
||||
self._pw["tomo_angle_range"].currentIndexChanged.connect(
|
||||
self._update_projection_preview
|
||||
)
|
||||
self._pw["tomo_angle_range"].currentIndexChanged.connect(
|
||||
self._update_always_forward_enabled
|
||||
)
|
||||
form.addRow("Angle range:", self._pw["tomo_angle_range"])
|
||||
|
||||
# requested total projections (user entry in edit mode)
|
||||
@@ -666,6 +739,8 @@ class OMNY_TomoParams(BECWidget, QWidget):
|
||||
|
||||
if self._profile["has_zero_deg_reference"]:
|
||||
self._add_bool(form, "zero_deg_reference_at_each_subtomo", "0° reference each sub-tomo")
|
||||
if self._profile.get("has_always_forward"):
|
||||
self._add_bool(form, "tomo_type1_always_forward", "Run all sub-tomos forward")
|
||||
return box
|
||||
|
||||
def _build_type23_section(self) -> QGroupBox:
|
||||
@@ -702,6 +777,87 @@ class OMNY_TomoParams(BECWidget, QWidget):
|
||||
)
|
||||
return box
|
||||
|
||||
def _build_type4_section(self) -> QGroupBox:
|
||||
box = QGroupBox("Type 4 – angles from file")
|
||||
form = QFormLayout(box)
|
||||
form.setLabelAlignment(Qt.AlignmentFlag.AlignRight)
|
||||
|
||||
# tomo_angle_file lives in self._pw like any other param, so the
|
||||
# generic _populate_fields()/_read_fields() handle it; the Browse
|
||||
# button is enabled/disabled alongside it in _set_fields_enabled().
|
||||
file_row = QHBoxLayout()
|
||||
self._pw["tomo_angle_file"] = QLineEdit()
|
||||
self._pw["tomo_angle_file"].setEnabled(False)
|
||||
self._pw["tomo_angle_file"].setPlaceholderText("path to angle file")
|
||||
self._pw["tomo_angle_file"].setToolTip(
|
||||
"Plain text, one angle (-2 to 362 deg) per line, optional 2nd column = "
|
||||
"sub-tomogram number (default 1), '#' starts a comment. Acquired in "
|
||||
"file order. Only the path is stored: the file is re-read at every "
|
||||
"scan start, resume, and when a queued job runs."
|
||||
)
|
||||
self._pw["tomo_angle_file"].textChanged.connect(self._update_angle_file_preview)
|
||||
file_row.addWidget(self._pw["tomo_angle_file"], stretch=1)
|
||||
self._btn_browse_angle_file = QPushButton("Browse…")
|
||||
self._btn_browse_angle_file.setEnabled(False)
|
||||
self._btn_browse_angle_file.clicked.connect(self._browse_angle_file)
|
||||
file_row.addWidget(self._btn_browse_angle_file)
|
||||
form.addRow("Angle file:", file_row)
|
||||
|
||||
self._lbl_angle_file_summary = QLabel("---")
|
||||
self._lbl_angle_file_summary.setWordWrap(True)
|
||||
form.addRow("Content:", self._lbl_angle_file_summary)
|
||||
return box
|
||||
|
||||
def _browse_angle_file(self) -> None:
|
||||
current = self._pw["tomo_angle_file"].text().strip()
|
||||
start_dir = os.path.dirname(os.path.expanduser(current)) if current else ""
|
||||
if not start_dir or not os.path.isdir(start_dir):
|
||||
start_dir = os.path.expanduser("~")
|
||||
path, _ = QFileDialog.getOpenFileName(
|
||||
self,
|
||||
"Select tomography angle file",
|
||||
start_dir,
|
||||
"Angle files (*.txt *.dat *.csv);;All files (*)",
|
||||
)
|
||||
if path:
|
||||
self._pw["tomo_angle_file"].setText(path)
|
||||
|
||||
def _set_angle_file_summary(self, text: str, stylesheet: str) -> None:
|
||||
"""Set _lbl_angle_file_summary's text/style, and recompute its
|
||||
minimum height from heightForWidth() at its current width.
|
||||
|
||||
QFormLayout sizes a row from the field widget's sizeHint() at
|
||||
construction time (when the label still holds its short initial
|
||||
placeholder); it doesn't reliably recompute that row's height later
|
||||
when setText() swaps in longer, word-wrapped content -- a known Qt
|
||||
quirk. Without this, multi-line content (e.g. a summary with a
|
||||
180-deg-pair or missing-END warning) gets visually clipped instead
|
||||
of wrapping into a taller row."""
|
||||
self._lbl_angle_file_summary.setText(text)
|
||||
self._lbl_angle_file_summary.setStyleSheet(stylesheet)
|
||||
w = self._lbl_angle_file_summary.width()
|
||||
if w > 0:
|
||||
self._lbl_angle_file_summary.setMinimumHeight(
|
||||
self._lbl_angle_file_summary.heightForWidth(w)
|
||||
)
|
||||
|
||||
def _update_angle_file_preview(self) -> None:
|
||||
if self._sec_type4 is None:
|
||||
return
|
||||
path = self._pw["tomo_angle_file"].text().strip()
|
||||
if not path:
|
||||
self._set_angle_file_summary("(no file set)", "color: orange;")
|
||||
return
|
||||
summary, error = _angle_file_status(
|
||||
path, self._profile.get("angle_file_opposite_warning", True)
|
||||
)
|
||||
if error:
|
||||
self._set_angle_file_summary(error, "color: red;")
|
||||
else:
|
||||
self._set_angle_file_summary(
|
||||
summary, "color: orange;" if "⚠" in summary else ""
|
||||
)
|
||||
|
||||
def _build_edit_buttons(self) -> QHBoxLayout:
|
||||
layout = QHBoxLayout()
|
||||
self._btn_edit = QPushButton("Edit")
|
||||
@@ -729,14 +885,36 @@ class OMNY_TomoParams(BECWidget, QWidget):
|
||||
# ── widget factory helpers ────────────────────────────────────────────────
|
||||
|
||||
def _add_double(
|
||||
self, form: QFormLayout, key: str, label: str, min_: float, max_: float, decimals: int
|
||||
self,
|
||||
form: QFormLayout,
|
||||
key: str,
|
||||
label: str,
|
||||
min_: float,
|
||||
max_: float,
|
||||
decimals: int,
|
||||
help_text: str | None = None,
|
||||
) -> QDoubleSpinBox:
|
||||
w = QDoubleSpinBox()
|
||||
w.setDecimals(decimals)
|
||||
w.setRange(min_, max_)
|
||||
w.setEnabled(False)
|
||||
self._pw[key] = w
|
||||
form.addRow(f"{label}:", w)
|
||||
if help_text is None:
|
||||
form.addRow(f"{label}:", w)
|
||||
else:
|
||||
# same "?" help-button pattern as _build_offsets_section/
|
||||
# _build_fermat_asymmetry_row -- a per-field popup instead of a
|
||||
# per-group one.
|
||||
row = QHBoxLayout()
|
||||
row.addWidget(w, stretch=1)
|
||||
btn_help = QPushButton("?")
|
||||
btn_help.setFixedWidth(24)
|
||||
btn_help.setToolTip(f"What does {label} do?")
|
||||
btn_help.clicked.connect(
|
||||
lambda: QMessageBox.information(self, label, help_text)
|
||||
)
|
||||
row.addWidget(btn_help)
|
||||
form.addRow(f"{label}:", row)
|
||||
return w
|
||||
|
||||
def _add_int(self, form: QFormLayout, key: str, label: str, min_: int, max_: int) -> QSpinBox:
|
||||
@@ -874,6 +1052,12 @@ class OMNY_TomoParams(BECWidget, QWidget):
|
||||
if numprj > 0:
|
||||
params["tomo_angle_stepsize"] = self._profile["type1_base_angle"] / numprj
|
||||
|
||||
# store angle files as absolute paths, same as the CLI setter
|
||||
if params.get("tomo_angle_file"):
|
||||
params["tomo_angle_file"] = os.path.abspath(
|
||||
os.path.expanduser(params["tomo_angle_file"])
|
||||
)
|
||||
|
||||
# single_point forces stitch to 0
|
||||
if self._profile["has_single_point"] and params.get("single_point_instead_of_fermat_scan"):
|
||||
for key, _label, _min, _max in self._profile["stitch_fields"]:
|
||||
@@ -893,8 +1077,16 @@ class OMNY_TomoParams(BECWidget, QWidget):
|
||||
fpt = params.get("frames_per_trigger", 1)
|
||||
if not isinstance(fpt, int) or fpt < 1 or isinstance(fpt, bool):
|
||||
return "frames_per_trigger must be a positive integer"
|
||||
if params.get("tomo_type") not in (1, 2, 3):
|
||||
return "tomo_type must be 1, 2, or 3"
|
||||
if params.get("tomo_type") not in self._profile["tomo_types"]:
|
||||
allowed = ", ".join(str(t) for t in self._profile["tomo_types"])
|
||||
return f"tomo_type must be one of {allowed}"
|
||||
if params.get("tomo_type") == 4:
|
||||
_summary, error = _angle_file_status(
|
||||
params.get("tomo_angle_file", ""),
|
||||
self._profile.get("angle_file_opposite_warning", True),
|
||||
)
|
||||
if error:
|
||||
return f"Angle file: {error}"
|
||||
if self._profile["has_180_mode"] and params.get("tomo_angle_range") not in (180, 360):
|
||||
return "tomo_angle_range must be 180 or 360"
|
||||
if self._profile["has_single_point"]:
|
||||
@@ -912,33 +1104,30 @@ class OMNY_TomoParams(BECWidget, QWidget):
|
||||
|
||||
# ── type visibility ───────────────────────────────────────────────────────
|
||||
|
||||
def _set_form_row_visible(self, form_owner: QGroupBox, key: str, visible: bool) -> None:
|
||||
"""Show/hide one self._pw field together with its QFormLayout label."""
|
||||
widget = self._pw.get(key)
|
||||
if widget is None:
|
||||
return
|
||||
widget.setVisible(visible)
|
||||
form = form_owner.layout()
|
||||
if isinstance(form, QFormLayout):
|
||||
idx = form.indexOf(widget)
|
||||
if idx >= 0:
|
||||
row, _role = form.getItemPosition(idx)
|
||||
lbl = form.itemAt(row, QFormLayout.ItemRole.LabelRole)
|
||||
if lbl and lbl.widget():
|
||||
lbl.widget().setVisible(visible)
|
||||
|
||||
def _update_type_visibility(self, tomo_type: int) -> None:
|
||||
self._sec_type1.setVisible(tomo_type == 1)
|
||||
self._sec_type23.setVisible(tomo_type in (2, 3))
|
||||
# _numprj_type3: only visible for type 3
|
||||
_w__numprj_type3 = self._pw.get("_numprj_type3")
|
||||
if _w__numprj_type3 is not None:
|
||||
_w__numprj_type3.setVisible(tomo_type == 3)
|
||||
_form = self._sec_type23.layout()
|
||||
if isinstance(_form, QFormLayout):
|
||||
_idx = _form.indexOf(_w__numprj_type3)
|
||||
if _idx >= 0:
|
||||
_row, _role = _form.getItemPosition(_idx)
|
||||
_lbl = _form.itemAt(_row, QFormLayout.ItemRole.LabelRole)
|
||||
if _lbl and _lbl.widget():
|
||||
_lbl.widget().setVisible(tomo_type == 3)
|
||||
# golden_ratio_bunch_size: only visible for type 2
|
||||
_w_golden_ratio_bunch_size = self._pw.get("golden_ratio_bunch_size")
|
||||
if _w_golden_ratio_bunch_size is not None:
|
||||
_w_golden_ratio_bunch_size.setVisible(tomo_type == 2)
|
||||
_form = self._sec_type23.layout()
|
||||
if isinstance(_form, QFormLayout):
|
||||
_idx = _form.indexOf(_w_golden_ratio_bunch_size)
|
||||
if _idx >= 0:
|
||||
_row, _role = _form.getItemPosition(_idx)
|
||||
_lbl = _form.itemAt(_row, QFormLayout.ItemRole.LabelRole)
|
||||
if _lbl and _lbl.widget():
|
||||
_lbl.widget().setVisible(tomo_type == 2)
|
||||
self._set_form_row_visible(self._sec_type23, "_numprj_type3", tomo_type == 3)
|
||||
self._set_form_row_visible(self._sec_type23, "golden_ratio_bunch_size", tomo_type == 2)
|
||||
if self._sec_type4 is not None:
|
||||
self._sec_type4.setVisible(tomo_type == 4)
|
||||
if tomo_type == 4:
|
||||
self._update_angle_file_preview()
|
||||
|
||||
# ── type-1 projection preview ─────────────────────────────────────────────
|
||||
|
||||
@@ -1148,8 +1337,10 @@ class OMNY_TomoParams(BECWidget, QWidget):
|
||||
|
||||
def _enter_edit_mode_with(self, params: dict[str, Any]) -> None:
|
||||
self._populate_fields(params)
|
||||
self._set_fields_enabled(True)
|
||||
# _edit_mode must be set first: _set_fields_enabled() consults it
|
||||
# (e.g. for the 'run all sub-tomos forward' checkbox).
|
||||
self._edit_mode = True
|
||||
self._set_fields_enabled(True)
|
||||
self._btn_edit.setVisible(False)
|
||||
self._btn_submit.setVisible(True)
|
||||
self._btn_add_edited_to_queue.setVisible(True)
|
||||
@@ -1175,9 +1366,10 @@ class OMNY_TomoParams(BECWidget, QWidget):
|
||||
``_tomo_scan_at_angle()`` at the start of every projection — this is
|
||||
updated whether the scan was started via the queue or directly via
|
||||
``<setup>.tomo_scan()``, so it's more reliable than queue job status.
|
||||
A heartbeat fresher than 2 minutes indicates a scan in progress;
|
||||
older than that it's either done, or stuck long enough that editing
|
||||
parameters is intentional.
|
||||
For flomni tomo_type 4, it is also refreshed while the scan waits at
|
||||
the end of an incomplete angle file. A heartbeat fresher than 2
|
||||
minutes indicates a scan in progress; older than that it's either
|
||||
done, or stuck long enough that editing parameters is intentional.
|
||||
"""
|
||||
import datetime
|
||||
|
||||
@@ -1331,6 +1523,14 @@ class OMNY_TomoParams(BECWidget, QWidget):
|
||||
self, "Added to queue", f"Added '{jobs[-1]['label']}' to the queue."
|
||||
)
|
||||
|
||||
def _update_always_forward_enabled(self, *_args) -> None:
|
||||
"""'Run all sub-tomos forward' only applies to the 180 degree range."""
|
||||
cb = self._pw.get("tomo_type1_always_forward")
|
||||
combo = self._pw.get("tomo_angle_range")
|
||||
if cb is None or combo is None:
|
||||
return
|
||||
cb.setEnabled(self._edit_mode and combo.currentData() == 180)
|
||||
|
||||
def _set_fields_enabled(self, enabled: bool) -> None:
|
||||
skip = {"_requested_total"} if not enabled else set()
|
||||
for key, widget in self._pw.items():
|
||||
@@ -1347,6 +1547,10 @@ class OMNY_TomoParams(BECWidget, QWidget):
|
||||
self._pw["single_point_random_shift_max"].setEnabled(enabled and single_point)
|
||||
# projection requested total only meaningful in edit mode
|
||||
self._pw["_requested_total"].setEnabled(enabled)
|
||||
if self._sec_type4 is not None:
|
||||
self._btn_browse_angle_file.setEnabled(enabled)
|
||||
if enabled:
|
||||
self._update_always_forward_enabled()
|
||||
|
||||
# ── queue dialog ──────────────────────────────────────────────────────────
|
||||
|
||||
@@ -1495,12 +1699,10 @@ class TomoQueueDialog(QDialog):
|
||||
btn_row = QHBoxLayout()
|
||||
self._btn_add = QPushButton("Add current params to queue")
|
||||
self._btn_add_cmd = QPushButton("Add command…")
|
||||
# Text/tooltip are dual-purpose ("Load into editor" for a tomo job,
|
||||
# "Edit command" for a pending command job) and set by
|
||||
# _update_load_button(), called immediately below via _refresh().
|
||||
self._btn_load = QPushButton("Load into editor")
|
||||
self._btn_load.setToolTip(
|
||||
"Load the selected tomo job's saved settings into the params panel's "
|
||||
"editor -- doesn't touch live params or the queue itself. Review, "
|
||||
'tweak, then Submit or "Add to queue" from there.'
|
||||
)
|
||||
self._btn_load.setEnabled(False)
|
||||
self._btn_del = QPushButton("Delete selected")
|
||||
self._btn_dup = QPushButton("Duplicate selected")
|
||||
@@ -1522,7 +1724,7 @@ class TomoQueueDialog(QDialog):
|
||||
btn_exe = QPushButton("Execute queue…")
|
||||
self._btn_add.clicked.connect(self.add_to_queue)
|
||||
self._btn_add_cmd.clicked.connect(self.add_command_to_queue)
|
||||
self._btn_load.clicked.connect(self._on_load_into_editor)
|
||||
self._btn_load.clicked.connect(self._on_load_or_edit_clicked)
|
||||
self._btn_del.clicked.connect(self._delete_selected)
|
||||
self._btn_dup.clicked.connect(self._duplicate_selected)
|
||||
self._btn_clr.clicked.connect(self._clear_queue)
|
||||
@@ -1666,10 +1868,57 @@ class TomoQueueDialog(QDialog):
|
||||
job = jobs[row]
|
||||
return job if _job_kind(job) == "tomo" else None
|
||||
|
||||
def _selected_single_pending_command_job(self) -> Optional[dict]:
|
||||
"""Return the single selected job if it's a *pending* command job,
|
||||
else None. Mirrors _selected_single_tomo_job() above.
|
||||
|
||||
Restricted to self._MOVABLE_STATUSES ("pending" only, not
|
||||
"incomplete") -- the same set already used for "is this row safe
|
||||
to reorder". An incomplete command job may have partially executed
|
||||
steps; editing that is a materially different, riskier case than a
|
||||
never-started job, and not supported here.
|
||||
"""
|
||||
rows = sorted({idx.row() for idx in self._table.selectedIndexes()})
|
||||
if len(rows) != 1:
|
||||
return None
|
||||
jobs = self._load_queue()
|
||||
row = rows[0]
|
||||
if not 0 <= row < len(jobs):
|
||||
return None
|
||||
job = jobs[row]
|
||||
if _job_kind(job) != "command":
|
||||
return None
|
||||
return job if job.get("status", "pending") in self._MOVABLE_STATUSES else None
|
||||
|
||||
def _update_load_button(self) -> None:
|
||||
self._btn_load.setEnabled(
|
||||
not self._sort_mode and self._selected_single_tomo_job() is not None
|
||||
)
|
||||
"""self._btn_load is dual-purpose: "Load into editor" for a single
|
||||
selected tomo job (any status, unchanged behavior), or "Edit
|
||||
command" for a single selected *pending* command job (new -- see
|
||||
_on_load_or_edit_clicked()). Anything else disables it.
|
||||
"""
|
||||
if self._sort_mode:
|
||||
self._btn_load.setText("Load into editor")
|
||||
self._btn_load.setEnabled(False)
|
||||
return
|
||||
if self._selected_single_tomo_job() is not None:
|
||||
self._btn_load.setText("Load into editor")
|
||||
self._btn_load.setToolTip(
|
||||
"Load the selected tomo job's saved settings into the params panel's "
|
||||
"editor -- doesn't touch live params or the queue itself. Review, "
|
||||
'tweak, then Submit or "Add to queue" from there.'
|
||||
)
|
||||
self._btn_load.setEnabled(True)
|
||||
elif self._selected_single_pending_command_job() is not None:
|
||||
self._btn_load.setText("Edit command")
|
||||
self._btn_load.setToolTip(
|
||||
"Open the selected pending command job's steps in the command "
|
||||
"builder for review/editing. OK replaces this queued job in "
|
||||
"place (same position); Cancel leaves it untouched."
|
||||
)
|
||||
self._btn_load.setEnabled(True)
|
||||
else:
|
||||
self._btn_load.setText("Load into editor")
|
||||
self._btn_load.setEnabled(False)
|
||||
|
||||
def _update_duplicate_button(self) -> None:
|
||||
# Unlike Load-into-editor, duplication works for a job of any kind
|
||||
@@ -1677,15 +1926,38 @@ class TomoQueueDialog(QDialog):
|
||||
# "exactly one row selected" is the only requirement.
|
||||
self._btn_dup.setEnabled(not self._sort_mode and self._selected_single_row() >= 0)
|
||||
|
||||
def _on_load_into_editor(self) -> None:
|
||||
job = self._selected_single_tomo_job()
|
||||
if job is None:
|
||||
def _on_load_or_edit_clicked(self) -> None:
|
||||
"""Dispatches self._btn_load's two behaviors -- see
|
||||
_update_load_button()'s docstring. Re-derives the selection fresh
|
||||
rather than trusting anything cached since the last ~2s poll.
|
||||
"""
|
||||
tomo_job = self._selected_single_tomo_job()
|
||||
if tomo_job is not None:
|
||||
parent = self.parent()
|
||||
if parent is None or not hasattr(parent, "load_params_into_editor"):
|
||||
QMessageBox.warning(self, "Can't load", "No params panel available to load into.")
|
||||
return
|
||||
parent.load_params_into_editor(dict(tomo_job.get("params", {})), tomo_job.get("label", ""))
|
||||
return
|
||||
parent = self.parent()
|
||||
if parent is None or not hasattr(parent, "load_params_into_editor"):
|
||||
QMessageBox.warning(self, "Can't load", "No params panel available to load into.")
|
||||
|
||||
command_job = self._selected_single_pending_command_job()
|
||||
if command_job is None:
|
||||
return
|
||||
parent.load_params_into_editor(dict(job.get("params", {})), job.get("label", ""))
|
||||
row = sorted({idx.row() for idx in self._table.selectedIndexes()})[0]
|
||||
dlg = CommandJobBuilderDialog(self._client, parent=self, job=command_job)
|
||||
if dlg.exec() == QDialog.DialogCode.Accepted and dlg.job is not None:
|
||||
jobs = self._load_queue()
|
||||
if 0 <= row < len(jobs) and jobs[row].get("id") == command_job.get("id"):
|
||||
jobs[row] = dlg.job
|
||||
self._save_queue(jobs)
|
||||
self._refresh()
|
||||
else:
|
||||
QMessageBox.warning(
|
||||
self,
|
||||
"Queue changed",
|
||||
"The queue changed while editing this job -- nothing was "
|
||||
"replaced. Re-select the job and try again.",
|
||||
)
|
||||
|
||||
# ── sort mode ─────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -1909,12 +2181,20 @@ class CommandJobBuilderDialog(QDialog):
|
||||
assembled ``steps`` list) so CLI and GUI stay interchangeable. The caller
|
||||
is responsible for appending it to the queue -- this dialog only builds
|
||||
the dict, it never touches the queue global var itself.
|
||||
|
||||
Pass an existing command ``job`` dict to pre-populate the builder for
|
||||
editing (``TomoQueueDialog._on_load_or_edit_clicked()``'s "Edit
|
||||
command" path) instead of starting empty. ``self.job`` on accept then
|
||||
keeps that job's original ``id``/``added_at`` (same queue entry being
|
||||
revised, not a new addition) -- the caller still owns writing it back
|
||||
into the queue at the right position.
|
||||
"""
|
||||
|
||||
def __init__(self, client, parent=None):
|
||||
def __init__(self, client, parent=None, job: Optional[dict] = None):
|
||||
super().__init__(parent)
|
||||
self._client = client
|
||||
self.setWindowTitle("Add command job")
|
||||
self._editing_job = job
|
||||
self.setWindowTitle("Edit command job" if job is not None else "Add command job")
|
||||
self.setMinimumSize(520, 480)
|
||||
self.job: Optional[dict] = None
|
||||
|
||||
@@ -1937,6 +2217,28 @@ class CommandJobBuilderDialog(QDialog):
|
||||
"can't be built without it.",
|
||||
)
|
||||
|
||||
if job is not None:
|
||||
self._populate_from_job(job)
|
||||
|
||||
def _populate_from_job(self, job: dict) -> None:
|
||||
"""Pre-fill the builder from an existing command job (the "Edit
|
||||
command" path). Steps are re-added in the same {action, kwargs}
|
||||
shape _on_add_step() produces, using its exact summary format, so
|
||||
pre-populated rows render identically to freshly-added ones.
|
||||
"""
|
||||
self._label_edit.setText(job.get("label", ""))
|
||||
for step in job.get("steps", []):
|
||||
action_name = step.get("action")
|
||||
kwargs = step.get("kwargs", {})
|
||||
self._steps.append({"action": action_name, "kwargs": kwargs})
|
||||
summary = f"{action_name}{kwargs}" if kwargs else action_name
|
||||
self._step_list.addItem(summary)
|
||||
# The job's actual saved flag, not a recomputed default -- mark
|
||||
# overridden so _recompute_idempotent_default() leaves it alone as
|
||||
# the operator edits steps.
|
||||
self._idem_check.setChecked(bool(job.get("idempotent", False)))
|
||||
self._idem_user_overridden = True
|
||||
|
||||
def _build_ui(self):
|
||||
vbox = QVBoxLayout(self)
|
||||
|
||||
@@ -2015,12 +2317,78 @@ class CommandJobBuilderDialog(QDialog):
|
||||
label = f"{label} ({unit})"
|
||||
self._field_form.addRow(f"{label}:", widget)
|
||||
|
||||
if action_name == "move_foptz_with_shift" and "foptz_target" in self._step_field_widgets:
|
||||
self._build_osa_collision_warning()
|
||||
|
||||
def _build_osa_collision_warning(self) -> None:
|
||||
"""Live, advisory-only OSA-collision warning for
|
||||
move_foptz_with_shift's foptz_target field.
|
||||
|
||||
Checked against fosaz's *defined* IN position (its "in"
|
||||
user_parameter), not wherever fosaz happens to be reading right
|
||||
now -- fosaz is normally parked safely OUT except when actually in
|
||||
use, so checking the live position would almost never warn. The
|
||||
real question is "if the OSA were swapped in at its nominal
|
||||
position while foptz is at this target, would it collide", which
|
||||
is also exactly what FlomniOpticsMixin.fosa_in() itself checks
|
||||
(and refuses on) for fosaz moves -- this surfaces the same check
|
||||
here, at data-entry time, for foptz instead.
|
||||
|
||||
The move itself stays a plain absolute move (see
|
||||
_queue_action_move_foptz_with_shift's docstring) -- this is
|
||||
advisory only, the queue action itself doesn't block on it.
|
||||
"""
|
||||
from csaxs_bec.bec_ipython_client.plugins.flomni.flomni_optics_mixin import (
|
||||
compute_osa_remaining_space,
|
||||
)
|
||||
|
||||
lbl = QLabel()
|
||||
lbl.setWordWrap(True)
|
||||
lbl.setStyleSheet("color: #B8860B;")
|
||||
lbl.setVisible(False)
|
||||
self._field_form.addRow("", lbl)
|
||||
|
||||
foptz_widget, _field = self._step_field_widgets["foptz_target"]
|
||||
|
||||
def _update(_value=None):
|
||||
fosaz = self._client.device_manager.devices.get("fosaz")
|
||||
if fosaz is None:
|
||||
return
|
||||
try:
|
||||
fosaz_in = (fosaz.user_parameter or {}).get("in")
|
||||
except Exception:
|
||||
return
|
||||
if fosaz_in is None:
|
||||
return
|
||||
clearance = compute_osa_remaining_space(foptz_widget.value(), fosaz_in)
|
||||
if clearance <= 0:
|
||||
lbl.setText(
|
||||
f"Warning: at this foptz target, OSA clearance at its defined "
|
||||
f"IN position ({fosaz_in:.2f} mm) would be {clearance:.2f} mm "
|
||||
"(<=0 = collision risk with a normal OMNY pin)."
|
||||
)
|
||||
lbl.setVisible(True)
|
||||
else:
|
||||
lbl.setVisible(False)
|
||||
|
||||
foptz_widget.valueChanged.connect(_update)
|
||||
_update()
|
||||
|
||||
def _build_field_widget(self, field: dict) -> QWidget:
|
||||
field_type = field.get("type")
|
||||
if field_type == "float":
|
||||
w = QDoubleSpinBox()
|
||||
w.setDecimals(4)
|
||||
w.setRange(field.get("min", -1e9), field.get("max", 1e9))
|
||||
lo, hi = field.get("min", -1e9), field.get("max", 1e9)
|
||||
device_name = field.get("device_limits_from")
|
||||
if device_name is not None:
|
||||
device = self._client.device_manager.devices.get(device_name)
|
||||
if device is not None:
|
||||
try:
|
||||
lo, hi = device.limits
|
||||
except Exception:
|
||||
pass # fall back to the static schema min/max above
|
||||
w.setRange(lo, hi)
|
||||
w.setValue(field.get("default", 0.0))
|
||||
return w
|
||||
if field_type == "device_positions":
|
||||
@@ -2118,14 +2486,22 @@ class CommandJobBuilderDialog(QDialog):
|
||||
QMessageBox.warning(self, "No steps", "Add at least one step before confirming.")
|
||||
return
|
||||
label = self._label_edit.text().strip() or f"cmd_{uuid.uuid4().hex[:6]}"
|
||||
# Editing an existing job keeps its id/added_at -- it's the same
|
||||
# queue entry being revised, not a new addition.
|
||||
job_id = self._editing_job["id"] if self._editing_job else uuid.uuid4().hex
|
||||
added_at = (
|
||||
self._editing_job["added_at"]
|
||||
if self._editing_job
|
||||
else datetime.datetime.now().isoformat(timespec="seconds")
|
||||
)
|
||||
self.job = {
|
||||
"kind": "command",
|
||||
"id": uuid.uuid4().hex,
|
||||
"id": job_id,
|
||||
"label": label,
|
||||
"steps": list(self._steps),
|
||||
"idempotent": self._idem_check.isChecked(),
|
||||
"status": "pending",
|
||||
"added_at": datetime.datetime.now().isoformat(timespec="seconds"),
|
||||
"added_at": added_at,
|
||||
}
|
||||
self.accept()
|
||||
|
||||
@@ -2329,14 +2705,27 @@ SETUP_PROFILES: dict[str, dict[str, Any]] = {
|
||||
"defaults": DEFAULTS,
|
||||
"has_180_mode": True,
|
||||
"has_single_point": True,
|
||||
# tomo_type 4 (angles from file) is flomni-only
|
||||
"tomo_types": (1, 2, 3, 4),
|
||||
"has_zero_deg_reference": True,
|
||||
"has_always_forward": True,
|
||||
"golden_bunch_size_min": 1,
|
||||
"fov_fields": [
|
||||
("fovx", "FOV x (µm)", 0.1, 220.0, 2),
|
||||
("fovy", "FOV y (µm)", 0.1, 99.9, 2),
|
||||
],
|
||||
"stitch_fields": [("stitch_x", "Stitch x", 0, 10), ("stitch_y", "Stitch y", 0, 10)],
|
||||
"manual_shift_fields": [("manual_shift_y", "Manual shift y (µm)", -50.0, 50.0, 3)],
|
||||
"manual_shift_fields": [
|
||||
("manual_shift_x", "Manual shift x (µm)", -10.0, 10.0, 3),
|
||||
("manual_shift_y", "Manual shift y (µm)", -50.0, 50.0, 3),
|
||||
],
|
||||
"manual_shift_help": {
|
||||
"manual_shift_x": (
|
||||
"A constant offset applied on top of the alignment, independent of "
|
||||
"the rotation angle. Normally zero -- only needed in special cases, "
|
||||
"e.g. when the FZP-to-sample distance is changed after alignment."
|
||||
)
|
||||
},
|
||||
"piezo_range_fields": [],
|
||||
"offset_fields": [],
|
||||
"offset_getter": None,
|
||||
@@ -2370,6 +2759,9 @@ SETUP_PROFILES: dict[str, dict[str, Any]] = {
|
||||
"defaults": LAMNI_DEFAULTS,
|
||||
"has_180_mode": False,
|
||||
"has_single_point": False,
|
||||
"tomo_types": (1, 2, 3, 4),
|
||||
# laminography: 180-deg-opposite angles are not redundant
|
||||
"angle_file_opposite_warning": False,
|
||||
"has_zero_deg_reference": True,
|
||||
# lamni.golden_ratio_bunch_size setter/CLI require >= 20 (see
|
||||
# _build_params_panel's bunch-size spinbox)
|
||||
|
||||
@@ -4,13 +4,15 @@
|
||||
# JungfrauJoch-side config (detector selection/init, pixel mask, azimuthal integration
|
||||
# settings) is done in the Web UI at http://sls-jfjoch-001.psi.ch:8080.
|
||||
# The Eiger 2 Si 500K detector server itself is reachable at 129.129.122.159.
|
||||
# eiger2_si_500k:
|
||||
|
||||
# eiger2_si_500k:
|
||||
# description: Eiger 2 Si 500K detector
|
||||
# deviceClass: csaxs_bec.devices.jungfraujoch.eiger2_si_500k.Eiger2Si500K
|
||||
# deviceConfig:
|
||||
# detector_distance: 1000
|
||||
# beam_center: [200, 200]
|
||||
# # Re-initialise the detector on staging if JFJ is in Error state (shutter must be closed)
|
||||
# auto_reinitialize: true
|
||||
# onFailure: raise
|
||||
# enabled: True
|
||||
# readoutPriority: async
|
||||
@@ -19,19 +21,19 @@
|
||||
# - bl_detectors
|
||||
# - eiger
|
||||
|
||||
eiger_1_5:
|
||||
description: Eiger 1.5M in-vacuum detector
|
||||
deviceClass: csaxs_bec.devices.jungfraujoch.eiger_1_5m.Eiger1_5M
|
||||
deviceConfig:
|
||||
detector_distance: 2200
|
||||
beam_center: [870, 1203]
|
||||
onFailure: raise
|
||||
enabled: True
|
||||
readoutPriority: async
|
||||
softwareTrigger: False
|
||||
deviceTags:
|
||||
- bl_detectors
|
||||
- eiger
|
||||
# eiger_1_5:
|
||||
# description: Eiger 1.5M in-vacuum detector
|
||||
# deviceClass: csaxs_bec.devices.jungfraujoch.eiger_1_5m.Eiger1_5M
|
||||
# deviceConfig:
|
||||
# detector_distance: 2200
|
||||
# beam_center: [870, 1203]
|
||||
# onFailure: raise
|
||||
# enabled: True
|
||||
# readoutPriority: async
|
||||
# softwareTrigger: False
|
||||
# deviceTags:
|
||||
# - bl_detectors
|
||||
# - eiger
|
||||
|
||||
# eiger_9:
|
||||
# description: Eiger 9M detector
|
||||
|
||||
@@ -25,14 +25,14 @@ detectors:
|
||||
#sastt:
|
||||
# - !include ./sastt.yaml
|
||||
|
||||
flomni:
|
||||
- !include ./ptycho_flomni.yaml
|
||||
# flomni:
|
||||
# - !include ./ptycho_flomni.yaml
|
||||
|
||||
#omny:
|
||||
# - !include ./ptycho_omny.yaml
|
||||
|
||||
#lamni:
|
||||
# - !include ./ptycho_lamni.yaml
|
||||
lamni:
|
||||
- !include ./ptycho_lamni.yaml
|
||||
|
||||
# user setup:
|
||||
# - !include ./user_setup.yaml
|
||||
|
||||
@@ -21,8 +21,9 @@ feyex:
|
||||
userParameter:
|
||||
in: -16.453
|
||||
out: -1
|
||||
fttrx_in: -2.3
|
||||
fttrx_out: -27
|
||||
fttrx_in: 11.025 #-2.3
|
||||
fttrx_out: -7 #-20
|
||||
#fttrx_out: -27
|
||||
deviceTags:
|
||||
- ptycho_flomni
|
||||
|
||||
@@ -94,14 +95,14 @@ foptx:
|
||||
# in: -14.191
|
||||
# out: -13.831
|
||||
#170 micron, 60 nm
|
||||
in: -13.8909
|
||||
out: -13.831
|
||||
#in: -13.8909
|
||||
#out: -13.831
|
||||
#250 micron, 30 nm, Abe structures
|
||||
# in: -13.8809375
|
||||
# out: -14.1809
|
||||
#250 micron, 30 nm, Tomas structures
|
||||
# in: -14.5490625
|
||||
# out: -14.1809
|
||||
in: -14.5490625
|
||||
out: -14.1809
|
||||
fzp_diameter: 170 # microns
|
||||
fzp_outermost_zone_width: 60 # nm
|
||||
detector_distance: 7196 # mm, sample-to-detector; unknown for now
|
||||
@@ -130,14 +131,14 @@ fopty:
|
||||
# in: 1.02
|
||||
# out: 0.869
|
||||
#170 micron, 60 nm
|
||||
in: 0.42
|
||||
out: 0.57
|
||||
#in: 0.42
|
||||
#out: 0.57
|
||||
#250 micron, 30 nm, Abe structures
|
||||
# in: 2.8299
|
||||
# out: 2.8299
|
||||
#250 micron, 30 nm, Tomas structures
|
||||
# in: 2.8419921875
|
||||
# out: 2.8419921875
|
||||
in: 2.8419921875
|
||||
out: 2.8419921875
|
||||
deviceTags:
|
||||
- ptycho_flomni
|
||||
|
||||
@@ -378,14 +379,14 @@ fosax:
|
||||
# in: 8.7568
|
||||
# out: 5.1
|
||||
#170 micron, 60 nm, 7.9 kev
|
||||
in: 8.718233
|
||||
out: 5.1
|
||||
#in: 8.718233
|
||||
#out: 5.1
|
||||
#250 micron, 30 nm, Abe structures
|
||||
# in: 8.7392
|
||||
# out: 5.1
|
||||
#250 micron, 30 nm, Tomas structures
|
||||
# in: 9.420798
|
||||
# out: 5.1
|
||||
in: 9.400897
|
||||
out: 5.1
|
||||
deviceTags:
|
||||
- ptycho_flomni
|
||||
|
||||
@@ -413,11 +414,11 @@ fosay:
|
||||
#170 micron, 60 nm, 7.6 kev
|
||||
#in: -0.0235
|
||||
#170 micron, 60 nm, 7.9 kev
|
||||
in: -0.062253
|
||||
#in: -0.062253
|
||||
#250 micron, 30 nm, Abe structures
|
||||
# in: -2.3684
|
||||
#250 micron, 30 nm, Tomas structures
|
||||
# in: -2.383993
|
||||
in: -2.401459
|
||||
deviceTags:
|
||||
- ptycho_flomni
|
||||
fosaz:
|
||||
@@ -444,14 +445,18 @@ fosaz:
|
||||
#in: 8.5
|
||||
#out: 6
|
||||
#170 micron, 60 nm, 7.9 kev, foptz 16.9, probe size 7.5 mu
|
||||
in: 14.1
|
||||
out: 4
|
||||
#in: 14.1
|
||||
#out: 4
|
||||
# micron, 30 nm, 7.9 kev, very close to the sample. make sure foptz is 32.02 or smaller //abe's fzp's
|
||||
# in: 0.5
|
||||
# out: -5
|
||||
in: 0.4
|
||||
out: -5
|
||||
deviceTags:
|
||||
- ptycho_flomni
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
############################################################
|
||||
#################### flOMNI RT motors ######################
|
||||
############################################################
|
||||
|
||||
@@ -19,7 +19,7 @@ leyex:
|
||||
readoutPriority: baseline
|
||||
connectionTimeout: 20
|
||||
userParameter:
|
||||
in: 14.117
|
||||
in: 13.18
|
||||
# flight tube positions used by leye_out()/leye_in() (draft, still commented there);
|
||||
# fill in and uncomment once the fttrz positions are known
|
||||
# fttrz_in:
|
||||
@@ -43,7 +43,7 @@ leyey:
|
||||
readoutPriority: baseline
|
||||
connectionTimeout: 20
|
||||
userParameter:
|
||||
in: 48.069
|
||||
in: 47.94
|
||||
out: 0.5
|
||||
deviceTags:
|
||||
- ptycho_lamni
|
||||
@@ -64,13 +64,21 @@ loptx:
|
||||
readoutPriority: baseline
|
||||
connectionTimeout: 20
|
||||
userParameter:
|
||||
in: -0.244
|
||||
out: -0.699
|
||||
in: -0.8698
|
||||
#out: -0.8665
|
||||
out: -1.07
|
||||
# 170 micron, 60 nm
|
||||
fzp_diameter: 170 # microns
|
||||
fzp_outermost_zone_width: 60 # nm
|
||||
fzp_diameter: 250 # microns
|
||||
fzp_outermost_zone_width: 30 # nm
|
||||
detector_distance: -1 # mm, sample-to-detector; unknown for now
|
||||
fzp_details: "manufacturing notes here" # free-text FZP manufacturing notes
|
||||
fzp_details: "30 nm Abe Tomas 05/2026" # free-text FZP manufacturing notes
|
||||
# in: -0.244
|
||||
# out: -0.699
|
||||
# # 170 micron, 60 nm
|
||||
# fzp_diameter: 170 # microns
|
||||
# fzp_outermost_zone_width: 60 # nm
|
||||
# detector_distance: -1 # mm, sample-to-detector; unknown for now
|
||||
# fzp_details: "manufacturing notes here" # free-text FZP manufacturing notes
|
||||
deviceTags:
|
||||
- ptycho_lamni
|
||||
lopty:
|
||||
@@ -90,8 +98,8 @@ lopty:
|
||||
readoutPriority: baseline
|
||||
connectionTimeout: 20
|
||||
userParameter:
|
||||
in: 3.724
|
||||
out: 3.53
|
||||
in: 2.6266
|
||||
out: 3.029
|
||||
deviceTags:
|
||||
- ptycho_lamni
|
||||
loptz:
|
||||
@@ -148,7 +156,7 @@ lsamx:
|
||||
readoutPriority: baseline
|
||||
connectionTimeout: 20
|
||||
userParameter:
|
||||
center: 8.768
|
||||
center: 9.68
|
||||
deviceTags:
|
||||
- ptycho_lamni
|
||||
lsamy:
|
||||
@@ -168,7 +176,7 @@ lsamy:
|
||||
readoutPriority: baseline
|
||||
connectionTimeout: 20
|
||||
userParameter:
|
||||
center: 10.041
|
||||
center: 9.47
|
||||
deviceTags:
|
||||
- ptycho_lamni
|
||||
|
||||
@@ -193,7 +201,7 @@ losax:
|
||||
readoutPriority: baseline
|
||||
connectionTimeout: 20
|
||||
userParameter:
|
||||
in: -1.442
|
||||
in: -1.582
|
||||
deviceTags:
|
||||
- ptycho_lamni
|
||||
losay:
|
||||
@@ -213,7 +221,7 @@ losay:
|
||||
readoutPriority: baseline
|
||||
connectionTimeout: 20
|
||||
userParameter:
|
||||
in: -0.171
|
||||
in: -0.131
|
||||
out: 3.8
|
||||
deviceTags:
|
||||
- ptycho_lamni
|
||||
@@ -239,6 +247,49 @@ losaz:
|
||||
deviceTags:
|
||||
- ptycho_lamni
|
||||
|
||||
lcsx:
|
||||
description: CS X
|
||||
deviceClass: csaxs_bec.devices.smaract.smaract_ophyd.SmaractMotor
|
||||
deviceConfig:
|
||||
axis_Id: D
|
||||
host: mpc2680.psi.ch
|
||||
limits:
|
||||
- 0
|
||||
- 0
|
||||
port: 8085
|
||||
sign: 1
|
||||
enabled: true
|
||||
onFailure: buffer
|
||||
readOnly: false
|
||||
readoutPriority: baseline
|
||||
connectionTimeout: 20
|
||||
userParameter:
|
||||
in: -1.831
|
||||
deviceTags:
|
||||
- ptycho_lamni
|
||||
|
||||
lcsy:
|
||||
description: CS Y
|
||||
deviceClass: csaxs_bec.devices.smaract.smaract_ophyd.SmaractMotor
|
||||
deviceConfig:
|
||||
axis_Id: E
|
||||
host: mpc2680.psi.ch
|
||||
limits:
|
||||
- 0
|
||||
- 0
|
||||
port: 8085
|
||||
sign: -1
|
||||
enabled: true
|
||||
onFailure: buffer
|
||||
readOnly: false
|
||||
readoutPriority: baseline
|
||||
connectionTimeout: 20
|
||||
userParameter:
|
||||
in: 1.0795
|
||||
out: 5
|
||||
deviceTags:
|
||||
- ptycho_lamni
|
||||
|
||||
############################################################
|
||||
#################### flOMNI RT motors ######################
|
||||
############################################################
|
||||
@@ -301,7 +352,7 @@ cam_xeye:
|
||||
deviceConfig:
|
||||
camera_id: 15
|
||||
bits_per_pixel: 24
|
||||
num_rotation_90: 3
|
||||
num_rotation_90: 1
|
||||
transpose: false
|
||||
force_monochrome: true
|
||||
m_n_colormode: 1
|
||||
@@ -310,7 +361,7 @@ cam_xeye:
|
||||
readOnly: false
|
||||
readoutPriority: async
|
||||
userParameter:
|
||||
pixel_calibration: 0.0009174311926605505 # mm/pixel (= 0.2 / 218)
|
||||
pixel_calibration: 0.00045872 # mm/pixel (= 0.2 / 218)
|
||||
deviceTags:
|
||||
- ptycho_lamni
|
||||
#################### GUI Signals ###########################
|
||||
|
||||
@@ -64,7 +64,10 @@ from ophyd import DeviceStatus
|
||||
from ophyd_devices import FileEventSignal, PreviewSignal
|
||||
from ophyd_devices.interfaces.base_classes.psi_device_base import PSIDeviceBase
|
||||
|
||||
from csaxs_bec.devices.jungfraujoch.jungfrau_joch_client import JungfrauJochClient
|
||||
from csaxs_bec.devices.jungfraujoch.jungfrau_joch_client import (
|
||||
JungfrauJochClient,
|
||||
JungfrauJochClientError,
|
||||
)
|
||||
from csaxs_bec.devices.jungfraujoch.jungfraujoch_preview import JungfrauJochPreview
|
||||
from csaxs_bec.devices.utils.utils import fetch_scan_info
|
||||
|
||||
@@ -121,6 +124,9 @@ class Eiger(PSIDeviceBase):
|
||||
device_manager (DeviceManagerDS): The device manager to use.
|
||||
raise_on_missing_packets (bool): Whether a 'missing packets' error from the
|
||||
JungfrauJoch broker fails the scan. See RAISE_ON_MISSING_PACKETS.
|
||||
auto_reinitialize (bool): Whether the detector is re-initialised on staging if
|
||||
JungfrauJoch is in ERROR state, instead of requiring a manual 'Initialize' in the
|
||||
JungfrauJoch WEB UI. Requires the shutter to be closed between scans.
|
||||
**kwargs: Additional keyword arguments.
|
||||
"""
|
||||
|
||||
@@ -151,12 +157,15 @@ class Eiger(PSIDeviceBase):
|
||||
scan_info: ScanInfo = None,
|
||||
readout_time: float = EIGER_READOUT_TIME,
|
||||
raise_on_missing_packets: bool = RAISE_ON_MISSING_PACKETS,
|
||||
auto_reinitialize: bool = False,
|
||||
dectris_url: str | None = None,
|
||||
device_manager=None,
|
||||
**kwargs,
|
||||
):
|
||||
super().__init__(name=name, scan_info=scan_info, device_manager=device_manager, **kwargs)
|
||||
self._host = f"{host}:{port}"
|
||||
self.jfj_client = JungfrauJochClient(host=self._host, parent=self)
|
||||
# dectris_url: base URL of the Dectris detector server, its status is logged on errors.
|
||||
self.jfj_client = JungfrauJochClient(host=self._host, parent=self, dectris_url=dectris_url)
|
||||
# NOTE: fetch this information from JungfrauJochClient during on_connected!
|
||||
self.jfj_preview_client = JungfrauJochPreview(
|
||||
url="tcp://129.129.95.114:5400", cb=self._preview_callback
|
||||
@@ -176,6 +185,7 @@ class Eiger(PSIDeviceBase):
|
||||
self.scan_parameters: ScanServerScanInfo | None = None
|
||||
self._raise_on_missing_packets = bool(raise_on_missing_packets)
|
||||
self._missing_packet_events = 0
|
||||
self._auto_reinitialize = bool(auto_reinitialize)
|
||||
|
||||
def get_raise_on_missing_packets(self) -> bool:
|
||||
"""
|
||||
@@ -239,6 +249,18 @@ class Eiger(PSIDeviceBase):
|
||||
return
|
||||
self.preview_image.put(frame)
|
||||
|
||||
# NOTE The device server applies every deviceConfig entry through update_config, which only
|
||||
# accepts keys that exist as attributes of the device. Hence this needs to be a property,
|
||||
# a constructor argument alone is rejected with 'Unknown config parameter'.
|
||||
# pylint: disable=missing-function-docstring
|
||||
@property
|
||||
def auto_reinitialize(self) -> bool:
|
||||
return self._auto_reinitialize
|
||||
|
||||
@auto_reinitialize.setter
|
||||
def auto_reinitialize(self, value: bool) -> None:
|
||||
self._auto_reinitialize = bool(value)
|
||||
|
||||
# pylint: disable=missing-function-docstring
|
||||
@property
|
||||
def detector_distance(self) -> float:
|
||||
@@ -343,7 +365,7 @@ class Eiger(PSIDeviceBase):
|
||||
"""
|
||||
start_time = time.time()
|
||||
logger.debug(f"On connected called for {self.name}")
|
||||
self.jfj_client.stop(request_timeout=3)
|
||||
self.jfj_client.stop(request_timeout=3, reason="on_connected")
|
||||
# Check which detector is selected
|
||||
|
||||
# Get available detectors
|
||||
@@ -415,8 +437,16 @@ class Eiger(PSIDeviceBase):
|
||||
logger.info(f"Device {self.name} on_stage called.")
|
||||
start_time = time.time()
|
||||
self.scan_parameters = fetch_scan_info(self.scan_info)
|
||||
if self._auto_reinitialize:
|
||||
try:
|
||||
self.jfj_client.recover_from_error()
|
||||
except JungfrauJochClientError as exc:
|
||||
raise EigerError(
|
||||
f"Device {self.name} is in ERROR state and could not be re-initialised "
|
||||
f"automatically, please initialise it in the WEB UI: {self._host}. {exc}"
|
||||
) from exc
|
||||
if self.jfj_client.detector_state != DetectorState.IDLE:
|
||||
self.jfj_client.stop(request_timeout=0.5)
|
||||
self.jfj_client.stop(request_timeout=0.5, reason="on_stage, detector not IDLE")
|
||||
self.jfj_client.wait_for_idle(timeout=5)
|
||||
|
||||
# TODO Reactivate
|
||||
@@ -536,6 +566,12 @@ class Eiger(PSIDeviceBase):
|
||||
# NOTE: This adjust the time (s) that should be waited for completion of the scan.
|
||||
timeout = self._wait_for_on_complete
|
||||
while time.time() - start_time < timeout:
|
||||
if self.jfj_client.detector_state == DetectorState.ERROR:
|
||||
self.jfj_client.log_dectris_status("ERROR state during acquisition")
|
||||
raise EigerError(
|
||||
f"Device {self.name} is in ERROR state during acquisition. Broker status: "
|
||||
f"{yaml.dump(self.jfj_client.jfj_status.to_dict(), indent=4)}"
|
||||
)
|
||||
if self.jfj_client.wait_for_idle(timeout=1, raise_on_timeout=False):
|
||||
# Call API endpoints to get broker status
|
||||
broker_status = self.jfj_client.jfj_status
|
||||
@@ -605,7 +641,8 @@ class Eiger(PSIDeviceBase):
|
||||
|
||||
def on_stop(self) -> None:
|
||||
"""Called when the device is stopped."""
|
||||
self.jfj_client.stop(request_timeout=0.5)
|
||||
logger.info(f"Device {self.name} on_stop called, cancelling JungfrauJoch acquisition.")
|
||||
self.jfj_client.stop(request_timeout=0.5, reason="on_stop")
|
||||
self.task_handler.shutdown()
|
||||
|
||||
def on_destroy(self):
|
||||
|
||||
@@ -18,6 +18,8 @@ from csaxs_bec.devices.jungfraujoch.eiger import RAISE_ON_MISSING_PACKETS, Eiger
|
||||
# the largest value among the detectors in a scan, or the slowest one overruns.
|
||||
EIGER2_SI_500K_READOUT_TIME = 2e-4 # s -- placeholder until a measured value exists
|
||||
DETECTOR_NAME = "Dectris EIGER2 Si 500K"
|
||||
# Detector server (Simplon API). Its status is logged when the detector goes into error state.
|
||||
DECTRIS_URL = "http://129.129.122.159"
|
||||
|
||||
|
||||
if TYPE_CHECKING: # pragma no cover
|
||||
@@ -47,11 +49,13 @@ class Eiger2Si500K(Eiger):
|
||||
device_manager: DeviceManagerDS = None,
|
||||
readout_time: float = EIGER2_SI_500K_READOUT_TIME,
|
||||
raise_on_missing_packets: bool = RAISE_ON_MISSING_PACKETS,
|
||||
dectris_url: str | None = DECTRIS_URL,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
super().__init__(
|
||||
name=name,
|
||||
detector_name=DETECTOR_NAME,
|
||||
dectris_url=dectris_url,
|
||||
readout_time=readout_time,
|
||||
raise_on_missing_packets=raise_on_missing_packets,
|
||||
detector_distance=detector_distance,
|
||||
|
||||
@@ -3,9 +3,11 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import enum
|
||||
import json
|
||||
import threading
|
||||
import time
|
||||
import traceback
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import requests
|
||||
@@ -14,6 +16,7 @@ from bec_lib.logger import bec_logger
|
||||
from jfjoch_client.api.default_api import DefaultApi
|
||||
from jfjoch_client.api_client import ApiClient
|
||||
from jfjoch_client.configuration import Configuration
|
||||
from jfjoch_client.exceptions import ServiceException
|
||||
from jfjoch_client.models.broker_status import BrokerStatus
|
||||
from jfjoch_client.models.dataset_settings import DatasetSettings
|
||||
from jfjoch_client.models.detector_settings import DetectorSettings
|
||||
@@ -23,6 +26,33 @@ logger = bec_logger.logger
|
||||
if TYPE_CHECKING: # pragma: no cover
|
||||
from ophyd import Device
|
||||
|
||||
# Keys of the Dectris detector server (Simplon API 1.8.0) endpoint detector/api/1.8.0/status/<key>
|
||||
DECTRIS_DETECTOR_STATUS_KEYS = (
|
||||
"state",
|
||||
"series_unique_id",
|
||||
"time",
|
||||
"temperature",
|
||||
"humidity",
|
||||
"board_000/th0_temp",
|
||||
"board_000/th0_humidity",
|
||||
"link_0",
|
||||
"link_1",
|
||||
"link_2",
|
||||
"link_3",
|
||||
)
|
||||
|
||||
|
||||
def _simplify_dectris_status(data):
|
||||
"""
|
||||
Reduce a Simplon status response to its values. A single entry {"value": x, ...} becomes x,
|
||||
a collection {"name": {"value": x, ...}, ...} becomes {"name": x, ...}.
|
||||
"""
|
||||
if isinstance(data, dict):
|
||||
if "value" in data:
|
||||
return data["value"]
|
||||
return {key: _simplify_dectris_status(val) for key, val in data.items()}
|
||||
return data
|
||||
|
||||
|
||||
# pylint: disable=raise-missing-from
|
||||
# pylint: disable=broad-except
|
||||
@@ -54,12 +84,21 @@ class JungfrauJochClient:
|
||||
host (str): Hostname of the Jungfrau Joch broker service.
|
||||
Default is "http://sls-jfjoch-001:8080"
|
||||
parent (Device, optional): Parent ophyd device, used for logging purposes.
|
||||
dectris_url (str, optional): Base URL of the Dectris detector server (Simplon API), e.g.
|
||||
"http://129.129.122.159". If set, its read-only status endpoints are logged
|
||||
by log_dectris_status for debugging detector errors. Default is None (off).
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self, host: str = "http://sls-jfjoch-001:8080", parent: Device | None = None
|
||||
self,
|
||||
host: str = "http://sls-jfjoch-001:8080",
|
||||
parent: Device | None = None,
|
||||
dectris_url: str | None = None,
|
||||
) -> None:
|
||||
self._dectris_url = dectris_url.rstrip("/") if dectris_url else None
|
||||
self._initialised = False
|
||||
self._recover_lock = threading.Lock()
|
||||
self._recoveries = 0
|
||||
configuration = Configuration(host=host)
|
||||
api_client = ApiClient(configuration)
|
||||
self.api = DefaultApi(api_client)
|
||||
@@ -97,10 +136,153 @@ class JungfrauJochClient:
|
||||
status = self.detector_state
|
||||
# TODO: #135 Check if the detector has to be in INACTIVE state before initialisation
|
||||
if status != DetectorState.IDLE:
|
||||
self.api.initialize_post()
|
||||
self.wait_for_idle(timeout)
|
||||
self._initialize(timeout)
|
||||
self.initialised = True
|
||||
|
||||
def _initialize(self, timeout: float) -> None:
|
||||
"""
|
||||
Trigger the initialisation of the detector and wait until it is done. The X-ray shutter
|
||||
must be closed, and the call can take up to a few minutes depending on the detector.
|
||||
|
||||
Args:
|
||||
timeout (float): Timeout in seconds to wait for the initialisation to finish.
|
||||
"""
|
||||
self.api.initialize_post()
|
||||
self.wait_for_idle(timeout)
|
||||
|
||||
def recover_from_error(self, timeout: float = 60) -> bool:
|
||||
"""
|
||||
Re-initialise the detector if JungfrauJoch is in ERROR state, equivalent to pressing
|
||||
'Initialize' in the JungfrauJoch WEB UI. Must only be called between acquisitions, as
|
||||
the initialisation requires a closed shutter and is not safe during a measurement.
|
||||
Only a single attempt is made per call.
|
||||
|
||||
Args:
|
||||
timeout (float): Timeout in seconds to wait for the detector to become IDLE again.
|
||||
|
||||
Returns:
|
||||
bool: True if the detector was re-initialised, False if it was not in ERROR state.
|
||||
|
||||
Raises:
|
||||
JungfrauJochClientError: If the detector is not IDLE after the re-initialisation.
|
||||
"""
|
||||
with self._recover_lock:
|
||||
if self.detector_state != DetectorState.ERROR:
|
||||
return False
|
||||
self._recoveries += 1
|
||||
logger.warning(
|
||||
f"JungfrauJoch backend for device {self._parent_name} is in ERROR state, "
|
||||
f"re-initialising the detector (recovery #{self._recoveries} since startup)."
|
||||
)
|
||||
# Capture the detector server state before the re-initialisation wipes it.
|
||||
dectris_status = self.log_dectris_status(
|
||||
f"before re-init (recovery #{self._recoveries})"
|
||||
)
|
||||
try:
|
||||
self._initialize(timeout)
|
||||
except Exception as exc:
|
||||
hint = ""
|
||||
series = self._dectris_still_armed(dectris_status)
|
||||
if series is not None:
|
||||
hint = (
|
||||
f" Dectris detector server is still armed (state ready, series {series}) "
|
||||
"from a previous acquisition that was not cancelled. JungfrauJoch cannot "
|
||||
"re-initialise until it is disarmed. Disarm it on the Dectris server, or "
|
||||
"re-seat the detector fibres."
|
||||
)
|
||||
raise JungfrauJochClientError(
|
||||
f"Error on device {self._parent_name}: re-initialisation after detector "
|
||||
f"error failed: {exc}{hint}"
|
||||
) from exc
|
||||
state = self.detector_state
|
||||
if state != DetectorState.IDLE:
|
||||
raise JungfrauJochClientError(
|
||||
f"Error on device {self._parent_name}: detector is in state {state.value} "
|
||||
"after re-initialisation, expected IDLE."
|
||||
)
|
||||
logger.info(f"Device {self._parent_name} re-initialised and back in IDLE state.")
|
||||
return True
|
||||
|
||||
def log_dectris_status(self, reason: str, timeout: float = 2) -> dict | None:
|
||||
"""
|
||||
Log the status endpoints of the Dectris detector server (Simplon API) as a single warning
|
||||
line of compact JSON. Only read-only GET requests are made. Does nothing if no dectris_url
|
||||
was configured. This never raises and takes at most roughly the timeout, it is meant to be
|
||||
called while handling a detector error.
|
||||
|
||||
Args:
|
||||
reason (str): Why the status is logged, included in the log line.
|
||||
timeout (float): Timeout in seconds of each HTTP request. Requests run in parallel.
|
||||
|
||||
Returns:
|
||||
dict | None: The logged status values, None if disabled or the status could not be read.
|
||||
"""
|
||||
if self._dectris_url is None:
|
||||
return None
|
||||
try:
|
||||
base = self._dectris_url
|
||||
urls = {
|
||||
f"detector/{key}": f"{base}/detector/api/1.8.0/status/{key}"
|
||||
for key in DECTRIS_DETECTOR_STATUS_KEYS
|
||||
}
|
||||
urls["detector/error"] = f"{base}/detector/api/1.8.0/status/error"
|
||||
for component in ("stream", "monitor", "filewriter"):
|
||||
urls[component] = f"{base}/{component}/api/1.8.0/status/"
|
||||
|
||||
def _get(url: str):
|
||||
try:
|
||||
response = requests.get(url, timeout=timeout)
|
||||
response.raise_for_status()
|
||||
return _simplify_dectris_status(response.json())
|
||||
except Exception as exc:
|
||||
return f"<{type(exc).__name__}: {str(exc)[:120]}>"
|
||||
|
||||
with ThreadPoolExecutor(max_workers=len(urls)) as pool:
|
||||
results = dict(zip(urls, pool.map(_get, urls.values())))
|
||||
logger.warning(
|
||||
f"Dectris detector server status for device {self._parent_name} ({reason}): "
|
||||
f"{json.dumps(results, separators=(',', ':'), default=str)}"
|
||||
)
|
||||
return results
|
||||
except Exception as exc:
|
||||
logger.warning(f"Could not log Dectris detector server status ({reason}): {exc}")
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def _dectris_still_armed(results: dict | None) -> str | None:
|
||||
"""
|
||||
Check a status returned by log_dectris_status for a detector server that is still armed,
|
||||
i.e. a series was started but never finished or cancelled. JungfrauJoch then refuses to
|
||||
initialise with "Detector busy".
|
||||
|
||||
Returns:
|
||||
str | None: The series id of the armed series ("unknown" if not available), None if
|
||||
the detector is not armed or the status is not available.
|
||||
"""
|
||||
if not isinstance(results, dict):
|
||||
return None
|
||||
stream = results.get("stream")
|
||||
stream_state = stream.get("state") if isinstance(stream, dict) else None
|
||||
if results.get("detector/state") != "ready" and stream_state != "acquire":
|
||||
return None
|
||||
series = results.get("detector/series_unique_id")
|
||||
return series if isinstance(series, str) and not series.startswith("<") else "unknown"
|
||||
|
||||
@staticmethod
|
||||
def _api_error_message(exc: Exception) -> str | None:
|
||||
"""
|
||||
Extract the message of a JungfrauJoch HTTP 500 error. The response body is a plain
|
||||
string that jfjoch_client fails to parse into its ErrorMessage model (pydantic
|
||||
ValidationError), so the message is read from the raw body of the ServiceException.
|
||||
"""
|
||||
if not isinstance(exc, ServiceException):
|
||||
return None
|
||||
try:
|
||||
body = json.loads(exc.body)
|
||||
return f"{body['msg']} ({body['reason']})"
|
||||
except Exception:
|
||||
return exc.body or exc.reason
|
||||
|
||||
def set_detector_settings(self, settings: dict | DetectorSettings, timeout: int = 10) -> None:
|
||||
"""
|
||||
Set the detector settings. The state of JungfrauJoch must be in IDLE,
|
||||
@@ -190,12 +372,21 @@ class JungfrauJochClient:
|
||||
f"{yaml.dump(settings, indent=4)}. \nTraceback: {content}."
|
||||
)
|
||||
|
||||
def stop(self, request_timeout: float = 0.5) -> None:
|
||||
"""Stop the acquisition, this only logs errors and is not raising."""
|
||||
def stop(self, request_timeout: float = 0.5, reason: str = "") -> None:
|
||||
"""
|
||||
Stop the acquisition, this only logs errors and is not raising. After the cancel, the
|
||||
Dectris detector server status is logged (if configured) to show whether it disarmed.
|
||||
|
||||
Args:
|
||||
request_timeout (float): Timeout in seconds for the cancel request.
|
||||
reason (str): Why the acquisition is stopped, included in the log lines.
|
||||
"""
|
||||
|
||||
def _stop_call(self):
|
||||
try:
|
||||
self.api.cancel_post_with_http_info() # (_request_timeout=request_timeout)
|
||||
logger.info(f"Cancel sent to JungfrauJoch for device {self._parent_name} ({reason}).")
|
||||
self.log_dectris_status(f"after cancel ({reason})")
|
||||
except requests.exceptions.Timeout:
|
||||
content = traceback.format_exc()
|
||||
logger.error(
|
||||
@@ -237,12 +428,15 @@ class JungfrauJochClient:
|
||||
return False
|
||||
except Exception as exc:
|
||||
content = traceback.format_exc()
|
||||
message = self._api_error_message(exc)
|
||||
logger.info(
|
||||
f"Error on device {self._parent_name} in wait_for_idle. Full traceback: {content}"
|
||||
f"Error on device {self._parent_name} in wait_for_idle. "
|
||||
f"{message or ''} Full traceback: {content}"
|
||||
)
|
||||
if raise_on_timeout:
|
||||
raise JungfrauJochClientError(
|
||||
f"Error on device {self._parent_name} in wait_for_idle: {content}"
|
||||
f"Error on device {self._parent_name} in wait_for_idle: "
|
||||
f"{message or content}"
|
||||
) from exc
|
||||
return False
|
||||
return True
|
||||
@@ -53,6 +53,7 @@ class RtLamniController(Controller):
|
||||
"show_signal_strength_interferometer",
|
||||
"show_analog_signals",
|
||||
"show_feedback_status",
|
||||
"show_interferometer_positions",
|
||||
]
|
||||
|
||||
def __init__(
|
||||
@@ -293,7 +294,7 @@ class RtLamniController(Controller):
|
||||
|
||||
def show_interferometer_positions(self) -> dict:
|
||||
return_table = (self.socket_put_and_receive("J4")).split(",")
|
||||
loop_status = bool(int(float(return_table[0])))
|
||||
loop_status = self.feedback_is_running()
|
||||
pos_y = float(return_table[1])
|
||||
pos_x = float(return_table[2])
|
||||
|
||||
|
||||
@@ -5,13 +5,32 @@ import numpy as np
|
||||
from ophyd import Device, Component as Cpt, Kind, Signal
|
||||
from ophyd_devices import PreviewSignal
|
||||
import traceback
|
||||
from urllib.parse import urlsplit
|
||||
|
||||
from bec_lib.logger import bec_logger
|
||||
|
||||
logger = bec_logger.logger
|
||||
|
||||
# The camera server passes "exposure" straight to V4L2 exposure_time_absolute,
|
||||
# which is in units of 100 us.
|
||||
EXPOSURE_UNIT_MS = 0.1
|
||||
# V4L2 auto_exposure menu values used by the camera server.
|
||||
AUTO_EXPOSURE_ON = 3 # aperture priority
|
||||
AUTO_EXPOSURE_OFF = 1 # manual
|
||||
SETTINGS_TIMEOUT_S = 3
|
||||
|
||||
|
||||
class WebcamViewer(Device):
|
||||
USER_ACCESS = ["start_live_mode", "stop_live_mode"]
|
||||
USER_ACCESS = [
|
||||
"start_live_mode",
|
||||
"stop_live_mode",
|
||||
"get_exposure_time",
|
||||
"set_exposure_time",
|
||||
"set_auto_exposure_enabled",
|
||||
"set_gain",
|
||||
"set_brightness",
|
||||
"refresh_settings",
|
||||
]
|
||||
preview = Cpt(PreviewSignal, ndim=2, num_rotation_90=0, transpose=False)
|
||||
live_mode_enabled = Cpt(
|
||||
Signal,
|
||||
@@ -20,6 +39,24 @@ class WebcamViewer(Device):
|
||||
doc="Enable or disable live mode.",
|
||||
kind=Kind.config,
|
||||
)
|
||||
exposure_time = Cpt(
|
||||
Signal,
|
||||
name="exposure_time",
|
||||
value=0.0,
|
||||
doc="Camera exposure time (ms). Only effective with auto exposure disabled.",
|
||||
kind=Kind.config,
|
||||
)
|
||||
auto_exposure_enabled = Cpt(
|
||||
Signal,
|
||||
name="auto_exposure_enabled",
|
||||
value=True,
|
||||
doc="Enable/disable camera auto exposure.",
|
||||
kind=Kind.config,
|
||||
)
|
||||
gain = Cpt(Signal, name="gain", value=0.0, doc="Camera gain.", kind=Kind.config)
|
||||
brightness = Cpt(
|
||||
Signal, name="brightness", value=0.0, doc="Camera brightness.", kind=Kind.config
|
||||
)
|
||||
|
||||
def __init__(self, url:str, name:str, num_rotation_90=0, transpose=False, **kwargs) -> None:
|
||||
super().__init__(name=name, **kwargs)
|
||||
@@ -31,7 +68,17 @@ class WebcamViewer(Device):
|
||||
self._live_mode_lock = threading.RLock()
|
||||
self.preview.num_rotation_90 = num_rotation_90
|
||||
self.preview.transpose = transpose
|
||||
parts = urlsplit(url)
|
||||
self._settings_url = f"{parts.scheme}://{parts.netloc}/camera/settings"
|
||||
self._refreshing = False
|
||||
self.live_mode_enabled.subscribe(self._on_live_mode_enabled_changed, run=False)
|
||||
self.exposure_time.subscribe(self._on_exposure_time_changed, run=False)
|
||||
self.auto_exposure_enabled.subscribe(self._on_auto_exposure_enabled_changed, run=False)
|
||||
self.gain.subscribe(self._on_gain_changed, run=False)
|
||||
self.brightness.subscribe(self._on_brightness_changed, run=False)
|
||||
# Load the camera's current settings in the background, so an unreachable
|
||||
# camera server cannot block the device from loading.
|
||||
threading.Thread(target=self._initial_refresh, daemon=True).start()
|
||||
|
||||
def start_live_mode(self) -> None:
|
||||
self.live_mode_enabled.put(True)
|
||||
@@ -39,6 +86,93 @@ class WebcamViewer(Device):
|
||||
def stop_live_mode(self) -> None:
|
||||
self.live_mode_enabled.put(False)
|
||||
|
||||
# ---- camera settings (HTTP /camera/settings on the camera server) ----
|
||||
|
||||
def _post_settings(self, payload: dict) -> None:
|
||||
"""POST settings to the camera server; log instead of raising on failure."""
|
||||
try:
|
||||
response = requests.post(
|
||||
self._settings_url, json=payload, timeout=SETTINGS_TIMEOUT_S
|
||||
)
|
||||
response.raise_for_status()
|
||||
applied = response.json().get("applied", {})
|
||||
bad = {k: v for k, v in applied.items() if isinstance(v, str)}
|
||||
if bad:
|
||||
logger.error(f"{self.name}: camera server rejected settings {bad}")
|
||||
except Exception: # pylint: disable=broad-except
|
||||
logger.error(
|
||||
f"{self.name}: failed to apply {payload} at {self._settings_url}: "
|
||||
f"{traceback.format_exc()}"
|
||||
)
|
||||
|
||||
def _on_exposure_time_changed(self, *args, value, **kwargs) -> None:
|
||||
if self._refreshing:
|
||||
return
|
||||
payload = {"exposure": float(value) / EXPOSURE_UNIT_MS}
|
||||
if self.auto_exposure_enabled.get():
|
||||
logger.warning(
|
||||
f"{self.name}: exposure_time set while auto exposure is enabled; "
|
||||
"it has no effect until set_auto_exposure_enabled(False)."
|
||||
)
|
||||
self._post_settings(payload)
|
||||
|
||||
def _on_auto_exposure_enabled_changed(self, *args, value, **kwargs) -> None:
|
||||
if self._refreshing:
|
||||
return
|
||||
self._post_settings({"auto_exposure": AUTO_EXPOSURE_ON if value else AUTO_EXPOSURE_OFF})
|
||||
|
||||
def _on_gain_changed(self, *args, value, **kwargs) -> None:
|
||||
if not self._refreshing:
|
||||
self._post_settings({"gain": float(value)})
|
||||
|
||||
def _on_brightness_changed(self, *args, value, **kwargs) -> None:
|
||||
if not self._refreshing:
|
||||
self._post_settings({"brightness": float(value)})
|
||||
|
||||
def get_exposure_time(self) -> float:
|
||||
"""Get the cached exposure time (ms). Use refresh_settings() to re-read the camera."""
|
||||
return float(self.exposure_time.get())
|
||||
|
||||
def set_exposure_time(self, value: float) -> None:
|
||||
"""Set the exposure time (ms). Disables auto exposure first, otherwise the camera ignores it."""
|
||||
if self.auto_exposure_enabled.get():
|
||||
self.auto_exposure_enabled.put(False)
|
||||
self.exposure_time.put(value)
|
||||
|
||||
def set_auto_exposure_enabled(self, enable: bool) -> None:
|
||||
self.auto_exposure_enabled.put(bool(enable))
|
||||
|
||||
def set_gain(self, value: float) -> None:
|
||||
self.gain.put(value)
|
||||
|
||||
def set_brightness(self, value: float) -> None:
|
||||
self.brightness.put(value)
|
||||
|
||||
def _initial_refresh(self) -> None:
|
||||
try:
|
||||
self.refresh_settings()
|
||||
except Exception as exc: # pylint: disable=broad-except
|
||||
logger.warning(f"{self.name}: could not read camera settings from {self._settings_url}: {exc}")
|
||||
|
||||
def refresh_settings(self) -> dict:
|
||||
"""Read the current settings from the camera server into the Signals."""
|
||||
response = requests.get(self._settings_url, timeout=SETTINGS_TIMEOUT_S)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
self._refreshing = True
|
||||
try:
|
||||
if "exposure" in data:
|
||||
self.exposure_time.put(float(data["exposure"]) * EXPOSURE_UNIT_MS)
|
||||
if "auto_exposure" in data:
|
||||
self.auto_exposure_enabled.put(float(data["auto_exposure"]) >= 2.0)
|
||||
if "gain" in data:
|
||||
self.gain.put(float(data["gain"]))
|
||||
if "brightness" in data:
|
||||
self.brightness.put(float(data["brightness"]))
|
||||
finally:
|
||||
self._refreshing = False
|
||||
return data
|
||||
|
||||
def _on_live_mode_enabled_changed(self, *args, value, **kwargs) -> None:
|
||||
self._apply_live_mode(bool(value))
|
||||
|
||||
|
||||
@@ -87,6 +87,13 @@ class LamNIComponents(ScanComponents):
|
||||
|
||||
logger.info(f"Current uncompensated drift of setup is x={x_drift:.3f}, y={y_drift:.3f}")
|
||||
|
||||
# a drift this large is not trusted (e.g. interferometer reset or error);
|
||||
# drop it so the second iteration does not apply it either
|
||||
if np.abs(y_drift) > 150 or np.abs(x_drift) > 150:
|
||||
logger.info("Drift exceeds 150 microns, ignoring it.")
|
||||
x_drift = 0.0
|
||||
y_drift = 0.0
|
||||
|
||||
move_x = (
|
||||
x_stage + lsamx_center + self.lamni_to_stage_coordinates(x_drift, y_drift)[0] / 1000
|
||||
)
|
||||
@@ -100,11 +107,7 @@ class LamNIComponents(ScanComponents):
|
||||
self._dev.lsamx.read_only = False
|
||||
self._dev.lsamy.read_only = False
|
||||
|
||||
if (
|
||||
np.abs(y_drift) > 150
|
||||
or np.abs(x_drift) > 150
|
||||
or (coarse_move_req_y < 0.003 and coarse_move_req_x < 0.003)
|
||||
):
|
||||
if coarse_move_req_y < 0.003 and coarse_move_req_x < 0.003:
|
||||
logger.info("No drift correction.")
|
||||
else:
|
||||
logger.info(
|
||||
|
||||
@@ -39,7 +39,7 @@ Conventions for writing these MyST/Sphinx docs and how changes go live on Read t
|
||||
|
||||
## LamNI smear aid architecture
|
||||
|
||||
Device/ipython-client/GUI widget interfaces for the smear rotation-center calibration aid, and the `bw-generate-cli` regeneration pitfall.
|
||||
Device/ipython-client/GUI widget interfaces for the smear rotation-center calibration aid, the min/max composite with opaque-pixel masking, and the `bw-generate-cli` regeneration pitfall.
|
||||
```
|
||||
|
||||
````
|
||||
@@ -123,9 +123,26 @@ stepping the motor:
|
||||
finish.
|
||||
2. A plain loop polls `report.status` (exactly what `ScanReport.wait()` does
|
||||
internally) and, while it isn't `"COMPLETED"`, grabs a frame via
|
||||
`get_last_image()` and folds it into a running `np.maximum` composite —
|
||||
`get_last_image()` and folds it into a running composite —
|
||||
all single-threaded, since the rotation is progressing concurrently on
|
||||
the server regardless of what the client does between polls.
|
||||
the server regardless of what the client does between polls. The
|
||||
composite is `np.minimum` for `projection="min"` (default; absorption
|
||||
contrast, dark features on a bright background) or `np.maximum` for
|
||||
`projection="max"` (bright features on a dark background). A
|
||||
max-projection is useless for absorption: the bright background passes
|
||||
every pixel at some point during the rotation and washes the dark
|
||||
features out. Taking the minimum is equivalent to inverting the frames,
|
||||
taking the maximum and inverting back, without needing the camera's
|
||||
full-scale value. The accumulation lives in `SmearComposite`
|
||||
(`x_ray_eye_align.py`). For `"min"` it also skips opaque pixels: in each
|
||||
frame, pixels darker than `opaque_level` (default 0.25) times the frame's
|
||||
99th-percentile value count as opaque and are left out, so the sample
|
||||
holder rotating through the view does not black out every pixel it ever
|
||||
covers. It keeps the plain minimum alongside the masked one and falls
|
||||
back to it for pixels that were opaque in every frame; `opaque_level=0`
|
||||
is the plain minimum. Both values are validated up front (`ValueError`
|
||||
before any motion) and stored as `projection`/`opaque_level` attributes
|
||||
in the calibration HDF5.
|
||||
3. Every `display_update_interval_s`, the current composite is pushed to
|
||||
`smear_preview` so the operator watches it grow; off-axis features trace
|
||||
arcs as the sample turns, and the common center of curvature of those
|
||||
|
||||
@@ -76,7 +76,7 @@ _To bypass the fine alignment: `flomni.feye_out()`_
|
||||
### Tomographic Measurement
|
||||
|
||||
Now that the sample is aligned, the tomographic measurement can be performed.
|
||||
1. `flomni.tomo_parameters()` opens the tomo parameter window (GUI) to adjust the scan parameters for the tomographic scan (`flomni.tomo_parameters_cli()` is the command line version, which prompts for each value in the terminal). This includes the parameters for ptychographic scans of projections plus the strategy for angular sampling. The vertical shift adjusts the field of view, up (positive) or down (negative). After adjusting the numbers, type again `flomni.tomo_parameters()` and verify that they are correct. For the "8 sub-tomograms" mode, an optional 0-deg reference shot can be enabled at the start of every odd sub-tomogram and once more at the end, useful for tracking radiation damage over the course of a long measurement.
|
||||
1. `flomni.tomo_parameters()` opens the tomo parameter window (GUI) to adjust the scan parameters for the tomographic scan (`flomni.tomo_parameters_cli()` is the command line version, which prompts for each value in the terminal). This includes the parameters for ptychographic scans of projections plus the strategy for angular sampling. The vertical shift adjusts the field of view, up (positive) or down (negative). After adjusting the numbers, type again `flomni.tomo_parameters()` and verify that they are correct. Besides the computed angular sampling modes, `tomo_type` 4 reads the angles from a text file, see [Angles from a file](user.ptychography.flomni.angle_file) for the file requirements. For the "8 sub-tomograms" mode, an optional 0-deg reference shot can be enabled at the start of every odd sub-tomogram and once more at the end, useful for tracking radiation damage over the course of a long measurement.
|
||||
1. `flomni.tomo_scan_projection(angle)` perform a ptychographic scan at the rotation angle <angle>. Launch the tomographic measurement by `flomni.tomo_scan()`. During the scan a live progress report is printed, including an estimated time of completion. Unusually long gaps (e.g. a beamline interruption) are detected automatically and excluded from the estimate, and the total time lost to such gaps is reported once the scan finishes.
|
||||
1. Before changing sample, verify that all subtomograms were completely acquired using the tomo_reconstruction matlab script.
|
||||
|
||||
@@ -370,21 +370,89 @@ Once satisfied with the alignment, the tomography scan can be started by
|
||||
|
||||
During the scan, a live progress report is printed (subtomogram/projection counters and an estimated time of completion). Gaps significantly longer than a normal acquisition cycle (e.g. a beamline-down interruption, or a crash followed by a restart) are detected automatically from the time between consecutive projections and excluded from the time estimate; the total time lost to such gaps is printed once the scan finishes.
|
||||
|
||||
Three modes for angular sampling are implemented and they have different optional parameters for the tomo_scan method:
|
||||
| tomography mode | parameters and defaults |
|
||||
| --- | --- |
|
||||
| 8 sub-tomograms | subtomo_start=1, start_angle=None |
|
||||
| Golden ratio tomography (sorted in bunches) | projection_number=None |
|
||||
| Equally spaced with golden starting angle | projection_number=None |
|
||||
Four modes for angular sampling are implemented (selected by `tomo_type` 1-4) and they have different optional parameters for the tomo_scan method:
|
||||
| tomography mode | tomo_type | parameters and defaults |
|
||||
| --- | --- | --- |
|
||||
| 8 sub-tomograms | 1 | subtomo_start=1, start_angle=None |
|
||||
| Golden ratio tomography (sorted in bunches) | 2 | projection_number=None |
|
||||
| Equally spaced with golden starting angle | 3 | projection_number=None |
|
||||
| Angles read from a file | 4 | projection_number=None |
|
||||
|
||||
See [Angles from a file](user.ptychography.flomni.angle_file) below for tomo_type 4.
|
||||
|
||||
For the "8 sub-tomograms" mode, `flomni.tomo_parameters()` also offers a `zero_deg_reference_at_each_subtomo` option: when enabled, an additional reference projection at exactly 0 degrees is acquired at the start of every odd (forward) sub-tomogram and once more after the final sub-tomogram, useful for tracking radiation damage across the full measurement.
|
||||
|
||||
By default odd sub-tomograms sweep 0→180° and even ones 180→0°, so the reference is taken before every second sub-tomogram. With the `tomo_type1_always_forward` option ("Run all sub-tomos forward", 180° range only), every sub-tomogram sweeps 0→180°; the rotation returns to 0° between sub-tomograms and a 0° reference is taken before each of the 8 (plus one at the end). The angle set of every sub-tomogram, and thus the combined angular grid, is unchanged.
|
||||
|
||||
`flomni.tomo_parameters()` also offers `stitch_x`/`stitch_y` (integers, default 0, up to 50 each) for a field of view larger than a single Fermat scan can cover: when nonzero, every projection angle is acquired as a `(2*stitch_x+1) × (2*stitch_y+1)` grid of separate Fermat scans instead of a single one, tiled around the normal alignment-corrected center and spaced by `fovx`/`fovy` minus `tomo_stitch_overlap` (µm, default 0.2) per step in x/y — stitching together an effective field of view of roughly `(2*stitch_x+1) × fovx` by `(2*stitch_y+1) × fovy`, with `tomo_stitch_overlap` controlling the deliberate overlap between adjacent tiles used for aligning them together during reconstruction. Stitching is forced to 0 (disabled) whenever `single_point_instead_of_fermat_scan` is enabled.
|
||||
|
||||
The parameters above can be used to __restart an interrupted acquisition__ manually, or - more conveniently - by running
|
||||
`flomni.tomo_scan_resume()`
|
||||
which reads the last recorded progress and resumes automatically at the exact point (subtomogram/angle, or projection for the golden ratio modes) the scan was interrupted at, without needing to look up the values by hand. When running from the tomo scan queuing system use `flomni.tomo_queue_execute()` instead!
|
||||
|
||||
(user.ptychography.flomni.angle_file)=
|
||||
#### Angles from a file (tomo_type 4)
|
||||
|
||||
With `tomo_type` 4 the projection angles are not computed but read from a plain-text file you provide, e.g. for a non-uniform sampling, a missing-wedge-aware set of angles, or to repeat a previously used list. Set the file in `flomni.tomo_parameters()` (choose type 4, then **Browse...** to pick the file; the GUI shows a live preview of the file content and blocks Submit / Add to queue while the file is invalid), or in `flomni.tomo_parameters_cli()` which prompts for the path until it parses. Only the path is stored (`flomni.tomo_angle_file`), so use a path that is readable from the BEC client machine.
|
||||
|
||||
**File requirements**
|
||||
- Plain text, one projection per line.
|
||||
- Column 1: the angle in degrees, `0 <= angle <= 360`. Angles above 180 are allowed; `tomo_angle_range` is not applied.
|
||||
- Column 2 (optional): the sub-tomogram number, an integer `>= 1`. If omitted, it is 1. It is only a label used for progress display and the saved metadata; it does not change the acquisition.
|
||||
- Columns are separated by whitespace and/or commas. Everything after `#` is a comment; blank lines are ignored.
|
||||
- Projections are acquired in file order, so the order in the file is the order of the measurement.
|
||||
- Further, optional `key=value` tokens after the angle/subtomo may override acquisition parameters for that projection -- see [Per-line acquisition overrides](user.ptychography.flomni.angle_file.overrides) below.
|
||||
- At least one angle is required. Any malformed line (not a number, angle outside 0-360, sub-tomogram number below 1, an unrecognized or malformed `key=value` token) makes the whole file invalid and names the offending line.
|
||||
- A line that is `END` (case-insensitive, nothing but whitespace/comment around it) marks the file as **complete** -- no more angles will ever be appended. A commented-out `# END` does not count, so the marker can be staged ahead of time and uncommented when the file is truly finished. Anything other than a comment or blank line after `END` is an error.
|
||||
|
||||
Example:
|
||||
```
|
||||
# angle subtomo
|
||||
0 1
|
||||
45 1
|
||||
190.5 2
|
||||
270, 2
|
||||
END
|
||||
```
|
||||
|
||||
**Behavior**
|
||||
- The file is read once at the start of `flomni.tomo_scan()` (and again at `flomni.tomo_scan_resume()` or when a queued job runs), before any sample registration or motion. A missing or invalid file fails immediately (in the queue the job is marked `incomplete` with the error message).
|
||||
- **While the scan is running, the file is re-read and polled (about every 5 s) whenever the scan catches up to the current end of file.** This means you can grow the file by appending lines while a scan is running -- newly appended angles are picked up automatically, with no need to call `tomo_scan_resume()`.
|
||||
- A file without an `END` marker is **incomplete**: once the scan reaches the end of it, it waits indefinitely for more lines rather than finishing, printing a reminder each time it starts waiting. Add an `END` line when you want the scan to finish on its own -- a file prepared in advance with the full list of angles should have `END` as its last line, or a scan against it will hang at the end rather than completing. Pressing Ctrl-C while waiting raises a clean, catchable error (safe to use to abort a queued job in this state) rather than a bare interrupt.
|
||||
- Only append new lines after the last one the scan has already acquired. Editing or removing a line that has already been acquired aborts the scan with an error on the next poll; editing a line that has been written to the file but not yet acquired is not guaranteed to be picked up, so avoid relying on it.
|
||||
- At scan start the number of angles known so far, their range and the number of sub-tomograms are printed (noting if the file is still incomplete), and the parsed angle list is written to scilog. The summary is printed again, together with the 180-deg-pair warning below, once the file becomes complete.
|
||||
- A warning is printed if the file contains angles that are identical or 180 deg apart (within 0.05 deg), which are redundant in parallel-beam geometry. The scan is still run.
|
||||
- No extra 0-deg damage-estimation shots are inserted (`golden_projections_at_0_deg_for_damage_estimation` and the sub-tomogram 0-deg reference are ignored); put 0-deg lines into the file if you want them.
|
||||
- `flomni.tomo_scan_resume()` is for recovering from a crash or an explicit abort (e.g. Ctrl-C) -- it is not needed just to pick up appended lines, since a running scan already waits for and picks those up on its own. When used, it continues at the interrupted projection index of the file, using the file's *current* content from that index on. If the file has become shorter than the resume index, the resume is refused.
|
||||
- In the tomo queue the file path is snapshotted per job when it is added, and `flomni.tomo_queue_reacquire(job_index, projection_number)` uses the projection index in the file (0-indexed, in file order).
|
||||
|
||||
(user.ptychography.flomni.angle_file.overrides)=
|
||||
**Per-line acquisition overrides**
|
||||
|
||||
Besides the angle and sub-tomogram number, each line may add any number of `key=value` tokens that override acquisition parameters otherwise taken from the live `flomni.tomo_parameters()` configuration, for that projection and every one after it:
|
||||
- `step=<um>` -- the Fermat scan step size (otherwise `flomni.tomo_shellstep`), range `0.025-20.0`.
|
||||
- `exptime=<s>` -- the exposure/counting time per frame (otherwise `flomni.tomo_countingtime`), range `0.001-100.0`.
|
||||
- `frames=<n>` -- the burst frame count (otherwise `flomni.frames_per_trigger`), integer, range `1-100`.
|
||||
|
||||
These three are **sticky**: once a line sets one, it stays in effect -- for that line and every line after it -- until a later line changes it again. They are independent of each other (setting `step=` doesn't touch `exptime=`/`frames=`) and independent of the sub-tomogram number, which is never sticky. A field that is never set on any line simply uses whatever the live configuration is at the moment that projection is actually acquired, so a mix of overridden and non-overridden lines behaves exactly as if the non-overridden ones weren't in a tomo_type-4 file at all.
|
||||
|
||||
Example -- coarse-and-fast for the first half, then fine-and-slow:
|
||||
```
|
||||
0 1
|
||||
45 1
|
||||
90 1 step=0.3 exptime=2 frames=3
|
||||
135 1
|
||||
180 2
|
||||
225 2 step=0.05 exptime=10
|
||||
270 2
|
||||
END
|
||||
```
|
||||
Here 0/45 use whatever `tomo_shellstep`/`tomo_countingtime`/`frames_per_trigger` are configured; 90/135/180 use `step=0.3 exptime=2 frames=3` (135 and 180 inherit it from 90); 225/270 use `step=0.05 exptime=10` with `frames=3` still carried forward from before (not reset, since `frames=` was never changed again).
|
||||
|
||||
A scan summary note ("N with per-line overrides") is printed whenever any entry carries an override, so a typo'd `key=value` token that silently parsed as something else would stand out as an unexpected count.
|
||||
|
||||
If a custom `at_each_angle_hook` or `flomni_at_each_angle` is active, it owns the whole per-projection acquisition and never sees these overrides -- a warning is printed once if the file has any overrides while such a hook is active.
|
||||
|
||||
In case of eight equally spaced sub-tomograms, an individual sub tomogram can be scanned by flomni.sub_tomo_scan(subtomo_number, start_angle). If the start angle is not specified, it will be computed depending on the subtomo_number, which is ranging from 1 to 8.
|
||||
|
||||
#### Queueing multiple scans
|
||||
|
||||
@@ -33,7 +33,16 @@ At the end you're shown the computed `lsamx_center`/`lsamy_center` and asked to
|
||||
|
||||
**Experimental: continuous-rotation "smear" aid**
|
||||
|
||||
`lamni.xrayeye_rotation_center_calibration_smear_experimental(sweep_deg=360.0)` is an **experimental** alternative to `..._extended()` for the same non-isolated-sample case. Instead of judging the rotation centre from a single instant of live rotation, it rotates `lsamrot` continuously through `sweep_deg` while accumulating a max-projection ("star-trail") composite from the camera: off-axis features smear into circular arcs, and the common center of curvature of those arcs is the rotation axis — usually an easier target to click than one live frame. The composite builds up live on screen as the sweep progresses and stays frozen once the sweep ends, until you submit your click. `sweep_deg` can be reduced below 360 if a shorter arc already shows enough curvature. There is no automatic circle fitting — you still submit the centre by eye, same click mechanism as `..._extended()`.
|
||||
`lamni.xrayeye_rotation_center_calibration_smear_experimental(sweep_deg=360.0, projection="min", opaque_level=0.25)` is an **experimental** alternative to `..._extended()` for the same non-isolated-sample case. Instead of judging the rotation centre from a single instant of live rotation, it rotates `lsamrot` continuously through `sweep_deg` while accumulating a "star-trail" composite from the camera: off-axis features smear into circular arcs, and the common center of curvature of those arcs is the rotation axis — usually an easier target to click than one live frame. The composite builds up live on screen as the sweep progresses and stays frozen once the sweep ends, until you submit your click. `sweep_deg` can be reduced below 360 if a shorter arc already shows enough curvature. There is no automatic circle fitting — you still submit the centre by eye, same click mechanism as `..._extended()`.
|
||||
|
||||
`projection` selects how the frames are combined:
|
||||
|
||||
- `"min"` (default) keeps the darkest value seen at each pixel. Use it for absorption contrast, i.e. dark sample features on a bright background — the usual case on the X-ray eye. Small absorbing features leave dark arcs. A large sample fills in as a dark region covering everything it occupied during the rotation; its outline is symmetric about the projected rotation axis (an ellipse rather than a circle, because of the tilted axis), so click the centre of that outline.
|
||||
|
||||
Opaque parts such as the sample holder frame would otherwise black out every pixel they pass at any angle and shrink the usable field of view. To avoid that, a pixel is ignored in any frame where it is darker than `opaque_level` (default `0.25`) times that frame's bright level (its 99th-percentile value), and the minimum is taken over the remaining frames only. A pixel that was opaque at every angle stays dark. If the holder still blacks out too much, raise `opaque_level` (e.g. `0.4`). If faint sample features disappear, lower it. `opaque_level=0` gives the plain minimum.
|
||||
- `"max"` keeps the brightest value at each pixel, for bright features on a dark background. Do not use it for absorption: every pixel sees the bright background at some point during the rotation, so the dark features are washed out.
|
||||
|
||||
Each run is archived to `~/data/raw/logs/xrayeye_smear_calibration/` (composites, clicked centres, result, and the `sweep_deg`/`projection`/`opaque_level` used).
|
||||
|
||||
**Manual fallback**
|
||||
|
||||
@@ -81,6 +90,22 @@ Example, checking the correction at 90°:
|
||||
3. `lamni.xrayeye_update_frame(keep_shutter_open=True)` to look at the sample
|
||||
4. `lamni.lamni_compute_additional_correction_xeye_mu(90)` prints the correction (x and y, in mm) that the scan will add on top of this position. The sample should appear displaced from the FZP centre by the size of this correction, and the correction compensates for that displacement. A clearly different displacement means the fit is off, e.g. a bad click, and the alignment should be repeated.
|
||||
|
||||
##### Moving to the scan centre including the X-ray eye fit
|
||||
|
||||
`lamni_move_to_scan_center()` above places the sample at the base shift only. To go where a projection at the current angle would go, including the sine fit from the X-ray eye alignment (`tomo_fit_xray_eye`), rotate first and then run:
|
||||
|
||||
```python
|
||||
angle = dev.lsamrot.user_setpoint.get()
|
||||
cx, cy = lamni.lamni_compute_additional_correction_xeye_mu(angle) # mm, from the X-ray eye fit
|
||||
from csaxs_bec.scans.lamni_components import LamNIComponents
|
||||
sx, sy = LamNIComponents.lamni_compute_scan_center(lamni.tomo_fovx_offset, lamni.tomo_fovy_offset, angle)
|
||||
lamni.lamni_move_to_scan_center(sx + cx, sy + cy, 0).wait()
|
||||
```
|
||||
|
||||
The base shift is rotated to the angle first, so the move itself is called with angle 0 to pass the target through unchanged. This matches a projection without stitching, manual shifts (`lamni.manual_shift_x/y`, add them) and the correction tables from fine alignment (`lamni.compute_additional_correction(angle)` and `..._2(angle)`, subtract them). The positions are relative to the `center` user parameters of `lsamx`/`lsamy`, which must be set. Check the result with `lamni.show_interferometer_positions()`.
|
||||
|
||||
If the drift between coarse stages and interferometer found at the start of the move is larger than 150 µm, it is considered unreliable and ignored; the move then relies on the second, interferometer-based iteration.
|
||||
|
||||
* If slits were opened during alignment, close the slits: `slits` to around 0.3
|
||||
* `lamni.leye_out()` remove the X-ray eye and move the flight tube in
|
||||
* *possibly check slit1, idgap*
|
||||
@@ -127,13 +152,14 @@ Start the laminography scan by
|
||||
|
||||
During the scan, a live progress report is printed (subtomogram/projection counters and an estimated time of completion). Gaps significantly longer than a normal acquisition cycle (e.g. a beamline-down interruption, or a crash followed by a restart) are detected automatically from the time between consecutive projections and excluded from the time estimate; the total time lost to such gaps is printed once the scan finishes.
|
||||
|
||||
Three angular sampling modes are implemented:
|
||||
Four angular sampling modes are implemented (the fourth reads the angles from a file, see [Angles from a file](user.ptychography.lamni.angle_file) below):
|
||||
|
||||
| tomography mode | parameters and defaults |
|
||||
| --- | --- |
|
||||
| 8 sub-tomograms | subtomo_start=1, start_angle=None |
|
||||
| Golden ratio tomography (sorted in bunches) | projection_number=None |
|
||||
| Equally spaced with golden starting angle | projection_number=None |
|
||||
| Angles from a file (tomo_type 4) | projection_number=None |
|
||||
|
||||
The parameters above can be used to __restart an interrupted acquisition__ manually, or - more conveniently - by running
|
||||
`lamni.tomo_scan_resume()`
|
||||
@@ -143,6 +169,37 @@ In case of eight equally spaced sub-tomograms, an individual sub-tomogram can be
|
||||
|
||||
`lamni.tomo_parameters()` also offers `lamni_stitch_x`/`lamni_stitch_y` (integers, default 0, up to 50 each) for a field of view larger than a single scan can cover: when nonzero, every projection angle is acquired as a `(2*lamni_stitch_x+1) × (2*lamni_stitch_y+1)` grid of separate Fermat scans instead of a single one, tiled around the normal alignment-corrected center and spaced by `lamni_piezo_range_x`/`lamni_piezo_range_y` minus `tomo_stitch_overlap` (µm, default 0.2) per step in x/y — stitching together an effective field of view of roughly `(2*lamni_stitch_x+1) × lamni_piezo_range_x` by `(2*lamni_stitch_y+1) × lamni_piezo_range_y`, with `tomo_stitch_overlap` controlling the deliberate overlap between adjacent tiles used for aligning them together during reconstruction. Same mechanism as flomni's `stitch_x`/`stitch_y`, just under lamni's own parameter names (see `lamni.tomo_scan_projection()`, which passes `stitch_x`/`stitch_y`/`stitch_overlap` straight through to `scans.lamni_fermat_scan()`).
|
||||
|
||||
(user.ptychography.lamni.angle_file)=
|
||||
#### Angles from a file (tomo_type 4)
|
||||
|
||||
With `tomo_type` 4 the projection angles are not computed but read from a plain-text file you provide, e.g. for a non-uniform sampling or to repeat a previously used list. Set the file in `lamni.tomo_parameters()` (choose type 4, then **Browse...**; the GUI shows a live preview of the file and blocks Submit / Add to queue while the file is invalid), or in `lamni.tomo_parameters_cli()`, which prompts for the path until it parses. Only the path is stored (`lamni.tomo_angle_file`), so use a path readable from the BEC client machine. The file format, the `END` marker and the per-line overrides are identical to flomni, see [Angles from a file](user.ptychography.flomni.angle_file) there for the full description.
|
||||
|
||||
**File requirements**
|
||||
- Plain text, one projection per line.
|
||||
- Column 1: the angle in degrees, `0 <= angle <= 360`.
|
||||
- Column 2 (optional): the sub-tomogram number, an integer `>= 1` (default 1). It is only a label for the progress display and saved metadata.
|
||||
- Whitespace and/or commas separate columns; everything after `#` is a comment.
|
||||
- Projections are acquired in file order.
|
||||
- Optional `key=value` tokens per line override `step=<um>` (`0.025-20.0`), `exptime=<s>` (`0.001-100.0`) and `frames=<n>` (`1-100`) for that projection and all following ones (sticky). They replace `lamni.tomo_shellstep`, `lamni.tomo_countingtime` and `lamni.frames_per_trigger`; an active `at_each_angle_hook` never sees them (a warning is printed).
|
||||
- A final `END` line marks the file complete. Without it, the scan waits (polling about every 5 s) for more lines to be appended once it reaches the end of the file; Ctrl-C while waiting gives a clean, catchable error.
|
||||
|
||||
Example:
|
||||
```
|
||||
# angle subtomo
|
||||
0 1
|
||||
45 1
|
||||
190.5 2 step=0.5
|
||||
270 2
|
||||
END
|
||||
```
|
||||
|
||||
**Behavior**
|
||||
- The file is read at the start of `lamni.tomo_scan()`, at `lamni.tomo_scan_resume()` and when a queued job runs, before any sample registration or motion. A missing or invalid file fails immediately (in the queue the job is marked `incomplete`).
|
||||
- Only append new lines after the last acquired one; editing or removing an already acquired line aborts the scan on the next poll.
|
||||
- Unlike flomni, no warning is given for angles that are identical or 180 deg apart: in laminography they are not redundant.
|
||||
- As for every new tomogram, an unconditional 0-deg reference projection is taken first. No further 0-deg damage-estimation shots are inserted (`golden_projections_at_0_deg_for_damage_estimation` and `zero_deg_reference_at_each_subtomo` are ignored); put 0-deg lines in the file if you want them.
|
||||
- `lamni.tomo_scan_resume()` continues at the interrupted projection index, using the file's current content from that index on. In the queue, `lamni.tomo_queue_reacquire(job_index, projection_number)` uses the projection index in the file (0-indexed).
|
||||
|
||||
#### Queueing multiple scans
|
||||
|
||||
Several tomo parameter sets can be queued and run sequentially on the same sample, without having to start each one by hand. Easiest is to use the tomo parameters GUI for preparing the scans. But it is also possible on the command line interface.
|
||||
@@ -494,11 +551,23 @@ Example: `lamni.lfzp_info(6.2)`
|
||||
|
||||
The laser feedback will be disabled and fine alignment lost if foptx/y are moved!
|
||||
|
||||
Following functions exist to move the optics in and out, the naming is self-explaining.
|
||||
Following functions exist to move the optics in and out. The positions are the `in`/`out` user parameters of the devices; a move is skipped if the axes are already within 3 µm of the target.
|
||||
|
||||
* `lamni.loptics_in()` moves FZP, OSA and central stop in, in that order
|
||||
* `lamni.loptics_out()` moves central stop, OSA and FZP out, in that order, with the interferometer feedback disabled, and re-enables the feedback with reset afterwards
|
||||
* `lamni.lfzp_in()`
|
||||
* `lamni.loptics_in()`
|
||||
* `lamni.loptics_out()`
|
||||
* `lamni.losa_in()`
|
||||
* `lamni.losa_out()`
|
||||
* `lamni.lfzp_in()`
|
||||
* `lamni.lcs_in()` moves `lcsx` and `lcsy` to their `in` positions
|
||||
* `lamni.lcs_out()` moves `lcsy` to its `out` position; `lcsx` has no `out` position and stays where it is
|
||||
|
||||
(user.ptychography.lamni.laser_feedback)=
|
||||
#### Interferometer feedback commands
|
||||
|
||||
* `lamni.feedback_enable_with_reset()` enables the closed-loop control and resets the interferometers
|
||||
* `lamni.feedback_disable()`
|
||||
* `lamni.feedback_status()` prints whether the loop is running
|
||||
* `lamni.show_interferometer_positions()` prints the current interferometer x/y positions in µm and whether the loop is running, and returns them as a dict
|
||||
* `lamni.show_signal_strength()` and `lamni.show_analog_signals()` print the interferometer signal strengths and the piezo/capacitive sensor readings
|
||||
|
||||
With the feedback disabled, the interferometer follows the coarse stages: moving `lsamx` by +0.1 mm changes x by about +50 µm (`x = 0.5 · lsamx` in the LamNI geometry). This is a quick check that the interferometer is working before moving to a scan centre.
|
||||
@@ -25,7 +25,7 @@ from csaxs_bec.bec_ipython_client.plugins.flomni import flomni as flomni_module
|
||||
from csaxs_bec.bec_ipython_client.plugins.flomni.flomni_webpage_generator import (
|
||||
FlomniWebpageGenerator,
|
||||
)
|
||||
from csaxs_bec.bec_ipython_client.plugins.omny.omny_general_tools import OMNYTools
|
||||
from csaxs_bec.bec_ipython_client.plugins.OMNY_shared.omny_general_tools import OMNYTools
|
||||
|
||||
|
||||
class _NoOpSciLogMessage:
|
||||
|
||||
@@ -65,7 +65,7 @@ def test_empty_and_all_done_queue_are_noops(flomni_sim, monkeypatch, capsys):
|
||||
flomni = flomni_sim
|
||||
|
||||
prompt_calls = []
|
||||
from csaxs_bec.bec_ipython_client.plugins.omny.omny_general_tools import OMNYTools
|
||||
from csaxs_bec.bec_ipython_client.plugins.OMNY_shared.omny_general_tools import OMNYTools
|
||||
|
||||
monkeypatch.setattr(
|
||||
OMNYTools, "yesno", lambda self, *a, **k: prompt_calls.append((a, k)) or True
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
"""Step 2 -- command jobs. See AI_docs/TOMO_QUEUE_COMMAND_JOBS_PLAN.md.
|
||||
|
||||
mokev/idgap (the real curated move targets) aren't in the flomni sim config
|
||||
(simulated_flomni.yaml) -- only on the real front end (bl_frontend.yaml), so
|
||||
the real device-moving path here is exercised against `ftray` (sample
|
||||
transfer tray), added to _TOMO_QUEUE_MOVE_DEVICES specifically for this
|
||||
sim-testability, per TOMO_QUEUE_TESTING.md.
|
||||
ccm_energy/idgap (the plain "move" allow-list) aren't in the flomni sim
|
||||
config (simulated_flomni.yaml) -- only on the real front end
|
||||
(bl_frontend.yaml/bl_optics_hutch.yaml). Tests that only exercise add-time
|
||||
validation/rendering (no tomo_queue_execute()) reference idgap for that
|
||||
reason, while tests that actually execute the queue use the dedicated
|
||||
"move_foptz_with_shift" action against foptz, which IS in the sim config
|
||||
and is the only queue-movable path for foptz (it is deliberately excluded
|
||||
from the plain "move" allow-list, see flomni.py's _TOMO_QUEUE_MOVE_DEVICES).
|
||||
"""
|
||||
|
||||
import datetime
|
||||
@@ -13,8 +16,8 @@ import uuid
|
||||
import pytest
|
||||
from _queue_helpers import FAST_STEPSIZE, add_short_job
|
||||
|
||||
FTRAY_A = -50.0
|
||||
FTRAY_B = -10.0
|
||||
FOPTZ_A = 15.0
|
||||
FOPTZ_B = 5.0
|
||||
|
||||
|
||||
def test_add_command_validation_errors(flomni_sim):
|
||||
@@ -28,12 +31,18 @@ def test_add_command_validation_errors(flomni_sim):
|
||||
|
||||
with pytest.raises(ValueError, match="above the maximum"):
|
||||
flomni.tomo_queue_add_command(
|
||||
{"action": "optimize_idgap", "kwargs": {"search_range": 99.0}}
|
||||
{
|
||||
"action": "move_foptz_with_shift",
|
||||
"kwargs": {"foptz_target": 99.0, "manual_shift_x": 0.0, "manual_shift_y": 0.0},
|
||||
}
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError, match="must be a number"):
|
||||
flomni.tomo_queue_add_command(
|
||||
{"action": "optimize_idgap", "kwargs": {"search_range": "wide"}}
|
||||
{
|
||||
"action": "move_foptz_with_shift",
|
||||
"kwargs": {"foptz_target": "far", "manual_shift_x": 0.0, "manual_shift_y": 0.0},
|
||||
}
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError, match="has no parameter"):
|
||||
@@ -74,23 +83,25 @@ def test_move_execution_time_allowlist_recheck(flomni_sim):
|
||||
def test_tomo_queue_actions_global_var_matches_registry(flomni_sim):
|
||||
flomni = flomni_sim
|
||||
published = flomni.client.get_global_var("tomo_queue_actions")
|
||||
assert set(published["actions"]) == {"move", "optimize_idgap"}
|
||||
assert published["move_devices"]["ftray"]["unit"] == "mm"
|
||||
assert set(published["actions"]) == {"move", "move_foptz_with_shift"}
|
||||
assert published["move_devices"]["idgap"]["unit"] == "mm"
|
||||
assert published["actions"]["move"]["idempotent"] is True
|
||||
assert published["actions"]["optimize_idgap"]["params"]["search_range"]["default"] == 0.5
|
||||
assert (
|
||||
published["actions"]["move_foptz_with_shift"]["params"]["foptz_target"]["default"] == 17.0
|
||||
)
|
||||
|
||||
|
||||
def test_show_and_webpage_survive_command_job(flomni_sim, capsys):
|
||||
flomni = flomni_sim
|
||||
flomni.tomo_queue_add_command(
|
||||
{"action": "move", "kwargs": {"positions": {"ftray": FTRAY_A}}},
|
||||
label="move ftray",
|
||||
{"action": "move", "kwargs": {"positions": {"idgap": 6.0}}},
|
||||
label="move idgap",
|
||||
idempotent=True,
|
||||
)
|
||||
capsys.readouterr()
|
||||
jobs = flomni.tomo_queue_show()
|
||||
out = capsys.readouterr().out
|
||||
assert "CMD move{'positions': {'ftray': -50.0}}" in out
|
||||
assert "CMD move{'positions': {'idgap': 6.0}}" in out
|
||||
assert "[idem]" in out
|
||||
assert jobs[0]["kind"] == "command"
|
||||
|
||||
@@ -100,7 +111,7 @@ def test_show_and_webpage_survive_command_job(flomni_sim, capsys):
|
||||
assert "isCommand" in html # the rendering branch exists; full JS isn't executed here
|
||||
|
||||
|
||||
def test_legacy_job_no_kind_defaults_to_tomo_and_command_moves_ftray(flomni_sim):
|
||||
def test_legacy_job_no_kind_defaults_to_tomo_and_command_moves_foptz(flomni_sim):
|
||||
"""B-style back-compat + the real command-job execution path, combined
|
||||
into one mixed-queue run to keep the (only) real scan in this file to
|
||||
one job.
|
||||
@@ -109,12 +120,12 @@ def test_legacy_job_no_kind_defaults_to_tomo_and_command_moves_ftray(flomni_sim)
|
||||
bec = flomni.client
|
||||
|
||||
# Start from a known position, not whatever a previous test run happened
|
||||
# to leave ftray at -- ftray's position is real device-server state that
|
||||
# to leave foptz at -- foptz's position is real device-server state that
|
||||
# persists across separate pytest invocations, unlike the tomo_queue/
|
||||
# tomo_progress global vars the flomni_sim fixture snapshots/restores.
|
||||
flomni._queue_action_move({"ftray": FTRAY_B})
|
||||
ftray_before = bec.device_manager.devices.ftray.readback.get()
|
||||
assert ftray_before == pytest.approx(FTRAY_B)
|
||||
flomni._queue_action_move_foptz_with_shift(FOPTZ_B, 0.0, 0.0)
|
||||
foptz_before = bec.device_manager.devices.foptz.readback.get()
|
||||
assert foptz_before == pytest.approx(FOPTZ_B)
|
||||
|
||||
add_short_job(flomni, "legacy_tomo", FAST_STEPSIZE)
|
||||
# Simulate a queue entry written before the "kind" field existed.
|
||||
@@ -123,41 +134,48 @@ def test_legacy_job_no_kind_defaults_to_tomo_and_command_moves_ftray(flomni_sim)
|
||||
bec.set_global_var("tomo_queue", jobs)
|
||||
|
||||
flomni.tomo_queue_add_command(
|
||||
{"action": "move", "kwargs": {"positions": {"ftray": FTRAY_A}}}, label="move ftray"
|
||||
)
|
||||
flomni.tomo_queue_add_command(
|
||||
{"action": "optimize_idgap", "kwargs": {"search_range": 0.5}}, label="idgap stub"
|
||||
{
|
||||
"action": "move_foptz_with_shift",
|
||||
"kwargs": {"foptz_target": FOPTZ_A, "manual_shift_x": 1.0, "manual_shift_y": -2.0},
|
||||
},
|
||||
label="move foptz",
|
||||
)
|
||||
|
||||
flomni.tomo_queue_execute()
|
||||
|
||||
jobs = flomni._tomo_queue_proxy.as_list()
|
||||
assert [j["status"] for j in jobs] == ["done", "done", "done"]
|
||||
assert [j["status"] for j in jobs] == ["done", "done"]
|
||||
# Still no "kind" key -- proves it ran (and got marked done) via the
|
||||
# back-compat default, not that execute() silently added one.
|
||||
assert "kind" not in jobs[0]
|
||||
assert "params" in jobs[0]
|
||||
assert jobs[1]["kind"] == "command"
|
||||
|
||||
ftray_after = bec.device_manager.devices.ftray.readback.get()
|
||||
assert ftray_after == pytest.approx(FTRAY_A)
|
||||
assert ftray_after != pytest.approx(ftray_before)
|
||||
foptz_after = bec.device_manager.devices.foptz.readback.get()
|
||||
assert foptz_after == pytest.approx(FOPTZ_A)
|
||||
assert foptz_after != pytest.approx(foptz_before)
|
||||
assert flomni.manual_shift_x == pytest.approx(1.0)
|
||||
assert flomni.manual_shift_y == pytest.approx(-2.0)
|
||||
|
||||
|
||||
def test_non_idempotent_command_resume_prompts(flomni_sim, monkeypatch):
|
||||
flomni = flomni_sim
|
||||
bec = flomni.client
|
||||
ftray = bec.device_manager.devices.ftray
|
||||
foptz = bec.device_manager.devices.foptz
|
||||
|
||||
# Use the same real move path the "move" action itself uses, rather than
|
||||
# the device's underlying ophyd object directly (ftray's BEC wrapper is
|
||||
# a Positioner here, unlike fsamx's DeviceBase -- no .obj to reach for).
|
||||
flomni._queue_action_move({"ftray": FTRAY_B})
|
||||
start = ftray.readback.get()
|
||||
assert start == pytest.approx(FTRAY_B)
|
||||
# Use the same real move path the "move_foptz_with_shift" action itself
|
||||
# uses, rather than the device's underlying ophyd object directly
|
||||
# (foptz's BEC wrapper is a Positioner here, unlike fsamx's DeviceBase --
|
||||
# no .obj to reach for).
|
||||
flomni._queue_action_move_foptz_with_shift(FOPTZ_B, 0.0, 0.0)
|
||||
start = foptz.readback.get()
|
||||
assert start == pytest.approx(FOPTZ_B)
|
||||
|
||||
index = flomni.tomo_queue_add_command(
|
||||
{"action": "move", "kwargs": {"positions": {"ftray": FTRAY_A}}},
|
||||
{
|
||||
"action": "move_foptz_with_shift",
|
||||
"kwargs": {"foptz_target": FOPTZ_A, "manual_shift_x": 0.0, "manual_shift_y": 0.0},
|
||||
},
|
||||
label="non-idempotent move",
|
||||
idempotent=False,
|
||||
)
|
||||
@@ -165,7 +183,7 @@ def test_non_idempotent_command_resume_prompts(flomni_sim, monkeypatch):
|
||||
flomni._tomo_queue_proxy.update_by_id(job_id, status="incomplete")
|
||||
|
||||
prompts = []
|
||||
from csaxs_bec.bec_ipython_client.plugins.omny.omny_general_tools import OMNYTools
|
||||
from csaxs_bec.bec_ipython_client.plugins.OMNY_shared.omny_general_tools import OMNYTools
|
||||
|
||||
def _yesno(resume_answer):
|
||||
# tomo_queue_execute()'s own top-level "OK to start?" confirmation
|
||||
@@ -180,11 +198,11 @@ def test_non_idempotent_command_resume_prompts(flomni_sim, monkeypatch):
|
||||
|
||||
return _fn
|
||||
|
||||
# "no" -> must not re-run: ftray stays put, job still ends up "done".
|
||||
# "no" -> must not re-run: foptz stays put, job still ends up "done".
|
||||
monkeypatch.setattr(OMNYTools, "yesno", _yesno(False))
|
||||
flomni.tomo_queue_execute()
|
||||
assert len(prompts) == 1
|
||||
assert ftray.readback.get() == pytest.approx(start)
|
||||
assert foptz.readback.get() == pytest.approx(start)
|
||||
assert flomni._tomo_queue_proxy.as_list()[0]["status"] == "done"
|
||||
|
||||
# Re-arm as incomplete and try again with "yes" -> must re-run.
|
||||
@@ -192,5 +210,5 @@ def test_non_idempotent_command_resume_prompts(flomni_sim, monkeypatch):
|
||||
monkeypatch.setattr(OMNYTools, "yesno", _yesno(True))
|
||||
flomni.tomo_queue_execute()
|
||||
assert len(prompts) == 2
|
||||
assert ftray.readback.get() == pytest.approx(FTRAY_A)
|
||||
assert foptz.readback.get() == pytest.approx(FOPTZ_A)
|
||||
assert flomni._tomo_queue_proxy.as_list()[0]["status"] == "done"
|
||||
@@ -0,0 +1,260 @@
|
||||
"""Tests for the tomo_type-4 angle file parser (OMNY_shared/angle_file.py)."""
|
||||
|
||||
import pytest
|
||||
|
||||
from csaxs_bec.bec_ipython_client.plugins.OMNY_shared.angle_file import (
|
||||
AngleEntry,
|
||||
AngleFileError,
|
||||
describe_opposite_pairs,
|
||||
describe_summary,
|
||||
read_angle_file,
|
||||
summarize_angles,
|
||||
)
|
||||
|
||||
|
||||
def _e(angle, subtomo=1, step=None, exp_time=None, frames_per_trigger=None):
|
||||
"""Shorthand for building the expected AngleEntry list in assertions."""
|
||||
return AngleEntry(
|
||||
angle=angle, subtomo=subtomo, step=step, exp_time=exp_time,
|
||||
frames_per_trigger=frames_per_trigger,
|
||||
)
|
||||
|
||||
|
||||
def _write(tmp_path, text, name="angles.txt"):
|
||||
path = tmp_path / name
|
||||
path.write_text(text)
|
||||
return str(path)
|
||||
|
||||
|
||||
def test_one_column_defaults_to_subtomo_1(tmp_path):
|
||||
path = _write(tmp_path, "0\n45.5\n360\n")
|
||||
result = read_angle_file(path)
|
||||
assert result.angles == [_e(0.0), _e(45.5), _e(360.0)]
|
||||
assert result.is_complete is False
|
||||
|
||||
|
||||
def test_two_columns_whitespace_and_comma_separators(tmp_path):
|
||||
path = _write(tmp_path, "0 1\n10\t1\n190.5, 2\n270,3\n")
|
||||
assert read_angle_file(path).angles == [_e(0.0), _e(10.0), _e(190.5, 2), _e(270.0, 3)]
|
||||
|
||||
|
||||
def test_comments_and_blank_lines_ignored_and_order_kept(tmp_path):
|
||||
path = _write(tmp_path, "# header\n\n90 2 # trailing comment\n \n30 1\n")
|
||||
assert read_angle_file(path).angles == [_e(90.0, 2), _e(30.0, 1)]
|
||||
|
||||
|
||||
def test_tilde_expansion(tmp_path, monkeypatch):
|
||||
monkeypatch.setenv("HOME", str(tmp_path))
|
||||
_write(tmp_path, "12\n")
|
||||
assert read_angle_file("~/angles.txt").angles == [_e(12.0)]
|
||||
|
||||
|
||||
def test_angle_range_matches_rotation_stage_overtravel(tmp_path):
|
||||
"""-2 and 362 (the stage's physical overtravel limits) must parse, not
|
||||
just the nominal 0-360 sweep."""
|
||||
path = _write(tmp_path, "-2\n-0.1\n0\n360.01\n362\n")
|
||||
assert read_angle_file(path).angles == [
|
||||
_e(-2.0),
|
||||
_e(-0.1),
|
||||
_e(0.0),
|
||||
_e(360.01),
|
||||
_e(362.0),
|
||||
]
|
||||
|
||||
|
||||
def test_end_marks_complete(tmp_path):
|
||||
path = _write(tmp_path, "0\n10\nEND\n")
|
||||
result = read_angle_file(path)
|
||||
assert result.angles == [_e(0.0), _e(10.0)]
|
||||
assert result.is_complete is True
|
||||
|
||||
|
||||
def test_missing_end_is_incomplete(tmp_path):
|
||||
path = _write(tmp_path, "0\n10\n")
|
||||
assert read_angle_file(path).is_complete is False
|
||||
|
||||
|
||||
@pytest.mark.parametrize("marker", ["END", "end", "EnD"])
|
||||
def test_end_case_insensitive(tmp_path, marker):
|
||||
path = _write(tmp_path, f"0\n{marker}\n")
|
||||
assert read_angle_file(path).is_complete is True
|
||||
|
||||
|
||||
def test_commented_end_is_not_sentinel(tmp_path):
|
||||
path = _write(tmp_path, "0\n# END\n10\n")
|
||||
result = read_angle_file(path)
|
||||
assert result.angles == [_e(0.0), _e(10.0)]
|
||||
assert result.is_complete is False
|
||||
|
||||
|
||||
def test_end_with_trailing_comment(tmp_path):
|
||||
path = _write(tmp_path, "0\nEND # done\n")
|
||||
assert read_angle_file(path).is_complete is True
|
||||
|
||||
|
||||
def test_content_after_end_raises(tmp_path):
|
||||
path = _write(tmp_path, "0\nEND\n10\n")
|
||||
with pytest.raises(AngleFileError, match="line 3.*line 2"):
|
||||
read_angle_file(path)
|
||||
|
||||
|
||||
def test_blank_and_comment_after_end_allowed(tmp_path):
|
||||
path = _write(tmp_path, "0\nEND\n\n# trailing note\n")
|
||||
result = read_angle_file(path)
|
||||
assert result.angles == [_e(0.0)]
|
||||
assert result.is_complete is True
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"text, match",
|
||||
[
|
||||
("-2.1\n", "outside"),
|
||||
("362.01\n", "outside"),
|
||||
("abc\n", "invalid angle"),
|
||||
("10 x\n", "invalid sub-tomogram"),
|
||||
("10 0\n", ">= 1"),
|
||||
("10 1 5\n", "expected"),
|
||||
("# only comments\n\n", "no angles"),
|
||||
],
|
||||
)
|
||||
def test_invalid_content_raises_with_reason(tmp_path, text, match):
|
||||
path = _write(tmp_path, text)
|
||||
with pytest.raises(AngleFileError, match=match):
|
||||
read_angle_file(path)
|
||||
|
||||
|
||||
def test_error_reports_line_number(tmp_path):
|
||||
path = _write(tmp_path, "# c\n10\nbad\n")
|
||||
with pytest.raises(AngleFileError, match="line 3"):
|
||||
read_angle_file(path)
|
||||
|
||||
|
||||
def test_missing_file_and_empty_path_raise(tmp_path):
|
||||
with pytest.raises(AngleFileError, match="Cannot read"):
|
||||
read_angle_file(str(tmp_path / "nope.txt"))
|
||||
with pytest.raises(AngleFileError, match="No angle file"):
|
||||
read_angle_file("")
|
||||
|
||||
|
||||
def test_angle_file_error_is_a_value_error():
|
||||
assert issubclass(AngleFileError, ValueError)
|
||||
|
||||
|
||||
def test_summary_counts():
|
||||
summary = summarize_angles([_e(0.0), _e(45.0), _e(100.0, 3)])
|
||||
assert summary["n"] == 3
|
||||
assert summary["min"] == 0.0
|
||||
assert summary["max"] == 100.0
|
||||
assert summary["n_subtomos"] == 2
|
||||
assert summary["n_overridden"] == 0
|
||||
assert summary["opposite_pairs"] == []
|
||||
assert describe_summary(summary) == "3 angles, 0.00-100.00 deg, 2 sub-tomograms"
|
||||
assert describe_opposite_pairs(summary) is None
|
||||
|
||||
|
||||
def test_summary_detects_180_opposite_and_exact_duplicates():
|
||||
entries = [_e(10.0), _e(190.0), _e(50.0), _e(50.0, 2), _e(120.0)]
|
||||
pairs = summarize_angles(entries)["opposite_pairs"]
|
||||
assert [(a, b) for a, b, _, _ in pairs] == [(0, 1), (2, 3)]
|
||||
|
||||
|
||||
def test_summary_detects_pair_across_the_mod_180_wrap():
|
||||
# 179.99 and 0.0 are 0.01 apart mod 180; so are 359.99 and 0.0
|
||||
pairs = summarize_angles([_e(0.0), _e(90.0), _e(359.99)])["opposite_pairs"]
|
||||
assert [(a, b) for a, b, _, _ in pairs] == [(0, 2)]
|
||||
|
||||
|
||||
def test_summary_no_false_positives_for_equally_spaced_360():
|
||||
# 0, 7, 14, ... 353: 360/7 is not an integer, so no 180-degree pairs
|
||||
entries = [_e(float(a)) for a in range(0, 360, 7)]
|
||||
assert summarize_angles(entries)["opposite_pairs"] == []
|
||||
|
||||
|
||||
def test_summary_flags_every_pair_for_equally_spaced_360_with_even_count():
|
||||
entries = [_e(float(a)) for a in range(0, 360, 10)]
|
||||
assert len(summarize_angles(entries)["opposite_pairs"]) == 18
|
||||
|
||||
|
||||
# ── per-line acquisition overrides (step=/exptime=/frames=) ────────────────
|
||||
|
||||
|
||||
def test_overrides_parsed_and_sticky_across_lines(tmp_path):
|
||||
path = _write(
|
||||
tmp_path,
|
||||
"0\n"
|
||||
"10 1\n"
|
||||
"20 1 step=0.5\n"
|
||||
"30 1\n"
|
||||
"40 2 step=2 exptime=5 frames=3\n"
|
||||
"50 2\n",
|
||||
)
|
||||
result = read_angle_file(path)
|
||||
assert result.angles == [
|
||||
_e(0.0),
|
||||
_e(10.0),
|
||||
_e(20.0, step=0.5),
|
||||
_e(30.0, step=0.5),
|
||||
_e(40.0, 2, step=2.0, exp_time=5.0, frames_per_trigger=3),
|
||||
_e(50.0, 2, step=2.0, exp_time=5.0, frames_per_trigger=3),
|
||||
]
|
||||
|
||||
|
||||
def test_overrides_independent_sticky_state(tmp_path):
|
||||
"""Each of step/exptime/frames carries forward independently -- setting
|
||||
one doesn't reset or require the others."""
|
||||
path = _write(tmp_path, "0 1 step=1\n10 1 exptime=2\n20 1 frames=4\n")
|
||||
result = read_angle_file(path)
|
||||
assert result.angles == [
|
||||
_e(0.0, step=1.0),
|
||||
_e(10.0, step=1.0, exp_time=2.0),
|
||||
_e(20.0, step=1.0, exp_time=2.0, frames_per_trigger=4),
|
||||
]
|
||||
|
||||
|
||||
def test_overrides_compose_with_subtomo_and_comma_separators(tmp_path):
|
||||
path = _write(tmp_path, "10, 2, step=0.3\n")
|
||||
result = read_angle_file(path)
|
||||
assert result.angles == [_e(10.0, 2, step=0.3)]
|
||||
|
||||
|
||||
def test_overrides_compose_with_end_sentinel(tmp_path):
|
||||
path = _write(tmp_path, "0 1 step=0.5\n10\nEND\n")
|
||||
result = read_angle_file(path)
|
||||
assert result.angles == [_e(0.0, step=0.5), _e(10.0, step=0.5)]
|
||||
assert result.is_complete is True
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"text, match",
|
||||
[
|
||||
("10 foo=1\n", "unknown key 'foo'"),
|
||||
("10 step=1 step=2\n", "duplicate key 'step'"),
|
||||
("10 step=\n", "malformed"),
|
||||
("10 =5\n", "malformed"),
|
||||
("10 step=abc\n", "invalid step"),
|
||||
("10 frames=abc\n", "invalid frames"),
|
||||
("10 step=0.01\n", r"outside \[0.025, 20\]"),
|
||||
("10 step=21\n", r"outside \[0.025, 20\]"),
|
||||
("10 exptime=0.0001\n", r"outside \[0.001, 100\]"),
|
||||
("10 exptime=200\n", r"outside \[0.001, 100\]"),
|
||||
("10 frames=0\n", r"outside \[1, 100\]"),
|
||||
("10 frames=101\n", r"outside \[1, 100\]"),
|
||||
],
|
||||
)
|
||||
def test_override_errors(tmp_path, text, match):
|
||||
path = _write(tmp_path, text)
|
||||
with pytest.raises(AngleFileError, match=match):
|
||||
read_angle_file(path)
|
||||
|
||||
|
||||
def test_summary_reports_override_count():
|
||||
entries = [_e(0.0), _e(10.0, step=0.5), _e(20.0, exp_time=2.0)]
|
||||
summary = summarize_angles(entries)
|
||||
assert summary["n_overridden"] == 2
|
||||
assert "2 with per-line overrides" in describe_summary(summary)
|
||||
|
||||
|
||||
def test_summary_omits_override_note_when_none_overridden():
|
||||
summary = summarize_angles([_e(0.0), _e(10.0)])
|
||||
assert summary["n_overridden"] == 0
|
||||
assert "overrides" not in describe_summary(summary)
|
||||
@@ -4,6 +4,7 @@ import types
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
import csaxs_bec.bec_ipython_client.plugins.flomni.flomni as flomni_module
|
||||
from csaxs_bec.bec_ipython_client.plugins.flomni.flomni import Flomni, _ProgressProxy
|
||||
|
||||
plan = Flomni._subtomo_angle_plan
|
||||
@@ -11,6 +12,23 @@ plan = Flomni._subtomo_angle_plan
|
||||
STEPSIZES = [10.0, 7.0, 25.0, 12.5]
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _fail_fast_on_unexpected_sleep(monkeypatch):
|
||||
"""Guard against a tomo_type-4 fixture missing its 'END' marker turning
|
||||
a test into a real wait-and-poll hang: any call to time.sleep() from
|
||||
flomni.py (only reached by the angle-file wait loop) fails the test
|
||||
immediately instead of blocking. Tests that intentionally exercise the
|
||||
wait loop override this with their own monkeypatch.setattr."""
|
||||
|
||||
def _unexpected_sleep(seconds):
|
||||
pytest.fail(
|
||||
f"tomo_scan() tried to sleep({seconds}) -- likely an angle file "
|
||||
"fixture missing its 'END' marker."
|
||||
)
|
||||
|
||||
monkeypatch.setattr(flomni_module.time, "sleep", _unexpected_sleep)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("stepsize", STEPSIZES)
|
||||
def test_180_mode_regression(stepsize):
|
||||
"""180-degree mode must be byte-for-byte identical to the original
|
||||
@@ -133,6 +151,37 @@ def test_resume_round_trip(tomo_angle_range, stepsize):
|
||||
assert offset == i, f"n={n} i={i} range={tomo_angle_range} stepsize={stepsize}"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("stepsize", STEPSIZES)
|
||||
def test_always_forward_180_mode(stepsize):
|
||||
"""always_forward: every sub-tomogram ascends, covers exactly the same
|
||||
angle set as the default alternating scheme, and resume round-trips."""
|
||||
for n in range(1, 9):
|
||||
default, _, N, _ = plan(n, 180, stepsize)
|
||||
fwd, _, _, _ = plan(n, 180, stepsize, always_forward=True)
|
||||
assert np.all(np.diff(fwd) > 0)
|
||||
assert np.allclose(np.sort(default), np.sort(fwd), atol=1e-9)
|
||||
for i in range(N):
|
||||
_, offset, _, _ = plan(n, 180, stepsize, start_angle=fwd[i], always_forward=True)
|
||||
assert offset == i
|
||||
|
||||
|
||||
def test_always_forward_ignored_in_360_mode():
|
||||
for n in range(1, 9):
|
||||
a, _, _, _ = plan(n, 360, 10.0)
|
||||
b, _, _, _ = plan(n, 360, 10.0, always_forward=True)
|
||||
assert np.array_equal(a, b)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("always_forward", [False, True])
|
||||
@pytest.mark.parametrize("n", range(1, 9))
|
||||
def test_subtomo_starts_near_zero_180(n, always_forward):
|
||||
flomni = Flomni.__new__(Flomni)
|
||||
flomni.client = types.SimpleNamespace(
|
||||
get_global_var=lambda name: {"tomo_angle_range": 180, "tomo_type1_always_forward": always_forward}.get(name)
|
||||
)
|
||||
assert flomni._subtomo_starts_near_zero(n) is (always_forward or bool(n % 2))
|
||||
|
||||
|
||||
def _actual_grid(stepsize):
|
||||
N = int(180.0 / stepsize)
|
||||
step = 180.0 / N
|
||||
@@ -257,3 +306,459 @@ def test_golden_fresh_start_still_fires_zero_deg_shot_at_natural_transition(monk
|
||||
flomni.tomo_scan() # projection_number=None -- a fresh, non-resumed run
|
||||
|
||||
assert (0, 3) in recorded, "a fresh (non-resume) run must still fire the shot at the natural transition"
|
||||
|
||||
|
||||
# ── tomo_type 4: angles read from file ──────────────────────────────────────
|
||||
|
||||
|
||||
def make_flomni_for_angle_file(monkeypatch, tmp_path, content: str) -> Flomni:
|
||||
flomni = make_flomni_for_golden_resume(monkeypatch, tomo_type=4)
|
||||
flomni.golden_projections_at_0_deg_for_damage_estimation = 0
|
||||
flomni.OMNYTools = types.SimpleNamespace(
|
||||
printgreenbold=lambda msg: None, printredbold=lambda msg: None
|
||||
)
|
||||
path = tmp_path / "angles.txt"
|
||||
path.write_text(content)
|
||||
flomni.tomo_angle_file = str(path)
|
||||
return flomni
|
||||
|
||||
|
||||
def test_tomo_type_4_accepted_and_5_rejected():
|
||||
flomni = object.__new__(Flomni)
|
||||
flomni.client = _FakeClient()
|
||||
flomni.tomo_type = 4
|
||||
assert flomni.tomo_type == 4
|
||||
with pytest.raises(ValueError):
|
||||
flomni.tomo_type = 5
|
||||
|
||||
|
||||
def test_tomo_angle_file_setter_normalizes_without_validating(tmp_path, monkeypatch):
|
||||
flomni = object.__new__(Flomni)
|
||||
flomni.client = _FakeClient()
|
||||
monkeypatch.setenv("HOME", str(tmp_path))
|
||||
# nonexistent file must be accepted: tomo_queue_execute() setattr()s
|
||||
# every param, also for jobs that don't use tomo_type 4
|
||||
flomni.tomo_angle_file = "~/does_not_exist.txt"
|
||||
assert flomni.tomo_angle_file == str(tmp_path / "does_not_exist.txt")
|
||||
flomni.tomo_angle_file = ""
|
||||
assert flomni.tomo_angle_file == ""
|
||||
|
||||
|
||||
def test_angle_file_scan_follows_file_order_and_subtomos(monkeypatch, tmp_path):
|
||||
flomni = make_flomni_for_angle_file(monkeypatch, tmp_path, "10 1\n170 1\n270 2\n5.5 3\nEND\n")
|
||||
flomni.tomo_angle_range = 180 # must not restrict type-4 angles
|
||||
recorded = []
|
||||
flomni._tomo_scan_at_angle = lambda angle, subtomo: recorded.append((angle, subtomo))
|
||||
|
||||
flomni.tomo_scan()
|
||||
|
||||
# (0, 1): unconditional 0-deg reference shot at the start of a new tomogram
|
||||
assert recorded == [(0, 1), (10.0, 1), (170.0, 1), (270.0, 2), (5.5, 3)]
|
||||
assert flomni.progress["total_projections"] == 4
|
||||
assert flomni.progress["tomo_type"] == "Angles from file"
|
||||
|
||||
|
||||
def test_angle_file_resume_starts_at_projection_index(monkeypatch, tmp_path):
|
||||
flomni = make_flomni_for_angle_file(monkeypatch, tmp_path, "10\n20\n30\n40\nEND\n")
|
||||
recorded = []
|
||||
flomni._tomo_scan_at_angle = lambda angle, subtomo: recorded.append((angle, subtomo))
|
||||
|
||||
flomni.tomo_scan(projection_number=2)
|
||||
|
||||
assert recorded == [(30.0, 1), (40.0, 1)]
|
||||
|
||||
|
||||
def test_angle_file_resume_rereads_edited_file(monkeypatch, tmp_path):
|
||||
flomni = make_flomni_for_angle_file(monkeypatch, tmp_path, "10\n20\n30\nEND\n")
|
||||
(tmp_path / "angles.txt").write_text("10\n20\n33\n44\nEND\n")
|
||||
recorded = []
|
||||
flomni._tomo_scan_at_angle = lambda angle, subtomo: recorded.append((angle, subtomo))
|
||||
|
||||
flomni.tomo_scan(projection_number=2)
|
||||
|
||||
assert recorded == [(33.0, 1), (44.0, 1)]
|
||||
|
||||
|
||||
def test_angle_file_resume_past_end_of_file_raises(monkeypatch, tmp_path):
|
||||
from csaxs_bec.bec_ipython_client.plugins.flomni.flomni import FlomniError
|
||||
|
||||
flomni = make_flomni_for_angle_file(monkeypatch, tmp_path, "10\n20\n")
|
||||
flomni._tomo_scan_at_angle = lambda angle, subtomo: pytest.fail("must not acquire")
|
||||
with pytest.raises(FlomniError, match="only 2 angles"):
|
||||
flomni.tomo_scan(projection_number=2)
|
||||
|
||||
|
||||
def test_angle_file_missing_raises_before_any_acquisition(monkeypatch, tmp_path):
|
||||
from csaxs_bec.bec_ipython_client.plugins.flomni.flomni import FlomniError
|
||||
|
||||
flomni = make_flomni_for_angle_file(monkeypatch, tmp_path, "10\n")
|
||||
flomni.tomo_angle_file = str(tmp_path / "gone.txt")
|
||||
calls = []
|
||||
flomni.add_sample_database = lambda *a, **k: calls.append("db")
|
||||
flomni.collect_empty_frames = lambda: calls.append("empty")
|
||||
flomni._tomo_scan_at_angle = lambda angle, subtomo: calls.append("scan")
|
||||
|
||||
with pytest.raises(FlomniError, match="Cannot read"):
|
||||
flomni.tomo_scan()
|
||||
assert calls == []
|
||||
|
||||
|
||||
def test_angle_file_ignores_golden_zero_deg_damage_flag(monkeypatch, tmp_path):
|
||||
"""No extra 0-deg shots at sub-tomogram transitions for type 4 -- only
|
||||
the unconditional reference shot at the start of a new tomogram."""
|
||||
flomni = make_flomni_for_angle_file(monkeypatch, tmp_path, "10 1\n20 2\n30 3\n40 5\nEND\n")
|
||||
flomni.golden_projections_at_0_deg_for_damage_estimation = 1
|
||||
recorded = []
|
||||
flomni._tomo_scan_at_angle = lambda angle, subtomo: recorded.append((angle, subtomo))
|
||||
|
||||
flomni.tomo_scan()
|
||||
|
||||
assert recorded == [(0, 1), (10.0, 1), (20.0, 2), (30.0, 3), (40.0, 5)]
|
||||
|
||||
|
||||
def test_max_scan_angle_is_362_only_for_type_4():
|
||||
flomni = object.__new__(Flomni)
|
||||
flomni.client = _FakeClient()
|
||||
flomni.tomo_angle_range = 180
|
||||
flomni.tomo_type = 4
|
||||
assert flomni._max_scan_angle() == 362
|
||||
flomni.tomo_type = 1
|
||||
assert flomni._max_scan_angle() == 180
|
||||
|
||||
|
||||
def test_min_scan_angle_is_minus_2_only_for_type_4():
|
||||
flomni = object.__new__(Flomni)
|
||||
flomni.client = _FakeClient()
|
||||
flomni.tomo_type = 4
|
||||
assert flomni._min_scan_angle() == -2
|
||||
flomni.tomo_type = 1
|
||||
assert flomni._min_scan_angle() == 0
|
||||
|
||||
|
||||
def test_angle_file_accepts_physical_overtravel_range(monkeypatch, tmp_path):
|
||||
"""Angles down to -2 and up to 362 (the rotation stage's physical
|
||||
overtravel range) must actually reach acquisition for tomo_type 4, not
|
||||
just pass angle-file parsing -- _tomo_scan_at_angle()'s own gate has to
|
||||
be widened to match, or they'd parse fine but then be silently skipped
|
||||
with no acquisition and no error."""
|
||||
flomni = make_flomni_for_angle_file(monkeypatch, tmp_path, "-2\n-0.1\n361\n362\nEND\n")
|
||||
flomni._write_tomo_scan_number = lambda *a, **k: None
|
||||
flomni._log_projection_timing = lambda **k: None
|
||||
recorded = []
|
||||
flomni._at_each_angle = lambda angle, **overrides: recorded.append(angle)
|
||||
|
||||
flomni.tomo_scan() # exercises the real _tomo_scan_at_angle(), not a stub
|
||||
|
||||
# 0: unconditional 0-deg reference shot at the start of a new tomogram
|
||||
assert recorded == [0, -2.0, -0.1, 361.0, 362.0]
|
||||
|
||||
|
||||
# ── tomo_type 4: live append -- wait at EOF without 'END', poll, pick up ────
|
||||
|
||||
|
||||
def test_angle_file_waits_then_picks_up_appended_lines(monkeypatch, tmp_path):
|
||||
flomni = make_flomni_for_angle_file(monkeypatch, tmp_path, "5\n10\n")
|
||||
recorded = []
|
||||
flomni._tomo_scan_at_angle = lambda angle, subtomo: recorded.append((angle, subtomo))
|
||||
|
||||
angle_path = tmp_path / "angles.txt"
|
||||
sleep_calls = []
|
||||
|
||||
def _sleep(seconds):
|
||||
sleep_calls.append(seconds)
|
||||
angle_path.write_text("5\n10\n20\nEND\n")
|
||||
|
||||
monkeypatch.setattr(flomni_module.time, "sleep", _sleep)
|
||||
|
||||
flomni.tomo_scan()
|
||||
|
||||
# (0, 1): unconditional 0-deg reference shot at the start of a new tomogram
|
||||
assert recorded == [(0, 1), (5.0, 1), (10.0, 1), (20.0, 1)]
|
||||
assert len(sleep_calls) == 1
|
||||
assert flomni.progress["total_projections"] == 3
|
||||
|
||||
|
||||
def test_angle_file_edit_of_already_acquired_line_while_waiting_raises(monkeypatch, tmp_path):
|
||||
from csaxs_bec.bec_ipython_client.plugins.flomni.flomni import FlomniError
|
||||
|
||||
flomni = make_flomni_for_angle_file(monkeypatch, tmp_path, "5\n10\n")
|
||||
flomni._tomo_scan_at_angle = lambda angle, subtomo: None
|
||||
|
||||
angle_path = tmp_path / "angles.txt"
|
||||
|
||||
def _sleep(seconds):
|
||||
angle_path.write_text("5\n99\n") # already-acquired index 1 (10) changed
|
||||
|
||||
monkeypatch.setattr(flomni_module.time, "sleep", _sleep)
|
||||
|
||||
with pytest.raises(FlomniError, match="changed"):
|
||||
flomni.tomo_scan()
|
||||
|
||||
|
||||
def test_angle_file_keyboard_interrupt_while_waiting_raises_flomnierror(monkeypatch, tmp_path):
|
||||
from csaxs_bec.bec_ipython_client.plugins.flomni.flomni import FlomniError
|
||||
|
||||
flomni = make_flomni_for_angle_file(monkeypatch, tmp_path, "5\n10\n")
|
||||
flomni._tomo_scan_at_angle = lambda angle, subtomo: None
|
||||
|
||||
def _sleep(seconds):
|
||||
raise KeyboardInterrupt
|
||||
|
||||
monkeypatch.setattr(flomni_module.time, "sleep", _sleep)
|
||||
|
||||
with pytest.raises(FlomniError, match="aborted while waiting"):
|
||||
flomni.tomo_scan()
|
||||
|
||||
|
||||
def test_angle_file_heartbeat_refreshed_while_waiting(monkeypatch, tmp_path):
|
||||
flomni = make_flomni_for_angle_file(monkeypatch, tmp_path, "5\n")
|
||||
recorded_heartbeats = []
|
||||
flomni._tomo_scan_at_angle = lambda angle, subtomo: recorded_heartbeats.append(
|
||||
flomni.progress.get("heartbeat")
|
||||
)
|
||||
|
||||
angle_path = tmp_path / "angles.txt"
|
||||
|
||||
def _sleep(seconds):
|
||||
angle_path.write_text("5\n10\nEND\n")
|
||||
|
||||
monkeypatch.setattr(flomni_module.time, "sleep", _sleep)
|
||||
|
||||
flomni.tomo_scan()
|
||||
|
||||
# (0, 1) reference shot, then (5.0, 1) -- both before any wait tick, so no
|
||||
# heartbeat has been written yet (the stub above never sets one itself).
|
||||
assert recorded_heartbeats[0] is None
|
||||
assert recorded_heartbeats[1] is None
|
||||
# (10.0, 1) is only reached after the wait tick refreshed the heartbeat.
|
||||
assert recorded_heartbeats[2] is not None
|
||||
|
||||
|
||||
# ── tomo_type 4: per-line acquisition overrides (step=/exptime=/frames=) ───
|
||||
|
||||
|
||||
def test_angle_file_overrides_reach_tomo_scan_at_angle_and_are_sticky(monkeypatch, tmp_path):
|
||||
flomni = make_flomni_for_angle_file(
|
||||
monkeypatch,
|
||||
tmp_path,
|
||||
"10\n20 1 step=0.5\n30 1\n40 1 step=2 exptime=5 frames=3\nEND\n",
|
||||
)
|
||||
recorded = []
|
||||
flomni._tomo_scan_at_angle = lambda angle, subtomo, **overrides: recorded.append(
|
||||
(angle, subtomo, overrides)
|
||||
)
|
||||
|
||||
flomni.tomo_scan()
|
||||
|
||||
assert recorded == [
|
||||
(0, 1, {}), # unconditional 0-deg reference shot at tomogram start
|
||||
(10.0, 1, {}),
|
||||
(20.0, 1, {"step": 0.5}),
|
||||
(30.0, 1, {"step": 0.5}), # sticky: step carries forward
|
||||
(40.0, 1, {"step": 2.0, "exp_time": 5.0, "frames_per_trigger": 3}),
|
||||
]
|
||||
|
||||
|
||||
def test_tomo_scan_at_angle_forwards_overrides_to_at_each_angle(monkeypatch, tmp_path):
|
||||
flomni = make_flomni_for_angle_file(monkeypatch, tmp_path, "10\nEND\n")
|
||||
recorded = []
|
||||
flomni._at_each_angle = lambda angle, **overrides: recorded.append((angle, overrides))
|
||||
|
||||
flomni._tomo_scan_at_angle(10.0, 1)
|
||||
flomni._tomo_scan_at_angle(10.0, 1, step=0.5, exp_time=2.0, frames_per_trigger=4)
|
||||
|
||||
assert recorded == [
|
||||
(10.0, {}),
|
||||
(10.0, {"step": 0.5, "exp_time": 2.0, "frames_per_trigger": 4}),
|
||||
]
|
||||
|
||||
|
||||
def test_at_each_angle_forwards_overrides_to_tomo_scan_projection(monkeypatch, tmp_path):
|
||||
flomni = make_flomni_for_angle_file(monkeypatch, tmp_path, "10\nEND\n")
|
||||
flomni.single_point_instead_of_fermat_scan = False
|
||||
recorded = []
|
||||
flomni.tomo_scan_projection = lambda angle, _internal=False, **overrides: recorded.append(
|
||||
(angle, overrides)
|
||||
)
|
||||
|
||||
flomni._at_each_angle(10.0, step=0.5, frames_per_trigger=4)
|
||||
|
||||
assert recorded == [(10.0, {"step": 0.5, "exp_time": None, "frames_per_trigger": 4})]
|
||||
|
||||
|
||||
def test_at_each_angle_forwards_overrides_to_tomo_acquire_at_angle_in_single_point_mode(
|
||||
monkeypatch, tmp_path
|
||||
):
|
||||
flomni = make_flomni_for_angle_file(monkeypatch, tmp_path, "10\nEND\n")
|
||||
flomni.single_point_instead_of_fermat_scan = True
|
||||
recorded = []
|
||||
flomni.tomo_acquire_at_angle = (
|
||||
lambda angle, frames_per_trigger=None, exp_time=None: recorded.append(
|
||||
(angle, frames_per_trigger, exp_time)
|
||||
)
|
||||
)
|
||||
|
||||
# step is accepted but silently has no effect in single-point mode (no
|
||||
# Fermat scan to apply a step size to).
|
||||
flomni._at_each_angle(10.0, step=0.5, exp_time=2.0, frames_per_trigger=4)
|
||||
|
||||
assert recorded == [(10.0, 4, 2.0)]
|
||||
|
||||
|
||||
def test_hook_active_with_overrides_warns_once_at_scan_start(monkeypatch, tmp_path):
|
||||
flomni = make_flomni_for_angle_file(
|
||||
monkeypatch, tmp_path, "10 1 step=0.5\n20\nEND\n"
|
||||
)
|
||||
flomni.at_each_angle_hook = "some_hook"
|
||||
flomni._tomo_scan_at_angle = lambda angle, subtomo, **overrides: None
|
||||
warnings = []
|
||||
flomni.OMNYTools.printredbold = lambda msg: warnings.append(msg)
|
||||
|
||||
flomni.tomo_scan()
|
||||
|
||||
assert len(warnings) == 1
|
||||
assert "hook is active" in warnings[0]
|
||||
|
||||
|
||||
def test_hook_active_without_overrides_does_not_warn(monkeypatch, tmp_path):
|
||||
flomni = make_flomni_for_angle_file(monkeypatch, tmp_path, "10\n20\nEND\n")
|
||||
flomni.at_each_angle_hook = "some_hook"
|
||||
flomni._tomo_scan_at_angle = lambda angle, subtomo, **overrides: None
|
||||
warnings = []
|
||||
flomni.OMNYTools.printredbold = lambda msg: warnings.append(msg)
|
||||
|
||||
flomni.tomo_scan()
|
||||
|
||||
assert warnings == []
|
||||
|
||||
|
||||
def test_hook_warning_fires_again_only_for_newly_appended_overridden_lines(monkeypatch, tmp_path):
|
||||
flomni = make_flomni_for_angle_file(monkeypatch, tmp_path, "10\n")
|
||||
flomni.at_each_angle_hook = "some_hook"
|
||||
flomni._tomo_scan_at_angle = lambda angle, subtomo, **overrides: None
|
||||
warnings = []
|
||||
flomni.OMNYTools.printredbold = lambda msg: warnings.append(msg)
|
||||
|
||||
angle_path = tmp_path / "angles.txt"
|
||||
|
||||
def _sleep(seconds):
|
||||
angle_path.write_text("10\n20 step=0.5\nEND\n")
|
||||
|
||||
monkeypatch.setattr(flomni_module.time, "sleep", _sleep)
|
||||
|
||||
flomni.tomo_scan()
|
||||
|
||||
# No overrides at scan start -- no warning yet; exactly one once the
|
||||
# overridden line is picked up mid-scan.
|
||||
assert len(warnings) == 1
|
||||
assert "hook is active" in warnings[0]
|
||||
|
||||
|
||||
# ── progress is reset before any pre-flight gate can abort a new scan ──────
|
||||
|
||||
|
||||
def test_new_scan_resets_progress_before_eye_optics_gate_declines(monkeypatch, tmp_path):
|
||||
"""A stale progress dict from a long-finished scan must not survive a
|
||||
declined *new* scan attempt (e.g. the eye/optics-in prompt, which
|
||||
disabling rt feedback can trigger) -- regression for a report where the
|
||||
progress widget kept showing a days-old scan after a failed start."""
|
||||
flomni = make_flomni_for_angle_file(monkeypatch, tmp_path, "10\nEND\n")
|
||||
flomni.progress.update(
|
||||
projection=196,
|
||||
total_projections=288,
|
||||
angle=196.2,
|
||||
subtomo=6,
|
||||
tomo_type="Equally spaced sub-tomograms",
|
||||
tomo_start_time="2026-09-25T14:57:28",
|
||||
heartbeat="2026-09-25T14:57:30",
|
||||
)
|
||||
flomni._check_eye_out_and_optics_in = lambda: False
|
||||
flomni.OMNYTools.yesno = lambda *a, **k: False # decline "Shall I continue?"
|
||||
flomni._tomo_scan_at_angle = lambda angle, subtomo, **overrides: pytest.fail(
|
||||
"must not acquire -- the gate declined"
|
||||
)
|
||||
|
||||
flomni.tomo_scan() # returns early, no exception
|
||||
|
||||
assert flomni.progress["projection"] == 0
|
||||
assert flomni.progress["total_projections"] == 1
|
||||
assert flomni.progress["angle"] == 0
|
||||
assert flomni.progress["subtomo"] == 0
|
||||
assert flomni.progress["tomo_type"] == 0
|
||||
assert flomni.progress["tomo_start_time"] is None
|
||||
assert flomni.progress["heartbeat"] is None
|
||||
|
||||
|
||||
def test_new_scan_resets_progress_before_angle_file_error(monkeypatch, tmp_path):
|
||||
"""Same as above, but the abort comes from the tomo_type-4 upfront angle-
|
||||
file validation, which runs even earlier than the eye/optics gate."""
|
||||
from csaxs_bec.bec_ipython_client.plugins.flomni.flomni import FlomniError
|
||||
|
||||
flomni = make_flomni_for_angle_file(monkeypatch, tmp_path, "10\nEND\n")
|
||||
flomni.tomo_angle_file = str(tmp_path / "gone.txt")
|
||||
flomni.progress.update(projection=42, tomo_start_time="2026-09-25T14:57:28")
|
||||
|
||||
with pytest.raises(FlomniError, match="Cannot read"):
|
||||
flomni.tomo_scan()
|
||||
|
||||
assert flomni.progress["projection"] == 0
|
||||
assert flomni.progress["tomo_start_time"] is None
|
||||
|
||||
|
||||
def test_resume_does_not_reset_progress(monkeypatch, tmp_path):
|
||||
"""A *resume* (projection_number given) must not wipe progress -- only a
|
||||
genuinely new scan start does."""
|
||||
flomni = make_flomni_for_angle_file(monkeypatch, tmp_path, "10\n20\n30\nEND\n")
|
||||
flomni.progress.update(estimated_remaining_time=42.0)
|
||||
reset_calls = []
|
||||
monkeypatch.setattr(flomni._progress_proxy, "reset", lambda: reset_calls.append(1))
|
||||
flomni._tomo_scan_at_angle = lambda angle, subtomo, **overrides: None
|
||||
|
||||
flomni.tomo_scan(projection_number=1)
|
||||
|
||||
assert reset_calls == []
|
||||
assert flomni.progress["estimated_remaining_time"] == 42.0
|
||||
|
||||
|
||||
def test_angle_file_progress_shows_type_and_total_before_the_zero_deg_shot(monkeypatch, tmp_path):
|
||||
flomni = make_flomni_for_angle_file(monkeypatch, tmp_path, "10\n20\nEND\n")
|
||||
seen = []
|
||||
flomni._tomo_scan_at_angle = lambda angle, subtomo, **kw: seen.append(
|
||||
(angle, flomni.progress["tomo_type"], flomni.progress["total_projections"])
|
||||
)
|
||||
|
||||
flomni.tomo_scan()
|
||||
|
||||
assert seen[0] == (0, "Angles from file", 2) # the initial 0-deg shot
|
||||
|
||||
|
||||
def test_flomni_gui_progress_survives_zero_total(monkeypatch):
|
||||
"""total_projections == 0 (type 4 file without 'END' yet, or endless
|
||||
golden ratio) must not make the GUI update raise (it was swallowed,
|
||||
leaving the progress window frozen on the reset defaults)."""
|
||||
from csaxs_bec.bec_ipython_client.plugins.flomni.gui_tools import flomniGuiTools
|
||||
|
||||
labels, ring_values = [], []
|
||||
ring = types.SimpleNamespace(set_value=ring_values.append)
|
||||
gui = object.__new__(flomniGuiTools)
|
||||
gui.progressbar = types.SimpleNamespace(
|
||||
rings=[ring, ring], set_center_label=labels.append
|
||||
)
|
||||
gui.progress = {
|
||||
"projection": 3,
|
||||
"total_projections": 0,
|
||||
"subtomo_projection": 1,
|
||||
"subtomo_total_projections": 2,
|
||||
"tomo_type": "Angles from file",
|
||||
"angle": 10.0,
|
||||
"subtomo": 1,
|
||||
"tomo_start_time": None,
|
||||
"estimated_remaining_time": None,
|
||||
"estimated_finish_time": None,
|
||||
}
|
||||
gui._describe_active_hook = lambda: None
|
||||
|
||||
gui._flomnigui_update_progress()
|
||||
|
||||
assert ring_values == [0.0, 50.0]
|
||||
assert "Angles from file" in labels[0]
|
||||
@@ -0,0 +1,271 @@
|
||||
"""tomo_type 4 (angles from a file) on LamNI -- the shared logic itself is
|
||||
covered by test_flomni_tomo_angles.py / test_angle_file.py; these tests pin
|
||||
the LamNI-specific wiring (setter, gates, overrides threading, no 180-deg
|
||||
warning, progress reset, resume, PDF report, queue job line)."""
|
||||
|
||||
import builtins
|
||||
import types
|
||||
|
||||
import pytest
|
||||
|
||||
import csaxs_bec.bec_ipython_client.plugins.LamNI.lamni as lamni_module
|
||||
from csaxs_bec.bec_ipython_client.plugins.LamNI.lamni import LamNI, LamNIError, _ProgressProxy
|
||||
|
||||
|
||||
class _FakeClient:
|
||||
def __init__(self):
|
||||
self._vars = {}
|
||||
|
||||
def get_global_var(self, key):
|
||||
return self._vars.get(key)
|
||||
|
||||
def set_global_var(self, key, value):
|
||||
self._vars[key] = value
|
||||
|
||||
|
||||
class _FakeContextManager:
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *exc_info):
|
||||
return False
|
||||
|
||||
|
||||
class _FakeScans:
|
||||
dataset_id_on_hold = _FakeContextManager()
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _fail_fast_on_unexpected_sleep(monkeypatch):
|
||||
"""A fixture file without 'END' would otherwise hang in the wait loop."""
|
||||
|
||||
def _unexpected_sleep(seconds):
|
||||
pytest.fail(f"tomo_scan() tried to sleep({seconds}) -- angle file missing 'END'?")
|
||||
|
||||
monkeypatch.setattr(lamni_module.time, "sleep", _unexpected_sleep)
|
||||
|
||||
|
||||
def make_lamni_for_angle_file(monkeypatch, tmp_path, content: str) -> LamNI:
|
||||
obj = object.__new__(LamNI)
|
||||
obj.client = _FakeClient()
|
||||
obj._progress_proxy = _ProgressProxy(obj.client)
|
||||
obj.tomo_type = 4
|
||||
obj.tomo_id = -1
|
||||
obj.special_angles = []
|
||||
obj._current_special_angles = []
|
||||
obj.at_each_angle_hook = None
|
||||
obj.corr_pos_x = [1.0] # skip the fine-alignment gate
|
||||
obj._check_eye_out_and_optics_in = lambda: True
|
||||
obj.lamnigui_show_progress = lambda: None
|
||||
obj._lamnigui_update_progress = lambda: None
|
||||
obj.add_sample_database = lambda *a, **k: 0
|
||||
obj.write_pdf_report = lambda: None
|
||||
obj._print_progress = lambda: None
|
||||
obj._log_tomogram_timing = lambda: None
|
||||
obj._build_timing_lines = lambda: ["a", "b", "c"]
|
||||
obj._active_hook_source = lambda: None
|
||||
obj.scilog = []
|
||||
obj.write_to_scilog = lambda content, tags=None: obj.scilog.append(content)
|
||||
obj.printed_red = []
|
||||
obj.OMNYTools = types.SimpleNamespace(
|
||||
printgreenbold=lambda msg: None, printredbold=lambda msg: obj.printed_red.append(msg)
|
||||
)
|
||||
fake_bec = types.SimpleNamespace(
|
||||
active_account="",
|
||||
queue=types.SimpleNamespace(next_scan_number=1),
|
||||
builtin_actors=types.SimpleNamespace(
|
||||
scan_interlock=types.SimpleNamespace(trigger_setting=None, enabled=False)
|
||||
),
|
||||
)
|
||||
monkeypatch.setitem(builtins.__dict__, "bec", fake_bec)
|
||||
monkeypatch.setitem(builtins.__dict__, "scans", _FakeScans())
|
||||
monkeypatch.setitem(builtins.__dict__, "dev", types.SimpleNamespace())
|
||||
monkeypatch.setattr(lamni_module, "filters_out_of_beam", lambda dev: (True, []))
|
||||
path = tmp_path / "angles.txt"
|
||||
path.write_text(content)
|
||||
obj.tomo_angle_file = str(path)
|
||||
return obj
|
||||
|
||||
|
||||
def test_tomo_type_4_accepted_and_5_rejected():
|
||||
obj = object.__new__(LamNI)
|
||||
obj.client = _FakeClient()
|
||||
obj.tomo_type = 4
|
||||
assert obj.tomo_type == 4
|
||||
with pytest.raises(ValueError):
|
||||
obj.tomo_type = 5
|
||||
|
||||
|
||||
def test_tomo_angle_file_is_a_queue_param():
|
||||
assert "tomo_angle_file" in LamNI._TOMO_SCAN_PARAM_NAMES
|
||||
|
||||
|
||||
def test_scan_follows_file_order_and_reports_progress(monkeypatch, tmp_path):
|
||||
lamni = make_lamni_for_angle_file(monkeypatch, tmp_path, "10 1\n170 1\n270 2\n362 3\nEND\n")
|
||||
recorded = []
|
||||
lamni._tomo_scan_at_angle = lambda angle, subtomo: recorded.append((angle, subtomo))
|
||||
|
||||
lamni.tomo_scan(force=True)
|
||||
|
||||
assert recorded == [(0, 1), (10.0, 1), (170.0, 1), (270.0, 2), (362.0, 3)]
|
||||
assert lamni.progress["total_projections"] == 4
|
||||
assert lamni.progress["tomo_type"] == "Angles from file"
|
||||
|
||||
|
||||
def test_no_180_deg_opposite_warning_for_laminography(monkeypatch, tmp_path):
|
||||
lamni = make_lamni_for_angle_file(monkeypatch, tmp_path, "10\n190\n10\nEND\n")
|
||||
lamni._tomo_scan_at_angle = lambda angle, subtomo: None
|
||||
|
||||
lamni.tomo_scan(force=True)
|
||||
|
||||
assert lamni.printed_red == []
|
||||
|
||||
|
||||
def test_resume_starts_at_projection_index(monkeypatch, tmp_path):
|
||||
lamni = make_lamni_for_angle_file(monkeypatch, tmp_path, "10\n20\n30\n40\nEND\n")
|
||||
recorded = []
|
||||
lamni._tomo_scan_at_angle = lambda angle, subtomo: recorded.append((angle, subtomo))
|
||||
|
||||
lamni.tomo_scan(projection_number=2, force=True)
|
||||
|
||||
assert recorded == [(30.0, 1), (40.0, 1)]
|
||||
|
||||
|
||||
def test_resume_past_end_of_file_raises(monkeypatch, tmp_path):
|
||||
lamni = make_lamni_for_angle_file(monkeypatch, tmp_path, "10\n20\nEND\n")
|
||||
lamni._tomo_scan_at_angle = lambda angle, subtomo: pytest.fail("must not acquire")
|
||||
with pytest.raises(LamNIError, match="only 2 angles"):
|
||||
lamni.tomo_scan(projection_number=2, force=True)
|
||||
|
||||
|
||||
def test_missing_file_raises_before_gates_and_acquisition(monkeypatch, tmp_path):
|
||||
lamni = make_lamni_for_angle_file(monkeypatch, tmp_path, "10\nEND\n")
|
||||
lamni.tomo_angle_file = str(tmp_path / "gone.txt")
|
||||
calls = []
|
||||
lamni._check_eye_out_and_optics_in = lambda: calls.append("gate") or True
|
||||
lamni.add_sample_database = lambda *a, **k: calls.append("db")
|
||||
lamni._tomo_scan_at_angle = lambda angle, subtomo: calls.append("scan")
|
||||
|
||||
with pytest.raises(LamNIError, match="Cannot read"):
|
||||
lamni.tomo_scan(force=True)
|
||||
assert calls == []
|
||||
|
||||
|
||||
def test_progress_reset_even_if_gate_declines(monkeypatch, tmp_path):
|
||||
lamni = make_lamni_for_angle_file(monkeypatch, tmp_path, "10\nEND\n")
|
||||
lamni.progress["projection"] = 99
|
||||
lamni.progress["angle"] = 123
|
||||
lamni._check_eye_out_and_optics_in = lambda: False
|
||||
lamni._confirm_sequence_override = lambda warning, force: False
|
||||
lamni._tomo_scan_at_angle = lambda angle, subtomo: pytest.fail("must not acquire")
|
||||
|
||||
lamni.tomo_scan()
|
||||
|
||||
assert lamni.progress["projection"] == 0
|
||||
assert lamni.progress["angle"] == 0
|
||||
|
||||
|
||||
def test_per_line_overrides_are_sticky_and_passed_to_scan_at_angle(monkeypatch, tmp_path):
|
||||
lamni = make_lamni_for_angle_file(
|
||||
monkeypatch, tmp_path, "10 1\n20 1 step=0.5 exptime=2 frames=3\n30 1 step=0.7\nEND\n"
|
||||
)
|
||||
calls = []
|
||||
lamni._tomo_scan_at_angle = lambda angle, subtomo, **kw: calls.append((angle, kw))
|
||||
|
||||
lamni.tomo_scan(force=True)
|
||||
|
||||
assert calls == [
|
||||
(0, {}),
|
||||
(10.0, {}),
|
||||
(20.0, {"step": 0.5, "exp_time": 2.0, "frames_per_trigger": 3}),
|
||||
(30.0, {"step": 0.7, "exp_time": 2.0, "frames_per_trigger": 3}),
|
||||
]
|
||||
|
||||
|
||||
def test_scan_at_angle_forwards_overrides_to_at_each_angle(monkeypatch):
|
||||
obj = object.__new__(LamNI)
|
||||
obj.client = _FakeClient()
|
||||
obj._progress_proxy = _ProgressProxy(obj.client)
|
||||
obj.tomo_type = 4
|
||||
obj.special_angles = []
|
||||
obj._current_special_angles = []
|
||||
obj._print_progress = lambda: None
|
||||
obj._write_tomo_scan_number = lambda *a: None
|
||||
obj._log_projection_timing = lambda **kw: None
|
||||
obj.lamni_stitch_x = 0
|
||||
obj.lamni_stitch_y = 0
|
||||
obj.tomo_countingtime = 0.1
|
||||
obj.frames_per_trigger = 1
|
||||
fake_bec = types.SimpleNamespace(queue=types.SimpleNamespace(next_scan_number=1))
|
||||
monkeypatch.setitem(builtins.__dict__, "bec", fake_bec)
|
||||
monkeypatch.setattr(lamni_module, "bec", fake_bec, raising=False)
|
||||
calls = []
|
||||
obj._at_each_angle = lambda angle, **kw: calls.append((angle, kw))
|
||||
|
||||
obj._tomo_scan_at_angle(-1.5, 1, step=0.3) # in range only for type 4
|
||||
obj._tomo_scan_at_angle(361.0, 1)
|
||||
obj.tomo_type = 1
|
||||
obj._tomo_scan_at_angle(-1.5, 1) # outside 0..360 -> skipped
|
||||
|
||||
assert calls == [(-1.5, {"step": 0.3}), (361.0, {})]
|
||||
|
||||
|
||||
def test_scan_projection_uses_overrides_else_globals(monkeypatch):
|
||||
obj = object.__new__(LamNI)
|
||||
obj.client = _FakeClient()
|
||||
obj.tomo_shellstep = 1.0
|
||||
obj.tomo_countingtime = 0.1
|
||||
obj.frames_per_trigger = 1
|
||||
obj.lamni_stitch_x = 0
|
||||
obj.lamni_stitch_y = 0
|
||||
obj.compute_additional_correction = lambda a: (0, 0)
|
||||
obj.compute_additional_correction_2 = lambda a: (0, 0)
|
||||
obj.lamni_compute_additional_correction_xeye_mu = lambda a: (0, 0)
|
||||
obj.tomo_reconstruct = lambda: None
|
||||
scan_calls = []
|
||||
fake_scans = types.SimpleNamespace(lamni_fermat_scan=lambda **kw: scan_calls.append(kw))
|
||||
fake_bec = types.SimpleNamespace(queue=types.SimpleNamespace(next_scan_number=1))
|
||||
monkeypatch.setitem(builtins.__dict__, "scans", fake_scans)
|
||||
monkeypatch.setattr(lamni_module, "bec", fake_bec, raising=False)
|
||||
monkeypatch.setitem(builtins.__dict__, "bec", fake_bec)
|
||||
|
||||
obj.tomo_scan_projection(10.0, _internal=True)
|
||||
obj.tomo_scan_projection(
|
||||
20.0, _internal=True, step=0.5, exp_time=2.0, frames_per_trigger=4
|
||||
)
|
||||
|
||||
assert (scan_calls[0]["step"], scan_calls[0]["exp_time"]) == (1.0, 0.1)
|
||||
assert scan_calls[0]["frames_per_trigger"] == 1
|
||||
assert (scan_calls[1]["step"], scan_calls[1]["exp_time"]) == (0.5, 2.0)
|
||||
assert scan_calls[1]["frames_per_trigger"] == 4
|
||||
|
||||
|
||||
def test_angle_gate_is_minus2_to_362_only_for_type_4():
|
||||
obj = object.__new__(LamNI)
|
||||
obj.client = _FakeClient()
|
||||
obj.tomo_type = 4
|
||||
assert (obj._min_scan_angle(), obj._max_scan_angle()) == (-2, 362)
|
||||
obj.tomo_type = 1
|
||||
assert (obj._min_scan_angle(), obj._max_scan_angle()) == (0, 360)
|
||||
|
||||
|
||||
def test_queue_job_line_shows_angle_file_for_type_4():
|
||||
obj = object.__new__(LamNI)
|
||||
line = obj._describe_tomo_job_line(
|
||||
{"params": {"tomo_type": 4, "tomo_angle_file": "/x/y/angles.txt", "tomo_circfov": 0}}
|
||||
)
|
||||
assert "angles=angles.txt" in line
|
||||
line = obj._describe_tomo_job_line({"params": {"tomo_type": 1}})
|
||||
assert "angles=" not in line
|
||||
|
||||
|
||||
def test_progress_shows_type_and_total_before_the_zero_deg_shot(monkeypatch, tmp_path):
|
||||
lamni = make_lamni_for_angle_file(monkeypatch, tmp_path, "10\n20\nEND\n")
|
||||
seen = []
|
||||
lamni._tomo_scan_at_angle = lambda angle, subtomo: seen.append(
|
||||
(angle, lamni.progress["tomo_type"], lamni.progress["total_projections"])
|
||||
)
|
||||
|
||||
lamni.tomo_scan(force=True)
|
||||
|
||||
assert seen[0] == (0, "Angles from file", 2) # the initial 0-deg shot
|
||||
@@ -317,8 +317,12 @@ def test_lamni_manual_shift_fields_are_in_mm():
|
||||
assert (min_, max_) == (-1.0, 1.0)
|
||||
|
||||
|
||||
def test_flomni_manual_shift_field_is_still_um():
|
||||
"""flomni's own field really is um -- must not be touched."""
|
||||
(_key, label, min_, max_, _dec), = SETUP_PROFILES["flomni"]["manual_shift_fields"]
|
||||
assert "µm" in label
|
||||
assert (min_, max_) == (-50.0, 50.0)
|
||||
def test_flomni_manual_shift_fields_are_in_um():
|
||||
"""flomni's own fields really are um -- must not be touched."""
|
||||
fields = SETUP_PROFILES["flomni"]["manual_shift_fields"]
|
||||
assert [f[0] for f in fields] == ["manual_shift_x", "manual_shift_y"]
|
||||
by_key = {key: (min_, max_) for key, _label, min_, max_, _dec in fields}
|
||||
for _key, label, _min_, _max_, _dec in fields:
|
||||
assert "µm" in label
|
||||
assert by_key["manual_shift_x"] == (-10.0, 10.0)
|
||||
assert by_key["manual_shift_y"] == (-50.0, 50.0)
|
||||
@@ -11,7 +11,7 @@ import types
|
||||
|
||||
import csaxs_bec.bec_ipython_client.plugins.LamNI.lamni as lamni_module
|
||||
import csaxs_bec.bec_ipython_client.plugins.OMNY_shared.tomo_queue_mixin as tomo_queue_mixin
|
||||
from csaxs_bec.bec_ipython_client.plugins.LamNI.lamni import LamNI, LamNIError
|
||||
from csaxs_bec.bec_ipython_client.plugins.LamNI.lamni import LamNI, LamNIError, _ProgressProxy
|
||||
|
||||
|
||||
class _FakeRtx:
|
||||
@@ -159,7 +159,7 @@ def test_tomo_queue_execute_resumes_incomplete_job():
|
||||
|
||||
lamni.tomo_queue_execute()
|
||||
|
||||
assert calls == [("resume", (), {})]
|
||||
assert calls == [("resume", (), {"from_queue": True})]
|
||||
job = lamni._tomo_queue_proxy.as_list()[0]
|
||||
assert job["status"] == "done"
|
||||
|
||||
@@ -302,3 +302,44 @@ def test_at_each_angle_hook_refreshed_after_module_reload(tmp_path, capsys):
|
||||
finally:
|
||||
sys.path.remove(str(tmp_path))
|
||||
sys.modules.pop(module_name, None)
|
||||
|
||||
|
||||
def _resumable_lamni(status=None, answer=True):
|
||||
lamni = make_lamni()
|
||||
calls = []
|
||||
lamni.tomo_scan = lambda *a, **k: calls.append(("scan", a, k))
|
||||
lamni.tomo_type = 2
|
||||
lamni._progress_proxy = _ProgressProxy(lamni.client)
|
||||
lamni.progress["tomo_start_time"] = "2026-01-01T00:00:00"
|
||||
lamni.progress["projection"] = 5
|
||||
lamni.OMNYTools = types.SimpleNamespace(yesno=lambda *a, **k: answer)
|
||||
if status:
|
||||
index = lamni.tomo_queue_add(label="job1")
|
||||
lamni._tomo_queue_proxy.update(index, status=status)
|
||||
return lamni, calls
|
||||
|
||||
|
||||
def test_tomo_scan_resume_no_unfinished_job_does_not_prompt():
|
||||
lamni, calls = _resumable_lamni(answer=False) # would abort if prompted
|
||||
lamni.tomo_scan_resume()
|
||||
assert calls == [("scan", (), {"projection_number": 5})]
|
||||
|
||||
|
||||
def test_tomo_scan_resume_warns_and_aborts_on_no(capsys):
|
||||
lamni, calls = _resumable_lamni(status="running", answer=False)
|
||||
lamni.tomo_scan_resume()
|
||||
assert calls == []
|
||||
out = capsys.readouterr().out
|
||||
assert "unfinished job" in out and "tomo_queue_resume()" in out
|
||||
|
||||
|
||||
def test_tomo_scan_resume_proceeds_on_yes():
|
||||
lamni, calls = _resumable_lamni(status="incomplete", answer=True)
|
||||
lamni.tomo_scan_resume()
|
||||
assert calls == [("scan", (), {"projection_number": 5})]
|
||||
|
||||
|
||||
def test_tomo_scan_resume_from_queue_never_prompts():
|
||||
lamni, calls = _resumable_lamni(status="running", answer=False)
|
||||
lamni.tomo_scan_resume(from_queue=True)
|
||||
assert calls == [("scan", (), {"projection_number": 5})]
|
||||
@@ -258,3 +258,64 @@ def test_tomo_queue_reacquire_allows_targeting_the_incomplete_job_itself():
|
||||
|
||||
job = lamni._tomo_queue_proxy.as_list()[index]
|
||||
assert job["status"] == "incomplete"
|
||||
|
||||
|
||||
# ── flomni tomo_type 4 (angles from file) ────────────────────────────────────
|
||||
|
||||
|
||||
def make_flomni_for_queue():
|
||||
"""make_flomni() plus what tomo_queue_add()/tomo_queue_execute() touch
|
||||
(ptycho_reconstruct_foldername setter, sample_name in the confirm prompt)."""
|
||||
obj = make_flomni()
|
||||
obj.reconstructor = types.SimpleNamespace(folder_name=None)
|
||||
obj.sample_get_name = lambda position=0: "test"
|
||||
return obj
|
||||
|
||||
|
||||
def test_flomni_tomo_queue_reacquire_type4_bounds_by_file(tmp_path):
|
||||
flomni = make_flomni_for_queue()
|
||||
angle_file = tmp_path / "angles.txt"
|
||||
angle_file.write_text("0\n10\n20\n")
|
||||
flomni.tomo_type = 4
|
||||
flomni.tomo_angle_file = str(angle_file)
|
||||
index = flomni.tomo_queue_add(label="job0")
|
||||
flomni._tomo_queue_proxy.update(index, status="done")
|
||||
|
||||
with pytest.raises(ValueError, match="< 3"):
|
||||
flomni.tomo_queue_reacquire(index, projection_number=3)
|
||||
|
||||
flomni.tomo_queue_reacquire(index, projection_number=2)
|
||||
assert flomni.progress["projection"] == 2
|
||||
assert flomni._tomo_queue_proxy.as_list()[index]["status"] == "incomplete"
|
||||
|
||||
|
||||
def test_flomni_queue_type1_job_with_stale_angle_file_still_runs(tmp_path):
|
||||
"""tomo_queue_execute() setattr()s every snapshotted param -- a dangling
|
||||
tomo_angle_file must not break a job that doesn't use tomo_type 4."""
|
||||
flomni = make_flomni_for_queue()
|
||||
flomni.tomo_type = 1
|
||||
flomni.tomo_angle_file = str(tmp_path / "deleted_later.txt")
|
||||
flomni.tomo_queue_add(label="job0")
|
||||
ran = []
|
||||
flomni.tomo_scan = lambda **kwargs: ran.append(flomni.tomo_type)
|
||||
|
||||
flomni.tomo_queue_execute()
|
||||
|
||||
assert ran == [1]
|
||||
assert flomni._tomo_queue_proxy.as_list()[0]["status"] == "done"
|
||||
|
||||
|
||||
def test_flomni_queue_jobs_keep_their_own_angle_file(tmp_path):
|
||||
flomni = make_flomni_for_queue()
|
||||
flomni.tomo_type = 4
|
||||
for name in ("a.txt", "b.txt"):
|
||||
(tmp_path / name).write_text("0\n")
|
||||
flomni.tomo_angle_file = str(tmp_path / name)
|
||||
flomni.tomo_queue_add(label=name)
|
||||
used = []
|
||||
flomni.tomo_scan = lambda **kwargs: used.append(flomni.tomo_angle_file)
|
||||
|
||||
flomni.tomo_queue_execute()
|
||||
|
||||
assert used == [str(tmp_path / "a.txt"), str(tmp_path / "b.txt")]
|
||||
assert "angles=a.txt" in flomni._describe_tomo_job_line(flomni._tomo_queue_proxy.as_list()[0])
|
||||
@@ -5,7 +5,10 @@ import pytest
|
||||
from bec_lib.device import DeviceBase
|
||||
|
||||
from csaxs_bec.bec_ipython_client.plugins.LamNI import LamNI
|
||||
from csaxs_bec.bec_ipython_client.plugins.LamNI.x_ray_eye_align import XrayEyeAlign
|
||||
from csaxs_bec.bec_ipython_client.plugins.LamNI.x_ray_eye_align import (
|
||||
SmearComposite,
|
||||
XrayEyeAlign,
|
||||
)
|
||||
|
||||
# pylint: disable=unused-import
|
||||
|
||||
@@ -190,6 +193,9 @@ def _make_calibration_align(client):
|
||||
# FZP reference click (step 0): (0, 0) px -> (0.0, 0.0) mm
|
||||
dev_mock.omny_xray_gui.xval_x_0.get.return_value = 0.0
|
||||
dev_mock.omny_xray_gui.yval_y_0.get.return_value = 0.0
|
||||
# no arrow-button press pending
|
||||
dev_mock.omny_xray_gui.mvx.get.return_value = 0
|
||||
dev_mock.omny_xray_gui.mvy.get.return_value = 0
|
||||
|
||||
return align, dev_mock
|
||||
|
||||
@@ -329,6 +335,40 @@ def test_find_rotation_center_isolated_computes_midpoint(bec_client_mock):
|
||||
dev_mock.lsamy.update_user_parameter.assert_called_once_with({"center": 10.05})
|
||||
|
||||
|
||||
def test_find_rotation_center_isolated_subtracts_arrow_shifts(bec_client_mock):
|
||||
"""Arrow moves during the isolated calibration move lsamx/lsamy via
|
||||
lamni_move_to_scan_center(angle=0.0); the shift active at each click is
|
||||
mapped back to the configured-center frame before taking the midpoint."""
|
||||
client = bec_client_mock
|
||||
align, dev_mock = _make_calibration_align(client)
|
||||
|
||||
# same clicks as test_find_rotation_center_isolated_computes_midpoint
|
||||
dev_mock.omny_xray_gui.xval_x_1.get.return_value = 300.0
|
||||
dev_mock.omny_xray_gui.yval_y_1.get.return_value = 300.0
|
||||
dev_mock.omny_xray_gui.xval_x_2.get.return_value = 500.0
|
||||
dev_mock.omny_xray_gui.yval_y_2.get.return_value = 700.0
|
||||
# one arrow press per step: +100 um in x at 0 deg, +50 um in y at 180 deg
|
||||
dev_mock.omny_xray_gui.mvx.get.side_effect = [100, 0]
|
||||
dev_mock.omny_xray_gui.mvy.get.side_effect = [0, 50]
|
||||
|
||||
with mock.patch(f"{XRAY_EYE_ALIGN}.dev", dev_mock):
|
||||
with mock.patch(f"{XRAY_EYE_ALIGN}.umv"):
|
||||
with mock.patch(f"{XRAY_EYE_ALIGN}.time.sleep"):
|
||||
with mock.patch(f"{OMNY_GENERAL_TOOLS}.input", return_value="n"):
|
||||
align.find_rotation_center(sample_type="isolated")
|
||||
|
||||
calls = align.scans.lamni_move_to_scan_center.call_args_list
|
||||
assert len(calls) == 4 # 2 arrow moves + 2 from _apply_rotation_center_shift
|
||||
assert [c.kwargs["angle"] for c in calls] == [0.0] * 4
|
||||
assert (calls[0].kwargs["shift_x"], calls[0].kwargs["shift_y"]) == pytest.approx((0.1, 0.0))
|
||||
assert (calls[1].kwargs["shift_x"], calls[1].kwargs["shift_y"]) == pytest.approx((0.1, 0.05))
|
||||
# p0 (0.3,0.3) @ shift (0.1,0) -> ref (0.2,0.3); p180 (0.5,0.7) @ shift
|
||||
# (0.1,0.05) -> ref (0.4,0.75); midpoint (0.3,0.525) -> target (-0.3,0.525)
|
||||
for call in calls[2:]:
|
||||
assert call.kwargs["shift_x"] == pytest.approx(-0.3)
|
||||
assert call.kwargs["shift_y"] == pytest.approx(0.525)
|
||||
|
||||
|
||||
def test_find_rotation_center_isolated_declines_apply(bec_client_mock):
|
||||
client = bec_client_mock
|
||||
align, dev_mock = _make_calibration_align(client)
|
||||
@@ -415,7 +455,7 @@ def test_smear_sweep_max_projection(bec_client_mock):
|
||||
with mock.patch(f"{XRAY_EYE_ALIGN}.dev", dev_mock):
|
||||
with mock.patch(f"{XRAY_EYE_ALIGN}.mv", return_value=report):
|
||||
with mock.patch(f"{XRAY_EYE_ALIGN}.time.sleep"):
|
||||
composite = align._smear_sweep()
|
||||
composite = align._smear_sweep(projection="max")
|
||||
|
||||
expected = np.maximum(np.maximum(frame_a, frame_b), frame_c)
|
||||
assert np.array_equal(composite, expected)
|
||||
@@ -432,6 +472,83 @@ def test_smear_sweep_max_projection(bec_client_mock):
|
||||
assert [c.args[0] for c in align.gui.set_smear_active.call_args_list] == [True, False]
|
||||
|
||||
|
||||
def test_smear_sweep_defaults_to_min_projection(bec_client_mock):
|
||||
# absorption contrast: dark features on a bright background must survive
|
||||
client = bec_client_mock
|
||||
align, dev_mock = _make_calibration_align(client)
|
||||
|
||||
frame_a = np.array([[1, 5], [3, 2]], dtype=np.uint8)
|
||||
frame_b = np.array([[4, 2], [3, 6]], dtype=np.uint8)
|
||||
frame_c = np.array([[0, 1], [9, 0]], dtype=np.uint8)
|
||||
dev_mock.cam_xeye.get_last_image.side_effect = [frame_a, frame_b, frame_c]
|
||||
report = _report_with_status_sequence("RUNNING", "RUNNING", "RUNNING", "COMPLETED")
|
||||
|
||||
with mock.patch(f"{XRAY_EYE_ALIGN}.dev", dev_mock):
|
||||
with mock.patch(f"{XRAY_EYE_ALIGN}.mv", return_value=report):
|
||||
with mock.patch(f"{XRAY_EYE_ALIGN}.time.sleep"):
|
||||
composite = align._smear_sweep(opaque_level=0)
|
||||
|
||||
expected = np.minimum(np.minimum(frame_a, frame_b), frame_c)
|
||||
assert np.array_equal(composite, expected)
|
||||
|
||||
|
||||
def test_smear_sweep_rejects_unknown_projection(bec_client_mock):
|
||||
client = bec_client_mock
|
||||
align, dev_mock = _make_calibration_align(client)
|
||||
|
||||
with mock.patch(f"{XRAY_EYE_ALIGN}.dev", dev_mock):
|
||||
with mock.patch(f"{XRAY_EYE_ALIGN}.mv") as mv_mock:
|
||||
with pytest.raises(ValueError):
|
||||
align._smear_sweep(projection="mean")
|
||||
|
||||
# rejected before anything moved
|
||||
mv_mock.assert_not_called()
|
||||
|
||||
|
||||
def test_smear_composite_min_skips_opaque_holder():
|
||||
# bright background 200, absorbing particle 100, opaque holder 10
|
||||
frame_a = np.array([[200, 100], [200, 200]], dtype=np.uint8) # particle at (0, 1)
|
||||
frame_b = np.array([[10, 200], [200, 200]], dtype=np.uint8) # holder at (0, 0)
|
||||
frame_c = np.array([[200, 200], [10, 10]], dtype=np.uint8) # holder on bottom row
|
||||
|
||||
comp = SmearComposite("min")
|
||||
for frame in (frame_a, frame_b, frame_c):
|
||||
comp.add(frame)
|
||||
|
||||
# holder ignored where the pixel was open at another angle; particle kept
|
||||
assert np.array_equal(comp.image(), np.array([[200, 100], [200, 200]], dtype=np.uint8))
|
||||
assert comp.image().dtype == np.uint8
|
||||
|
||||
|
||||
def test_smear_composite_min_always_opaque_pixel_stays_dark():
|
||||
frame_a = np.array([[10, 200], [200, 200]], dtype=np.uint8)
|
||||
frame_b = np.array([[5, 200], [200, 200]], dtype=np.uint8)
|
||||
|
||||
comp = SmearComposite("min")
|
||||
comp.add(frame_a)
|
||||
comp.add(frame_b)
|
||||
|
||||
# never open -> plain minimum
|
||||
assert comp.image()[0, 0] == 5
|
||||
|
||||
|
||||
def test_smear_composite_opaque_level_zero_is_plain_min():
|
||||
frames = [
|
||||
np.array([[200, 100], [200, 200]], dtype=np.uint8),
|
||||
np.array([[10, 200], [200, 200]], dtype=np.uint8),
|
||||
]
|
||||
comp = SmearComposite("min", opaque_level=0)
|
||||
for frame in frames:
|
||||
comp.add(frame)
|
||||
|
||||
assert np.array_equal(comp.image(), np.minimum(*frames))
|
||||
|
||||
|
||||
def test_smear_composite_rejects_bad_opaque_level():
|
||||
with pytest.raises(ValueError):
|
||||
SmearComposite("min", opaque_level=1.0)
|
||||
|
||||
|
||||
def test_smear_sweep_computes_capture_fps(bec_client_mock):
|
||||
client = bec_client_mock
|
||||
align, dev_mock = _make_calibration_align(client)
|
||||
@@ -453,11 +570,16 @@ def test_smear_sweep_computes_capture_fps(bec_client_mock):
|
||||
clock["t"] += 0.05
|
||||
return clock["t"]
|
||||
|
||||
# Replace the module's `time` rather than patching time.time globally:
|
||||
# background threads left running by other tests (e.g. EPICS CA
|
||||
# callbacks) would otherwise also advance the fake clock.
|
||||
fake_time_module = mock.MagicMock()
|
||||
fake_time_module.time.side_effect = fake_time
|
||||
|
||||
with mock.patch(f"{XRAY_EYE_ALIGN}.dev", dev_mock):
|
||||
with mock.patch(f"{XRAY_EYE_ALIGN}.mv", return_value=report):
|
||||
with mock.patch(f"{XRAY_EYE_ALIGN}.time.sleep"):
|
||||
with mock.patch(f"{XRAY_EYE_ALIGN}.time.time", side_effect=fake_time):
|
||||
align._smear_sweep()
|
||||
with mock.patch(f"{XRAY_EYE_ALIGN}.time", fake_time_module):
|
||||
align._smear_sweep(opaque_level=0)
|
||||
|
||||
assert align.last_smear_fps == pytest.approx(10.0)
|
||||
|
||||
|
||||
@@ -0,0 +1,190 @@
|
||||
"""Tests for the TomoParams widget's tomo_type 4 (angles from file) support:
|
||||
per-setup type availability, validation, tooltip gating, and the type-4
|
||||
section's live file preview.
|
||||
|
||||
The full OMNY_TomoParams widget needs a live BEC client, so these exercise
|
||||
its methods on minimal stubs, same approach as test_x_ray_eye_vertical_lock.py.
|
||||
"""
|
||||
|
||||
import os
|
||||
import types
|
||||
|
||||
import pytest
|
||||
|
||||
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
||||
|
||||
from qtpy.QtWidgets import QApplication, QLineEdit # noqa: E402
|
||||
|
||||
from csaxs_bec.bec_widgets.widgets.tomo_params.tomo_params import ( # noqa: E402
|
||||
DEFAULTS,
|
||||
SETUP_PROFILES,
|
||||
OMNY_TomoParams,
|
||||
_irrelevant_params_for_type,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def qapp():
|
||||
return QApplication.instance() or QApplication([])
|
||||
|
||||
|
||||
def _valid_flomni_params(**overrides):
|
||||
params = dict(DEFAULTS)
|
||||
params.update(overrides)
|
||||
return params
|
||||
|
||||
|
||||
def _validate(setup, params):
|
||||
stub = types.SimpleNamespace(_profile=SETUP_PROFILES[setup])
|
||||
return OMNY_TomoParams._validate(stub, params)
|
||||
|
||||
|
||||
def test_type_4_available_for_flomni_and_lamni():
|
||||
assert 4 in SETUP_PROFILES["flomni"]["tomo_types"]
|
||||
assert 4 in SETUP_PROFILES["lamni"]["tomo_types"]
|
||||
assert "tomo_angle_file" in SETUP_PROFILES["lamni"]["param_names"]
|
||||
assert "tomo_angle_file" in SETUP_PROFILES["lamni"]["defaults"]
|
||||
|
||||
|
||||
def test_validate_accepts_type_4_with_good_file(tmp_path):
|
||||
angle_file = tmp_path / "angles.txt"
|
||||
angle_file.write_text("0\n90 2\n")
|
||||
params = _valid_flomni_params(tomo_type=4, tomo_angle_file=str(angle_file), fovx=40.0)
|
||||
assert _validate("flomni", params) is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize("content", [None, "400\n", "# nothing\n"])
|
||||
def test_validate_rejects_type_4_with_bad_file(tmp_path, content):
|
||||
angle_file = tmp_path / "angles.txt"
|
||||
if content is not None:
|
||||
angle_file.write_text(content)
|
||||
params = _valid_flomni_params(tomo_type=4, tomo_angle_file=str(angle_file), fovx=40.0)
|
||||
error = _validate("flomni", params)
|
||||
assert error is not None and error.startswith("Angle file:")
|
||||
|
||||
|
||||
def test_validate_ignores_bad_angle_file_for_other_types(tmp_path):
|
||||
params = _valid_flomni_params(
|
||||
tomo_type=1, tomo_angle_file=str(tmp_path / "gone.txt"), fovx=40.0
|
||||
)
|
||||
assert _validate("flomni", params) is None
|
||||
|
||||
|
||||
def test_validate_type_4_on_lamni(tmp_path):
|
||||
params = dict(SETUP_PROFILES["lamni"]["defaults"])
|
||||
params["tomo_type"] = 4
|
||||
params["tomo_angle_file"] = str(tmp_path / "gone.txt")
|
||||
assert _validate("lamni", params).startswith("Angle file:")
|
||||
angle_file = tmp_path / "angles.txt"
|
||||
angle_file.write_text("0\n180\n")
|
||||
params["tomo_angle_file"] = str(angle_file)
|
||||
assert _validate("lamni", params) is None
|
||||
params["tomo_type"] = 5
|
||||
assert "tomo_type must be one of 1, 2, 3, 4" in _validate("lamni", params)
|
||||
|
||||
|
||||
def test_tooltip_gating_for_type_4():
|
||||
irrelevant = _irrelevant_params_for_type(4)
|
||||
assert "tomo_angle_file" not in irrelevant
|
||||
assert "golden_projections_at_0_deg_for_damage_estimation" in irrelevant
|
||||
assert "golden_max_number_of_projections" in irrelevant
|
||||
assert "golden_ratio_bunch_size" in irrelevant
|
||||
assert "tomo_angle_range" in irrelevant
|
||||
for other in (1, 2, 3):
|
||||
assert "tomo_angle_file" in _irrelevant_params_for_type(other)
|
||||
|
||||
|
||||
class _StubTomoParams:
|
||||
"""Just enough of OMNY_TomoParams to build and drive the type-4 section."""
|
||||
|
||||
_build_type4_section = OMNY_TomoParams._build_type4_section
|
||||
_update_angle_file_preview = OMNY_TomoParams._update_angle_file_preview
|
||||
_set_angle_file_summary = OMNY_TomoParams._set_angle_file_summary
|
||||
_browse_angle_file = OMNY_TomoParams._browse_angle_file
|
||||
|
||||
def __init__(self, setup="flomni"):
|
||||
self._profile = SETUP_PROFILES[setup]
|
||||
self._pw = {}
|
||||
self._sec_type4 = self._build_type4_section()
|
||||
|
||||
|
||||
def test_type4_section_preview_tracks_file_content(qapp, tmp_path):
|
||||
stub = _StubTomoParams()
|
||||
assert isinstance(stub._pw["tomo_angle_file"], QLineEdit)
|
||||
|
||||
angle_file = tmp_path / "angles.txt"
|
||||
angle_file.write_text("10 1\n100 2\nEND\n")
|
||||
stub._pw["tomo_angle_file"].setText(str(angle_file))
|
||||
assert stub._lbl_angle_file_summary.text() == "2 angles, 10.00-100.00 deg, 2 sub-tomograms"
|
||||
assert "red" not in stub._lbl_angle_file_summary.styleSheet()
|
||||
|
||||
angle_file.write_text("10\n190\n")
|
||||
stub._update_angle_file_preview()
|
||||
assert "180 deg apart" in stub._lbl_angle_file_summary.text()
|
||||
assert "orange" in stub._lbl_angle_file_summary.styleSheet()
|
||||
|
||||
stub._pw["tomo_angle_file"].setText(str(tmp_path / "missing.txt"))
|
||||
assert "Cannot read" in stub._lbl_angle_file_summary.text()
|
||||
assert "red" in stub._lbl_angle_file_summary.styleSheet()
|
||||
|
||||
|
||||
def test_type4_preview_has_no_180_deg_warning_on_lamni(qapp, tmp_path):
|
||||
stub = _StubTomoParams("lamni")
|
||||
angle_file = tmp_path / "angles.txt"
|
||||
angle_file.write_text("10\n190\nEND\n")
|
||||
stub._pw["tomo_angle_file"].setText(str(angle_file))
|
||||
assert "180 deg apart" not in stub._lbl_angle_file_summary.text()
|
||||
assert "orange" not in stub._lbl_angle_file_summary.styleSheet()
|
||||
|
||||
|
||||
def test_type4_section_preview_grows_minimum_height_for_wrapped_content(qapp, tmp_path):
|
||||
"""QFormLayout sizes the Content row from the label's sizeHint() at
|
||||
construction time (still holding the short '---' placeholder) and
|
||||
doesn't reliably recompute it later when setText() swaps in longer,
|
||||
word-wrapped text -- a known Qt quirk. _set_angle_file_summary() must
|
||||
grow the label's minimum height to compensate, or multi-line content
|
||||
(e.g. a 180-deg-pair or missing-END warning) gets visually clipped."""
|
||||
stub = _StubTomoParams()
|
||||
initial_height = stub._lbl_angle_file_summary.minimumHeight()
|
||||
|
||||
angle_file = tmp_path / "angles.txt"
|
||||
angle_file.write_text("10\n190\n") # triggers the 180-deg-pair warning line
|
||||
stub._pw["tomo_angle_file"].setText(str(angle_file))
|
||||
|
||||
assert stub._lbl_angle_file_summary.minimumHeight() > initial_height
|
||||
assert stub._lbl_angle_file_summary.minimumHeight() >= (
|
||||
stub._lbl_angle_file_summary.heightForWidth(stub._lbl_angle_file_summary.width())
|
||||
)
|
||||
|
||||
|
||||
def test_type4_section_preview_flags_missing_end_marker(qapp, tmp_path):
|
||||
stub = _StubTomoParams()
|
||||
|
||||
angle_file = tmp_path / "angles.txt"
|
||||
angle_file.write_text("10 1\n100 2\n")
|
||||
stub._pw["tomo_angle_file"].setText(str(angle_file))
|
||||
assert "No 'END' marker" in stub._lbl_angle_file_summary.text()
|
||||
assert "orange" in stub._lbl_angle_file_summary.styleSheet()
|
||||
|
||||
angle_file.write_text("10 1\n100 2\nEND\n")
|
||||
stub._update_angle_file_preview()
|
||||
assert "No 'END' marker" not in stub._lbl_angle_file_summary.text()
|
||||
|
||||
|
||||
def test_browse_sets_selected_path(qapp, tmp_path, monkeypatch):
|
||||
stub = _StubTomoParams()
|
||||
chosen = str(tmp_path / "picked.txt")
|
||||
seen = {}
|
||||
|
||||
def fake_dialog(parent, caption, start_dir, filters):
|
||||
seen["start_dir"] = start_dir
|
||||
return chosen, filters
|
||||
|
||||
monkeypatch.setattr(
|
||||
"csaxs_bec.bec_widgets.widgets.tomo_params.tomo_params.QFileDialog.getOpenFileName",
|
||||
fake_dialog,
|
||||
)
|
||||
stub._pw["tomo_angle_file"].setText(str(tmp_path / "old.txt"))
|
||||
stub._browse_angle_file()
|
||||
assert seen["start_dir"] == str(tmp_path)
|
||||
assert stub._pw["tomo_angle_file"].text() == chosen
|
||||
@@ -10,6 +10,7 @@ import numpy as np
|
||||
import pytest
|
||||
from bec_lib.messages import FileMessage, ScanStatusMessage
|
||||
from bec_server.scan_server.scans.scan_base import ScanInfo as ScanServerScanInfo
|
||||
from jfjoch_client.exceptions import ServiceException
|
||||
from jfjoch_client.models.broker_status import BrokerStatus
|
||||
from jfjoch_client.models.dataset_settings import DatasetSettings
|
||||
from jfjoch_client.models.detector_list import DetectorList
|
||||
@@ -29,6 +30,11 @@ from csaxs_bec.devices.jungfraujoch.eiger import (
|
||||
MISSING_PACKETS_MESSAGE,
|
||||
RAISE_ON_MISSING_PACKETS,
|
||||
)
|
||||
from csaxs_bec.devices.jungfraujoch.jungfrau_joch_client import (
|
||||
DetectorState,
|
||||
JungfrauJochClient,
|
||||
JungfrauJochClientError,
|
||||
)
|
||||
from csaxs_bec.devices.jungfraujoch.eiger_1_5m import EIGER1_5M_READOUT_TIME, Eiger1_5M
|
||||
from csaxs_bec.devices.jungfraujoch.eiger_9m import EIGER9M_READOUT_TIME, Eiger9M
|
||||
from csaxs_bec.devices.jungfraujoch.eiger2_si_500k import (
|
||||
@@ -161,6 +167,24 @@ def eiger_9m(mock_scan_info) -> Generator[Eiger9M, None, None]:
|
||||
dev.destroy()
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def no_live_jfjoch_cancel():
|
||||
"""
|
||||
The devices point to the production JungfrauJoch host, and destroy() -> on_stop() sends a
|
||||
cancel from a background thread. Block it so tests never cancel a live acquisition, nor read
|
||||
the Dectris detector server afterwards. Tests patching the client instance still override this.
|
||||
"""
|
||||
with mock.patch(
|
||||
"jfjoch_client.api.default_api.DefaultApi.cancel_post_with_http_info",
|
||||
side_effect=ConnectionError("tests must not reach JungfrauJoch"),
|
||||
):
|
||||
yield
|
||||
# Stop threads started by device teardown must finish while the patch is active.
|
||||
for thread in threading.enumerate():
|
||||
if thread.name == "stop_jungfraujoch_thread":
|
||||
thread.join(timeout=5)
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def eiger2_si_500k(mock_scan_info) -> Generator[Eiger2Si500K, None, None]:
|
||||
"""Fixture for the Eiger 2 Si 500K device.
|
||||
@@ -794,3 +818,290 @@ def test_eiger_subclasses_name_raise_on_missing_packets_for_the_device_server():
|
||||
class signature names it, so **kwargs alone would silently drop it."""
|
||||
for cls in (Eiger1_5M, Eiger9M, Eiger2Si500K):
|
||||
assert "raise_on_missing_packets" in inspect.signature(cls).parameters
|
||||
|
||||
|
||||
def test_eiger_on_stage_auto_reinitialize(eiger_1_5m):
|
||||
"""With auto_reinitialize the detector is recovered on staging, otherwise it is not."""
|
||||
eiger = eiger_1_5m
|
||||
for flag in (True, False):
|
||||
eiger._auto_reinitialize = flag
|
||||
with (
|
||||
mock.patch.object(eiger.jfj_client, "wait_for_idle", return_value=True),
|
||||
mock.patch.object(eiger.jfj_client, "start"),
|
||||
mock.patch.object(eiger.jfj_client, "stop"),
|
||||
mock.patch.object(eiger.jfj_client, "recover_from_error") as mock_recover,
|
||||
mock.patch.object(
|
||||
eiger.jfj_client.api, "status_get", return_value=BrokerStatus(state="Idle")
|
||||
),
|
||||
):
|
||||
eiger.stage()
|
||||
assert mock_recover.call_count == (1 if flag else 0)
|
||||
eiger.unstage()
|
||||
|
||||
|
||||
def test_eiger_on_stage_auto_reinitialize_failure(eiger_1_5m):
|
||||
"""A failed recovery raises a single EigerError pointing to the WEB UI."""
|
||||
eiger = eiger_1_5m
|
||||
eiger._auto_reinitialize = True
|
||||
with mock.patch.object(
|
||||
eiger.jfj_client, "recover_from_error", side_effect=JungfrauJochClientError("still bad")
|
||||
):
|
||||
with pytest.raises(EigerError, match="could not be re-initialised"):
|
||||
eiger.on_stage()
|
||||
|
||||
|
||||
def test_eiger_on_complete_fails_fast_in_error_state(eiger_1_5m):
|
||||
"""on_complete raises right away if the detector is in ERROR state, not after the timeout."""
|
||||
eiger = eiger_1_5m
|
||||
with mock.patch.object(
|
||||
eiger.jfj_client.api,
|
||||
"status_get",
|
||||
return_value=BrokerStatus(state="Error", message_severity="error", message="boom"),
|
||||
):
|
||||
start = time.time()
|
||||
status = eiger.complete()
|
||||
with pytest.raises(EigerError):
|
||||
status.wait(timeout=5)
|
||||
assert time.time() - start < 5
|
||||
|
||||
|
||||
def _jfj_client() -> JungfrauJochClient:
|
||||
return JungfrauJochClient(host="http://localhost:1")
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"states, expected, n_init",
|
||||
[
|
||||
(["Idle"], False, 0),
|
||||
(["Error", "Idle"], True, 1),
|
||||
],
|
||||
)
|
||||
def test_jfj_client_recover_from_error(states, expected, n_init):
|
||||
client = _jfj_client()
|
||||
with (
|
||||
mock.patch.object(
|
||||
JungfrauJochClient,
|
||||
"detector_state",
|
||||
new_callable=mock.PropertyMock,
|
||||
side_effect=[DetectorState(s) for s in states],
|
||||
),
|
||||
mock.patch.object(client.api, "initialize_post") as mock_init,
|
||||
mock.patch.object(client, "wait_for_idle", return_value=True) as mock_wait,
|
||||
):
|
||||
assert client.recover_from_error() is expected
|
||||
assert mock_init.call_count == n_init
|
||||
assert mock_wait.call_count == n_init
|
||||
|
||||
|
||||
def test_jfj_client_recover_from_error_still_in_error():
|
||||
client = _jfj_client()
|
||||
with (
|
||||
mock.patch.object(
|
||||
JungfrauJochClient,
|
||||
"detector_state",
|
||||
new_callable=mock.PropertyMock,
|
||||
return_value=DetectorState.ERROR,
|
||||
),
|
||||
mock.patch.object(client.api, "initialize_post"),
|
||||
mock.patch.object(client, "wait_for_idle", return_value=True),
|
||||
):
|
||||
with pytest.raises(JungfrauJochClientError, match="after re-initialisation"):
|
||||
client.recover_from_error()
|
||||
|
||||
|
||||
def test_jfj_client_wait_for_idle_reports_broker_message():
|
||||
"""The HTTP 500 body is reported instead of the pydantic validation traceback."""
|
||||
client = _jfj_client()
|
||||
body = (
|
||||
'{"msg":"DAQ state error (Detector error (Error in detector operation))",'
|
||||
'"reason":"WrongDAQState"}'
|
||||
)
|
||||
exc = ServiceException(status=500, reason="Internal Server Error", body=body)
|
||||
with mock.patch.object(client.api, "wait_till_done_post", side_effect=exc):
|
||||
with pytest.raises(JungfrauJochClientError, match="WrongDAQState"):
|
||||
client.wait_for_idle(timeout=1)
|
||||
assert client.wait_for_idle(timeout=1, raise_on_timeout=False) is False
|
||||
|
||||
|
||||
def test_eiger_auto_reinitialize_is_a_config_attribute(eiger_1_5m):
|
||||
"""The device server rejects deviceConfig keys that are not attributes of the device."""
|
||||
eiger = eiger_1_5m
|
||||
assert hasattr(eiger, "auto_reinitialize")
|
||||
assert eiger.auto_reinitialize is False
|
||||
eiger.auto_reinitialize = True
|
||||
assert eiger._auto_reinitialize is True
|
||||
|
||||
|
||||
def test_dectris_url_only_set_for_eiger2_si_500k(eiger_1_5m, eiger_9m, eiger2_si_500k):
|
||||
"""Only the Eiger2 Si 500K logs the Dectris detector server status, the others are unchanged."""
|
||||
assert eiger_1_5m.jfj_client._dectris_url is None
|
||||
assert eiger_9m.jfj_client._dectris_url is None
|
||||
assert eiger2_si_500k.jfj_client._dectris_url == "http://129.129.122.159"
|
||||
|
||||
|
||||
def test_jfj_client_log_dectris_status_disabled():
|
||||
"""Without dectris_url no request is made and nothing is logged."""
|
||||
client = _jfj_client()
|
||||
with (
|
||||
mock.patch("csaxs_bec.devices.jungfraujoch.jungfrau_joch_client.requests.get") as mock_get,
|
||||
mock.patch("csaxs_bec.devices.jungfraujoch.jungfrau_joch_client.logger") as mock_logger,
|
||||
):
|
||||
client.log_dectris_status("test")
|
||||
mock_get.assert_not_called()
|
||||
mock_logger.warning.assert_not_called()
|
||||
|
||||
|
||||
def test_jfj_client_log_dectris_status():
|
||||
"""All endpoints are read and logged in a single line, failed requests do not raise."""
|
||||
client = JungfrauJochClient(host="http://localhost:1", dectris_url="http://dcu/")
|
||||
|
||||
def fake_get(url, timeout):
|
||||
response = mock.Mock()
|
||||
if url.endswith("/status/link_2"):
|
||||
raise TimeoutError("too slow")
|
||||
if url.endswith("/status/state"):
|
||||
response.json.return_value = {"value": "error", "value_type": "string"}
|
||||
elif "/monitor/" in url:
|
||||
response.json.return_value = {"state": {"value": "overflow"}, "dropped": {"value": 5}}
|
||||
else:
|
||||
response.json.return_value = {"value": 1}
|
||||
return response
|
||||
|
||||
with (
|
||||
mock.patch(
|
||||
"csaxs_bec.devices.jungfraujoch.jungfrau_joch_client.requests.get", side_effect=fake_get
|
||||
) as mock_get,
|
||||
mock.patch("csaxs_bec.devices.jungfraujoch.jungfrau_joch_client.logger") as mock_logger,
|
||||
):
|
||||
client.log_dectris_status("test reason")
|
||||
assert mock_get.call_count == 15
|
||||
mock_logger.warning.assert_called_once()
|
||||
line = mock_logger.warning.call_args[0][0]
|
||||
assert "test reason" in line
|
||||
assert '"detector/state":"error"' in line
|
||||
assert '"monitor":{"state":"overflow","dropped":5}' in line
|
||||
assert "TimeoutError" in line # link_2 failed, the rest is still logged
|
||||
|
||||
|
||||
def test_jfj_client_recover_from_error_logs_dectris_status_before_init():
|
||||
client = JungfrauJochClient(host="http://localhost:1", dectris_url="http://dcu")
|
||||
calls = mock.Mock()
|
||||
with (
|
||||
mock.patch.object(
|
||||
JungfrauJochClient,
|
||||
"detector_state",
|
||||
new_callable=mock.PropertyMock,
|
||||
side_effect=[DetectorState.ERROR, DetectorState.IDLE],
|
||||
),
|
||||
mock.patch.object(client, "log_dectris_status", calls.log),
|
||||
mock.patch.object(client.api, "initialize_post", calls.init),
|
||||
mock.patch.object(client, "wait_for_idle", return_value=True),
|
||||
):
|
||||
assert client.recover_from_error() is True
|
||||
assert [c[0] for c in calls.mock_calls] == ["log", "init"]
|
||||
|
||||
|
||||
def test_eiger_on_complete_logs_dectris_status_in_error_state(eiger2_si_500k):
|
||||
eiger = eiger2_si_500k
|
||||
with (
|
||||
mock.patch.object(
|
||||
eiger.jfj_client.api,
|
||||
"status_get",
|
||||
return_value=BrokerStatus(state="Error", message_severity="error", message="boom"),
|
||||
),
|
||||
mock.patch.object(eiger.jfj_client, "log_dectris_status") as mock_log,
|
||||
):
|
||||
status = eiger.complete()
|
||||
with pytest.raises(EigerError):
|
||||
status.wait(timeout=5)
|
||||
mock_log.assert_called_once()
|
||||
|
||||
|
||||
def test_jfj_client_log_dectris_status_returns_results():
|
||||
disabled = _jfj_client()
|
||||
assert disabled.log_dectris_status("test") is None
|
||||
|
||||
client = JungfrauJochClient(host="http://localhost:1", dectris_url="http://dcu")
|
||||
response = mock.Mock()
|
||||
response.json.return_value = {"value": "idle"}
|
||||
with (
|
||||
mock.patch(
|
||||
"csaxs_bec.devices.jungfraujoch.jungfrau_joch_client.requests.get",
|
||||
return_value=response,
|
||||
),
|
||||
mock.patch("csaxs_bec.devices.jungfraujoch.jungfrau_joch_client.logger"),
|
||||
):
|
||||
results = client.log_dectris_status("test")
|
||||
assert results["detector/state"] == "idle"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"results, expected",
|
||||
[
|
||||
({"detector/state": "ready", "detector/series_unique_id": "S1"}, "S1"),
|
||||
({"detector/state": "idle", "stream": {"state": "acquire"}}, "unknown"),
|
||||
({"detector/state": "ready", "detector/series_unique_id": "<TimeoutError: x>"}, "unknown"),
|
||||
({"detector/state": "idle", "stream": {"state": "ready"}}, None),
|
||||
({"detector/state": "<TimeoutError: x>", "stream": "<TimeoutError: x>"}, None),
|
||||
({}, None),
|
||||
(None, None),
|
||||
],
|
||||
)
|
||||
def test_jfj_client_dectris_still_armed(results, expected):
|
||||
assert JungfrauJochClient._dectris_still_armed(results) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"dectris_status, hint_expected",
|
||||
[
|
||||
(
|
||||
{
|
||||
"detector/state": "ready",
|
||||
"detector/series_unique_id": "01M4J4FPRPZ2J7M6MW2YSA27VG",
|
||||
"stream": {"state": "acquire"},
|
||||
},
|
||||
True,
|
||||
),
|
||||
({"detector/state": "idle", "stream": {"state": "ready"}}, False),
|
||||
(None, False),
|
||||
],
|
||||
)
|
||||
def test_jfj_client_recover_from_error_hints_armed_dectris(dectris_status, hint_expected):
|
||||
"""A failed re-init tells the operator when the Dectris server is still armed."""
|
||||
client = JungfrauJochClient(host="http://localhost:1", dectris_url="http://dcu")
|
||||
with (
|
||||
mock.patch.object(
|
||||
JungfrauJochClient,
|
||||
"detector_state",
|
||||
new_callable=mock.PropertyMock,
|
||||
return_value=DetectorState.ERROR,
|
||||
),
|
||||
mock.patch.object(client, "log_dectris_status", return_value=dectris_status),
|
||||
mock.patch.object(client.api, "initialize_post"),
|
||||
mock.patch.object(
|
||||
client, "wait_for_idle", side_effect=JungfrauJochClientError("Detector busy")
|
||||
),
|
||||
):
|
||||
with pytest.raises(JungfrauJochClientError, match="Detector busy") as exc_info:
|
||||
client.recover_from_error()
|
||||
message = str(exc_info.value)
|
||||
assert ("still armed" in message) is hint_expected
|
||||
assert ("01M4J4FPRPZ2J7M6MW2YSA27VG" in message) is hint_expected
|
||||
|
||||
|
||||
def test_jfj_client_stop_logs_cancel_and_dectris_status():
|
||||
client = JungfrauJochClient(host="http://localhost:1", dectris_url="http://dcu")
|
||||
calls = mock.Mock()
|
||||
with (
|
||||
mock.patch.object(client.api, "cancel_post_with_http_info", calls.cancel),
|
||||
mock.patch.object(client, "log_dectris_status", calls.log),
|
||||
mock.patch("csaxs_bec.devices.jungfraujoch.jungfrau_joch_client.logger") as mock_logger,
|
||||
mock.patch(
|
||||
"csaxs_bec.devices.jungfraujoch.jungfrau_joch_client.threading.Thread",
|
||||
side_effect=lambda target, args, **kwargs: mock.Mock(start=lambda: target(*args)),
|
||||
),
|
||||
):
|
||||
client.stop(reason="on_stop")
|
||||
assert [c[0] for c in calls.mock_calls] == ["cancel", "log"]
|
||||
calls.log.assert_called_once_with("after cancel (on_stop)")
|
||||
assert "on_stop" in mock_logger.info.call_args[0][0]
|
||||
Reference in new issue
Block a user