def write_sorting(sorting: SortingExtractor, save_path: PathType): # if multiple groups, use the NeuroscopeMultiSortingExtactor write function if 'group' in sorting.get_shared_unit_property_names(): NeuroscopeMultiSortingExtractor.write_sorting(sorting, save_path) else: save_path.mkdir(parents=True, exist_ok=True) if save_path.suffix == '': sorting_name = save_path.name else: sorting_name = save_path.stem xml_name = sorting_name save_xml_filepath = save_path / (str(xml_name) + '.xml') # 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'), 'samplingRate') xml_root.find('acquisitionSystem').find('samplingRate').text = str( sorting.get_sampling_frequency()) et.ElementTree(xml_root).write(str(save_xml_filepath.absolute()), pretty_print=True) # Create and save .res and .clu files from the current sorting object save_res = save_path / f'{sorting_name}.res' save_clu = save_path / f'{sorting_name}.clu' res, clu = _extract_res_clu_arrays(sorting) np.savetxt(save_res, res, fmt='%i') np.savetxt(save_clu, clu, fmt='%i')
def write_sorting(sorting: SortingExtractor, save_path: PathType): assert save_path.suffixes == [ ".spikes", ".cellinfo", ".mat", ], "The save_path must correspond to the CellExplorer format of sorting_id.spikes.cellinfo.mat!" base_path = save_path.parent sorting_id = save_path.name.split(".")[0] session_info_save_path = base_path / f"{sorting_id}.sessionInfo.mat" spikes_save_path = save_path base_path.mkdir(parents=True, exist_ok=True) sampling_frequency = sorting.get_sampling_frequency() session_info_mat_dict = dict(sessionInfo=dict(rates=dict( wideband=sampling_frequency))) scipy.io.savemat(file_name=session_info_save_path, mdict=session_info_mat_dict) spikes_mat_dict = dict(spikes=dict( UID=sorting.get_unit_ids(), times=[[[y / sampling_frequency] for y in x] for x in sorting.get_units_spike_train()], )) # If, in the future, it is ever desired to allow this to write unit properties, they must conform # to the format here: https://cellexplorer.org/datastructure/data-structure-and-format/ scipy.io.savemat(file_name=spikes_save_path, mdict=spikes_mat_dict)
def from_memory(sorting: se.SortingExtractor, serialize=False): if serialize: with hi.TemporaryDirectory() as tmpdir: fname = tmpdir + '/' + _random_string(10) + '_firings.mda' MdaSortingExtractor.write_sorting(sorting=sorting, save_path=fname) with ka.config(use_hard_links=True): uri = ka.store_file(fname, basename='firings.mda') sorting = LabboxEphysSortingExtractor({ 'sorting_format': 'mda', 'data': { 'firings': uri, 'samplerate': sorting.get_sampling_frequency() } }) return sorting obj = { 'sorting_format': 'in_memory', 'data': register_in_memory_object(sorting) } return LabboxEphysSortingExtractor(obj)
def write_sorting(sorting: SortingExtractor, save_path: PathType): # if multiple groups, use the NeuroscopeMultiSortingExtactor write function if 'group' in sorting.get_shared_unit_property_names(): NeuroscopeMultiSortingExtractor.write_sorting(sorting, save_path) else: if not save_path.is_dir(): os.makedirs(save_path) if save_path.suffix == '': sorting_name = save_path.name else: sorting_name = save_path.stem xml_name = sorting_name save_xml_filepath = save_path / (str(xml_name) + '.xml') # 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('samplingrate') new_tag.string = str(sorting.get_sampling_frequency()) soup.append(new_tag) # write parameters file with save_xml_filepath.open("w") as f: f.write(str(soup)) # Create and save .res and .clu files from the current sorting object save_res = save_path / f'{sorting_name}.res' save_clu = save_path / f'{sorting_name}.clu' res, clu = _extract_res_clu_arrays(sorting) np.savetxt(save_res, res, fmt='%i') np.savetxt(save_clu, clu, fmt='%i')