Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Validate center_frequency shape in compute_time_to_peak
A size-1 frequency is treated as a scalar, so a single waveform keeps
returning a scalar. A per-waveform frequency must match the waveforms'
leading shape; mismatches now raise a clear ValueError instead of
silently broadcasting or failing deep inside the backend.
  • Loading branch information
tristan-deep committed Oct 20, 2026
commit d617b64e5597605351bbd6a88d043ea5e04c8e63
12 changes: 12 additions & 0 deletions tests/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,18 @@ def test_compute_time_to_peak():
t_peak = compute_time_to_peak_stack(waveforms, center_frequencies, 250e6)
assert np.allclose(t_peak, 1e-6, atol=1e-8)

# A size-1 frequency acts as a scalar, both for one waveform and for a stack
t_peak = compute_time_to_peak(waveform0, np.array([center_frequency0]), 250e6)
assert np.shape(t_peak) == ()
t_peak = compute_time_to_peak(waveforms, np.array([center_frequency0]), 250e6)
assert np.shape(t_peak) == (2,)

# A frequency per waveform must match the number of waveforms
with pytest.raises(ValueError, match="center_frequency"):
compute_time_to_peak(waveform0, center_frequencies, 250e6)
with pytest.raises(ValueError, match="center_frequency"):
compute_time_to_peak(np.stack([waveform0] * 3), center_frequencies, 250e6)


# Only the beamformers zea.ops defines itself. A model-backed beamformer (such as
# "able", registered by zea.models.able) is not comparable across backends: its
Expand Down
12 changes: 10 additions & 2 deletions zea/func/ultrasound.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ def compute_time_to_peak(waveform, center_frequency, waveform_sampling_frequency
waveform (ndarray): The waveform of shape (n_samples), or a stack of them of
shape (n_waveforms, n_samples).
center_frequency (float or ndarray): The center frequency in Hz, either a scalar
or, for a stack, one per waveform of shape (n_waveforms,).
(or size-1 array) or, for a stack, one per waveform of shape (n_waveforms,).
waveform_sampling_frequency (float): The sampling frequency of the waveform in Hz.

Returns:
Expand All @@ -540,8 +540,16 @@ def compute_time_to_peak(waveform, center_frequency, waveform_sampling_frequency
if n_samples == 0:
raise ValueError("Waveform has zero samples.")

# Trailing axes for the sample and channel axes of `waveform[..., None]`
center_frequency = ops.convert_to_tensor(center_frequency)
batch_shape = tuple(waveform.shape[:-1])
if ops.size(center_frequency) == 1:
center_frequency = ops.reshape(center_frequency, ())
elif tuple(center_frequency.shape) != batch_shape:
raise ValueError(
f"center_frequency must be a scalar or have shape {batch_shape} to match the "
f"waveforms of shape {tuple(waveform.shape)}, got {tuple(center_frequency.shape)}."
)
# Trailing axes for the sample and channel axes of `waveform[..., None]`
center_frequency = ops.reshape(center_frequency, tuple(center_frequency.shape) + (1, 1))

waveforms_iq_complex_channels = demodulate(
Expand Down