fix multi grid scan and take args
CI for mx_bec / test (push) Failing after 50s

This commit is contained in:
x10sa committed 2026-11-12 08:27:46 +01:00
1 parent 49b699434e
commit 5475503238
2 files changed
+132 -34

No files matched your search

+121 -20
View File
@@ -9,8 +9,10 @@ import secrets
from time import sleep
from typing import Any, ClassVar
from aarelcinfer_client.models import DetectionModel
from bec_lib.endpoints import EndpointInfo
from bec_lib.logger import bec_logger
from pxii_bec.macros.calculator import calc_scam_microns
from pxii_bec.macros.flux_macros import bpm2flux
from mx_bec.actors.core import MXAutomationActor
@@ -50,6 +52,28 @@ class OnSampleMountedRunOpticalCentering(MXAutomationActor):
)
def mock_grid_calculation():
sleep(1)
logger.info("calculating mock grid given no optical results ...")
sleep(1)
n_x = 10
n_y = 10
step_x_mm = 0.05
step_y_mm = 0.05
strategy = {
"n_x": n_x,
"n_y": n_y,
"step_x_mm": step_x_mm,
"step_y_mm": step_y_mm,
"exp_time_s": 0.05,
"omega_deg": 0.0,
"file_prefix": f"AareBecTestGridScan_{secrets.token_hex(6)}",
"dtz": 250,
"transmission": 0.2,
}
return strategy
class OnOpticalCenteringDoneRunXrayCentering(MXAutomationActor):
"""When optical centering finished, run X-ray centering. Runs regardless of success."""
@@ -66,33 +90,110 @@ class OnOpticalCenteringDoneRunXrayCentering(MXAutomationActor):
sample_id = client.sample_info.sample_id
logger.info(f"{self.name}: running X-ray centering for sample {sample_id}")
logger.info(
f"Calculating gridscan strategy given optical centering results of: {client.sample_info.optical_centering_results}"
f"Calculating gridscan strategy given optical centering results available: {client.sample_info.optical_centering_results is not None}"
)
sleep(2)
logger.info("calculating...")
sleep(2)
n_x = 10
n_y = 10
step_x_mm = 0.05
step_y_mm = 0.05
strategy = {
"n_x": n_x,
"n_y": n_y,
"step_x_mm": step_x_mm,
"step_y_mm": step_y_mm,
"exp_time_s": 0.05,
"omega_deg": 0.0,
"file_prefix": f"AareBecTestGridScan_{secrets.token_hex(6)}",
"dtz": 250,
"transmission": 0.2,
}
logger.info(f"calculated strategy: {strategy}")
if (optical_results := client.sample_info.optical_centering_results) is None or len(
boxes := optical_results.prediction.boxes
) == 0:
strategy = mock_grid_calculation()
else:
box_data = {box.label: ((box.x1, box.y1), (box.x2, box.y2)) for box in boxes}
logger.info(f"got detection boxes: {box_data}")
box_corners = None
for label_option in ["crystal", "loop_face", "loop_all"]:
if label_option in box_data:
box_corners = box_data[label_option]
if box_corners is None:
strategy = mock_grid_calculation()
else:
step_x_mm = 0.03
step_y_mm = 0.02
n_x = int(
calc_scam_microns(abs(box_corners[0][0] - box_corners[1][0])) / 1000 / step_x_mm
)
n_y = int(
calc_scam_microns(abs(box_corners[0][1] - box_corners[1][1])) / 1000 / step_y_mm
)
strategy = {
"n_x": n_x,
"n_y": n_y,
"step_x_mm": step_x_mm,
"step_y_mm": step_y_mm,
"exp_time_s": 0.05,
"omega_deg": 0.0,
"file_prefix": f"AareBecTestGridScan_{secrets.token_hex(6)}",
"dtz": 250,
"transmission": 0.2,
}
logger.info(f"calculated strategy: {strategy}")
client.add_task_to_queue(
MXTaskExecutionMessage(name="xray_centering", args=(sample_id, strategy), kwargs={})
)
class OnOpticalCenteringDoneRunMultiGrid(MXAutomationActor):
"""When optical centering finished, run X-ray centering. Runs regardless of success."""
config_key = "multigrid_after_optical"
def default_monitor_endpoints(self) -> set[EndpointInfo[type[MXMeasurementSequenceMessage]]]:
return {StateEndpoints.optical_centering_done()}
def condition(self, client: AareBec) -> bool:
return self.last_message is not None
def action(self, client: AareBec) -> None:
assert client.sample_info, "No sample loaded"
sample_id = client.sample_info.sample_id
logger.info(f"{self.name}: running X-ray centering for sample {sample_id}")
logger.info(
f"Calculating gridscan strategy given optical centering results available: {client.sample_info.optical_centering_results is not None}"
)
if (optical_results := client.sample_info.optical_centering_results) is None or len(
boxes := optical_results.prediction.boxes
) == 0:
strategy = mock_grid_calculation()
else:
box_data = {box.label: ((box.x1, box.y1), (box.x2, box.y2)) for box in boxes}
logger.info(f"got detection boxes: {box_data}")
box_corners = None
for label_option in ["crystal", "loop_face", "loop_all"]:
if label_option in box_data:
box_corners = box_data[label_option]
if box_corners is None:
strategy = mock_grid_calculation()
else:
step_x_mm = 0.03
step_y_mm = 0.02
n_x = int(
calc_scam_microns(abs(box_corners[0][0] - box_corners[1][0]), zoom=400)
/ 1000
/ step_x_mm
* 1.5
)
n_y = int(
calc_scam_microns(abs(box_corners[0][1] - box_corners[1][1]), zoom=400)
/ 1000
/ step_y_mm
* 1.5
)
strategy = {
"n_x": n_x,
"n_y": n_y,
"step_x_mm": step_x_mm,
"step_y_mm": step_y_mm,
"exp_time_s": 0.01,
"file_prefix": "AareBecTestGridScan",
"dtz": 200,
}
logger.info(f"calculated strategy: {strategy}")
client.add_task_to_queue(
MXTaskExecutionMessage(name="multiple_rasters", args=("0,60,120",), kwargs=strategy)
)
class SelfDrivingActor(MXAutomationActor):
"""Shared helpers. Abstract: subclasses set ``config_key`` and implement ``action``."""
+11 -14
View File
@@ -2,6 +2,7 @@ from __future__ import annotations
import functools
import inspect
from secrets import token_hex
from threading import Event, RLock, Thread
from typing import TYPE_CHECKING, Callable, Concatenate, ParamSpec, Self, TypeVar
@@ -146,6 +147,8 @@ class MxTasks:
if self._client.sample_info is None or self._client.sample_info.sample_id != sample_id:
self._client.reset_sample_info(sample_id)
self._client.aaredaq.auto_loop_center()
self._client.scans.mv(self._client.devices.scam_zoom, 400, relative=False).wait()
self._client.optical_prediction.update_latest()
return self._optical_centering_done_update(sample_id, success=True)
def _optical_centering_done_update(self, sample_id: int, *, success: bool):
@@ -191,22 +194,16 @@ class MxTasks:
) -> None:
"""Run multiple raster scans at different omega values"""
omegas = [float(ang.strip()) for ang in omegas_deg_list.split(",")]
smargon = self._client.devices.smargon
smargon_top_left = SmargonCoordinate(
sh_mm=Coordinate(x=smargon.x.get(), y=smargon.y.get(), z=smargon.z.get()),
chi_deg=smargon.chi.get(),
phi_deg=smargon.phi.get(),
)
for i, omega in enumerate(omegas):
self.aaredaq_raster_scan(
self._run_raster(
n_x,
n_y,
step_x_mm,
step_y_mm,
exp_time_s,
file_prefix=f"{file_prefix}_{i}_{omega:.2f}deg",
file_prefix=f"{file_prefix}_{i}_{omega:.2f}deg_{token_hex(3)}",
dtz=dtz,
smargon_top_left=smargon_top_left,
omega_deg=omega,
)
logger.info(f"Raster {i + 1} out of {len(omegas)} done.")
@@ -260,16 +257,16 @@ class MxTasks:
f"offsetting smargon x,y by half of grid: {x_offset_mm=} {y_offset_mm=} (grid x to smargon z)"
)
smargon = self._client.devices.smargon
aerotech = self._client.devices.aerotech
# aerotech = self._client.devices.aerotech
adjustment = smargon_nudge(
smargon.phi_deg.get(),
smargon.chi_deg.get(),
aerotech.u.get(),
smargon.phi.get(),
smargon.chi.get(),
omega_deg,
Coordinate(x=x_offset_mm, y=y_offset_mm, z=0),
)
smargon_top_left = SmargonCoordinate(
sh_mm=Coordinate(x=smargon.x.get(), y=smargon.y.get(), z=smargon.z.get())
+ adjustment,
- adjustment,
chi_deg=smargon.chi.get(),
phi_deg=smargon.phi.get(),
)