DAQ: added ml_bounding_box to loop centering, creates grid scan grid using ml. Fixed bug where filename wouldn't iterate for copied grid scans.

This commit is contained in:
2025-07-04 10:59:42 +02:00
parent 804d18db4d
commit 731efccc2b
8 changed files with 124 additions and 18 deletions
+34 -10
View File
@@ -21,7 +21,7 @@ from aaredaqlib.models import (
SampleShortInfo,
PuckLoadedInfo,
SampleShortInfoList,
DAQStatusModel, BeamlineStatus, SessionStatus, SampleCameraSettings, AutofocusSettings, )
DAQStatusModel, BeamlineStatus, SessionStatus, SampleCameraSettings, AutofocusSettings, BoundingBoxModel, )
from aaredaqlib.raster_grid import RasterGridRequest, CompletedRasterGrid
from aaredaqlib.rotation_scan import RotationScanRequest, CompletedRotationScan
from aaredaqlib.sample_geometry import SampleGeometryModel
@@ -50,7 +50,8 @@ class AareDAQ:
self.__jfjoch = JFJochWrapper(bl)
self.__bl = bl.value.upper()
self.__aare = AareWrapper(bl)
self.__saved_box = None
@property
def state(self) -> BeamlineStateEnum:
return self.__cfg.state
@@ -80,6 +81,7 @@ class AareDAQ:
@omega.setter
def omega(self, val: float):
self.__cfg.set_busy(BeamlineStateEnum.SampleAlignment)
self.__saved_box = None
if -720 < val < 720:
self.__devs.aerotech.move(val, wait=True, speed=180.0, direct=True)
self.__cfg.state_busy = False
@@ -93,6 +95,7 @@ class AareDAQ:
@zoom.setter
def zoom(self, val: float):
self.__saved_box = None
self.__devs.zoom = val
@property
@@ -153,6 +156,7 @@ class AareDAQ:
new_meas_pos = self.__cfg.abr_meas_pos + c
self.__cfg.abr_meas_pos = new_meas_pos
self.__devs.abr_pos = new_meas_pos
self.__saved_box = None
self.__cfg.state_busy = False
except:
self.__cfg.state_busy = False
@@ -193,6 +197,7 @@ class AareDAQ:
def __mount(self, target: SampleShortInfo | None):
self.__set_state(BeamlineStateEnum.RobotSampleExchange)
self.__saved_box = None
self.__devs.smargon.move_home(wait=True)
self.__devs.abr_pos = ABR_POS_MOUNT
time.sleep(0.5)
@@ -433,6 +438,7 @@ class AareDAQ:
def smargon(self, sc: SmargonCoordinate):
self.__cfg.set_busy(BeamlineStateEnum.SampleAlignment)
try:
self.__saved_box = None
self.__devs.smargon.target = sc
self.__devs.smargon.wait()
self.__cfg.state_busy = False
@@ -495,7 +501,7 @@ class AareDAQ:
def listen_changes(self) -> redis.client.PubSub:
return self.__cfg.listen_changes()
def __ml_bounding_box(self, sample_id: int | None = None, filename: str | None = None, move: bool = False) -> None | Tuple[float, float, float, float]:
def __ml_bounding_box(self, sample_id: int | None = None, filename: str | None = None, move: bool = False) -> BoundingBoxModel | None:
time.sleep(0.2) # Just to be sure image is stable
#curr_image = self.camera_image
#box = self.__mlbox.predict(curr_image)
@@ -515,16 +521,33 @@ class AareDAQ:
coord = geom.picture_to_smargon(Coordinate(x=x1, y=y1))
self.__devs.smargon.target = SmargonCoordinate(sh_mm=coord)
self.__devs.smargon.wait(60)
return box
return BoundingBoxModel(bottom_x=x1, bottom_y=y1, top_x=x2, top_y=y2)
def ml_bounding_box(self, sample_id: int | None = None, filename: str | None = None, move: bool = False) -> None | Tuple[float, float, float, float]:
def ml_bounding_box(self, sample_id: int | None = None, filename: str | None = None, move: bool = False) -> RasterGridRequest | None:
try:
self.__cfg.try_set_busy(timeout=360)
box = self.__ml_bounding_box(sample_id, filename, move)
if not box:
raise LoopCenteringFailed
ml_box = self.__ml_bounding_box(sample_id, filename, move)
r = None
if ml_box is not None:
geom = self.sample_geometry
start_coord = geom.picture_to_smargon(Coordinate(x=ml_box.bottom_x, y=ml_box.bottom_y))
grid_size = Coordinate(x=geom.beam_size_mm.x * 0.8, y=geom.beam_size_mm.y * 0.8)
n_x = abs(ceil((ml_box.top_x - ml_box.bottom_x) * geom.pixel_in_mm / grid_size.x))
n_y = abs(ceil((ml_box.top_y - ml_box.bottom_y) * geom.pixel_in_mm / grid_size.y))
r = RasterGridRequest(
exp_time_s=0.05,
smargon= SmargonCoordinate(chi_deg = geom.smargon.chi_deg,
phi_deg= geom.smargon.phi_deg,
sh_mm=start_coord),
n_x=n_x,
n_y=n_y,
grid_size_mm= grid_size,
omega_deg=geom.omega_deg
)
self.__cfg.state_busy = False
return box
return r
except Exception:
self.__cfg.state_busy = False
raise
@@ -836,7 +859,8 @@ class AareDAQ:
bl=self.beamline_status,
sample=self.sample,
session=SessionStatus(),
diffraction=self.diffraction_geometry
diffraction=self.diffraction_geometry,
box=self.__saved_box
)
def cancel(self):
+13 -5
View File
@@ -7,7 +7,8 @@ import urllib3
import uvicorn
from aaredaqlib.coordinate import SmargonCoordinate, Coordinate
from aaredaqlib.models import SampleShortInfo, DAQStatusModel, BeamlineStateEnum, BeamlineSettingsModel, \
SampleShortInfoList, SessionStatus, SampleCameraSettings, AutofocusSettings, TokenData
SampleShortInfoList, SessionStatus, SampleCameraSettings, AutofocusSettings, TokenData, \
CryojetSettingsModel
from aaredaqlib.raster_grid import RasterGridRequest, CompletedRasterGrid
from aaredaqlib.rotation_scan import RotationScanRequest, CompletedRotationScan
from aaredaqlib.sample_geometry import SampleGeometryModel
@@ -330,11 +331,9 @@ async def alc_center_loop(token: str = Depends(oauth2_scheme)) -> str:
@app.post("/alc/ml_bounding_box")
async def alc_ml_bounding_box(token: str = Depends(oauth2_scheme), move: bool = False, filename: str = "") -> Tuple[
float, float, float, float] | None:
async def alc_ml_bounding_box(token: str = Depends(oauth2_scheme)) -> RasterGridRequest | None:
auth.check_jwt_rw(cfg, auth.parse_token(token))
return daq.ml_bounding_box(move=move)
return daq.ml_bounding_box()
# Access management
@app.get("/access/pgroup")
@@ -387,6 +386,15 @@ async def put_settings(s: BeamlineSettingsModel, token: str = Depends(oauth2_sch
auth.check_jwt_staff(auth.parse_token(token))
cfg.settings = s
@app.get("/beamline/cryo_settings")
async def get_cryo_settings(token: str = Depends(oauth2_scheme)) -> CryojetSettingsModel:
auth.check_jwt_staff(auth.parse_token(token))
return cfg.cryojet_settings
@app.put("/beamline/cryo_settings")
async def put_cryo_settings(s: CryojetSettingsModel, token: str = Depends(oauth2_scheme)):
auth.check_jwt_staff(auth.parse_token(token))
cfg.cryojet_settings = s
LOGGING_CONFIG = {
"version": 1,