Skip to content

Share digital write feature calculation - #890

Merged
tompollard merged 2 commits into
mainfrom
refactor/shared-digital-features
Dec 3, 2026
Merged

tompollard merged 2 commits into
mainfrom
refactor/shared-digital-features

Conversation

@tompollard

@tompollard tompollard commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Summary

Continue the #866 cleanup after #889 by sharing the digital feature
calculation used by the modern write model and the public
Record.set_d_features() method.

  • A private helper takes digital arrays and, for expanded data,
    samps_per_frame. It returns frame and channel counts, initial values,
    and checksums.
  • Both write paths call it after their existing field checks. Record keeps
    its public signature, conversion logic, and mutation behavior; the model
    still prepares calibration separately.
  • Add fixed expectations for uniform and expanded public setter calls.

The 5.17.0 byte fixtures and differential parity tests remain in place.
Successful calls and release bytes are unchanged. On an empty digital array,
Record.set_d_features() still raises IndexError, but now leaves sig_len
and n_sig unchanged rather than partially updating them. A regression test
and changelog entry cover that failure-state improvement.

Checks

  • Full offline suite on the original commit: 2,279 passed, 48 skipped, 23
    deselected; 41 focused feature and byte-fixture tests pass after the new test
  • Writer byte fixtures and parity comparisons passed
  • Ruff check and format passed
  • Sphinx docs build passed with warnings as errors

@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 shares the feature calculation correctly, and it can merge. The fixture, parity and feature tests pass (413 passed, 44 expected skips), as do the offline suite (2,279 passed, 48 skipped) and Ruff.

What I checked

_digital_feature_values returns exactly what the removed lines assigned, in the same order: sig_len from samps_per_frame[0] for expanded values, n_sig, initial values and checksums. As with #889, I compared the public Record.set_d_features() with main (a988bfe) on 3,000 random records. They had 1 to 3 channels and 0 to 4 frames, uniform or expanded, do_adc on or off, and fmt, calibration and samps_per_frame each sometimes missing. After each call I compared the resulting attributes, or the error.

  • All 2,071 successful calls give identical attributes, and every error type and message is unchanged.
  • 423 calls differ in one way, and it's an improvement. With zero-frame arrays, both versions raise IndexError: index 0 is out of bounds for axis 0 with size 0 from the initial-value calculation. On main, sig_len=0 and n_sig had already been assigned when it failed, so the record was left half-updated. Here, the tuple assignment means nothing changes unless every value was calculated.

Minor

The description says there's no public behaviour change. Strictly, the partial update on failure is gone. I don't think it needs a changelog entry, since it only affects calls that already raise. But a one-line test that a failing set_d_features() leaves sig_len and n_sig unchanged would pin the new behaviour, rather than leaving it as a side effect of the refactor.

@tompollard

Copy link
Copy Markdown
Member Author

Thanks for checking the public setter against main, especially its error
state. You're right that an empty array now leaves sig_len and n_sig
unchanged after the same IndexError, so the original description overstated
the behavior claim.

I added uniform and expanded regression cases in 9eecb3e, updated the PR
description, and added a short changelog entry because the failed call's
mutation behavior is observable. The 41 focused feature and byte-fixture
tests pass, and Ruff is clean.

@tompollard
tompollard merged commit 87ba3c3 into main Dec 3, 2026
19 checks passed
@tompollard
tompollard deleted the refactor/shared-digital-features branch December 3, 2026 16:47
tompollard added a commit that referenced this pull request Dec 3, 2026
## 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
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