Skip to content

Share single-segment write default rules - #889

Merged
tompollard merged 2 commits into
mainfrom
refactor/shared-write-defaults
Dec 3, 2026
Merged

tompollard merged 2 commits into
mainfrom
refactor/shared-write-defaults

Conversation

@tompollard

Copy link
Copy Markdown
Member

Summary

Follow up on #888 and the decision in #866 by keeping one set of
single-segment write-default rules for both the modern write model and the
public Record.set_default() / Record.set_defaults() methods.

  • Resolve each default from the current field value, channel count, formats,
    and captured channel snapshot in one private helper.
  • Preserve dynamic file-name grouping, format-derived adc_res, and the rule
    that a supplied list is not filled or replaced.
  • Keep Record's public setters and their mutation behavior. The model applies
    the resolved values to its own write state; neither path delegates to the
    other's public setter.
  • Add a direct Record.set_default() test for file names and ADC defaults.

The digital feature calculations are the next separate step. This PR changes
no public behavior or release bytes.

Checks

  • Full offline suite: 2,276 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 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.

One set of rules for the model and the public setters is the right outcome. I found one small regression in Record.set_default to fix before merging. The fixture, parity and defaults tests pass (414 passed, 44 expected skips), as do the offline suite (2,276 passed, 48 skipped) and Ruff.

What I checked

Because this changes a public method, I compared Record behaviour directly with main (f67eb21). I built 4,000 random sparse Records with 1 to 3 channels. n_sig, fmt, file_name, adc_res, adc_gain, fs and samps_per_frame were each sometimes set and sometimes not. Each record then either called set_defaults() or called set_default(field) for every record and signal field (plus comments and seg_len) in random order. Then I compared every resulting attribute, or the error.

  • set_defaults(): all ~2,000 runs match main exactly, including the errors for records without n_sig.
  • set_default(field): 1,707 runs that succeed on main now raise AttributeError: 'Record' object has no attribute 'n_seg'.

The regression: set_default("n_seg") on a single-segment Record

r = wfdb.Record(record_name="r", n_sig=1)
r.set_default("n_seg")
# main: no-op
# here: AttributeError: 'Record' object has no attribute 'n_seg'

n_seg is in RECORD_SPECS, but a single-segment Record has no n_seg attribute. The old code checked write_default is None first, so it never read the attribute. The new set_default calls getattr(self, field) before the shared helper decides anything. set_defaults() doesn't hit this, because get_write_fields never includes n_seg for a Record. A direct set_default("n_seg") does, for example code that loops over RECORD_SPECS.index. Using getattr(self, field, None) in Record.set_default (_WriteModel._apply_default already does) restores the old no-op. A one-line test, Record(...).set_default("n_seg") leaving the record unchanged, would pin it.

Otherwise, _single_segment_write_default reproduces each legacy branch: record defaults only when unset; the file-name sentinel before the default check; adc_res from the captured formats with a snapshot, or from self.fmt without one, falling through to the per-channel default when there are no formats; and no change to a supplied list.

@tompollard

Copy link
Copy Markdown
Member Author

Thanks for testing Record.set_default() directly. You're right: I read
n_seg before checking whether it has a default, which changed its existing
no-op behavior on a single-segment Record.

I fixed that in 62b4a1b by treating an absent attribute as unset and added a
regression test for set_default("n_seg"). The defaults, byte-fixture, and
parity tests pass (415 passed, 44 expected skips), and Ruff passes.

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

62b4a1b fixes it. I reran the same 4,000-record comparison against main (f67eb21): every set_defaults() and set_default(field) outcome, attribute values and errors alike, is now identical, with the same digest and the same 812 expected errors. The new test pins the n_seg no-op, and checking not hasattr(record, "n_seg") is the right assertion. The defaults, fixture and parity tests pass (415 passed, 44 expected skips), and Ruff is clean.

Ready to merge from my side once CI passes.

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