From 8fca19fee4cf75897d0be49912d1b369e0b6665c Mon Sep 17 00:00:00 2001 From: Tom Pollard Date: Sun, 8 Nov 2026 15:41:29 -0500 Subject: [PATCH] Reject negative infinity in expanded signals in calc_adc_params The inf check for e_p_signal only compared the channel minimum and maximum against math.inf, so a channel containing -inf got past it and failed later with "cannot convert float NaN to integer". Use math.isinf so both signs are caught, as the p_signal branch already does. --- changelog.d/adc-negative-inf.rst | 2 ++ src/wfdb/io/_signal.py | 4 +--- tests/test_record.py | 13 +++++++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 changelog.d/adc-negative-inf.rst diff --git a/changelog.d/adc-negative-inf.rst b/changelog.d/adc-negative-inf.rst new file mode 100644 index 00000000..6918577e --- /dev/null +++ b/changelog.d/adc-negative-inf.rst @@ -0,0 +1,2 @@ +**Reject negative infinity in expanded signals** + ``calc_adc_params`` now raises "Signal contains inf" when ``e_p_signal`` contains ``-inf``, as it already did for ``p_signal``, instead of failing with ``cannot convert float NaN to integer``. diff --git a/src/wfdb/io/_signal.py b/src/wfdb/io/_signal.py index 9b5e4dc8..f83c3c88 100644 --- a/src/wfdb/io/_signal.py +++ b/src/wfdb/io/_signal.py @@ -845,9 +845,7 @@ def calc_adc_params(self): minvals.append(float(np.nanmin(ch))) maxvals.append(float(np.nanmax(ch))) - if any(x == math.inf for x in minvals) or any( - x == math.inf for x in maxvals - ): + if any(math.isinf(x) for x in minvals + maxvals): raise ValueError("Signal contains inf. Cannot perform adc.") for ch, _ in enumerate(self.e_p_signal): diff --git a/tests/test_record.py b/tests/test_record.py index 788a9abe..55f8415d 100644 --- a/tests/test_record.py +++ b/tests/test_record.py @@ -1513,6 +1513,19 @@ def test_physical_conversion(self, temp_path): atol=(0.05 / gain), ) + @pytest.mark.parametrize("value", [np.inf, -np.inf]) + @pytest.mark.parametrize("expanded", [False, True]) + def test_adc_params_inf(self, value, expanded): + """Reject signals containing positive or negative infinity.""" + p_signal = np.array([[1.0, 0.5], [value, -0.25]]) + if expanded: + record = wfdb.Record(e_p_signal=list(p_signal.T), fmt=["16", "16"]) + else: + record = wfdb.Record(p_signal=p_signal, fmt=["16", "16"]) + + with pytest.raises(ValueError, match="Signal contains inf"): + record.calc_adc_params() + def test_adc_gain_max_boundary(self, temp_path): """Exercise the MAX_I32 clamp path.