Skip to content

Validate tp and fix noise-duration unit check in calculate_snr - #246

Merged
lispandfound merged 2 commits into
masterfrom
ai/issue-236
Oct 5, 2026
Merged

lispandfound merged 2 commits into
masterfrom
ai/issue-236

Conversation

@ucgmsim

@ucgmsim ucgmsim Bot commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

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

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>
@ucgmsim ucgmsim Bot added ai-generated Opened by the scheduled Claude jobs ai-fix Bot-written fix that needs review labels Oct 1, 2026
@ucgmsim
ucgmsim Bot requested a review from lispandfound October 1, 2026 13:18
@lispandfound

Copy link
Copy Markdown
Contributor

@joelridden point 1 seems significant, no?

@lispandfound
lispandfound merged commit 17d5559 into master Oct 5, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-fix Bot-written fix that needs review ai-generated Opened by the scheduled Claude jobs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

calculate_snr: noise-length check counts samples, not seconds; tp is never validated; SNRResult durations are documented

2 participants