From ea7ba7e1294b2cb077de0cbd5a2fe22047e88062 Mon Sep 17 00:00:00 2001 From: Mirko Holler Date: Fri, 25 Sep 2026 16:00:40 +0200 Subject: [PATCH] fix(lamni): go-live audit tier 2 -- wrong numbers and missing flomni ports - tomo_scan_projection() now adds the projection to the reconstruction queue itself (like flomni), so custom at_each_angle hooks and direct calls are reconstructed; _at_each_angle()/MagLamNI no longer do it (no double entry). tomo_alignment_scan() opts out (_reconstruct=False) to keep its previous behaviour -- flomni's alignment projections are queued, LamNI's never were. - write_pdf_report(): branch on tomo_type (a golden-ratio scan reported a stale type-1 count and a hardcoded '8 sub-tomograms'), e-account from bec.active_account, tolerate a missing detector_distance. - tomo_parameters_cli(): range guards (stitch 0-50, frames 1-100, >= 8 projections, type-3 >= 1, golden max 0-1e6, bunch size 20-100). - Webpage: projection count for tomo type 1 used flomni's 180-degree sub-tomograms (half the real count); TOMO_TYPES gets a base_angle. Also show the piezo range, manual shift and 0-degree reference rows. - Tomo-params GUI: manual shift is mm end to end (label/range said um, +-1000); golden bunch size floor 20 for LamNI (setter/CLI already required it, so a smaller GUI value broke tomo_queue_execute). flomni fields untouched. - lfzp_info() and the queue 'move Energy' entry used the legacy 'mokev' device (only in device_configs/legacy); now ccm_energy. - Special-angle UnboundLocalError fixed in flomni too (list consumed only after a successful projection, so retries still see the special angle). - Tomo type 3: exact integer sub-tomogram numbering (float truncation mislabelled the boundary projection for ~35 values of n < 400). - XrayEyeAlign.align(): always reset the GUI (DAP forwarding, crosshair) on any failure; xrayeye_update_frame() warns when beamline states are invalid. - Stale text / debug print; gap doc updated with the current status. Co-Authored-By: Claude Sonnet 5 --- .../FLOMNI_LAMNI_FEATURE_GAPS_2026-07.md | 48 +++++ .../plugins/LamNI/LamNI_webpage_generator.py | 12 +- .../plugins/LamNI/extra_tomo.py | 2 +- .../bec_ipython_client/plugins/LamNI/lamni.py | 192 ++++++++++++++--- .../plugins/LamNI/lamni_alignment_mixin.py | 12 +- .../plugins/LamNI/lamni_optics_mixin.py | 8 +- .../plugins/LamNI/x_ray_eye_align.py | 31 ++- .../OMNY_shared/webpage_generator_base.py | 25 ++- .../plugins/flomni/flomni.py | 22 +- .../widgets/tomo_params/tomo_params.py | 24 ++- .../test_flomni_special_angles.py | 88 ++++++++ .../test_lamni_get_val_range.py | 122 +++++++++++ .../test_lamni_golive_fixes.py | 202 ++++++++++++++++++ .../test_lamni_osa_collision_info.py | 34 +++ .../test_lamni_pdf_report.py | 172 +++++++++++++++ .../test_lamni_tomo_alignment_scan.py | 22 +- .../test_lamni_tomo_params_widget_math.py | 73 +++++++ .../test_lamni_tomo_queue.py | 19 +- .../test_tomo_queue_reacquire.py | 4 +- .../test_webpage_projection_count.py | 108 ++++++++++ .../test_x_ray_eye_align.py | 58 ++++- tests/tests_scans/test_lamni_fermat_scan.py | 38 ++++ 22 files changed, 1243 insertions(+), 73 deletions(-) create mode 100644 tests/tests_bec_ipython_client/test_flomni_special_angles.py create mode 100644 tests/tests_bec_ipython_client/test_lamni_pdf_report.py create mode 100644 tests/tests_bec_ipython_client/test_webpage_projection_count.py diff --git a/csaxs_bec/bec_ipython_client/plugins/LamNI/AI_docs/FLOMNI_LAMNI_FEATURE_GAPS_2026-07.md b/csaxs_bec/bec_ipython_client/plugins/LamNI/AI_docs/FLOMNI_LAMNI_FEATURE_GAPS_2026-07.md index 2ba01a01..63fba981 100644 --- a/csaxs_bec/bec_ipython_client/plugins/LamNI/AI_docs/FLOMNI_LAMNI_FEATURE_GAPS_2026-07.md +++ b/csaxs_bec/bec_ipython_client/plugins/LamNI/AI_docs/FLOMNI_LAMNI_FEATURE_GAPS_2026-07.md @@ -1,5 +1,53 @@ # flomni → lamni feature gaps still open (2026-07 sweep) +> **Status update (2026-09-25 go-live audit).** The sections below are the +> original July findings; several are done since. Current state: +> +> - **Done**: §1 timing log + `scilog_last_ptycho_scans()`, §2 account-change +> reset, §4 `lfzp_in()` skip-if-in-position, §6 `tomo_alignment_scan()` (12 +> angles over 360°, `write_alignment_scan_numbers()`). §7 (OSA clearance in +> `lfzp_info()`) is implemented but **inactive** until `loptz`'s +> `collision_offset` userParameter is commissioned in `ptycho_lamni.yaml`. +> - **Still open**: §3 `collect_empty_frames()` (needs a decision whether +> lamni's reconstruction uses flat fields), §5 hard-stop button in the GUI +> (see the caveat in the audit notes below), §9 status-page rows (zero-degree +> reference toggle is now shown; golden damage-check shots already were). +> - **Fixed by the September audit** (LamNI compared against the running +> flomni; tests in `tests/tests_bec_ipython_client/test_lamni_golive_fixes.py` +> and siblings): projections silently dropped on a TimeoutError alarm +> (also in `tomo_alignment_scan`); `leye_out()` opened the shutter instead of +> closing it; endless golden-ratio scan crashed on its first projection and +> any GUI failure aborted the scan; scilog writes used the removed +> `bec.logbook` API; `raise_window()` could hide the GUI; tomo_scan_projection +> now reconstructs itself like flomni (custom hooks were never reconstructed); +> PDF report ignored the tomo type; CLI range guards; special-angle +> `UnboundLocalError` (also fixed in flomni); tomo-type-3 sub-tomogram +> boundary float truncation; `lfzp_info()`/queue "move Energy" used the +> legacy `mokev` device (now `ccm_energy`); webpage showed half the +> type-1 projection count; GUI manual-shift unit label (mm, not µm) and a +> bunch-size floor of 20; x-ray-eye `align()` cleanup on any failure; +> `xrayeye_update_frame()` beam-state warning. +> - **Open questions found by the audit** (need a hardware/design decision, not +> changed): default `lopty.limits = [3.0, 3.6]` excludes the FZP-in position +> 3.724 from `ptycho_lamni.yaml`; no OSA collision guard in `losa_in()` (needs +> LamNI's own geometry constant); the hard-stop button must not blindly reuse +> flomni's `hard_abort_and_restore_positioning_mode()` (it runs `XQ#POSMODE`, +> a transfer routine LamNI's Galil program may not have; LamNI has one Galil, +> a SmarAct controller for the OSA and the RT controller); +> `_write_tomo_scan_number()` writes the literal `'lamni'` (flomni: sanitized +> sample name, 0cadd85) and `.3f` angles (flomni `.5f`); `LamniFermatScan` +> lacks flomni's `randomize_offset` switch and `MAX_CORRIDOR_SIZE` cap +> (19cfee3, 53f246a); `tomo_alignment_scan()` always runs the full eye-out +> sequence (flomni skips it when already out). +> - **Behaviour to be aware of**: flomni's `tomo_alignment_scan()` queues its +> projections for reconstruction (it goes through `tomo_scan_projection`); +> LamNI's deliberately does not (`_reconstruct=False`). +> - **Same latent issues remain in flomni** (running in production, not +> touched here): tomo-type-3 sub-tomogram boundary float truncation +> (`flomni.py` `_golden_equally_spaced`), and the queue "move Energy" device +> name `mokev` (`_TOMO_QUEUE_MOVE_DEVICES`) which is only defined in +> `device_configs/legacy/`. + Follow-up to `FLOMNI_TO_LAMNI_COMPARISON.md` (flomni's `AI_docs/`) and this folder's `TOMO_QUEUE_PORT.md`/`TOMO_PARAMS_GUI_PORT.md` — those covered the tomo-queue backend, the GUI, the webpage generator, and the angle-distribution diff --git a/csaxs_bec/bec_ipython_client/plugins/LamNI/LamNI_webpage_generator.py b/csaxs_bec/bec_ipython_client/plugins/LamNI/LamNI_webpage_generator.py index ead6fe36..ce758c5d 100644 --- a/csaxs_bec/bec_ipython_client/plugins/LamNI/LamNI_webpage_generator.py +++ b/csaxs_bec/bec_ipython_client/plugins/LamNI/LamNI_webpage_generator.py @@ -59,7 +59,11 @@ class LamniWebpageGenerator(WebpageGeneratorBase): # FlomniWebpageGenerator's TOMO_TYPES -- confirmed against lamni.py's # actual tomo_scan()/sub_tomo_scan() implementation, not a guess. TOMO_TYPES = { - 1: {"kind": "equally_spaced_grid", "n_subtomos": 8}, + # base_angle 360: each lamni sub-tomogram sweeps the full circle + # (N = int(360 / step), LamNI._tomo_type1_actual_grid()), unlike + # flomni's 180-degree sub-tomograms -- without it the page showed + # half the real projection count (144 instead of 288 at 10 deg). + 1: {"kind": "equally_spaced_grid", "n_subtomos": 8, "base_angle": 360}, 2: {"kind": "golden_capped"}, 3: {"kind": "golden_capped"}, } @@ -72,6 +76,11 @@ class LamniWebpageGenerator(WebpageGeneratorBase): ("tomo_type", "Type"), ("tomo_countingtime", "Exposure (s)"), ("tomo_circfov", "Circular FOV (µm)"), + # the piezo range is the FOV of each ptycho tile actually scanned + ("lamni_piezo_range_x", "Piezo range x (µm)"), + ("lamni_piezo_range_y", "Piezo range y (µm)"), + ("manual_shift_x", "Manual shift x (mm)"), + ("manual_shift_y", "Manual shift y (mm)"), ("tomo_angle_stepsize", "Angle step (°)"), ("tomo_shellstep", "Shell step (µm)"), ("lamni_stitch_x", "Stitch x"), @@ -82,6 +91,7 @@ class LamniWebpageGenerator(WebpageGeneratorBase): ("golden_ratio_bunch_size", "Golden bunch size"), ("golden_max_number_of_projections", "Golden max projections"), ("golden_projections_at_0_deg_for_damage_estimation", "0° damage-check shots"), + ("zero_deg_reference_at_each_subtomo", "0° reference per sub-tomo"), ("ptycho_reconstruct_foldername", "Recon folder"), ] diff --git a/csaxs_bec/bec_ipython_client/plugins/LamNI/extra_tomo.py b/csaxs_bec/bec_ipython_client/plugins/LamNI/extra_tomo.py index b52af657..4ba826f7 100644 --- a/csaxs_bec/bec_ipython_client/plugins/LamNI/extra_tomo.py +++ b/csaxs_bec/bec_ipython_client/plugins/LamNI/extra_tomo.py @@ -61,8 +61,8 @@ class MagLamNI(LamNI): lamni_at_each_angle(self, angle) return + # tomo_scan_projection() adds the projection to the reconstruction queue self.tomo_scan_projection(angle, _internal=True) - self.tomo_reconstruct() class DataDrivenLamNI(LamNI): diff --git a/csaxs_bec/bec_ipython_client/plugins/LamNI/lamni.py b/csaxs_bec/bec_ipython_client/plugins/LamNI/lamni.py index 6df69807..f4847c67 100644 --- a/csaxs_bec/bec_ipython_client/plugins/LamNI/lamni.py +++ b/csaxs_bec/bec_ipython_client/plugins/LamNI/lamni.py @@ -163,10 +163,14 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools ) # Curated allow-list for the "move" queue action -- same beamline-wide - # devices as flomni's (mokev, idgap). Lamni has no equivalent of + # devices as flomni's (energy, idgap). Lamni has no equivalent of # flomni's ftray (sample-transfer tray) sim-testable bonus target. + # The energy device is "ccm_energy" (bl_optics_hutch.yaml; also what + # write_pdf_report()/lfzp_info() read) -- the legacy "mokev" only exists + # in device_configs/legacy/, so a queued "move Energy" job would have + # failed with a KeyError in dev["mokev"] and paused the queue. _TOMO_QUEUE_MOVE_DEVICES = { - "mokev": {"label": "Energy", "unit": "keV"}, + "ccm_energy": {"label": "Energy", "unit": "keV"}, "idgap": {"label": "Undulator gap", "unit": "mm"}, } @@ -575,9 +579,48 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools Args: keep_shutter_open: If True the shutter is left open after the frame. """ + all_valid, bad_states = self._check_beamline_states_valid() + if not all_valid: + print( + "Attention: not all beamline states are valid:\n " + + "\n ".join(bad_states) + + "\nThe beam may be down, so the frame could come back empty." + ) + if not self.OMNYTools.yesno("Take a frame anyway?", "n"): + print("Stopping.") + return aligner = XrayEyeAlignGUI(self.client, self) aligner.update_frame(keep_shutter_open=keep_shutter_open) + def _check_beamline_states_valid(self): + """Return (all_valid, invalid_labels). + + Reads every configured beamline state (bec.beamline_states) and + collects the ones whose status is not "valid" (i.e. "invalid", + "warning" or "unknown"). Used to warn before taking a manual X-ray + eye frame: if e.g. the beam is down, the frame would come back empty + and the user is left confused about why they see nothing. Ported from + Flomni._check_beamline_states_valid() (4fe8d02). + + Returns all_valid=True (with an empty list) if the beamline-state + machinery isn't available at all, so this never blocks on a session + that doesn't use beamline states. + """ + bec = builtins.__dict__.get("bec") + manager = getattr(bec, "beamline_states", None) if bec is not None else None + if manager is None: + return True, [] + + bad = [] + for name in list(getattr(manager, "_states", {})): + try: + status = manager.get_status_by_name(name) + except Exception: # pylint: disable=broad-except + continue + if status is not None and status != "valid": + bad.append(f"{name}: {status}") + return (len(bad) == 0), bad + def xrayeye_rotation_center_calibration_isolated(self, keep_shutter_open: bool = False): """Determine new lsamx/lsamy rotation-center values for a sparse/isolated particle, via the X-ray eye GUI. @@ -1275,8 +1318,18 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools # Scan projection # ------------------------------------------------------------------ - def tomo_scan_projection(self, angle: float, _internal: bool = False): - """Acquire one fermat-scan projection at `angle`. + def tomo_scan_projection( + self, angle: float, _internal: bool = False, _reconstruct: bool = True + ): + """Acquire one fermat-scan projection at `angle` and add it to the + reconstruction queue (tomo_reconstruct()), like + Flomni.tomo_scan_projection(). + + Reconstruction lives here, not in _at_each_angle(), so that custom + at_each_angle hooks -- which call ``setup.tomo_scan_projection(angle, + _internal=True)`` (see tomo_queue_mixin's hook example) -- and direct + calls also get their projection reconstructed; it used to be + skipped for them. _internal=True is passed by callers that already sit inside an already-checked flow (tomo_scan()'s/tomo_alignment_scan()'s own @@ -1284,6 +1337,10 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools below only runs once per tomogram/alignment run instead of once per projection. Direct/standalone calls (the default) are checked every time, mirroring Flomni.tomo_scan_projection()'s _internal convention. + + _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") @@ -1345,6 +1402,9 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools corridor_size=corridor_size, ) + if _reconstruct: + self.tomo_reconstruct() + def write_alignment_scan_numbers( self, first_scan: int, scans: list[int] | None = None ) -> None: @@ -1437,7 +1497,7 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools for attempt in range(1, max_attempts + 1): start_scan_number = bec.queue.next_scan_number try: - self.tomo_scan_projection(angle, _internal=True) + self.tomo_scan_projection(angle, _internal=True, _reconstruct=False) break except AlarmBase as exc: if exc.alarm_type != "TimeoutError" or attempt == max_attempts: @@ -1511,8 +1571,9 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools 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_reconstruct() # ------------------------------------------------------------------ # Progress reporting @@ -1864,7 +1925,13 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools ): """Return angles for equally spaced tomography with golden ratio sub-tomogram starting angles.""" angular_step = maxangle / number_of_projections_per_subtomo - subtomo_number = int((ii * angular_step) / maxangle) + 1 + # Exact integer division. The equivalent float expression + # int((ii * angular_step) / maxangle) + 1 truncates just below an + # integer for ii == k * n for some n (e.g. n = 19, 33, 38, 39, 66, + # ...): the first projection of a sub-tomogram was then labelled with + # the previous sub-tomogram number and acquired as a duplicate of its + # closing angle, and the next sub-tomogram skipped its first angle. + subtomo_number = int(ii // number_of_projections_per_subtomo) + 1 start_angle = self._golden(subtomo_number - 1, 1, angular_step)[0] projection_number_of_subtomo = ii - (subtomo_number - 1) * number_of_projections_per_subtomo @@ -2321,8 +2388,14 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools self.lamni_piezo_range_y = self._get_val( " um", self.lamni_piezo_range_y, float, min_=0.1, max_=79.9 ) - self.lamni_stitch_x = self._get_val("", self.lamni_stitch_x, int) - self.lamni_stitch_y = self._get_val("", self.lamni_stitch_y, int) + # min 0: a negative stitch makes range(-n, n + 1) empty, so a + # projection would silently run zero scans. max 50 = the GUI's range. + self.lamni_stitch_x = self._get_val( + "", self.lamni_stitch_x, int, min_=0, max_=50 + ) + self.lamni_stitch_y = self._get_val( + "", self.lamni_stitch_y, int, min_=0, max_=50 + ) self.tomo_stitch_overlap = self._get_val( " um", self.tomo_stitch_overlap, float, min_=0.0, max_=10.0 ) @@ -2333,7 +2406,7 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools "Reconstruction queue", self.ptycho_reconstruct_foldername, str ) self.frames_per_trigger = self._get_val( - "Frames per trigger (burst)", self.frames_per_trigger, int + "Frames per trigger (burst)", self.frames_per_trigger, int, min_=1, max_=100 ) print("Tomography type:") @@ -2344,7 +2417,11 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools if self.tomo_type == 1: _, _, current_total = self._tomo_type1_actual_grid() - tomo_numberofprojections = self._get_val("Number of projections", current_total, int) + # min 8: fewer than one projection per sub-tomogram makes + # N = int(360 / step) zero and divides by zero later. + tomo_numberofprojections = self._get_val( + "Number of projections", current_total, int, min_=8, max_=10000 + ) self.tomo_angle_stepsize = 360 / tomo_numberofprojections * 8 # Report what was ACTUALLY achieved, via the same helper # sub_tomo_scan() itself uses -- not the raw value just typed in, @@ -2370,20 +2447,19 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools ) elif self.tomo_type == 2: - while True: - bunch_size = self._get_val( - "Number of projections sorted per bunch (minimum 20)", - self.golden_ratio_bunch_size, - int, - ) - if bunch_size >= 20: - self.golden_ratio_bunch_size = bunch_size - break - print("Bunch size must be at least 20. Please try again.") + self.golden_ratio_bunch_size = self._get_val( + "Number of projections sorted per bunch (minimum 20)", + self.golden_ratio_bunch_size, + int, + min_=20, + max_=100, + ) self.golden_max_number_of_projections = self._get_val( "Stop after number of projections (0 for endless)", self.golden_max_number_of_projections, int, + min_=0, + max_=1_000_000, ) self.golden_projections_at_0_deg_for_damage_estimation = self._get_val( "Repeat projections at 0 deg every second subtomo 1/0?", @@ -2393,13 +2469,19 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools elif self.tomo_type == 3: numprj = self._get_val( - "Number of projections per sub-tomogram", int(360 / self.tomo_angle_stepsize), int + "Number of projections per sub-tomogram", + int(360 / self.tomo_angle_stepsize), + int, + min_=1, + max_=10000, ) self.tomo_angle_stepsize = 360 / numprj self.golden_max_number_of_projections = self._get_val( "Stop after number of projections (0 for endless)", self.golden_max_number_of_projections, int, + min_=0, + max_=1_000_000, ) self.golden_projections_at_0_deg_for_damage_estimation = self._get_val( "Repeat projections at 0 deg every second subtomo 1/0?", @@ -2467,7 +2549,52 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools piezo_range = f"{self.lamni_piezo_range_x:.2f}/{self.lamni_piezo_range_y:.2f}" stitching = f"{self.lamni_stitch_x:.2f}/{self.lamni_stitch_y:.2f}" dataset_id = str(self.client.queue.next_dataset_number) - _, _, report_total_projections = self._tomo_type1_actual_grid() + # bec.active_account, not client.username: the e-account the data is + # stored under (same as flomni, c1ad2fc). + account = getattr(builtins.__dict__.get("bec"), "active_account", None) or "N/A" + # The correct total-projections / sub-tomogram-count / angular-step + # values depend on which tomography type is actually configured -- + # mirrors tomo_parameters()'s tomo_type 1/2/3 branching and flomni's + # write_pdf_report() (5a22651). This used to always call + # _tomo_type1_actual_grid(), which is only meaningful for type 1: a + # golden-ratio scan (type 2/3) reported a stale, unrelated type-1 + # total left over in the persistent tomo_angle_stepsize global var + # instead of golden_max_number_of_projections, plus a hardcoded "8 + # sub-tomograms". + if self.tomo_type == 1: + _, achievable_step, total_projections = self._tomo_type1_actual_grid() + subtomo_count_label = "Number of individual sub-tomograms:" + subtomo_count_value = "8" + angle_step_value = f"{achievable_step:.2f}" + elif self.tomo_type == 3: + total_projections = ( + self.golden_max_number_of_projections + if self.golden_max_number_of_projections > 0 + else 0 + ) + 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}" + else: + # tomo_type == 2: golden ratio tomography -- there is no fixed set + # of sub-tomograms or a fixed angular step, so report the bunch + # size instead (matches tomo_parameters()'s printout). + total_projections = ( + self.golden_max_number_of_projections + if self.golden_max_number_of_projections > 0 + else 0 + ) + subtomo_count_label = "Sorted in bunches of:" + subtomo_count_value = f"{self.golden_ratio_bunch_size}" + angle_step_value = "N/A (golden ratio)" + total_projections_str = ( + f"{total_projections:.0f}" if total_projections else "N/A (manual stop)" + ) + last_scan_number_str = ( + f"{self.client.queue.next_scan_number + total_projections + 10:.0f}" + if total_projections + else "N/A" + ) # The energy readback device is "ccm_energy", not "mokev" -- and may # not be configured/available in every session (e.g. some simulated # configs). Read it defensively so a missing/misbehaving device @@ -2509,7 +2636,13 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools fzp_sample_distance_str = "N/A" # Sample-to-detector distance: userParameter on loptx, defaults to # -1 (unknown) until someone measures and sets it. - detector_distance = self._get_user_param_safe("loptx", "detector_distance") + # Read defensively like the other device values above: a missing + # parameter must not crash the report after add_sample_database() + # already registered this tomogram. + try: + detector_distance = self._get_user_param_safe("loptx", "detector_distance") + except Exception: + detector_distance = None detector_distance_str = ( f"{detector_distance:.1f}" if detector_distance and detector_distance > 0 else "N/A" ) @@ -2518,19 +2651,18 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools f"{'Measurement ID:':<{padding}}{self.tomo_id}\n", f"{'Dataset ID:':<{padding}}{dataset_id}\n", f"{'Sample Info:':<{padding}}Sample Info\n", - f"{'e-account:':<{padding}}{self.client.username}\n", - f"{'Number of projections:':<{padding}}{report_total_projections}\n", + f"{'e-account:':<{padding}}{account}\n", + f"{'Number of projections:':<{padding}}{total_projections_str}\n", f"{'First scan number:':<{padding}}{self.client.queue.next_scan_number}\n", - f"{'Last scan number approx.:':<{padding}}" - f"{self.client.queue.next_scan_number + report_total_projections + 10}\n", + f"{'Last scan number approx.:':<{padding}}{last_scan_number_str}\n", f"{'Current photon energy:':<{padding}}{energy_str}\n", f"{'Exposure time:':<{padding}}{self.tomo_countingtime:.2f}\n", f"{'Fermat spiral step size:':<{padding}}{self.tomo_shellstep:.2f}\n", f"{'Piezo range (FOV sample plane):':<{padding}}{piezo_range}\n", f"{'Restriction to circular FOV:':<{padding}}{self.tomo_circfov:.2f}\n", f"{'Stitching:':<{padding}}{stitching}\n", - f"{'Number of individual sub-tomograms:':<{padding}}8\n", - f"{'Angular step within sub-tomogram:':<{padding}}{self.tomo_angle_stepsize:.2f}\n", + f"{subtomo_count_label:<{padding}}{subtomo_count_value}\n", + f"{'Angular step within sub-tomogram:':<{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", diff --git a/csaxs_bec/bec_ipython_client/plugins/LamNI/lamni_alignment_mixin.py b/csaxs_bec/bec_ipython_client/plugins/LamNI/lamni_alignment_mixin.py index e404710a..a60d914f 100644 --- a/csaxs_bec/bec_ipython_client/plugins/LamNI/lamni_alignment_mixin.py +++ b/csaxs_bec/bec_ipython_client/plugins/LamNI/lamni_alignment_mixin.py @@ -202,8 +202,10 @@ class LamNIAlignmentMixin: .. important:: This method reads from the live GUI widget. If the XRayEye GUI window has been closed since the alignment was performed the call - will fail. In that case use :meth:`read_xray_eye_correction` to - reload from the archived text files. + will fail. (The former file-based fallback + ``read_xray_eye_correction()`` is disabled -- see the commented-out + code above -- so re-open the X-ray eye GUI and let the fit converge + again, or re-run the alignment.) """ _dev = builtins.__dict__.get("dev") tomo_fit_xray_eye = np.zeros((2, 3)) @@ -223,9 +225,8 @@ class LamNIAlignmentMixin: f"{type(params).__name__ if not isinstance(params, dict) else 'a dict missing keys'}). " "This means the fit hasn't converged / DAP forwarding hasn't produced " "a result yet, not that the correction data is missing. Wait for the " - "fit to converge in the XRayEye GUI's Fit tab, or use " - "read_xray_eye_correction() to reload from the archived text files " - "instead." + "fit to converge in the XRayEye GUI's Fit tab (keep the XRayEye " + "window open), or re-run the alignment." ) tomo_fit_xray_eye[0][0] = params_x["SineModel_0_amplitude"] @@ -314,7 +315,6 @@ class LamNIAlignmentMixin: with open(correction_file, "r") as f: num_elements = f.readline() int_num_elements = int(num_elements.split(" ")[2]) - print(int_num_elements) corr_pos_x = [] corr_pos_y = [] corr_angle = [] diff --git a/csaxs_bec/bec_ipython_client/plugins/LamNI/lamni_optics_mixin.py b/csaxs_bec/bec_ipython_client/plugins/LamNI/lamni_optics_mixin.py index d3e7e398..27998ae8 100644 --- a/csaxs_bec/bec_ipython_client/plugins/LamNI/lamni_optics_mixin.py +++ b/csaxs_bec/bec_ipython_client/plugins/LamNI/lamni_optics_mixin.py @@ -459,11 +459,15 @@ class LamNIOpticsMixin: def lfzp_info(self, mokev_val=-1): if mokev_val == -1: + # The energy readback device is "ccm_energy" (same as flomni's + # ffzp_info() and LamNI.write_pdf_report()); the legacy "mokev" + # device is not in the current configs, so this always bailed out. try: - mokev_val = dev.mokev.readback.get() + mokev_val = dev.ccm_energy.read(cached=True)[dev.ccm_energy.name]["value"] except Exception: print( - "Device mokev does not exist. You can specify the energy in keV as an argument instead." + "Could not read the photon energy from device ccm_energy. " + "You can specify the energy in keV as an argument instead." ) return loptz_val = dev.loptz.read()["loptz"]["value"] diff --git a/csaxs_bec/bec_ipython_client/plugins/LamNI/x_ray_eye_align.py b/csaxs_bec/bec_ipython_client/plugins/LamNI/x_ray_eye_align.py index 0cff8843..cc03dabd 100644 --- a/csaxs_bec/bec_ipython_client/plugins/LamNI/x_ray_eye_align.py +++ b/csaxs_bec/bec_ipython_client/plugins/LamNI/x_ray_eye_align.py @@ -273,6 +273,35 @@ class XrayEyeAlign: return False def align(self, keep_shutter_open: bool = False): + """Run the full LamNI X-ray eye alignment (see _align_impl() for the + procedure). + + Whatever ends the run -- success, Ctrl-C or ANY other exception (a GUI + RPC error, a missing GUI output key, ...) -- the GUI is put back into + a clean state afterwards: DAP parameter forwarding off and the + alignment crosshair hidden. Previously only KeyboardInterrupt (handled + by the caller) did this, so any other failure left forwarding on and + the crosshair drawn. Mirrors flomni's XrayEyeAlign.align() try/finally + (LamNI has no skip button or ROI vertical lock to reset). + """ + try: + self.gui.reset_zoom() + except Exception as exc: # pylint: disable=broad-except + logger.warning(f"Failed to reset the XRayEye zoom: {exc}") + + try: + self._align_impl(keep_shutter_open) + finally: + try: + self.gui.set_dap_params_forwarding(False) + except Exception as exc: # pylint: disable=broad-except + logger.warning(f"Failed to disable XRayEye DAP parameter forwarding: {exc}") + try: + self.gui.hide_crosshair() + except Exception as exc: # pylint: disable=broad-except + logger.warning(f"Failed to hide XRayEye alignment crosshair: {exc}") + + def _align_impl(self, keep_shutter_open: bool = False): """Run the full LamNI X-ray eye alignment. Args: @@ -556,7 +585,7 @@ class XrayEyeAlign: self.tomo_rotate(0) print( "Done. You are ready to remove the X-ray eye and start ptychography scans.\n" - "Fine alignment: lamni.tomo_parameters() with offset 0, then lamni.sub_tomo_scan(1,0)" + "Fine alignment: lamni.tomo_parameters(), then lamni.tomo_alignment_scan()" ) # ------------------------------------------------------------------ diff --git a/csaxs_bec/bec_ipython_client/plugins/OMNY_shared/webpage_generator_base.py b/csaxs_bec/bec_ipython_client/plugins/OMNY_shared/webpage_generator_base.py index 0bdddcb1..38028410 100644 --- a/csaxs_bec/bec_ipython_client/plugins/OMNY_shared/webpage_generator_base.py +++ b/csaxs_bec/bec_ipython_client/plugins/OMNY_shared/webpage_generator_base.py @@ -404,14 +404,21 @@ class WebpageGeneratorBase: takes. Used by the UI to compute total-projection counts for queued jobs without hardcoding a per-instrument formula in JS. Recognised "kind"s: - "equally_spaced_grid" (params: n_subtomos) - total = floor(180 / angle_stepsize) * n_subtomos + "equally_spaced_grid" (params: n_subtomos, + optional base_angle, + default 180) + total = floor(base_angle / angle_stepsize) + * n_subtomos (independent of angle_range - see Flomni._subtomo_angle_plan()'s docstring for why: in 360-degree mode, angle_range only splits the n_subtomos into complementary low/high halves, it does not change the - total count) + total count). base_angle is the angular span + ONE sub-tomogram covers: 180 for flomni, 360 + for lamni (which always sweeps the full + circle per sub-tomogram -- see + LamNI._tomo_type1_actual_grid()). "golden_capped" total = golden_max_number_of_projections TQ_PARAM_DISPLAY -- list of (global_var_key, display_label) pairs, in @@ -2701,13 +2708,15 @@ function calcProjections(params){{ return (gmax!=null&&gmax>0)?gmax:null; }} if(def.kind==='equally_spaced_grid'){{ - // Always floor(180/step), never floor(angle_range/step): in 360-degree - // mode angle_range only splits the sub-tomograms into complementary - // low/high halves, it does not change the total projection count - - // see Flomni._subtomo_angle_plan()'s docstring. + // Always floor(base_angle/step), never floor(angle_range/step): in + // 360-degree mode angle_range only splits the sub-tomograms into + // complementary low/high halves, it does not change the total + // projection count - see Flomni._subtomo_angle_plan()'s docstring. + // base_angle is the span of one sub-tomogram: 180 (default, flomni) or + // 360 (lamni, which sweeps the full circle in every sub-tomogram). const step=params.tomo_angle_stepsize; if(step>0){{ - const N=Math.floor(180/step); + const N=Math.floor((def.base_angle||180)/step); return N*(def.n_subtomos||1); }} }} diff --git a/csaxs_bec/bec_ipython_client/plugins/flomni/flomni.py b/csaxs_bec/bec_ipython_client/plugins/flomni/flomni.py index a2ed2c7d..5d83d2e7 100644 --- a/csaxs_bec/bec_ipython_client/plugins/flomni/flomni.py +++ b/csaxs_bec/bec_ipython_client/plugins/flomni/flomni.py @@ -2600,18 +2600,26 @@ class Flomni( if not self.special_angles: self._current_special_angles = [] - if self._current_special_angles: - next_special_angle = self._current_special_angles[0] - if np.isclose(angle, next_special_angle, atol=0.5): - self._current_special_angles.pop(0) - num_repeats = self.special_angle_repeats - else: - num_repeats = 1 + # num_repeats is always defined (it used to be unbound whenever + # special_angles was set but this angle wasn't near the next one + # -> UnboundLocalError, retried 10x by @scan_repeat) and the + # special-angle list is consumed only after the projection + # succeeded, so a retried attempt still sees the same special + # angle and repeat count. + num_repeats = 1 + is_special_angle = bool( + self._current_special_angles + and np.isclose(angle, self._current_special_angles[0], atol=0.5) + ) + if is_special_angle: + num_repeats = self.special_angle_repeats start_scan_number = bec.queue.next_scan_number projection_start = time.perf_counter() for i in range(num_repeats): self._at_each_angle(angle) projection_duration = time.perf_counter() - projection_start + if is_special_angle: + self._current_special_angles.pop(0) end_scan_number = bec.queue.next_scan_number for scan_nr in range(start_scan_number, end_scan_number): diff --git a/csaxs_bec/bec_widgets/widgets/tomo_params/tomo_params.py b/csaxs_bec/bec_widgets/widgets/tomo_params/tomo_params.py index 5a464656..f6b22a81 100644 --- a/csaxs_bec/bec_widgets/widgets/tomo_params/tomo_params.py +++ b/csaxs_bec/bec_widgets/widgets/tomo_params/tomo_params.py @@ -676,8 +676,16 @@ class OMNY_TomoParams(BECWidget, QWidget): self._add_int( form, "_numprj_type3", "Projections per sub-tomo (type 3)", min_=1, max_=10000 ) + # per-setup floor (SETUP_PROFILES[...]["golden_bunch_size_min"]): lamni's + # golden_ratio_bunch_size setter/CLI reject < 20, so a GUI-legal + # smaller value would raise ValueError in tomo_queue_execute() (which + # setattr()s every job param) and pause the queue. self._add_int( - form, "golden_ratio_bunch_size", "Bunch size (type 2 only)", min_=1, max_=100 + form, + "golden_ratio_bunch_size", + "Bunch size (type 2 only)", + min_=self._profile.get("golden_bunch_size_min", 1), + max_=100, ) self._add_double( form, @@ -2322,6 +2330,7 @@ SETUP_PROFILES: dict[str, dict[str, Any]] = { "has_180_mode": True, "has_single_point": True, "has_zero_deg_reference": 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), @@ -2362,14 +2371,23 @@ SETUP_PROFILES: dict[str, dict[str, Any]] = { "has_180_mode": False, "has_single_point": False, "has_zero_deg_reference": True, + # lamni.golden_ratio_bunch_size setter/CLI require >= 20 (see + # _build_params_panel's bunch-size spinbox) + "golden_bunch_size_min": 20, "fov_fields": [("tomo_circfov", "Circular FOV (µm, 0 = disabled)", 0.0, 500.0, 2)], "stitch_fields": [ ("lamni_stitch_x", "Stitch x", 0, 50), ("lamni_stitch_y", "Stitch y", 0, 50), ], + # lamni's manual_shift_x/y are in mm end to end (added to the mm scan + # centre in tomo_scan_projection -> lamni_fermat_scan(shift_x=..., + # Units.mm), applied to lsamx/lsamy in mm; the CLI prints ). This + # was labelled "(µm)" with a +-1000 range, copied from flomni's field + # (which really is um): typing "5" as um would have shifted the + # scan by 5 mm. +-1 mm is the span the old um label implied. "manual_shift_fields": [ - ("manual_shift_x", "Manual shift x (µm)", -1000.0, 1000.0, 3), - ("manual_shift_y", "Manual shift y (µm)", -1000.0, 1000.0, 3), + ("manual_shift_x", "Manual shift x (mm)", -1.0, 1.0, 4), + ("manual_shift_y", "Manual shift y (mm)", -1.0, 1.0, 4), ], "piezo_range_fields": [ ("lamni_piezo_range_x", "Piezo range x (µm)", 0.1, 79.9, 2), diff --git a/tests/tests_bec_ipython_client/test_flomni_special_angles.py b/tests/tests_bec_ipython_client/test_flomni_special_angles.py new file mode 100644 index 00000000..7cfab251 --- /dev/null +++ b/tests/tests_bec_ipython_client/test_flomni_special_angles.py @@ -0,0 +1,88 @@ +"""Special-angle handling in Flomni._tomo_scan_at_angle(). + +num_repeats used to be unbound whenever ``special_angles`` was set but the +current angle wasn't near the next special angle (UnboundLocalError, retried +10x by @scan_repeat and then aborting the tomogram), and the special-angle +list was consumed before the projection ran, so a retried attempt no longer +saw its special angle. (Same fix as LamNI's, see test_lamni_golive_fixes.py.) +""" + +import builtins +import types + +import csaxs_bec.bec_ipython_client.plugins.flomni.flomni as flomni_module +from csaxs_bec.bec_ipython_client.plugins.flomni.flomni import Flomni, _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 + + +def _make_flomni(monkeypatch): + obj = object.__new__(Flomni) + obj.client = _FakeClient() + obj._progress_proxy = _ProgressProxy(obj.client) + obj.tomo_angle_range = 180 + obj.special_angles = [] + obj.special_angle_repeats = 3 + obj._current_special_angles = [] + obj._print_progress = lambda: None + obj._format_duration = lambda seconds: "0s" + obj._write_tomo_scan_number = lambda *a, **k: None + obj._log_projection_timing = lambda **k: None + fake_bec = types.SimpleNamespace(queue=types.SimpleNamespace(next_scan_number=1)) + monkeypatch.setitem(builtins.__dict__, "bec", fake_bec) + monkeypatch.setattr(flomni_module, "bec", fake_bec, raising=False) + return obj + + +def test_non_special_angle_with_special_list_does_not_crash(monkeypatch): + flomni = _make_flomni(monkeypatch) + flomni.special_angles = [90.0] + flomni._current_special_angles = [90.0] + calls = [] + flomni._at_each_angle = lambda angle: calls.append(angle) + + flomni._tomo_scan_at_angle(0.0, 1) + + assert calls == [0.0] + assert flomni._current_special_angles == [90.0] + + +def test_special_angle_repeats_and_is_consumed_once(monkeypatch): + flomni = _make_flomni(monkeypatch) + flomni.special_angles = [90.0] + flomni._current_special_angles = [90.0] + calls = [] + flomni._at_each_angle = lambda angle: calls.append(angle) + + flomni._tomo_scan_at_angle(90.0, 1) + + assert calls == [90.0] * 3 + assert flomni._current_special_angles == [] + + +def test_special_angle_repeats_survive_a_retry(monkeypatch): + flomni = _make_flomni(monkeypatch) + flomni.special_angles = [90.0] + flomni._current_special_angles = [90.0] + calls = [] + + def _at_each_angle(angle): + calls.append(angle) + if len(calls) == 1: + raise RuntimeError("transient failure") + + flomni._at_each_angle = _at_each_angle + + flomni._tomo_scan_at_angle(90.0, 1) # @scan_repeat retries once + + assert len(calls) == 1 + 3, "the retry must still repeat the special angle" + assert flomni._current_special_angles == [] diff --git a/tests/tests_bec_ipython_client/test_lamni_get_val_range.py b/tests/tests_bec_ipython_client/test_lamni_get_val_range.py index 8e5774f1..2227eb63 100644 --- a/tests/tests_bec_ipython_client/test_lamni_get_val_range.py +++ b/tests/tests_bec_ipython_client/test_lamni_get_val_range.py @@ -44,3 +44,125 @@ def test_get_val_boundary_values_are_accepted(monkeypatch): _fake_inputs(monkeypatch, ["0.025", "20.0"]) assert LamNI._get_val("a", 0.0, float, min_=0.025, max_=20.0) == 0.025 assert LamNI._get_val("b", 0.0, float, min_=0.025, max_=20.0) == 20.0 + + +# ── tomo_parameters_cli(): the prompts must enforce sane ranges ───────────── +# +# It only enforced ranges on some prompts: a negative stitch made +# range(-n, n + 1) empty (a projection silently ran zero scans), fewer than 8 +# projections gave N = 0 sub-tomogram angles (ZeroDivisionError later), 0 +# projections per type-3 sub-tomogram divided by zero, and frames per trigger / +# the golden-ratio stop count were unbounded. Ranges mirror flomni's (158dc80, +# 1dd03d1) and the tomo-params GUI (stitch 0-50 is lamni's own GUI range). + + +class _CliClient: + 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 + + +def _make_cli_lamni(monkeypatch, answers: dict): + """A bare LamNI whose tomo_parameters_cli() is answered by prompt keyword: + answers maps a substring of the prompt to the list of replies given for + it, in order; anything not listed accepts the default (empty reply).""" + import types + + from csaxs_bec.bec_ipython_client.plugins.LamNI.lamni import _ProgressProxy + + lamni = object.__new__(LamNI) + lamni.client = _CliClient() + lamni._progress_proxy = _ProgressProxy(lamni.client) + lamni._expected_fermat_position_count = lambda: 1000 + lamni._fermat_min_positions = lambda: 10 + lamni._describe_active_hook = lambda: "" + lamni.at_each_angle_hook = None + lamni.reconstructor = types.SimpleNamespace(folder_name=None) + lamni.OMNYTools = types.SimpleNamespace(yesno=lambda *a, **k: False) # "not correct" -> edit + + queues = {key: list(vals) for key, vals in answers.items()} + + def _input(prompt=""): + for key, vals in queues.items(): + if key in prompt and vals: + return vals.pop(0) + return "" + + monkeypatch.setattr(builtins, "input", _input) + # the piezo-range setters consult dev.rtx.user_parameter + import csaxs_bec.bec_ipython_client.plugins.LamNI.lamni as lamni_module + + fake_dev = types.SimpleNamespace(rtx=types.SimpleNamespace(user_parameter={})) + monkeypatch.setattr(lamni_module, "dev", fake_dev, raising=False) + return lamni + + +def test_cli_rejects_negative_stitch(monkeypatch, capsys): + lamni = _make_cli_lamni(monkeypatch, {"": ["-1", "2"], "": ["-3", "0"]}) + + lamni.tomo_parameters_cli() + + assert (lamni.lamni_stitch_x, lamni.lamni_stitch_y) == (2, 0) + assert "must be >= 0" in capsys.readouterr().out + + +def test_cli_rejects_out_of_range_frames_per_trigger(monkeypatch): + lamni = _make_cli_lamni(monkeypatch, {"Frames per trigger": ["0", "500", "5"]}) + + lamni.tomo_parameters_cli() + + assert lamni.frames_per_trigger == 5 + + +def test_cli_type1_rejects_fewer_than_8_projections(monkeypatch): + lamni = _make_cli_lamni( + monkeypatch, {"Tomography type": ["1"], "Number of projections": ["0", "4", "144"]} + ) + + lamni.tomo_parameters_cli() + + assert lamni.tomo_angle_stepsize == 20.0 # 360 / 144 * 8 + assert lamni._tomo_type1_actual_grid()[2] == 144 + + +def test_cli_type2_enforces_bunch_size_of_at_least_20(monkeypatch, capsys): + lamni = _make_cli_lamni( + monkeypatch, + { + "Tomography type": ["2"], + "sorted per bunch": ["5", "19", "150", "25"], + "Stop after": ["-1", "2000000", "800"], + }, + ) + + lamni.tomo_parameters_cli() + + assert lamni.golden_ratio_bunch_size == 25 + assert lamni.golden_max_number_of_projections == 800 + out = capsys.readouterr().out + assert "must be >= 20" in out + assert "must be <= 100" in out + assert "must be <= 1000000" in out + + +def test_cli_type2_zero_is_an_allowed_endless_stop_count(monkeypatch): + lamni = _make_cli_lamni(monkeypatch, {"Tomography type": ["2"], "Stop after": ["0"]}) + + lamni.tomo_parameters_cli() + + assert lamni.golden_max_number_of_projections == 0 + + +def test_cli_type3_rejects_zero_projections_per_subtomogram(monkeypatch): + lamni = _make_cli_lamni( + monkeypatch, {"Tomography type": ["3"], "per sub-tomogram": ["0", "-4", "36"]} + ) + + lamni.tomo_parameters_cli() + + assert lamni.tomo_angle_stepsize == 10.0 # 360 / 36 diff --git a/tests/tests_bec_ipython_client/test_lamni_golive_fixes.py b/tests/tests_bec_ipython_client/test_lamni_golive_fixes.py index 537a65f6..588b1381 100644 --- a/tests/tests_bec_ipython_client/test_lamni_golive_fixes.py +++ b/tests/tests_bec_ipython_client/test_lamni_golive_fixes.py @@ -364,3 +364,205 @@ def test_write_to_scilog_without_messaging_service(monkeypatch): obj.client = FakeClient() monkeypatch.setitem(builtins.__dict__, "bec", types.SimpleNamespace()) assert obj.write_to_scilog("x", ["t"]) is False + + +# ── tomo_scan_projection adds the projection to the reconstruction queue ──── +# +# Like flomni, reconstruction is done by tomo_scan_projection() itself, so +# custom at_each_angle hooks (which call tomo_scan_projection(_internal=True)) +# and direct calls are reconstructed too. It used to live in _at_each_angle(), +# so hooks silently produced no reconstruction entries. + + +def make_lamni_for_projection(fake_bec): + obj = object.__new__(LamNI) + obj.client = FakeClient() + obj._progress_proxy = _ProgressProxy(obj.client) + obj.compute_additional_correction = lambda angle: (0, 0) + obj.compute_additional_correction_2 = lambda angle: (0, 0) + obj.lamni_compute_additional_correction_xeye_mu = lambda angle, verbose=True: (0, 0) + obj.reconstruct_calls = [] + obj.tomo_reconstruct = lambda: obj.reconstruct_calls.append(1) + obj.fermat_calls = [] + fake_scans = types.SimpleNamespace( + lamni_fermat_scan=lambda **kw: obj.fermat_calls.append(kw) + ) + return obj, fake_scans + + +@pytest.fixture +def projection_env(monkeypatch, fake_bec): + obj, fake_scans = make_lamni_for_projection(fake_bec) + monkeypatch.setitem(builtins.__dict__, "scans", fake_scans) + return obj + + +def test_tomo_scan_projection_reconstructs_exactly_once(projection_env): + projection_env.tomo_scan_projection(30.0, _internal=True) + + assert len(projection_env.fermat_calls) == 1 + assert projection_env.reconstruct_calls == [1] + + +def test_tomo_scan_projection_reconstructs_once_per_projection_not_per_tile(projection_env): + """With stitching several Fermat scans make up one projection; the + reconstruction entry is written once, after all tiles.""" + projection_env.lamni_stitch_x = 1 # 3 x 1 tiles + projection_env.tomo_scan_projection(30.0, _internal=True) + + assert len(projection_env.fermat_calls) == 3 + assert projection_env.reconstruct_calls == [1] + + +def test_tomo_scan_projection_can_skip_reconstruction(projection_env): + projection_env.tomo_scan_projection(30.0, _internal=True, _reconstruct=False) + + assert len(projection_env.fermat_calls) == 1 + assert projection_env.reconstruct_calls == [] + + +def test_custom_hook_using_tomo_scan_projection_gets_reconstructed(projection_env): + """The documented hook pattern (tomo_queue_mixin's example) is just + setup.tomo_scan_projection(angle, _internal=True).""" + + def hook(setup, angle): + setup.tomo_scan_projection(angle, _internal=True) + + projection_env.at_each_angle_hook = "my_hook" + projection_env._resolve_at_each_angle_hook = lambda name, err: hook + + projection_env._at_each_angle(45.0) + + assert projection_env.reconstruct_calls == [1] + + +def test_default_at_each_angle_reconstructs_only_once(projection_env): + projection_env.at_each_angle_hook = None + + projection_env._at_each_angle(45.0) + + assert projection_env.reconstruct_calls == [1], "must not be reconstructed twice" + + +# ── tomo_type 3: sub-tomogram boundaries must be exact ────────────────────── + + +@pytest.mark.parametrize("n_per_subtomo", range(1, 401)) +def test_type3_subtomo_numbering_is_exact(n_per_subtomo): + """subtomo_number was int((ii * step) / 360) + 1, which truncates one + below the integer for ii == k * n at ~35 values of n < 400 (e.g. 19, 33, + 38, 39, 66): the first projection of a sub-tomogram got the previous + sub-tomogram's number and a duplicate of its closing angle.""" + lamni = object.__new__(LamNI) + for ii in range(0, 6 * n_per_subtomo + 1): + angle, subtomo = lamni._golden_equally_spaced(ii, n_per_subtomo, maxangle=360, reverse=True) + assert subtomo == ii // n_per_subtomo + 1, (n_per_subtomo, ii) + assert 0 <= angle < 720 + + +@pytest.mark.parametrize("n_per_subtomo", [19, 33, 38, 39, 66, 76, 78, 103, 161]) +def test_type3_each_subtomo_has_n_distinct_angles(n_per_subtomo): + lamni = object.__new__(LamNI) + for subtomo in range(1, 5): + angles = [ + lamni._golden_equally_spaced( + (subtomo - 1) * n_per_subtomo + p, n_per_subtomo, maxangle=360, reverse=True + )[0] + % 360 + for p in range(n_per_subtomo) + ] + assert len({round(a, 6) for a in angles}) == n_per_subtomo, (n_per_subtomo, subtomo) + + +# ── xrayeye_update_frame warns when the beamline states are not valid ─────── + + +class _FakeStates: + def __init__(self, statuses): + self._states = dict(statuses) + + def get_status_by_name(self, name): + status = self._states[name] + if isinstance(status, Exception): + raise status + return status + + +def _update_frame_lamni(monkeypatch, states, answer_yes): + obj = object.__new__(LamNI) + obj.client = FakeClient() + fake_bec = ( + types.SimpleNamespace(beamline_states=states) + if states is not None + else types.SimpleNamespace() + ) + monkeypatch.setitem(builtins.__dict__, "bec", fake_bec) + asked = [] + obj.OMNYTools = types.SimpleNamespace( + yesno=lambda question, default: asked.append((question, default)) or answer_yes + ) + frames = [] + + class FakeAligner: + def __init__(self, client, lamni): + pass + + def update_frame(self, keep_shutter_open=False): + frames.append(keep_shutter_open) + + monkeypatch.setattr(lamni_module, "XrayEyeAlignGUI", FakeAligner) + return obj, frames, asked + + +def test_update_frame_takes_a_frame_when_all_states_are_valid(monkeypatch): + lamni, frames, asked = _update_frame_lamni( + monkeypatch, _FakeStates({"beam": "valid", "shutter": "valid"}), answer_yes=False + ) + + lamni.xrayeye_update_frame(keep_shutter_open=True) + + assert frames == [True] + assert asked == [] + + +def test_update_frame_warns_and_stops_when_a_state_is_invalid(monkeypatch, capsys): + lamni, frames, asked = _update_frame_lamni( + monkeypatch, _FakeStates({"beam": "invalid", "shutter": "valid"}), answer_yes=False + ) + + lamni.xrayeye_update_frame() + + assert frames == [] + assert asked == [("Take a frame anyway?", "n")] + out = capsys.readouterr().out + assert "beam: invalid" in out + assert "shutter" not in out.split("Attention")[1] + + +def test_update_frame_proceeds_if_the_user_insists(monkeypatch): + lamni, frames, _ = _update_frame_lamni( + monkeypatch, _FakeStates({"beam": "warning"}), answer_yes=True + ) + + lamni.xrayeye_update_frame() + + assert frames == [False] + + +def test_update_frame_never_blocks_without_beamline_states(monkeypatch): + lamni, frames, asked = _update_frame_lamni(monkeypatch, None, answer_yes=False) + + lamni.xrayeye_update_frame() + + assert frames == [False] + assert asked == [] + + +def test_update_frame_ignores_states_that_cannot_be_read(monkeypatch): + lamni, frames, _ = _update_frame_lamni( + monkeypatch, _FakeStates({"flaky": RuntimeError("unreadable"), "beam": "valid"}), True + ) + + lamni.xrayeye_update_frame() + + assert frames == [False] diff --git a/tests/tests_bec_ipython_client/test_lamni_osa_collision_info.py b/tests/tests_bec_ipython_client/test_lamni_osa_collision_info.py index 1a441213..88afbc8d 100644 --- a/tests/tests_bec_ipython_client/test_lamni_osa_collision_info.py +++ b/tests/tests_bec_ipython_client/test_lamni_osa_collision_info.py @@ -144,3 +144,37 @@ def test_lfzp_info_notes_parked_out(fake_dev, capsys): out = capsys.readouterr().out assert "likely parked OUT" in out assert "Remaining space if OSA is moved to its IN position" in out + + +class FakeCcmEnergy: + """ccm_energy is a PSISimplePositioner: read(cached=True) -> {name: {"value": ...}}.""" + + name = "ccm_energy" + + def read(self, cached=False): + return {"ccm_energy": {"value": 6.2}} + + +def test_lfzp_info_reads_the_energy_from_ccm_energy(fake_dev, capsys): + """It used to read dev.mokev, which only exists in device_configs/legacy/, + so lfzp_info() without an argument always bailed out.""" + dev = fake_dev(collision_offset=113.0) + del dev.mokev # not in the current configs + dev.ccm_energy = FakeCcmEnergy() + + make_optics().lfzp_info() + + out = capsys.readouterr().out + assert "6.2000 keV" in out + assert "Could not read" not in out + + +def test_lfzp_info_without_energy_device_explains_and_returns(fake_dev, capsys): + dev = fake_dev(collision_offset=113.0) + del dev.mokev + + make_optics().lfzp_info() # no ccm_energy either: must not raise + + out = capsys.readouterr().out + assert "ccm_energy" in out + assert "keV" in out # tells the user to pass the energy explicitly diff --git a/tests/tests_bec_ipython_client/test_lamni_pdf_report.py b/tests/tests_bec_ipython_client/test_lamni_pdf_report.py new file mode 100644 index 00000000..f790a818 --- /dev/null +++ b/tests/tests_bec_ipython_client/test_lamni_pdf_report.py @@ -0,0 +1,172 @@ +"""Tests for LamNI.write_pdf_report()'s report content. + +It always used the tomo_type-1 grid (int(360/tomo_angle_stepsize) * 8), so a +golden-ratio tomogram (tomo_type 2/3) reported a stale type-1 projection count +left over in the persistent tomo_angle_stepsize global var, and a hardcoded +"8 sub-tomograms" -- fixed like flomni's 5a22651. It also read the +e-account from client.username (flomni: bec.active_account, c1ad2fc), and a +missing detector_distance userParameter crashed the report after the +tomogram had already been registered. + +Drives the real method with a fake PDFWriter that captures the report text. +""" + +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, _ProgressProxy + + +class FakeClient: + def __init__(self): + self._vars = {} + self.queue = types.SimpleNamespace(next_dataset_number=7, next_scan_number=1000) + self.username = "unix_user" + + def get_global_var(self, key): + return self._vars.get(key) + + def set_global_var(self, key, value): + self._vars[key] = value + + +class FakePDFWriter: + written: list[str] = [] + + def __init__(self, path): + self._pdf = types.SimpleNamespace(image=lambda *a, **k: None, ln=lambda *a, **k: None) + + def __enter__(self): + return self + + def __exit__(self, *exc): + return False + + def write(self, text): + FakePDFWriter.written.append(text) + + +@pytest.fixture +def report_lamni(monkeypatch, tmp_path): + FakePDFWriter.written = [] + monkeypatch.setattr(lamni_module, "PDFWriter", FakePDFWriter) + monkeypatch.setattr( + "os.path.expanduser", + lambda p: p.replace("~", str(tmp_path)) if p.startswith("~") else p, + ) + monkeypatch.setitem( + builtins.__dict__, + "bec", + types.SimpleNamespace(active_account="e12345", messaging=types.SimpleNamespace()), + ) + monkeypatch.setitem(builtins.__dict__, "dev", types.SimpleNamespace()) + + obj = object.__new__(LamNI) + obj.client = FakeClient() + obj._progress_proxy = _ProgressProxy(obj.client) + obj.tomo_id = 42 + obj.sample_name = "sample1" + obj._add_psi_footer = lambda file: None + obj._upload_pdf_report_to_samples = lambda path: None + obj._describe_active_hook = lambda: "" + obj._active_hook_source = lambda: None + obj._get_user_param_safe = lambda device, var: {"detector_distance": 1500.0}.get(var, "x") + return obj + + +def _report(lamni) -> str: + lamni.write_pdf_report() + assert FakePDFWriter.written, "no report was written" + return FakePDFWriter.written[0] + + +def _line(report: str, label: str) -> str: + for line in report.splitlines(): + if line.startswith(label): + return line[len(label) :].strip() + raise AssertionError(f"{label!r} not in report:\n{report}") + + +def test_type1_report_uses_the_grid(report_lamni): + report_lamni.tomo_type = 1 + report_lamni.tomo_angle_stepsize = 10.0 + + report = _report(report_lamni) + + assert _line(report, "Number of projections:") == "288" + assert _line(report, "Number of individual sub-tomograms:") == "8" + assert _line(report, "Angular step within sub-tomogram:") == "10.00" + assert _line(report, "Last scan number approx.:") == "1298" # 1000 + 288 + 10 + + +def test_type1_report_shows_the_achievable_step_not_the_configured_one(report_lamni): + """7 deg does not divide 360: N = 51, achievable step 360/51 = 7.06.""" + report_lamni.tomo_type = 1 + report_lamni.tomo_angle_stepsize = 7.0 + + report = _report(report_lamni) + + assert _line(report, "Number of projections:") == str(51 * 8) + assert _line(report, "Angular step within sub-tomogram:") == "7.06" + + +def test_golden_ratio_report_does_not_use_the_stale_type1_grid(report_lamni): + report_lamni.tomo_type = 2 + report_lamni.tomo_angle_stepsize = 10.0 # stale type-1 leftover: would give 288 + report_lamni.golden_max_number_of_projections = 500 + report_lamni.golden_ratio_bunch_size = 30 + + report = _report(report_lamni) + + assert _line(report, "Number of projections:") == "500" + assert _line(report, "Sorted in bunches of:") == "30" + assert _line(report, "Angular step within sub-tomogram:") == "N/A (golden ratio)" + assert "Number of individual sub-tomograms" not in report + assert _line(report, "Last scan number approx.:") == "1510" + + +def test_golden_ratio_endless_scan_reports_manual_stop(report_lamni): + report_lamni.tomo_type = 2 + report_lamni.golden_max_number_of_projections = 0 + report_lamni.golden_ratio_bunch_size = 20 + + report = _report(report_lamni) + + assert _line(report, "Number of projections:") == "N/A (manual stop)" + assert _line(report, "Last scan number approx.:") == "N/A" + + +def test_type3_report_uses_projections_per_full_circle_subtomo(report_lamni): + report_lamni.tomo_type = 3 + report_lamni.tomo_angle_stepsize = 10.0 + report_lamni.golden_max_number_of_projections = 400 + + report = _report(report_lamni) + + assert _line(report, "Number of projections:") == "400" + # lamni sub-tomograms span 360 degrees (flomni: 180) + assert _line(report, "Number of projections per sub-tomogram:") == "36.0" + assert _line(report, "Angular step within sub-tomogram:") == "10.00" + + +def test_report_uses_the_active_account(report_lamni): + report_lamni.tomo_type = 1 + report = _report(report_lamni) + assert _line(report, "e-account:") == "e12345" + + +def test_report_survives_a_missing_detector_distance(report_lamni): + def _get(device, var): + if var == "detector_distance": + raise ValueError("no user parameter definition") + return "x" + + report_lamni._get_user_param_safe = _get + report_lamni.tomo_type = 1 + + report = _report(report_lamni) + + assert _line(report, "Sample-to-detector distance:") == "N/A mm" diff --git a/tests/tests_bec_ipython_client/test_lamni_tomo_alignment_scan.py b/tests/tests_bec_ipython_client/test_lamni_tomo_alignment_scan.py index 95a48aa3..a3d45ab8 100644 --- a/tests/tests_bec_ipython_client/test_lamni_tomo_alignment_scan.py +++ b/tests/tests_bec_ipython_client/test_lamni_tomo_alignment_scan.py @@ -85,7 +85,7 @@ def test_tomo_alignment_scan_aborts_without_xray_eye_fit(monkeypatch): lamni = make_lamni(monkeypatch, xray_eye_fit=None) lamni._confirm_sequence_override = lambda *a, **k: False calls = [] - lamni.tomo_scan_projection = lambda angle: calls.append(angle) + lamni.tomo_scan_projection = lambda angle, **kw: calls.append(angle) lamni.tomo_alignment_scan() @@ -97,7 +97,7 @@ def test_tomo_alignment_scan_runs_12_projections_across_360(monkeypatch): lamni = make_lamni(monkeypatch, xray_eye_fit=[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]) calls = [] - def _fake_projection(angle, _internal=False): + def _fake_projection(angle, _internal=False, **kw): calls.append(angle) builtins.__dict__["bec"].queue.next_scan_number += 1 @@ -115,7 +115,7 @@ def test_tomo_alignment_scan_runs_12_projections_across_360(monkeypatch): def test_tomo_alignment_scan_rotates_back_to_zero(monkeypatch): lamni = make_lamni(monkeypatch, xray_eye_fit=[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]) - lamni.tomo_scan_projection = lambda angle, _internal=False: None + lamni.tomo_scan_projection = lambda angle, _internal=False, **kw: None umv_calls = [] monkeypatch.setattr(lamni_module, "umv", lambda *a: umv_calls.append(a), raising=False) @@ -166,7 +166,7 @@ def test_tomo_alignment_scan_retries_timed_out_angle_and_records_real_scan_numbe monkeypatch.setattr(lamni_module.time, "sleep", lambda s: None) calls = [] - def _projection(angle, _internal=False): + def _projection(angle, _internal=False, **kw): calls.append(angle) builtins.__dict__["bec"].queue.next_scan_number += 1 # each attempt uses a scan number if len(calls) == 3: # 3rd call = first attempt at the 3rd angle @@ -189,3 +189,17 @@ def test_tomo_alignment_scan_retries_timed_out_angle_and_records_real_scan_numbe assert [float(a) for a in lines[1].split()] == expected_angles content, _tags = lamni._scilog_calls[0] assert "Number of alignment scans: 12" in content + + +def test_tomo_alignment_scan_projections_are_not_queued_for_reconstruction(monkeypatch): + """Alignment projections were never added to the reconstruction queue on + lamni; moving tomo_reconstruct() into tomo_scan_projection() must not + change that.""" + lamni = make_lamni(monkeypatch, xray_eye_fit=[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]) + kwargs_seen = [] + lamni.tomo_scan_projection = lambda angle, **kw: kwargs_seen.append(kw) + + lamni.tomo_alignment_scan() + + assert len(kwargs_seen) == 12 + assert all(kw.get("_reconstruct") is False for kw in kwargs_seen) diff --git a/tests/tests_bec_ipython_client/test_lamni_tomo_params_widget_math.py b/tests/tests_bec_ipython_client/test_lamni_tomo_params_widget_math.py index 7b7b06f4..909c3abc 100644 --- a/tests/tests_bec_ipython_client/test_lamni_tomo_params_widget_math.py +++ b/tests/tests_bec_ipython_client/test_lamni_tomo_params_widget_math.py @@ -249,3 +249,76 @@ def test_compute_fermat_positions_lamni_zero_step_is_safe(): ) assert count == 0 assert min_positions == LamniFermatScan.MIN_POSITIONS + + +# ── golden bunch-size floor + manual-shift units ──────────────────────────── +# +# lamni's golden_ratio_bunch_size setter/CLI reject < 20, but the shared GUI +# spinbox allowed 1..100: a GUI-queued job with a smaller value made +# tomo_queue_execute()'s setattr() raise ValueError and pause the queue. +# The manual-shift fields were labelled um (+-1000) but lamni's manual shift is +# mm end to end (added to the mm scan centre, applied to lsamx/lsamy in mm). + + +def _bare_widget_for(setup: str): + from qtpy.QtWidgets import QApplication + + from csaxs_bec.bec_widgets.widgets.tomo_params.tomo_params import OMNY_TomoParams + + QApplication.instance() or QApplication([]) + widget = OMNY_TomoParams.__new__(OMNY_TomoParams) + widget._profile = SETUP_PROFILES[setup] + widget._pw = {} + return widget + + +def test_lamni_bunch_size_spinbox_floor_is_20(): + widget = _bare_widget_for("lamni") + box = widget._build_type23_section() # keep a reference: it owns the spinboxes + assert box is not None + assert widget._pw["golden_ratio_bunch_size"].minimum() == 20 + assert widget._pw["golden_ratio_bunch_size"].maximum() == 100 + + +def test_flomni_bunch_size_spinbox_is_unchanged(): + widget = _bare_widget_for("flomni") + box = widget._build_type23_section() # keep a reference: it owns the spinboxes + assert box is not None + assert widget._pw["golden_ratio_bunch_size"].minimum() == 1 + + +def test_lamni_gui_bunch_size_floor_matches_the_setter(): + """Every value the GUI can produce must be accepted by lamni's setter.""" + + 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 + + lamni = object.__new__(LamNI) + lamni.client = FakeClient() + floor = SETUP_PROFILES["lamni"]["golden_bunch_size_min"] + + lamni.golden_ratio_bunch_size = floor # must not raise + with pytest.raises(ValueError): + lamni.golden_ratio_bunch_size = floor - 1 + + +def test_lamni_manual_shift_fields_are_in_mm(): + fields = SETUP_PROFILES["lamni"]["manual_shift_fields"] + assert [f[0] for f in fields] == ["manual_shift_x", "manual_shift_y"] + for _key, label, min_, max_, _decimals in fields: + assert "(mm)" in label and "µm" not in label + 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) diff --git a/tests/tests_bec_ipython_client/test_lamni_tomo_queue.py b/tests/tests_bec_ipython_client/test_lamni_tomo_queue.py index a35f12e3..0397b2d8 100644 --- a/tests/tests_bec_ipython_client/test_lamni_tomo_queue.py +++ b/tests/tests_bec_ipython_client/test_lamni_tomo_queue.py @@ -166,17 +166,19 @@ def test_tomo_queue_execute_resumes_incomplete_job(): def test_tomo_queue_add_command_move_action(monkeypatch): lamni = make_lamni() - fake_dev = {"mokev": "mokev-device"} + # lamni's energy device is ccm_energy (the legacy "mokev" is not in the + # current configs) + fake_dev = {"ccm_energy": "ccm_energy-device"} moved = [] monkeypatch.setattr(tomo_queue_mixin, "dev", fake_dev, raising=False) monkeypatch.setattr(tomo_queue_mixin, "umv", lambda *a: moved.append(a), raising=False) lamni.tomo_queue_add_command( - {"action": "move", "kwargs": {"positions": {"mokev": 6.2}}}, label="reconfigure" + {"action": "move", "kwargs": {"positions": {"ccm_energy": 6.2}}}, label="reconfigure" ) lamni.tomo_queue_execute() - assert moved == [("mokev-device", 6.2)] + assert moved == [("ccm_energy-device", 6.2)] job = lamni._tomo_queue_proxy.as_list()[0] assert job["kind"] == "command" assert job["status"] == "done" @@ -204,16 +206,19 @@ def test_register_at_each_angle_hook_dispatches_from_at_each_angle(): def test_at_each_angle_default_path_unchanged_without_hook(): - """No hook registered -- must fall through to the exact same default - behaviour lamni had before this port: tomo_scan_projection + tomo_reconstruct.""" + """No hook registered -- falls through to tomo_scan_projection(), which + itself adds the projection to the reconstruction queue (like flomni), so + _at_each_angle() must NOT call tomo_reconstruct() a second time.""" lamni = make_lamni() calls = [] - lamni.tomo_scan_projection = lambda angle, _internal=False: calls.append(("projection", angle)) + lamni.tomo_scan_projection = lambda angle, _internal=False, **kw: calls.append( + ("projection", angle, _internal) + ) lamni.tomo_reconstruct = lambda: calls.append(("reconstruct",)) lamni._at_each_angle(30.0) - assert calls == [("projection", 30.0), ("reconstruct",)] + assert calls == [("projection", 30.0, True)] def test_at_each_angle_raises_for_unregistered_hook_name(): diff --git a/tests/tests_bec_ipython_client/test_tomo_queue_reacquire.py b/tests/tests_bec_ipython_client/test_tomo_queue_reacquire.py index 2d1bf83c..8d59dce1 100644 --- a/tests/tests_bec_ipython_client/test_tomo_queue_reacquire.py +++ b/tests/tests_bec_ipython_client/test_tomo_queue_reacquire.py @@ -191,7 +191,7 @@ def test_tomo_queue_reacquire_resets_later_jobs_to_pending_tomo_and_command(): i0 = lamni.tomo_queue_add(label="job0") i1 = lamni.tomo_queue_add(label="job1") - lamni.tomo_queue_add_command({"action": "move", "kwargs": {"positions": {"mokev": 6.2}}}) + lamni.tomo_queue_add_command({"action": "move", "kwargs": {"positions": {"ccm_energy": 6.2}}}) i3 = lamni.tomo_queue_add(label="job3") for i in (i0, i1, i3): @@ -213,7 +213,7 @@ def test_tomo_queue_reacquire_rejects_out_of_range_job_index(): def test_tomo_queue_reacquire_rejects_command_job(): lamni = make_lamni() - lamni.tomo_queue_add_command({"action": "move", "kwargs": {"positions": {"mokev": 6.2}}}) + lamni.tomo_queue_add_command({"action": "move", "kwargs": {"positions": {"ccm_energy": 6.2}}}) with pytest.raises(TomoQueueError): lamni.tomo_queue_reacquire(0, projection_number=0) diff --git a/tests/tests_bec_ipython_client/test_webpage_projection_count.py b/tests/tests_bec_ipython_client/test_webpage_projection_count.py new file mode 100644 index 00000000..c74dc307 --- /dev/null +++ b/tests/tests_bec_ipython_client/test_webpage_projection_count.py @@ -0,0 +1,108 @@ +"""The status webpage computes a job's total projection count in JS +(calcProjections) from a per-setup TOMO_TYPES table. + +For lamni's equally-spaced tomo_type 1 it used to hardcode flomni's +floor(180/step) * 8, so the page showed half of the real count (144 instead of +288 at a 10 degree step): lamni sweeps the full 360 degrees in every +sub-tomogram (N = int(360/step), see LamNI._tomo_type1_actual_grid()). + +There is no JS engine in the test environment, so this checks the data the JS +consumes (the TOMO_TYPES JSON rendered into the page) and that the JS uses +it, and cross-checks the formula against the real LamNI grid. +""" + +import json +import math +import re + +import pytest + +from csaxs_bec.bec_ipython_client.plugins.flomni.flomni_webpage_generator import ( + FlomniWebpageGenerator, +) +from csaxs_bec.bec_ipython_client.plugins.LamNI.lamni import LamNI, _ProgressProxy +from csaxs_bec.bec_ipython_client.plugins.LamNI.LamNI_webpage_generator import ( + LamniWebpageGenerator, +) +from csaxs_bec.bec_ipython_client.plugins.OMNY_shared.webpage_generator_base import _render_html + + +class FakeClient: + """Minimal in-memory stand-in for BEC's global-var store.""" + + 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 + + +def _rendered_tomo_types(generator_cls) -> dict: + html = _render_html( + [], generator_cls.HAS_TOMO_QUEUE, generator_cls.TOMO_TYPES, generator_cls.TQ_PARAM_DISPLAY + ) + match = re.search(r"const TOMO_TYPES = (\{.*?\});", html, re.S) + assert match, "TOMO_TYPES not found in the rendered page" + # JSON object keys are strings once serialized + return json.loads(match.group(1)) + + +def _js_expression_for_grid(generator_cls) -> str: + html = _render_html( + [], generator_cls.HAS_TOMO_QUEUE, generator_cls.TOMO_TYPES, generator_cls.TQ_PARAM_DISPLAY + ) + match = re.search(r"const N=Math\.floor\((.*?)/step\);", html) + assert match, "grid projection formula not found in the rendered page" + return match.group(1) + + +def test_page_js_uses_the_per_setup_base_angle(): + assert _js_expression_for_grid(LamniWebpageGenerator) == "(def.base_angle||180)" + + +def test_lamni_grid_type_declares_360_degree_sub_tomograms(): + types = _rendered_tomo_types(LamniWebpageGenerator) + assert types["1"] == {"kind": "equally_spaced_grid", "n_subtomos": 8, "base_angle": 360} + + +def test_flomni_grid_type_is_unchanged(): + """flomni's page must keep computing floor(180/step) * 8.""" + types = _rendered_tomo_types(FlomniWebpageGenerator) + assert types["1"] == {"kind": "equally_spaced_grid", "n_subtomos": 8} + assert "base_angle" not in types["1"] + + +@pytest.mark.parametrize("stepsize", [10.0, 7.0, 25.0, 12.5, 5.0]) +def test_page_projection_count_matches_the_real_lamni_grid(stepsize): + grid = LamniWebpageGenerator.TOMO_TYPES[1] + page_total = math.floor(grid.get("base_angle", 180) / stepsize) * grid["n_subtomos"] + + lamni = object.__new__(LamNI) + lamni.client = FakeClient() + lamni._progress_proxy = _ProgressProxy(lamni.client) + lamni.tomo_angle_stepsize = stepsize + _n, _step, real_total = lamni._tomo_type1_actual_grid() + + assert page_total == real_total + + +def test_lamni_10_degree_step_shows_288_not_144(): + grid = LamniWebpageGenerator.TOMO_TYPES[1] + assert math.floor(grid["base_angle"] / 10.0) * grid["n_subtomos"] == 288 + + +def test_lamni_param_table_only_shows_real_lamni_parameters(): + """Every row of the page's parameter table must be a parameter the + tomo queue actually stores for lamni (or the special hook row).""" + known = set(LamNI._TOMO_SCAN_PARAM_NAMES) | {"tomo_type"} + for key, _label in LamniWebpageGenerator.TQ_PARAM_DISPLAY: + assert key in known, f"{key!r} is not a lamni tomo-queue parameter" + + +def test_lamni_param_table_shows_the_scan_fov_and_manual_shift(): + keys = [key for key, _label in LamniWebpageGenerator.TQ_PARAM_DISPLAY] + for wanted in ("lamni_piezo_range_x", "lamni_piezo_range_y", "manual_shift_x", "manual_shift_y"): + assert wanted in keys diff --git a/tests/tests_bec_ipython_client/test_x_ray_eye_align.py b/tests/tests_bec_ipython_client/test_x_ray_eye_align.py index 9987ebe4..d8024347 100644 --- a/tests/tests_bec_ipython_client/test_x_ray_eye_align.py +++ b/tests/tests_bec_ipython_client/test_x_ray_eye_align.py @@ -586,4 +586,60 @@ def test_find_rotation_center_smear_experimental_saves_composites_and_clicks(bec save_mock.assert_called_once() _, kwargs = save_mock.call_args assert kwargs["fzp_xy"] == (0.0, 0.0) - assert kwargs["sweep_deg"] == 45.0 \ No newline at end of file + assert kwargs["sweep_deg"] == 45.0 + +# ── align() must always leave the GUI clean ───────────────────────────────── +# +# Only KeyboardInterrupt (handled by the caller) used to reset the GUI; any +# other exception (a GUI RPC error, a missing GUI output key, ...) left DAP +# parameter forwarding on and the alignment crosshair drawn. Mirrors flomni's +# XrayEyeAlign.align() try/finally. + + +def _bare_aligner(): + aligner = object.__new__(XrayEyeAlign) + # .gui is a read-only property returning self.lamni.xeyegui + aligner.lamni = mock.MagicMock() + return aligner + + +def test_align_cleans_up_gui_after_success(): + aligner = _bare_aligner() + aligner._align_impl = mock.MagicMock() + + aligner.align(keep_shutter_open=True) + + aligner._align_impl.assert_called_once_with(True) + aligner.lamni.xeyegui.set_dap_params_forwarding.assert_called_with(False) + aligner.lamni.xeyegui.hide_crosshair.assert_called_once() + + +def test_align_cleans_up_gui_after_any_exception_and_reraises(): + aligner = _bare_aligner() + aligner._align_impl = mock.MagicMock(side_effect=KeyError("gui output key missing")) + + with pytest.raises(KeyError): + aligner.align() + + aligner.lamni.xeyegui.set_dap_params_forwarding.assert_called_with(False) + aligner.lamni.xeyegui.hide_crosshair.assert_called_once() + + +def test_align_cleanup_failures_do_not_mask_the_original_error(): + aligner = _bare_aligner() + aligner._align_impl = mock.MagicMock(side_effect=ValueError("real problem")) + aligner.lamni.xeyegui.set_dap_params_forwarding.side_effect = RuntimeError("GUI gone") + aligner.lamni.xeyegui.hide_crosshair.side_effect = RuntimeError("GUI gone") + + with pytest.raises(ValueError, match="real problem"): + aligner.align() + + +def test_align_survives_a_gui_without_reset_zoom(): + aligner = _bare_aligner() + aligner.lamni.xeyegui.reset_zoom.side_effect = AttributeError("no reset_zoom") + aligner._align_impl = mock.MagicMock() + + aligner.align() + + aligner._align_impl.assert_called_once_with(False) diff --git a/tests/tests_scans/test_lamni_fermat_scan.py b/tests/tests_scans/test_lamni_fermat_scan.py index d12595eb..f2da0c09 100644 --- a/tests/tests_scans/test_lamni_fermat_scan.py +++ b/tests/tests_scans/test_lamni_fermat_scan.py @@ -1,5 +1,7 @@ from unittest import mock +import numpy as np + from csaxs_bec.scans.lamni_fermat_scan import LamniFermatScan @@ -57,3 +59,39 @@ def test_lamni_fermat_spiral_reaches_elongated_fov_corners(): -fovx / 2, fovx / 2, -fovy / 2, fovy / 2, step=1, fovx=fovx, fovy=fovy ) assert len(pos) >= 60 + + +def _spiral(**overrides): + kwargs = dict( + m1_start=-40.0, + m1_stop=40.0, + m2_start=-40.0, + m2_stop=40.0, + step=10.0, + angle=0.0, + fovx=80.0, + fovy=80.0, + seed=0, + ) + kwargs.update(overrides) + return LamniFermatScan.get_lamni_fermat_spiral_pos(**kwargs) + + +def test_shift_and_center_are_mm_and_positions_are_um(): + """Unit contract of the LamNI scan: shift_x/shift_y (LamNI's manual shift) + and center_x/center_y (the FOV offsets) are in mm, the generated scan + positions handed to the RT controller are in um. A shift of 0.01 mm must + therefore move every position by exactly 10 um. + + (The tomo-parameters GUI used to label the manual shift "um" with a + +-1000 range -- copied from flomni -- although it is mm end to end.) + """ + base = _spiral() + shifted_x = _spiral(shift_x=0.01) + shifted_y = _spiral(shift_y=0.005) + centered_x = _spiral(center_x=0.01) # at angle 0, stitch 0: same physical shift + + assert base.shape == shifted_x.shape == shifted_y.shape == centered_x.shape + np.testing.assert_allclose(shifted_x - base, [[10.0, 0.0]] * len(base), atol=1e-9) + np.testing.assert_allclose(shifted_y - base, [[0.0, 5.0]] * len(base), atol=1e-9) + np.testing.assert_allclose(centered_x - base, [[10.0, 0.0]] * len(base), atol=1e-9)