Skip to content

Handle missing skew tails in format 8 reads - #913

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

tompollard merged 2 commits into
mainfrom
fix/format8-skew-tail

Conversation

@tompollard

@tompollard tompollard commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes #910. A skewed format-8 signal has unavailable samples at the end of an aligned read, but format 8 has no reserved digital missing-sample value. The decoder previously tried to assign None to its integer array and raised TypeError.

  • Use a temporary zero in the decoder and mark the unavailable tail in modern reads: masked digital samples or physical NaNs. Raw reads remain unmasked.
  • Return physical NaNs from legacy rdrecord; raise a clear ValueError only for digital legacy reads, which cannot represent the missing tail. ignore_skew=True remains available for raw data.
  • Fill every sample of a missing frame in expanded reads with multiple samples per frame. This fixes incorrect zeros in every format, including legacy rdrecord.
  • Add tests for both layouts, both kinds, partial windows, channel reordering, combined and uncombined multi-segment reads, legacy behavior, and writing the masked result in format 16.

Checks

  • Offline suite: 2,199 passed, 4 skipped, 23 deselected (using the project's compatible PhysioNet package path).
  • Ruff check and format passed.

@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.

This fixes #910, and the expanded-layout change fixes a silent bug in every format. It can merge. I have one suggestion for legacy physical reads and one changelog point. The offline suite (2,196 passed, 4 skipped), Ruff and CI pass.

What I checked

  • Nothing else changes. My comparisons against main (ceb13f2), 3,520 single-segment calls and 976 multi-segment calls, show zero differences. No sample record has a skewed tail that this touches.

  • Format 8 tails. I built a skewed format 8 segment followed by a format 16 segment, as an 8 + 8 frame multi-segment record with skew 2 on the format 8 channel:

    Reader main This branch
    read_signals, digital TypeError segment a's last 2 frames masked, segment b intact
    read_segments TypeError the same 2 frames masked in entry a
    rdrecord, all forms (single-segment, m2s=True/False) TypeError the new ValueError
    read_record TypeError the new ValueError

    So the legacy check covers multi-segment reads too, through the segment reads. rdrecord of binformats, which is format 8 without skew, is unaffected.

Changelog: the expanded fix affects all formats

For a skewed channel with several samples per frame, _skew_sig used to mark only nan_replace samples rather than nan_replace × samples_per_frame. The remaining samples of the missing frames came back as unmarked zeros. With a format 16 channel at 2 samples per frame and skew 2, main's rdrecord(..., smooth_frames=False) ends with [..., 16, 0, 0, -32768, -32768], and read_signals masks only the last two. Here all four are marked in both readers.

That's wrong data on main for any format, not only format 8. The entry's last sentence covers it, but it's the more widely relevant fix. I'd make it its own sentence or entry: "Expanded reads of skewed signals with more than one sample per frame previously returned some samples beyond the end of the signal as 0 instead of missing; every sample of each missing frame is now marked, in rdrecord as well as the new readers."

Suggestion: legacy physical reads don't need to fail

rdrecord(path), which is physical by default, now raises "Cannot represent the skewed tail of a format 8 signal in a legacy Record" for the format 8 record above. But a physical Record can represent the tail as NaN, as it does for every other format. Only physical=False lacks a marker. Limiting the error to digital legacy reads, and returning NaN for physical ones, would keep the common case working. That would need a matching change to the message and changelog. It's not a regression, since main raised TypeError here, so it can be a follow-up if you'd rather keep this PR small.

@tompollard

Copy link
Copy Markdown
Member Author

Agreed on both points. Legacy physical reads now return NaNs for the unavailable format-8 tail; only digital legacy reads raise ValueError, since they have no missing-sample marker. I added tests for uniform and expanded reads, partial windows, and combined and uncombined multi-segment reads.

I also expanded the changelog to call out the separate bug affecting every format: expanded reads with multiple samples per frame could return zeros for some unavailable samples. The entry now names rdrecord as well as the modern readers. The offline suite passes (2,199 passed, 4 skipped), and Ruff passes. CI is rerunning on the new commit.

@tompollard
tompollard merged commit 1c6e9f0 into main Dec 4, 2026
19 checks passed
@tompollard
tompollard deleted the fix/format8-skew-tail branch December 4, 2026 17:50
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 skewed reads to record end fail without a missing marker

1 participant