2
2
import os
3
3
import math
4
4
from . import downloads
5
- import pdb
6
5
7
6
# All defined WFDB dat formats
8
7
datformats = ["80" ,"212" ,"16" ,"24" ,"32" ]
@@ -287,12 +286,18 @@ def adc(self, expanded=False):
287
286
return d_signals
288
287
289
288
290
- def dac (self , expanded = False , returnres = 64 ):
289
+ def dac (self , expanded = False , returnres = 64 , inplace = False ):
291
290
"""
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
293
292
in d_signals if expanded is False, or e_d_signals if expanded is True.
293
+
294
294
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
+
295
299
"""
300
+
296
301
# The digital nan values for each channel
297
302
dnans = digi_nan (self .fmt )
298
303
@@ -305,21 +310,48 @@ def dac(self, expanded=False, returnres=64):
305
310
else :
306
311
floatdtype = 'float16'
307
312
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
316
340
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
323
355
324
356
325
357
# 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):
863
895
else :
864
896
sigbytes = downloads .streamdat (filename , pbdir , fmt , bytecount , startbyte , dataloadtypes )
865
897
866
- #pdb.set_trace()
867
-
868
898
return sigbytes
869
899
870
900
0 commit comments