feat: add samcam conversion
CI for mx_bec / test (pull_request) Failing after 1m15s
CI for mx_bec / test (push) Failing after 1m15s

This commit is contained in:
perl_d committed 2026-11-24 13:18:30 +01:00
1 parent 3b8fe99403
commit 2c23ce6d87
8 files changed
+32 -15

No files matched your search

+2 -2
View File
@@ -94,9 +94,9 @@ jobs:
uv pip install --system -e ./bec/bec_lib[dev]
uv pip install --system -e ./bec/bec_ipython_client
uv pip install --system -e ./bec/bec_server[dev]
uv pip install --system -e ./bec_widgets[dev,pyside6]
uv pip install --system -e ./bec_widgets[dev]
# MX-internal dependencies (aarecommon, aare_config, jfjoch_client, ...) come from gitea
uv pip install --system --extra-index-url https://gitea.psi.ch/api/packages/mx/pypi/simple/ -e ./mx_bec
uv pip install --system --extra-index-url https://gitea.psi.ch/api/packages/mx/pypi/simple/ -e ./mx_bec[dev]
- name: Run Pytest with Coverage
id: coverage
-1
View File
@@ -11,7 +11,6 @@ from time import sleep
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 mx_bec.actors.core import MXAutomationActor
from mx_bec.decisions import (
+6
View File
@@ -0,0 +1,6 @@
import numpy as np
def samcam_pixels_to_microns(pixels: int, zoom: float, multiplier: float, exponent: float):
"""Convert pixels to microns for the sample camera"""
return float(pixels / (multiplier * np.exp(exponent * zoom)))
+1 -2
View File
@@ -3,8 +3,7 @@
grid scan -> analyse_grid -> plan_rotation -> collect_dataset -> assess_collection
-> decide_followup -> (re-raster | new position | chi sweep | finish)
Everything here is pure: pydantic models and functions, no BEC, Redis or device access (only
``gridscan_calculation`` uses a pxii calibration macro, imported when it is called). The
Everything here is pure: pydantic models and functions, no BEC, Redis or device access. The
actors in ``mx_bec.actors.mx_automation`` call these functions, and the same functions can be
replayed offline against archived Jungfraujoch results to tune thresholds.
+5 -6
View File
@@ -54,6 +54,7 @@ def gridscan_strategy_from_optical(
exp_time_s: float,
dtz_mm: float,
):
"""Work out the grid to scan from the loop detected by the optical centering."""
if optical_results is None or len(boxes := optical_results.prediction.boxes) == 0:
return mock_grid_calculation()
box_data = {box.label: ((box.x1, box.y1), (box.x2, box.y2)) for box in boxes}
@@ -77,6 +78,7 @@ def gridscan_strategy_from_optical(
file_prefix,
exp_time_s,
dtz_mm,
optical_results.microns_per_pixel,
)
@@ -89,17 +91,14 @@ def gridscan_calculation(
file_prefix: str,
exp_time_s: float,
dtz_mm: float,
microns_per_pixel: float,
):
"""calculate a gridscan from box coordinates and params. needs omega added later."""
# pyright: ignore[reportMissingImports]
from pxii_bec.macros.calculator import calc_scam_microns
# TODO: migrate utils from plugins to mx_bec
box_x_pixels = abs(x1_y1[0] - x2_y2[0])
box_y_pixels = abs(x1_y1[1] - x2_y2[1])
logger.info(f"{box_x_pixels=}, {box_y_pixels=}")
box_x_microns = calc_scam_microns(box_x_pixels, zoom=400)
box_y_microns = calc_scam_microns(box_y_pixels, zoom=400)
box_x_microns = box_x_pixels * microns_per_pixel
box_y_microns = box_y_pixels * microns_per_pixel
logger.info(f"{box_x_microns=}, {box_y_microns=}")
logger.info(f"adding margin to detection boxes of {margin}x")
n_x = int(box_x_microns / 1000 / size_x_mm * margin)
+13 -1
View File
@@ -6,6 +6,7 @@ from aarelcinfer_client import Client as AareLcClient
from aarelcinfer_client.models import LatestPredictionModel
from bec_lib.logger import bec_logger
from mx_bec.calc import samcam_pixels_to_microns
from mx_bec.sample_info import OpticalCenteringResults
if TYPE_CHECKING:
@@ -32,5 +33,16 @@ class OpticalPrediction:
logger.info("Updating latest prediction on sample info")
if self._client.sample_info:
self._client.sample_info.optical_centering_results = OpticalCenteringResults(
prediction=latest
prediction=latest, microns_per_pixel=self.microns_per_pixel()
)
def microns_per_pixel(self) -> float:
"""Size of a sample camera pixel on the sample, at the current zoom."""
camera = self._client.aare_config.camera
zoom = self._client.devices.scam_zoom.user_readback.get()
return samcam_pixels_to_microns(
1,
zoom,
multiplier=camera.translation_factor_b / 1000,
exponent=camera.translation_factor_a,
)
+4
View File
@@ -12,6 +12,10 @@ class OpticalCenteringResults(BaseModel):
"""Outputs from the optical centering stage"""
prediction: LatestPredictionModel
microns_per_pixel: float = Field(
description="Size of a pixel of the prediction's image on the sample, from the sample "
"camera calibration at the zoom the image was taken with"
)
timestamp: float = Field(default_factory=time.time)
+1 -3
View File
@@ -16,7 +16,7 @@ dependencies = [
"aarecommon",
"aare_config",
"aarelcinfer_client==0.1.1a8",
"jfjoch_client>=1.0.0rc166", # only pre-releases exist; the rc specifier lets pip pick them
"jfjoch_client>=1.0.0rc166", # only pre-releases exist; the rc specifier lets pip pick them
"bec_lib>=3.130.3",
"bec-server>=3.130.3",
"bec-ipython-client>=3.130.3",
@@ -38,7 +38,6 @@ dev = [
"ophyd_devices",
"bec_server",
"pytest",
"pytest-cov",
"pytest-mock",
"pytest-asyncio",
"pytest-timeout",
@@ -80,4 +79,3 @@ line-length = 100
[tool.ruff.format]
skip-magic-trailing-comma = true