Fixes/lamni #326

Merged
holler merged 13 commits from fixes/lamni into main 2026-09-29 13:07:47 +02:00
37 changed files with 2587 additions and 239 deletions

No files matched your search

@@ -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
@@ -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"),
]
@@ -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):
@@ -110,9 +110,7 @@ class DataDrivenLamNI(LamNI):
content = f"Loading tomo parameters from {fname}."
logger.warning(content)
msg = bec.logbook.LogbookMessage()
msg.add_text(content).add_tag(["Data_driven_file", "BEC"])
self.client.logbook.send_logbook_message(msg)
self.write_to_scilog(content, ["Data_driven_file"])
self._update_tomo_data_from_file(fname)
self._current_special_angles = self.special_angles.copy()
@@ -176,6 +174,10 @@ class DataDrivenLamNI(LamNI):
error_caught = False
if 0 <= angle < 360.05:
print(f"Starting LamNI scan for angle {angle}")
# default for angles that aren't a special angle; without it
# num_repeats is unbound whenever special_angles is set but
# this angle isn't near the next one in the list
num_repeats = 1
while not successful:
self._start_beam_check()
if not self.special_angles:
@@ -4,6 +4,10 @@ import builtins
import datetime
import time
from bec_lib import bec_logger
logger = bec_logger.logger
if builtins.__dict__.get("bec") is not None:
bec = builtins.__dict__.get("bec")
dev = builtins.__dict__.get("dev")
@@ -41,13 +45,19 @@ class LamniGuiTools:
def lamnigui_show_gui(self):
if "lamni" in self.gui.windows:
self.lamni_window = self.gui.windows["lamni"]
self.gui.lamni.raise_window()
# raise_window() is currently buggy (bec_widgets RPCServer "raise" handling can
# detach/hide the window instead of bringing it to front) - disabled so that
# calling a lamnigui_show_* macro again on an already-open GUI is a safe no-op
# instead of risking hiding the window. Same workaround as flomni's
# flomnigui_show_gui().
# self.gui.lamni.raise_window()
else:
self.lamni_window = self.gui.new("lamni", timeout=self.GUI_RPC_TIMEOUT)
time.sleep(1)
def lamnigui_raise(self):
self.gui.lamni.raise_window()
# raise_window() disabled, see lamnigui_show_gui().
pass
def lamnigui_stop_gui(self):
self.gui.lamni.hide()
@@ -198,20 +208,27 @@ class LamniGuiTools:
# ------------------------------------------------------------------
def lamnigui_show_progress(self):
self.lamnigui_show_gui()
if self._lamnigui_is_missing("progressbar"):
self.lamnigui_remove_all_docks()
self.progressbar = self.gui.lamni.new(
"RingProgressBar", timeout=self.GUI_RPC_TIMEOUT
)
# Outer ring: overall tomo progress (manual update)
self.progressbar.add_ring().set_update("manual")
# Middle ring: current sub-tomo progress (manual update)
self.progressbar.add_ring().set_update("manual")
# Inner ring: current scan progress (driven by BEC scan events)
self.progressbar.add_ring().set_update("scan")
# The progress GUI is a display only: it must never be able to abort
# a tomogram (a closed GUI / RPC timeout raising out of here would
# propagate through @scan_repeat and fail the projection). Same
# safeguard as flomni's flomnigui_show_progress() (dea43ed).
try:
self.lamnigui_show_gui()
if self._lamnigui_is_missing("progressbar"):
self.lamnigui_remove_all_docks()
self.progressbar = self.gui.lamni.new(
"RingProgressBar", timeout=self.GUI_RPC_TIMEOUT
)
# Outer ring: overall tomo progress (manual update)
self.progressbar.add_ring().set_update("manual")
# Middle ring: current sub-tomo progress (manual update)
self.progressbar.add_ring().set_update("manual")
# Inner ring: current scan progress (driven by BEC scan events)
self.progressbar.add_ring().set_update("scan")
self._lamnigui_update_progress()
self._lamnigui_update_progress()
except Exception as exc: # pylint: disable=broad-except
logger.warning(f"Error in lamnigui_show_progress: {exc}")
def _lamnigui_update_progress(self):
"""Update the progress ring bar and centre label from the current progress state.
@@ -220,18 +237,35 @@ class LamniGuiTools:
so this reflects the live state accessible from any BEC client session via::
client.get_global_var("tomo_progress")
Never raises: called from _print_progress() inside every projection, so a
GUI failure must not fail (and, via @scan_repeat, retry) the scan itself.
"""
try:
self._lamnigui_update_progress_impl()
except Exception as exc: # pylint: disable=broad-except
logger.warning(f"Error updating the LamNI progress GUI: {exc}")
@staticmethod
def _percent(numerator, denominator) -> float:
"""numerator/denominator in percent; 0 when the denominator is not
positive -- e.g. the total of an "endless" golden-ratio scan
(golden_max_number_of_projections == 0), which has no meaningful
percentage."""
if not denominator or denominator <= 0:
return 0.0
return numerator / denominator * 100
def _lamnigui_update_progress_impl(self):
if self.progressbar is None:
return
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)
@@ -269,11 +303,14 @@ class LamniGuiTools:
else:
finish_display = "N/A"
total = self.progress["total_projections"]
total_display = f"{total:.0f}" if total and total > 0 else "endless"
text = (
f"Progress report:\n"
f" Tomo type: {self.progress['tomo_type']}\n"
f" Projection: {self.progress['projection']:.0f}\n"
f" Total projections expected: {self.progress['total_projections']:.1f}\n"
f" Total projections expected: {total_display}\n"
f" Angle: {self.progress['angle']:.1f}\n"
f" Current subtomo: {self.progress['subtomo']}\n"
f" Current projection within subtomo: {self.progress['subtomo_projection']}\n"
@@ -130,6 +130,9 @@ class LamNIError(Exception):
class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools):
_TOMO_PARAMS_WINDOW_NAME = "lamni_tomo_params"
_TOMO_PARAMS_ERROR = LamNIError
# Ordered set of all global-var-backed tomo scan parameters snapshotted
# by tomo_queue_add()/restored by tomo_queue_execute(). Lamni's own
# names for overlapping flomni concepts (tomo_circfov instead of
@@ -160,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"},
}
@@ -210,9 +217,10 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
f"step={p.get('tomo_shellstep')}um ctime={p.get('tomo_countingtime')}s"
)
def __init__(self, client):
def __init__(self, client, csaxs=None):
super().__init__()
self.client = client
self.csaxs = csaxs
self.set_client(client)
self.device_manager = client.device_manager
self.init = LaMNIInitStages(client)
@@ -473,6 +481,11 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
for line in timing_lines:
print(line)
timing_content = "\n".join(timing_lines)
hook_source = self._active_hook_source()
if hook_source:
timing_content += (
f"\n\nAt-each-angle hook source ('{self.at_each_angle_hook}'):\n{hook_source}"
)
self.write_to_scilog(timing_content, ["tomoscan"])
@property
@@ -567,9 +580,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.
@@ -733,6 +785,21 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
def lights_on(self):
self.device_manager.devices.lsamx.controller.lights_on()
def lamni_move_to_scan_center(self, shift_x: float, shift_y: float, angle: float):
"""
LamNI scan to move the interferometer to the computed scan center based on the provided shift and angle.
Args:
shift_x (float): Shift x.
shift_y (float): Shift y.
angle (float): Angle.
Returns:
ScanReport
"""
scans = builtins.__dict__.get("scans")
return scans.lamni_move_to_scan_center(shift_x=shift_x, shift_y=shift_y, angle=angle)
# ------------------------------------------------------------------
# Global parameters (backed by BEC global vars)
# ------------------------------------------------------------------
@@ -992,17 +1059,38 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
# Logging helpers
# ------------------------------------------------------------------
def write_to_scilog(self, content, tags: list = None):
def _get_scilog(self):
"""The enabled scilog messaging service, or None (with a warning) if
scilog is unavailable/disabled -- e.g. a dev/sim session."""
scilog = getattr(getattr(bec, "messaging", None), "scilog", None)
if scilog is None or not getattr(scilog, "_enabled", False):
logger.warning("scilog is not enabled; skipping scilog entry.")
return None
return scilog
def write_to_scilog(self, content, tags: list = None) -> bool:
"""Write a text entry to scilog, tolerating scilog being disabled or
unreachable. This is documentation of a scan, not part of the scan
itself -- it must never fail a caller (in particular a tomo_queue
job, which would otherwise be marked "incomplete" and pause the whole
queue over a logging side effect). Uses the ``bec.messaging.scilog``
service like Flomni._scilog_write(); the old ``bec.logbook`` API this
used to call no longer exists in current bec_lib, so every entry was
silently dropped.
Returns True if the entry was sent, False if skipped/failed.
"""
scilog = self._get_scilog()
if scilog is None:
return False
try:
if tags is not None:
tags.append("BEC")
else:
tags = ["BEC"]
msg = bec.logbook.LogbookMessage()
msg.add_text(content).add_tag(tags)
self.client.logbook.send_logbook_message(msg)
except Exception:
logger.warning("Failed to write to scilog.")
tags = list(tags) if tags is not None else []
tags.append("BEC")
scilog.new().add_text(content.replace("\n", "<br>")).add_tags(tags).send()
return True
except Exception as exc: # pylint: disable=broad-except
logger.warning(f"Failed to write to scilog: {exc}")
return False
_TIMING_LOG_DIR = "~/data/raw/logs/timing_statistics"
_TIMING_SETUP = "lamni"
@@ -1231,8 +1319,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
@@ -1240,6 +1338,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")
@@ -1301,9 +1403,19 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
corridor_size=corridor_size,
)
def write_alignment_scan_numbers(self, first_scan: int) -> None:
if _reconstruct:
self.tomo_reconstruct()
def write_alignment_scan_numbers(
self, first_scan: int, scans: list[int] | None = None
) -> None:
"""Write the scan-number/angle/offset log consumed by BEC_ptycho_align.
``scans`` (optional): the actual scan number of each alignment point,
used to rewrite the file after the scan when a retried angle consumed
extra scan numbers. Defaults to the contiguous ``first_scan + k``
prediction written before the scan starts.
Mirrors Flomni.write_alignment_scan_numbers() exactly (same 4-line
format), adapted to lamni's own alignment-scan angle set (12 points
across the full 360 degrees, see tomo_alignment_scan()) and its own
@@ -1313,7 +1425,8 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
version makes.
"""
angles = list(np.linspace(0, 360, num=12, endpoint=False))
scans = [first_scan + k for k in range(len(angles))]
if scans is None:
scans = [first_scan + k for k in range(len(angles))]
x_vals = []
for angle in angles:
@@ -1378,29 +1491,38 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
)
self.lamnigui_show_alignment_progress()
first_scan_per_angle = []
max_attempts = 10
for idx, angle in enumerate(angles):
successful = False
print(f"Starting LamNI scan for angle {angle}")
while not successful:
for attempt in range(1, max_attempts + 1):
start_scan_number = bec.queue.next_scan_number
try:
start_scan_number = bec.queue.next_scan_number
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":
bec.queue.request_queue_reset()
time.sleep(2)
else:
raise exc
if exc.alarm_type != "TimeoutError" or attempt == max_attempts:
raise
# Re-acquire this angle after clearing the queue. This used
# to swallow the alarm and carry on, silently losing the
# angle while still logging the failed attempt's scan
# numbers as if it had succeeded.
print(f"Alignment angle {angle}: scan timed out, retrying ({attempt}).")
bec.queue.request_queue_reset()
time.sleep(2)
end_scan_number = bec.queue.next_scan_number
for scan_nr in range(start_scan_number, end_scan_number):
alignment_scan_numbers.append(scan_nr)
successful = True
end_scan_number = bec.queue.next_scan_number
first_scan_per_angle.append(start_scan_number)
alignment_scan_numbers.extend(range(start_scan_number, end_scan_number))
self.alignment_scan_progress.update(angle_index=idx + 1, angle=angle)
self._lamnigui_update_alignment_progress()
# The file written up front assumes first_scan + k per angle; a retried
# angle consumes extra scan numbers, so rewrite it with what was
# actually acquired.
self.write_alignment_scan_numbers(first_scan_per_angle[0], scans=first_scan_per_angle)
umv(dev.lsamrot, 0)
self.OMNYTools.printgreenbold(
"\n\nAlignment scan finished. Please run BEC_ptycho_align and load the new fit"
@@ -1421,12 +1543,26 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
self.write_to_scilog(scilog_content, ["alignmentscan"])
def tomo_reconstruct(self, base_path="~/data/raw/logs/reconstruction_queue"):
"""write the tomo reconstruct file for the reconstruction queue"""
"""Write the tomo reconstruct file for the reconstruction queue.
Normally called automatically at the end of tomo_scan_projection(),
which keeps self._current_scan_list up to date with the scan
number(s) of the projection just acquired (possibly several, when
stitching). When called directly -- e.g. from the command line after
a plain scans.lamni_fermat_scan(), without going through
tomo_scan_projection() -- that cached list is either stale (left over
from an earlier tomo scan) or not set at all, so fall back to just
the most recently completed scan number.
"""
bec = builtins.__dict__.get("bec")
next_scan_number = bec.queue.next_scan_number
last_scan_number = next_scan_number - 1
scan_list = getattr(self, "_current_scan_list", None)
if not scan_list or scan_list[-1] != last_scan_number:
scan_list = [last_scan_number]
self.reconstructor.folder_name = self.ptycho_reconstruct_foldername
self.reconstructor.write(
scan_list=self._current_scan_list,
next_scan_number=bec.queue.next_scan_number,
base_path=base_path,
scan_list=scan_list, next_scan_number=next_scan_number, base_path=base_path
)
def _at_each_angle(self, angle: float) -> None:
@@ -1436,8 +1572,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
@@ -1478,6 +1615,56 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
print(f"Estimated finish time: ........... {finish_str}\x1b[0m")
self._lamnigui_update_progress()
def _reset_progress_for_new_scan(self) -> None:
"""Reset ``tomo_progress`` (and refresh the progress GUI) for a brand-new
tomogram, in one global-var round-trip.
Every field must be reset, not just the timing/ETA ones: a finished
previous scan leaves projection == total_projections (100 %), its last
subtomo and angle behind, and the new scan's very first
_print_progress() -- fired by the 0-degree reference shot, before the
grid loop has written a single field -- would otherwise show those
stale values (rings at 100 %, old subtomo/angle, and an ETA of "0 s"
computed from the old projection count against the NEW start time).
subtomo is set to 1 (not 0) so a scan interrupted during the 0-degree
shot is still resumable by tomo_scan_resume().
"""
if self.tomo_type == 1:
tomo_type_name = "Equally spaced sub-tomograms"
subtomo_total, _, total = self._tomo_type1_actual_grid()
elif self.tomo_type == 2:
tomo_type_name = "Golden ratio tomography"
subtomo_total = self.golden_ratio_bunch_size
total = self.golden_max_number_of_projections
elif self.tomo_type == 3:
tomo_type_name = "Equally spaced, golden ratio starting angles"
subtomo_total = 360 / self.tomo_angle_stepsize
total = self.golden_max_number_of_projections
else:
raise ValueError(f"Unknown tomo_type: {self.tomo_type}.")
self.progress.update(
{
"tomo_type": tomo_type_name,
"subtomo": 1,
"subtomo_projection": 0,
# totals are used as divisors by the GUI rings -- never 0
"subtomo_total_projections": subtomo_total or 1,
"projection": 0,
"total_projections": total if total and total > 0 else 1,
"angle": 0,
"tomo_start_time": datetime.datetime.now().isoformat(),
# no leftover ETA from a previous scan until this one has
# accumulated enough projections to compute a fresh one
"estimated_remaining_time": None,
"estimated_finish_time": None,
"heartbeat": None,
"accumulated_idle_time": 0.0,
}
)
self._lamnigui_update_progress()
# ------------------------------------------------------------------
# Tomo scan orchestration
# ------------------------------------------------------------------
@@ -1511,35 +1698,64 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
(no tomo_angle_range/forward-reverse split -- lamni always scans
the full 360 degrees).
Args:
start_angle (float, optional): resume point -- the literal angle
(on this sub-tomogram's own grid) to continue from, NOT the
origin of a new sweep. The returned angles are then the
remaining tail of the normal sweep, beginning at (and
including) this angle. Raises ValueError if it isn't on
the grid. Mirrors Flomni._subtomo_angle_plan().
Returns:
angles (np.ndarray): the N angles (degrees) for this sub-tomogram.
angles (np.ndarray): the N angles (degrees) for this sub-tomogram
(or, when resuming, the remaining ones from start_angle on).
subtomo_offset (int): index of angles[0] within the full
sub-tomogram sweep (0 unless resuming), for progress numbering.
N (int): number of projections in this (and every) sub-tomogram.
"""
N = int(360.0 / tomo_angle_stepsize)
achievable_step = 360.0 / N
if start_angle is None:
# Phase offset must be a fraction of the ACHIEVABLE step (after
# int() truncation), not the raw configured tomo_angle_stepsize --
# they only coincide when 360/tomo_angle_stepsize happens to
# already be a whole number. Using the raw value here breaks the
# interlacing scheme: sub-tomograms combined in pairs/quads/all-8
# land on unevenly-spaced angles instead of the intended
# progressively finer, evenly-spaced grid at every combination
# level. Mirrors Flomni._subtomo_angle_plan() exactly.
offsets = {1: 0, 2: 4, 3: 2, 4: 6, 5: 1, 6: 5, 7: 3, 8: 7}
start_angle = achievable_step / 8.0 * offsets[subtomo_number]
# Phase offset must be a fraction of the ACHIEVABLE step (after
# int() truncation), not the raw configured tomo_angle_stepsize --
# they only coincide when 360/tomo_angle_stepsize happens to
# already be a whole number. Using the raw value here breaks the
# interlacing scheme: sub-tomograms combined in pairs/quads/all-8
# land on unevenly-spaced angles instead of the intended
# progressively finer, evenly-spaced grid at every combination
# level. Mirrors Flomni._subtomo_angle_plan() exactly.
offsets = {1: 0, 2: 4, 3: 2, 4: 6, 5: 1, 6: 5, 7: 3, 8: 7}
sweep_start = achievable_step / 8.0 * offsets[subtomo_number]
angle_end = start_angle + 360
# endpoint=False: a full 360-degree sweep must NOT re-measure both
# start_angle and start_angle+360 -- they're the same physical angle,
# sweep_start and sweep_start+360 -- they're the same physical angle,
# unlike a [0, 180) span's two distinct endpoints.
angles = np.linspace(start_angle, angle_end, num=N, endpoint=False)
angles = np.linspace(sweep_start, sweep_start + 360, num=N, endpoint=False)
if not (subtomo_number % 2):
angles = np.flip(angles)
return angles, N
if start_angle is None:
return angles, 0, N
# Resuming: locate start_angle on this sub-tomogram's own grid (with
# 360-degree wrap-around) and continue from there in the same
# direction. start_angle must NOT be used as the sweep origin -- that
# would generate angles past 360 (silently skipped by
# _tomo_scan_at_angle's range gate) and never reach the remaining
# part of the sweep.
diff = np.abs((angles - start_angle + 180.0) % 360.0 - 180.0)
subtomo_offset = int(np.argmin(diff))
# start_angle normally comes straight from progress["angle"] (taken
# from this same grid), so only float noise is tolerated here -- the
# nearest grid point is always within step/2, so a looser check
# would never catch a genuinely off-grid angle.
if diff[subtomo_offset] > 0.01:
raise ValueError(
f"start_angle {start_angle} is not on the angle grid of sub-tomogram "
f"{subtomo_number} (tomo_angle_stepsize={tomo_angle_stepsize})."
)
return angles[subtomo_offset:], subtomo_offset, N
def _resolve_type1_projection(self, projection_number: int) -> tuple[int, float]:
"""Map a flat 0-indexed tomo_type-1 projection number to
@@ -1551,7 +1767,7 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
tomography_scannumbers.txt log resolves to the same angle
sub_tomo_scan() would have produced at that position.
"""
_, N = self._subtomo_angle_plan(1, self.tomo_angle_stepsize)
_, _, N = self._subtomo_angle_plan(1, self.tomo_angle_stepsize)
total = N * 8
if not 0 <= projection_number < total:
raise ValueError(
@@ -1559,20 +1775,20 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
f"(tomo_angle_stepsize={self.tomo_angle_stepsize})."
)
subtomo_number = projection_number // N + 1
angles, _ = self._subtomo_angle_plan(subtomo_number, self.tomo_angle_stepsize)
angles, _, _ = self._subtomo_angle_plan(subtomo_number, self.tomo_angle_stepsize)
return subtomo_number, float(angles[projection_number % N])
def sub_tomo_scan(self, subtomo_number, start_angle=None):
"""Perform one sub-tomogram (tomo_type 1 only)."""
angles, N = self._subtomo_angle_plan(
angles, subtomo_offset, N = self._subtomo_angle_plan(
subtomo_number, self.tomo_angle_stepsize, start_angle=start_angle
)
total_projections = N * 8
for angle in angles:
for i, angle in enumerate(angles):
self.progress["tomo_type"] = "Equally spaced sub-tomograms"
self.progress["subtomo"] = subtomo_number
self.progress["subtomo_projection"] = np.where(angles == angle)[0][0]
self.progress["subtomo_projection"] = subtomo_offset + i
self.progress["subtomo_total_projections"] = N
self.progress["projection"] = (subtomo_number - 1) * N + self.progress[
"subtomo_projection"
@@ -1610,8 +1826,6 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
@scan_repeat(max_repeats=10, default=True, exc_handler=_retry_unless_lamni_error)
def _tomo_scan_at_angle(self, angle, subtomo_number):
successful = False
error_caught = False
if 0 <= angle < 360.05:
now = datetime.datetime.now()
prev_heartbeat_str = self.progress.get("heartbeat")
@@ -1643,49 +1857,54 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
print(f"Starting LamNI scan for angle {angle} in subtomo {subtomo_number}")
self._print_progress()
while not successful:
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
try:
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
error_caught = False
except AlarmBase as exc:
if exc.alarm_type == "TimeoutError":
bec.queue.request_queue_reset()
time.sleep(2)
error_caught = True
else:
raise exc
end_scan_number = bec.queue.next_scan_number
for scan_nr in range(start_scan_number, end_scan_number):
self._write_tomo_scan_number(scan_nr, angle, subtomo_number)
if not self.special_angles:
self._current_special_angles = []
# num_repeats is decided without consuming the special-angle list,
# which is only popped once the projection succeeded: this method
# runs under @scan_repeat, so a failed attempt is retried and must
# see the same special angle (and repeat count) again.
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
if not error_caught:
# Only reached for a clean completion -- an
# AlarmBase/TimeoutError attempt above has no reliable
# duration measurement, so it's excluded from the
# timing log rather than polluting it.
self._log_projection_timing(
angle=angle,
subtomo_number=subtomo_number,
duration_s=projection_duration,
start_scan_number=start_scan_number,
end_scan_number=end_scan_number,
)
start_scan_number = bec.queue.next_scan_number
projection_start = time.perf_counter()
try:
for _ in range(num_repeats):
self._at_each_angle(angle)
except AlarmBase as exc:
if exc.alarm_type == "TimeoutError":
# Clear the stuck queue, then re-raise so @scan_repeat
# re-acquires this projection. Swallowing the alarm here
# (as this used to) silently dropped the projection: its
# failed scan numbers were logged as if it had succeeded
# and it never reached the reconstruction queue.
bec.queue.request_queue_reset()
time.sleep(2)
raise
projection_duration = time.perf_counter() - projection_start
successful = True
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):
self._write_tomo_scan_number(scan_nr, angle, subtomo_number)
# Only reached when the projection completed without @scan_repeat
# re-raising, so this is the final successful attempt's duration.
self._log_projection_timing(
angle=angle,
subtomo_number=subtomo_number,
duration_s=projection_duration,
start_scan_number=start_scan_number,
end_scan_number=end_scan_number,
)
def _golden(self, ii, howmany_sorted, maxangle=360, reverse=False):
"""Return the ii-th golden ratio angle within sorted bunches and its subtomo number."""
@@ -1707,7 +1926,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
@@ -1828,14 +2053,7 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
"BEC",
)
self.write_pdf_report()
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
self._reset_progress_for_new_scan()
# Unconditional single 0-degree reference projection at the very
# start of a new tomogram, independent of
# zero_deg_reference_at_each_subtomo /
@@ -1989,6 +2207,11 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
for line in timing_lines[3:]:
print(line)
timing_content = "\n".join(timing_lines)
hook_source = self._active_hook_source()
if hook_source:
timing_content += (
f"\n\nAt-each-angle hook source ('{self.at_each_angle_hook}'):\n{hook_source}"
)
self.write_to_scilog(timing_content, ["tomoscan"])
def tomo_scan_resume(self) -> None:
@@ -2085,8 +2308,9 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
return LamniFermatScan.MIN_POSITIONS
def tomo_parameters(self):
"""Print and interactively update the tomo parameters."""
def tomo_parameters_cli(self):
"""Print and interactively update the tomo parameters on the command line.
tomo_parameters() opens the GUI version."""
print("Current settings:")
print(f"Counting time per frame <ctime> = {self.tomo_countingtime} s")
print(f"Stepsize microns <step> = {self.tomo_shellstep}")
@@ -2165,8 +2389,14 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
self.lamni_piezo_range_y = self._get_val(
"<piezo range Y (max 80)> um", self.lamni_piezo_range_y, float, min_=0.1, max_=79.9
)
self.lamni_stitch_x = self._get_val("<stitch X>", self.lamni_stitch_x, int)
self.lamni_stitch_y = self._get_val("<stitch Y>", 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(
"<stitch X>", self.lamni_stitch_x, int, min_=0, max_=50
)
self.lamni_stitch_y = self._get_val(
"<stitch Y>", self.lamni_stitch_y, int, min_=0, max_=50
)
self.tomo_stitch_overlap = self._get_val(
"<stitch overlap> um", self.tomo_stitch_overlap, float, min_=0.0, max_=10.0
)
@@ -2177,7 +2407,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:")
@@ -2188,7 +2418,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,
@@ -2214,20 +2448,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?",
@@ -2237,13 +2470,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?",
@@ -2311,7 +2550,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
@@ -2353,7 +2637,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"
)
@@ -2362,19 +2652,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",
@@ -2391,6 +2680,7 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
user_target = os.path.expanduser(
f"~/data/raw/documentation/tomo_scan_ID_{self.tomo_id}.pdf"
)
os.makedirs(os.path.dirname(user_target), exist_ok=True)
with PDFWriter(user_target) as file:
self._add_psi_footer(file)
# PDFWriter (bec_lib) has no public image API -- reach into its
@@ -2408,22 +2698,25 @@ class LamNI(TomoQueueMixin, LamNIAlignmentMixin, LamNIOpticsMixin, LamniGuiTools
# Replaces the old upload_last_pon.sh script (broken, never rewritten --
# see git history) with a direct HTTP upload to the samples web folder.
self._upload_pdf_report_to_samples(user_target)
# Same tolerance as write_to_scilog(): a session without scilog/logbook
# Same tolerance as write_to_scilog(): a session without scilog
# configured (e.g. a dev/sim session) must not crash report generation
# over the logbook upload -- the PDF itself is already written above.
# over the scilog upload -- the PDF itself is already written above.
scilog = self._get_scilog()
if scilog is None:
return
try:
scilog_text = content
if hook_source:
scilog_text += (
f"\n\nAt-each-angle hook source ('{self.at_each_angle_hook}'):\n{hook_source}"
)
msg = bec.logbook.LogbookMessage()
msg.add_file(logo_path).add_text(scilog_text.replace("\n", "</p><p>")).add_tag(
scilog.new().add_attachment(logo_path, width=200).add_text(
scilog_text.replace("\n", "<br>")
).add_tags(
["BEC", "tomo_parameters", f"dataset_id_{dataset_id}", "LamNI", self.sample_name]
)
self.client.logbook.send_logbook_message(msg)
except Exception:
logger.warning("Failed to write PDF report to scilog.")
).send()
except Exception as exc: # pylint: disable=broad-except
logger.warning(f"Failed to write PDF report to scilog: {exc}")
def get_calibration_of_capstops_left_and_right(self):
import time
@@ -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 = []
@@ -214,23 +214,122 @@ class LamNIOpticsMixin:
return None
return param.get(var)
@property
def _sl1xs_before_eye(self):
return self.client.get_global_var("lamni_sl1xs_before_eye")
@_sl1xs_before_eye.setter
def _sl1xs_before_eye(self, val):
self.client.set_global_var("lamni_sl1xs_before_eye", val)
@property
def _sl1ys_before_eye(self):
return self.client.get_global_var("lamni_sl1ys_before_eye")
@_sl1ys_before_eye.setter
def _sl1ys_before_eye(self, val):
self.client.set_global_var("lamni_sl1ys_before_eye", val)
def leye_out(self):
self.loptics_in()
dev.fsh.fshopen()
# Close (not open) the shutter as the eye leaves the beam, like
# flomni's feye_out(). Was fshclose() originally; commit 4967474
# ("removed fsh commands") turned it into fshopen() by mistake.
dev.fsh.fshclose()
if self.csaxs is not None:
self.csaxs.fil_trans(1, print_only=False)
sl1xs_before = self._sl1xs_before_eye
sl1ys_before = self._sl1ys_before_eye
if sl1xs_before is not None and sl1ys_before is not None:
# if the slit was manually re-adjusted while the eye was in,
# offer to keep the current size instead of restoring the
# pre-eye value
current_sl1xs = dev.sl1xs.user_readback.get()
current_sl1ys = dev.sl1ys.user_readback.get()
expected_sl1xs = sl1xs_before + 0.3
expected_sl1ys = sl1ys_before + 0.3
tol = 0.01 # mm (10 microns)
manually_adjusted = not np.isclose(
current_sl1xs, expected_sl1xs, atol=tol
) or not np.isclose(current_sl1ys, expected_sl1ys, atol=tol)
if manually_adjusted and self.OMNYTools.yesno(
"sl1xs/sl1ys appear to have been adjusted manually while the "
"eye was in. Keep the current slit size instead of "
"restoring the pre-eye value?"
):
print("Keeping current slit size, not restoring pre-eye value.")
else:
umv(dev.sl1xs, sl1xs_before, dev.sl1ys, sl1ys_before)
self._sl1xs_before_eye = None
self._sl1ys_before_eye = None
leyey_out = self._get_user_param_safe("leyey", "out")
umv(dev.leyey, leyey_out)
umv(dev.lsamrot, 0)
#umv(dev.dttrz, 5854, dev.fttrz, 2395)
# DRAFT, disabled until the fttrz positions are known: move the flight tube
# in after the eye is out, like flomni's feye_out(). Needs the userParameters
# fttrz_in / fttrz_out on leyex (see ptycho_lamni.yaml).
# if "fttrz" not in dev:
# if not self.OMNYTools.yesno(
# "Device 'fttrz' does not exist in the current config, so it "
# "can't be moved into position. Continue anyway? WARNING: "
# "this could cause collisions when operating on the real "
# "beamline."
# ):
# print("Aborting leye_out(): fttrz not moved.")
# return
# else:
# fttrz_in = self._get_user_param_safe("leyex", "fttrz_in")
# umv(dev.fttrz, fttrz_in)
def leye_in(self):
bec.queue.next_dataset_number += 1
umv(dev.lsamrot, 0)
#umv(dev.dttrz, 6419.677, dev.fttrz, 2959.979)
# DRAFT, disabled until the fttrz positions are known: move the flight tube
# out before the eye goes in, like flomni's feye_in(). When enabled, this
# replaces the manual "Did the flight tube move out?" prompt below.
# if "fttrz" not in dev:
# if not self.OMNYTools.yesno(
# "Device 'fttrz' does not exist in the current config, so it "
# "can't be moved out of the way. Continue anyway? WARNING: "
# "this could cause collisions when operating on the real "
# "beamline."
# ):
# print("Aborting leye_in(): fttrz not moved.")
# return
# else:
# fttrz_out = self._get_user_param_safe("leyex", "fttrz_out")
# umv(dev.fttrz, fttrz_out)
if not self.OMNYTools.yesno("Did the flight tube move out?", "y"):
return
leyex_in = self._get_user_param_safe("leyex", "in")
leyey_in = self._get_user_param_safe("leyey", "in")
current_leyex = dev.leyex.readback.get()
current_leyey = dev.leyey.readback.get()
# check if both are close enough (within 0.01)
if np.isclose(current_leyex, leyex_in, atol=0.01) and np.isclose(current_leyey, leyey_in, atol=0.01):
# both already in position -> do nothing
pass
else:
if "sl1xs" in dev and "sl1ys" in dev:
self._sl1xs_before_eye = dev.sl1xs.user_readback.get()
self._sl1ys_before_eye = dev.sl1ys.user_readback.get()
scans.umv(dev.sl1xs, 0.3, dev.sl1ys, 0.3, relative=True)
if self.csaxs is not None:
self.csaxs.fil_trans(0.05, print_only=False)
umv(dev.leyex, leyex_in, dev.leyey, leyey_in)
self.xrayeye_update_frame()
@@ -423,11 +522,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"]
@@ -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()"
)
# ------------------------------------------------------------------
@@ -41,6 +41,7 @@ import inspect
import json
import os
import threading
import time
import uuid
from typing import Callable
@@ -210,6 +211,12 @@ class TomoQueueMixin:
_TOMO_QUEUE_ACTIONS: dict = {}
_TOMO_QUEUE_MOVE_DEVICES: dict = {}
# tomo_parameters() GUI window: subclasses set a unique window name (a valid
# Python identifier, e.g. "lamni_tomo_params") and their own error class.
_TOMO_PARAMS_WINDOW_NAME: str = ""
_TOMO_PARAMS_ERROR: type[Exception] = RuntimeError
_TOMO_PARAMS_GUI_TIMEOUT = 20
# Name of a registered at_each_angle hook (see register_at_each_angle_hook()),
# or None for the default per-projection behaviour.
at_each_angle_hook = _GlobalVarParam(None)
@@ -237,6 +244,42 @@ class TomoQueueMixin:
# since-ended session must not linger and look registered.
self._publish_at_each_angle_hooks()
# ── tomo parameter GUI window ───────────────────────────────────────────
def tomo_parameters(self) -> None:
"""Open the tomo parameter GUI (OMNY_TomoParams) in its own window.
Calling it again while the window is open does nothing. The widget
works on the same global variables as the command line version,
``tomo_parameters_cli()``, which is not used as an automatic fallback:
if the GUI cannot be opened this raises, so the problem gets fixed.
"""
name = self._TOMO_PARAMS_WINDOW_NAME
error = self._TOMO_PARAMS_ERROR
gui = getattr(self.client, "gui", None)
if gui is None:
raise error(
"No BEC GUI is available in this session, so the tomo parameter window cannot be"
" opened. Use tomo_parameters_cli() for the command line version."
)
timeout = self._TOMO_PARAMS_GUI_TIMEOUT
try:
if name in gui.windows:
window = getattr(gui, name)
if hasattr(window, "tomo_params"):
print("The tomo parameter window is already open.")
return
else:
gui.new(name, timeout=timeout)
time.sleep(1)
window = getattr(gui, name)
window.new("OMNY_TomoParams", object_name="tomo_params", timeout=timeout)
except Exception as exc: # pylint: disable=broad-except
raise error(
f"Could not open the tomo parameter window ({type(exc).__name__}: {exc}). Use"
" tomo_parameters_cli() for the command line version."
) from exc
# ── at_each_angle hook registry ─────────────────────────────────────────
def register_at_each_angle_hook(self, name: str, func) -> None:
@@ -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);
}}
}}
@@ -1784,6 +1784,9 @@ class Flomni(
cSAXSBeamlineChecks,
flomniGuiTools,
):
_TOMO_PARAMS_WINDOW_NAME = "flomni_tomo_params"
_TOMO_PARAMS_ERROR = FlomniError
# Backed by a BEC global var (Redis) so the pre-open slit position
# survives a kernel restart while the eye is still in.
_sl1xs_before_eye = _GlobalVarParam(None)
@@ -2597,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):
@@ -3757,8 +3768,9 @@ class Flomni(
return FlomniFermatScan.MIN_POSITIONS
def tomo_parameters(self):
"""print and update the tomo parameters"""
def tomo_parameters_cli(self):
"""print and update the tomo parameters on the command line.
tomo_parameters() opens the GUI version."""
print("Current settings:")
print(f"Counting time per frame <ctime> = {self.tomo_countingtime} s")
print(f"Stepsize microns <step> = {self.tomo_shellstep}")
@@ -4289,6 +4301,7 @@ class Flomni(
user_target = os.path.expanduser(
f"~/data/raw/documentation/tomo_scan_ID_{self.tomo_id}.pdf"
)
os.makedirs(os.path.dirname(user_target), exist_ok=True)
with PDFWriter(user_target) as file:
self._add_psi_footer(file)
# PDFWriter (bec_lib) has no public image API -- reach into its
@@ -0,0 +1,46 @@
# TODO: OMNY still uses the old `~/Data10` layout, and its PDF report can fail on a missing folder
LamNI and flomni write their files under the new data structure (`~/data/raw/...`).
OMNY still points at the old `~/Data10/...` tree in several places and needs porting.
Not done yet, to avoid conflicting with other in-progress OMNY changes.
## 1. Port `~/Data10` paths to the new data structure
Where OMNY (`csaxs_bec/bec_ipython_client/plugins/omny/`) still uses `~/Data10`:
| File | Line | Path | LamNI/flomni equivalent |
|---|---|---|---|
| `omny.py` | 1217 | `tomo_reconstruct(base_path="~/Data10/specES1")` | `~/data/raw/logs/reconstruction_queue` |
| `omny.py` | 1236 | `~/Data10/specES1/dat-files/tomography_scannumbers.txt` | `~/data/raw/logs/tomography_scannumbers.txt` |
| `omny.py` | 1484 | `~/Data10/documentation/tomo_scan_ID_<id>.pdf` | `~/data/raw/documentation/tomo_scan_ID_<id>.pdf` |
| `omny.py` | 1489 | `xterm /work/sls/spec/local/XOMNY/bin/upload/upload_last_pon.sh &` | old spec upload script, check it still applies |
| `x_ray_eye_align.py` | 392 | `~/Data10/specES1/internal/xrayeye_alignmentvalues` | `~/data/raw/logs/xrayeye_alignmentvalues/` (timestamped HDF5) |
| `omny_alignment_mixin.py` | 62 | `~/Data10/specES1/internal/` | see the same alignment-values location |
The reconstruction queue folder in the new layout is created and written by the shared
`PtychoReconstructor.write()` (`OMNY_shared/omny_general_tools.py`), which OMNY does not use
yet. See also `TODO_tomo_reconstruct_stale_scan_list.md` (already ported to flomni and LamNI,
so switching OMNY to the shared reconstructor would cover both items).
## 2. Create the documentation folder if it does not exist
`OMNY.write_pdf_report()` (`omny.py:1439`) opens `PDFWriter(user_target)` without creating the
target folder first. `PDFWriter.close()` calls fpdf's `output(path)`
(`bec_lib/pdf_writer.py:90`), which does not create directories, so on an account without the
folder `tomo_scan()` fails with:
```
FileNotFoundError: [Errno 2] No such file or directory: '.../documentation/tomo_scan_ID_0.pdf'
```
Seen on LamNI; flomni had the identical pattern. Both are fixed by creating the folder before
the writer is opened:
```python
os.makedirs(os.path.dirname(user_target), exist_ok=True)
with PDFWriter(user_target) as file:
```
Apply the same line in `OMNY.write_pdf_report()`, and use the new-structure path from item 1.
Note the later steps in that method (the `upload_last_pon.sh` call and the logbook message)
only run if the PDF write succeeds.
@@ -51,7 +51,7 @@ if _args.session.lower() == "lamni":
from csaxs_bec.bec_ipython_client.plugins.LamNI import LamNI
_session_name = "LamNI"
lamni = LamNI(bec)
lamni = LamNI(bec, csaxs=csaxs)
logger.success("LamNI session loaded.")
print(r"""
██████╗ ███████╗ ██████╗ ██╗ █████╗ ███╗ ███╗███╗ ██╗██╗
@@ -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 <mm>). 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),
@@ -20,6 +20,10 @@ leyex:
connectionTimeout: 20
userParameter:
in: 14.117
# 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:
# fttrz_out:
deviceTags:
- ptycho_lamni
leyey:
@@ -141,7 +141,7 @@ lsamrot:
sign: 1
sim_stppermm: 50154.32099
sim_encpermm: 36000
sim_velocity: 5 # 48 #4x than hw
sim_velocity: 48 # 4x faster than hw
sim_initial_position: 0
tolerance: 1
enabled: true
+14 -1
View File
@@ -12,7 +12,8 @@ RT (LAMNI_server.cpp, port 3333): its own protocol dialect —
As (start cap sampling, no reply) / Ar -> 8-field cap sensor data,
Ss (start rt sampler, no reply) / Sr -> 7-field sampler sums,
V<um_per_s> velocity limit (no reply), a<rad> (no reply) / ar -> angle,
pa/pr, scan s/sc/sd/sr/sh, r<i> -> 21-field sample rows
pa/pr, scan s/sc/sd/sr/sh (sd only starts, and the scan only continues, while the
feedback loop is running), r<i> -> 21-field sample rows
(x/y target-avg-stdev, five cap-sensor avg/stdev pairs, angle avg/stdev), o/t.
"""
@@ -183,6 +184,9 @@ class SimRtLamniState:
num_pos = len(self.scan_positions)
if num_pos == 0 or self.scan_mode > 0:
return num_pos
if not self.feedback_running:
logger.warning("[sim rt lamni] scan not started: feedback loop is not running")
return num_pos
self.scan_mode = 3
self.scan_current = 0
self._scan_abort.clear()
@@ -198,6 +202,13 @@ class SimRtLamniState:
if self._scan_abort.wait(self.point_dwell_s):
return
with self.lock:
if not self.feedback_running:
logger.warning(
"[sim rt lamni] scan aborted at point"
f" {self.scan_current}: feedback loop stopped"
)
self.scan_mode = 0
return
self.scan_current = index + 1
self.targets[: len(self.scan_positions[index])] = self.scan_positions[index]
with self.lock:
@@ -305,6 +316,8 @@ class SimRtLamniSocket(SimSocketBase):
return f"{mode:.0f},{num_pos:.0f},{current:.0f}\n"
if args.startswith("d"):
num_pos = state.start_scan()
if num_pos > 0 and not state.feedback_running:
return "Scan not started: feedback loop not running.\n"
return f"Scan mode started with #positions in scan {num_pos:.0f}. Timing: Detector Trigger.\n"
if args.startswith("h"):
state.clear_scan()
+3 -8
View File
@@ -60,7 +60,7 @@ class LamniFermatScan(ScanBase):
"fov_circular",
"stitch_overlap",
],
"Acquisition Parameters": ["exp_time", "frames_per_trigger", "readout_time"],
"Acquisition Parameters": ["exp_time", "frames_per_trigger"],
}
MIN_POSITIONS = 20 # Minimum number of positions required for a valid scan
@@ -81,9 +81,8 @@ class LamniFermatScan(ScanBase):
fov_circular: Annotated[float, ScanArgument(display_name="Fov Circular", description="Generate a circular field of view in the sample plane. This is an additional cropping within the rectangular fovx/fovy range", units=Units.µm)] = 0,
stitch_overlap: Annotated[float, ScanArgument(display_name="Stitch Overlap", description="Overlap of the stitched regions", units=Units.µm)] = 1,
corridor_size: Annotated[float | None, ScanArgument(display_name="Corridor Size", description="Corridor size for the corridor optimization.", units=Units.µm)] = None,
exp_time: DefaultArgType.ExposureTime = 0,
exp_time: DefaultArgType.ExposureTime = 0.1,
frames_per_trigger: DefaultArgType.FramesPerTrigger = 1,
readout_time: DefaultArgType.ReadoutTime = 0,
**kwargs,
# fmt: on
):
@@ -106,7 +105,6 @@ class LamniFermatScan(ScanBase):
exp_time (float): Exposure time in seconds
corridor_size (float | None): Corridor size for the corridor optimization. If None, the corridor size will be estimated.
frames_per_trigger (int): Number of frames per trigger for devices that support configurable frame counts per trigger.
readout_time (float): Configuration for devices that support configurable readout times.
Returns:
ScanReport
@@ -128,12 +126,9 @@ class LamniFermatScan(ScanBase):
self.stitch_overlap = stitch_overlap
self.exp_time = exp_time
self.frames_per_trigger = frames_per_trigger
self.readout_time = readout_time
self.corridor_size = corridor_size
self.update_scan_info(
exp_time=exp_time, frames_per_trigger=frames_per_trigger, readout_time=readout_time
)
self.update_scan_info(exp_time=exp_time, frames_per_trigger=frames_per_trigger)
@scan_hook
def prepare_scan(self):
+3 -8
View File
@@ -57,7 +57,7 @@ class OmnyFermatScan(ScanBase):
"angle",
"corridor_size",
],
"Acquisition Parameters": ["exp_time", "frames_per_trigger", "readout_time"],
"Acquisition Parameters": ["exp_time", "frames_per_trigger"],
}
def __init__(
@@ -71,9 +71,8 @@ class OmnyFermatScan(ScanBase):
zshift: Annotated[float, ScanArgument(display_name="Zshift", description="Shift in z.", units=Units.µm, example=0)],
angle: Annotated[float | None, ScanArgument(display_name="Angle", description="Rotation angle (will rotate first)", units=Units.deg)] = None,
corridor_size: Annotated[float, ScanArgument(display_name="Corridor Size", description="Corridor size for the corridor optimization. ", units=Units.µm)] = 3,
exp_time: DefaultArgType.ExposureTime = 0,
exp_time: DefaultArgType.ExposureTime = 0.1,
frames_per_trigger: DefaultArgType.FramesPerTrigger = 1,
readout_time: DefaultArgType.ReadoutTime = 0,
**kwargs,
# fmt: on
):
@@ -91,7 +90,6 @@ class OmnyFermatScan(ScanBase):
corridor_size (float): Corridor size for the corridor optimization.
exp_time (float): Exposure time in seconds
frames_per_trigger (int): Number of frames per trigger for devices that support configurable frame counts per trigger.
readout_time (float): Configuration for devices that support configurable readout times.
Returns:
ScanReport
@@ -108,7 +106,6 @@ class OmnyFermatScan(ScanBase):
self.corridor_size = corridor_size
self.exp_time = exp_time
self.frames_per_trigger = frames_per_trigger
self.readout_time = readout_time
if self.zshift > 100:
logger.warning("The zshift is larger than 100 um. It will be limited to 100 um.")
@@ -117,9 +114,7 @@ class OmnyFermatScan(ScanBase):
logger.warning("The zshift is smaller than -100 um. It will be limited to -100 um.")
self.zshift = -100
self.update_scan_info(
exp_time=exp_time, frames_per_trigger=frames_per_trigger, readout_time=readout_time
)
self.update_scan_info(exp_time=exp_time, frames_per_trigger=frames_per_trigger)
self.omny_rotation_status = None
+7 -5
View File
@@ -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()` adjust the scan parameters for the tomographic scan. 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. 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.
@@ -404,11 +404,13 @@ The queue is persisted (it survives a BEC client restart). Each job's status is
If the process running the queue dies hard enough that it never gets to write a status update at all (a kernel restart, a Ctrl-C during a blocking prompt) a job can be left stuck showing `running` with nothing actually running. `flomni.tomo_queue_delete(index)` has no status guard and works on a stuck row regardless; you can also fix the status directly with `flomni._tomo_queue_proxy.update_by_id(job_id, status="incomplete")` (to make it resumable) or `status="done"` (to skip it). The GUI's Delete/Clear normally refuse to touch a `running` row, but detect a stale one (no recent scan heartbeat) and offer to proceed anyway with an explicit confirmation instead of blocking forever.
In the examples, `flomni.tomo_parameters_cli()` is used because it waits until the values are entered before the next command runs. With the GUI window, adjust the values there and add the job with its queue button, or with `flomni.tomo_queue_add()`.
Example:
```
flomni.tomo_parameters() # set up parameter set #1
flomni.tomo_parameters_cli() # set up parameter set #1
flomni.tomo_queue_add("fast overview")
flomni.tomo_parameters() # set up parameter set #2
flomni.tomo_parameters_cli() # set up parameter set #2
flomni.tomo_queue_add("hires scan")
flomni.tomo_queue_show()
flomni.tomo_queue_execute() # runs both, in order, on this sample
@@ -544,7 +546,7 @@ Example — queue a polarizer-modulated tomogram, then a normal one:
```python
flomni.register_at_each_angle_hook("polarizer_mod", polarizer_modulation)
flomni.tomo_parameters() # set up the scan parameters as usual
flomni.tomo_parameters_cli() # set up the scan parameters as usual
flomni.at_each_angle_hook = "polarizer_mod" # activate the hook
flomni.tomo_queue_add("polarizer run")
@@ -613,7 +615,7 @@ Load and use it:
import my_hooks
flomni.register_at_each_angle_hook("high_res_every_5th", my_hooks.high_res_every_5th)
flomni.tomo_parameters() # set up the scan parameters as usual
flomni.tomo_parameters_cli() # set up the scan parameters as usual
flomni.at_each_angle_hook = "high_res_every_5th" # activate the hook
flomni.tomo_queue_add("subtomo 2 high-res every 5th")
+37 -5
View File
@@ -67,6 +67,20 @@ To capture a single fresh frame without running the full alignment:
`lamni.xrayeye_update_frame()`
or with the shutter left open: `lamni.xrayeye_update_frame(keep_shutter_open=True)`
##### Checking the x-ray eye alignment with the eye still in
While the X-ray eye is still in, you can move the interferometer to the scan centre of any angle and look at where the sample ends up, without starting a scan:
`lamni.lamni_move_to_scan_center(shift_x, shift_y, angle)`
This is the same move that the alignment procedure and every scan use to place the sample. `shift_x` and `shift_y` are the base shift in mm, i.e. the centre found with the first click at 0° (`lamni.tomo_fovx_offset`, `lamni.tomo_fovy_offset`), `angle` is the rotation angle in degrees. The shift is rotated with the angle using the LamNI geometry. The command does not rotate the sample itself and it returns a scan report, so add `.wait()` to block until the move is done.
Example, checking the correction at 90°:
1. `lamni.feedback_disable()` and rotate: `umv(dev.lsamrot, 90)`
2. `lamni.lamni_move_to_scan_center(lamni.tomo_fovx_offset, lamni.tomo_fovy_offset, 90).wait()` (this re-enables the feedback)
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.
* 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*
@@ -75,6 +89,22 @@ or with the shutter left open: `lamni.xrayeye_update_frame(keep_shutter_open=Tru
The sample fine alignment can be obtained using ptychography. For this a short laminogram has to be recorded.
**How the shift at each projection angle is computed.** `lamni.tomo_scan_projection(angle)`, which is used by the alignment scan and by the laminography scan, combines several contributions. All are evaluated for the requested angle:
| Contribution | Source | Applied as |
|---|---|---|
| Base shift | first click (0°) of the x-ray eye alignment, stored as `lamni.tomo_fovx_offset` / `tomo_fovy_offset` | scan centre (`center_x`, `center_y`), rotated with the angle |
| X-ray eye correction | sinusoidal fit `A·sin(angle + B) + C` of the 8 clicked positions, `lamni.lamni_compute_additional_correction_xeye_mu(angle)` | added to the shift |
| Manual shift | `lamni.manual_shift_x` / `manual_shift_y`, constant for all angles | added to the shift |
| Additional correction 1 | lookup table from the fine alignment, `lamni.read_additional_correction(file)` | subtracted from the shift |
| Additional correction 2 | lookup table of the second iteration, `lamni.read_additional_correction_2(file)` | subtracted from the shift |
The shift is therefore `manual + x-ray eye − correction 1 − correction 2`, on top of the rotated base shift. The x-ray eye fit was measured after the base shift was applied, so the two do not count twice.
The correction tables are read from the files written by `BEC_ptycho_align`, with one shift per alignment angle. There is no interpolation: for each projection the entry with the closest angle is used. If a table has no data, it is skipped and `Not applying additional correction` is printed. Without an x-ray eye fit, `Not applying any X-ray eye correction` is printed. Each correction can be removed again with `lamni.reset_correction()`, `lamni.reset_correction_2()` and `lamni.reset_xray_eye_correction()`.
The alignment scan itself already uses the x-ray eye correction and any correction tables that are loaded. So the ptychographic alignment measures what is left over after those corrections, which is why a second iteration with `read_additional_correction_2()` is useful.
* `lamni.tomo_parameters()` adjust the ptychographic scan parameters for the alignment scan (FOV/step size/counting time) — `tomo_type` and number of projections are ignored, since the alignment scan always runs its own fixed 12 points spread evenly across the full 360 degrees, independent of the main tomogram's settings.
* `lamni.tomo_alignment_scan()` perform the alignment scan. Requires x-ray-eye alignment to have already been done — it will abort with a message otherwise. Scan numbers, angles and the existing x-ray-eye-fit offset at each angle are written to `~/data/raw/logs/ptychotomoalign_scannum.txt` and also printed at the end.
* Run `BEC_ptycho_align` (in Matlab, **force ptycho=1**, and **correct scan numbers**) using the printed/logged scan numbers.
@@ -89,7 +119,7 @@ The sample fine alignment can be obtained using ptychography. For this a short l
Start the laminography scan by
1. `lamni.tomo_parameters()` adjust the parameters to the desired settings.
1. `lamni.tomo_parameters()` opens the tomo parameter window (GUI) to adjust the parameters to the desired settings. The command line version, which prompts for each value in the terminal, is `lamni.tomo_parameters_cli()`.
2. for test scans run
`lamni.tomo_scan_projection(angle)`
`lamni.tomo_reconstruct()`
@@ -130,11 +160,13 @@ The queue is persisted (it survives a BEC client restart). Each job's status is
If the process running the queue dies hard enough that it never gets to write a status update at all (a kernel restart, a Ctrl-C during a blocking prompt) a job can be left stuck showing `running` with nothing actually running. `lamni.tomo_queue_delete(index)` has no status guard and works on a stuck row regardless; you can also fix the status directly with `lamni._tomo_queue_proxy.update_by_id(job_id, status="incomplete")` (to make it resumable) or `status="done"` (to skip it). The GUI's Delete/Clear normally refuse to touch a `running` row, but detect a stale one (no recent scan heartbeat) and offer to proceed anyway with an explicit confirmation instead of blocking forever.
In the examples, `lamni.tomo_parameters_cli()` is used because it waits until the values are entered before the next command runs. With the GUI window, adjust the values there and add the job with its queue button, or with `lamni.tomo_queue_add()`.
Example:
```
lamni.tomo_parameters() # set up parameter set #1
lamni.tomo_parameters_cli() # set up parameter set #1
lamni.tomo_queue_add("fast overview")
lamni.tomo_parameters() # set up parameter set #2
lamni.tomo_parameters_cli() # set up parameter set #2
lamni.tomo_queue_add("hires scan")
lamni.tomo_queue_show()
lamni.tomo_queue_execute() # runs both, in order, on this sample
@@ -257,7 +289,7 @@ Example — queue a polarizer-modulated laminogram, then a normal one:
```python
lamni.register_at_each_angle_hook("polarizer_mod", polarizer_modulation)
lamni.tomo_parameters() # set up the scan parameters as usual
lamni.tomo_parameters_cli() # set up the scan parameters as usual
lamni.at_each_angle_hook = "polarizer_mod" # activate the hook
lamni.tomo_queue_add("polarizer run")
@@ -326,7 +358,7 @@ Load and use it:
import my_hooks
lamni.register_at_each_angle_hook("high_res_every_5th", my_hooks.high_res_every_5th)
lamni.tomo_parameters() # set up the scan parameters as usual
lamni.tomo_parameters_cli() # set up the scan parameters as usual
lamni.at_each_angle_hook = "high_res_every_5th" # activate the hook
lamni.tomo_queue_add("subtomo 2 high-res every 5th")
@@ -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 == []
@@ -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, {"<stitch X>": ["-1", "2"], "<stitch Y>": ["-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
@@ -0,0 +1,577 @@
"""Regression tests for the LamNI go-live audit (bugs found by comparing
LamNI against the battle-tested flomni).
Each test pins one fixed bug:
* a TimeoutError alarm used to be swallowed in ``_tomo_scan_at_angle`` --
the projection was silently dropped (never retried, never reconstructed)
while its failed scan numbers were logged as if it had succeeded;
* ``leye_out()`` opened the fast shutter instead of closing it;
* the progress GUI divided by ``total_projections == 0`` (endless golden-ratio
scan) and any GUI failure propagated into the scan itself;
* ``write_to_scilog`` used the removed ``bec.logbook`` API and could raise.
Uses bare LamNI instances (bypassing __init__'s heavy side effects), same
pattern as test_lamni_tomo_angles.py.
"""
import builtins
import types
import pytest
from bec_lib.alarm_handler import AlarmBase
import csaxs_bec.bec_ipython_client.plugins.LamNI.lamni as lamni_module
import csaxs_bec.bec_ipython_client.plugins.LamNI.lamni_optics_mixin as optics_mixin_module
from csaxs_bec.bec_ipython_client.plugins.LamNI.gui_tools import LamniGuiTools
from csaxs_bec.bec_ipython_client.plugins.LamNI.lamni import LamNI, _ProgressProxy
from csaxs_bec.bec_ipython_client.plugins.LamNI.lamni_optics_mixin import LamNIOpticsMixin
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 _timeout_alarm() -> AlarmBase:
"""A TimeoutError alarm, without needing a full AlarmMessage."""
alarm = AlarmBase.__new__(AlarmBase)
alarm.alarm_type = "TimeoutError"
return alarm
class FakeQueue:
def __init__(self, start=100):
self.next_scan_number = start
self.resets = 0
def request_queue_reset(self):
self.resets += 1
@pytest.fixture
def fake_bec(monkeypatch):
queue = FakeQueue()
fake = types.SimpleNamespace(queue=queue)
monkeypatch.setitem(builtins.__dict__, "bec", fake)
# lamni.py also binds a module-level `bec` at import time when one exists
monkeypatch.setattr(lamni_module, "bec", fake, raising=False)
monkeypatch.setattr(lamni_module.time, "sleep", lambda s: None)
return fake
def make_lamni_for_angle_scan():
obj = object.__new__(LamNI)
obj.client = FakeClient()
obj._progress_proxy = _ProgressProxy(obj.client)
obj.special_angles = []
obj.special_angle_repeats = 3
obj._current_special_angles = []
obj._print_progress = lambda: None
obj._format_duration = lambda seconds: "0s"
obj.written_scans = []
obj._write_tomo_scan_number = lambda scan_nr, angle, subtomo: obj.written_scans.append(scan_nr)
obj.timing_logged = []
obj._log_projection_timing = lambda **kw: obj.timing_logged.append(kw)
return obj
# ── TimeoutError alarm must retry the projection, not drop it ───────────────
def test_timeout_alarm_retries_projection_and_logs_only_successful_attempt(fake_bec):
lamni = make_lamni_for_angle_scan()
attempts = []
def _at_each_angle(angle):
attempts.append(angle)
fake_bec.queue.next_scan_number += 1 # every attempt consumes a scan number
if len(attempts) == 1:
raise _timeout_alarm()
lamni._at_each_angle = _at_each_angle
lamni._tomo_scan_at_angle(10.0, 1)
assert attempts == [10.0, 10.0], "the projection must be re-acquired after a timeout"
assert fake_bec.queue.resets == 1
# scan 100 belonged to the failed attempt; only the successful one (101)
# may be recorded for reconstruction
assert lamni.written_scans == [101]
assert len(lamni.timing_logged) == 1
assert lamni.timing_logged[0]["start_scan_number"] == 101
def test_timeout_alarm_that_never_clears_is_not_swallowed(fake_bec):
"""A persistently timing-out projection must eventually fail loudly
(TooManyScanRestarts from @scan_repeat) instead of being marked done."""
from bec_lib.scan_repeat import TooManyScanRestarts
lamni = make_lamni_for_angle_scan()
def _always_timeout(angle):
raise _timeout_alarm()
lamni._at_each_angle = _always_timeout
with pytest.raises(TooManyScanRestarts):
lamni._tomo_scan_at_angle(10.0, 1)
assert lamni.written_scans == []
assert lamni.timing_logged == []
# ── special angles ──────────────────────────────────────────────────────────
def test_non_special_angle_with_special_list_does_not_crash(fake_bec):
"""num_repeats used to be unbound when special_angles was set but the
current angle wasn't near the next special angle."""
lamni = make_lamni_for_angle_scan()
lamni.special_angles = [90.0]
lamni._current_special_angles = [90.0]
calls = []
lamni._at_each_angle = lambda angle: calls.append(angle)
lamni._tomo_scan_at_angle(0.0, 1)
assert calls == [0.0]
assert lamni._current_special_angles == [90.0], "must not be consumed by another angle"
def test_special_angle_repeats_survive_a_retry(fake_bec):
"""The special-angle list is only consumed after the projection
succeeded, so a retried attempt still repeats the projection."""
lamni = make_lamni_for_angle_scan()
lamni.special_angles = [90.0]
lamni._current_special_angles = [90.0]
calls = []
def _at_each_angle(angle):
calls.append(angle)
if len(calls) == 1:
raise _timeout_alarm()
lamni._at_each_angle = _at_each_angle
lamni._tomo_scan_at_angle(90.0, 1)
# 1 failed call, then a full retry with special_angle_repeats == 3 repeats
assert len(calls) == 1 + 3
assert lamni._current_special_angles == []
# ── leye_out closes the shutter ─────────────────────────────────────────────
def test_leye_out_closes_the_fast_shutter(monkeypatch):
events = []
class FakeShutter:
def fshopen(self):
events.append("open")
def fshclose(self):
events.append("close")
class FakeDev:
fsh = FakeShutter()
leyey = object()
lsamrot = object()
monkeypatch.setattr(optics_mixin_module, "dev", FakeDev(), raising=False)
monkeypatch.setattr(optics_mixin_module, "umv", lambda *a: None, raising=False)
class FakeClient:
# no slit position stored before the eye went in -> nothing to restore
@staticmethod
def get_global_var(name):
return None
class Optics(LamNIOpticsMixin):
csaxs = None
client = FakeClient()
def loptics_in(self):
pass
@staticmethod
def _get_user_param_safe(device, var):
return 0.0
Optics().leye_out()
assert "close" in events
assert "open" not in events
# ── progress GUI must never abort the scan ──────────────────────────────────
class FakeRing:
def __init__(self):
self.value = None
def set_value(self, v):
self.value = v
class FakeProgressBar:
def __init__(self):
self.rings = [FakeRing(), FakeRing(), FakeRing()]
self.label = None
def set_center_label(self, text):
self.label = text
def make_gui_lamni(total_projections):
obj = object.__new__(LamNI)
obj.client = FakeClient()
obj._progress_proxy = _ProgressProxy(obj.client)
obj.progress.update(
{"projection": 5, "total_projections": total_projections, "subtomo_total_projections": 20}
)
obj.progressbar = FakeProgressBar()
obj._describe_active_hook = lambda: ""
return obj
def test_progress_gui_endless_scan_does_not_divide_by_zero():
"""golden_max_number_of_projections == 0 ("endless") gives
total_projections == 0: the GUI update must cope instead of raising into
the scan (which @scan_repeat would retry 10 times, then abort)."""
lamni = make_gui_lamni(total_projections=0)
lamni._lamnigui_update_progress()
assert lamni.progressbar.rings[0].value == 0.0
assert "endless" in lamni.progressbar.label
def test_progress_gui_normal_scan_still_reports_percentages():
lamni = make_gui_lamni(total_projections=20)
lamni._lamnigui_update_progress()
assert lamni.progressbar.rings[0].value == pytest.approx(25.0)
assert "Total projections expected: 20" in lamni.progressbar.label
def test_progress_gui_failure_never_propagates():
lamni = make_gui_lamni(total_projections=20)
class Broken:
@property
def rings(self):
raise RuntimeError("GUI closed / RPC timeout")
lamni.progressbar = Broken()
lamni._lamnigui_update_progress() # must not raise
def test_lamnigui_show_progress_failure_never_propagates():
lamni = make_gui_lamni(total_projections=20)
def _boom():
raise RuntimeError("no GUI")
lamni.lamnigui_show_gui = _boom
lamni.lamnigui_show_progress() # must not raise
def test_show_gui_does_not_raise_window():
"""raise_window() can hide the window (bec_widgets RPC bug) -- same
workaround as flomni's flomnigui_show_gui()."""
raised = []
gui = LamniGuiTools()
gui.gui = types.SimpleNamespace(
windows={"lamni": object()},
lamni=types.SimpleNamespace(raise_window=lambda: raised.append(1)),
)
gui.lamnigui_show_gui()
gui.lamnigui_raise()
assert raised == []
# ── scilog ──────────────────────────────────────────────────────────────────
class FakeSciLogMessage:
def __init__(self, sink):
self._sink = sink
self._sink["entry"] = {}
def add_text(self, text):
self._sink["entry"]["text"] = text
return self
def add_tags(self, tags):
self._sink["entry"]["tags"] = tags
return self
def send(self):
self._sink["sent"] = True
class FakeSciLog:
def __init__(self, enabled=True):
self._enabled = enabled
self.sink = {}
def new(self):
return FakeSciLogMessage(self.sink)
def make_scilog_lamni(monkeypatch, scilog):
obj = object.__new__(LamNI)
obj.client = FakeClient()
monkeypatch.setitem(
builtins.__dict__, "bec", types.SimpleNamespace(messaging=types.SimpleNamespace(scilog=scilog))
)
return obj
def test_write_to_scilog_uses_messaging_service(monkeypatch):
scilog = FakeSciLog()
lamni = make_scilog_lamni(monkeypatch, scilog)
tags = ["tomoscan"]
assert lamni.write_to_scilog("line1\nline2", tags) is True
assert scilog.sink["sent"]
assert scilog.sink["entry"]["text"] == "line1<br>line2"
assert scilog.sink["entry"]["tags"] == ["tomoscan", "BEC"]
assert tags == ["tomoscan"], "the caller's list must not be mutated"
def test_write_to_scilog_disabled_is_a_noop(monkeypatch):
lamni = make_scilog_lamni(monkeypatch, FakeSciLog(enabled=False))
assert lamni.write_to_scilog("x", ["t"]) is False
def test_write_to_scilog_never_raises(monkeypatch):
class Exploding(FakeSciLog):
def new(self):
raise RuntimeError("scilog unreachable")
lamni = make_scilog_lamni(monkeypatch, Exploding())
assert lamni.write_to_scilog("x", ["t"]) is False
def test_write_to_scilog_without_messaging_service(monkeypatch):
obj = object.__new__(LamNI)
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]
@@ -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
@@ -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"
@@ -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)
@@ -142,3 +142,64 @@ def test_write_alignment_scan_numbers_format(tmp_path, monkeypatch):
assert angles == list(np.linspace(0, 360, num=12, endpoint=False))
assert len(lines[2].split()) == 12
assert len(lines[3].split()) == 12
def _timeout_alarm():
from bec_lib.alarm_handler import AlarmBase
alarm = AlarmBase.__new__(AlarmBase)
alarm.alarm_type = "TimeoutError"
return alarm
def test_tomo_alignment_scan_retries_timed_out_angle_and_records_real_scan_numbers(
tmp_path, monkeypatch
):
"""A TimeoutError alarm used to be swallowed: the angle was silently lost
while the failed attempt's scan numbers were logged as if it succeeded, and
the pre-written scan-number file (first_scan + k per angle) went stale."""
lamni = make_lamni(monkeypatch, xray_eye_fit=[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]])
monkeypatch.setattr(
"os.path.expanduser",
lambda p: p.replace("~", str(tmp_path)) if p.startswith("~") else p,
)
monkeypatch.setattr(lamni_module.time, "sleep", lambda s: None)
calls = []
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
raise _timeout_alarm()
lamni.tomo_scan_projection = _projection
lamni.tomo_alignment_scan()
expected_angles = list(np.linspace(0, 360, num=12, endpoint=False))
assert len(calls) == 13, "the timed-out angle must be re-acquired"
assert calls[2] == calls[3] == expected_angles[2]
log_file = tmp_path / "data/raw/logs/ptychotomoalign_scannum.txt"
lines = log_file.read_text().splitlines()
scans = [int(s) for s in lines[0].split()]
# scans 100, 101 = angles 0, 1; 102 = the failed attempt at angle 2 (must be
# skipped); 103.. = the retry and the remaining angles
assert scans == [100, 101] + list(range(103, 113))
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)
@@ -129,6 +129,112 @@ def test_all_eight_subtomos_are_equally_spaced_when_combined(stepsize):
_assert_equally_spaced(combined, "full (all 8)")
# ── progress reset at the start of a new scan ───────────────────────────────
#
# A finished scan leaves projection == total (100 %), its last subtomo and
# angle in tomo_progress. The new scan's first _print_progress() (from the
# 0-degree reference shot) used to render those stale values in the progress
# GUI, because only the timing/ETA fields were reset.
def test_new_scan_resets_stale_progress_before_first_projection(monkeypatch):
lamni = make_lamni_for_tomo_scan(monkeypatch, 10.0, active_account="")
lamni.progress.update(
{
"tomo_type": "Golden ratio tomography",
"subtomo": 8,
"subtomo_projection": 36,
"subtomo_total_projections": 36,
"projection": 288,
"total_projections": 288,
"angle": 355.0,
"estimated_remaining_time": 0.0,
"estimated_finish_time": "2020-01-01T00:00:00",
"accumulated_idle_time": 99.0,
"heartbeat": "2020-01-01T00:00:00",
}
)
lamni.add_sample_database = lambda *a, **k: 0
seen = []
lamni._tomo_scan_at_angle = lambda angle, subtomo_number: seen.append(
(angle, subtomo_number, dict(lamni.progress.as_dict()))
)
lamni.tomo_scan()
angle, subtomo_number, prog = seen[0]
assert (angle, subtomo_number) == (0, 1)
assert prog["tomo_type"] == "Equally spaced sub-tomograms"
assert prog["subtomo"] == 1
assert prog["projection"] == 0
assert prog["subtomo_projection"] == 0
assert prog["angle"] == 0
assert prog["total_projections"] == 36 * 8
assert prog["subtomo_total_projections"] == 36
assert prog["estimated_remaining_time"] is None
assert prog["estimated_finish_time"] is None
assert prog["accumulated_idle_time"] == 0.0
assert prog["heartbeat"] is None
assert prog["tomo_start_time"] is not None
def test_resume_does_not_reset_progress(monkeypatch):
lamni = make_lamni_for_tomo_scan(monkeypatch, 10.0, active_account="")
resets = []
lamni._reset_progress_for_new_scan = lambda: resets.append(1)
lamni.tomo_scan(subtomo_start=2, start_angle=85.0)
assert resets == []
# ── resuming mid-sub-tomogram (start_angle) ─────────────────────────────────
#
# start_angle used to be treated as the ORIGIN of a fresh 360-degree sweep,
# so resuming e.g. subtomo 2 at 85 deg generated 435..85 (flipped): the >360
# artifacts were silently skipped, the resume started at 355 deg instead of
# 85, re-measured done angles and never reached the remaining ones.
def _resume(stepsize: float, subtomo_number: int, start_angle: float):
lamni = make_lamni(stepsize)
recorded = []
def _record(angle, subtomo):
recorded.append((float(angle), int(lamni.progress["subtomo_projection"])))
lamni._tomo_scan_at_angle = _record
lamni.sub_tomo_scan(subtomo_number, start_angle=start_angle)
return recorded
@pytest.mark.parametrize("stepsize", STEPSIZES)
@pytest.mark.parametrize("subtomo_number", range(1, 9))
def test_resume_yields_tail_of_normal_sweep(stepsize, subtomo_number):
full = make_lamni(stepsize)
full_recorded = []
full._tomo_scan_at_angle = lambda angle, subtomo: full_recorded.append(float(angle))
full.sub_tomo_scan(subtomo_number)
for k, start in enumerate(full_recorded):
resumed = _resume(stepsize, subtomo_number, start)
assert [a for a, _ in resumed] == pytest.approx(full_recorded[k:])
assert [i for _, i in resumed] == list(range(k, len(full_recorded)))
assert all(0 <= a < 360.05 for a, _ in resumed)
def test_resume_reported_case_subtomo2_at_85_deg():
resumed = _resume(10.0, 2, 85.0)
assert resumed[0] == (pytest.approx(85.0), 27)
assert resumed[-1][0] == pytest.approx(5.0)
assert len(resumed) == 36 - 27
def test_resume_off_grid_start_angle_raises():
with pytest.raises(ValueError):
_resume(10.0, 2, 80.0)
# ── tomo_scan()'s new-scan account handling ─────────────────────────────────
#
# bec.active_account used to be passed through .decode() (as if it were
@@ -171,7 +277,10 @@ def make_lamni_for_tomo_scan(
obj._print_progress = lambda: None
obj._format_duration = lambda seconds: "0s"
obj.lamnigui_show_progress = lambda: None
obj._lamnigui_update_progress = lambda: None
obj.at_each_angle_hook = None
# the real __init__ sets up the hook registry _active_hook_source() reads
obj._active_hook_source = lambda: None
obj.OMNYTools = types.SimpleNamespace(printgreenbold=lambda msg: None)
# These tests exercise tomo_scan()'s account-handling/heartbeat/progress-GUI
# logic, not the fine-alignment confirmation gate or the measurement-
@@ -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)
@@ -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():
@@ -0,0 +1,56 @@
"""tomo_reconstruct() must write the right scan list and queue folder, also when called
directly (without tomo_scan_projection() having just set _current_scan_list)."""
import builtins
import types
import pytest
from csaxs_bec.bec_ipython_client.plugins.LamNI.lamni import LamNI
class FakeReconstructor:
def __init__(self):
self.folder_name = "stale_folder"
self.calls = []
def write(self, **kwargs):
self.calls.append(dict(kwargs, folder_name=self.folder_name))
class FakeClient:
def get_global_var(self, key):
return {"ptycho_reconstruct_foldername": "current_folder"}.get(key)
@pytest.fixture
def lamni(monkeypatch):
obj = object.__new__(LamNI)
obj.client = FakeClient()
obj.reconstructor = FakeReconstructor()
fake_bec = types.SimpleNamespace(queue=types.SimpleNamespace(next_scan_number=101))
monkeypatch.setitem(builtins.__dict__, "bec", fake_bec)
return obj
def test_uses_cached_scan_list_when_current(lamni):
lamni._current_scan_list = [99, 100]
lamni.tomo_reconstruct()
assert lamni.reconstructor.calls[0]["scan_list"] == [99, 100]
assert lamni.reconstructor.calls[0]["next_scan_number"] == 101
def test_falls_back_when_scan_list_stale(lamni):
lamni._current_scan_list = [40, 41]
lamni.tomo_reconstruct()
assert lamni.reconstructor.calls[0]["scan_list"] == [100]
def test_falls_back_when_scan_list_missing(lamni):
lamni.tomo_reconstruct()
assert lamni.reconstructor.calls[0]["scan_list"] == [100]
def test_folder_name_resynced_from_global_var(lamni):
lamni.tomo_reconstruct()
assert lamni.reconstructor.calls[0]["folder_name"] == "current_folder"
@@ -0,0 +1,96 @@
"""tomo_parameters() opens the OMNY_TomoParams widget in its own window (shared TomoQueueMixin
implementation, used by LamNI and Flomni); tomo_parameters_cli() is the command line version."""
import pytest
import csaxs_bec.bec_ipython_client.plugins.OMNY_shared.tomo_queue_mixin as mixin_module
from csaxs_bec.bec_ipython_client.plugins.flomni.flomni import Flomni, FlomniError
from csaxs_bec.bec_ipython_client.plugins.LamNI.lamni import LamNI, LamNIError
class FakeWindow:
def __init__(self):
self.new_calls = []
def new(self, widget, **kwargs):
self.new_calls.append((widget, kwargs))
self.tomo_params = object()
class FakeGui:
def __init__(self, fail_new=False):
self.windows = {}
self.new_calls = []
self.fail_new = fail_new
def new(self, name, **kwargs):
if self.fail_new:
raise TimeoutError("GUI server did not answer")
self.new_calls.append((name, kwargs))
self.windows[name] = window = FakeWindow()
setattr(self, name, window)
return window
class FakeClient:
def __init__(self, gui):
if gui is not None:
self.gui = gui
@pytest.fixture(autouse=True)
def no_sleep(monkeypatch):
monkeypatch.setattr(mixin_module.time, "sleep", lambda *_: None)
def make(cls, gui):
obj = object.__new__(cls)
obj.client = FakeClient(gui)
return obj
@pytest.mark.parametrize(
"cls, window_name, error",
[(LamNI, "lamni_tomo_params", LamNIError), (Flomni, "flomni_tomo_params", FlomniError)],
)
class TestTomoParametersWindow:
def test_opens_widget_in_own_window(self, cls, window_name, error):
gui = FakeGui()
make(cls, gui).tomo_parameters()
assert [name for name, _ in gui.new_calls] == [window_name]
widget, kwargs = gui.windows[window_name].new_calls[0]
assert widget == "OMNY_TomoParams"
assert kwargs["object_name"] == "tomo_params"
def test_second_call_does_not_open_another(self, cls, window_name, error, capsys):
gui = FakeGui()
obj = make(cls, gui)
obj.tomo_parameters()
obj.tomo_parameters()
assert len(gui.new_calls) == 1
assert len(gui.windows[window_name].new_calls) == 1
assert "already open" in capsys.readouterr().out
def test_window_without_widget_gets_the_widget(self, cls, window_name, error):
gui = FakeGui()
gui.new(window_name) # window exists, but holds no tomo_params widget
make(cls, gui).tomo_parameters()
assert len(gui.new_calls) == 1 # window reused, not created again
assert gui.windows[window_name].new_calls[0][0] == "OMNY_TomoParams"
def test_raises_without_gui_and_names_cli(self, cls, window_name, error):
with pytest.raises(error, match="tomo_parameters_cli"):
make(cls, None).tomo_parameters()
def test_raises_when_gui_fails_and_does_not_fall_back(self, cls, window_name, error):
obj = make(cls, FakeGui(fail_new=True))
obj.tomo_parameters_cli = lambda: pytest.fail("must not fall back to the CLI")
with pytest.raises(error, match="tomo_parameters_cli") as exc_info:
obj.tomo_parameters()
assert isinstance(exc_info.value.__cause__, TimeoutError)
def test_cli_version_exists_and_gui_version_is_the_mixin_one():
for cls in (LamNI, Flomni):
assert "tomo_parameters_cli" in vars(cls)
assert "tomo_parameters" not in vars(cls)
@@ -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)
@@ -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
@@ -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
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)
@@ -0,0 +1,61 @@
"""The simulated LamNI RT server must not run scans while the feedback loop is off."""
import time
import pytest
from csaxs_bec.devices.sim.sim_lamni import SimRtLamniSocket, SimRtLamniState
N_POSITIONS = 10
@pytest.fixture
def state():
state = SimRtLamniState()
state.point_dwell_s = 0.02
for ii in range(N_POSITIONS):
state.add_scan_position([float(ii), float(ii)])
yield state
state.clear_scan()
def _wait_for_scan_end(state, timeout=5):
deadline = time.time() + timeout
while state.scan_mode != 0 and time.time() < deadline:
time.sleep(0.005)
assert state.scan_mode == 0
def test_scan_not_started_without_feedback(state):
state.feedback_running = False
assert state.start_scan() == N_POSITIONS
assert state.scan_mode == 0
assert state.scan_current == 0
assert state._scan_thread is None
def test_scan_runs_with_feedback(state):
state.feedback_running = True
state.start_scan()
_wait_for_scan_end(state)
assert state.scan_current == N_POSITIONS
def test_scan_aborts_when_feedback_drops(state):
state.feedback_running = True
state.start_scan()
time.sleep(3.5 * state.point_dwell_s)
state.feedback_running = False
_wait_for_scan_end(state)
assert 0 < state.scan_current < N_POSITIONS
def test_sd_command_reply_without_feedback(state):
socket = SimRtLamniSocket.__new__(SimRtLamniSocket)
socket.state = state
state.feedback_running = False
assert "not started" in socket.handle_command("sd")
assert state.scan_mode == 0
state.feedback_running = True
assert "Scan mode started" in socket.handle_command("sd")
_wait_for_scan_end(state)
@@ -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)