Skip to content

Commit 2b1d824

Browse files
author
Benjamin Moody
committed
adc: replace NaNs before converting array to integers.
When converting physical to digital sample arrays, we must replace NaN values (which represent a missing sample) with the appropriate invalid-sample sentinel value. Attempting to convert a floating-point NaN to an integer, as was done here, is implementation-defined behavior (and is controlled, to an extent, by the global numpy configuration.) We don't want to be dependent on the hardware or the global numpy configuration, and for efficiency it's best to avoid triggering floating-point errors to begin with. So instead of converting the floating-point array to integers, and fixing up the integer array after the fact, we want to replace the floating-point values *first*, and then convert to integers.
1 parent 384b987 commit 2b1d824

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

wfdb/io/_signal.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,8 @@ def adc_inplace_1d(ch_p_signal, adc_gain, baseline, d_nan):
539539
np.multiply(ch_p_signal, adc_gain, ch_p_signal)
540540
np.add(ch_p_signal, baseline, ch_p_signal)
541541
np.round(ch_p_signal, 0, ch_p_signal)
542+
ch_p_signal[ch_nanlocs] = d_nan
542543
ch_d_signal = ch_p_signal.astype(intdtype, copy=False)
543-
ch_d_signal[ch_nanlocs] = d_nan
544544
return ch_d_signal
545545

546546
# Convert a 2D physical signal array to digital. Note that the
@@ -550,12 +550,11 @@ def adc_inplace_2d(p_signal):
550550
np.multiply(p_signal, self.adc_gain, p_signal)
551551
np.add(p_signal, self.baseline, p_signal)
552552
np.round(p_signal, 0, p_signal)
553-
d_signal = p_signal.astype(intdtype, copy=False)
554-
555553
if nanlocs.any():
556-
for ch in range(d_signal.shape[1]):
554+
for ch in range(p_signal.shape[1]):
557555
if nanlocs[:, ch].any():
558-
d_signal[nanlocs[:, ch], ch] = d_nans[ch]
556+
p_signal[nanlocs[:, ch], ch] = d_nans[ch]
557+
d_signal = p_signal.astype(intdtype, copy=False)
559558
return d_signal
560559

561560
# Do inplace conversion and set relevant variables.

0 commit comments

Comments
 (0)