@@ -113,6 +113,20 @@ def _sample_spikes_evenly(spike_ids, n_spikes):
113113 return np .asarray (spike_ids [indices ], dtype = np .int64 )
114114
115115
116+ def _select_spikes_evenly (selector , n_spikes , cluster_ids , ** kwargs ):
117+ """Use phylib's even selector when available, with a 2.7-compatible fallback."""
118+ if 'sample_evenly' in inspect .signature (selector ).parameters :
119+ return selector (n_spikes , cluster_ids , sample_evenly = True , ** kwargs )
120+
121+ selected = [
122+ _sample_spikes_evenly (selector (None , [cluster_id ], ** kwargs ), n_spikes )
123+ for cluster_id in cluster_ids
124+ ]
125+ if not selected :
126+ return np .array ([], dtype = np .int64 )
127+ return np .concatenate (selected )
128+
129+
116130def _spike_budget_fields (per_cluster , total , max_n_clusters , background = None ):
117131 """Return dialog fields for a per-cluster budget and optional shared cap."""
118132 per_cluster_default = per_cluster if per_cluster is not None else total or 100000
@@ -318,8 +332,8 @@ def _get_waveforms_with_n_spikes(self, cluster_id, n_spikes_waveforms, current_f
318332 # Or keep spikes from a subset of the chunks for performance reasons (decompression will
319333 # happen on the fly here).
320334 else :
321- spike_ids = self . selector (
322- n_spikes_waveforms , [cluster_id ], subset_chunks = True , sample_evenly = True
335+ spike_ids = _select_spikes_evenly (
336+ self . selector , n_spikes_waveforms , [cluster_id ], subset_chunks = True
323337 )
324338
325339 # Get the best channels.
@@ -1269,13 +1283,17 @@ def spikes_per_cluster(cluster_id):
12691283 except AttributeError :
12701284 chunk_bounds = [0.0 , self .model .spike_samples [- 1 ] + 1 ]
12711285
1272- self .selector = SpikeSelector (
1273- get_spikes_per_cluster = spikes_per_cluster ,
1274- spike_times = self .model .spike_samples , # NOTE: chunk_bounds is in samples, not seconds
1275- chunk_bounds = chunk_bounds ,
1276- n_chunks_kept = self .n_chunks_kept ,
1277- spikes_are_disjoint = True ,
1278- )
1286+ selector_kwargs = {
1287+ 'get_spikes_per_cluster' : spikes_per_cluster ,
1288+ # NOTE: chunk_bounds is in samples, not seconds.
1289+ 'spike_times' : self .model .spike_samples ,
1290+ 'chunk_bounds' : chunk_bounds ,
1291+ 'n_chunks_kept' : self .n_chunks_kept ,
1292+ }
1293+ # phylib 2.7 does not yet expose this optimization hint.
1294+ if 'spikes_are_disjoint' in inspect .signature (SpikeSelector ).parameters :
1295+ selector_kwargs ['spikes_are_disjoint' ] = True
1296+ self .selector = SpikeSelector (** selector_kwargs )
12791297
12801298 def _cache_methods (self ):
12811299 """Cache methods as specified in `self._memcached` and `self._cached`."""
0 commit comments