feat: add tasks for aaredaq scans
CI for mx_bec / test (push) Failing after 49s

This commit is contained in:
perl_d committed 2026-11-03 15:52:43 +01:00
1 parent 75b51fc314
commit 02be72d885
3 files changed
+181 -7

No files matched your search

@@ -12,6 +12,7 @@ from qtpy.QtWidgets import (
QHBoxLayout,
QLineEdit,
QPushButton,
QScrollArea,
QSpinBox,
QVBoxLayout,
QWidget,
@@ -45,6 +46,22 @@ TASK_FORMS: dict[str, list[tuple[str, int | float | str]]] = {
("pgroup", "p16371"),
("sample_name", "unknown_sample"),
],
# the AareDAQ-driven tasks; their bool/optional arguments keep their defaults here
"aaredaq_mount_sample": [("dbid", 0)],
"aaredaq_raster_scan": [
("n_x", 4),
("n_y", 3),
("step_x_mm", 0.05),
("step_y_mm", 0.05),
("exp_time_s", 0.1),
("omega_deg", 0.0),
],
"aaredaq_rotation_scan": [
("exp_time_s", 0.05),
("incr_omega_deg", 0.1),
("steps", 100),
("start_omega_deg", 0.0),
],
}
@@ -69,19 +86,27 @@ class MxTaskPanel(BECWidget, QWidget):
) -> None:
super().__init__(parent=parent, client=client, config=config, gui_id=gui_id, **kwargs)
self._inputs: dict[str, dict[str, QSpinBox | QDoubleSpinBox | QLineEdit]] = {}
layout = QVBoxLayout(self)
# all the forms live in a scrollable content widget
content = QWidget()
layout = QVBoxLayout(content)
for task, fields in TASK_FORMS.items():
layout.addWidget(self._task_box(task, fields))
layout.addWidget(self._change_state_box())
layout.addWidget(self._task_box(content, task, fields))
layout.addWidget(self._change_state_box(content))
layout.addStretch()
scroll = QScrollArea(parent=self)
scroll.setWidgetResizable(True)
scroll.setWidget(content)
outer = QVBoxLayout(self)
outer.setContentsMargins(0, 0, 0, 0)
outer.addWidget(scroll)
def _change_state_box(self) -> QGroupBox:
def _change_state_box(self, parent: QWidget) -> QGroupBox:
"""One button per beamline state, each requesting the `change_state` task."""
from pxii_bec.macros.beamline_planner import ( # pyright: ignore[reportMissingImports]
BeamlineState,
)
box = QGroupBox("change_state", parent=self)
box = QGroupBox("change_state", parent=parent)
grid = QGridLayout(box)
for i, state in enumerate(BeamlineState):
button = QPushButton(state.value, parent=box)
@@ -91,8 +116,10 @@ class MxTaskPanel(BECWidget, QWidget):
grid.addWidget(button, i // self.STATE_BUTTONS_PER_ROW, i % self.STATE_BUTTONS_PER_ROW)
return box
def _task_box(self, task: str, fields: list[tuple[str, int | float | str]]) -> QGroupBox:
box = QGroupBox(task, parent=self)
def _task_box(
self, parent: QWidget, task: str, fields: list[tuple[str, int | float | str]]
) -> QGroupBox:
box = QGroupBox(task, parent=parent)
form = QFormLayout()
self._inputs[task] = {}
for name, default in fields:
+68
View File
@@ -5,6 +5,9 @@ import inspect
from threading import Event, RLock, Thread
from typing import TYPE_CHECKING, Callable, ParamSpec, TypeVar
from aarecommon.math.coordinate import Coordinate
from aarecommon.models.raster_grid import RasterGridRequest
from aarecommon.models.rotation_scan import RotationScanRequest
from bec_lib.config_values import RedisConfigValue
from bec_lib.logger import bec_logger
@@ -120,6 +123,71 @@ class MxTasks:
logger.info(f"Changing beamline state to {target.value}, modifier={modifier}")
self.planner.move_to(target, modifier)
@task
def aaredaq_mount_sample(self, dbid: int, reference: bool = False) -> None:
"""Have AareDAQ mount the sample with database id `dbid` (a reference tool if
`reference`)."""
self._client.aaredaq.mount_sample(dbid, reference=reference)
@task
def aaredaq_raster_scan(
self,
n_x: int,
n_y: int,
step_x_mm: float,
step_y_mm: float,
exp_time_s: float,
omega_deg: float = 0.0,
file_prefix: str | None = None,
dtz: float | None = None,
transmission: float | None = None,
auto_center: bool = False,
) -> None:
"""Have AareDAQ run an n_x by n_y raster scan from the current Smargon position
(`smargon_top_left` is left unset), optionally auto-centering afterwards."""
request = RasterGridRequest(
n_x=n_x,
n_y=n_y,
grid_size_mm=Coordinate(x=step_x_mm, y=step_y_mm),
exp_time_s=exp_time_s,
omega_deg=omega_deg,
file_prefix=file_prefix,
dtz=dtz,
transmission=transmission,
smargon_top_left=None,
)
result = self._client.aaredaq.raster_scan(request, auto_center=auto_center)
logger.info(f"AareDAQ raster scan done: {len(result.r)} grid(s) collected")
@task
def aaredaq_rotation_scan(
self,
exp_time_s: float,
incr_omega_deg: float,
steps: int,
start_omega_deg: float = 0.0,
file_prefix: str | None = None,
dtz: float | None = None,
transmission: float | None = None,
screening: bool = False,
wedge_omega_deg: float | None = None,
) -> None:
"""Have AareDAQ run a rotation (or screening) scan of `steps` images of `incr_omega_deg`
each, starting at the current Smargon position (`start`/`end` are left unset)."""
request = RotationScanRequest(
exp_time_s=exp_time_s,
incr_omega_deg=incr_omega_deg,
steps=steps,
start_omega_deg=start_omega_deg,
file_prefix=file_prefix,
dtz=dtz,
transmission=transmission,
screening=screening,
wedge_omega_deg=wedge_omega_deg,
)
result = self._client.aaredaq.rotation_scan(request)
logger.info(f"AareDAQ rotation scan done: {len(result.result.images)} images")
@task
def run_grid_scan(
self,
+79
View File
@@ -0,0 +1,79 @@
from unittest.mock import MagicMock
import pytest
from aarecommon.math.coordinate import Coordinate
from aarecommon.models.raster_grid import RasterGridRequest
from aarecommon.models.rotation_scan import RotationScanRequest
from mx_bec.tasks import MxTasks
@pytest.fixture
def client():
return MagicMock()
@pytest.fixture
def tasks(client):
return MxTasks(client)
@pytest.mark.parametrize(
"name", ["aaredaq_mount_sample", "aaredaq_raster_scan", "aaredaq_rotation_scan"]
)
def test_aaredaq_methods_are_tasks(name):
assert MxTasks.is_task(name)
def test_mount_sample(tasks, client):
tasks.aaredaq_mount_sample(42)
client.aaredaq.mount_sample.assert_called_once_with(42, reference=False)
tasks.aaredaq_mount_sample(7, reference=True)
client.aaredaq.mount_sample.assert_called_with(7, reference=True)
def test_raster_scan_builds_request_from_flat_arguments(tasks, client):
tasks.aaredaq_raster_scan(
n_x=4, n_y=3, step_x_mm=0.05, step_y_mm=0.04, exp_time_s=0.1, omega_deg=12.0, dtz=150.0
)
request, kwargs = (
client.aaredaq.raster_scan.call_args.args[0],
client.aaredaq.raster_scan.call_args.kwargs,
)
assert request == RasterGridRequest(
n_x=4,
n_y=3,
grid_size_mm=Coordinate(x=0.05, y=0.04),
exp_time_s=0.1,
omega_deg=12.0,
dtz=150.0,
smargon_top_left=None,
)
assert kwargs == {"auto_center": False}
def test_raster_scan_passes_auto_center(tasks, client):
tasks.aaredaq_raster_scan(1, 1, 0.05, 0.05, 0.1, auto_center=True)
assert client.aaredaq.raster_scan.call_args.kwargs == {"auto_center": True}
def test_rotation_scan_builds_request_from_flat_arguments(tasks, client):
tasks.aaredaq_rotation_scan(
exp_time_s=0.05, incr_omega_deg=0.1, steps=100, start_omega_deg=90.0, file_prefix="rot"
)
assert client.aaredaq.rotation_scan.call_args.args[0] == RotationScanRequest(
exp_time_s=0.05, incr_omega_deg=0.1, steps=100, start_omega_deg=90.0, file_prefix="rot"
)
def test_screening_scan(tasks, client):
tasks.aaredaq_rotation_scan(0.05, 1.0, 4, screening=True, wedge_omega_deg=90.0)
request = client.aaredaq.rotation_scan.call_args.args[0]
assert request.screening is True and request.wedge_omega_deg == 90.0
def test_failed_aaredaq_call_pauses_queue_and_reraises(tasks, client):
client.aaredaq.mount_sample.side_effect = RuntimeError("sample not found")
with pytest.raises(RuntimeError, match="sample not found"):
tasks.aaredaq_mount_sample(1)
client.task_queue.pause.assert_called_once()