From 9e802c905be95591ba7238a1d2bb59dd8f995c32 Mon Sep 17 00:00:00 2001 From: Ivan Ivanov Date: Mon, 18 Mar 2024 13:06:36 -0700 Subject: [PATCH 1/6] fix bug finding focus in stack with only one slice --- waveorder/focus.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/waveorder/focus.py b/waveorder/focus.py index 3c259c19..6d70c4e2 100644 --- a/waveorder/focus.py +++ b/waveorder/focus.py @@ -3,6 +3,7 @@ from waveorder import util import matplotlib.pyplot as plt import numpy as np +import warnings def focus_from_transverse_band( @@ -63,6 +64,8 @@ def focus_from_transverse_band( minmaxfunc = _check_focus_inputs( zyx_array, NA_det, lambda_ill, pixel_size, midband_fractions, mode ) + if minmaxfunc is None: + return 0 # Calculate coordinates _, Y, X = zyx_array.shape @@ -109,10 +112,8 @@ def _check_focus_inputs( f"{N}D array supplied. `focus_from_transverse_band` only accepts 3D arrays." ) if zyx_array.shape[0] == 1: - print( - "WARNING: The dataset only contained a single slice. Returning trivial slice index = 0." - ) - return 0 + warnings.warn("The dataset only contained a single slice. Returning trivial slice index = 0.") + return if NA_det < 0: raise ValueError("NA must be > 0") From bb9eb49f186e3c2a20b4477e298d7bdbdb822ec6 Mon Sep 17 00:00:00 2001 From: Talon Chandler Date: Mon, 18 Mar 2024 14:37:23 -0700 Subject: [PATCH 2/6] refactor for clarify --- waveorder/focus.py | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/waveorder/focus.py b/waveorder/focus.py index 6d70c4e2..4f1c72d1 100644 --- a/waveorder/focus.py +++ b/waveorder/focus.py @@ -61,10 +61,17 @@ def focus_from_transverse_band( >>> slice = focus_from_transverse_band(zyx_array, NA_det=0.55, lambda_ill=0.532, pixel_size=6.5/20) >>> in_focus_data = data[slice,:,:] """ - minmaxfunc = _check_focus_inputs( - zyx_array, NA_det, lambda_ill, pixel_size, midband_fractions, mode + minmaxfunc = _mode_to_minmaxfunc(mode) + + _check_focus_inputs( + zyx_array, NA_det, lambda_ill, pixel_size, midband_fractions ) - if minmaxfunc is None: + + # Check for single slice + if zyx_array.shape[0] == 1: + warnings.warn( + "The dataset only contained a single slice. Returning trivial slice index = 0." + ) return 0 # Calculate coordinates @@ -103,17 +110,24 @@ def focus_from_transverse_band( return in_focus_index +def _mode_to_minmaxfunc(mode): + if mode == "min": + minmaxfunc = np.argmin + elif mode == "max": + minmaxfunc = np.argmax + else: + raise ValueError("mode must be either `min` or `max`") + return minmaxfunc + + def _check_focus_inputs( - zyx_array, NA_det, lambda_ill, pixel_size, midband_fractions, mode + zyx_array, NA_det, lambda_ill, pixel_size, midband_fractions ): N = len(zyx_array.shape) if N != 3: raise ValueError( f"{N}D array supplied. `focus_from_transverse_band` only accepts 3D arrays." ) - if zyx_array.shape[0] == 1: - warnings.warn("The dataset only contained a single slice. Returning trivial slice index = 0.") - return if NA_det < 0: raise ValueError("NA must be > 0") @@ -135,13 +149,6 @@ def _check_focus_inputs( raise ValueError("midband_fractions[0] must be between 0 and 1") if not (0 <= midband_fractions[1] <= 1): raise ValueError("midband_fractions[1] must be between 0 and 1") - if mode == "min": - minmaxfunc = np.argmin - elif mode == "max": - minmaxfunc = np.argmax - else: - raise ValueError("mode must be either `min` or `max`") - return minmaxfunc def _plot_focus_metric( From 18d39f13fcce99d3b50937ceddf465ba4b15c0d0 Mon Sep 17 00:00:00 2001 From: Talon Chandler Date: Mon, 18 Mar 2024 14:37:52 -0700 Subject: [PATCH 3/6] formatting --- waveorder/focus.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/waveorder/focus.py b/waveorder/focus.py index 4f1c72d1..dfbcbcc9 100644 --- a/waveorder/focus.py +++ b/waveorder/focus.py @@ -104,7 +104,12 @@ def focus_from_transverse_band( # Plot if plot_path is not None: _plot_focus_metric( - plot_path, midband_sum, peak_index, in_focus_index, peak_results, threshold_FWHM + plot_path, + midband_sum, + peak_index, + in_focus_index, + peak_results, + threshold_FWHM, ) return in_focus_index @@ -152,7 +157,12 @@ def _check_focus_inputs( def _plot_focus_metric( - plot_path, midband_sum, peak_index, in_focus_index, peak_results, threshold_FWHM + plot_path, + midband_sum, + peak_index, + in_focus_index, + peak_results, + threshold_FWHM, ): _, ax = plt.subplots(1, 1, figsize=(4, 4)) ax.plot(midband_sum, "-k") From 8e39e4117716f1676fe04f53a5298b3c83d3860c Mon Sep 17 00:00:00 2001 From: Talon Chandler Date: Mon, 18 Mar 2024 14:38:08 -0700 Subject: [PATCH 4/6] print -> warnings.warn --- waveorder/focus.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/waveorder/focus.py b/waveorder/focus.py index dfbcbcc9..709be235 100644 --- a/waveorder/focus.py +++ b/waveorder/focus.py @@ -141,7 +141,7 @@ def _check_focus_inputs( if pixel_size < 0: raise ValueError("pixel_size must be > 0") if not 0.4 < lambda_ill / pixel_size < 10: - print( + warnings.warn( f"WARNING: lambda_ill/pixel_size = {lambda_ill/pixel_size}." f"Did you use the same units?" f"Did you enter the pixel size in (demagnified) object-space units?" From 797654ffa84d2fb4b8b638d4ed2d14a0f1971af1 Mon Sep 17 00:00:00 2001 From: Talon Chandler Date: Mon, 18 Mar 2024 14:40:30 -0700 Subject: [PATCH 5/6] test single-slice case --- tests/test_focus_estimator.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/test_focus_estimator.py b/tests/test_focus_estimator.py index 560dc580..eb7cf2b1 100644 --- a/tests/test_focus_estimator.py +++ b/tests/test_focus_estimator.py @@ -45,6 +45,15 @@ def test_focus_estimator(tmp_path): assert slice <= data3D.shape[0] assert plot_path.exists() + # Check single slice + slice = focus.focus_from_transverse_band( + np.random.random((1, 10, 10)), + NA_det, + lambda_ill, + ps, + ) + assert slice == 0 + def test_focus_estimator_snr(tmp_path): ps = 6.5 / 100 From eb56c4d336812412857f91941aa2ed505b90fcdd Mon Sep 17 00:00:00 2001 From: Talon Chandler Date: Mon, 18 Mar 2024 14:40:56 -0700 Subject: [PATCH 6/6] fix test bugs --- tests/test_focus_estimator.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_focus_estimator.py b/tests/test_focus_estimator.py index eb7cf2b1..c3cca07b 100644 --- a/tests/test_focus_estimator.py +++ b/tests/test_focus_estimator.py @@ -39,7 +39,7 @@ def test_focus_estimator(tmp_path): plot_path = tmp_path.joinpath("test.pdf") data3D = np.random.random((11, 256, 256)) slice = focus.focus_from_transverse_band( - data3D, ps, lambda_ill, NA_det, plot_path=str(plot_path) + data3D, NA_det, lambda_ill, ps, plot_path=str(plot_path) ) assert slice >= 0 assert slice <= data3D.shape[0] @@ -75,9 +75,9 @@ def test_focus_estimator_snr(tmp_path): plot_path = tmp_path / f"test-{snr}.pdf" slice = focus.focus_from_transverse_band( data, - ps, - lambda_ill, NA_det, + lambda_ill, + ps, plot_path=plot_path, threshold_FWHM=5, )