Skip to content

Add a common header-only channel metadata reader - #827

Merged
tompollard merged 4 commits into
mainfrom
api/read-signal-header
Dec 1, 2026
Merged

tompollard merged 4 commits into
mainfrom
api/read-signal-header

Conversation

@tompollard

@tompollard tompollard commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Summary

Add read_channel_header(path) so header-only callers can inspect signal names, units, and samples per frame through the same immutable SingleSegmentHeader type for ordinary and multi-segment records. The new function accepts one explicit path or URI; it does not take the deprecated pn_dir argument.

  • Single-segment: return its header.
  • Variable layout: return the zero-length layout header.
  • Fixed layout: return the first non-gap segment header.
  • All gaps: return None.

The function reads no samples and does not add mutable channel fields to MultiRecord. Calibration and file layout remain explicitly tied to the returned source header, which may differ from later segments. This addresses the header-only use case in #441 and advances #746.

Checks

  • uv run pytest tests/test_read_header.py tests/test_source_provenance.py -q -m 'not network' (38 passed)
  • uv run pytest -m 'not network' -q (1569 passed, 4 skipped, 23 deselected)
  • uv run ruff check .
  • uv run ruff format --check .
  • uv run --only-group docs make -C docs html SPHINXOPTS='-W --keep-going'

@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. One function that gives the channel-defining header for both record forms is a good fit for the header-only case in #441, and not adding more mutable per-channel lists to MultiRecord matches the direction in #744. It returns the right header for 100, 041s (041s01), p000878 (3269321_layout) and a physionet:// URI, reads no signal files, and the shared helper avoids parsing the master header twice. The offline suite, Ruff and the strict docs build pass locally.

Two things before the name is public:

  • Name. wfdb.SignalHeader is already the per-signal type (each entry in header.signals), so read_signal_header() returning a whole SingleSegmentHeader is easy to misread. Could it be renamed, for example to read_channel_header(path)? Alternatively, read_signal_headers(path) -> tuple[SignalHeader, ...] (empty when all segments are gaps) would match the type name, at the cost of the source record name and fs.
  • pn_dir. This is a new function, but it accepts the just-deprecated pn_dir, and read_signal_header("100", pn_dir="mitdb/1.0.0") immediately emits the FutureWarning. Could it accept only a path or URI, so the new API never takes pn_dir?

@tompollard tompollard changed the title Add a common header-only signal metadata reader Add a common header-only channel metadata reader Dec 1, 2026
@tompollard

Copy link
Copy Markdown
Member Author

Thanks, both points make sense. I renamed the function to read_channel_header(path) so it is distinct from the per-channel SignalHeader type, and removed pn_dir from its signature. The new API now requires one explicit local path or URI; existing readers retain their legacy pn_dir support. I updated the docs and changelog, added a test that rejects pn_dir, and pushed the changes in 6048925. The focused tests (38 passed), Ruff, and the strict docs build pass.

@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, 6048925 covers both points: the function is now read_channel_header(path), and passing pn_dir raises the usual unexpected-keyword TypeError. The full offline suite, Ruff and the strict docs build pass locally.

One small fix: removing the pn_dir parameter also removed the blank line before Returns in the docstring, so numpydoc renders it inside the path description. The built page shows "...or local .wfdb archive path. Returns ——- SingleSegmentHeader or None ..." as part of the parameter text. The strict build doesn't catch it, but adding the blank line back fixes it. The changelog also still says "returns an immutable signal header"; "channel-defining header" would match the new name.

@tompollard

Copy link
Copy Markdown
Member Author

Fixed the missing blank line before Returns and changed the changelog wording to “channel-defining header” in c439b44. I also added a short note connecting read_channel_header(path) to the existing source_signal_header property on loaded records. I would leave source_channel_header as a possible naming follow-up rather than add a second public property name in this PR. Ruff and the strict docs build pass.

@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, c439b44 fixes both: the Returns section renders properly now and the changelog wording matches. Leaving the property name for a possible follow-up makes sense. The full offline suite, Ruff and the strict docs build pass locally.

One optional wording tweak to the new docs sentence: source_signal_header returns the whole header the read used, not only the selected channels, and for fixed layout it can be a later segment. For example, after read_record("…/041s", start=1500, stop=1600, channels=[1]) it is 041s02 with all 7 signals, while read_channel_header returns 041s01. Moving the sentence to the end of the paragraph and saying something like "On a loaded record, source_signal_header gives the header that read used, which for fixed layout may be a later segment" would avoid it reading as if the following "For variable layout…" sentence described the property.

Ready to merge once CI is green.

@tompollard

Copy link
Copy Markdown
Member Author

Good catch. I moved the sentence to the end of the paragraph and clarified that source_signal_header is the complete header used for that read, which may come from a later fixed-layout segment. read_channel_header(path) still uses the first non-gap segment for fixed layout. Pushed in c1a475b; the strict docs build and Ruff pass.

@tompollard
tompollard merged commit fc3e26d into main Dec 1, 2026
19 checks passed
@tompollard
tompollard deleted the api/read-signal-header branch December 1, 2026 05:55
tompollard added a commit that referenced this pull request Dec 1, 2026
## Summary

Add `MultiRecord.segment_views`, a read-only snapshot of the current
segment names, roles, and frame ranges. Each entry is a
`SegmentView(index, name, start, stop, kind)` with `n_frames` available
as a property. The roles are `layout`, `gap`, and `data`.

This lets callers inspect gap lengths and variable-layout headers
without interpreting `None` entries in `MultiRecord.segments`. A non-gap
segment stays `data` even when no selected channels caused its `Record`
to be loaded. The existing mutable lists and segment objects remain
unchanged. Combined `Record.source_segments` also gains `kind` and
`n_frames` properties, so both return forms expose the same distinction.

This addresses the segment representation and provenance questions in
#746. The separate header-only channel view is in #827.

## Checks

- `uv run pytest tests/test_segment_views.py tests/test_read_record.py
tests/test_source_provenance.py -q -m 'not network'` (79 passed)
- `uv run pytest -m 'not network' -q` (1578 passed, 4 skipped, 23
deselected)
- `uv run ruff check .`
- `uv run ruff format --check .`
- `uv run --only-group docs make -C docs html SPHINXOPTS='-W
--keep-going'`
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