Repository navigation
Share single-segment write default rules - #889
Conversation
tompollard
left a comment
There was a problem hiding this comment.
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 matchmainexactly, including the errors for records withoutn_sig.set_default(field): 1,707 runs that succeed onmainnow raiseAttributeError: '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.
|
Thanks for testing I fixed that in 62b4a1b by treating an absent attribute as unset and added a |
tompollard
left a comment
There was a problem hiding this comment.
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.
## 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
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.and captured channel snapshot in one private helper.
adc_res, and the rulethat a supplied list is not filled or replaced.
Record's public setters and their mutation behavior. The model appliesthe resolved values to its own write state; neither path delegates to the
other's public setter.
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