@@ -726,6 +726,7 @@ def convert_dtype(self, physical, return_res, smooth_frames):
726726 self .e_d_signal [ch ] = self .e_d_signal [ch ].astype (return_dtype , copy = False )
727727 return
728728
729+
729730 def calc_checksum (self , expanded = False ):
730731 """
731732 Calculate the checksum(s) of the input signal.
@@ -859,7 +860,7 @@ def smooth_frames(self, sigtype='physical'):
859860
860861def _rd_segment (file_name , dir_name , pn_dir , fmt , n_sig , sig_len , byte_offset ,
861862 samps_per_frame , skew , sampfrom , sampto , channels ,
862- smooth_frames , ignore_skew , return_res = 64 ):
863+ smooth_frames , ignore_skew , no_file = False , sig_data = None , return_res = 64 ):
863864 """
864865 Read the digital samples from a single segment record's associated
865866 dat file(s).
@@ -897,6 +898,12 @@ def _rd_segment(file_name, dir_name, pn_dir, fmt, n_sig, sig_len, byte_offset,
897898 Specifies whether to apply the skew to align the signals in the
898899 output variable (False), or to ignore the skew field and load in
899900 all values contained in the dat files unaligned (True).
901+ no_file : bool, optional
902+ Used when using this function with just an array of signal data
903+ and no associated file to read the data from.
904+ sig_data : ndarray, optional
905+ The signal data that would normally be imported using the associated
906+ .dat and .hea files. Should only be used when no_file is set to True.
900907 return_res : int, optional
901908 The numpy array dtype of the returned signals. Options are: 64,
902909 32, 16, and 8, where the value represents the numpy int or float
@@ -918,6 +925,10 @@ def _rd_segment(file_name, dir_name, pn_dir, fmt, n_sig, sig_len, byte_offset,
918925 specifications of the segment.
919926
920927 """
928+ # Check for valid inputs
929+ if no_file and sig_data is None :
930+ raise Exception ('signal_dat empty: No signal data provided' )
931+
921932 # Avoid changing outer variables
922933 byte_offset = byte_offset [:]
923934 samps_per_frame = samps_per_frame [:]
@@ -984,10 +995,17 @@ def _rd_segment(file_name, dir_name, pn_dir, fmt, n_sig, sig_len, byte_offset,
984995
985996 # Read each wanted dat file and store signals
986997 for fn in w_file_name :
987- signals [:, out_dat_channel [fn ]] = _rd_dat_signals (fn , dir_name , pn_dir ,
988- w_fmt [fn ], len (datchannel [fn ]), sig_len , w_byte_offset [fn ],
989- w_samps_per_frame [fn ], w_skew [fn ], sampfrom , sampto ,
990- smooth_frames )[:, r_w_channel [fn ]]
998+ if no_file :
999+ signals [:, out_dat_channel [fn ]] = _rd_dat_signals (fn , dir_name ,
1000+ pn_dir , w_fmt [fn ], len (datchannel [fn ]), sig_len ,
1001+ w_byte_offset [fn ], w_samps_per_frame [fn ], w_skew [fn ],
1002+ sampfrom , sampto , smooth_frames , no_file = True ,
1003+ sig_data = sig_data )[:, r_w_channel [fn ]]
1004+ else :
1005+ signals [:, out_dat_channel [fn ]] = _rd_dat_signals (fn , dir_name ,
1006+ pn_dir , w_fmt [fn ], len (datchannel [fn ]), sig_len ,
1007+ w_byte_offset [fn ], w_samps_per_frame [fn ], w_skew [fn ],
1008+ sampfrom , sampto , smooth_frames )[:, r_w_channel [fn ]]
9911009
9921010 # Return each sample in signals with multiple samples/frame, without smoothing.
9931011 # Return a list of numpy arrays for each signal.
@@ -996,10 +1014,16 @@ def _rd_segment(file_name, dir_name, pn_dir, fmt, n_sig, sig_len, byte_offset,
9961014
9971015 for fn in w_file_name :
9981016 # Get the list of all signals contained in the dat file
999- datsignals = _rd_dat_signals (fn , dir_name , pn_dir , w_fmt [fn ],
1000- len (datchannel [fn ]), sig_len , w_byte_offset [fn ],
1001- w_samps_per_frame [fn ], w_skew [fn ], sampfrom , sampto ,
1002- smooth_frames )
1017+ if no_file :
1018+ datsignals = _rd_dat_signals (fn , dir_name , pn_dir , w_fmt [fn ],
1019+ len (datchannel [fn ]), sig_len , w_byte_offset [fn ],
1020+ w_samps_per_frame [fn ], w_skew [fn ], sampfrom , sampto ,
1021+ smooth_frames , no_file = True , sig_data = sig_data )
1022+ else :
1023+ datsignals = _rd_dat_signals (fn , dir_name , pn_dir , w_fmt [fn ],
1024+ len (datchannel [fn ]), sig_len , w_byte_offset [fn ],
1025+ w_samps_per_frame [fn ], w_skew [fn ], sampfrom , sampto ,
1026+ smooth_frames )
10031027
10041028 # Copy over the wanted signals
10051029 for cn in range (len (out_dat_channel [fn ])):
@@ -1010,7 +1034,7 @@ def _rd_segment(file_name, dir_name, pn_dir, fmt, n_sig, sig_len, byte_offset,
10101034
10111035def _rd_dat_signals (file_name , dir_name , pn_dir , fmt , n_sig , sig_len ,
10121036 byte_offset , samps_per_frame , skew , sampfrom , sampto ,
1013- smooth_frames ):
1037+ smooth_frames , no_file = False , sig_data = None ):
10141038 """
10151039 Read all signals from a WFDB dat file.
10161040
@@ -1042,6 +1066,12 @@ def _rd_dat_signals(file_name, dir_name, pn_dir, fmt, n_sig, sig_len,
10421066 The final sample number to be read from the signals.
10431067 smooth_frames : bool
10441068 Whether to smooth channels with multiple samples/frame.
1069+ no_file : bool, optional
1070+ Used when using this function with just an array of signal data
1071+ and no associated file to read the data from.
1072+ sig_data : ndarray, optional
1073+ The signal data that would normally be imported using the associated
1074+ .dat and .hea files. Should only be used when no_file is set to True.
10451075
10461076 Returns
10471077 -------
@@ -1058,6 +1088,10 @@ def _rd_dat_signals(file_name, dir_name, pn_dir, fmt, n_sig, sig_len,
10581088 specifications of the segment.
10591089
10601090 """
1091+ # Check for valid inputs
1092+ if no_file and sig_data is None :
1093+ raise Exception ('signal_dat empty: No signal data provided' )
1094+
10611095 # Total number of samples per frame
10621096 tsamps_per_frame = sum (samps_per_frame )
10631097 # The signal length to read (per channel)
@@ -1086,26 +1120,27 @@ def _rd_dat_signals(file_name, dir_name, pn_dir, fmt, n_sig, sig_len,
10861120 # already load samples.
10871121
10881122 # Read values from dat file. Append bytes/samples if needed.
1123+ if no_file :
1124+ data_to_read = sig_data
1125+ else :
1126+ data_to_read = _rd_dat_file (file_name , dir_name , pn_dir , fmt ,
1127+ start_byte , n_read_samples )
1128+
10891129 if extra_flat_samples :
10901130 if fmt in UNALIGNED_FMTS :
10911131 # Extra number of bytes to append onto the bytes read from
10921132 # the dat file.
10931133 n_extra_bytes = total_process_bytes - total_read_bytes
10941134
1095- sig_data = np .concatenate ((_rd_dat_file (file_name , dir_name ,
1096- pn_dir , fmt , start_byte ,
1097- n_read_samples ),
1135+ sig_data = np .concatenate ((data_to_read ,
10981136 np .zeros (n_extra_bytes ,
1099- dtype = np .dtype (DATA_LOAD_TYPES [fmt ]))))
1137+ dtype = np .dtype (DATA_LOAD_TYPES [fmt ]))))
11001138 else :
1101- sig_data = np .concatenate ((_rd_dat_file (file_name , dir_name ,
1102- pn_dir , fmt , start_byte ,
1103- n_read_samples ),
1139+ sig_data = np .concatenate ((data_to_read ,
11041140 np .zeros (extra_flat_samples ,
1105- dtype = np .dtype (DATA_LOAD_TYPES [fmt ]))))
1141+ dtype = np .dtype (DATA_LOAD_TYPES [fmt ]))))
11061142 else :
1107- sig_data = _rd_dat_file (file_name , dir_name , pn_dir , fmt , start_byte ,
1108- n_read_samples )
1143+ sig_data = data_to_read
11091144
11101145 # Finish processing the read data into proper samples if not already
11111146
0 commit comments