Skip to content

Commit e013d5b

Browse files
author
Benjamin Moody
committed
adc: optimize replacement of NaNs.
When converting physical to digital sample arrays, we must replace NaN values with the appropriate invalid-sample sentinel value. To do this, we need to call np.isnan and use the result as a mask to replace entries in the output array. (Although the function np.nan_to_num also exists, it's less efficient: it literally does just this, but also handles infinities.) What we don't need to do is to call any() to check whether there are any true entries - that just means we're iterating through the same array three times rather than once. Furthermore, np.copyto can broadcast d_nans across the rows of p_signal, so all the channels can be handled at once. Also use copyto in adc_inplace_1d for consistency.
1 parent 5a05f7a commit e013d5b

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

wfdb/io/_signal.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ 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
542+
np.copyto(ch_p_signal, d_nan, where=ch_nanlocs)
543543
ch_d_signal = ch_p_signal.astype(intdtype, copy=False)
544544
return ch_d_signal
545545

@@ -550,10 +550,7 @@ 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-
if nanlocs.any():
554-
for ch in range(p_signal.shape[1]):
555-
if nanlocs[:, ch].any():
556-
p_signal[nanlocs[:, ch], ch] = d_nans[ch]
553+
np.copyto(p_signal, d_nans, where=nanlocs)
557554
d_signal = p_signal.astype(intdtype, copy=False)
558555
return d_signal
559556

0 commit comments

Comments
 (0)