Skip to content

Derive RecordData write declarations from encoded samples - #921

Merged
tompollard merged 2 commits into
mainfrom
fix/recorddata-write-declarations
Dec 4, 2026
Merged

tompollard merged 2 commits into
mainfrom
fix/recorddata-write-declarations

Conversation

@tompollard

@tompollard tompollard commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Summary

Closes #920.

RecordData.write() and write_record(path, RecordData) now derive init_value and checksum from the encoded samples on every write. This corrects source declarations that become stale after frame smoothing or skew alignment. It also makes formats= neutral when the caller repeats the source format.

The shared writer no longer sums samples a second time after it has just calculated the checksum. Derived checksums are written in WFDB's signed 16-bit form. The mutable Record writer retains its existing checksum behavior.

The expected header differences are documented in the changelog: some initial values change to the first written sample, derived checksums use the signed form, and a source header that omitted ADC fields can gain adc_res and adc_zero when the new declarations require them. Signal data bytes do not change.

Comparison against merged main

I compared 684 read/write cases across the sample headers, using physical and digital values, uniform and expanded layout, short windows, full reads of five representative records, and implicit or explicit source formats. Of those, 552 writes succeeded on both versions. 376 headers changed, no .dat bytes changed, and no success/failure outcome changed. Most header changes are short windows whose derived checksums now use the signed form. Full unedited writes of record 100 retain the original signed checksums. The test for a full 03700181 write also pins its data-file SHA-256 to the value from main.

For 03700181 in uniform layout, the source declares initial values [67, -943, -304]; the output now declares [56, -943, -208], matching its first encoded frame after smoothing and skew alignment.

Benchmark

On sample-data/100skew (650,000 frames), I read digital uniform values once, warmed up three writes, then timed 30 writes to fresh temporary directories using signal_alignment="aligned". On macOS 15.7.3 arm64, Python 3.11.13 and NumPy 2.4.6, median write time was 8.89 ms on main and 8.33 ms here. A separate traced write peaked at 33,179,560 and 33,179,760 bytes respectively. Skipping the duplicate checksum sum removes the initial slowdown.

Checks

  • 2,320 offline tests passed; 4 skipped and 23 network tests deselected.
  • Ruff check and the warning-as-error Sphinx build passed.
  • Tests cover full and partial reads, both kinds and layouts, aligned skew, multi-frequency frame smoothing, missing samples, formats= equivalence, and the n8 header without ADC declarations. The two old byte fixtures whose headers intentionally change now pin the new headers while keeping their v5.17.0 data hashes.

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

This does what #920 asked: every header written from RecordData now matches the data written with it. It can merge. I'd still like one change first, to the checksum form, because without it most unedited writes get a different header for no gain. The offline suite (2,319 passed, 4 skipped) and Ruff pass.

What I checked

  • Headers now match their data. For every sample record, both kinds and layouts, full and partial windows, and a select of each, I wrote the result. I then read the raw stored samples back with ignore_skew=True and no frame smoothing, and checked that each channel's header init_value equals its first stored sample and its checksum equals the sum modulo 65536. That's 3,672 channels with 0 mismatches. On main, the same check found 54 stale init_values in 364 channels of unedited writes.
  • Changes against main (983407b). Of 1,836 RecordData.write outcomes (formats None, "16" and "32"), 532 differ:
    • in headers only: no .dat bytes changed, and no write changed between succeeding and failing;
    • 174 header lines get a corrected init_value;
    • 8 gain the adc_res/adc_zero fields that the derived values require;
    • 1,042 header lines change only the form of the checksum, from signed to unsigned. For example, an unedited full read of 100 used to write -22131, as in its source header, and now writes 43405.

Suggested change: write checksums in WFDB's signed form

WFDB's own header writer prints (short int)(siarray[s].cksum & 0xffff) (lib/signal.c:3918), which is the signed 16-bit form. Its reader compares only the low 16 bits, so both forms are accepted. But every header written by WFDB's own tools uses the signed form.

On #908 I accepted the unsigned form as the library's convention. Having now checked WFDB, I think that was the wrong call. Converting the derived checksum to the signed form costs nothing (((c + 32768) % 65536) - 32768), needs no second pass over the samples, and would:

  • match WFDB's output;
  • keep the checksums of unedited full writes, like 100's, the same as in their source headers and in 5.17.0;
  • shrink the header changes in this PR to the 182 that actually correct something.

The legacy Record writer's unsigned checksums for partial records could follow later, for consistency, but that's outside this PR.

@tompollard

Copy link
Copy Markdown
Member Author

Agreed. I changed the derived checksums in RecordData.write() and write_record(path, RecordData) to WFDB's signed 16-bit form. The conversion only touches the per-channel checksum declarations; it does not sum the samples again. The mutable Record writer keeps its existing behavior.

I added a regression test showing that an unedited write of record 100 retains its signed checksums, and updated the header expectations, docs, and changelog. The offline suite now passes (2,320 passed, 4 skipped), as do Ruff and the docs build. In the 684-case comparison against main, 552 writes succeeded on both versions, no .dat files differed, and there were no success/failure changes. I updated the PR description with the new header comparison count.

@tompollard
tompollard merged commit 5b106c8 into main Dec 4, 2026
19 checks passed
@tompollard
tompollard deleted the fix/recorddata-write-declarations branch December 4, 2026 20:15
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.

Regenerate RecordData declarations from written samples

1 participant