Repository navigation
Derive RecordData write declarations from encoded samples - #921
Conversation
tompollard
left a comment
There was a problem hiding this comment.
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
selectof each, I wrote the result. I then read the raw stored samples back withignore_skew=Trueand no frame smoothing, and checked that each channel's headerinit_valueequals its first stored sample and itschecksumequals the sum modulo 65536. That's 3,672 channels with 0 mismatches. Onmain, the same check found 54 staleinit_values in 364 channels of unedited writes. - Changes against
main(983407b). Of 1,836RecordData.writeoutcomes (formatsNone,"16"and"32"), 532 differ:- in headers only: no
.datbytes changed, and no write changed between succeeding and failing; - 174 header lines get a corrected
init_value; - 8 gain the
adc_res/adc_zerofields 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
100used to write-22131, as in its source header, and now writes43405.
- in headers only: no
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.
|
Agreed. I changed the derived checksums in I added a regression test showing that an unedited write of record |
Summary
Closes #920.
RecordData.write()andwrite_record(path, RecordData)now deriveinit_valueandchecksumfrom the encoded samples on every write. This corrects source declarations that become stale after frame smoothing or skew alignment. It also makesformats=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
Recordwriter 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_resandadc_zerowhen 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
.datbytes 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 record100retain the original signed checksums. The test for a full03700181write also pins its data-file SHA-256 to the value from main.For
03700181in 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 usingsignal_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
formats=equivalence, and then8header 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.