Skip to content

Preserve source header and selection on loaded reads - #762

Merged
tompollard merged 3 commits into
mainfrom
preserve-read-source-selection
Nov 28, 2026
Merged

tompollard merged 3 commits into
mainfrom
preserve-read-source-selection

Conversation

@tompollard

@tompollard tompollard commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Summary

Keep the parsed source header and requested source coordinates on records returned by rdrecord, including flattened and segment-preserving multi-segment reads. The new _read_source field is private. It holds the source header, frame range, and selected channel indices; it is not a second mutable copy of the returned record's metadata.

Parsed header comments are now tuples, making the cached source header immutable throughout. Editing the returned Record or MultiRecord metadata does not change that source description. The cache keeps the original parsed header separate from an effective copy when a missing signal length must be inferred, so _read_source.header still records the omission.

Record equality continues to compare record contents rather than read provenance. Private read provenance is omitted from pickle state so records saved with this branch can still be loaded by the PR base version. The pickle implementation preserves slot state on user-defined Record subclasses.

This prepares the source-versus-selection contract in #743 without adding a public API yet.

Checks

  • Offline suite: 927 passed, 4 skipped, 21 deselected.
  • Sphinx HTML build with warnings as errors passed.
  • Ruff format and lint passed.
  • git diff --check passed.
  • A multi-segment record pickled from this branch loaded successfully with the PR base version.

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

I found two issues with the new read provenance:

  1. The retained source header can differ from the file (src/wfdb/io/record.py:2803-2810, 3010). When a header omits sig_len, the read path replaces it with an inferred length before attaching it as _read_source.header. For sample-data/100-no-len, rdheader().sig_len is None, while rdrecord()._read_source.header.record_fields.sig_len is 650000. If this field is meant to preserve the parsed source header, keep the original header there and use a separate effective copy for reading and caching.

  2. The new field changes pickle compatibility (src/wfdb/io/record.py:2450-2458, 3010). A record pickled by this branch cannot be loaded by the PR's base version: it raises AttributeError: Can't get attribute '_ReadSource'. If saved records need to survive a downgrade or move to an older installation, exclude this private provenance from the serialized state. If cross-version pickle compatibility is outside the contract, this is a limitation to document rather than a blocker.

The full offline suite passed (923 passed, 4 skipped, 21 deselected).

@tompollard

Copy link
Copy Markdown
Member Author

Thanks, I fixed both points in b2a4ee3.

  1. The header cache now keeps the parsed source header separate from the effective copy used for reading. If sig_len is absent, _read_source.header.record_fields.sig_len stays None; the inferred length goes only into the effective copy. I added coverage for direct reads and successive DataFrame chunks.
  2. BaseRecord.__getstate__ now leaves _read_source out of pickles, including nested segment records. I added tests for flattened and segment-preserving reads, and verified that a multi-segment record pickled from this branch loads with the PR base version.

The full offline suite passes (926 passed, 4 skipped, 21 deselected), as do the docs build and Ruff checks. I also updated the PR description.

@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 update addresses both points from my previous review: an omitted source sig_len stays unset in _read_source.header, and ordinary Record and MultiRecord pickles load with the base version.

I found one new serialization regression in BaseRecord.__getstate__ (src/wfdb/io/record.py:273-277). Returning only self.__dict__ drops slot state from subclasses. A Record subclass with __slots__ = ("tag",) and tag = "kept" retains tag after a pickle roundtrip on the base commit, but loses it on this branch. copy.deepcopy also loses it. Please preserve the default slot state and remove _read_source only from the dictionary portion.

The affected offline tests pass (223 passed, 14 deselected).

@tompollard

Copy link
Copy Markdown
Member Author

Good catch. I fixed this in 44f4f02. BaseRecord.__getstate__ now starts with Python's default object state, which includes subclass slots, and removes _read_source only from the dictionary portion.

I added a Record subclass with a tag slot and verified that both pickle roundtrips and copy.deepcopy retain tag while omitting the private read information. I also rechecked that a multi-segment record pickled from this branch loads with the PR base version. The offline suite passes (927 passed, 4 skipped, 21 deselected), along with the docs build and Ruff checks.

@tompollard
tompollard merged commit f7a174d into main Nov 28, 2026
19 checks passed
@tompollard
tompollard deleted the preserve-read-source-selection branch November 28, 2026 04:00
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