From ea3ad2547c1c2877b01b67d5fd8db6e83932f154 Mon Sep 17 00:00:00 2001 From: Ian OHara Date: Mon, 20 Jan 2025 14:17:42 -0800 Subject: [PATCH] debug(laser af): only catch Exception, print exec_info, indicate failure for caller in autofocus method --- software/control/core/core.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/software/control/core/core.py b/software/control/core/core.py index 72dc8fdb..b200bf90 100644 --- a/software/control/core/core.py +++ b/software/control/core/core.py @@ -1636,7 +1636,10 @@ def acquire_at_position(self, region_id, current_path, fov): multipoint_custom_script_entry(self, current_path, region_id, fov) return - self.perform_autofocus(region_id, fov) + if not self.perform_autofocus(region_id, fov): + self._log.error( + f"Autofocus failed in acquire_at_position. Continuing to acquire anyway using the current z position (z={self.stage.get_pos().z_mm} [mm])" + ) if self.NZ > 1: self.prepare_z_stack() @@ -1742,7 +1745,7 @@ def run_real_time_processing(self, current_round_images, z_level): print(repr(e)) def perform_autofocus(self, region_id, fov): - if self.do_reflection_af == False: + if not self.do_reflection_af: # contrast-based AF; perform AF only if when not taking z stack or doing z stack from center if ( ((self.NZ == 1) or self.z_stacking_config == "FROM CENTER") @@ -1765,7 +1768,7 @@ def perform_autofocus(self, region_id, fov): self.autofocusController.wait_till_autofocus_has_completed() else: # initialize laser autofocus if it has not been done - if self.microscope.laserAutofocusController.is_initialized == False: + if not self.microscope.laserAutofocusController.is_initialized: self._log.info("init reflection af") # initialize the reflection AF self.microscope.laserAutofocusController.initialize_auto() @@ -1792,13 +1795,16 @@ def perform_autofocus(self, region_id, fov): self.microscope.laserAutofocusController.move_to_target( 0 ) # for stepper in open loop mode, repeat the operation to counter backlash. It's harmless if any other case. - except: + except Exception as e: file_ID = f"{region_id}_focus_camera.bmp" saving_path = os.path.join(self.base_path, self.experiment_ID, str(self.time_point), file_ID) iio.imwrite(saving_path, self.microscope.laserAutofocusController.image) self._log.error( - "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! laser AF failed !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! laser AF failed !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", + exc_info=e, ) + return False + return True def prepare_z_stack(self): # move to bottom of the z stack