Repository navigation
Validate tp and fix noise-duration unit check in calculate_snr - #246
Merged
Merged
Conversation
Closes #236 ## What was wrong `IM/snr_calculation.py:calculate_snr` had two related bugs: 1. The "noise duration must be at least 1s" check compared `noise_duration` (a sample count, equal to `tp`) directly against `1`, instead of `noise_duration * dt` against `1.0` seconds. A noise window of e.g. 0.5s (`tp=50`, `dt=0.01`) was silently accepted; the check only ever fired when `tp <= 0`. 2. `tp` was never validated against the waveform length `nt`. If `tp >= nt`, the signal slice became empty and the call failed deep inside `ims.fourier_amplitude_spectra` with an unrelated `OverflowError` (`cannot convert float infinity to integer`) instead of a clear error at the point of the bad input. Also fixed the accompanying docstrings: `SNRResult.signal_duration` / `noise_duration` were documented as seconds but are actually raw sample counts (now typed and documented as such, per the issue's suggestion — the SNR calculation only uses their ratio, so the unit choice doesn't affect results), and the `tp` parameter was documented as `float` but is used as an integer index (now documented as `int`). ## Fix - Added an upfront `if not (0 < tp < nt): raise ValueError(...)` check in `calculate_snr`. - Changed the noise-duration check to `noise_duration * dt < 1.0`, matching the documented "less than 1s" rule. - Updated `SNRResult` and `calculate_snr` docstrings accordingly. ## Test Added `tests/test_ims.py::test_snr_noise_duration_in_seconds` (a 1000-sample, `dt=0.01` waveform with `tp=50`, i.e. 0.5s of noise, now raises `ValueError` instead of being silently accepted) and `tests/test_ims.py::test_snr_tp_validated_against_record_length` (parametrized over `tp` of `0`, `-1`, `1000`, `1200` against a 1000-sample waveform, all of which now raise a clear `ValueError` instead of `tp >= nt` crashing with `OverflowError` downstream). Both tests failed before the fix for the reasons described in the issue, and pass after it. ## For the reviewer - `tp == nt - 1` (e.g. `tp=999` for `nt=1000`) is intentionally still accepted: it satisfies `0 < tp < nt` and produces a very short (2-sample) signal window, per the issue's suggested fix of validating `0 < tp < nt` rather than requiring a minimum signal length. Worth double-checking this matches the intended behaviour. - `signal_duration`/`noise_duration` are kept as sample counts (not converted to seconds) to avoid changing the numeric values returned by `calculate_snr` beyond what the issue describes; only the type annotation and docstrings were corrected. _Cost $0.48, 27 turns._ 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Contributor
|
@joelridden point 1 seems significant, no? |
lispandfound
approved these changes
Oct 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #236
What was wrong
IM/snr_calculation.py:calculate_snrhad two related bugs:noise_duration(a sample count, equal to
tp) directly against1, instead ofnoise_duration * dtagainst1.0seconds. A noise window of e.g. 0.5s(
tp=50,dt=0.01) was silently accepted; the check only ever fired whentp <= 0.tpwas never validated against the waveform lengthnt. Iftp >= nt, the signal slice became empty and the call failed deep insideims.fourier_amplitude_spectrawith an unrelatedOverflowError(
cannot convert float infinity to integer) instead of a clear error atthe point of the bad input.
Also fixed the accompanying docstrings:
SNRResult.signal_duration/noise_durationwere documented as seconds but are actually raw samplecounts (now typed and documented as such, per the issue's suggestion — the
SNR calculation only uses their ratio, so the unit choice doesn't affect
results), and the
tpparameter was documented asfloatbut is used as aninteger index (now documented as
int).Fix
if not (0 < tp < nt): raise ValueError(...)check incalculate_snr.noise_duration * dt < 1.0, matchingthe documented "less than 1s" rule.
SNRResultandcalculate_snrdocstrings accordingly.Test
Added
tests/test_ims.py::test_snr_noise_duration_in_seconds(a 1000-sample,dt=0.01waveform withtp=50, i.e. 0.5s of noise, now raisesValueErrorinstead of being silently accepted) and
tests/test_ims.py::test_snr_tp_validated_against_record_length(parametrizedover
tpof0,-1,1000,1200against a 1000-sample waveform, all ofwhich now raise a clear
ValueErrorinstead oftp >= ntcrashing withOverflowErrordownstream). Both tests failed before the fix for the reasonsdescribed in the issue, and pass after it.
For the reviewer
tp == nt - 1(e.g.tp=999fornt=1000) is intentionally stillaccepted: it satisfies
0 < tp < ntand produces a very short (2-sample)signal window, per the issue's suggested fix of validating
0 < tp < ntrather than requiring a minimum signal length. Worth double-checking this
matches the intended behaviour.
signal_duration/noise_durationare kept as sample counts (notconverted to seconds) to avoid changing the numeric values returned by
calculate_snrbeyond what the issue describes; only the type annotationand docstrings were corrected.
Cost $0.48, 27 turns.
🤖 Generated with Claude Code