Repository navigation
Zero-pad waveform averages before the record starts - #731
Conversation
Updated indexing logic to handle negative NumPy indices and removed unnecessary exception handling.
Update documentation to clarify waveform average padding behavior.
tompollard
left a comment
There was a problem hiding this comment.
The bounds check fixes the negative-index wraparound, and the focused tests pass. I found one edge case to address before approval: sigavg returns physical units, but skipping an out-of-range position contributes digital zero before the code subtracts rec.baseline[j] and divides by rec.adc_gain[j].
For example, with a flat signal of digital value 100, baseline 100, and gain 2, the signal is 0 mV, but a beat at sample zero produces -50 mV at both pre-record positions. The existing end padding has the same issue. Could you contribute the channel's digital baseline for out-of-range positions at both ends, so the calibrated padding is 0 in the returned units, and add a regression case with a nonzero baseline?
I ran the new and existing focused processing tests locally: 222 passed. Ruff check and format also passed.
Added tests for signal averaging with zero padding and flat signals.
Updated the description of waveform average padding to clarify the handling of out-of-range positions and the use of the channel's digital baseline.
tompollard
left a comment
There was a problem hiding this comment.
Looks great, thanks @Kulraj69
Problem
processing.sigavgreads negative sample positions as NumPy negative indices. For a beat near the start of a record, the averaging window before time zero therefore contains unrelated samples from the end of the record.This is reachable through the public API with real local records and annotations. With a beat at sample zero, changing only the final sample from 9 to 9000 changes the pre-record estimate.
Change
Check both bounds before adding a sample to the average. For an out-of-range position at either end, add the channel's digital baseline so the padding becomes zero in physical units after calibration. The time grid and denominator are unchanged.
The first revision only skipped out-of-range samples. Review identified that digital zero is not physical zero for a nonzero baseline. This revision corrects both boundaries and adds eight regressions, including a flat zero-mV signal with baseline 100 and gain 2, positive and negative baselines, different gains, and mixed boundary/interior beats.
The tests write and read actual WFDB records and annotations. They also retain the original first/second-sample tail-independence tests and interior/end-padding controls. Expected results use explicit bounded physical-unit averages.
Validation
Against upstream
98df2c9fba00943004ceac42677744f85a98e702, using Python 3.12.16:tests/test_processing.pyandtests/test_hr.py, excluding network tests: 230 passed. Repeated against the exact files downloaded from the revised branch: 230 passed.-W --keep-going -E) passed, including the revised changelog.Other private fixes were not applied during these isolated runs. Full-repository tests and other Python/dependency versions were not run locally. Remote CI is separate from these local results.
Prepared and tested with AI assistance. I have not obtained independent human review of the revised code; the earlier changes-requested review remains pending re-review.