@@ -224,45 +224,47 @@ def read_header(fid):
224224 return header
225225
226226
227- INBOX = Path ("/Users/tolgadincer/ClientData/Utah_Alex/2020Data/RawData/Organoid21" )
228- header_filename = INBOX / "032520_US_885kHz_sham/info.rhs"
229- with open (header_filename , "rb" ) as fid :
230- header = read_header (fid )
227+ def load_rhs (folder : str , file_expr : str ):
228+ """Load rhs data
229+
230+ Example:
231+ # Read data
232+ >>> signals, time = load_rhs("/home/inbox/organoids21/032520_US_885kHz_sham", file_expr="amp*dat")
233+
234+ # Plot data
235+ >>> for signal in signals:
236+ >>> plt.plot(time, signal)
237+ >>> plt.xlabel("Time (s)")
238+ >>> plt.ylabel("Signal (microvolts)")
239+ >>> plt.show()
240+
241+ Args:
242+ folder (str): Folder that contains info.rhs, time.dat, and *.dat files
243+ file_expr (str): regex pattern of the file names to be read.
244+
245+ Returns:
246+ signals (np.array_like): Signal amplitudes in microvolts
247+ time (np.array_like): Time stamps
248+ """
231249
250+ header_filepath = next (Path (folder ).glob ("info.rhs" ))
251+ with open (header_filepath , "rb" ) as fid :
252+ header = read_header (fid )
232253
233- def read_time ( file_paths , time_filename = "time.dat" , header_filename = "info.rhs" ):
254+ time_file = next ( Path ( folder ). glob ( "time.dat" ))
234255
235256 time = (
236- np .memmap (time_filename , dtype = np .int32 , offset = 2_000_000 , shape = 10000 )
257+ np .memmap (time_file , dtype = np .int32 , offset = 2_000_000 , shape = 10000 )
237258 / header ["frequency_parameters" ]["amplifier_sample_rate" ]
238259 )
239260
261+ file_paths = list (Path (folder ).glob (file_expr ))
240262 signals = np .empty ([len (file_paths ), len (time )])
263+
241264 for i , file_path in enumerate (file_paths ):
242265 signals [i , :] = np .memmap (
243266 file_path , dtype = np .int16 , offset = 2_000_000 , shape = 10000
244267 )
245268 signals = signals * 0.195 # Convert to microvolts
246269
247270 return signals , time
248-
249-
250- INBOX = Path ("/Users/tolgadincer/ClientData/Utah_Alex/2020Data/RawData/Organoid21" )
251-
252- time_filename = INBOX / "032520_US_885kHz_sham/time.dat"
253- header_filename = INBOX / "032520_US_885kHz_sham/info.rhs"
254-
255- relative_paths = [
256- "032520_US_885kHz_sham/amp-B-001.dat" ,
257- "032520_US_885kHz_sham/amp-B-005.dat" ,
258- ]
259- file_paths = [INBOX / x for x in relative_paths ]
260-
261- signals , time = read_time (file_paths , time_filename , header_filename )
262-
263- for signal in signals :
264- plt .plot (time , signal )
265- # plt.title("/".join(file_path.parts[-2:]))
266- plt .xlabel ("Time (s)" )
267- plt .ylabel ("Signal (microvolts)" )
268- plt .show ()
0 commit comments