Skip to content

Add readable read_record API - #781

Merged
tompollard merged 2 commits into
mainfrom
read-record-api
Nov 30, 2026
Merged

tompollard merged 2 commits into
mainfrom
read-record-api

Conversation

@tompollard

@tompollard tompollard commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Summary

Add read_record(path, *, start, stop, kind, layout, combine_segments, resolution, ...) as an opt-in, readable entry point for the existing record reader. It returns the same mutable Record or MultiRecord as rdrecord and delegates to rdrecord for all file and signal I/O. pn_dir remains available for custom index servers, as agreed in #748.

layout="uniform" is an intentional default: read_record(path) matches the familiar rdrecord(path) result, including averaging within multi-frequency frames. read_signal_values still requires an explicit layout and keeps its distinct SignalRead return type. It now calls read_record, so both new readers share validation and use start and stop in range errors. rdrecord keeps its signature and behavior, with no warnings in this first step.

Export the function from wfdb and wfdb.io and document it in the I/O guide. The README quick start remains on the existing names until a matching annotation reader is available. This is the first focused part of #672; annotation, write, and attribute names can follow separately.

Verification

  • Tests cover selected frames and channels, digital expanded values and resolution, combined and preserved multi-segment reads, a local archive, a cloud URI, every argument's forwarding, and range errors from both new readers.
  • Full offline suite: 1,079 passed, 4 skipped, 22 network tests deselected (MPLBACKEND=Agg).
  • Ruff check and format pass; Sphinx HTML documentation builds with warnings treated as errors.

Related: #672, #745, #748, and follow-up #782.

@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 looks correct, and the names match #672 (read_record, start/stop, combine_segments, resolution). There's one design decision to settle before release, plus a few smaller suggestions.

Checked locally:

  • Digital expanded reads match rdrecord, and uniform digital reads match read_signal_values.
  • resolution=16 gives int16 digital and float16 physical values, and np.int64(32) is accepted.
  • channel_names takes precedence over channels.
  • combine_segments=False with expanded layout returns a MultiRecord.
  • Extra positional arguments are rejected.
  • The new tests, ruff and the strict docs build pass.

Decide before release: the default layout.

  • #775 made layout required in read_signal_values, so that a multi-rate read "cannot silently choose averaging or full-resolution samples".
  • read_record defaults to layout="uniform", so read_record("sample-data/mixedsignals") silently averages.
  • Matching rdrecord is reasonable for a renamed function. But under #672's "rename once" principle, requiring layout later would break code that uses the new name.

Keeping the default seems right to me, since this is the familiar reader. It would help to say in the PR that it's intentional.

Errors use the legacy argument names. read_record("sample-data/100", start=10, stop=5) raises "sampto must be greater than sampfrom", from rdrecord's checks (record.py:663-679). That names arguments the caller didn't use, and read_signal_values has the same problem.

  • A fix: check start and stop in _check_read_signal_options, with messages that use the new names.
  • This is likely the first error anyone learning the new names will hit.

Tests for argument forwarding. The tests compare results against rdrecord, which is a good check, but nothing tests whether ignore_skew, force_channels, channel_names, warn_empty and pn_dir are passed through. One test could cover the whole mapping:

  • monkeypatch wfdb.io.record.rdrecord;
  • call read_record with every argument set to a non-default value;
  • check the keyword arguments that come through.

README. The quick start now has read_record(..., stop=3600) next to rdann(..., sampto=3600) (README.md:34-35), so a new user sees two names for the same idea on consecutive lines. Either keep the README on rdrecord until the annotation names land, or add a short note that they're coming.

Older problem, not for this PR: kind="digital", resolution=8 on a 12-bit record (such as sample-data/100) raises a bare Exception("Cannot convert digital samples to lower dtype. Risk of overflow/underflow."). A ValueError would be easier to catch.

@tompollard

Copy link
Copy Markdown
Member Author

Thanks for checking this. I kept layout="uniform" intentionally so read_record(path) matches rdrecord(path); the PR description and docs now say that explicitly. read_signal_values still requires an explicit layout.

Both new readers now validate start and stop before opening the source and translate remaining range errors from rdrecord, including out-of-bounds errors, to use the new argument names. I added a test that checks every keyword is forwarded to its matching rdrecord option.

I reverted the README quick start to rdrecord so it stays consistent with rdann until the annotation alias is available. I also reproduced the bare Exception for an undersized digital resolution and opened #782 to track that separately.

The full offline suite passes (1,079 passed, 4 skipped), as do Ruff and the strict docs build. These changes are in 3a329e7.

@tompollard
tompollard merged commit a6adb88 into main Nov 30, 2026
19 checks passed
@tompollard
tompollard deleted the read-record-api branch November 30, 2026 05:33
tompollard added a commit that referenced this pull request Nov 30, 2026
## Summary

Add `read_annotations` as the annotation counterpart to `read_record`
from #781. The path difference is intentional: `read_record("100")`
takes a record stem, while `read_annotations("100.atr")` takes an
annotation filename because an annotation is one file. Full filenames
support local, cloud, and `physionet://` paths. A record stem or local
`.wfdb` archive can be used with `annotator="atr"`; the annotator is
required for an archive because it may contain multiple annotation
files.

The wrapper maps `start`, `stop`, `relative_samples`, and `label_fields`
to the existing `rdann` options and returns the same mutable
`Annotation`. It shares range validation with the new record readers. It
rejects common non-annotation suffixes when inferring an annotator,
conflicting filename/annotator combinations, and malformed annotator or
label options. `rdann` keeps its signature and behavior, with no
warnings. The README quick start now uses `read_record` and
`read_annotations` together.

## Verification

- Tests cover filename and stem reads, archive and `memory://` paths,
selections, label options, argument forwarding, non-annotation suffixes,
and invalid arguments.
- Offline suite: 1,090 passed, 4 skipped, 22 network tests deselected.
- Ruff check and format pass; Sphinx HTML documentation builds with
warnings treated as errors.

Related: #672 and #534.
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