From 38b8c5baa51593ae19597e5c881f60012a35449d Mon Sep 17 00:00:00 2001 From: Julian Stirling Date: Wed, 12 Nov 2025 19:11:14 +0000 Subject: [PATCH] Apply suggestions from code review of branch scanning-stability Co-authored-by: Joe Knapper --- .../background_detect.py | 12 ++++++------ .../things/autofocus.py | 5 +++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/openflexure_microscope_server/background_detect.py b/src/openflexure_microscope_server/background_detect.py index dfc7adbd..ece3fd6e 100644 --- a/src/openflexure_microscope_server/background_detect.py +++ b/src/openflexure_microscope_server/background_detect.py @@ -267,8 +267,8 @@ class ColourChannelDetectLUV(BackgroundDetectAlgorithm): class ChannelDeviationLUV(BackgroundDetectAlgorithm): """Compare the standard deviations of the LUV channels in a grid to background data. - This uses an LUV colour space, each image is divided into an 8x8 grid of images - each the standard deviation of each channel of each image is calculates and compared + Using an LUV colour space, each image is divided into an 8x8 grid of images. + The standard deviation of each channel of each image is calculated and compared to the median standard deviation for a grid of background images. """ @@ -295,11 +295,11 @@ class ChannelDeviationLUV(BackgroundDetectAlgorithm): u_cut = bg_stds[1] * self.settings.channel_tolerance v_cut = bg_stds[2] * self.settings.channel_tolerance - decisions = ( + populated_regions = ( (stds[:, :, 0] > l_cut) | (stds[:, :, 1] > u_cut) | (stds[:, :, 2] > v_cut) ) - return float(100 * np.sum(decisions) / 64) + return float(100 * np.sum(populated_regions) / 64) def image_is_sample(self, image: np.ndarray) -> tuple[bool, str]: """Label the current image as either background or sample. @@ -330,12 +330,12 @@ class ChannelDeviationLUV(BackgroundDetectAlgorithm): def _chunked_stds(img: np.ndarray, n_rows: int = 8, n_cols: int = 8) -> np.ndarray: - """Split image into a grid and calculated std of each channel in each chunk. + """Split image into a grid and calculate std of each channel in each chunk. :param img: The image to analyse :param n_rows: The number of rows in the grid :param n_cols: The number of cols in the grid - :return: A nummpy array of the grid of standard deviations. + :return: A numpy array of the grid of standard deviations. """ h, w = img.shape[:2] row_height = h // n_rows diff --git a/src/openflexure_microscope_server/things/autofocus.py b/src/openflexure_microscope_server/things/autofocus.py index 1e0a525b..2cd42f3e 100644 --- a/src/openflexure_microscope_server/things/autofocus.py +++ b/src/openflexure_microscope_server/things/autofocus.py @@ -812,7 +812,7 @@ class AutofocusThing(lt.Thing): return "restart", capture_id return "continue", capture_id - # Fint the peak + # Fit the peak x = range(n_imgs) coeffs, cov = np.polyfit(x, sharpnesses, deg=2, cov=True) a = float(coeffs[0]) @@ -833,7 +833,8 @@ class AutofocusThing(lt.Thing): # the stack ie for a stack of 7 images, best image must be between 3rd and 5th edge_size = 2 - # Check both the peak from fitting and the sharpest image are not the + # Check both the peak from fitting and the sharpest image are not at the + # edge of the stack. if turning < edge_size - 0.5 or sharpest_index < edge_size: return "restart", capture_id if turning > n_imgs - (edge_size - 0.5) or sharpest_index >= n_imgs - edge_size: