Repository navigation
Read multi-segment channel metadata without samples - #770
Conversation
tompollard
left a comment
There was a problem hiding this comment.
This works in every case I tried, including real MIMIC-III records from PhysioNet. One suggestion before release: rename MultiSegmentHeaders, since it differs from MultiSegmentHeader by one letter. Some minor follow-ups below.
Checked:
- PhysioNet, three ways. I read MIMIC-III record
30/3000003as"3000003"withpn_dir="mimic3wdb/1.0/30/3000003", as"30/3000003/3000003"withpn_dir="mimic3wdb", and asphysionet://mimic3wdb/1.0/30/3000003/3000003. All three return the variable layout,3000003_layout, and signalsI, II, V, ABP, V+. - Only two files are read. For the
physionet://read, only3000003.heaand3000003_layout.heaare opened. strandPathpaths give equal results.- Tests:
tests/test_read_header.pyandtests/test_memory.py(41 passed) and ruff pass.
Suggestion: rename MultiSegmentHeaders. It differs from MultiSegmentHeader by one letter, which is easy to mix up in code, autocompletion and review. For example, isinstance(x, wfdb.MultiSegmentHeader) is false when x is the new type. A name such as MultiSegmentChannels or MultiSegmentSignalInfo would describe what it holds. Names are much harder to change after a release.
Minor:
- A variable-layout record without a layout header isn't reported. When the first segment entry is
~ 0rather than a layout header, which isn't valid, the result hassignal_reference=Noneandsignal_header=None, the same as for a record made only of gaps. RaisingValueErrorhere would distinguish a malformed record from one with no data. signal_referencepartly duplicatessignal_header.record_name. It also gives the segment's length from the main header, so keeping it is reasonable.- Two tests would help: a
@pytest.mark.networktest usingpn_diror aphysionet://path, like the MIMIC-III record above, and a test for the "is not a single-segment header" error, for example a fixed-layout record whose first segment is itself multi-segment.
|
Thanks for the detailed check. I addressed the naming and malformed-layout points in 6e9834c. The return type is now A variable-layout main header whose zero-length first entry is I added tests for that invalid layout, a nested multi-segment header in place of a data segment, and the MIMIC-III |
tompollard
left a comment
There was a problem hiding this comment.
Thanks for addressing the earlier points. I found one caveat in the new I/O description: the guide and docstring say the function does not read the remaining segment headers. For a supported .wfdb archive whose filename differs from the main header's name, WFDBArchive.header_file_name() reads every header in the archive to identify the main one.
I reproduced this with renamed.wfdb containing m.hea, s1.hea, and s2.hea. read_multi_segment_headers() returned the expected main and first-segment headers, but it read m.hea, s1.hea, s2.hea, then m.hea and s1.hea again. Could we qualify the documentation to say that only two headers are returned, while archive discovery may read others? If avoiding that I/O is part of the contract, the caller would need a way to identify the main header inside the archive.
The focused header and memory tests passed (43 passed, 1 network test deselected).
## Summary Prepare WFDB-Python 5.8.0 after the merged, additive metadata work in #767 and #770. This bumps the version to 5.8.0 and collects the two changelog fragments. #769 also clarified the existing multi-segment read contract. The legacy `rdrecord`, `rdheader`, and writer behavior remains unchanged. - `Record.metadata` provides an immutable snapshot of the current logical view and reports every loaded signal form. - `read_multi_segment_headers` reads the main multi-segment header plus the layout header (or first non-gap data segment header for fixed layout), without loading samples or all segment headers. ## Checks - `uv build` produced the 5.8.0 source distribution and wheel. - Sphinx HTML build passed with warnings treated as errors. - `git diff --check` passed. - The feature PRs passed CI before merging; this release PR will run CI again.
Summary
Add
read_multi_segment_headers(path)as an explicit metadata-only read for #442 and #746. It returns an immutableMultiSegmentHeaderBundlevalue containing:main_header: the multi-segment header and its segment references;signal_reference: the segment used for channel metadata;signal_header: that segment'sSingleSegmentHeader.For variable layout, the second header is the zero-length layout segment. For fixed layout, it is the first non-gap data segment, so a leading
~gap is skipped. If every segment is a gap, the latter two fields areNone. The function loads no signal samples or other segment headers, and works with local paths, local.wfdbarchives, and supported cloud URIs. It raisesValueErrorfor a single-segment input, a nested segment header, or a variable-layout header whose zero-length first entry is~. The existingread_headerandrdheaderbehavior is unchanged.The documentation keeps the two headers distinct: calibration in
signal_headerdescribes that source segment and should not be read as calibration for every segment.Checks
memory://tests: 18 passed, plus 1 PhysioNet integration test.