Skip to content

Commit 71d3366

Browse files
committed
working on fixing layout specification header
1 parent 1831d87 commit 71d3366

File tree

3 files changed

+139
-70
lines changed

3 files changed

+139
-70
lines changed

wfdb/io/_header.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ def set_default(self, field):
377377

378378
# Set more specific defaults if possible
379379
if field == 'adc_res' and self.fmt is not None:
380-
self.adc_res = _signal.wfdbfmtres(self.fmt)
380+
self.adc_res = _signal.fmt_res(self.fmt)
381381
return
382382

383383
setattr(self, field,

wfdb/io/_signal.py

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44

55
from . import download
6-
6+
import pdb
77
# WFDB dat formats - https://www.physionet.org/physiotools/wag/signal-5.htm
88
SIMPLE_FMTS = ['80', '16', '24', '32']
99
SPECIAL_FMTS = ['212', '310', '311']
@@ -65,7 +65,7 @@ def check_sig_cohesion(self, write_fields, expanded):
6565
raise ValueError('Length of channel '+str(ch)+'does not match samps_per_frame['+str(ch+']*sig_len'))
6666

6767
# 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):
6969
fmt = self.fmt[ch]
7070
dmin, dmax = digi_bounds(self.fmt[ch])
7171

@@ -97,7 +97,7 @@ def check_sig_cohesion(self, write_fields, expanded):
9797
raise ValueError('sig_len and n_sig do not match shape of d_signal')
9898

9999
# 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):
101101
fmt = self.fmt[ch]
102102
dmin, dmax = digi_bounds(self.fmt[ch])
103103

@@ -327,7 +327,7 @@ def adc(self, expanded=False, inplace=False):
327327
# Do inplace conversion and set relevant variables.
328328
if inplace:
329329
if expanded:
330-
for ch in range(0, self.n_sig):
330+
for ch in range(self.n_sig):
331331
# nan locations for the channel
332332
ch_nanlocs = np.isnan(self.e_p_signal[ch])
333333
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):
348348
else:
349349
if expanded:
350350
d_signal = []
351-
for ch in range(0, self.n_sig):
351+
for ch in range(self.n_sig):
352352
# nan locations for the channel
353353
ch_nanlocs = np.isnan(self.e_p_signal[ch])
354354
ch_d_signal = self.e_p_signal.copy()
@@ -428,7 +428,7 @@ def dac(self, expanded=False, return_res=64, inplace=False):
428428
# Do inplace conversion and set relevant variables.
429429
if inplace:
430430
if expanded:
431-
for ch in range(0, self.n_sig):
431+
for ch in range(self.n_sig):
432432
# nan locations for the channel
433433
ch_nanlocs = self.e_d_signal[ch] == dnans[ch]
434434
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):
452452
else:
453453
if expanded:
454454
p_signal = []
455-
for ch in range(0, self.n_sig):
455+
for ch in range(self.n_sig):
456456
# nan locations for the channel
457457
ch_nanlocs = self.e_d_signal[ch] == dnans[ch]
458458
ch_p_signal = self.e_d_signal[ch].astype(floatdtype, copy=False)
@@ -488,7 +488,7 @@ def calc_adc_params(self):
488488

489489
dnans = digi_nan(self.fmt)
490490

491-
for ch in range(0, np.shape(self.p_signal)[1]):
491+
for ch in range(np.shape(self.p_signal)[1]):
492492
# Get the minimum and maximum (valid) storage values
493493
dmin, dmax = digi_bounds(self.fmt[ch])
494494
# 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,
677677
skew = skew[:]
678678

679679
# Set defaults for empty fields
680-
for i in range(0, n_sig):
680+
for i in range(n_sig):
681681
if byte_offset[i] == None:
682682
byte_offset[i] = 0
683683
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,
727727
# Return uniform numpy array
728728
if smooth_frames or sum(samps_per_frame) == n_sig:
729729
# 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)
731731
# Allocate signal array. Minimize dtype
732732
signals = np.zeros([sampto-sampfrom, len(channels)], dtype=max_dtype)
733733

@@ -1249,7 +1249,7 @@ def est_res(signals):
12491249
res : list
12501250
A list of estimated integer resolutions for each channel
12511251
"""
1252-
res_levels = np.power(2, np.arange(0,33))
1252+
res_levels = np.power(2, np.arange(0, 33))
12531253
# Expanded sample signals. List of numpy arrays
12541254
if isinstance(signals, list):
12551255
n_sig = len(signals)
@@ -1315,11 +1315,13 @@ def wfdbfmt(res, single_fmt=True):
13151315
else:
13161316
return '32'
13171317

1318-
# Return the resolution of the WFDB format(s).
1319-
def wfdbfmtres(fmt, maxres=False):
13201318

1319+
def fmt_res(fmt, maxres=False):
1320+
"""
1321+
Return the resolution of the WFDB format(s).
1322+
"""
13211323
if isinstance(fmt, list):
1322-
res = [wfdbfmtres(f) for f in fmt]
1324+
res = [fmt_res(f) for f in fmt]
13231325
if maxres is True:
13241326
res = np.max(res)
13251327
return res
@@ -1339,21 +1341,33 @@ def wfdbfmtres(fmt, maxres=False):
13391341
else:
13401342
raise ValueError('Invalid WFDB format.')
13411343

1342-
# Given the resolution of a signal, return the minimum
1343-
# dtype to store it
1344-
def npdtype(res, discrete):
13451344

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')
13481359

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:
13511362
break
13521363

13531364
if discrete is True:
1354-
return 'int'+str(npres)
1365+
return 'int' + str(np_res)
13551366
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)
13571371

13581372

13591373
def wr_dat_file(file_name, fmt, d_signal, byte_offset, expanded=False,

0 commit comments

Comments
 (0)