From 161c17812ed1083b7bd0204a6acc74d50d5ee442 Mon Sep 17 00:00:00 2001 From: Richard Bowman Date: Thu, 23 Apr 2020 11:10:36 +0100 Subject: [PATCH 1/3] Removed vestigial global statement There was a global declaration for the modules np and logging, probably left over from running this code in the dodgy console plugin. I've removed it as it's ugly, dodgy, and no longer needed. --- openflexure_microscope/api/default_extensions/autofocus.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/openflexure_microscope/api/default_extensions/autofocus.py b/openflexure_microscope/api/default_extensions/autofocus.py index 0b3f752b..a3c2cf3a 100644 --- a/openflexure_microscope/api/default_extensions/autofocus.py +++ b/openflexure_microscope/api/default_extensions/autofocus.py @@ -372,7 +372,8 @@ class FastAutofocusAPI(View): autofocus_extension_v2 = BaseExtension( - "org.openflexure.autofocus", version="2.0.0" + "org.openflexure.autofocus", version="2.0.0", + description="Actions to move the microscope in Z and pick the point with the sharpest image." ) autofocus_extension_v2.add_method(fast_autofocus, "fast_autofocus") From 5a4aae8ed1db2f10fdc5b6a0b5214a183df7b92e Mon Sep 17 00:00:00 2001 From: Richard Bowman Date: Thu, 23 Apr 2020 11:15:10 +0100 Subject: [PATCH 2/3] Added helpful errors if the stream has stopped --- .../api/default_extensions/autofocus.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/openflexure_microscope/api/default_extensions/autofocus.py b/openflexure_microscope/api/default_extensions/autofocus.py index a3c2cf3a..d733b81f 100644 --- a/openflexure_microscope/api/default_extensions/autofocus.py +++ b/openflexure_microscope/api/default_extensions/autofocus.py @@ -92,15 +92,20 @@ class JPEGSharpnessMonitor: def move_data(self, istart, istop=None): "Extract sharpness as a function of (interpolated) z" - global np, logging if istop is None: istop = istart + 2 jpeg_times = np.array(self.jpeg_times) jpeg_sizes = np.array(self.jpeg_sizes) stage_times = np.array(self.stage_times)[istart:istop] stage_zs = np.array(self.stage_positions)[istart:istop, 2] - start = np.argmax(jpeg_times > stage_times[0]) - stop = np.argmax(jpeg_times > stage_times[1]) + try: + start = np.argmax(jpeg_times > stage_times[0]) + stop = np.argmax(jpeg_times > stage_times[1]) + except ValueError as e: + if np.sum(jpeg_times > stage_times[0]) == 0: + raise ValueError("No images were captured during the move of the stage. Perhaps the camera is not streaming images?") + else: + raise e if stop < 1: stop = len(jpeg_times) logging.debug("changing stop to {}".format(stop)) @@ -111,6 +116,8 @@ class JPEGSharpnessMonitor: def sharpest_z_on_move(self, index): """Return the z position of the sharpest image on a given move""" jt, jz, js = self.move_data(index) + if len(js) == 0: + raise ValueError("No images were captured during the move of the stage. Perhaps the camera is not streaming images?") return jz[np.argmax(js)] def data_dict(self): From be85d07210e65e714a08b89dc37994e6d1dd805e Mon Sep 17 00:00:00 2001 From: Richard Bowman Date: Thu, 23 Apr 2020 11:22:14 +0100 Subject: [PATCH 3/3] Attempt to start the stream if it's stopped. --- openflexure_microscope/api/default_extensions/autofocus.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/openflexure_microscope/api/default_extensions/autofocus.py b/openflexure_microscope/api/default_extensions/autofocus.py index d733b81f..54ea3dde 100644 --- a/openflexure_microscope/api/default_extensions/autofocus.py +++ b/openflexure_microscope/api/default_extensions/autofocus.py @@ -50,6 +50,9 @@ class JPEGSharpnessMonitor: def start(self): "Start monitoring sharpness by looking at JPEG size" + if not self.camera.stream_active: + logging.warn("Autofocus sharpness monitor was started but the camera isn't streaming. Attempting to start the stream...") + self.camera.start_stream_recording() self.background_thread = Thread(target=self._measure_jpegs) self.background_thread.start() return self