Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/single-delay-axis.fixed.md
Original file line number Diff line number Diff line change
@@ -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).
21 changes: 20 additions & 1 deletion croak/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
34 changes: 34 additions & 0 deletions tests/test_preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down