Skip to content

Expose source provenance on loaded records - #801

Merged
tompollard merged 3 commits into
mainfrom
api/source-provenance
Nov 30, 2026
Merged

tompollard merged 3 commits into
mainfrom
api/source-provenance

Conversation

@tompollard

@tompollard tompollard commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Summary

Expose the source of a loaded Record, MultiRecord, or SignalRead through three read-only properties:

  • source_header is an immutable view of the original header, before selection, length inference, or skew alignment.
  • source_signal_header is the header defining the source channel indices. For multi-segment data, this is the layout header or a segment header already loaded by the read.
  • source_selection records the requested source frame range and channel indices. Frames refer to source_header; channels refer to source_signal_header.signals.

The existing mutable record fields still describe the returned view. The source properties remain independent of later edits to those fields. Copies retain provenance; pickling drops it as before. In-memory records and legacy header-only reads have no retained source.

The public SourceSelection value is frozen. This adds no file-location property; that contract can be addressed separately.

Related to #743 and the API modernization in #672.

Validation

  • Full offline suite: 1,335 passed, 4 skipped, 22 deselected
  • Ruff check and format check: passed
  • Strict Sphinx HTML build: 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.

Thanks, exposing this read-only makes sense, and most of it was already tracked privately. Single-segment reads look right: source_header keeps an omitted length as None and the stored skew, source_selection.stop is the resolved stop (650000 when none is given), channels are resolved indices in requested order (including from channel_names), archives work, and edits to the record don't affect either view. The offline suite, Ruff and the strict docs build pass locally.

Two things I'd fix before these become public:

  • Copies drop the provenance. copy.copy(loaded).source_selection and copy.deepcopy(loaded).source_selection are both None, because __getstate__ (meant for pickling) is also used by the copy module. The docs only mention pickling, and both values are immutable, so copies could keep them. Could you add __copy__/__deepcopy__ that keep _read_source (leaving pickling as is), plus a test next to the pickle one?
  • Multi-segment channel indices can't be resolved from source_header. The docs say channels are indices "in its source header", but a MultiSegmentHeader has segments and n_signals, not a channel list. For 041s, channels=(1,) means I, and for the variable-layout p000878 record (0,) means PLETH. Both names live in the layout or first segment header (read_multi_segment_headers(path).signal_header), which the record doesn't keep, and there's no path to re-read it. Could the read source also keep that signal header and expose it (for example source_signal_header)? If not, the docs should say where the indices point.

Optional:

  • SignalRead from read_signals doesn't have these properties, although it has source_segments. It would be good for the modern reader to match, here or in a follow-up.
  • SourceSelection is exported from wfdb and wfdb.io, but SourceSegment only from wfdb.io. Exporting both the same way would be tidier.

@tompollard

Copy link
Copy Markdown
Member Author

Thanks, I fixed both points in a903fd0.

copy.copy and copy.deepcopy now retain the read provenance, including on subclasses with slots. Pickling still drops it. I added tests for both copy paths and updated the existing slot test.

For multi-segment reads, source_signal_header now holds the channel-defining layout header or first non-gap segment header. source_selection.channels indexes its signals; frame offsets still refer to the main source_header. If a fixed-layout read selects later frames and the first segment header is unavailable, source_signal_header is None and the read still succeeds. The tests cover fixed and variable layouts, an archive, and that missing-header case.

I also exposed these views on SignalRead, carried them through conversions, and exported SourceSegment from the top-level package. The docs and PR description now explain the coordinate mapping.

Validation: 1,334 offline tests passed; Ruff and the strict docs build 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.

Thanks, a903fd0 covers both points and the optional ones. Copies keep the provenance (pickling still drops it), source_signal_header resolves the channel indices (041s with channels=(1,) gives I, matching the returned record), multi-segment archives work, SignalRead has the same views and keeps them through to_digital() and to_physical(), and SourceSegment is exported. The offline suite, Ruff and the strict docs build pass locally.

One remaining issue with how source_signal_header is found for fixed layouts. It always uses the first non-gap segment, so when the selected frames lie in a later segment the read does an extra header read it didn't otherwise need. Reading 041s with start=1500, stop=1800 reads 041s, 041s02 and then 041s01. On PhysioNet that's an extra request, and only FileNotFoundError is caught: when I made that extra read raise PermissionError, read_record failed after the signal data had already loaded. A network error or a malformed first-segment header would do the same.

Since every segment of a fixed-layout record has the same channels, any segment header the read already loaded defines the same indices. Could source_signal_header use the first non-gap segment that was actually read (it's already in header_cache)? That would avoid both the extra request and the failure, and the docstring could say "a segment header from the read" rather than "the first non-gap segment".

@tompollard

Copy link
Copy Markdown
Member Author

Thanks, you're right. Fixed in f2c33e7. For a fixed-layout read, source_signal_header now comes from the first non-gap segment actually loaded for the selected frames. It no longer requests an earlier segment just to populate provenance. Variable-layout reads continue to use the layout header already cached during the read.

I changed the docs to say "a segment header already loaded by the read." The new test selects only the second segment, makes any attempt to read the first header raise PermissionError, and checks both combined and preserved results. The full offline suite passes (1,335 tests), as do Ruff and the strict docs build.

@tompollard
tompollard merged commit 90587ba into main Nov 30, 2026
19 checks passed
@tompollard
tompollard deleted the api/source-provenance branch November 30, 2026 18:59
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