diff --git a/mx_bec/actors/mx_automation.py b/mx_bec/actors/mx_automation.py index 490d3b2..de04cda 100644 --- a/mx_bec/actors/mx_automation.py +++ b/mx_bec/actors/mx_automation.py @@ -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 ( diff --git a/mx_bec/calc.py b/mx_bec/calc.py new file mode 100644 index 0000000..a223586 --- /dev/null +++ b/mx_bec/calc.py @@ -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))) diff --git a/mx_bec/decisions/__init__.py b/mx_bec/decisions/__init__.py index f989983..bcbbd36 100644 --- a/mx_bec/decisions/__init__.py +++ b/mx_bec/decisions/__init__.py @@ -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. diff --git a/mx_bec/decisions/grid_scans.py b/mx_bec/decisions/grid_scans.py index 0b2be1c..5fe2386 100644 --- a/mx_bec/decisions/grid_scans.py +++ b/mx_bec/decisions/grid_scans.py @@ -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) diff --git a/mx_bec/optical_centering.py b/mx_bec/optical_centering.py index 2cae007..a8748c2 100644 --- a/mx_bec/optical_centering.py +++ b/mx_bec/optical_centering.py @@ -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, + ) diff --git a/mx_bec/sample_info.py b/mx_bec/sample_info.py index ecd5912..7525550 100644 --- a/mx_bec/sample_info.py +++ b/mx_bec/sample_info.py @@ -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)