diff --git a/src/wfdb/io/_header.py b/src/wfdb/io/_header.py index f3b2badc..ec8b5249 100644 --- a/src/wfdb/io/_header.py +++ b/src/wfdb/io/_header.py @@ -137,6 +137,37 @@ # Specifications of all WFDB header fields, except for comments FIELD_SPECS = pd.concat((RECORD_SPECS, SIGNAL_SPECS, SEGMENT_SPECS)) +_AUTO_FILE_NAME = object() + + +def _single_segment_write_default(field, current, n_sig, formats, snapshot=None): + """Resolve one missing header field from explicit values and channel data.""" + if field in RECORD_SPECS.index: + default = RECORD_SPECS.loc[field, "write_default"] + return default if current is None else None + if field not in SIGNAL_SPECS.index: + return None + + present = ( + current is not None if snapshot is None else field in snapshot.present_fields + ) + if field == "file_name" and not present: + return _AUTO_FILE_NAME + default = SIGNAL_SPECS.loc[field, "write_default"] + if default is None or present: + return None + if field == "adc_res": + if snapshot is not None: + formats = ( + [channel.fmt for channel in snapshot.channels] + if "fmt" in snapshot.present_fields + else None + ) + if formats is not None: + return _signal._fmt_res(formats) + count = snapshot.n_channels if snapshot is not None else n_sig + return [default] * count + @dataclass(frozen=True) class _ParsedSignalLine: @@ -837,56 +868,15 @@ def set_default(self, field, *, signal_snapshot=None): N/A """ - # Record specification fields - if field in RECORD_SPECS.index: - # Return if no default to set, or if the field is already - # present. - if ( - RECORD_SPECS.loc[field, "write_default"] is None - or getattr(self, field) is not None - ): - return - setattr(self, field, RECORD_SPECS.loc[field, "write_default"]) - - # Signal specification fields - # Setting entire list default, not filling in blanks in lists. - elif field in SIGNAL_SPECS.index: - # Specific dynamic case - present = ( - getattr(self, field) is not None - if signal_snapshot is None - else field in signal_snapshot.present_fields - ) - if field == "file_name" and not present: - self.file_name = self._auto_signal_file_names(signal_snapshot) - return - - # Return if no default to set, or if the field is already - # present. - if SIGNAL_SPECS.loc[field, "write_default"] is None or present: - return - - # Set more specific defaults if possible - if field == "adc_res": - formats = ( - self.fmt - if signal_snapshot is None - else ( - [channel.fmt for channel in signal_snapshot.channels] - if "fmt" in signal_snapshot.present_fields - else None - ) - ) - if formats is not None: - self.adc_res = _signal._fmt_res(formats) - return - - setattr( - self, - field, - [SIGNAL_SPECS.loc[field, "write_default"]] - * (signal_snapshot.n_channels if signal_snapshot else self.n_sig), - ) + if field not in RECORD_SPECS.index and field not in SIGNAL_SPECS.index: + return + value = _single_segment_write_default( + field, getattr(self, field, None), self.n_sig, self.fmt, signal_snapshot + ) + if value is _AUTO_FILE_NAME: + value = self._auto_signal_file_names(signal_snapshot) + if value is not None: + setattr(self, field, value) def check_field_cohesion( self, rec_write_fields, sig_write_fields, signal_snapshot=None diff --git a/src/wfdb/io/record.py b/src/wfdb/io/record.py index e20775d9..30698892 100644 --- a/src/wfdb/io/record.py +++ b/src/wfdb/io/record.py @@ -5774,23 +5774,19 @@ def _prepare_defaults(self) -> None: ) record_fields, signal_fields = self.get_write_fields(snapshot) for field in record_fields: - if field not in _header.RECORD_SPECS.index: - continue - default = _header.RECORD_SPECS.loc[field, "write_default"] - if default is not None and getattr(self, field) is None: - setattr(self, field, default) + self._apply_default(field) for field in signal_fields: - if field == "file_name" and field not in snapshot.present_fields: - self.file_name = self._auto_signal_file_names(snapshot) - continue - default = _header.SIGNAL_SPECS.loc[field, "write_default"] - if default is None or field in snapshot.present_fields: - continue - if field == "adc_res" and "fmt" in snapshot.present_fields: - formats = [channel.fmt for channel in snapshot.channels] - self.adc_res = _signal._fmt_res(formats) - continue - setattr(self, field, [default] * snapshot.n_channels) + self._apply_default(field, snapshot) + + def _apply_default(self, field, snapshot=None) -> None: + """Apply a shared default to the model's current field values.""" + value = _header._single_segment_write_default( + field, getattr(self, field, None), self.n_sig, self.fmt, snapshot + ) + if value is _header._AUTO_FILE_NAME: + value = self._auto_signal_file_names(snapshot) + if value is not None: + setattr(self, field, value) def _prepare_calibration(self) -> None: """Resolve format and calibration before inherited feature setup.""" diff --git a/tests/test_channel_defaults_snapshot.py b/tests/test_channel_defaults_snapshot.py index b3a4985d..f4594430 100644 --- a/tests/test_channel_defaults_snapshot.py +++ b/tests/test_channel_defaults_snapshot.py @@ -57,6 +57,28 @@ def test_existing_lists_remain_the_mutable_owner(): assert record.adc_res == [None, 12] +def test_direct_set_default_uses_current_channel_fields(): + """The public single-field setter shares the current default rules.""" + record = wfdb.Record(record_name="manual", n_sig=2, fmt=["16", "80"]) + + record.set_default("file_name") + record.set_default("adc_res") + record.set_default("adc_zero") + + assert record.file_name == ["manual_1.dat", "manual_2.dat"] + assert record.adc_res == [16, 8] + assert record.adc_zero == [0, 0] + + +def test_direct_set_default_ignores_multisegment_only_field(): + """The public setter leaves n_seg absent on a single-segment Record.""" + record = wfdb.Record(record_name="manual", n_sig=1) + + record.set_default("n_seg") + + assert not hasattr(record, "n_seg") + + def test_invalid_channel_list_fails_before_defaults_mutate_record(): """A partial list does not leave a half-defaulted Record.""" record = wfdb.Record(record_name="manual", n_sig=2, fmt=["16"])