Skip to content

Extract flattened write preparation from Record - #891

Merged
tompollard merged 2 commits into
mainfrom
refactor/flattened-write-preparation
Dec 3, 2026
Merged

tompollard merged 2 commits into
mainfrom
refactor/flattened-write-preparation

Conversation

@tompollard

Copy link
Copy Markdown
Member

Summary

Continue #866 after #890 by moving flattened multi-segment write preparation
out of Record._prepare_flattened_write.

  • A private helper takes the selected units, formats, file names, calibration,
    and arrays explicitly. It preserves the format-32 fallback and translates
    missing-sample markers when digital channels need a common format.
  • The public legacy Record.wrsamp path retains its private wrapper. The
    modern _WriteModel calls the shared helper directly and no longer borrows
    Record._prepare_flattened_write.
  • Add tests for mixed-format marker recoding without changing input arrays,
    physical fallback calibration, and the model's independence from the Record
    method.

The helper is shared by both write paths, so the differential test alone
cannot catch a mistake inside it. I also compared this branch with the
pre-change main checkout (87ba3c3) across 24 flattened write cases from
five sample masters, both kinds and layouts, plus a full-length 041s read.
All 12 successful cases produced the same file names and bytes; the other 12
failed with the same exception type and message. The fixed 5.17.0 flattened
fixture also passes. No public behavior change is intended.

Checks

  • Full offline suite: 2,284 passed, 48 skipped, 23 deselected
  • Writer byte fixtures and parity comparisons passed
  • Ruff check and format passed
  • Sphinx docs build passed with warnings as errors

@tompollard

Copy link
Copy Markdown
Member Author

The failing check was the docs build. Its docs-only environment mocks NumPy,
so evaluating the new np.ndarray | Sequence[np.ndarray] annotation while
importing record.py raised a TypeError and prevented Sphinx from importing
wfdb.

I deferred that annotation's evaluation in 2510aa0. I reran the docs-only
build with warnings as errors and a fresh Sphinx environment; it passes.
The 39 focused preparation and byte-fixture tests and Ruff also pass. CI is
rerunning on the new commit.

@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 extraction is correct, output is unchanged, and it can merge. 2510aa0 fixes the docs failure: the warnings-as-errors docs build passes locally with NumPy mocked, and all 19 CI checks pass.

What I checked

  • The helper keeps every branch of _prepare_flattened_write:

    • units default to "NU";
    • format 32 is used when formats are incomplete or a file mixes formats;
    • for physical values, the format 32 fallback is followed by calculated calibration;
    • for digital values, the old marker (or the dtype minimum when the format is unknown) is recoded to format 32's marker on an int64 copy;
    • missing gains and baselines are filled with 1.0 and 0.

    _WriteModel no longer binds Record._prepare_flattened_write.

  • Flattened output is byte-identical to main across all writers. Because the helper is shared, the parity test can't check it, so I compared this branch with main (87ba3c3) directly. That covered six masters (041s, v102s, both s00001 records, s25047, and p000878), five windows including full length, both kinds and layouts, and four writers: write_record, SignalRead.write with inferred formats and with formats="32", and the legacy rdrecord(..., m2s=True).wrsamp. All 480 cases match: 258 writes with identical file names and bytes, and 222 identical errors. The annotation fix in 2510aa0 doesn't change behaviour.

  • The flattened-preparation and fixture tests pass (39 passed), as do the offline suite (2,284 passed, 48 skipped) and Ruff.

Minor

As with #890, failure is now cleaner. The old method set self.units (with None replaced by "NU") before raising "No signal values are available for this layout", so a failed Record.wrsamp left units changed. The helper raises before anything is applied. That's an improvement and needs no changelog entry.

@tompollard
tompollard merged commit 8a88d1c into main Dec 3, 2026
19 checks passed
@tompollard
tompollard deleted the refactor/flattened-write-preparation branch December 3, 2026 17: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