From 040373ef8862d878c574192fdf9c5bc50e5786bf Mon Sep 17 00:00:00 2001 From: Julian Stirling Date: Thu, 10 Apr 2025 11:45:57 +0100 Subject: [PATCH] Run check background aonly fter scan variables are stored --- src/openflexure_microscope_server/logging.py | 2 +- .../things/smart_scan.py | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/openflexure_microscope_server/logging.py b/src/openflexure_microscope_server/logging.py index 9082e092..d161197d 100644 --- a/src/openflexure_microscope_server/logging.py +++ b/src/openflexure_microscope_server/logging.py @@ -76,7 +76,7 @@ class OFMHandler(logging.Handler): def append_record(self, record): """ Use the built in formatter to format the record, then save - it to an array. Pop any in excess of the mamimum number of logs + it to an array. Pop any in excess of the maximum number of logs """ self._log.append(self.format(record)) while len(self._log) > self._max_logs: diff --git a/src/openflexure_microscope_server/things/smart_scan.py b/src/openflexure_microscope_server/things/smart_scan.py index b4150d95..2d3ac212 100644 --- a/src/openflexure_microscope_server/things/smart_scan.py +++ b/src/openflexure_microscope_server/things/smart_scan.py @@ -193,8 +193,7 @@ class SmartScanThing(Thing): background_detect Thing). """ - self._check_background_is_set(background_detect) - + started_scan = False locked = self._scan_lock.acquire(timeout=0.1) if not locked: raise RuntimeError("Trying to run scan while scan is already running!") @@ -211,14 +210,17 @@ class SmartScanThing(Thing): self._scan_images_taken = 0 try: + self._check_background_is_set() self._ongoing_scan_name = self._get_uniuqe_scan_name_and_dir(scan_name) overlap = self.overlap # record starting position so we can return there self._starting_position = self._stage.position + started_scan = True self._run_scan(scan_name, overlap) except Exception as e: - self._return_to_starting_position() - self._perform_final_stitch(overlap) + if started_scan: + self._return_to_starting_position() + self._perform_final_stitch(overlap) # Error must be raised so UI gives correct output raise e finally: @@ -235,14 +237,14 @@ class SmartScanThing(Thing): self._scan_images_taken = None self._scan_lock.release() - def _check_background_is_set(self, background_detect): + def _check_background_is_set(self): """Before starting a scand check that we've got a background set Raise error if it is not set but backroung detect is being used. """ if self.skip_background: - d = background_detect.background_distributions + d = self._background_detect.background_distributions if not d: raise RuntimeError( "Background is not set: you need to calibrate background detection."