Repository navigation
Add validated calibration edits to RecordData - #918
Conversation
96b6898 to
8cfe663
Compare
tompollard
left a comment
There was a problem hiding this comment.
This implements the calibration contract, and the arithmetic is right. One small fix is needed: rescale changes the dtype. Thanks for correcting the rounding rule. _adc_inplace uses np.round, which rounds halves to even, and I had mixed it up with the round-half-up rule in frame smoothing. The offline suite (2,276 passed, 4 skipped) and Ruff pass.
What I checked
rescaleequals converting to physical and back through the ADC. For every sample record with known calibration, in both layouts, I chose a random new gain (0.1× to 2× the current one) and a baseline offset of up to ±5. I comparedwith_calibration(..., mode="rescale")withnp.round(to_physical() * new_gain + new_baseline). All 68 accepted edits match exactly, masks included. The other 18 were rejected when the edit was made, because the rescaled values didn't fit the channel's format.- Round trips. Rescaled data for
100(uniform) andtest01_00s_frame(expanded, with several samples per frame) writes and reads back unchanged, and its physical values stay within one new quantisation step of the originals. Gains of1/3,200.123456789,1e-07and123456789.5all reach the header and read back exactly. - Unknown calibration. The
s25047variable-layout read, whose channels haveadc_gain=Noneafter combining, is rejected forrescalewith a message listing the channels.reinterpretcan still set it.
To fix: rescale changes the dtype
A read with resolution=16 returns int16 values. After with_calibration(..., mode="rescale") they're int64, because the converted arrays are created as int64 and kept. with_values keeps the dtype (int16 stays int16), and the other edits do too. resolution is the caller's choice and shouldn't change through an edit.
Casting back to the original dtype after the range check would fix it. If a rescaled value doesn't fit that dtype, even though it fits the storage format, raise a clear error rather than widen silently. A test with resolution=16 would pin it.
|
Thanks, agreed. I fixed this in I added uniform and expanded |
## Summary - Add paired `adc_gain` and `baseline` options to `RecordData.write` and `write_record(path, data)` for physical `RecordData`. The two entry points use the same write path. - Validate one positive finite gain and one signed 32-bit baseline per channel before creating files. Digital `RecordData` uses `with_calibration`; mutable `Record` keeps its existing fields. - Apply the calibration to a private write copy, leaving loaded values and source metadata unchanged. For flattened reads with ambiguous formats, require `formats=` so the writer cannot silently replace the requested calibration. - Refresh initial values and checksums when calibration changes the integers or a target format recodes missing markers. Repeating the source format leaves an otherwise unchanged write byte-identical. - Fill omitted `adc_res` and `adc_zero` in the outgoing header when derived initial values or checksums require those positional fields. This also repairs partial writes through the legacy `Record.wrsamp` path. - Both paths use the existing format range and missing-marker checks before writing. - Share gain/baseline validation with `RecordData.with_calibration` from #918. This PR is rebased onto main after #918 merged. ## Verification - 2,307 offline tests passed; 4 skipped and 23 network tests deselected. - Ruff check and the warning-as-error Sphinx build passed. - New tests cover both writer forms producing identical bytes, uniform and expanded values, flattened records, mixed formats, old pickles, valid and invalid calibration, marker and range failures before files exist, and checksum refresh after missing-marker recoding. They also cover full and partial `n8_evoked_raw_95_F1_R9` writes, edits, and the legacy partial writer. The broader existing issue of stale `init_value` and `checksum` on otherwise unedited `RecordData` writes is tracked separately in #920. That fix will deliberately change more header bytes and deserves its own comparison and review.
Summary
RecordData.with_calibration(adc_gain=..., baseline=..., mode=...)for digital reads.mode="reinterpret"keeps the stored integers and changes their physical interpretation.mode="rescale"adjusts the integers to preserve physical values as closely as the new calibration permits.This is based on main after #917 merged. The physical write-option change is in the stacked #919 PR.
Rounding detail
The existing ADC implementation uses NumPy rounding, so exact halves go to the even integer. This PR uses that same rule. The proposed “half up” wording does not match the current code.
WFDB's existing writer requires positive gains, so this edit uses positive finite gains rather than allowing negative ones.
Verification