From 01315817d29c52587431d85154440f34213b23bb Mon Sep 17 00:00:00 2001 From: Tom Pollard Date: Thu, 3 Dec 2026 13:21:54 -0500 Subject: [PATCH] Write model signal files from explicit arrays --- src/wfdb/io/_signal.py | 38 ++++++++++++++++++++++++++++++ src/wfdb/io/record.py | 25 +++++++++++++++----- tests/test_explicit_dat_writing.py | 29 +++++++++++++++++++++++ 3 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 tests/test_explicit_dat_writing.py diff --git a/src/wfdb/io/_signal.py b/src/wfdb/io/_signal.py index ccf477f6..55e50567 100644 --- a/src/wfdb/io/_signal.py +++ b/src/wfdb/io/_signal.py @@ -2441,6 +2441,44 @@ def _np_dtype(bit_res, discrete): return "float" + str(max(np_res, 16)) +def _check_dat_values(signal_snapshot, values, expanded, sig_len): + """Check digital shapes and format bounds from explicit values.""" + n_sig = signal_snapshot.n_channels + if expanded: + spf = [channel.samps_per_frame or 1 for channel in signal_snapshot.channels] + if n_sig != len(values): + raise ValueError("n_sig does not match the length of e_d_signal") + for ch in range(n_sig): + if len(values[ch]) != spf[ch] * sig_len: + raise ValueError( + f"Length of channel {ch} does not match " + f"samps_per_frame[{ch}]*sig_len" + ) + elif (sig_len, n_sig) != values.shape: + print("sig_len: ", sig_len) + print("n_sig: ", n_sig) + print("d_signal.shape: ", values.shape) + raise ValueError("sig_len and n_sig do not match shape of d_signal") + + for ch, channel in enumerate(signal_snapshot.channels): + fmt = channel.fmt + dmin, dmax = _digi_bounds(fmt) + samples = values[ch] if expanded else values[:, ch] + chmin = np.min(samples) + chmax = np.max(samples) + if chmin < dmin or chmax > dmax: + raise IndexError( + "Channel " + + str(ch) + + " contain values outside allowed range [" + + str(dmin) + + ", " + + str(dmax) + + "] for fmt " + + str(fmt) + ) + + def _write_dat_files(signal_snapshot, values, expanded, write_dir, wfdb_archive): """Write grouped dat files from explicit channel fields and digital arrays.""" storage = signal_snapshot.storage diff --git a/src/wfdb/io/record.py b/src/wfdb/io/record.py index 03307df1..cb3f0776 100644 --- a/src/wfdb/io/record.py +++ b/src/wfdb/io/record.py @@ -5198,6 +5198,16 @@ def validate(field, required_channels="all", signal_snapshot=None): ) +def _write_model_dats(writer, snapshot, expanded, write_dir, archive): + """Validate and write model digital arrays without SignalMixin methods.""" + field = "e_d_signal" if expanded else "d_signal" + values = vars(writer) + digital = values[field] + _check_field_value(field, digital, lookup=values.__getitem__) + _signal._check_dat_values(snapshot, digital, expanded, writer.sig_len) + _signal._write_dat_files(snapshot, digital, expanded, write_dir, archive) + + def _write_prepared_files(writer, expanded, write_dir, wfdb_archive): """Write checked header and signal files from prepared encoder fields.""" # A layout header with no signal files has only a header to write. @@ -5224,12 +5234,15 @@ def _write_prepared_files(writer, expanded, write_dir, wfdb_archive): else: snapshot = writer._write_checked_header(write_dir, expanded, archive) if has_signal_files: - writer.wr_dats( - expanded=expanded, - write_dir=write_dir, - wfdb_archive=archive, - signal_snapshot=snapshot, - ) + if isinstance(writer, _WriteModel): + _write_model_dats(writer, snapshot, expanded, write_dir, archive) + else: + writer.wr_dats( + expanded=expanded, + write_dir=write_dir, + wfdb_archive=archive, + signal_snapshot=snapshot, + ) def _destination_file_names(file_names, name: str, n_signals: int) -> list[str]: diff --git a/tests/test_explicit_dat_writing.py b/tests/test_explicit_dat_writing.py new file mode 100644 index 00000000..413edcf7 --- /dev/null +++ b/tests/test_explicit_dat_writing.py @@ -0,0 +1,29 @@ +"""Modern signal file writes use explicit arrays and channel fields.""" + +import numpy as np +import pytest + +import wfdb +from wfdb.io import _signal + + +def test_modern_dat_write_does_not_call_signal_mixin(tmp_path, monkeypatch): + """The model writes .dat files without inherited signal methods.""" + + def unexpected_call(*_args, **_kwargs): + pytest.fail("called an inherited .dat writer") + + monkeypatch.setattr(_signal.SignalMixin, "wr_dats", unexpected_call) + monkeypatch.setattr(_signal.SignalMixin, "check_sig_cohesion", unexpected_call) + monkeypatch.setattr(_signal.SignalMixin, "wr_dat_files", unexpected_call) + wfdb.write_signals( + tmp_path / "copy", + np.array([[0.0], [0.1], [0.2]]), + fs=125, + kind="physical", + formats="16", + channel_names=["I"], + units=["mV"], + ) + + assert (tmp_path / "copy.dat").exists()