Skip to content

Write records through private model state - #878

Merged
tompollard merged 1 commit into
mainfrom
refactor/model-write-record
Dec 3, 2026
Merged

tompollard merged 1 commit into
mainfrom
refactor/model-write-record

Conversation

@tompollard

@tompollard tompollard commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Summary

Start the model handoff in #866 with write_record. It now captures the selected values and the current Record fields once at the write boundary, builds private _WriteModel state, and writes through that state. It no longer builds a temporary Record with _prepare_record_for_write or calls Record.wrsamp.

The old write_record implementation is retained temporarily for differential tests. _WriteModel still 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_signals reaches the new path through write_record, but still constructs a temporary Record first. SignalRead.write still calls Record.wrsamp directly.

No public API or file-output change is intended, so there is no changelog entry.

Evidence

  • The differential test now calls the old and model-backed write_record paths. It compares every output file byte or the exception type and message across the sample corpus, including flattened multi-segment writes.
  • Added comparisons for selected skewed channels in both layouts and kinds, plus a short raw-skew read that must fail before writing.
  • Added a test that makes _prepare_record_for_write and Record.wrsamp fail if the new write_record calls either one.
  • Targeted parity, write-record, and skew tests: 451 passed, 44 known unreadable sample cases skipped.
  • Full offline suite: 2,173 passed, 48 skipped, 23 deselected.
  • Ruff and the warning-as-error Sphinx build pass.

The next handoff PR will switch SignalRead.write and remove the temporary Record construction in write_signals. The live differential path remains until the final encoder cleanup, when representative reference bytes from the pinned baseline will replace it.

@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 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. _WriteModel shares the selected arrays and then sets features, defaults and checksums on itself. So I re-ran my mutation check over 155 writes (write_record and SignalRead.write across single and multi-segment sample records, both kinds and layouts). Every array, mask and Record field 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_record takes 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_write and Record.wrsamp are 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" behind

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

@tompollard

Copy link
Copy Markdown
Member Author

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.

@tompollard
tompollard merged commit 2ed6fc0 into main Dec 3, 2026
19 checks passed
@tompollard
tompollard deleted the refactor/model-write-record branch December 3, 2026 06:07
tompollard added a commit that referenced this pull request Dec 3, 2026
## 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.
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.

1 participant