From 79b396a66f4edef6fb159033fa0dcae131e13e75 Mon Sep 17 00:00:00 2001 From: Joe Knapper Date: Wed, 2 Apr 2025 17:54:12 +0100 Subject: [PATCH 1/7] CSM always returns to start on error --- .../things/camera_stage_mapping.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/openflexure_microscope_server/things/camera_stage_mapping.py b/src/openflexure_microscope_server/things/camera_stage_mapping.py index 5efdfb59..337b08fb 100644 --- a/src/openflexure_microscope_server/things/camera_stage_mapping.py +++ b/src/openflexure_microscope_server/things/camera_stage_mapping.py @@ -33,7 +33,6 @@ from camera_stage_mapping.camera_stage_calibration_1d import ( image_to_stage_displacement_from_1d, ) from labthings_fastapi.dependencies.invocation import ( - InvocationCancelledError, InvocationLogger, ) from labthings_fastapi.types.numpy import NDArray, denumpify, DenumpifyingDict @@ -206,7 +205,7 @@ class CameraStageMapper(Thing): result: dict = calibrate_backlash_1d( tracker, move, direction_array, logger=logger ) - except InvocationCancelledError as e: + except Exception as e: logger.info("Returning to starting position") stage.move_absolute(**starting_position, block_cancellation=True) raise e From 46d513d9e661fdb4a364566b81df239d4bc542fa Mon Sep 17 00:00:00 2001 From: jaknapper Date: Wed, 2 Apr 2025 18:25:35 +0100 Subject: [PATCH 2/7] Catch cancelled and other errors in single line --- .../things/camera_stage_mapping.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/openflexure_microscope_server/things/camera_stage_mapping.py b/src/openflexure_microscope_server/things/camera_stage_mapping.py index 337b08fb..0c3e79b2 100644 --- a/src/openflexure_microscope_server/things/camera_stage_mapping.py +++ b/src/openflexure_microscope_server/things/camera_stage_mapping.py @@ -33,6 +33,7 @@ from camera_stage_mapping.camera_stage_calibration_1d import ( image_to_stage_displacement_from_1d, ) from labthings_fastapi.dependencies.invocation import ( + InvocationCancelledError, InvocationLogger, ) from labthings_fastapi.types.numpy import NDArray, denumpify, DenumpifyingDict @@ -205,7 +206,7 @@ class CameraStageMapper(Thing): result: dict = calibrate_backlash_1d( tracker, move, direction_array, logger=logger ) - except Exception as e: + except (InvocationCancelledError, Exception) as e: logger.info("Returning to starting position") stage.move_absolute(**starting_position, block_cancellation=True) raise e From 67443ff939b3dc04ab78bec45cca4744088c4736 Mon Sep 17 00:00:00 2001 From: jaknapper Date: Thu, 3 Apr 2025 13:52:04 +0100 Subject: [PATCH 3/7] Capture ValueErrors, which covers most CSM errors, not all Exceptions --- .../things/camera_stage_mapping.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openflexure_microscope_server/things/camera_stage_mapping.py b/src/openflexure_microscope_server/things/camera_stage_mapping.py index 0c3e79b2..bfe3f9cf 100644 --- a/src/openflexure_microscope_server/things/camera_stage_mapping.py +++ b/src/openflexure_microscope_server/things/camera_stage_mapping.py @@ -206,7 +206,7 @@ class CameraStageMapper(Thing): result: dict = calibrate_backlash_1d( tracker, move, direction_array, logger=logger ) - except (InvocationCancelledError, Exception) as e: + except (InvocationCancelledError, ValueError) as e: logger.info("Returning to starting position") stage.move_absolute(**starting_position, block_cancellation=True) raise e From 9995e5b3bb2967c3975e4d7ce1e779dd855d63b9 Mon Sep 17 00:00:00 2001 From: Joe Knapper Date: Mon, 7 Apr 2025 14:18:07 +0100 Subject: [PATCH 4/7] Import custom Mapping error from camera_stage_mapping --- .../things/camera_stage_mapping.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/openflexure_microscope_server/things/camera_stage_mapping.py b/src/openflexure_microscope_server/things/camera_stage_mapping.py index bfe3f9cf..30fb7d10 100644 --- a/src/openflexure_microscope_server/things/camera_stage_mapping.py +++ b/src/openflexure_microscope_server/things/camera_stage_mapping.py @@ -32,6 +32,7 @@ from camera_stage_mapping.camera_stage_calibration_1d import ( calibrate_backlash_1d, image_to_stage_displacement_from_1d, ) +from camera_stage_mapping.exceoptions import MappingError from labthings_fastapi.dependencies.invocation import ( InvocationCancelledError, InvocationLogger, @@ -206,7 +207,7 @@ class CameraStageMapper(Thing): result: dict = calibrate_backlash_1d( tracker, move, direction_array, logger=logger ) - except (InvocationCancelledError, ValueError) as e: + except (InvocationCancelledError, MappingError) as e: logger.info("Returning to starting position") stage.move_absolute(**starting_position, block_cancellation=True) raise e From a506625e405c37bbed42eda074a5b22110bbd92d Mon Sep 17 00:00:00 2001 From: Joe Knapper Date: Mon, 7 Apr 2025 14:22:43 +0100 Subject: [PATCH 5/7] Treat cancellation and other errors differently --- .../things/camera_stage_mapping.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/openflexure_microscope_server/things/camera_stage_mapping.py b/src/openflexure_microscope_server/things/camera_stage_mapping.py index 30fb7d10..ab6d4058 100644 --- a/src/openflexure_microscope_server/things/camera_stage_mapping.py +++ b/src/openflexure_microscope_server/things/camera_stage_mapping.py @@ -207,9 +207,13 @@ class CameraStageMapper(Thing): result: dict = calibrate_backlash_1d( tracker, move, direction_array, logger=logger ) - except (InvocationCancelledError, MappingError) as e: + except InvocationCancelledError as e: + logger.info("User cancelled the camera stage mapping calibration") logger.info("Returning to starting position") stage.move_absolute(**starting_position, block_cancellation=True) + except MappingError as e: + logger.info("Returning to starting position due to failed calibration") + stage.move_absolute(**starting_position, block_cancellation=True) raise e result["move_history"] = move.history result["image_resolution"] = hw.grab_image().shape[:2] From 33c74741fe4ac6a0e55e56a1231231d09120bec3 Mon Sep 17 00:00:00 2001 From: jaknapper Date: Mon, 7 Apr 2025 15:46:20 +0100 Subject: [PATCH 6/7] Fixed import and re-raise cancelled error --- .../things/camera_stage_mapping.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/openflexure_microscope_server/things/camera_stage_mapping.py b/src/openflexure_microscope_server/things/camera_stage_mapping.py index ab6d4058..a860636f 100644 --- a/src/openflexure_microscope_server/things/camera_stage_mapping.py +++ b/src/openflexure_microscope_server/things/camera_stage_mapping.py @@ -32,7 +32,7 @@ from camera_stage_mapping.camera_stage_calibration_1d import ( calibrate_backlash_1d, image_to_stage_displacement_from_1d, ) -from camera_stage_mapping.exceoptions import MappingError +from camera_stage_mapping.exceptions import MappingError from labthings_fastapi.dependencies.invocation import ( InvocationCancelledError, InvocationLogger, @@ -211,6 +211,7 @@ class CameraStageMapper(Thing): logger.info("User cancelled the camera stage mapping calibration") logger.info("Returning to starting position") stage.move_absolute(**starting_position, block_cancellation=True) + raise e except MappingError as e: logger.info("Returning to starting position due to failed calibration") stage.move_absolute(**starting_position, block_cancellation=True) From bf82f37c47e727d81d98e31458601ce9699f3b03 Mon Sep 17 00:00:00 2001 From: Joe Knapper Date: Tue, 8 Apr 2025 12:09:15 +0000 Subject: [PATCH 7/7] Dependency bump for camera_stage_mapping to 0.1.8 Returns the errors we expect --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 1d8276c1..23eb7fd8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,7 +17,7 @@ classifiers = [ dependencies = [ "labthings-fastapi[server] == 0.0.7", "labthings-sangaboard", - "camera-stage-mapping ~= 0.1.7", + "camera-stage-mapping ~= 0.1.8", "opencv-python ~= 4.7.0", "pillow ~= 10.4", "anyio ~= 4.0",