Skip to content

Rescale combined variable-layout digital reads to layout calibration - #945

Open
tompollard wants to merge 1 commit into
fix-uncalibrated-metadatafrom
fix-variable-digital
Open

tompollard wants to merge 1 commit into
fix-uncalibrated-metadatafrom
fix-variable-digital

Conversation

@tompollard

@tompollard tompollard commented Dec 6, 2026 •

Copy link
Copy Markdown
Member

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's floor(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.
  • Digital missing samples and gap segments remain masked. read_segments still returns each segment in its original calibration.
  • Rescaling with scale < 1 emits 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 raises ValueError; 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.
  • Legacy rdrecord(..., physical=False, m2s=True) still refuses conflicting segment fields, but its bare Exception becomes a ValueError naming the channel, field, conflicting values and modern reader. Other legacy reads are unchanged.

The known gap multi-segment-variable-rounding is 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 spec main commit after those merge.

Verification

  • Pinned WFDB specification: 35 passed.
  • Offline suite: 2,514 passed, 5 skipped (-n auto -m "not network") on the stacked tip.
  • Ruff check and format; Sphinx warnings-as-errors build: passed.
  • WFDB C 10.7 rdsamp differential test: passed locally on the s00001-2896-10-10-00-31 boundary window; added to the differential CI job.

@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.

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-31 and s25047-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 from read_segments independently with floor(x * scale + layout_baseline - scale * segment_baseline + 0.5). All 84 match read_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) raises ValueError: "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:

  1. 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, so layout="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.
  2. Pin the resulting wfdb-spec main commit 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.

@tompollard
tompollard force-pushed the fix-variable-digital branch from 229cce9 to e03cf0a Compare December 6, 2026 16:10
@tompollard
tompollard changed the base branch from main to fix-uncalibrated-metadata December 6, 2026 16:10
@tompollard

Copy link
Copy Markdown
Member Author

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 s00001 window at frames 10005–10017, a valid unmasked value equal to a source-format marker, and a true resolution overflow. The local C rdsamp comparison still passes.

I also changed wfdb/wfdb-spec#36 to use layout="expanded", so it reports every sample of a multi-frequency channel. The three adapter PRs and the three remaining Python PRs are now stacked. I checked #943 against the final adapter: multi-segment-variable errors until #933 is present, so the adapters cannot all merge before the earlier Python PRs without changing their expected conformance results. I've noted that sequence on #943. This stacked tip passes 35 spec cases, 2,514 offline tests, Ruff, and the warnings-as-errors docs build.

This branch has not been deployed

No deployments
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.

Read variable-layout records as digital values by rescaling segments to the layout calibration

1 participant