Skip to content

Commit 0ab2fa0

Browse files
committed
implement inplace conversion for dac to save memory, and fix rdann sampto case reading in one extra sample
1 parent 961d5e5 commit 0ab2fa0

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# Versions should comply with PEP440. For a discussion on single-sourcing
2121
# the version across setup.py and the project code, see
2222
# https://packaging.python.org/en/latest/single_source_version.html
23-
version='1.3.2',
23+
version='1.3.3',
2424

2525
description='The WFDB Python Toolbox',
2626
long_description=long_description,

wfdb/readwrite/_signals.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -312,20 +312,17 @@ def dac(self, expanded=False, returnres=64, inplace=False):
312312

313313
# Do inplace conversion and set relevant variables.
314314
if inplace:
315-
# No clever memory saving here...
316315
if expanded:
317-
p_signal = []
318316
for ch in range(0, self.nsig):
319317
# nan locations for the channel
320-
chnanlocs = self.e_d_signals[ch] == dnans[ch]
321-
322-
p_signal.append(((self.e_d_signals[ch] - self.baseline[ch])/self.adcgain[ch]).astype(floatdtype, copy=False))
323-
p_signal[ch][chnanlocs] = np.nan
324-
325-
self.e_p_signals = p_signal
318+
ch_nanlocs = self.e_d_signals[ch] == dnans[ch]
319+
self.e_d_signals[ch] = self.e_d_signals[ch].astype(floatdtype, copy=False)
320+
np.subtract(self.e_d_signals[ch], self.baseline[ch], self.e_d_signals[ch])
321+
np.divide(self.e_d_signals[ch], self.adcgain[ch], self.e_d_signals[ch])
322+
self.e_d_signals[ch][ch_nanlocs] = np.nan
323+
self.e_p_signals = self.e_d_signals
326324
self.e_d_signals = None
327325
else:
328-
# nan locations
329326
nanlocs = self.d_signals == dnans
330327
# Do float conversion immediately to avoid potential under/overflow
331328
# of efficient int dtype
@@ -342,13 +339,17 @@ def dac(self, expanded=False, returnres=64, inplace=False):
342339
p_signal = []
343340
for ch in range(0, self.nsig):
344341
# nan locations for the channel
345-
chnanlocs = self.e_d_signals[ch] == dnans[ch]
346-
p_signal.append(((self.e_d_signals[ch] - self.baseline[ch])/self.adcgain[ch]).astype(floatdtype, copy=False))
347-
p_signal[ch][chnanlocs] = np.nan
342+
ch_nanlocs = self.e_d_signals[ch] == dnans[ch]
343+
ch_p_signal = self.e_d_signals[ch].astype(floatdtype, copy=False)
344+
np.subtract(ch_p_signal, self.baseline[ch], ch_p_signal)
345+
np.divide(ch_p_signal, self.adcgain[ch], ch_p_signal)
346+
ch_p_signal[ch_nanlocs] = np.nan
347+
p_signal.append(ch_p_signal)
348348
else:
349-
# nan locations
350349
nanlocs = self.d_signals == dnans
351-
p_signal = ((self.d_signals - self.baseline) / self.adcgain).astype(floatdtype, copy=False)
350+
p_signal = self.d_signal.astype(floatdtype, copy=False)
351+
np.subtract(p_signal, self.baseline, p_signal)
352+
np.divide(p_signal, self.adcgain, p_signal)
352353
p_signal[nanlocs] = np.nan
353354

354355
return p_signal

wfdb/readwrite/annotations.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,7 @@ def proc_ann_bytes(filebytes, sampto):
12471247
subtype, chan, num, aux_note = update_extra_fields(subtype, chan, num, aux_note, update)
12481248

12491249
if sampto and sampto<sample_total:
1250+
sample, label_store, subtype, chan, num, aux_note = rm_last(sample, label_store, subtype, chan, num, aux_note)
12501251
break
12511252

12521253
return sample, label_store, subtype, chan, num, aux_note
@@ -1442,7 +1443,15 @@ def lists_to_arrays(*args):
14421443
"""
14431444
return [np.array(a, dtype='int') for a in args]
14441445

1445-
1446+
def rm_last(*args):
1447+
"""
1448+
Remove the last index from each list
1449+
"""
1450+
if len(args) == 1:
1451+
return args[:-1]
1452+
else:
1453+
return [a[:-1] for a in args]
1454+
return
14461455

14471456
## ------------- /Reading Annotations ------------- ##
14481457

0 commit comments

Comments
 (0)