Skip to content

Write model headers from explicit fields - #895

Merged
tompollard merged 1 commit into
mainfrom
refactor/explicit-header-writing
Dec 3, 2026
Merged

tompollard merged 1 commit into
mainfrom
refactor/explicit-header-writing

Conversation

@tompollard

Copy link
Copy Markdown
Member

Summary

Write _WriteModel headers from its current record fields and a captured channel snapshot. The new path validates fields, checks shared-file cohesion, serializes lines, and writes the header without calling inherited HeaderMixin methods. The legacy Record header method remains in place, and .dat writing is unchanged.

This is the header step of #866. No public behavior or file output change is intended.

Checks

  • 2,293 offline tests passed; 48 skipped and 23 network tests deselected.
  • Writer byte fixtures, header error parity, and warning caller tests passed (416 passed, 44 expected skips in the focused run).
  • A new test makes inherited header writer methods fail if the modern path calls them. It fails on pre-change main and passes here.
  • Docs built with warnings treated as errors; Ruff and git diff --check passed.

No changelog entry because this changes only the internal writer path.

@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 header step is correct, and it can merge. I have one follow-up about the duplicate it leaves on the Record side. The focused header, fixture, parity and warning tests pass (416 passed, 44 expected skips), as do the offline suite (2,293 passed, 48 skipped) and Ruff.

What I checked

  • The comparison test still compares old and new header code. Since the model no longer calls the inherited header methods, I checked what the legacy side runs. LEGACY_WRITE_PATCHES swaps the public writers for their _legacy_* versions and _write_prepared_files for _legacy_write_prepared_files, so the legacy side never reaches _write_model_header. Every modern write is therefore still compared against the old Record header path.

  • Output compared directly with main (9a79f42). Every modern write now goes through new header code, so I also compared bytes and errors across versions:

    • every readable sample header, both kinds and layouts, through write_record and SignalRead.write;
    • 3,000 random sparse records through write_record, with optional names, units, formats, calibration, adc_res, comments, counter frequency, base counter and shared file names;
    • 15 invalid header fields through write_record, covering fs, sig_name, units, fmt, adc_gain, adc_res, baseline, counter_freq, base_counter, comments, file_name, byte_offset and block_size;
    • the 480 flattened multi-segment writes from my #891 check, across all four writers.

    Every outcome matches main exactly, file names and bytes included. Every invalid case still fails with the same message and leaves no files.

  • The new test fails on main. On main, the model inherits these HeaderMixin methods rather than binding them in its class body, so the patches do reach it. I copied the test into a main checkout, and it fails there.

Follow-up: the Record header path is now a second copy

HeaderMixin._write_checked_header (from #875) and the new _header._write_checked_single_header now implement the same sequence twice: select fields, validate, check cohesion, serialize. _check_signal_snapshot_cohesion likewise repeats the snapshot branch of HeaderMixin.check_field_cohesion, including the "must have the same fmt" check. Record.wrsamp still uses the mixin versions, and it's public. As with #888/#889, having the mixin method call _write_checked_single_header with getattr-built fields, and check_field_cohesion call the new function in its snapshot branch, would leave one implementation. That fits naturally in the cleanup PR, before the legacy comparison paths are removed, while the comparison can still confirm the Record side.

@tompollard

Copy link
Copy Markdown
Member Author

Thanks, I agree. I kept Record's current header path separate in this PR so the old and new implementations remain independently comparable. In the #866 cleanup PR, I'll route HeaderMixin._write_checked_header through the explicit function and use _check_signal_snapshot_cohesion for its snapshot branch, while keeping the public Record methods and their behavior. The _legacy_* comparison path can remain independent until its tests are replaced by the fixed byte fixtures. No change is needed in #895 for this follow-up.

@tompollard
tompollard merged commit fa5f324 into main Dec 3, 2026
19 checks passed
@tompollard
tompollard deleted the refactor/explicit-header-writing branch December 3, 2026 18:17
tompollard added a commit that referenced this pull request Dec 3, 2026
## Summary

Consolidate the duplicate header and signal validation left by #895 and
#896. `Record._write_checked_header` now calls the explicit header
function, its snapshot cohesion check calls the shared helper, and
`SignalMixin.check_sig_cohesion` uses the explicit shape and range
checker while keeping its direct-call checksum and initial-value checks.
`_WriteModel` no longer inherits `BaseRecord`, `HeaderMixin`, or
`SignalMixin`.

This is the first #866 cleanup step. The `_legacy_*` paths and parity
test remain independent for this review. Removing them needs a separate
checklist that maps each parity case to a fixed byte fixture or test.
The model still projects channel fields to lists while preparing a
write; that remaining metadata representation work is separate from
inheritance removal.

## Checks

- 2,294 offline tests passed; 48 skipped and 23 network tests
deselected.
- Writer byte fixtures, old-versus-new parity, explicit header and
`.dat` tests, and warning caller tests passed (417 passed, 44 expected
skips in the focused run).
- Direct `Record.check_sig_cohesion` calls matched pre-change main in
2,000 generated cases, including errors, printed diagnostics, and
in-place samples-per-frame defaults.
- Docs built with warnings treated as errors; Ruff and `git diff
--check` passed.

No changelog entry because this changes only internal writer structure.
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