Skip to content

Preserve uncalibrated gains in modern metadata - #944

Open
tompollard wants to merge 1 commit into
fix-format0from
fix-uncalibrated-metadata
Open

tompollard wants to merge 1 commit into
fix-format0from
fix-uncalibrated-metadata

Conversation

@tompollard

@tompollard tompollard commented Dec 6, 2026 •

Copy link
Copy Markdown
Member

Fixes #937. This PR is based on #943; #945 is based on this PR. Its matching spec adapter is wfdb-spec #35, which checks these fields through modern read_header rather than the intentionally unchanged legacy rdheader. After the adapter merges, I will pin a commit reachable from spec main.

Behavior differences from 6.1.0

  • read_header, RecordData.metadata, and SegmentedRecordData.metadata.channels report adc_gain=None when a source signal's gain is omitted or zero. Their source-header views reflect the same distinction. Previously modern views reported 200.
  • These modern views report the specification's format-specific default ADC resolution when it is omitted or zero. Previously an omitted value appeared as None and an explicit zero remained zero.
  • Writing a genuinely uncalibrated RecordData now writes zero gain rather than 200, so a read of the result remains uncalibrated. An explicit physical write calibration overrides this behavior. A combined record whose segments disagree on gain is not treated as uncalibrated and retains its prior write behavior.

Physical conversion still uses an effective gain of 200. Record, MultiRecord, rdheader, rdrecord, and rdsamp keep their existing gain and ADC-resolution values. The private parser records whether the source gain was uncalibrated while retaining the legacy effective gain. The separate pre-existing physical-write failure is tracked in #946.

The header-partial-signal-fields and header-signal-defaults known gaps are removed. Verification on the stacked tip: pinned WFDB spec suite (35 passed), parallel offline suite (2,507 passed, 4 skipped), Ruff, and the warnings-as-errors Sphinx docs build.

@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 behaviour matches the scope agreed on #937. Apart from the specification pin (see #943), this can merge. I have one small point about combined reads, and found an older bug while testing.

What I checked

  • On the specification's records, the modern read_header gives (None, 12) for sigdefaults, and (None, 12), (50.0, 12), (50.0, 12), (50.0, 8) for partial. Legacy rdheader is unchanged: (200.0, None) and (200.0, None), (50.0, 0), (50.0, None), (50.0, 8).
  • Physical values of the uncalibrated signal still use a gain of 200.
  • Writing the uncalibrated digital read gives 16 0.0(0)/mV …, and it reads back as uncalibrated.
  • Against main (0c1a867), no signal values change, and the legacy Record values are the same. The other differences are in metadata, as intended:
    • combined multi-segment metadata reports adc_res=12 instead of None;
    • source headers with adc_res=0 report 12;
    • the private parsed header gains an uncalibrated field.

Point: "uncalibrated" and "segments disagree" are treated the same when writing

A combined variable-layout read has adc_gain=None when its segments have different gains. That isn't the same as an uncalibrated source. Writing such a read (s25047-2704-05-04-10-44, frames 0–5000, physical) now writes 0.0(1)/NU, where main wrote 1(1)/NU. In every window where I could test this, the values were all missing, so no data changes. Even so, it would be clearer to write a zero gain only when the source signal is actually uncalibrated, which is what the new parser flag records.

Already on main, for a separate issue

Physically writing a combined variable-layout read whose segments have different gains fails on both main and this branch, whenever the window contains values. Examples are s25047, frames 25000–55000, and s00001-2896-10-10-00-31, frames 2225802–2225810. The error is "adc_gain and baseline must be provided together": the combined metadata has no gain but keeps a baseline.

@tompollard
tompollard force-pushed the fix-uncalibrated-metadata branch from 235590c to 40dd5ca Compare December 6, 2026 16:10
@tompollard
tompollard changed the base branch from main to fix-format0 December 6, 2026 16:10
@tompollard

Copy link
Copy Markdown
Member Author

Thanks for catching the distinction between an uncalibrated source and a combined result whose segments disagree. I fixed it in the updated #944 branch: the writer now uses the parsed source's uncalibrated flag, rather than treating every metadata.adc_gain=None as uncalibrated. A new regression test reads s25047 frames 0–5000 and checks that writing still produces 1(1)/NU; the true uncalibrated round trip still writes zero gain.

The separate physical-write failure is tracked in #946. This PR remains focused on #937. The stacked tip passes the pinned spec tests, Ruff, the warnings-as-errors docs build, and the parallel offline suite (2,507 passed, 4 skipped). The spec pin will move to a reachable main commit after the adapter PRs it needs merge.

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.

Report uncalibrated signals and default ADC resolutions as the specification defines them

1 participant