Skip to content

Read multi-segment channel metadata without samples - #770

Merged
tompollard merged 3 commits into
mainfrom
read-multi-segment-headers
Nov 29, 2026
Merged

tompollard merged 3 commits into
mainfrom
read-multi-segment-headers

Conversation

@tompollard

@tompollard tompollard commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Summary

Add read_multi_segment_headers(path) as an explicit metadata-only read for #442 and #746. It returns an immutable MultiSegmentHeaderBundle value containing:

  • main_header: the multi-segment header and its segment references;
  • signal_reference: the segment used for channel metadata;
  • signal_header: that segment's SingleSegmentHeader.

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 are None. The function loads no signal samples or other segment headers, and works with local paths, local .wfdb archives, and supported cloud URIs. It raises ValueError for a single-segment input, a nested segment header, or a variable-layout header whose zero-length first entry is ~. The existing read_header and rdheader behavior is unchanged.

The documentation keeps the two headers distinct: calibration in signal_header describes that source segment and should not be read as calibration for every segment.

Checks

  • Full offline suite: 1,006 passed, 4 skipped, 22 network tests deselected.
  • Focused header and memory:// tests: 18 passed, plus 1 PhysioNet integration test.
  • Ruff lint and format checks passed.
  • Sphinx HTML build passed with warnings treated as errors.

@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.

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/3000003 as "3000003" with pn_dir="mimic3wdb/1.0/30/3000003", as "30/3000003/3000003" with pn_dir="mimic3wdb", and as physionet://mimic3wdb/1.0/30/3000003/3000003. All three return the variable layout, 3000003_layout, and signals I, II, V, ABP, V+.
  • Only two files are read. For the physionet:// read, only 3000003.hea and 3000003_layout.hea are opened.
  • str and Path paths give equal results.
  • Tests: tests/test_read_header.py and tests/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 ~ 0 rather than a layout header, which isn't valid, the result has signal_reference=None and signal_header=None, the same as for a record made only of gaps. Raising ValueError here would distinguish a malformed record from one with no data.
  • signal_reference partly duplicates signal_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.network test using pn_dir or a physionet:// 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.

@tompollard

Copy link
Copy Markdown
Member Author

Thanks for the detailed check. I addressed the naming and malformed-layout points in 6e9834c. The return type is now MultiSegmentHeaderBundle, which is easier to distinguish from the single source MultiSegmentHeader and still describes what it contains.

A variable-layout main header whose zero-length first entry is ~ now raises ValueError instead of returning an empty channel source. I kept signal_reference because it preserves the selected segment's length and position from the main header, which signal_header.record_name alone does not give.

I added tests for that invalid layout, a nested multi-segment header in place of a data segment, and the MIMIC-III physionet:// path you checked. The PhysioNet test passed with network access. The full offline suite passes (1,006 passed, 4 skipped, 22 network tests deselected), as do Ruff and the strict docs build.

@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 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).

@tompollard
tompollard merged commit 9a6076a into main Nov 29, 2026
19 checks passed
@tompollard
tompollard deleted the read-multi-segment-headers branch November 29, 2026 23:30
tompollard added a commit that referenced this pull request Nov 29, 2026
## 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.
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