def write_recording(recording: RecordingExtractor, save_path: PathType, dtype: DtypeType = None, **write_binary_kwargs): """ Convert and save the recording extractor to Neuroscope format. Parameters ---------- recording: RecordingExtractor The recording extractor to be converted and saved. save_path: str Path to desired target folder. The name of the files will be the same as the final directory. dtype: dtype Optional. Data type to be used in writing; must be int16 or int32 (default). Will throw a warning if stored recording type from get_traces() does not match. **write_binary_kwargs: keyword arguments for write_to_binary_dat_format function - chunk_size - chunk_mb """ save_path = Path(save_path) save_path.mkdir(parents=True, exist_ok=True) if save_path.suffix == "": recording_name = save_path.name else: recording_name = save_path.stem xml_name = recording_name save_xml_filepath = save_path / f"{xml_name}.xml" recording_filepath = save_path / recording_name # create parameters file if none exists if save_xml_filepath.is_file(): raise FileExistsError(f"{save_xml_filepath} already exists!") xml_root = et.Element('xml') et.SubElement(xml_root, 'acquisitionSystem') et.SubElement(xml_root.find('acquisitionSystem'), 'nBits') et.SubElement(xml_root.find('acquisitionSystem'), 'nChannels') et.SubElement(xml_root.find('acquisitionSystem'), 'samplingRate') recording_dtype = str(recording.get_dtype()) int_loc = recording_dtype.find('int') recording_n_bits = recording_dtype[(int_loc + 3):(int_loc + 5)] valid_dtype = ["16", "32"] if dtype is None: if int_loc != -1 and recording_n_bits in valid_dtype: n_bits = recording_n_bits else: print( "Warning: Recording data type must be int16 or int32! Defaulting to int32." ) n_bits = "32" dtype = f"int{n_bits}" # update dtype in pass to BinDatRecordingExtractor.write_recording else: dtype = str(dtype) # if user passed numpy data type int_loc = dtype.find('int') assert int_loc != -1, "Data type must be int16 or int32! Non-integer received." n_bits = dtype[(int_loc + 3):(int_loc + 5)] assert n_bits in valid_dtype, "Data type must be int16 or int32!" xml_root.find('acquisitionSystem').find('nBits').text = n_bits xml_root.find('acquisitionSystem').find('nChannels').text = str( recording.get_num_channels()) xml_root.find('acquisitionSystem').find('samplingRate').text = str( recording.get_sampling_frequency()) et.ElementTree(xml_root).write(str(save_xml_filepath), pretty_print=True) recording.write_to_binary_dat_format(recording_filepath, dtype=dtype, **write_binary_kwargs)
def write_recording(recording: RecordingExtractor, save_path: PathType, dtype: DtypeType = None, **write_binary_kwargs): """ Convert and save the recording extractor to Neuroscope format. Parameters ---------- recording: RecordingExtractor The recording extractor to be converted and saved. save_path: str Path to desired target folder. The name of the files will be the same as the final directory. dtype: dtype Optional. Data type to be used in writing; must be int16 or int32 (default). Will throw a warning if stored recording type from get_traces() does not match. **write_binary_kwargs: keyword arguments for write_to_binary_dat_format function - chunk_size - chunk_mb """ save_path = Path(save_path) if not save_path.is_dir(): os.makedirs(save_path) if save_path.suffix == '': recording_name = save_path.name else: recording_name = save_path.stem xml_name = recording_name save_xml_filepath = save_path / (str(xml_name) + '.xml') recording_filepath = save_path / recording_name # create parameters file if none exists if save_xml_filepath.is_file(): raise FileExistsError(f'{save_xml_filepath} already exists!') soup = BeautifulSoup("", 'xml') new_tag = soup.new_tag('nbits') recording_dtype = str(recording.get_dtype()) int_loc = recording_dtype.find('int') recording_n_bits = recording_dtype[(int_loc + 3):(int_loc + 5)] if dtype is None: # user did not specify data type if int_loc != -1 and recording_n_bits in ['16', '32']: n_bits = recording_n_bits else: print('Warning: Recording data type must be int16 or int32! Defaulting to int32.') n_bits = '32' dtype = 'int' + n_bits # update dtype in pass to BinDatRecordingExtractor.write_recording else: dtype = str(dtype) # if user passed numpy data type int_loc = dtype.find('int') assert int_loc != -1, 'Data type must be int16 or int32! Non-integer received.' n_bits = dtype[(int_loc + 3):(int_loc + 5)] assert n_bits in ['16', '32'], 'Data type must be int16 or int32!' new_tag.string = n_bits soup.append(new_tag) new_tag = soup.new_tag('nchannels') new_tag.string = str(recording.get_num_channels()) soup.append(new_tag) new_tag = soup.new_tag('samplingrate') new_tag.string = str(recording.get_sampling_frequency()) soup.append(new_tag) # write parameters file # create parameters file if none exists with save_xml_filepath.open("w") as f: f.write(str(soup)) recording.write_to_binary_dat_format(recording_filepath, dtype=dtype, **write_binary_kwargs)