Repository navigation
Preserve source segment ranges on flattened records - #726
Conversation
tompollard
left a comment
There was a problem hiding this comment.
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-57ands25047-2704-05-04-10-44(variable layout) andv102sand041s(fixed layout), with a mix of physical and digital reads. In every case,source_segmentsmatched the boundaries computed independently from the header'sseg_len, clipped to the window, and the last range ended atsig_len. - Channel selection: reading
p000878withchannel_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 theirsource_segments, andm2s=Falsestill returns aMultiRecord. - 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.
|
Done in 4402c8d: >>> 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. |
Summary
Recordobjects assource_segments.~) segments and clip ranges to the requestedsampfrom/samptowindow. Exclude the zero-length layout 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 deselectedruff check src/wfdb/io/record.py tests/test_multi_record.pyruff format --check src/wfdb/io/record.py tests/test_multi_record.py-W --keep-going— passed