3
3
import os
4
4
5
5
from . import download
6
-
6
+ import pdb
7
7
# WFDB dat formats - https://www.physionet.org/physiotools/wag/signal-5.htm
8
8
SIMPLE_FMTS = ['80' , '16' , '24' , '32' ]
9
9
SPECIAL_FMTS = ['212' , '310' , '311' ]
@@ -65,7 +65,7 @@ def check_sig_cohesion(self, write_fields, expanded):
65
65
raise ValueError ('Length of channel ' + str (ch )+ 'does not match samps_per_frame[' + str (ch + ']*sig_len' ))
66
66
67
67
# For each channel (if any), make sure the digital format has no values out of bounds
68
- for ch in range (0 , self .n_sig ):
68
+ for ch in range (self .n_sig ):
69
69
fmt = self .fmt [ch ]
70
70
dmin , dmax = digi_bounds (self .fmt [ch ])
71
71
@@ -97,7 +97,7 @@ def check_sig_cohesion(self, write_fields, expanded):
97
97
raise ValueError ('sig_len and n_sig do not match shape of d_signal' )
98
98
99
99
# For each channel (if any), make sure the digital format has no values out of bounds
100
- for ch in range (0 , self .n_sig ):
100
+ for ch in range (self .n_sig ):
101
101
fmt = self .fmt [ch ]
102
102
dmin , dmax = digi_bounds (self .fmt [ch ])
103
103
@@ -327,7 +327,7 @@ def adc(self, expanded=False, inplace=False):
327
327
# Do inplace conversion and set relevant variables.
328
328
if inplace :
329
329
if expanded :
330
- for ch in range (0 , self .n_sig ):
330
+ for ch in range (self .n_sig ):
331
331
# nan locations for the channel
332
332
ch_nanlocs = np .isnan (self .e_p_signal [ch ])
333
333
np .multiply (self .e_p_signal [ch ], self .adc_gain [ch ], self .e_p_signal [ch ])
@@ -348,7 +348,7 @@ def adc(self, expanded=False, inplace=False):
348
348
else :
349
349
if expanded :
350
350
d_signal = []
351
- for ch in range (0 , self .n_sig ):
351
+ for ch in range (self .n_sig ):
352
352
# nan locations for the channel
353
353
ch_nanlocs = np .isnan (self .e_p_signal [ch ])
354
354
ch_d_signal = self .e_p_signal .copy ()
@@ -428,7 +428,7 @@ def dac(self, expanded=False, return_res=64, inplace=False):
428
428
# Do inplace conversion and set relevant variables.
429
429
if inplace :
430
430
if expanded :
431
- for ch in range (0 , self .n_sig ):
431
+ for ch in range (self .n_sig ):
432
432
# nan locations for the channel
433
433
ch_nanlocs = self .e_d_signal [ch ] == dnans [ch ]
434
434
self .e_d_signal [ch ] = self .e_d_signal [ch ].astype (floatdtype , copy = False )
@@ -452,7 +452,7 @@ def dac(self, expanded=False, return_res=64, inplace=False):
452
452
else :
453
453
if expanded :
454
454
p_signal = []
455
- for ch in range (0 , self .n_sig ):
455
+ for ch in range (self .n_sig ):
456
456
# nan locations for the channel
457
457
ch_nanlocs = self .e_d_signal [ch ] == dnans [ch ]
458
458
ch_p_signal = self .e_d_signal [ch ].astype (floatdtype , copy = False )
@@ -488,7 +488,7 @@ def calc_adc_params(self):
488
488
489
489
dnans = digi_nan (self .fmt )
490
490
491
- for ch in range (0 , np .shape (self .p_signal )[1 ]):
491
+ for ch in range (np .shape (self .p_signal )[1 ]):
492
492
# Get the minimum and maximum (valid) storage values
493
493
dmin , dmax = digi_bounds (self .fmt [ch ])
494
494
# add 1 because the lowest value is used to store nans
@@ -677,7 +677,7 @@ def rd_segment(file_name, dirname, pb_dir, n_sig, fmt, sig_len, byte_offset,
677
677
skew = skew [:]
678
678
679
679
# Set defaults for empty fields
680
- for i in range (0 , n_sig ):
680
+ for i in range (n_sig ):
681
681
if byte_offset [i ] == None :
682
682
byte_offset [i ] = 0
683
683
if samps_per_frame [i ] == None :
@@ -727,7 +727,7 @@ def rd_segment(file_name, dirname, pb_dir, n_sig, fmt, sig_len, byte_offset,
727
727
# Return uniform numpy array
728
728
if smooth_frames or sum (samps_per_frame ) == n_sig :
729
729
# Figure out the largest required dtype for the segment to minimize memory usage
730
- max_dtype = npdtype ( wfdbfmtres (fmt , maxres = True ), discrete = True )
730
+ max_dtype = np_dtype ( fmt_res (fmt , maxres = True ), discrete = True )
731
731
# Allocate signal array. Minimize dtype
732
732
signals = np .zeros ([sampto - sampfrom , len (channels )], dtype = max_dtype )
733
733
@@ -1249,7 +1249,7 @@ def est_res(signals):
1249
1249
res : list
1250
1250
A list of estimated integer resolutions for each channel
1251
1251
"""
1252
- res_levels = np .power (2 , np .arange (0 ,33 ))
1252
+ res_levels = np .power (2 , np .arange (0 , 33 ))
1253
1253
# Expanded sample signals. List of numpy arrays
1254
1254
if isinstance (signals , list ):
1255
1255
n_sig = len (signals )
@@ -1315,11 +1315,13 @@ def wfdbfmt(res, single_fmt=True):
1315
1315
else :
1316
1316
return '32'
1317
1317
1318
- # Return the resolution of the WFDB format(s).
1319
- def wfdbfmtres (fmt , maxres = False ):
1320
1318
1319
+ def fmt_res (fmt , maxres = False ):
1320
+ """
1321
+ Return the resolution of the WFDB format(s).
1322
+ """
1321
1323
if isinstance (fmt , list ):
1322
- res = [wfdbfmtres (f ) for f in fmt ]
1324
+ res = [fmt_res (f ) for f in fmt ]
1323
1325
if maxres is True :
1324
1326
res = np .max (res )
1325
1327
return res
@@ -1339,21 +1341,33 @@ def wfdbfmtres(fmt, maxres=False):
1339
1341
else :
1340
1342
raise ValueError ('Invalid WFDB format.' )
1341
1343
1342
- # Given the resolution of a signal, return the minimum
1343
- # dtype to store it
1344
- def npdtype (res , discrete ):
1345
1344
1346
- if not hasattr (res , '__index__' ) or res > 64 :
1347
- raise TypeError ('res must be integer based and <=64' )
1345
+ def np_dtype (res , discrete ):
1346
+ """
1347
+ Given the resolution of a signal, return the minimum
1348
+ dtype to store it
1349
+
1350
+ Parameters
1351
+ ----------
1352
+ res : int
1353
+ The resolution.
1354
+ discrete : bool
1355
+ Whether the dtype is to be discrete or floating.
1356
+ """
1357
+ if not hasattr (res , '__index__' ) or res > 64 :
1358
+ raise TypeError ('res must be integer based and <= 64' )
1348
1359
1349
- for npres in [8 , 16 , 32 , 64 ]:
1350
- if res <= npres :
1360
+ for np_res in [8 , 16 , 32 , 64 ]:
1361
+ if res <= np_res :
1351
1362
break
1352
1363
1353
1364
if discrete is True :
1354
- return 'int' + str (npres )
1365
+ return 'int' + str (np_res )
1355
1366
else :
1356
- return 'float' + str (npres )
1367
+ # No float8 dtype
1368
+ if np_res == 8 :
1369
+ np_res = 16
1370
+ return 'float' + str (np_res )
1357
1371
1358
1372
1359
1373
def wr_dat_file (file_name , fmt , d_signal , byte_offset , expanded = False ,
0 commit comments