From 07baab9773f8305db94e6f4284cd5d7b49ecfae2 Mon Sep 17 00:00:00 2001 From: KasparP Date: Thu, 3 Dec 2026 05:13:43 -0800 Subject: [PATCH] Fix _shorth_center window span to match exact m-sample slices Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../extraction/raster/solver_baseline.py | 2 +- tests/raster/test_raster_variance_interp.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/giant_python/extraction/raster/solver_baseline.py b/src/giant_python/extraction/raster/solver_baseline.py index 962038bc..5051c8e7 100644 --- a/src/giant_python/extraction/raster/solver_baseline.py +++ b/src/giant_python/extraction/raster/solver_baseline.py @@ -183,7 +183,7 @@ def _shorth_center(vals: np.ndarray) -> float | None: return None s = np.sort(vals) m = max(n // 2, 1) - idx = int(np.argmin(s[m:] - s[:-m])) + idx = int(np.argmin(s[m - 1 :] - s[: n - m + 1])) return float(np.median(s[idx : idx + m])) diff --git a/tests/raster/test_raster_variance_interp.py b/tests/raster/test_raster_variance_interp.py index dc05ff01..b91c8b92 100644 --- a/tests/raster/test_raster_variance_interp.py +++ b/tests/raster/test_raster_variance_interp.py @@ -497,3 +497,22 @@ def test_apply_trial_sd_finv_and_discard_masked_read_varfac(tmp_path): np.testing.assert_allclose( trial_spike.finv[0] / trial_spike.finv[0, 0], [1.0, 2.0, 1.5] ) + + +def test_shorth_center_exact_m_window_and_final_window(): + """_shorth_center scores exact m-element windows including the final + window.""" + from giant_python.extraction.raster.solver_baseline import _shorth_center + + low = np.array( + [0.0 + 0.01 * k for k in range(8)] + + [100.0 + 0.001 * k for k in range(7)] + + [101.0] + ) + assert np.isclose(_shorth_center(low), 0.035) + high = np.array( + [0.0] + + [0.5 + 0.001 * k for k in range(7)] + + [200.0 + 0.0001 * k for k in range(8)] + ) + assert np.isclose(_shorth_center(high), 200.00035)