Skip to content

Commit 961d5e5

Browse files
committed
fix error and add option in dac function
1 parent a06d163 commit 961d5e5

File tree

3 files changed

+57
-30
lines changed

3 files changed

+57
-30
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.0',
23+
version='1.3.2',
2424

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

wfdb/readwrite/_signals.py

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import os
33
import math
44
from . import downloads
5-
import pdb
65

76
# All defined WFDB dat formats
87
datformats = ["80","212","16","24","32"]
@@ -287,12 +286,18 @@ def adc(self, expanded=False):
287286
return d_signals
288287

289288

290-
def dac(self, expanded=False, returnres=64):
289+
def dac(self, expanded=False, returnres=64, inplace=False):
291290
"""
292-
Returns the digital to analogue conversion for a Record object's signal stored
291+
Performs the digital to analogue conversion of the signal stored
293292
in d_signals if expanded is False, or e_d_signals if expanded is True.
293+
294294
The d_signals/e_d_signals, fmt, gain, and baseline fields must all be valid.
295+
296+
If inplace is True, the dac will be performed inplace on the variable, the
297+
p_signals attribute will be set, and d_signals will be set to None.
298+
295299
"""
300+
296301
# The digital nan values for each channel
297302
dnans = digi_nan(self.fmt)
298303

@@ -305,21 +310,48 @@ def dac(self, expanded=False, returnres=64):
305310
else:
306311
floatdtype = 'float16'
307312

308-
if expanded:
309-
p_signal = []
310-
for ch in range(0, self.nsig):
311-
# nan locations for the channel
312-
chnanlocs = self.e_d_signals[ch] == dnans[ch]
313-
p_signal.append((self.e_d_signals[ch] - np.array(self.baseline[ch], dtype=self.e_d_signals[ch].dtype))/
314-
np.array(self.adcgain[ch], dtype=floatdtype).astype(floatdtype, copy=False))
315-
p_signal[ch][chnanlocs] = np.nan
313+
# Do inplace conversion and set relevant variables.
314+
if inplace:
315+
# No clever memory saving here...
316+
if expanded:
317+
p_signal = []
318+
for ch in range(0, self.nsig):
319+
# 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
326+
self.e_d_signals = None
327+
else:
328+
# nan locations
329+
nanlocs = self.d_signals == dnans
330+
# Do float conversion immediately to avoid potential under/overflow
331+
# of efficient int dtype
332+
self.d_signals = self.d_signals.astype(floatdtype, copy=False)
333+
np.subtract(self.d_signals, self.baseline, self.d_signals)
334+
np.divide(self.d_signals, self.adcgain, self.d_signals)
335+
self.d_signals[nanlocs] = np.nan
336+
self.p_signals = self.d_signals
337+
self.d_signals = None
338+
339+
# Return the variable
316340
else:
317-
# nan locations
318-
nanlocs = self.d_signals == dnans
319-
p_signal = (self.d_signals - np.array(self.baseline, dtype=self.d_signals.dtype)) / np.array(self.adcgain, dtype=floatdtype).astype(floatdtype, copy=False)
320-
p_signal[nanlocs] = np.nan
321-
322-
return p_signal
341+
if expanded:
342+
p_signal = []
343+
for ch in range(0, self.nsig):
344+
# 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
348+
else:
349+
# nan locations
350+
nanlocs = self.d_signals == dnans
351+
p_signal = ((self.d_signals - self.baseline) / self.adcgain).astype(floatdtype, copy=False)
352+
p_signal[nanlocs] = np.nan
353+
354+
return p_signal
323355

324356

325357
# Compute appropriate gain and baseline parameters given the physical signal and the fmts
@@ -863,8 +895,6 @@ def getdatbytes(filename, dirname, pbdir, fmt, startbyte, nsamp):
863895
else:
864896
sigbytes = downloads.streamdat(filename, pbdir, fmt, bytecount, startbyte, dataloadtypes)
865897

866-
#pdb.set_trace()
867-
868898
return sigbytes
869899

870900

wfdb/readwrite/records.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -821,13 +821,11 @@ def rdsamp(recordname, sampfrom=0, sampto=None, channels = None, physical = True
821821

822822
# Arrange/edit the object fields to reflect user channel and/or signal range input
823823
record.arrangefields(channels, expanded=False)
824-
# Obtain physical values
825-
if physical is True:
826-
# Perform dac to get physical signal
827-
record.d_signals = record.dac(expanded=False, returnres=returnres)
828-
record.p_signals = record.d_signals
829-
record.d_signals = None
830824

825+
if physical is True:
826+
# Perform inplace dac to get physical signal
827+
record.dac(expanded=False, returnres=returnres, inplace=True)
828+
831829
# Return each sample of the signals with multiple samples per frame
832830
else:
833831
record.e_d_signals = _signals.rdsegment(record.filename, dirname, pbdir, record.nsig, record.fmt, record.siglen,
@@ -836,12 +834,11 @@ def rdsamp(recordname, sampfrom=0, sampto=None, channels = None, physical = True
836834

837835
# Arrange/edit the object fields to reflect user channel and/or signal range input
838836
record.arrangefields(channels, expanded=True)
839-
# Obtain physical values
837+
840838
if physical is True:
841839
# Perform dac to get physical signal
842-
record.e_d_signals = record.dac(expanded=True, returnres=returnres)
843-
record.e_p_signals = record.e_d_signals
844-
record.e_d_signals = None
840+
record.dac(expanded=True, returnres=returnres, inplace=True)
841+
845842

846843
# A multi segment record
847844

0 commit comments

Comments
 (0)