From 30f194a7d7d421cc715d292c96b22b5f2e2b0cce Mon Sep 17 00:00:00 2001 From: Tom Pollard Date: Sat, 5 Dec 2026 21:47:58 -0500 Subject: [PATCH] Read format 0 signals as missing --- changelog.d/read-format0-signals.rst | 6 +++ src/wfdb/io/_read_model.py | 31 ++++++++++-- src/wfdb/io/_signal.py | 7 +++ src/wfdb/io/record.py | 9 ++++ tests/spec_conformance.json | 6 +-- tests/test_format0_reads.py | 75 ++++++++++++++++++++++++++++ 6 files changed, 125 insertions(+), 9 deletions(-) create mode 100644 changelog.d/read-format0-signals.rst create mode 100644 tests/test_format0_reads.py diff --git a/changelog.d/read-format0-signals.rst b/changelog.d/read-format0-signals.rst new file mode 100644 index 00000000..fc0ad03a --- /dev/null +++ b/changelog.d/read-format0-signals.rst @@ -0,0 +1,6 @@ +**Read missing format 0 signals** + ``read_record`` and ``read_segments`` now return fully masked digital + values or ``NaN`` physical values for format 0 signals. Legacy physical + reads also return ``NaN``; legacy digital reads raise a clear ``ValueError`` + pointing to ``read_record(..., kind="digital")``. Other signals in the + record remain readable. diff --git a/src/wfdb/io/_read_model.py b/src/wfdb/io/_read_model.py index b7e55b26..6db5e388 100644 --- a/src/wfdb/io/_read_model.py +++ b/src/wfdb/io/_read_model.py @@ -205,6 +205,9 @@ def _dac_values(values, formats, gains, baselines, expanded, resolution): if expanded: converted = [] for array, fmt, gain, baseline in zip(values, formats, gains, baselines): + if fmt == "0": + converted.append(np.full(array.shape, np.nan, dtype=dtype)) + continue missing = array == _signal._digi_nan(fmt) physical = array.astype(dtype, copy=False) np.subtract(physical, baseline, physical) @@ -212,7 +215,14 @@ def _dac_values(values, formats, gains, baselines, expanded, resolution): physical[missing] = np.nan converted.append(physical) return converted - missing = values == _signal._digi_nan(formats) + if "0" in formats: + missing = np.zeros(values.shape, dtype=bool) + for index, fmt in enumerate(formats): + missing[:, index] = ( + True if fmt == "0" else values[:, index] == _signal._digi_nan(fmt) + ) + else: + missing = values == _signal._digi_nan(formats) converted = values.astype(dtype, copy=False) np.subtract(converted, baselines, converted) np.divide(converted, gains, converted) @@ -225,14 +235,27 @@ def _digital_mask(values, channels, expanded): if expanded: masks = None for index, (array, signal) in enumerate(zip(values, channels)): - missing = array == _signal._digi_nan(signal.fmt) + missing = ( + np.ones(array.shape, dtype=bool) + if signal.fmt == "0" + else array == _signal._digi_nan(signal.fmt) + ) if np.any(missing): if masks is None: masks = [np.ma.nomask] * len(values) masks[index] = missing return None if masks is None else tuple(masks) - markers = [_signal._digi_nan(signal.fmt) for signal in channels] - masks = values == (markers[0] if len(set(markers)) == 1 else markers) + if any(signal.fmt == "0" for signal in channels): + masks = np.zeros(values.shape, dtype=bool) + for index, signal in enumerate(channels): + masks[:, index] = ( + True + if signal.fmt == "0" + else values[:, index] == _signal._digi_nan(signal.fmt) + ) + else: + markers = [_signal._digi_nan(signal.fmt) for signal in channels] + masks = values == (markers[0] if len(set(markers)) == 1 else markers) return masks if np.any(masks) else None diff --git a/src/wfdb/io/_signal.py b/src/wfdb/io/_signal.py index 680493af..8ea596cf 100644 --- a/src/wfdb/io/_signal.py +++ b/src/wfdb/io/_signal.py @@ -100,6 +100,7 @@ # Digital value used to represent a missing/invalid sample, in each of the # WFDB dat formats. INVALID_SAMPLE_VALUE = { + "0": None, "80": -(2**7), "508": -(2**7), "310": -(2**9), @@ -1162,6 +1163,12 @@ def _rd_segment( signals = [None] * len(channels) for fn in w_file_name: + if w_fmt[fn] == "0": + for channel in w_channel[fn]: + output = channels.index(channel) + count = (sampto - sampfrom) * samps_per_frame[channel] + signals[output] = np.zeros(count, dtype=f"int{return_res}") + continue # Get the list of all signals contained in the dat file datsignals = _rd_dat_signals( file_name=fn, diff --git a/src/wfdb/io/record.py b/src/wfdb/io/record.py index e544fb43..5d49f6f6 100644 --- a/src/wfdb/io/record.py +++ b/src/wfdb/io/record.py @@ -4046,6 +4046,15 @@ def _rdrecord( # A single segment record elif isinstance(record, Record): + if not physical: + for channel in channels: + if record.fmt[channel] == "0": + raise ValueError( + f"Signal {record.sig_name[channel]!r} (channel {channel}) " + "has format 0 and cannot be represented in a digital " + 'legacy Record; use read_record(..., kind="digital") ' + "for masked values." + ) markerless_tails = ( _signal._format8_missing_skew_tails( record.file_name, diff --git a/tests/spec_conformance.json b/tests/spec_conformance.json index afc39794..b5231d88 100644 --- a/tests/spec_conformance.json +++ b/tests/spec_conformance.json @@ -1,11 +1,7 @@ { "spec_repository": "https://github.com/wfdb/wfdb-spec", - "spec_commit": "8170251a5f8d2379a0f0a687ab89e224a032e5ab", + "spec_commit": "ad32e3231a55cf9c8b8fb4da68f64d198bf4884d", "known_gaps": { - "format0": { - "status": "unsupported", - "issue": 935 - }, "header-partial-signal-fields": { "status": "differs", "issue": 937 diff --git a/tests/test_format0_reads.py b/tests/test_format0_reads.py new file mode 100644 index 00000000..f8e76a9c --- /dev/null +++ b/tests/test_format0_reads.py @@ -0,0 +1,75 @@ +"""Format 0 declares a signal without a data file or valid samples.""" + +import numpy as np +import pytest + +import wfdb + + +def _make_record(tmp_path, *, mixed=False): + """Write a format 0 header, optionally with an ordinary signal beside it.""" + n_sig = 2 if mixed else 1 + lines = [f"null {n_sig} 250 4", "~ 0 200/mV 12 0 0 0 0 absent"] + if mixed: + (tmp_path / "present.dat").write_bytes( + np.array([1, 2, 3, 4], dtype="