Skip to content

Commit db6d5a0

Browse files
committed
improve readability
1 parent 275b69e commit db6d5a0

File tree

1 file changed

+83
-79
lines changed

1 file changed

+83
-79
lines changed

wfdb/io/_signal.py

Lines changed: 83 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ def rd_segment(file_name, dir_name, pb_dir, n_sig, fmt, sig_len, byte_offset,
818818

819819
# Read each wanted dat file and store signals
820820
for fn in w_file_name:
821-
signals[:, out_dat_channel[fn]] = rddat(fn, dir_name, pb_dir,
821+
signals[:, out_dat_channel[fn]] = rd_dat_signals(fn, dir_name, pb_dir,
822822
w_fmt[fn], len(datchannel[fn]), sig_len, w_byte_offset[fn],
823823
w_samps_per_frame[fn], w_skew[fn], sampfrom, sampto,
824824
smooth_frames)[:, r_w_channel[fn]]
@@ -830,7 +830,7 @@ def rd_segment(file_name, dir_name, pb_dir, n_sig, fmt, sig_len, byte_offset,
830830

831831
for fn in w_file_name:
832832
# Get the list of all signals contained in the dat file
833-
datsignals = rddat(fn, dir_name, pb_dir, w_fmt[fn], len(datchannel[fn]),
833+
datsignals = rd_dat_signals(fn, dir_name, pb_dir, w_fmt[fn], len(datchannel[fn]),
834834
sig_len, w_byte_offset[fn], w_samps_per_frame[fn], w_skew[fn], sampfrom, sampto, smooth_frames)
835835

836836
# Copy over the wanted signals
@@ -840,11 +840,11 @@ def rd_segment(file_name, dir_name, pb_dir, n_sig, fmt, sig_len, byte_offset,
840840
return signals
841841

842842

843-
def rddat(file_name, dir_name, pb_dir, fmt, n_sig,
844-
sig_len, byte_offset, samps_per_frame,
845-
skew, sampfrom, sampto, smooth_frames):
843+
def rd_dat_signals(file_name, dir_name, pb_dir, fmt, n_sig, sig_len,
844+
byte_offset, samps_per_frame, skew, sampfrom, sampto,
845+
smooth_frames):
846846
"""
847-
Read samples from a WFDB dat file. Returns all channels
847+
Read signals from a WFDB dat file. Returns all channels.
848848
849849
850850
Parameters
@@ -863,7 +863,7 @@ def rddat(file_name, dir_name, pb_dir, fmt, n_sig,
863863
The number of signals contained in the dat file
864864
sig_len : int
865865
The signal length (per channel) of the dat file
866-
byte_offset : list
866+
byte_offset : int
867867
The byte offset of the dat file
868868
samps_per_frame : list
869869
The samples/frame for each signal of the dat file
@@ -894,7 +894,7 @@ def rddat(file_name, dir_name, pb_dir, fmt, n_sig,
894894
read_len = sampto - sampfrom
895895

896896
# Calculate parameters used to read and process the dat file
897-
start_byte, n_read_samples, block_floor_samples, extra_flat_samples, nanreplace = _dat_read_params(fmt, sig_len, byte_offset,
897+
start_byte, n_read_samples, block_floor_samples, extra_flat_samples, nan_replace = _dat_read_params(fmt, sig_len, byte_offset,
898898
skew, tsamps_per_frame,
899899
sampfrom, sampto)
900900

@@ -929,7 +929,7 @@ def rddat(file_name, dir_name, pb_dir, fmt, n_sig,
929929

930930
# For special fmts, Turn the bytes into actual samples
931931
if fmt in SPECIAL_FMTS:
932-
sig_bytes = bytes_to_samples(sig_bytes, total_process_samples, fmt)
932+
sig_bytes = _bytes_to_samples(sig_bytes, total_process_samples, fmt)
933933
# Remove extra leading sample read within the byte block if any
934934
if block_floor_samples:
935935
sig_bytes = sig_bytes[block_floor_samples:]
@@ -947,7 +947,7 @@ def rddat(file_name, dir_name, pb_dir, fmt, n_sig,
947947
# Reshape into multiple channels
948948
sig = sig_bytes.reshape(-1, n_sig)
949949
# Skew the signal
950-
sig = _skew_sig(sig, skew, n_sig, read_len, fmt, nanreplace)
950+
sig = _skew_sig(sig, skew, n_sig, read_len, fmt, nan_replace)
951951

952952
# Extra frames present to be smoothed. Obtain averaged uniform numpy array
953953
elif smooth_frames:
@@ -966,7 +966,7 @@ def rddat(file_name, dir_name, pb_dir, fmt, n_sig,
966966
startind = np.sum(samps_per_frame[:ch])
967967
sig[:,ch] = [np.average(sig_bytes[ind:ind+samps_per_frame[ch]]) for ind in range(startind,len(sig_bytes),tsamps_per_frame)]
968968
# Skew the signal
969-
sig = _skew_sig(sig, skew, n_sig, read_len, fmt, nanreplace)
969+
sig = _skew_sig(sig, skew, n_sig, read_len, fmt, nan_replace)
970970

971971
# Extra frames present without wanting smoothing. Return all expanded samples.
972972
else:
@@ -978,7 +978,7 @@ def rddat(file_name, dir_name, pb_dir, fmt, n_sig,
978978
ch_indices = np.concatenate([np.array(range(samps_per_frame[ch])) + sum([0]+samps_per_frame[:ch]) + tsamps_per_frame*framenum for framenum in range(int(len(sig_bytes)/tsamps_per_frame))])
979979
sig.append(sig_bytes[ch_indices])
980980
# Skew the signal
981-
sig = _skew_sig(sig, skew, n_sig, read_len, fmt, nanreplace, samps_per_frame)
981+
sig = _skew_sig(sig, skew, n_sig, read_len, fmt, nan_replace, samps_per_frame)
982982

983983
# Integrity check of signal shape after reading
984984
_check_sig_dims(sig, read_len, n_sig, samps_per_frame)
@@ -992,43 +992,46 @@ def _dat_read_params(fmt, sig_len, byte_offset, skew, tsamps_per_frame,
992992
Calculate the parameters used to read and process a dat file, given
993993
its layout, and the desired sample range.
994994
995-
Returns
996-
-------
997-
start_byte
998-
The starting byte to read the dat file from. Always points to the start of a
999-
byte block for special formats.
1000-
n_read_samples
1001-
The number of flat samples to read from the dat file.
1002-
block_floor_samples
1003-
The extra samples read prior to the first desired sample, for special
1004-
formats in order to ensure entire byte blocks are read.
1005-
extra_flat_samples
1006-
The extra samples desired beyond what is contained in the file.
1007-
nanreplace
1008-
The number of samples to replace with nan at the end of each signal
1009-
due to skew wanting samples beyond the file
995+
Parameters
996+
----------
997+
fmt : str
998+
The format of the dat file
999+
sig_len : int
1000+
The signal length (per channel) of the dat file
1001+
byte_offset : int
1002+
The byte offset of the dat file
1003+
skew : list
1004+
The skew for the signals of the dat file
1005+
tsamps_per_frame : int
1006+
The total samples/frame for all channels of the dat file
1007+
sampfrom : int
1008+
The starting sample number to be read from the signals
1009+
sampto : int
1010+
The final sample number to be read from the signals
10101011
10111012
Returns
10121013
-------
10131014
start_byte : int
1014-
The starting byte to read from
1015+
The starting byte to read the dat file from. Always points to
1016+
the start of a byte block for special formats.
10151017
n_read_samples : int
1016-
1017-
1018-
block_floor_samples
1019-
1020-
extra_flat_samples
1021-
1022-
nanreplace)
1023-
1018+
The number of flat samples to read from the dat file.
1019+
block_floor_samples : int
1020+
The extra samples read prior to the first desired sample, for
1021+
special formats, in order to ensure entire byte blocks are read.
1022+
extra_flat_samples : int
1023+
The extra samples desired beyond what is contained in the file.
1024+
nan_replace : list
1025+
The number of samples to replace with nan at the end of each
1026+
signal, due to skew wanting samples beyond the file.
10241027
10251028
Examples
10261029
--------
10271030
sig_len=100, t = 4 (total samples/frame), skew = [0, 2, 4, 5]
1028-
sampfrom=0, sampto=100 --> read_len = 100, n_sampread = 100*t, extralen = 5, nanreplace = [0, 2, 4, 5]
1029-
sampfrom=50, sampto=100 --> read_len = 50, n_sampread = 50*t, extralen = 5, nanreplace = [0, 2, 4, 5]
1030-
sampfrom=0, sampto=50 --> read_len = 50, n_sampread = 55*t, extralen = 0, nanreplace = [0, 0, 0, 0]
1031-
sampfrom=95, sampto=99 --> read_len = 4, n_sampread = 5*t, extralen = 4, nanreplace = [0, 1, 3, 4]
1031+
sampfrom=0, sampto=100 --> read_len = 100, n_sampread = 100*t, extralen = 5, nan_replace = [0, 2, 4, 5]
1032+
sampfrom=50, sampto=100 --> read_len = 50, n_sampread = 50*t, extralen = 5, nan_replace = [0, 2, 4, 5]
1033+
sampfrom=0, sampto=50 --> read_len = 50, n_sampread = 55*t, extralen = 0, nan_replace = [0, 0, 0, 0]
1034+
sampfrom=95, sampto=99 --> read_len = 4, n_sampread = 5*t, extralen = 4, nan_replace = [0, 1, 3, 4]
10321035
10331036
"""
10341037

@@ -1067,12 +1070,10 @@ def _dat_read_params(fmt, sig_len, byte_offset, skew, tsamps_per_frame,
10671070

10681071
# The number of samples to replace with nan at the end of each signal
10691072
# due to skew wanting samples beyond the file
1070-
1071-
# Calculate this using the above statement case: if (sampto + max(skew))>sig_len:
1072-
nanreplace = [max(0, sampto + s - sig_len) for s in skew]
1073+
nan_replace = [max(0, sampto + s - sig_len) for s in skew]
10731074

10741075
return (start_byte, n_read_samples, block_floor_samples,
1075-
extra_flat_samples, nanreplace)
1076+
extra_flat_samples, nan_replace)
10761077

10771078

10781079
def _required_byte_num(mode, fmt, n_samp):
@@ -1130,10 +1131,8 @@ def _rd_dat_file(file_name, dir_name, pb_dir, fmt, start_byte, n_samp):
11301131
Read data from a dat file, either local or remote, into a 1d numpy
11311132
array.
11321133
1133-
Slightly misleading function name. Does not return bytes object.
1134-
11351134
This is the lowest level dat reading function, and is called by
1136-
`rddat`.
1135+
`rd_dat_signals`.
11371136
11381137
Parameters
11391138
----------
@@ -1162,17 +1161,15 @@ def _rd_dat_file(file_name, dir_name, pb_dir, fmt, start_byte, n_samp):
11621161
11631162
Notes
11641163
-----
1165-
`n_samp` and `start_byte` should make it so that the bytes are read
1166-
from the start of a byte block, even if sampfrom points into the middle of one.
1167-
1168-
This will not
1169-
be checked here. `_dat_read_params` should ensure it.
1164+
`n_samp` and `start_byte` should be such that the bytes are read
1165+
from the start of a byte block. This will not be checked here.
1166+
`_dat_read_params` should ensure it.
11701167
11711168
"""
11721169

1173-
# element_count is the number of elements to read using np.fromfile (for local files)
1170+
# element_count is the number of elements to read using np.fromfile
1171+
# (for local files)
11741172
# byte_count is the number of bytes to read (for streaming files)
1175-
11761173
if fmt == '212':
11771174
byte_count = _required_byte_num('read', '212', n_samp)
11781175
element_count = byte_count
@@ -1198,26 +1195,32 @@ def _rd_dat_file(file_name, dir_name, pb_dir, fmt, start_byte, n_samp):
11981195
return sig_bytes
11991196

12001197

1201-
def bytes_to_samples(sig_bytes, n_samp, fmt):
1198+
def _bytes_to_samples(sig_bytes, n_samp, fmt):
12021199
"""
12031200
Convert uint8 blocks into samples for special dat formats.
12041201
12051202
Parameters
12061203
----------
12071204
sig_bytes : numpy array
1208-
The uint8 data blocks
1205+
The uint8 data blocks. Note, not a bytes object, but a numpy
1206+
array with dtype='uint8'
12091207
n_samp : int
1208+
The number of samples contained in the bytes
12101209
1210+
Returns
1211+
-------
1212+
signal : numpy array
1213+
The numpy array of digital samples
12111214
12121215
"""
12131216
if fmt == '212':
12141217
# Easier to process when dealing with whole blocks
12151218
if n_samp % 2:
1216-
n_samp = n_samp + 1
1217-
addedsamps = 1
1219+
n_samp += 1
1220+
added_samps = 1
12181221
sig_bytes = np.append(sig_bytes, np.zeros(1, dtype='uint8'))
12191222
else:
1220-
addedsamps = 0
1223+
added_samps = 0
12211224

12221225
sig_bytes = sig_bytes.astype('int16')
12231226
sig = np.zeros(n_samp, dtype='int16')
@@ -1229,9 +1232,10 @@ def bytes_to_samples(sig_bytes, n_samp, fmt):
12291232
# Odd numbered samples (len(sig) always >1 due to processing of whole blocks)
12301233
sig[1::2] = sig_bytes[2::3] + 256*np.bitwise_and(sig_bytes[1::3] >> 4, 0x0f)
12311234

1232-
# Remove trailing sample read within the byte block if originally odd sampled
1233-
if addedsamps:
1234-
sig = sig[:-addedsamps]
1235+
# Remove trailing sample read within the byte block if
1236+
# originally odd sampled
1237+
if added_samps:
1238+
sig = sig[:-added_samps]
12351239

12361240
# Loaded values as un_signed. Convert to 2's complement form:
12371241
# values > 2^11-1 are negative.
@@ -1241,10 +1245,10 @@ def bytes_to_samples(sig_bytes, n_samp, fmt):
12411245
# Easier to process when dealing with whole blocks
12421246
if n_samp % 3:
12431247
n_samp = upround(n_samp,3)
1244-
addedsamps = n_samp % 3
1245-
sig_bytes = np.append(sig_bytes, np.zeros(addedsamps, dtype='uint8'))
1248+
added_samps = n_samp % 3
1249+
sig_bytes = np.append(sig_bytes, np.zeros(added_samps, dtype='uint8'))
12461250
else:
1247-
addedsamps = 0
1251+
added_samps = 0
12481252

12491253
sig_bytes = sig_bytes.astype('int16')
12501254
sig = np.zeros(n_samp, dtype='int16')
@@ -1258,8 +1262,8 @@ def bytes_to_samples(sig_bytes, n_samp, fmt):
12581262
sig[2::3] = np.bitwise_and((sig_bytes[1::4] >> 3), 0x1f)[0:len(sig[2::3])] + 32 * np.bitwise_and(sig_bytes[3::4] >> 3, 0x1f)[0:len(sig[2::3])]
12591263

12601264
# Remove trailing samples read within the byte block if originally not 3n sampled
1261-
if addedsamps:
1262-
sig = sig[:-addedsamps]
1265+
if added_samps:
1266+
sig = sig[:-added_samps]
12631267

12641268
# Loaded values as un_signed. Convert to 2's complement form:
12651269
# values > 2^9-1 are negative.
@@ -1269,10 +1273,10 @@ def bytes_to_samples(sig_bytes, n_samp, fmt):
12691273
# Easier to process when dealing with whole blocks
12701274
if n_samp % 3:
12711275
n_samp = upround(n_samp,3)
1272-
addedsamps = n_samp % 3
1273-
sig_bytes = np.append(sig_bytes, np.zeros(addedsamps, dtype='uint8'))
1276+
added_samps = n_samp % 3
1277+
sig_bytes = np.append(sig_bytes, np.zeros(added_samps, dtype='uint8'))
12741278
else:
1275-
addedsamps = 0
1279+
added_samps = 0
12761280

12771281
sig_bytes = sig_bytes.astype('int16')
12781282
sig = np.zeros(n_samp, dtype='int16')
@@ -1286,16 +1290,16 @@ def bytes_to_samples(sig_bytes, n_samp, fmt):
12861290
sig[2::3] = (sig_bytes[2::4] >> 4)[0:len(sig[2::3])] + 16 * np.bitwise_and(sig_bytes[3::4], 0x7f)[0:len(sig[2::3])]
12871291

12881292
# Remove trailing samples read within the byte block if originally not 3n sampled
1289-
if addedsamps:
1290-
sig = sig[:-addedsamps]
1293+
if added_samps:
1294+
sig = sig[:-added_samps]
12911295

1292-
# Loaded values as un_signed. Convert to 2's complement form:
1293-
# values > 2^9-1 are negative.
1296+
# Loaded values as un_signed. Convert to 2's complement form.
1297+
# Values > 2^9-1 are negative.
12941298
sig[sig > 511] -= 1024
12951299
return sig
12961300

12971301

1298-
def _skew_sig(sig, skew, n_sig, read_len, fmt, nanreplace, samps_per_frame=None):
1302+
def _skew_sig(sig, skew, n_sig, read_len, fmt, nan_replace, samps_per_frame=None):
12991303
"""
13001304
Skew the signal, insert nans and shave off end of array if needed.
13011305
@@ -1330,8 +1334,8 @@ def _skew_sig(sig, skew, n_sig, read_len, fmt, nanreplace, samps_per_frame=None)
13301334

13311335
# Insert nans where skewed signal overran dat file
13321336
for ch in range(n_sig):
1333-
if nanreplace[ch]>0:
1334-
sig[ch][-nanreplace[ch]:] = digi_nan(fmt)
1337+
if nan_replace[ch]>0:
1338+
sig[ch][-nan_replace[ch]:] = digi_nan(fmt)
13351339
# Uniform array
13361340
else:
13371341
# Shift the channel samples
@@ -1343,8 +1347,8 @@ def _skew_sig(sig, skew, n_sig, read_len, fmt, nanreplace, samps_per_frame=None)
13431347

13441348
# Insert nans where skewed signal overran dat file
13451349
for ch in range(n_sig):
1346-
if nanreplace[ch]>0:
1347-
sig[-nanreplace[ch]:, ch] = digi_nan(fmt)
1350+
if nan_replace[ch]>0:
1351+
sig[-nan_replace[ch]:, ch] = digi_nan(fmt)
13481352

13491353
return sig
13501354

0 commit comments

Comments
 (0)