def test_detect_peaks(): repo = 'https://gin.g-node.org/NeuralEnsemble/ephy_testing_data' remote_path = 'mearec/mearec_test_10s.h5' local_path = download_dataset(repo=repo, remote_path=remote_path, local_folder=None) recording = MEArecRecordingExtractor(local_path) peaks = detect_peaks(recording, method='by_channel', peak_sign='neg', detect_threshold=5, n_shifts=2, chunk_size=10000, verbose=1, progress_bar=False, outputs='numpy_compact') sample_inds, chan_inds, amplitudes, seg_inds = detect_peaks( recording, method='locally_exclusive', peak_sign='neg', detect_threshold=5, n_shifts=2, chunk_size=10000, verbose=1, progress_bar=False, outputs='numpy_split')
def test_detect_peaks(): repo = 'https://gin.g-node.org/NeuralEnsemble/ephy_testing_data' remote_path = 'mearec/mearec_test_10s.h5' local_path = download_dataset(repo=repo, remote_path=remote_path, local_folder=None) recording = MEArecRecordingExtractor(local_path) # by_channel peaks = detect_peaks(recording, method='by_channel', peak_sign='neg', detect_threshold=5, n_shifts=2, chunk_size=10000, verbose=1, progress_bar=False) # by_channel sorting = detect_peaks(recording, method='by_channel', peak_sign='neg', detect_threshold=5, n_shifts=2, chunk_size=10000, verbose=1, progress_bar=False, outputs="sorting") assert isinstance(sorting, BaseSorting) # locally_exclusive peaks = detect_peaks(recording, method='locally_exclusive', peak_sign='neg', detect_threshold=5, n_shifts=2, chunk_size=10000, verbose=1, progress_bar=False) # locally_exclusive + localization peaks = detect_peaks( recording, method='locally_exclusive', peak_sign='neg', detect_threshold=5, n_shifts=2, chunk_size=10000, verbose=1, progress_bar=True, localization_dict=dict(method='center_of_mass', local_radius_um=150, ms_before=0.1, ms_after=0.3), #localization_dict=dict(method='monopolar_triangulation', local_radius_um=150, # ms_before=0.1, ms_after=0.3, max_distance_um=1000) ) assert 'x' in peaks.dtype.fields
def plot(self): rec = self.recording peaks = self.peaks if peaks is None: from spikeinterface.sortingcomponents import detect_peaks self.detect_peaks_kwargs['outputs'] = 'numpy_compact' peaks = detect_peaks(rec, **self.detect_peaks_kwargs) fs = rec.get_sampling_frequency() duration = rec.get_total_duration() probes = rec.get_probes() assert len(probes) == 1, "Activity map is only available for a single probe. If you have a probe group, "\ "consider splitting the recording from different probes" probe = probes[0] if self.bin_duration_s is None: self._plot_one_bin(rec, probe, peaks, duration) else: bin_size = int(self.bin_duration_s * fs) num_frames = int(duration / self.bin_duration_s) def animate_func(i): i0 = np.searchsorted(peaks['sample_ind'], bin_size * i) i1 = np.searchsorted(peaks['sample_ind'], bin_size * (i + 1)) local_peaks = peaks[i0:i1] artists = self._plot_one_bin(rec, probe, local_peaks, self.bin_duration_s) return artists self.animation = FuncAnimation(self.figure, animate_func, frames=num_frames, interval=100, blit=True)
def plot(self): rec = self.recording peaks = self.peaks if peaks is None: from spikeinterface.sortingcomponents import detect_peaks self.detect_peaks_kwargs['outputs'] = 'numpy_compact' peaks = detect_peaks(rec, **self.detect_peaks_kwargs) fs = rec.get_sampling_frequency() duration = rec.get_total_duration() probe = rec.get_probe() if self.bin_duration_s is None: self._plot_one_bin(rec, probe, peaks, duration) else: bin_size = int(self.bin_duration_s * fs) num_frames = int(duration / self.bin_duration_s) def animate_func(i): i0 = np.searchsorted(peaks['sample_ind'], bin_size * i) i1 = np.searchsorted(peaks['sample_ind'], bin_size * (i + 1)) local_peaks = peaks[i0:i1] artists = self._plot_one_bin(rec, probe, local_peaks, self.bin_duration_s) return artists self.animation = FuncAnimation(self.figure, animate_func, frames=num_frames, interval=100, blit=True)
def test_localize_peaks(): repo = 'https://gin.g-node.org/NeuralEnsemble/ephy_testing_data' remote_path = 'mearec/mearec_test_10s.h5' local_path = download_dataset(repo=repo, remote_path=remote_path, local_folder=None) recording = MEArecRecordingExtractor(local_path) peaks = detect_peaks(recording, method='locally_exclusive', peak_sign='neg', detect_threshold=5, n_shifts=2, chunk_size=10000, verbose=False, progress_bar=False) peak_locations = localize_peaks(recording, peaks, method='center_of_mass', chunk_size=10000, verbose=True, progress_bar=False) assert peaks.size == peak_locations.shape[0] peak_locations = localize_peaks(recording, peaks, method='monopolar_triangulation', n_jobs=1, chunk_size=10000, verbose=True, progress_bar=True) assert peaks.size == peak_locations.shape[0]
def test_plot_drift_over_time(self): from spikeinterface.sortingcomponents import detect_peaks peaks = detect_peaks(self._rec, method='locally_exclusive') sw.plot_drift_over_time(self._rec, peaks=peaks, bin_duration_s=1., weight_with_amplitudes=True, mode='heatmap') sw.plot_drift_over_time(self._rec, peaks=peaks, bin_duration_s=1., weight_with_amplitudes=False, mode='heatmap') sw.plot_drift_over_time(self._rec, peaks=peaks, weight_with_amplitudes=False, mode='scatter', scatter_plot_kwargs={'color':'r'})
def _do_plot(self): rec = self.recording peaks = self.peaks if peaks is None: from spikeinterface.sortingcomponents import detect_peaks self.detect_peaks_kwargs['outputs'] = 'numpy_compact' peaks = detect_peaks(rec, **self.detect_peaks_kwargs) fs = rec.get_sampling_frequency() bin_size = int(fs * self.bin_duration_s) total_size = rec.get_num_samples(segment_index=0) probe = rec.get_probe() positions = probe.contact_positions all_depth = np.unique(positions[:, self.probe_axis]) ndepth = all_depth.size step = np.min(np.diff(all_depth)) depth_bins = np.arange(np.min(all_depth), np.max(all_depth) + step, step) nchunk = total_size // bin_size peak_density = np.zeros((depth_bins.size - 1, nchunk), dtype='float32') for i in range(nchunk): mask = (peaks['sample_ind'] >= (i * bin_size)) & (peaks['sample_ind'] < ((i + 1) * bin_size)) depths = positions[peaks['channel_ind'][mask], 1] if self.weight_with_amplitudes: count, bins = np.histogram(depths, bins=depth_bins, weights=np.abs( peaks['channel_ind'][mask])) else: count, bins = np.histogram(depths, bins=depth_bins) peak_density[:, i] = count extent = (0, self.bin_duration_s * nchunk, depth_bins[0], depth_bins[-1]) im = self.ax.imshow(peak_density, interpolation='nearest', origin='upper', aspect='auto', extent=extent) self.ax.set_xlabel('time (s)') txt_axis = ['x', 'y'][self.probe_axis] self.ax.set_ylabel(f'{txt_axis} (um)')
def test_localize_peaks(): repo = 'https://gin.g-node.org/NeuralEnsemble/ephy_testing_data' remote_path = 'mearec/mearec_test_10s.h5' local_path = download_dataset(repo=repo, remote_path=remote_path, local_folder=None) recording = MEArecRecordingExtractor(local_path) peaks = detect_peaks(recording, method='by_channel', peak_sign='neg', detect_threshold=5, n_shifts=2, chunk_size=10000, verbose=False, progress_bar=False, outputs='numpy_compact' ) peak_locations = localize_peaks(recording, peaks, chunk_size=10000, verbose=True, progress_bar=False, ) assert peaks.size == peak_locations.shape[0]
def _do_plot(self): rec = self.recording peaks = self.peaks if peaks is None: from spikeinterface.sortingcomponents import detect_peaks self.detect_peaks_kwargs['outputs'] = 'numpy_compact' peaks = detect_peaks(rec, **self.detect_peaks_kwargs) fs = rec.get_sampling_frequency() duration = rec.get_total_duration() probe = rec.get_probe() positions = probe.contact_positions # TODO: @alessio weight_with_amplitudes is not implemented yet rates = np.zeros(rec.get_num_channels(), dtype='float64') for chan_ind, chan_id in enumerate(rec.channel_ids): mask = peaks['channel_ind'] == chan_ind num_spike = np.sum(mask) rates[chan_ind] = num_spike / duration if self.with_contact_color: plot_probe(probe, ax=self.ax, contacts_values=rates, probe_shape_kwargs={ 'facecolor': 'w', 'alpha': .1 }, contacts_kargs={'alpha': 1.}) if self.with_interpolated_map: image, xlims, ylims = probe.to_image(rates, pixel_size=0.5, num_pixel=None, method='linear', xlims=None, ylims=None) self.ax.imshow(image, extent=xlims + ylims, origin='lower', alpha=0.5)
def test_detect_peaks(): repo = 'https://gin.g-node.org/NeuralEnsemble/ephy_testing_data' remote_path = 'mearec/mearec_test_10s.h5' local_path = download_dataset(repo=repo, remote_path=remote_path, local_folder=None) recording = MEArecRecordingExtractor(local_path) # by_channel noise_levels = get_noise_levels(recording, return_scaled=False) peaks = detect_peaks(recording, method='by_channel', peak_sign='neg', detect_threshold=5, n_shifts=2, chunk_size=10000, verbose=1, progress_bar=False, noise_levels=noise_levels) subset_peaks = select_peaks(peaks, max_peaks_per_channel=100) subset_peaks = select_peaks(peaks, 'smart_sampling', max_peaks_per_channel=100, noise_levels=noise_levels) assert len(subset_peaks) < len(peaks)
def setup_module(): local_path = download_dataset(repo=repo, remote_path=remote_path, local_folder=None) recording = MEArecRecordingExtractor(local_path) # detect and localize peaks = detect_peaks( recording, method='locally_exclusive', peak_sign='neg', detect_threshold=5, n_shifts=2, chunk_size=10000, verbose=1, progress_bar=True, localization_dict=dict(method='center_of_mass', local_radius_um=150, ms_before=0.1, ms_after=0.3), #~ localization_dict=dict(method='monopolar_triangulation', local_radius_um=150, ms_before=0.1, ms_after=0.3, max_distance_um=1000), ) np.save('mearec_detected_peaks.npy', peaks)
def _do_plot(self): rec = self.recording peaks = self.peaks if peaks is None: from spikeinterface.sortingcomponents import detect_peaks self.detect_peaks_kwargs['outputs'] = 'numpy_compact' peaks = detect_peaks(rec, **self.detect_peaks_kwargs) fs = rec.get_sampling_frequency() bin_size = int(fs * self.bin_duration_s) total_size = rec.get_num_samples(segment_index=0) positions = rec.get_channel_locations() all_depth = np.unique(positions[:, self.probe_axis]) all_depth = np.sort(all_depth) if self.mode == 'heatmap': step = np.min(np.diff(all_depth)) depth_bins = np.arange(np.min(all_depth), np.max(all_depth) + step, step) nchunk = total_size // bin_size peak_density = np.zeros((depth_bins.size - 1, nchunk), dtype='float32') for i in range(nchunk): mask = (peaks['sample_ind'] >= (i * bin_size)) & (peaks['sample_ind'] < ((i + 1) * bin_size)) depths = positions[peaks['channel_ind'][mask], self.probe_axis] if self.weight_with_amplitudes: count, bins = np.histogram(depths, bins=depth_bins, weights=np.abs( peaks['amplitude'][mask])) else: count, bins = np.histogram(depths, bins=depth_bins) peak_density[:, i] = count extent = (0, self.bin_duration_s * nchunk, depth_bins[0], depth_bins[-1]) kwargs = dict() kwargs.update(self.imshow_kwargs) self.ax.imshow(peak_density, interpolation='nearest', origin='lower', aspect='auto', extent=extent, **kwargs) elif self.mode == 'scatter': times = peaks['sample_ind'] / fs depths = positions[peaks['channel_ind'], self.probe_axis] # add fake depth jitter factor = np.min(np.diff(all_depth)) depths += np.random.randn(depths.size) * factor * 0.15 kwargs = dict(alpha=0.4, s=1, color='k') kwargs.update(self.scatter_plot_kwargs) self.ax.scatter(times, depths, **kwargs) self.ax.set_xlabel('time (s)') txt_axis = ['x', 'y'][self.probe_axis] self.ax.set_ylabel(f'{txt_axis} (um)')
local_path = si.download_dataset(remote_path='mearec/mearec_test_10s.h5') rec, sorting = si.read_mearec(local_path) ############################################################################## # Lets filter and detect peak on it from spikeinterface.sortingcomponents import detect_peaks rec_filtred = si.bandpass_filter(rec, freq_min=300., freq_max=6000., margin_ms=5.0) print(rec_filtred) peaks = detect_peaks( rec_filtred, method='locally_exclusive', peak_sign='neg', detect_threshold=6, n_shifts=7, local_radius_um=100, noise_levels=None, random_chunk_kwargs={}, chunk_memory='10M', n_jobs=1, progress_bar=True) ############################################################################## # peaks is a numpy 1D array with structured dtype that contains several fields: # sample_ind/channel_ind/amplitude/segment_ind print(peaks.dtype) print(peaks.shape) print(peaks.dtype.fields.keys()) ############################################################################## # This "peaks" vector can be used in several widgets, for instance # plot_peak_activity_map()
def mask_traces(self, sample_window_ms=2, percent_spikes=None, balance_spikes_on_channel=False, max_num_spikes=None, detect_threshold=5, method='locally_exclusive', **job_kwargs): """ Find mask based on spike peaks Parameters ---------- sample_window_ms: float, int, list, or None If float or int, it's a symmetric window If list, it needs to have 2 elements. Asymmetric window If None, all traces are used percent_spikes: float Percentage of spikes selected If None, all spikes are used max_num_spikes: int Maximum number of spikes allowed If None, all spikes are used balance_spikes_on_channel: bool If true, the number of samples taken from each channel depends on the total number of spikes on the channel If false, random subsampling detect_threshold: float MAD threshold to detect peaks. method: str Method to detect peaks: * 'by_channel' : peak are detected in each channel independently. (default) * 'locally_exclusive' : locally given a radius the best peak only is taken but not neighboring channels. job_kwargs: dict dict for parallel peak detection. Returns ------- cut_traces: np.ndarray Array with subsampled traces """ if sample_window_ms is None: self.cut_traces = self.recording.get_traces().astype('int16').T return # set sample window if isinstance(sample_window_ms, float) or isinstance(sample_window_ms, int): sample_window_ms = [sample_window_ms, sample_window_ms] sample_window = [int(sample_window_ms[0] * self.fs / 1000), int(sample_window_ms[1] * self.fs / 1000)] num_channels = self.recording.get_num_channels() peaks = sc.detect_peaks(self.recording, method=method, detect_threshold=detect_threshold, **job_kwargs) t_init = time.time() # subsampling if percent_spikes is not None: if max_num_spikes is not None and percent_spikes * len(peaks['sample_ind']) > max_num_spikes: percent_spikes = max_num_spikes / len(peaks['sample_ind']) if balance_spikes_on_channel: final_idxs = [] for chan in np.arange(num_channels): occurrences = list(peaks['channel_ind']).count(chan) num_samples = occurrences * percent_spikes idxs = np.where(peaks['channel_ind'] == chan)[0] idxs = np.random.choice(idxs, int(num_samples)) final_idxs.extend(list(idxs)) final_idxs = sorted(final_idxs) self.peaks_subsamp = peaks['sample_ind'][final_idxs] else: num_samples = len(peaks['sample_ind']) * percent_spikes self.peaks_subsamp = np.random.choice(peaks['sample_ind'], int(num_samples)) else: self.peaks_subsamp = peaks['sample_ind'] t_end = time.time() - t_init print(f"Number of detected spikes: {len(peaks['sample_ind'])}") print(f"Number of sampled spikes: {len(self.peaks_subsamp)}") print(f"Elapsed time subsampling: {t_end}") # find idxs t_init2 = time.time() idxs_spike = map(lambda peak: np.arange(peak - sample_window[0], peak + sample_window[1]), self.peaks_subsamp) self.selected_idxs = np.unique(list(idxs_spike)) t_end2 = time.time() - t_init2 print(f"Elapsed time idxs selection: {t_end2}") self.selected_idxs = np.array(sorted(list(self.selected_idxs))) self.selected_idxs = self.selected_idxs[self.selected_idxs > 0] self.selected_idxs = self.selected_idxs[self.selected_idxs < self.recording.get_num_samples(0) - 1] t_init3 = time.time() self.cut_traces = self.recording.get_traces().astype('int16').T[:, self.selected_idxs] t_end3 = time.time() - t_init3 print(f"Sample number for ICA: {len(self.selected_idxs)} from {self.recording.get_num_samples(0)}\n" f"Elapsed time getting traces: {t_end3}") print(f"Shape: {self.cut_traces.shape}")