Skip to content

Correct format 8 window decoding - #911

Merged
tompollard merged 2 commits into
mainfrom
fix/format8-window
Dec 4, 2026
Merged

tompollard merged 2 commits into
mainfrom
fix/format8-window

Conversation

@tompollard

@tompollard tompollard commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

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 rdsamp seeks directly in format 8 and starts the running value from the header's initval, 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

  • 44 regression cases compare windows with full reads sliced to the same frames, covering modern and legacy readers, physical and digital values, both layouts, segment-local windows, interleaved channels, byte offsets, skew, and forced multi-chunk prefix reads.
  • Full offline suite: 2,177 passed, 4 skipped.
  • Ruff and warnings-as-errors Sphinx build pass.

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.

@tompollard tompollard left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 binformats and 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 8 binformats partial 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 rdsamp starts 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 against rdsamp output 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.

@tompollard

Copy link
Copy Markdown
Member Author

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 rdsamp behavior to the decoder comment and changelog, including the fact that late format-8 windows must process the earlier bytes and can take about as long as a full read. I also updated the PR description.

The current differential CI job covers bxb, not signal windows, so it needs no exception for this PR. If a signal differential job is added later, format-8 windows should be listed as an intentional difference. The 44 focused tests, Ruff, and formatting still pass.

@tompollard
tompollard merged commit a8ad0c4 into main Dec 4, 2026
19 checks passed
@tompollard
tompollard deleted the fix/format8-window branch December 4, 2026 17:29
tompollard added a commit that referenced this pull request Dec 4, 2026
## 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Format 8 reads that start after frame 0 return wrong values

1 participant