Repository navigation
Expose source provenance on loaded records - #801
Conversation
tompollard
left a comment
There was a problem hiding this comment.
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_selectionandcopy.deepcopy(loaded).source_selectionare bothNone, because__getstate__(meant for pickling) is also used by thecopymodule. 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 saychannelsare indices "in its source header", but aMultiSegmentHeaderhas segments andn_signals, not a channel list. For041s,channels=(1,)meansI, and for the variable-layoutp000878record(0,)meansPLETH. 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 examplesource_signal_header)? If not, the docs should say where the indices point.
Optional:
SignalReadfromread_signalsdoesn't have these properties, although it hassource_segments. It would be good for the modern reader to match, here or in a follow-up.SourceSelectionis exported fromwfdbandwfdb.io, butSourceSegmentonly fromwfdb.io. Exporting both the same way would be tidier.
|
Thanks, I fixed both points in a903fd0.
For multi-segment reads, I also exposed these views on Validation: 1,334 offline tests passed; Ruff and the strict docs build passed. |
tompollard
left a comment
There was a problem hiding this comment.
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".
|
Thanks, you're right. Fixed in f2c33e7. For a fixed-layout 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 |
Summary
Expose the source of a loaded
Record,MultiRecord, orSignalReadthrough three read-only properties:source_headeris an immutable view of the original header, before selection, length inference, or skew alignment.source_signal_headeris 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_selectionrecords the requested source frame range and channel indices. Frames refer tosource_header; channels refer tosource_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
SourceSelectionvalue is frozen. This adds no file-location property; that contract can be addressed separately.Related to #743 and the API modernization in #672.
Validation