def write_recording(recording, save_path, initial_sorting_fn, dtype='float32', verbose=True, **job_kwargs): """ Convert and save the recording extractor to SHYBRID format Parameters ---------- recording: RecordingExtractor The recording extractor to be converted and saved save_path: str Full path to desired target folder initial_sorting_fn: str Full path to the initial sorting csv file (can also be generated using write_sorting static method from the SHYBRIDSortingExtractor) dtype: dtype Type of the saved data. Default float32. **write_binary_kwargs: keyword arguments for write_to_binary_dat_format() function """ assert HAVE_SBEX, SHYBRIDRecordingExtractor.installation_mesg assert recording.get_num_segments( ) == 1, "SHYBRID can only write single segment recordings" save_path = Path(save_path) recording_name = 'recording.bin' probe_name = 'probe.prb' params_name = 'recording.yml' # location information has to be present in order for shybrid to # be able to operate on the recording if recording.get_channel_locations() is None: raise GeometryNotLoadedError("Channel locations were not found") # write recording recording_fn = (save_path / recording_name).absolute() write_binary_recording(recording, files_path=recording_fn, dtype=dtype, verbose=verbose, **job_kwargs) # write probe file probe_fn = (save_path / probe_name).absolute() probegroup = recording.get_probegroup() write_prb(probe_fn, probegroup, total_nb_channels=recording.get_num_channels()) # create parameters file parameters = dict(clusters=initial_sorting_fn, data=dict(dtype=dtype, fs=str(recording.get_sampling_frequency()), order='F', probe=str(probe_fn))) # write parameters file parameters_fn = (save_path / params_name).absolute() with parameters_fn.open('w') as fp: yaml.dump(parameters, fp)
def _setup_recording(cls, recording, output_folder, params, verbose): source_dir = Path(__file__).parent # alias to params p = params experiment_name = output_folder / 'recording' # save prb file prb_file = output_folder / 'probe.prb' probegroup = recording.get_probegroup() write_prb(prb_file, probegroup, radius=p['adjacency_radius']) # source file if isinstance( recording, BinaryRecordingExtractor) and recording._kwargs['offset'] == 0: # no need to copy raw_filename = str( Path(recording._kwargs['files_path'][0]).resolve()) dtype = recording._kwargs['dtype'] else: # save binary file (chunk by hcunk) into a new file raw_filename = output_folder / 'recording.dat' dtype = 'int16' BinaryRecordingExtractor.write_recording( recording, files_path=[raw_filename], dtype='int16', total_memory=p["total_memory"], n_jobs=p["n_jobs_bin"], verbose=False, progress_bar=verbose) if p['detect_sign'] < 0: detect_sign = 'negative' elif p['detect_sign'] > 0: detect_sign = 'positive' else: detect_sign = 'both' # set up klusta config file with (source_dir / 'config_default.prm').open('r') as f: klusta_config = f.readlines() # Note: should use format with dict approach here klusta_config = ''.join(klusta_config).format( experiment_name, prb_file, raw_filename, float(recording.get_sampling_frequency()), recording.get_num_channels(), "'{}'".format(dtype), p['threshold_strong_std_factor'], p['threshold_weak_std_factor'], "'" + detect_sign + "'", p['extract_s_before'], p['extract_s_after'], p['n_features_per_channel'], p['pca_n_waveforms_max'], p['num_starting_clusters']) with (output_folder / 'config.prm').open('w') as f: f.writelines(klusta_config)
def _setup_recording(cls, recording, output_folder, params, verbose): p = params if p['detect_sign'] < 0: detect_sign = 'negative' elif p['detect_sign'] > 0: detect_sign = 'positive' else: detect_sign = 'both' if p['merge_spikes']: auto = p['auto_merge'] else: auto = 0 source_dir = Path(__file__).parent # save prb file # note: only one group here, the split is done in basesorter prb_file = output_folder / 'probe.prb' probegroup = recording.get_probegroup() write_prb(prb_file, probegroup, total_nb_channels=recording.get_num_channels(), radius=p['adjacency_radius']) # save binary file file_name = 'recording' # We should make this copy more efficient with chunks n_chan = recording.get_num_channels() n_frames = recording.get_num_frames(segment_index=0) chunk_size = 2**24 // n_chan npy_file = str(output_folder / file_name) + '.npy' data_file = open_memmap(npy_file, shape=(n_frames, n_chan), dtype=np.float32, mode='w+') nb_chunks = n_frames // chunk_size for i in range(nb_chunks + 1): start_frame = i * chunk_size end_frame = min((i + 1) * chunk_size, n_frames) data = recording.get_traces(start_frame=start_frame, end_frame=end_frame).astype('float32') data_file[start_frame:end_frame, :] = data sample_rate = float(recording.get_sampling_frequency()) # set up spykingcircus config file with (source_dir / 'config_default.params').open('r') as f: circus_config = f.readlines() circus_config = ''.join(circus_config).format( sample_rate, prb_file, p['template_width_ms'], p['detect_threshold'], detect_sign, p['filter'], p['whitening_max_elts'], p['clustering_max_elts'], auto) with (output_folder / (file_name + '.params')).open('w') as f: f.writelines(circus_config)
def test_prb(): probegroup = read_prb(folder / 'dummy.prb') with open('two_tetrodes.prb', 'w') as f: f.write(prb_two_tetrodes) two_tetrode = read_prb('two_tetrodes.prb') assert len(two_tetrode.probes) == 2 assert two_tetrode.probes[0].get_electrode_count() == 4 write_prb('two_tetrodes_written.prb', two_tetrode) two_tetrode_back = read_prb('two_tetrodes_written.prb')
def _setup_recording(cls, recording, output_folder, params, verbose): import tridesclous as tdc # save prb file probegroup = recording.get_probegroup() prb_file = output_folder / 'probe.prb' write_prb(prb_file, probegroup) num_seg = recording.get_num_segments() sr = recording.get_sampling_frequency() # source file if isinstance(recording, BinaryRecordingExtractor ) and recording._kwargs['time_axis'] == 0: # no need to copy kwargs = recording._kwargs file_paths = kwargs['file_paths'] dtype = kwargs['dtype'] num_chan = kwargs['num_chan'] file_offset = kwargs['file_offset'] else: if verbose: print('Local copy of recording') # save binary file (chunk by chunk) into a new file num_chan = recording.get_num_channels() dtype = recording.get_dtype().str file_paths = [ str(output_folder / f'raw_signals_{i}.raw') for i in range(num_seg) ] BinaryRecordingExtractor.write_recording( recording, file_paths=file_paths, dtype=dtype, total_memory=params["total_memory"], n_jobs=params["n_jobs_bin"], verbose=False, progress_bar=verbose) file_offset = 0 # initialize source and probe file tdc_dataio = tdc.DataIO(dirname=str(output_folder)) tdc_dataio.set_data_source(type='RawData', filenames=file_paths, dtype=dtype, sample_rate=float(sr), total_channel=int(num_chan), offset=int(file_offset)) tdc_dataio.set_probe_file(str(prb_file)) if verbose: print(tdc_dataio)