Skip to content

Preserve source segment ranges on flattened records - #726

Merged
tompollard merged 2 commits into
mainfrom
issue-405-segment-provenance
Nov 25, 2026
Merged

tompollard merged 2 commits into
mainfrom
issue-405-segment-provenance

Conversation

@tompollard

Copy link
Copy Markdown
Member

Summary

  • Preserve each source segment's name and half-open frame range on flattened Record objects as source_segments.
  • Include explicit empty (~) segments and clip ranges to the requested sampfrom/sampto window. Exclude the zero-length layout header.
  • Document that this provenance is kept in memory and is not serialized to a single-segment WFDB header.

For example, reading a window spanning an empty segment and a data segment yields [("~", 0, 24740), ("3234460_0001", 24740, 25000)].

Fixes #405.

Validation

  • pytest -m 'not network' -q — 748 passed, 4 skipped, 21 deselected
  • ruff check src/wfdb/io/record.py tests/test_multi_record.py
  • ruff format --check src/wfdb/io/record.py tests/test_multi_record.py
  • Sphinx HTML build with -W --keep-going — 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.

Looks good to merge. It does what #405 asks: the boundaries of empty segments, and of all the others, are kept after flattening. One optional suggestion.

Checked:

  • Boundaries: I read 160 random windows across p000878-2137-10-26-16-57 and s25047-2704-05-04-10-44 (variable layout) and v102s and 041s (fixed layout), with a mix of physical and digital reads. In every case, source_segments matched the boundaries computed independently from the header's seg_len, clipped to the window, and the last range ended at sig_len.
  • Channel selection: reading p000878 with channel_names=["ABP"], a channel with no samples anywhere, still lists all three segments. Gaps therefore stay distinguishable from channels that are simply absent.
  • Equality and m2s=False: records compare equal whatever their source_segments, and m2s=False still returns a MultiRecord.
  • Tests: pytest -m "not network" (748 passed) and ruff pass.

Optional: a named tuple, such as SourceSegment(name, start, stop), would make code that uses this more readable (segment.start), and it still unpacks and compares like a tuple. It would be easy to change now, and harder after a release.

Unrelated: a flattened record can't be written with wrsamp at all, on main as well: v102s fails with "Missing field required: adc_zero", and p000878 with KeyError: None. The docstring is right that source_segments isn't written to a header, but I've opened #727 for the failure itself.

Record.source_segments now holds SourceSegment named tuples, so ranges
can be used by field name (segment.start) as well as unpacked or
compared as (name, start, stop) tuples.
@tompollard

Copy link
Copy Markdown
Member Author

Done in 4402c8d: source_segments now holds wfdb.io.SourceSegment named tuples, with fields name, start and stop:

>>> record.source_segments
[SourceSegment(name='~', start=0, stop=24740), SourceSegment(name='3234460_0001', start=24740, stop=25000)]

They still unpack and compare like plain tuples, so the existing tests pass unchanged. A new test checks field access and unpacking. SourceSegment is exported from wfdb.io and documented in the I/O reference, and the changelog entry mentions it.

@tompollard
tompollard merged commit 167f671 into main Nov 25, 2026
19 checks passed
@tompollard
tompollard deleted the issue-405-segment-provenance branch November 25, 2026 00:23
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.

Information contained in multi-segment records and carrying it forward to single-segment records

1 participant