From 60f854b7dba368ae47ad9b87830ab32e6bcab1a7 Mon Sep 17 00:00:00 2001 From: Richard Bowman Date: Fri, 1 Dec 2023 21:29:14 +0000 Subject: [PATCH] Return None if calibration matrix is missing Previously, we raised an exception (and thus HTTP error code) if the CSM matrix was requested and not yet set. Now, we just return None, and I've added a (not-network-exposed) function that will raise an HTTPException if it's missing. --- .../things/camera_stage_mapping.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/openflexure_microscope_server/things/camera_stage_mapping.py b/src/openflexure_microscope_server/things/camera_stage_mapping.py index f48692a9..a29454b2 100644 --- a/src/openflexure_microscope_server/things/camera_stage_mapping.py +++ b/src/openflexure_microscope_server/things/camera_stage_mapping.py @@ -175,7 +175,7 @@ class CameraStageMapper(Thing): return data @thing_property - def image_to_stage_displacement_matrix(self) -> List[List[float]]: # 2x2 integer array + def image_to_stage_displacement_matrix(self) -> Optional[List[List[float]]]: # 2x2 integer array """A 2x2 matrix that converts displacement in image coordinates to stage coordinates. Note that this matrix is defined using "matrix coordinates", i.e. image coordinates @@ -195,8 +195,13 @@ class CameraStageMapper(Thing): """ displacement_matrix = self.thing_settings.get("image_to_stage_displacement") if not displacement_matrix: - raise CSMUncalibratedError() + return None return np.array(displacement_matrix).tolist() + + def assert_calibrated(self): + """Raise an exception if the image_to_stage_displacement matrix is not set""" + if self.image_to_stage_displacement_matrix is None: + raise CSMUncalibratedError() @thing_property def last_calibration(self) -> Optional[Dict]: @@ -222,6 +227,7 @@ class CameraStageMapper(Thing): and `y` to the shorter one. Checking what shape your chosen toolkit reports for an image usually helps resolve any ambiguity. """ + self.assert_calibrated() relative_move: np.ndarray = np.dot( np.array([y, x]), np.array(self.image_to_stage_displacement_matrix) @@ -232,12 +238,9 @@ class CameraStageMapper(Thing): def thing_state(self) -> dict: """Summary metadata describing the current state of the Thing""" state: dict[str, Any] = {} - try: - state["image_to_stage_displacement_matrix"] = ( - self.image_to_stage_displacement_matrix - ) - except ValueError: - pass # If it can't be retrieved, we'll just return an empty dict + state["image_to_stage_displacement_matrix"] = ( + self.image_to_stage_displacement_matrix + ) if self.last_calibration: try: state["image_resolution"] = self.last_calibration["image_resolution"]