Repository navigation
Add readable read_record API - #781
Conversation
tompollard
left a comment
There was a problem hiding this comment.
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 matchread_signal_values. resolution=16givesint16digital andfloat16physical values, andnp.int64(32)is accepted.channel_namestakes precedence overchannels.combine_segments=Falsewith expanded layout returns aMultiRecord.- Extra positional arguments are rejected.
- The new tests, ruff and the strict docs build pass.
Decide before release: the default layout.
- #775 made
layoutrequired inread_signal_values, so that a multi-rate read "cannot silently choose averaging or full-resolution samples". read_recorddefaults tolayout="uniform", soread_record("sample-data/mixedsignals")silently averages.- Matching
rdrecordis reasonable for a renamed function. But under #672's "rename once" principle, requiringlayoutlater 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
startandstopin_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_recordwith 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.
|
Thanks for checking this. I kept Both new readers now validate I reverted the README quick start to The full offline suite passes (1,079 passed, 4 skipped), as do Ruff and the strict docs build. These changes are in |
## 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.
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 mutableRecordorMultiRecordasrdrecordand delegates tordrecordfor all file and signal I/O.pn_dirremains available for custom index servers, as agreed in #748.layout="uniform"is an intentional default:read_record(path)matches the familiarrdrecord(path)result, including averaging within multi-frequency frames.read_signal_valuesstill requires an explicit layout and keeps its distinctSignalReadreturn type. It now callsread_record, so both new readers share validation and usestartandstopin range errors.rdrecordkeeps its signature and behavior, with no warnings in this first step.Export the function from
wfdbandwfdb.ioand 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
MPLBACKEND=Agg).Related: #672, #745, #748, and follow-up #782.