@@ -818,7 +818,7 @@ def rd_segment(file_name, dir_name, pb_dir, n_sig, fmt, sig_len, byte_offset,
818
818
819
819
# Read each wanted dat file and store signals
820
820
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 ,
822
822
w_fmt [fn ], len (datchannel [fn ]), sig_len , w_byte_offset [fn ],
823
823
w_samps_per_frame [fn ], w_skew [fn ], sampfrom , sampto ,
824
824
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,
830
830
831
831
for fn in w_file_name :
832
832
# 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 ]),
834
834
sig_len , w_byte_offset [fn ], w_samps_per_frame [fn ], w_skew [fn ], sampfrom , sampto , smooth_frames )
835
835
836
836
# 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,
840
840
return signals
841
841
842
842
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 ):
846
846
"""
847
- Read samples from a WFDB dat file. Returns all channels
847
+ Read signals from a WFDB dat file. Returns all channels.
848
848
849
849
850
850
Parameters
@@ -863,7 +863,7 @@ def rddat(file_name, dir_name, pb_dir, fmt, n_sig,
863
863
The number of signals contained in the dat file
864
864
sig_len : int
865
865
The signal length (per channel) of the dat file
866
- byte_offset : list
866
+ byte_offset : int
867
867
The byte offset of the dat file
868
868
samps_per_frame : list
869
869
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,
894
894
read_len = sampto - sampfrom
895
895
896
896
# 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 ,
898
898
skew , tsamps_per_frame ,
899
899
sampfrom , sampto )
900
900
@@ -929,7 +929,7 @@ def rddat(file_name, dir_name, pb_dir, fmt, n_sig,
929
929
930
930
# For special fmts, Turn the bytes into actual samples
931
931
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 )
933
933
# Remove extra leading sample read within the byte block if any
934
934
if block_floor_samples :
935
935
sig_bytes = sig_bytes [block_floor_samples :]
@@ -947,7 +947,7 @@ def rddat(file_name, dir_name, pb_dir, fmt, n_sig,
947
947
# Reshape into multiple channels
948
948
sig = sig_bytes .reshape (- 1 , n_sig )
949
949
# 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 )
951
951
952
952
# Extra frames present to be smoothed. Obtain averaged uniform numpy array
953
953
elif smooth_frames :
@@ -966,7 +966,7 @@ def rddat(file_name, dir_name, pb_dir, fmt, n_sig,
966
966
startind = np .sum (samps_per_frame [:ch ])
967
967
sig [:,ch ] = [np .average (sig_bytes [ind :ind + samps_per_frame [ch ]]) for ind in range (startind ,len (sig_bytes ),tsamps_per_frame )]
968
968
# 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 )
970
970
971
971
# Extra frames present without wanting smoothing. Return all expanded samples.
972
972
else :
@@ -978,7 +978,7 @@ def rddat(file_name, dir_name, pb_dir, fmt, n_sig,
978
978
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 ))])
979
979
sig .append (sig_bytes [ch_indices ])
980
980
# 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 )
982
982
983
983
# Integrity check of signal shape after reading
984
984
_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,
992
992
Calculate the parameters used to read and process a dat file, given
993
993
its layout, and the desired sample range.
994
994
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
1010
1011
1011
1012
Returns
1012
1013
-------
1013
1014
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.
1015
1017
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.
1024
1027
1025
1028
Examples
1026
1029
--------
1027
1030
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]
1032
1035
1033
1036
"""
1034
1037
@@ -1067,12 +1070,10 @@ def _dat_read_params(fmt, sig_len, byte_offset, skew, tsamps_per_frame,
1067
1070
1068
1071
# The number of samples to replace with nan at the end of each signal
1069
1072
# 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 ]
1073
1074
1074
1075
return (start_byte , n_read_samples , block_floor_samples ,
1075
- extra_flat_samples , nanreplace )
1076
+ extra_flat_samples , nan_replace )
1076
1077
1077
1078
1078
1079
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):
1130
1131
Read data from a dat file, either local or remote, into a 1d numpy
1131
1132
array.
1132
1133
1133
- Slightly misleading function name. Does not return bytes object.
1134
-
1135
1134
This is the lowest level dat reading function, and is called by
1136
- `rddat `.
1135
+ `rd_dat_signals `.
1137
1136
1138
1137
Parameters
1139
1138
----------
@@ -1162,17 +1161,15 @@ def _rd_dat_file(file_name, dir_name, pb_dir, fmt, start_byte, n_samp):
1162
1161
1163
1162
Notes
1164
1163
-----
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.
1170
1167
1171
1168
"""
1172
1169
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)
1174
1172
# byte_count is the number of bytes to read (for streaming files)
1175
-
1176
1173
if fmt == '212' :
1177
1174
byte_count = _required_byte_num ('read' , '212' , n_samp )
1178
1175
element_count = byte_count
@@ -1198,26 +1195,32 @@ def _rd_dat_file(file_name, dir_name, pb_dir, fmt, start_byte, n_samp):
1198
1195
return sig_bytes
1199
1196
1200
1197
1201
- def bytes_to_samples (sig_bytes , n_samp , fmt ):
1198
+ def _bytes_to_samples (sig_bytes , n_samp , fmt ):
1202
1199
"""
1203
1200
Convert uint8 blocks into samples for special dat formats.
1204
1201
1205
1202
Parameters
1206
1203
----------
1207
1204
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'
1209
1207
n_samp : int
1208
+ The number of samples contained in the bytes
1210
1209
1210
+ Returns
1211
+ -------
1212
+ signal : numpy array
1213
+ The numpy array of digital samples
1211
1214
1212
1215
"""
1213
1216
if fmt == '212' :
1214
1217
# Easier to process when dealing with whole blocks
1215
1218
if n_samp % 2 :
1216
- n_samp = n_samp + 1
1217
- addedsamps = 1
1219
+ n_samp += 1
1220
+ added_samps = 1
1218
1221
sig_bytes = np .append (sig_bytes , np .zeros (1 , dtype = 'uint8' ))
1219
1222
else :
1220
- addedsamps = 0
1223
+ added_samps = 0
1221
1224
1222
1225
sig_bytes = sig_bytes .astype ('int16' )
1223
1226
sig = np .zeros (n_samp , dtype = 'int16' )
@@ -1229,9 +1232,10 @@ def bytes_to_samples(sig_bytes, n_samp, fmt):
1229
1232
# Odd numbered samples (len(sig) always >1 due to processing of whole blocks)
1230
1233
sig [1 ::2 ] = sig_bytes [2 ::3 ] + 256 * np .bitwise_and (sig_bytes [1 ::3 ] >> 4 , 0x0f )
1231
1234
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 ]
1235
1239
1236
1240
# Loaded values as un_signed. Convert to 2's complement form:
1237
1241
# values > 2^11-1 are negative.
@@ -1241,10 +1245,10 @@ def bytes_to_samples(sig_bytes, n_samp, fmt):
1241
1245
# Easier to process when dealing with whole blocks
1242
1246
if n_samp % 3 :
1243
1247
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' ))
1246
1250
else :
1247
- addedsamps = 0
1251
+ added_samps = 0
1248
1252
1249
1253
sig_bytes = sig_bytes .astype ('int16' )
1250
1254
sig = np .zeros (n_samp , dtype = 'int16' )
@@ -1258,8 +1262,8 @@ def bytes_to_samples(sig_bytes, n_samp, fmt):
1258
1262
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 ])]
1259
1263
1260
1264
# 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 ]
1263
1267
1264
1268
# Loaded values as un_signed. Convert to 2's complement form:
1265
1269
# values > 2^9-1 are negative.
@@ -1269,10 +1273,10 @@ def bytes_to_samples(sig_bytes, n_samp, fmt):
1269
1273
# Easier to process when dealing with whole blocks
1270
1274
if n_samp % 3 :
1271
1275
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' ))
1274
1278
else :
1275
- addedsamps = 0
1279
+ added_samps = 0
1276
1280
1277
1281
sig_bytes = sig_bytes .astype ('int16' )
1278
1282
sig = np .zeros (n_samp , dtype = 'int16' )
@@ -1286,16 +1290,16 @@ def bytes_to_samples(sig_bytes, n_samp, fmt):
1286
1290
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 ])]
1287
1291
1288
1292
# 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 ]
1291
1295
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.
1294
1298
sig [sig > 511 ] -= 1024
1295
1299
return sig
1296
1300
1297
1301
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 ):
1299
1303
"""
1300
1304
Skew the signal, insert nans and shave off end of array if needed.
1301
1305
@@ -1330,8 +1334,8 @@ def _skew_sig(sig, skew, n_sig, read_len, fmt, nanreplace, samps_per_frame=None)
1330
1334
1331
1335
# Insert nans where skewed signal overran dat file
1332
1336
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 )
1335
1339
# Uniform array
1336
1340
else :
1337
1341
# Shift the channel samples
@@ -1343,8 +1347,8 @@ def _skew_sig(sig, skew, n_sig, read_len, fmt, nanreplace, samps_per_frame=None)
1343
1347
1344
1348
# Insert nans where skewed signal overran dat file
1345
1349
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 )
1348
1352
1349
1353
return sig
1350
1354
0 commit comments