From 291bbde9cf99ee7f42b4f18713d8e4da6e232e23 Mon Sep 17 00:00:00 2001 From: Richard Bowman Date: Tue, 28 Nov 2023 21:06:42 +0000 Subject: [PATCH] Don't raise an exception if thing_state called before calibration It's helpful to raise an exception if we access the calibration before it's been set - but thing state should not raise exceptions, just return an empty dict if the data is not there yet. --- .../things/camera_stage_mapping.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/openflexure_microscope_server/things/camera_stage_mapping.py b/src/openflexure_microscope_server/things/camera_stage_mapping.py index 0e303db3..229d68cb 100644 --- a/src/openflexure_microscope_server/things/camera_stage_mapping.py +++ b/src/openflexure_microscope_server/things/camera_stage_mapping.py @@ -187,7 +187,7 @@ class CameraStageMapper(Thing): return np.array(displacement_matrix).tolist() @thing_property - def last_calibration(self) -> Optional[Dict]: # 2x2 integer array + def last_calibration(self) -> Optional[Dict]: """The results of the last calibration that was run """ return self.thing_settings.get("last_calibration", None) @@ -219,11 +219,16 @@ class CameraStageMapper(Thing): @thing_property def thing_state(self) -> dict: """Summary metadata describing the current state of the Thing""" - return { - "image_to_stage_displacement_matrix": ( + state: dict[str, Any] = {} + try: + state["image_to_stage_displacement_matrix"] = ( self.image_to_stage_displacement_matrix - ), - "image_resolution": ( - self.thing_settings.get("image_to_stage_displacement", None) ) - } \ No newline at end of file + except ValueError: + pass # If it can't be retrieved, we'll just return an empty dict + if self.last_calibration: + try: + state["image_resolution"] = self.last_calibration["image_resolution"] + except KeyError: + logging.warning("Couldn't find `image_resolution` in CSM calibration") + return state