66import numpy as np
77
88
9- def select_peaks (peaks , method = "uniform" , seed = None , return_indices = False , ** method_kwargs ):
9+ def select_peaks (
10+ peaks , recording = None , method = "uniform" , seed = None , return_indices = False , margin = None , ** method_kwargs
11+ ):
1012 """
1113 Method to select a subset of peaks from a set of peaks.
1214 Usually use for reducing computational foorptint of downstream methods.
@@ -28,6 +30,9 @@ def select_peaks(peaks, method="uniform", seed=None, return_indices=False, **met
2830 The seed for random generations
2931 return_indices: bool
3032 If True, return the indices of selection such that selected_peaks = peaks[selected_indices]
33+ margin : Margin in timesteps. default: None. Otherwise should be a tuple (nbefore, nafter)
34+ preventing peaks to be selected at the borders of the segments. A recording should be provided to get the duration
35+ of the segments
3136
3237 method_kwargs: dict of kwargs method
3338 Keyword arguments for the chosen method:
@@ -66,8 +71,27 @@ def select_peaks(peaks, method="uniform", seed=None, return_indices=False, **met
6671 return_indices is True.
6772 """
6873
74+ if margin is not None :
75+ assert recording is not None , "recording should be provided if margin is not None"
76+
6977 selected_indices = select_peak_indices (peaks , method = method , seed = seed , ** method_kwargs )
7078 selected_peaks = peaks [selected_indices ]
79+
80+ if margin is not None :
81+ to_keep = np .zeros (len (selected_peaks ), dtype = bool )
82+ offset = 0
83+ for segment_index in range (recording .get_num_segments ()):
84+ duration = recording .get_num_frames (segment_index )
85+ i0 , i1 = np .searchsorted (selected_peaks ["segment_index" ], [segment_index , segment_index + 1 ])
86+ while selected_peaks ["sample_index" ][i0 ] <= margin [0 ] + offset :
87+ i0 += 1
88+ while selected_peaks ["sample_index" ][i1 - 1 ] >= (duration - margin [1 ]) + offset :
89+ i1 -= 1
90+ to_keep [i0 :i1 ] = True
91+ offset += duration
92+ selected_indices = selected_indices [to_keep ]
93+ selected_peaks = peaks [selected_indices ]
94+
7195 if return_indices :
7296 return selected_peaks , selected_indices
7397 else :
0 commit comments