22import os
33import math
44from . import downloads
5- import pdb
65
76# All defined WFDB dat formats
87datformats = ["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
0 commit comments