Skip to content

Write signal views and arrays through private model state - #879

Merged
tompollard merged 2 commits into
mainfrom
refactor/model-signal-writers
Dec 3, 2026
Merged

tompollard merged 2 commits into
mainfrom
refactor/model-signal-writers

Conversation

@tompollard

Copy link
Copy Markdown
Member

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.

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

The behaviour is right, and it's ready after #878, with two structural points. The offline suite (2,182 passed, 48 skipped) and Ruff pass locally. CI hasn't run on this PR, which targets refactor/model-write-record rather than main. Please retarget it to main after #878 merges and let the full matrix run before merging.

What I checked

  • Writes don't change inputs. I ran 1,464 SignalRead.write calls across the sample records (both kinds and layouts, converted views, both alignments, folder, archive and formats="32" targets) and 155 mixed write_record/SignalRead.write calls. No array, mask or retained-record field changed, and the error counts (232 and 18) match the code before this stack.
  • Performance is unchanged or better. On the same 100skew benchmark (15 runs, write only), SignalRead.write takes 8.00 → 8.08 ms for digital and 16.90 → 17.05 ms for physical. write_signals with physical values and calculated calibration goes from 35.15 to 32.71 ms. Peak memory is identical.
  • The parity cases for explicit and calculated calibration with NaN, masked gaps in formats 16 and 212, and skewed views cover the paths this PR changes.

1. The validation preamble is duplicated

The new write_signals repeats about 100 lines of argument checks from _legacy_write_signals: layout, formats, names, units, fs, dates, comments, calibration and the missing-value rules. The new SignalRead.write likewise repeats the path, format and file-name handling from _legacy_write. Those checks aren't what this refactor is changing, so a shared _validate_write_signals_arguments(...) (and a shared path/format helper for SignalRead) used by both versions would keep the comparison meaningful for the encoding route. It would also mean a fix to the checks before the cleanup can't land in only one copy.

2. Avoid the after_features callback

_WriteModel.write(after_features=...) exists so write_signals can check physical values once calibration has been calculated. A callback into the middle of the write is hard to follow, and it gives only one of three writers that check. An alternative is for _WriteModel.write to validate the digital values itself, for range and the reserved marker, after features are derived and before _write_files, for every writer. That replaces the callback. It also covers the write_record bug I noted on #878, which leaves a .hea behind and raises IndexError for out-of-range samples. That part changes write_record's behaviour, so it could be a follow-up PR. If so, a # TODO(#866) on after_features would mark it as temporary.

@tompollard

Copy link
Copy Markdown
Member Author

Thanks. I pushed 44561ac to address the duplicated preambles. The legacy and model write_signals paths now share one argument validator, and both SignalRead.write paths share destination, format, and source preparation. The byte parity tests still compare the separate encoding routes; the focused tests pass (441 passed, 44 skipped), as do the full offline suite (2,182 passed, 48 skipped), Ruff, and formatting.

I kept after_features for this no-behavior-change handoff and marked it as temporary with TODO(#866). The shared preflight validation that can replace it, including the partial-header fix for write_record, is tracked in #880 and will get its own PR and changelog entry.

I’ll retarget #879 to main after #878 merges, then wait for the full CI matrix before merging #879.

Base automatically changed from refactor/model-write-record to main December 3, 2026 06:07
@tompollard
tompollard force-pushed the refactor/model-signal-writers branch from 44561ac to 4bf6a8f Compare December 3, 2026 06:08
@tompollard
tompollard merged commit f6e4a36 into main Dec 3, 2026
19 checks passed
@tompollard
tompollard deleted the refactor/model-signal-writers branch December 3, 2026 06:14
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