Repository navigation
Rescale combined variable-layout digital reads to layout calibration - #945
tompollard wants to merge 1 commit into
Conversation
tompollard
left a comment
There was a problem hiding this comment.
The rescaling matches WFDB C exactly where it runs. But the range check rejects real PhysioNet windows that WFDB C reads. The specification pin also needs the same change as #943 and #944.
What I checked
- The rescaling. I took 84 digital reads of
s00001-2896-10-09-01-56,s00001-2896-10-10-00-31ands25047-2704-05-04-10-44, plus the specification's two variable-layout records, in both layouts, using windows across segment boundaries and random windows. For each, I rescaled the raw samples fromread_segmentsindependently withfloor(x * scale + layout_baseline - scale * segment_baseline + 0.5). All 84 matchread_record(..., kind="digital"), masks included. The metadata reports the layout gains. - Nothing else changes. Against
main(0c1a867), every difference is a digital read: 48 reads that raised before now succeed, and 226 that already succeeded now return the layout calibration. Physical reads are unchanged. - The legacy error.
rdrecord(..., physical=False)raisesValueError: "Cannot combine … channel 'MCL1' has different adc_gain across segments (26.0 and 66.0); use read_record(…)".
To fix: the range check uses the segment's storage format
Four of my windows fail, for example s00001-2896-10-10-00-31, frames 10005–10017:
ValueError: Rescaled channel 'II' in segment '3975656_0001' does not fit format 80 and resolution 64 without using its missing-sample marker
That segment stores II in format 80 with gain 1.0, while the layout gain is 83. Rescaled samples are therefore around −2,900, which can't be stored in format 80. But the combined view isn't stored in format 80. It's a new view at the layout calibration, and WFDB C's sigmap returns these values. The check should be against the output's integer type only, the requested resolution. Missing samples are already carried by the mask, so a value equal to a format's marker isn't a problem in the combined view. A later write chooses its own format and checks the range then. Please add one of these windows as a test. s25047-2704-05-04-10-44, frames 159147–159647, fails the same way.
The specification pin and order
This PR pins 697cd4f, the head of wfdb/wfdb-spec#36, while #943 and #944 pin #34 and #35. I suggest:
- Merge wfdb-spec #34 and #35, and #36 after one change: read multi-segment samples with
layout="expanded". The specification's samples are at each signal's own frequency, solayout="uniform"would average multi-frequency signals. The current multi-segment cases all have one sample per frame, so it passes today, but it wouldn't for a future case. - Pin the resulting wfdb-spec
maincommit in each of #943, #944 and #945. The first to merge sets it, and the other two rebase.
Until a release includes these fixes, the specification's weekly report will show WFDB Python 6.1.0 as failing a few cases through the new adapter, rather than as unsupported. That clears with the next release.
229cce9 to
e03cf0a
Compare
|
Thanks, you're right: the combined digital view is not stored in a segment's format. I removed that format and marker check. Rescaling now checks only that valid values fit the requested integer resolution; the mask continues to identify missing samples. I added tests for the I also changed wfdb/wfdb-spec#36 to use |
Closes #933.
Behavior changes from 6.1.0
read_record(..., kind="digital")now combines variable-layout segments with differing calibrations. Each segment's valid samples are mapped to the layout header's gain and baseline with WFDB C'sfloor(sample * scale + layout_baseline - scale * segment_baseline + 0.5)rule. Exact halves round up. The selected metadata and legacy projection use the layout calibration, including when a selected window contains only one segment; storage format remains unknown where it cannot be inferred.read_segmentsstill returns each segment in its original calibration.scale < 1emits a precision-loss warning. The combined view is independent of each segment's storage format: values may exceed that format's range or equal its missing marker while remaining valid under the mask. A value outside the requested integer resolution raisesValueError; writing later checks the chosen output format.to_physical()on the combined digital result now uses the layout calibration even where the layout has no storage format. Its values match a direct physical read within one layout quantization step.rdrecord(..., physical=False, m2s=True)still refuses conflicting segment fields, but its bareExceptionbecomes aValueErrornaming the channel, field, conflicting values and modern reader. Other legacy reads are unchanged.The known gap
multi-segment-variable-roundingis removed. The conformance test pins wfdb-spec #36, which reads expanded digital samples directly. The spec adapter PRs are stacked as #34 → #35 → #36; the pin will move to a reachable specmaincommit after those merge.Verification
-n auto -m "not network") on the stacked tip.rdsampdifferential test: passed locally on thes00001-2896-10-10-00-31boundary window; added to the differential CI job.