Repository navigation
Add opt-in explicit signal values reader - #772
Conversation
tompollard
left a comment
There was a problem hiding this comment.
Reading works: all four representations match rdrecord, and conversions leave the original values unchanged. Before merging, I'd fix three API issues, since names and behaviour are hard to change after a release. Follow-ups are listed further down.
Checked:
tests/test_signal_values.py(10 passed) and ruff pass.- In-place edits to
read.signals.valuescarry through toto_physical()andto_legacy_record(). - Editing the record from
to_legacy_record()leavesread.metadataunchanged.
Should fix before merging:
- Converting a variable-layout read to digital fails unhelpfully.
wfdb.read_signal_values("sample-data/multi-segment/p000878/p000878-2137-10-26-16-57", stop=7500).to_digital()raisesKeyError: None. That record's channels have no single format or calibration across segments, so the conversion can't work, but the error doesn't say so. Checking before converting that each channel hasadc_gain,baselineandfmt, and raising aValueErrorthat names the channels without them, would explain it. dtypetakes a number of bits, not a dtype.dtype=32works, butdtype=np.float32anddtype="float32"both raisereturn_res must be one of the following: 64, 32, 16, 8, which refers to a parameter the caller didn't use. Either accept NumPy dtypes (a floating dtype for physical values, an integer dtype for digital ones), or name the parameterresolution. Both options are listed in #672.to_physical(return_res=...)should then use the same name.SignalReadcan be built without its hidden record._recordis a dataclass field with a default, sowfdb.SignalRead(signals, metadata)is accepted, butto_digital()then raises'NoneType' object has no attribute 'adc', andto_legacy_record()returnsNone. Either exclude_recordfrom__init__and build results only through the reader, or makeSignalReadself-contained: conversions could use the calibration inmetadata, and the legacy record could be built from the result's own fields.
Follow-ups:
- A conversion shares lists with the original's record.
_converted_readusescopy.copy(read._record), so a converted read's record shares the original'ssig_name,units,adc_gain,baseline,fmtandcommentslists. Changingadc_gain[0]on a converted read's record also changed the original's record, while the original'smetadatastill reported the old gain, and later conversions of the original used the new one. Nothing public changes these lists today, sinceto_legacy_recordmakes a deep copy, but copying the lists in_converted_readwould prevent this. It's also an example ofmetadataand_recordgetting out of step. One source of truth for calibration would avoid that. - Mark the new APIs as provisional.
read_header,Record.metadata,read_multi_segment_headersand nowread_signal_values, with its three classes, have all arrived while the naming in #672 is still open. Marking them as provisional in the docs would allow changes without a deprecation cycle. - More tests: a conversion that can't be done (point 1), the
dtypeargument (point 2), and a check that a conversion doesn't share lists with the original. - Memory:
to_legacy_recordcopies all the values, about 10 MB for the whole of record 100. It would be worth mentioning in its docstring.
|
Thanks for the careful review. I pushed aab176b with the three changes you identified:
I also copied the mutable record lists when making a converted view, marked this new API as provisional in the docs while #672 is open, and documented that |
tompollard
left a comment
There was a problem hiding this comment.
Thanks, aab176b addresses all three points and the follow-ups. Looks good to merge.
Checked:
- The variable-layout conversion now fails with a clear message:
Cannot convert signal values; missing channel fields: 2 (V): fmt, adc_gain, baseline; 3 (ABP): fmt, adc_gain, baseline. resolutionis used in both places.np.float32, and8for physical values, are rejected with errors that name it.SignalRead(...)raises a clearTypeError. Pickling andcopy.deepcopystill work, and the copies can still be converted.- A converted read no longer shares lists with the original's record.
- Tests:
tests/test_signal_values.py(13 passed) and ruff pass.
Minor, not blocking:
dataclasses.replace(read, ...)now raises the sameTypeError, which is consistent, but worth a mention in the docstring since it's a common way to adjust frozen dataclasses.dataclasses.asdict(read)includes_recordand recursively copies the wholeRecord, including its arrays. Storing_recordas a plain attribute set in_create, rather than as a dataclass field, would keep it out ofasdict,fields()and comparisons.
|
Thanks for checking again. I addressed both minor points in a16ac5d. The conversion record is now a plain internal attribute rather than a dataclass field, so I added regression tests for introspection, |
|
Good catch. I fixed this in 01c12e5. Both methods now return a distinct result with independent signal arrays when the values already have the requested kind. I added tests for physical and digital values in both uniform and expanded layouts, array independence, changing physical resolution, and rejecting an invalid resolution. The full offline suite passes (1,025 passed, 4 skipped), as do Ruff and the strict docs build. |
## Summary Require `layout` as a keyword argument to `read_signal_values`. Callers choose `layout="uniform"` to average multi-frequency channels to one value per base frame, or `layout="expanded"` to retain every channel sample. Omitting it now raises a `TypeError` before any file is read. This is a small follow-up to #772 while the opt-in API is still unreleased. It addresses the default-layout concern in #745 and #376 without changing `rdrecord`, `rdsamp`, or the WFDB file reader. The docs explain the choice, and all examples and tests now pass a layout. A regression test covers the omitted argument. ## Verification - Offline suite: 1,032 passed, 4 skipped, 22 network tests deselected (`MPLBACKEND=Agg`). - Ruff check and format pass. - Sphinx HTML build passes with warnings treated as errors.
Summary
Add
read_signal_valuesas an opt-in reader with a singlesignalsfield. The field isUniformSignalsfor a two-dimensional array with one value per channel per base frame, orExpandedSignalsfor one array per channel that preserves every sample. Both state whether values are physical or digital. The result also includes a snapshot of the selected record metadata and source segment references for flattened multi-segment reads.to_physicalandto_digitalreturn another result without changing the original.to_legacy_recordreturns an independentRecordfor the existing writer. The new reader delegates file parsing tordrecord, so plain WFDB files,.wfdbarchives, and the current legacy APIs retain their behavior.This addresses the representation choice in #745. I used
read_signal_valuesas a distinct opt-in name so the broaderread_record/read_signalsnaming discussion in #672 remains open.Verification
.wfdbarchive write/readback.MPLBACKEND=Agg).