def compute_metrics(self, rec_name, metric_names=['snr'], ms_before=3., ms_after=4., max_spikes_per_unit=500, n_jobs=-1, total_memory='1G', **snr_kargs): rec = self.get_recording(rec_name) gt_sorting = self.get_ground_truth(rec_name) # waveform extractor waveform_folder = self.study_folder / 'metrics' / f'waveforms_{rec_name}' if waveform_folder.is_dir(): shutil.rmtree(waveform_folder) we = WaveformExtractor.create(rec, gt_sorting, waveform_folder) we.set_params(ms_before=ms_before, ms_after=ms_after, max_spikes_per_unit=max_spikes_per_unit) we.run(n_jobs=n_jobs, total_memory=total_memory) # metrics metrics = compute_quality_metrics(we, metric_names=metric_names) filename = self.study_folder / 'metrics' / f'metrics _{rec_name}.txt' metrics.to_csv(filename, sep='\t', index=True) return metrics
def test_compute_quality_metrics_peak_sign(): rec = load_extractor('toy_rec') sort = load_extractor('toy_sorting') # invert recording rec_inv = scale(rec, gain=-1.) we = WaveformExtractor.load_from_folder('toy_waveforms') print(we) we_inv = WaveformExtractor.create(rec_inv, sort, 'toy_waveforms_inv') we_inv.set_params(ms_before=3., ms_after=4., max_spikes_per_unit=500) we_inv.run_extract_waveforms(n_jobs=1, chunk_size=30000) print(we_inv) # without PC metrics = compute_quality_metrics(we, metric_names=['snr', 'amplitude_cutoff'], peak_sign="neg") metrics_inv = compute_quality_metrics( we_inv, metric_names=['snr', 'amplitude_cutoff'], peak_sign="pos") assert np.allclose(metrics["snr"].values, metrics_inv["snr"].values) assert np.allclose(metrics["amplitude_cutoff"].values, metrics_inv["amplitude_cutoff"].values)
def setup_module(): for folder in ('toy_rec', 'toy_sorting', 'toy_waveforms'): if Path(folder).is_dir(): shutil.rmtree(folder) recording, sorting = toy_example(num_segments=2, num_units=10) recording = recording.save(folder='toy_rec') sorting = sorting.save(folder='toy_sorting') we = WaveformExtractor.create(recording, sorting, 'toy_waveforms') we.set_params(ms_before=3., ms_after=4., max_spikes_per_unit=500) we.run_extract_waveforms(n_jobs=1, chunk_size=30000)
def test_WaveformExtractor(): durations = [30, 40] sampling_frequency = 30000. # 2 segments recording = generate_recording(num_channels=2, durations=durations, sampling_frequency=sampling_frequency) recording.annotate(is_filtered=True) folder_rec = "wf_rec1" recording = recording.save(folder=folder_rec) sorting = generate_sorting(num_units=5, sampling_frequency=sampling_frequency, durations=durations) # test with dump !!!! recording = recording.save() sorting = sorting.save() folder = Path('test_waveform_extractor') if folder.is_dir(): shutil.rmtree(folder) we = WaveformExtractor.create(recording, sorting, folder) we.set_params(ms_before=3., ms_after=4., max_spikes_per_unit=500) we.run_extract_waveforms(n_jobs=1, chunk_size=30000) we.run_extract_waveforms(n_jobs=4, chunk_size=30000, progress_bar=True) wfs = we.get_waveforms(0) assert wfs.shape[0] <= 500 assert wfs.shape[1:] == (210, 2) wfs, sampled_index = we.get_waveforms(0, with_index=True) # load back we = WaveformExtractor.load_from_folder(folder) wfs = we.get_waveforms(0) template = we.get_template(0) assert template.shape == (210, 2) templates = we.get_all_templates() assert templates.shape == (5, 210, 2) wf_std = we.get_template(0, mode='std') assert wf_std.shape == (210, 2) wfs_std = we.get_all_templates(mode='std') assert wfs_std.shape == (5, 210, 2) wf_segment = we.get_template_segment(unit_id=0, segment_index=0) assert wf_segment.shape == (210, 2) assert wf_segment.shape == (210, 2)
def setup_module(): for folder in ('toy_rec', 'toy_sorting', 'toy_waveforms'): if Path(folder).is_dir(): shutil.rmtree(folder) recording, sorting = toy_example(num_segments=2, num_units=10) recording = recording.save(folder='toy_rec') sorting = sorting.save(folder='toy_sorting') we = WaveformExtractor.create(recording, sorting, 'toy_waveforms') we.set_params(ms_before=3., ms_after=4., max_spikes_per_unit=500) we.run(n_jobs=1, chunk_size=30000) pca = WaveformPrincipalComponent(we) pca.set_params(n_components=5, mode='by_channel_local') pca.run()
def get_waveform_extractor(self, rec_name, sorter_name=None): rec = self.get_recording(rec_name) if sorter_name is None: name = 'GroundTruth' sorting = self.get_ground_truth(rec_name) else: assert sorter_name in self.sorter_names name = sorter_name sorting = self.get_sorting(sorter_name, rec_name) waveform_folder = self.study_folder / 'waveforms' / f'waveforms_{name}_{rec_name}' if waveform_folder.is_dir(): we = WaveformExtractor.load_from_folder(waveform_folder) else: we = WaveformExtractor.create(rec, sorting, waveform_folder) return we
sorting, folder, ms_before=1.5, ms_after=2., max_spikes_per_unit=500, load_if_exists=True) print(we) ############################################################################### # Alternatively, the :code:`WaveformExtractor` object can be instantiated # directly. In this case, we need to :code:`set_params()` to set the desired # parameters: folder = 'waveform_folder2' we = WaveformExtractor.create(recording, sorting, folder, remove_if_exists=True) we.set_params(ms_before=3., ms_after=4., max_spikes_per_unit=1000) we.run_extract_waveforms(n_jobs=1, chunk_size=30000, progress_bar=True) print(we) ############################################################################### # The :code:`'waveform_folder'` folder contains: # * the dumped recording (json) # * the dumped sorting (json) # * the parameters (json) # * a subfolder with "waveforms_XXX.npy" and "sampled_index_XXX.npy" import os print(os.listdir(folder))