Repository navigation
Write records through private model state - #878
Conversation
tompollard
left a comment
There was a problem hiding this comment.
This handoff is correct, and it can merge. The offline suite (2,173 passed, 48 skipped), Ruff and all 19 CI checks pass.
What I checked
- Writes don't change the caller's data.
_WriteModelshares the selected arrays and then sets features, defaults and checksums on itself. So I re-ran my mutation check over 155 writes (write_recordandSignalRead.writeacross single and multi-segment sample records, both kinds and layouts). Every array, mask andRecordfield was unchanged, with the same 18 expected write errors as before this PR. - Performance is unchanged. I timed writes of
100skew(650,000 frames, 15 runs each, write only), comparing 5f3666e with the stack tip. This is the #866 benchmark condition that the description doesn't report.write_recordtakes 10.19 → 10.55 ms for digital (IQR 10.37–10.67) and 16.12 → 16.38 ms for physical. Peak traced memory is identical (21.73 and 31.64 MiB). That's well inside the 10% target. - The parity test now compares the whole old and new
write_record, including the skewed-channel cases and the short raw read that must fail. The guard that_prepare_record_for_writeandRecord.wrsampare no longer called makes the handoff explicit.
A bug on main that the model should fix (follow-up)
write_record doesn't keep the "validate before creating files" rule that SignalRead.write and write_signals now follow. On main, and on this branch:
rec = wfdb.read_record("sample-data/100", kind="digital", layout="uniform", stop=5)
rec.d_signal[0, 0] = 5000 # outside format 212
wfdb.write_record(path, rec) # IndexError: Channel 0 contain values outside allowed range ...
# leaves path + ".hea" behindThe same happens for an out-of-range physical value. The header is written before wr_dats checks the samples. Now that write_record goes through _WriteModel, the fix fits naturally there: validate the digital values for range and markers before _write_files, using the same helpers as the other two writers. That would remove the partial header and the IndexError, and give all three writers the same errors. It changes behaviour, so it belongs in its own PR with a changelog entry rather than in this no-change step.
Note
As the description says, _WriteModel still inherits the Record encoder classes and binds Record.check_field and other methods onto itself. That's the same shape as the #863 prototype. Fine for this step, as long as the final #866 cleanup removes it.
|
Thanks for checking the mutation cases and the benchmark. The partial-header failure is real, and I filed it as #880 with the reproduction and the proposed preflight checks. I’ll keep that behavior change and its changelog entry in a separate PR, so #878 remains an encoder handoff with no intended output change. |
## Summary Continue #866 after #878. `SignalRead.write` now sends its selected values and current metadata to private `_WriteModel` state instead of calling `Record.wrsamp`. `write_signals` builds that model directly from validated arrays and channel fields instead of constructing a temporary `Record`. These changes keep the existing missing-sample, calibration, format, skew, and destination rules. This PR is stacked on #878 and should be merged after it. The previous two functions remain temporarily as `_legacy_write` and `_legacy_write_signals` for the differential tests; they can be removed with the other legacy paths at the end of #866. No public API or file-output change is intended, so there is no changelog entry. ## Checks - The differential test switches both public routes to their retained legacy implementations and compares every output file byte or error outcome. New cases cover explicit and inferred physical calibration with NaN, masked digital gaps in formats 16 and 212, and selected skewed `SignalRead` views in both layouts and kinds. - Tests verify that `SignalRead.write` no longer calls `Record.wrsamp` and `write_signals` no longer constructs a `Record`. The existing preparation test now checks memory sharing at the model boundary. - Targeted parity, signal-write, and preparation tests: 403 passed, 44 known unreadable sample cases skipped. - Full offline suite: 2,182 passed, 48 skipped, 23 deselected. - Ruff and the warning-as-error Sphinx build pass. The private model still inherits the legacy encoder mixins and projects fields into parallel lists. The final #866 cleanup will remove those dependencies after reference bytes from the pinned baseline are in place.
Summary
Start the model handoff in #866 with
write_record. It now captures the selected values and the currentRecordfields once at the write boundary, builds private_WriteModelstate, and writes through that state. It no longer builds a temporaryRecordwith_prepare_record_for_writeor callsRecord.wrsamp.The old
write_recordimplementation is retained temporarily for differential tests._WriteModelstill uses the legacy header and signal mixins and projects channel fields into their parallel lists. Removing that inheritance and the temporary lists is the later cleanup step in #866.write_signalsreaches the new path throughwrite_record, but still constructs a temporaryRecordfirst.SignalRead.writestill callsRecord.wrsampdirectly.No public API or file-output change is intended, so there is no changelog entry.
Evidence
write_recordpaths. It compares every output file byte or the exception type and message across the sample corpus, including flattened multi-segment writes._prepare_record_for_writeandRecord.wrsampfail if the newwrite_recordcalls either one.The next handoff PR will switch
SignalRead.writeand remove the temporaryRecordconstruction inwrite_signals. The live differential path remains until the final encoder cleanup, when representative reference bytes from the pinned baseline will replace it.