diff --git a/changelog.d/single-delay-axis.fixed.md b/changelog.d/single-delay-axis.fixed.md new file mode 100644 index 0000000..633a91b --- /dev/null +++ b/changelog.d/single-delay-axis.fixed.md @@ -0,0 +1 @@ +`load_and_clean` (and `filter_frog_trace`) now reject a delay axis with fewer than two distinct finite values with a `ValueError` naming what was given, instead of failing later with an unrelated `ZeroDivisionError` or a segfault (#32). diff --git a/croak/preprocess.py b/croak/preprocess.py index 14085a7..9771b9a 100644 --- a/croak/preprocess.py +++ b/croak/preprocess.py @@ -268,6 +268,18 @@ def marginal_peak_delay(delays: ArrayLike, marginal: ArrayLike) -> float: return float(delays[i] + min(max(vertex, x0), x2)) +def _validate_delay_axis(tau: ArrayLike) -> NDArray[np.float64]: + """Return ``tau`` as a float array with at least two finite, distinct delays.""" + values = np.asarray(tau, dtype=float) + finite = values[np.isfinite(values)] + if values.size < 2 or np.unique(finite).size < 2: + raise ValueError( + "the delay axis needs at least two distinct finite values " + f"(got {values.size} value(s), {np.unique(finite).size} distinct)" + ) + return values + + def _delay_filter( Ifrog: NDArray[np.float64], tau: NDArray[np.float64], @@ -518,7 +530,9 @@ def filter_frog_trace( # Bind to a fresh local (not the ``tau`` parameter): its inferred ndarray type # then survives the in-place recentre/subsample below, instead of being reset # to the parameter's wide ``ArrayLike`` declared type. - delays = np.asarray(tau, dtype=float).copy() + # A single (or repeated) delay cannot be a FROG trace: reject it here with a + # clear error instead of failing later in the filter or the grid. + delays = _validate_delay_axis(np.asarray(tau, dtype=float).copy()) Ifrogf = _delay_filter( Ifrog, @@ -1711,6 +1725,11 @@ def load_and_clean( else: raise ValueError("input_unit must be 'position' or 'delay'") + # A single (or repeated) delay cannot be a FROG trace: reject it at the + # boundary with a clear error instead of failing later in the filter or + # the grid (``filter_frog_trace`` validates again for direct callers). + tau_meas = _validate_delay_axis(tau_meas) + tau_filt, Ifrog_filt = filter_frog_trace( Ifrog, lam_frog, diff --git a/tests/test_preprocess.py b/tests/test_preprocess.py index 1309a2b..5f6adb9 100644 --- a/tests/test_preprocess.py +++ b/tests/test_preprocess.py @@ -442,6 +442,40 @@ def test_load_and_clean_produces_retrievable_trace(experiment): assert res.error < 7e-2 +@pytest.mark.parametrize( + "delays", + [ + np.array([0.0]), # a single delay cannot be a FROG trace + np.array([0.0, 0.0, 0.0]), # repeated delays carry no delay information + np.array([0.0, np.nan]), # only one finite delay + ], +) +def test_load_and_clean_rejects_degenerate_delay_axis(delays): + """A one-delay (or repeated-delay) trace fails at the boundary (#32).""" + lam = np.linspace(350e-9, 450e-9, 64) + trace = np.exp(-(((lam - 400e-9) / 15e-9) ** 2))[:, None].repeat( + delays.size, axis=1 + ) + with pytest.raises(ValueError, match="the delay axis needs at least two distinct"): + preprocess.load_and_clean( + trace, + lam, + delays, + "shg", + lam_min=600e-9, + lam_max=1000e-9, + input_unit="delay", + ) + + +def test_filter_frog_trace_rejects_single_delay(): + """Direct callers of the filter get the same boundary error (#32).""" + lam = np.linspace(700e-9, 900e-9, 6) + trace = np.ones((lam.size, 1)) + with pytest.raises(ValueError, match="the delay axis needs at least two distinct"): + preprocess.filter_frog_trace(trace, lam, np.array([0.0])) + + def test_tau_crop_reduces_delays(experiment): exp = experiment full = preprocess.load_and_clean(