Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/wfdb/io/_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 19 additions & 6 deletions src/wfdb/io/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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]:
Expand Down
29 changes: 29 additions & 0 deletions tests/test_explicit_dat_writing.py
Original file line number Diff line number Diff line change
@@ -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()
Loading