Repository navigation
Correct format 8 window decoding - #911
Conversation
tompollard
left a comment
There was a problem hiding this comment.
The fix is correct: windowed reads now return the values the format defines. But it moves wfdb-python away from the WFDB C library, which keeps the old behaviour. We've decided to keep the fix, so the difference needs documenting before merging. The offline suite (2,177 passed, 4 skipped), Ruff and CI pass.
What I checked
- Windows now match the full read. On
binformatsand the format 8 records I built for #902, in both kinds and both layouts, windows starting at frames 1, 2, 3, 250, 477 and near the end all equal the full read sliced to the same frames: 96 of 96. Those records cover single-segment, fixed and variable layouts, and gaps.rdrecord(..., sampfrom=477)now returns[570, 442, 314]. - Nothing else changes. Against
main(e2ffc00), my 3,520-call single-segment comparison differs only in 8binformatspartial windows, and the 976-call multi-segment comparison doesn't differ at all.
To document: WFDB's C library behaves like the old code
I said in #909 that WFDB "has to do the same". I was wrong, and the comment this PR deletes was right. In lib/signal.c, isgsetframe treats format 8 like format 80: case 8: case 80: default: b = nn; break; (around line 2588). It then does a plain byte seek, and discards the bytes before the target sample without accumulating them:
/* Read any bytes in the current block that precede the desired sample. */
while (nb-- > 0 && ig->stat > 0)
i = r8(ig);The running value is set to the header's initval only when the signal is opened (around lines 2744 and 3161). So rdsamp -f 477 on a format 8 record starts from the initial value at frame 477, as wfdb-python did before this PR.
We've decided to keep this fix. The old values aren't a different convention, they're wrong samples. Please document the difference where people will look:
- In the code comment: WFDB's
rdsampstarts from the initial value when seeking in format 8, and this library deliberately reconstructs the running value instead. - In the changelog entry: windowed format 8 reads now differ from WFDB's
rdsamp, and from earlier wfdb-python releases, so that anyone comparing againstrdsampoutput knows why. - In the WFDB differential CI job, if it ever covers format 8 windows, as a documented exception.
I'll correct my statement on #909.
Note
The prefix sum reads every byte before the window, in 8 MiB chunks. So a window near the end of a long format 8 record costs about as much as a full read. That's inherent to the format, and the memory bound is the right protection. Saying so in the changelog would set expectations.
|
Thanks for checking this against the C library. I agree we should keep the corrected samples and make the difference explicit. In f12611f I added the WFDB C The current differential CI job covers |
## Summary Fixes #907. `read_signals` now translates legacy `sampfrom`/`sampto` wording to its public `start`/`stop` names for invalid windows on multi-segment records. A shared helper keeps the modern `read_signals` and `read_record` messages aligned with `read_segments`, including accurate wording that `stop` may equal the record length. Empty records keep their existing error. This is separate from the format-8 decoding fix in #911. ## Checks - Seven regression cases cover `read_signals`, `read_record`, and `read_segments`, checking both invalid bounds on single- and multi-segment records. - Full offline suite: 2,140 passed, 4 skipped. - Ruff lint and formatting pass.
Summary
Fixes #909. Format 8 stores differences between samples. A window starting after frame zero previously added those differences to the header's initial value without accounting for earlier frames, silently returning incorrect digital and physical values. The shared decoder now sums preceding differences per channel in bounded chunks and decodes only the requested window.
The fix applies to
read_signals,read_record,rdrecord, and format-8 segments within multi-segment records. It handles interleaved channels, multiple samples per frame, byte offsets, and skew.WFDB's C
rdsampseeks directly in format 8 and starts the running value from the header'sinitval, so its windowed output retains the old incorrect values. This intentional difference is documented in the code and changelog. A late window must process earlier bytes; the implementation limits memory use but may take about as long as a full read.Checks
The separate format-8 skew-tail case exposed during testing is tracked in #910. It predates this change and requires a missing-sample representation fix for a format with no reserved marker.