@@ -737,7 +737,7 @@ def smooth_frames(self, sigtype='physical'):
737
737
738
738
#------------------- Reading Signals -------------------#
739
739
740
- def rd_segment (file_name , dirname , pb_dir , n_sig , fmt , sig_len , byte_offset ,
740
+ def rd_segment (file_name , dir_name , pb_dir , n_sig , fmt , sig_len , byte_offset ,
741
741
samps_per_frame , skew , sampfrom , sampto , channels ,
742
742
smooth_frames , ignore_skew ):
743
743
"""
@@ -809,7 +809,7 @@ def rd_segment(file_name, dirname, pb_dir, n_sig, fmt, sig_len, byte_offset,
809
809
810
810
# Read each wanted dat file and store signals
811
811
for fn in w_file_name :
812
- signals [:, out_datchannel [fn ]] = rddat (fn , dirname , pb_dir , w_fmt [fn ], len (datchannel [fn ]),
812
+ signals [:, out_datchannel [fn ]] = rddat (fn , dir_name , pb_dir , w_fmt [fn ], len (datchannel [fn ]),
813
813
sig_len , w_byte_offset [fn ], w_samps_per_frame [fn ], w_skew [fn ], sampfrom , sampto , smooth_frames )[:, r_w_channel [fn ]]
814
814
815
815
# Return each sample in signals with multiple samples/frame, without smoothing.
@@ -819,7 +819,7 @@ def rd_segment(file_name, dirname, pb_dir, n_sig, fmt, sig_len, byte_offset,
819
819
820
820
for fn in w_file_name :
821
821
# Get the list of all signals contained in the dat file
822
- datsignals = rddat (fn , dirname , pb_dir , w_fmt [fn ], len (datchannel [fn ]),
822
+ datsignals = rddat (fn , dir_name , pb_dir , w_fmt [fn ], len (datchannel [fn ]),
823
823
sig_len , w_byte_offset [fn ], w_samps_per_frame [fn ], w_skew [fn ], sampfrom , sampto , smooth_frames )
824
824
825
825
# Copy over the wanted signals
@@ -829,35 +829,58 @@ def rd_segment(file_name, dirname, pb_dir, n_sig, fmt, sig_len, byte_offset,
829
829
return signals
830
830
831
831
832
- def rddat (file_name , dirname , pb_dir , fmt , n_sig ,
832
+ def rddat (file_name , dir_name , pb_dir , fmt , n_sig ,
833
833
sig_len , byte_offset , samps_per_frame ,
834
834
skew , sampfrom , sampto , smooth_frames ):
835
835
"""
836
- Get samples from a WFDB dat file.
837
- 'sampfrom', 'sampto', and smooth_frames are user desired
836
+ Read samples from a WFDB dat file. Returns all channels
837
+
838
+
839
+ Parameters
840
+ ----------
841
+ file_name : str
842
+ The name of the dat file.
843
+ dir_name : str
844
+ The full directory where the dat file is located, if the dat
845
+ file is local.
846
+ pb_dir : str
847
+ The physiobank directory where the dat file is located, if the
848
+ dat file is remote.
849
+ fmt : str
850
+ The format of the dat file
851
+ n_sig : int
852
+ The number of signals contained in the dat file
853
+ sig_len : int
854
+ The signal length (per channel) of the dat file
855
+ byte_offset : list
856
+ The byte offset of the dat file
857
+ samps_per_frame : list
858
+ The samples/frame for each signal of the dat file
859
+ skew : list
860
+ The skew for the signals of the dat file
861
+ sampfrom : int
862
+ The starting sample number to be read from the signals
863
+ sampto : int
864
+ The final sample number to be read from the signals
865
+ smooth_frames : bool
866
+ Whether to smooth channels with multiple samples/frame
867
+
868
+ Returns
869
+ -------
870
+ signal : numpy array
871
+ The read signal
872
+
873
+ Notes
874
+ -----
875
+ `sampfrom`, `sampto`, and `smooth_frames` are user desired
838
876
input fields. All other fields specify the file parameters.
839
877
840
- Returns all channels
841
-
842
- Input arguments:
843
- - file_name: The name of the dat file.
844
- - dirname: The full directory where the dat file is located, if the dat file is local.
845
- - pb_dir: The physiobank directory where the dat file is located, if the dat file is remote.
846
- - fmt: The format of the dat file
847
- - n_sig: The number of signals contained in the dat file
848
- - sig_len : The signal length (per channel) of the dat file
849
- - byte_offset: The byte offsets of the dat file
850
- - samps_per_frame: The samples/frame for the signals of the dat file
851
- - skew: The skew for the signals of the dat file
852
- - sampfrom: The starting sample number to be read from the signals
853
- - sampto: The final sample number to be read from the signals
854
- - smooth_frames: Whether to smooth channels with multiple samples/frame
855
878
"""
856
879
857
880
# Total number of samples per frame
858
881
tsamps_per_frame = sum (samps_per_frame )
859
882
# The signal length to read (per channel)
860
- readlen = sampto - sampfrom
883
+ read_len = sampto - sampfrom
861
884
862
885
# Calculate parameters used to read and process the dat file
863
886
start_byte , n_read_samples , block_floor_samples , extra_flat_samples , nanreplace = calc_read_params (fmt , sig_len , byte_offset ,
@@ -882,13 +905,13 @@ def rddat(file_name, dirname, pb_dir, fmt, n_sig,
882
905
# Extra number of bytes to append onto the bytes read from the dat file.
883
906
n_extra_bytes = total_process_bytes - total_read_bytes
884
907
885
- sig_bytes = np .concatenate ((getdatbytes (file_name , dirname , pb_dir , fmt , start_byte , n_read_samples ),
908
+ sig_bytes = np .concatenate ((get_dat_bytes (file_name , dir_name , pb_dir , fmt , start_byte , n_read_samples ),
886
909
np .zeros (n_extra_bytes , dtype = np .dtype (DATA_LOAD_TYPES [fmt ]))))
887
910
else :
888
- sig_bytes = np .concatenate ((getdatbytes (file_name , dirname , pb_dir , fmt , start_byte , n_read_samples ),
911
+ sig_bytes = np .concatenate ((get_dat_bytes (file_name , dir_name , pb_dir , fmt , start_byte , n_read_samples ),
889
912
np .zeros (extra_flat_samples , dtype = np .dtype (DATA_LOAD_TYPES [fmt ]))))
890
913
else :
891
- sig_bytes = getdatbytes (file_name , dirname , pb_dir , fmt , start_byte , n_read_samples )
914
+ sig_bytes = get_dat_bytes (file_name , dir_name , pb_dir , fmt , start_byte , n_read_samples )
892
915
893
916
# Continue to process the read values into proper samples
894
917
@@ -912,7 +935,7 @@ def rddat(file_name, dirname, pb_dir, fmt, n_sig,
912
935
# Reshape into multiple channels
913
936
sig = sig_bytes .reshape (- 1 , n_sig )
914
937
# Skew the signal
915
- sig = skewsig (sig , skew , n_sig , readlen , fmt , nanreplace )
938
+ sig = skew_sig (sig , skew , n_sig , read_len , fmt , nanreplace )
916
939
917
940
# Extra frames present to be smoothed. Obtain averaged uniform numpy array
918
941
elif smooth_frames :
@@ -931,7 +954,7 @@ def rddat(file_name, dirname, pb_dir, fmt, n_sig,
931
954
startind = np .sum (samps_per_frame [:ch ])
932
955
sig [:,ch ] = [np .average (sig_bytes [ind :ind + samps_per_frame [ch ]]) for ind in range (startind ,len (sig_bytes ),tsamps_per_frame )]
933
956
# Skew the signal
934
- sig = skewsig (sig , skew , n_sig , readlen , fmt , nanreplace )
957
+ sig = skew_sig (sig , skew , n_sig , read_len , fmt , nanreplace )
935
958
936
959
# Extra frames present without wanting smoothing. Return all expanded samples.
937
960
else :
@@ -943,10 +966,10 @@ def rddat(file_name, dirname, pb_dir, fmt, n_sig,
943
966
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 ))])
944
967
sig .append (sig_bytes [ch_indices ])
945
968
# Skew the signal
946
- sig = skewsig (sig , skew , n_sig , readlen , fmt , nanreplace , samps_per_frame )
969
+ sig = skew_sig (sig , skew , n_sig , read_len , fmt , nanreplace , samps_per_frame )
947
970
948
971
# Integrity check of signal shape after reading
949
- checksigdims (sig , readlen , n_sig , samps_per_frame )
972
+ checksigdims (sig , read_len , n_sig , samps_per_frame )
950
973
951
974
return sig
952
975
@@ -974,10 +997,10 @@ def calc_read_params(fmt, sig_len, byte_offset, skew, tsamps_per_frame, sampfrom
974
997
Examples
975
998
--------
976
999
sig_len=100, t = 4 (total samples/frame), skew = [0, 2, 4, 5]
977
- sampfrom=0, sampto=100 --> readlen = 100, n_sampread = 100*t, extralen = 5, nanreplace = [0, 2, 4, 5]
978
- sampfrom=50, sampto=100 --> readlen = 50, n_sampread = 50*t, extralen = 5, nanreplace = [0, 2, 4, 5]
979
- sampfrom=0, sampto=50 --> readlen = 50, n_sampread = 55*t, extralen = 0, nanreplace = [0, 0, 0, 0]
980
- sampfrom=95, sampto=99 --> readlen = 4, n_sampread = 5*t, extralen = 4, nanreplace = [0, 1, 3, 4]
1000
+ sampfrom=0, sampto=100 --> read_len = 100, n_sampread = 100*t, extralen = 5, nanreplace = [0, 2, 4, 5]
1001
+ sampfrom=50, sampto=100 --> read_len = 50, n_sampread = 50*t, extralen = 5, nanreplace = [0, 2, 4, 5]
1002
+ sampfrom=0, sampto=50 --> read_len = 50, n_sampread = 55*t, extralen = 0, nanreplace = [0, 0, 0, 0]
1003
+ sampfrom=95, sampto=99 --> read_len = 4, n_sampread = 5*t, extralen = 4, nanreplace = [0, 1, 3, 4]
981
1004
"""
982
1005
983
1006
# First flat sample number to read (if all channels were flattened)
@@ -1021,18 +1044,24 @@ def calc_read_params(fmt, sig_len, byte_offset, skew, tsamps_per_frame, sampfrom
1021
1044
1022
1045
return (start_byte , n_read_samples , block_floor_samples , extra_flat_samples , nanreplace )
1023
1046
1047
+
1024
1048
def requiredbytenum (mode , fmt , n_samp ):
1025
1049
"""
1026
- Determine how many signal bytes are needed to read a file, or now many
1027
- should be written to a file, for special formats.
1028
-
1029
- Input arguments:
1030
- - mode: 'read' or 'write'
1031
- - fmt: format
1032
- - n_samp: number of samples
1050
+ Determine how many signal bytes are needed to read a file, or how
1051
+ many should be written to a file, for special formats.
1033
1052
1034
1053
It would be nice if read and write were the same, but fmt 311 for
1035
1054
n_extra == 2 ruins it.
1055
+
1056
+ Parameters
1057
+ ----------
1058
+ mode : str
1059
+ 'read' or 'write'
1060
+ fmt:
1061
+ The wfdb format
1062
+ n_samp : int
1063
+ The number of samples
1064
+
1036
1065
"""
1037
1066
1038
1067
if fmt == '212' :
@@ -1059,15 +1088,17 @@ def requiredbytenum(mode, fmt, n_samp):
1059
1088
return int (nbytes )
1060
1089
1061
1090
1062
- def getdatbytes (file_name , dirname , pb_dir , fmt , start_byte , n_samp ):
1091
+ def get_dat_bytes (file_name , dir_name , pb_dir , fmt , start_byte , n_samp ):
1063
1092
"""
1064
1093
Read bytes from a dat file, either local or remote, into a numpy array.
1065
1094
Slightly misleading function name. Does not return bytes object.
1066
1095
Output argument dtype varies depending on fmt. Non-special fmts are
1067
1096
read in their final required format. Special format are read as uint8.
1068
1097
1069
- Input arguments:
1070
- - n_samp: The total number of samples to read. Does NOT need to create whole blocks
1098
+ Parameters
1099
+ ----------
1100
+ n_samp : int
1101
+ The total number of samples to read. Does NOT need to create whole blocks
1071
1102
for special format. Any number of samples should be readable. But see below*.
1072
1103
- start_byte: The starting byte to read from. * See below.
1073
1104
@@ -1091,7 +1122,7 @@ def getdatbytes(file_name, dirname, pb_dir, fmt, start_byte, n_samp):
1091
1122
1092
1123
# Local dat file
1093
1124
if pb_dir is None :
1094
- fp = open (os .path .join (dirname , file_name ), 'rb' )
1125
+ fp = open (os .path .join (dir_name , file_name ), 'rb' )
1095
1126
fp .seek (start_byte )
1096
1127
1097
1128
# Read file using corresponding dtype
@@ -1202,7 +1233,7 @@ def bytes_to_samples(sig_bytes, n_samp, fmt):
1202
1233
return sig
1203
1234
1204
1235
1205
- def skewsig (sig , skew , n_sig , readlen , fmt , nanreplace , samps_per_frame = None ):
1236
+ def skew_sig (sig , skew , n_sig , read_len , fmt , nanreplace , samps_per_frame = None ):
1206
1237
"""
1207
1238
Skew the signal, insert nans and shave off end of array if needed.
1208
1239
@@ -1216,11 +1247,11 @@ def skewsig(sig, skew, n_sig, readlen, fmt, nanreplace, samps_per_frame=None):
1216
1247
# Shift the channel samples
1217
1248
for ch in range (n_sig ):
1218
1249
if skew [ch ]> 0 :
1219
- sig [ch ][:readlen * samps_per_frame [ch ]] = sig [ch ][skew [ch ]* samps_per_frame [ch ]:]
1250
+ sig [ch ][:read_len * samps_per_frame [ch ]] = sig [ch ][skew [ch ]* samps_per_frame [ch ]:]
1220
1251
1221
1252
# Shave off the extra signal length at the end
1222
1253
for ch in range (n_sig ):
1223
- sig [ch ] = sig [ch ][:readlen * samps_per_frame [ch ]]
1254
+ sig [ch ] = sig [ch ][:read_len * samps_per_frame [ch ]]
1224
1255
1225
1256
# Insert nans where skewed signal overran dat file
1226
1257
for ch in range (n_sig ):
@@ -1231,9 +1262,9 @@ def skewsig(sig, skew, n_sig, readlen, fmt, nanreplace, samps_per_frame=None):
1231
1262
# Shift the channel samples
1232
1263
for ch in range (n_sig ):
1233
1264
if skew [ch ]> 0 :
1234
- sig [:readlen , ch ] = sig [skew [ch ]:, ch ]
1265
+ sig [:read_len , ch ] = sig [skew [ch ]:, ch ]
1235
1266
# Shave off the extra signal length at the end
1236
- sig = sig [:readlen , :]
1267
+ sig = sig [:read_len , :]
1237
1268
1238
1269
# Insert nans where skewed signal overran dat file
1239
1270
for ch in range (n_sig ):
@@ -1244,15 +1275,15 @@ def skewsig(sig, skew, n_sig, readlen, fmt, nanreplace, samps_per_frame=None):
1244
1275
1245
1276
1246
1277
# Integrity check of signal shape after reading
1247
- def checksigdims (sig , readlen , n_sig , samps_per_frame ):
1278
+ def checksigdims (sig , read_len , n_sig , samps_per_frame ):
1248
1279
if isinstance (sig , np .ndarray ):
1249
- if sig .shape != (readlen , n_sig ):
1280
+ if sig .shape != (read_len , n_sig ):
1250
1281
raise ValueError ('Samples were not loaded correctly' )
1251
1282
else :
1252
1283
if len (sig ) != n_sig :
1253
1284
raise ValueError ('Samples were not loaded correctly' )
1254
1285
for ch in range (n_sig ):
1255
- if len (sig [ch ]) != samps_per_frame [ch ] * readlen :
1286
+ if len (sig [ch ]) != samps_per_frame [ch ] * read_len :
1256
1287
raise ValueError ('Samples were not loaded correctly' )
1257
1288
1258
1289
0 commit comments