Skip to content

Do not apply skew twice to rewritten records - #677

Merged
tompollard merged 1 commit into
mainfrom
wrsamp-applied-skew
Nov 24, 2026
Merged

tompollard merged 1 commit into
mainfrom
wrsamp-applied-skew

Conversation

@tompollard

Copy link
Copy Markdown
Member

rdrecord aligns skewed signals, but the record keeps its skew field. Record.wrsamp then wrote the aligned samples with a header that still gave the skew, so reading the new record shifted those signals a second time, with no error:

record = wfdb.rdrecord("sample-data/100skew", physical=False)
record.wrsamp(write_dir="out")
written = wfdb.rdrecord("out/100skew", physical=False)
np.array_equal(record.d_signal, written.d_signal)  # False on main, True here

This is the first step towards a reliable read and write round trip (#484, #500). Of the 75 single-segment records in sample-data, the 4 with skew were written incorrectly. All 4 now read back with the same samples.

The record now remembers the skew applied when it was read, and Record.wrsamp leaves it out of the header.

  • record.skew does not change, so code that reads it after rdrecord is not affected.
  • A value assigned to record.skew after reading is written to the header.
  • Records read with ignore_skew=True are still written with their skew, and their signal files are byte-identical to the originals.
  • wrheader on its own still writes the skew, since that header may describe the original signal files.
  • The marker is ignored by Record.__eq__.
  • Skewed segments of a multi-segment record are handled through the same path.

wfdb_to_wav with write_header=True had the same problem, and now writes no skew.

Testing:

  • New TestWriteAppliedSkew, covering full and partial reads, channel selection, physical and digital reads, ignore_skew, an assigned skew, wrheader, equality and multi-segment records, and test_write_header_skew for wfdb_to_wav. The tests for the bug fail on main.
  • uv run pytest -m "not network" -W error (582 passed), the network tests in tests/test_record.py (14 passed), ruff format --check, ruff check and the docs build with -W pass. Changelog entry added in changelog.d/.

Refs #484, #500

@tompollard 🟥 red 🐳 whale · quince

rdrecord aligns skewed signals, but the record keeps its skew field, so
Record.wrsamp wrote aligned samples with a header that still gave the
skew. Reading the new record shifted those signals a second time.

Remember the skew applied when reading, and leave it out of the header
when writing the samples. The skew attribute is not changed, a value
assigned to it after reading is written, and wrheader on its own still
describes the original signal files. wfdb_to_wav had the same problem
with write_header=True.

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

Reviewed commit 3612391. I found no issues. The write path suppresses skew already applied to the samples while preserving the in-memory skew value and allowing a newly assigned value to be written. All 125 offline record tests and the focused WAV header test pass. Docs and style checks pass; the test matrix is still running. Ready to merge once CI passes.

@tompollard
tompollard merged commit 72fec54 into main Nov 24, 2026
18 checks passed
@tompollard
tompollard deleted the wrsamp-applied-skew branch November 24, 2026 01:55
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.

1 participant