-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathanalyzer_extension_core.py
More file actions
1767 lines (1479 loc) · 70.6 KB
/
Copy pathanalyzer_extension_core.py
File metadata and controls
1767 lines (1479 loc) · 70.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Implement AnalyzerExtension that are essential and imported in core
* ComputeRandomSpikes
* ComputeWaveforms
* ComputeTemplates
Theses two classes replace the WaveformExtractor
It also implements:
* ComputeNoiseLevels which is very convenient to have
"""
from copy import copy, deepcopy
import warnings
import numpy as np
from collections import namedtuple
from .numpyextractors import NumpySorting
from .sortinganalyzer import SortingAnalyzer, AnalyzerExtension, register_result_extension
from .waveform_tools import extract_waveforms_to_single_buffer, estimate_templates_with_accumulator
from .recording_tools import get_noise_levels
from .template import Templates
from .sorting_tools import random_spikes_selection, select_sorting_periods_mask, spike_vector_to_indices
from .job_tools import fix_job_kwargs, split_job_kwargs
from .core_tools import ms_to_samples
class ComputeRandomSpikes(AnalyzerExtension):
"""
AnalyzerExtension that select some random spikes.
This allows for a subsampling of spikes for further calculations and is important
for managing that amount of memory and speed of computation in the analyzer.
This will be used by the `waveforms`/`templates` extensions.
This internally uses `random_spikes_selection()` parameters.
Parameters
----------
method: "uniform" | "percentage" | "maximum_rate" | "all" , default: "uniform"
Method to select spikes: "uniform" randomly up to max_spikes_per_unit, "percentage" selects a fraction of spikes, and "maximum_rate" limits selection by spike rate over time.
max_spikes_per_unit : int, default: 500
The maximum number of spikes per unit, ignored if method="all"
margin_size : int, default: None
A margin on each border of segments to avoid border spikes, ignored if method="all"
seed : int or None, default: None
A seed for the random generator, ignored if method="all"
percentage: float | None, default: None
In case of `percentage` method. The proportion of spikes per units.
maximum_rate: float | None, default: None
In case of `maximum_rate` method. The cap rate per units.
Returns
-------
random_spike_indices: np.array
The indices of the selected spikes
"""
extension_name = "random_spikes"
depend_on = []
need_recording = False
use_nodepipeline = False
need_job_kwargs = False
def _run(self, verbose=False):
self.data["random_spikes_indices"] = random_spikes_selection(
self.sorting_analyzer.sorting,
num_samples=self.sorting_analyzer.rec_attributes["num_samples"],
**self.params,
)
def _set_params(
self, method="uniform", max_spikes_per_unit=500, margin_size=None, seed=None, percentage=None, maximum_rate=None
):
params = dict(method=method, max_spikes_per_unit=max_spikes_per_unit, margin_size=margin_size, seed=seed)
return params
def _select_units_extension_data(self, unit_ids):
random_spikes_indices = self.data["random_spikes_indices"]
spikes = self.sorting_analyzer.sorting.to_spike_vector()
keep_unit_indices = np.flatnonzero(np.isin(self.sorting_analyzer.unit_ids, unit_ids))
keep_spike_mask = np.isin(spikes["unit_index"], keep_unit_indices)
selected_mask = np.zeros(spikes.size, dtype=bool)
selected_mask[random_spikes_indices] = True
new_data = dict()
new_data["random_spikes_indices"] = np.flatnonzero(selected_mask[keep_spike_mask])
return new_data
def _merge_extension_data(
self, merge_unit_groups, new_unit_ids, new_sorting_analyzer, keep_mask=None, verbose=False, **job_kwargs
):
new_data = dict()
random_spikes_indices = self.data["random_spikes_indices"]
if keep_mask is None:
new_data["random_spikes_indices"] = random_spikes_indices.copy()
else:
spikes = self.sorting_analyzer.sorting.to_spike_vector()
selected_mask = np.zeros(spikes.size, dtype=bool)
selected_mask[random_spikes_indices] = True
new_data["random_spikes_indices"] = np.flatnonzero(selected_mask[keep_mask])
return new_data
def _split_extension_data(self, split_units, new_unit_ids, new_sorting_analyzer, verbose=False, **job_kwargs):
new_data = dict()
new_data["random_spikes_indices"] = self.data["random_spikes_indices"].copy()
return new_data
def _get_data(self):
return self.data["random_spikes_indices"]
def get_random_spikes(self):
# utils to get the some_spikes vector
# use internal cache
if not hasattr(self, "_some_spikes"):
spikes = self.sorting_analyzer.sorting.to_spike_vector()
self._some_spikes = spikes[self.data["random_spikes_indices"]]
return self._some_spikes
def get_selected_indices_in_spike_train(self, unit_id, segment_index):
# useful for WaveformExtractor backwards compatibility
# In Waveforms extractor "selected_spikes" was a dict (key: unit_id) of list (segment_index) of indices of spikes in spiketrain
sorting = self.sorting_analyzer.sorting
random_spikes_indices = self.data["random_spikes_indices"]
unit_index = sorting.id_to_index(unit_id)
spikes = sorting.to_spike_vector()
spike_indices_in_seg = np.flatnonzero(
(spikes["segment_index"] == segment_index) & (spikes["unit_index"] == unit_index)
)
common_element, inds_left, inds_right = np.intersect1d(
spike_indices_in_seg, random_spikes_indices, return_indices=True
)
selected_spikes_in_spike_train = inds_left
return selected_spikes_in_spike_train
compute_random_spikes = ComputeRandomSpikes.function_factory()
register_result_extension(ComputeRandomSpikes)
class ComputeWaveforms(AnalyzerExtension):
"""
AnalyzerExtension that extract some waveforms of each units.
The sparsity is controlled by the SortingAnalyzer sparsity.
Parameters
----------
ms_before : float, default: 1.0
The number of ms to extract before the spike events
ms_after : float, default: 2.0
The number of ms to extract after the spike events
dtype : None | dtype, default: None
The dtype of the waveforms. If None, the dtype of the recording is used.
Returns
-------
waveforms : np.ndarray
Array with computed waveforms with shape (num_random_spikes, num_samples, num_channels)
"""
extension_name = "waveforms"
depend_on = ["random_spikes"]
need_recording = True
use_nodepipeline = False
need_job_kwargs = True
@property
def nbefore(self):
return ms_to_samples(self.params["ms_before"], self.sorting_analyzer.sampling_frequency)
@property
def nafter(self):
return ms_to_samples(self.params["ms_after"], self.sorting_analyzer.sampling_frequency)
def _run(self, verbose=False, **job_kwargs):
self.data.clear()
recording = self.sorting_analyzer.recording
sorting = self.sorting_analyzer.sorting
unit_ids = sorting.unit_ids
# retrieve spike vector and the sampling
some_spikes = self.sorting_analyzer.get_extension("random_spikes").get_random_spikes()
if self.format == "binary_folder":
# in that case waveforms are extacted directly in files
file_path = self._get_binary_extension_folder() / "waveforms.npy"
mode = "memmap"
copy = False
else:
file_path = None
mode = "shared_memory"
copy = True
if self.sparsity is None:
sparsity_mask = None
else:
sparsity_mask = self.sparsity.mask
all_waveforms = extract_waveforms_to_single_buffer(
recording,
some_spikes,
unit_ids,
self.nbefore,
self.nafter,
mode=mode,
return_in_uV=self.sorting_analyzer.return_in_uV,
file_path=file_path,
dtype=self.params["dtype"],
sparsity_mask=sparsity_mask,
copy=copy,
job_name="compute_waveforms",
verbose=verbose,
**job_kwargs,
)
self.data["waveforms"] = all_waveforms
def _set_params(
self,
ms_before: float = 1.0,
ms_after: float = 2.0,
dtype=None,
):
recording = self.sorting_analyzer.recording
if dtype is None:
dtype = recording.get_dtype()
if np.issubdtype(dtype, np.integer) and self.sorting_analyzer.return_in_uV:
dtype = "float32"
dtype = np.dtype(dtype)
params = dict(
ms_before=float(ms_before),
ms_after=float(ms_after),
dtype=dtype.str,
)
return params
def _select_units_extension_data(self, unit_ids):
# random_spikes_indices = self.sorting_analyzer.get_extension("random_spikes").get_data()
some_spikes = self.sorting_analyzer.get_extension("random_spikes").get_random_spikes()
keep_unit_indices = np.flatnonzero(np.isin(self.sorting_analyzer.unit_ids, unit_ids))
spikes = self.sorting_analyzer.sorting.to_spike_vector()
# some_spikes = spikes[random_spikes_indices]
keep_spike_mask = np.isin(some_spikes["unit_index"], keep_unit_indices)
new_data = dict()
new_data["waveforms"] = self.data["waveforms"][keep_spike_mask, :, :]
return new_data
def _select_channels_extension_data(self, channel_ids):
unit_ids = self.sorting_analyzer.unit_ids
old_unit_id_to_channel_ids = self.sorting_analyzer.sparsity.unit_id_to_channel_ids
# Compute how to slice the original sparsity to get the newly selected sparsity
unit_sparsity_slices = {}
for unit_id in unit_ids:
unit_sparsity_channel_indices = []
unit_channel_ids = old_unit_id_to_channel_ids[unit_id]
for channel_id in channel_ids:
if channel_id in unit_channel_ids:
idx = np.where(old_unit_id_to_channel_ids[unit_id] == channel_id)[0][0]
unit_sparsity_channel_indices.append(idx)
unit_sparsity_slices[unit_id] = np.array(unit_sparsity_channel_indices)
old_waveforms = self.data["waveforms"]
random_spikes = self.sorting_analyzer.get_extension("random_spikes").get_random_spikes()
new_waveforms = np.zeros_like(old_waveforms)
max_num_active_channels = 0
for waveform_index, (waveform, unit_index) in enumerate(zip(old_waveforms, random_spikes["unit_index"])):
unit_id = unit_ids[unit_index]
channel_slice = unit_sparsity_slices[unit_id]
if len(channel_slice) > 0:
size_of_new_mask = len(channel_slice)
new_waveforms[waveform_index, :, :size_of_new_mask] = waveform[:, channel_slice]
max_num_active_channels = max(max_num_active_channels, size_of_new_mask)
data = {"waveforms": new_waveforms[:, :, :max_num_active_channels]}
return data
def _merge_extension_data(
self, merge_unit_groups, new_unit_ids, new_sorting_analyzer, keep_mask=None, verbose=False, **job_kwargs
):
waveforms = self.data["waveforms"]
some_spikes = self.sorting_analyzer.get_extension("random_spikes").get_random_spikes()
if keep_mask is not None:
spike_indices = self.sorting_analyzer.get_extension("random_spikes").get_data()
valid = keep_mask[spike_indices]
some_spikes = some_spikes[valid]
waveforms = waveforms[valid]
else:
waveforms = waveforms.copy()
old_sparsity = self.sorting_analyzer.sparsity
if old_sparsity is not None:
# we need a realignement inside each group because we take the channel intersection sparsity
for group_ids in merge_unit_groups:
group_indices = self.sorting_analyzer.sorting.ids_to_indices(group_ids)
group_sparsity_mask = old_sparsity.mask[group_indices, :]
group_selection = []
for unit_id in group_ids:
unit_index = self.sorting_analyzer.sorting.id_to_index(unit_id)
selection = np.flatnonzero(some_spikes["unit_index"] == unit_index)
group_selection.append(selection)
_inplace_sparse_realign_waveforms(waveforms, group_selection, group_sparsity_mask)
old_num_chans = int(np.max(np.sum(old_sparsity.mask, axis=1)))
new_num_chans = int(np.max(np.sum(new_sorting_analyzer.sparsity.mask, axis=1)))
if new_num_chans < old_num_chans:
waveforms = waveforms[:, :, :new_num_chans]
return dict(waveforms=waveforms)
def _split_extension_data(self, split_units, new_unit_ids, new_sorting_analyzer, verbose=False, **job_kwargs):
# splitting only affects random spikes, not waveforms
new_data = dict(waveforms=self.data["waveforms"].copy())
return new_data
def get_waveforms_one_unit(self, unit_id, force_dense: bool = False):
"""
Returns the waveforms of a unit id.
Parameters
----------
unit_id : int or str
The unit id to return waveforms for
force_dense : bool, default: False
If True, and SortingAnalyzer must be sparse then only waveforms on sparse channels are returned.
Returns
-------
waveforms: np.array
The waveforms (num_waveforms, num_samples, num_channels).
In case sparsity is used, only the waveforms on sparse channels are returned.
"""
sorting = self.sorting_analyzer.sorting
unit_index = sorting.id_to_index(unit_id)
waveforms = self.data["waveforms"]
some_spikes = self.sorting_analyzer.get_extension("random_spikes").get_random_spikes()
spike_mask = some_spikes["unit_index"] == unit_index
wfs = waveforms[spike_mask, :, :]
if self.sorting_analyzer.sparsity is not None:
chan_inds = self.sorting_analyzer.sparsity.unit_id_to_channel_indices[unit_id]
wfs = wfs[:, :, : chan_inds.size]
if force_dense:
num_channels = self.sorting_analyzer.get_num_channels()
dense_wfs = np.zeros((wfs.shape[0], wfs.shape[1], num_channels), dtype=wfs.dtype)
dense_wfs[:, :, chan_inds] = wfs
wfs = dense_wfs
return wfs
def _get_data(self):
return self.data["waveforms"]
def _inplace_sparse_realign_waveforms(waveforms, group_selection, group_sparsity_mask):
# this is used by "waveforms" extension but also "pca"
# common mask is intersection
common_mask = np.all(group_sparsity_mask, axis=0)
for i in range(len(group_selection)):
chan_mask = group_sparsity_mask[i, :]
sel = group_selection[i]
wfs = waveforms[sel, :, :][:, :, : np.sum(chan_mask)]
keep_mask = common_mask[chan_mask]
wfs = wfs[:, :, keep_mask]
waveforms[:, :, : wfs.shape[2]][sel, :, :] = wfs
waveforms[:, :, wfs.shape[2] :][sel, :, :] = 0.0
compute_waveforms = ComputeWaveforms.function_factory()
register_result_extension(ComputeWaveforms)
class ComputeTemplates(AnalyzerExtension):
"""
AnalyzerExtension that computes templates (average, std, median, percentile, ...)
This depends on the "waveforms" extension (`SortingAnalyzer.compute("waveforms")`)
When the "waveforms" extension is already computed, then the recording is not needed anymore for this extension.
Note: by default only the average and std are computed. Other operators (std, median, percentile) can be computed on demand
after the SortingAnalyzer.compute("templates") and then the data dict is updated on demand.
Parameters
----------
operators: list[str] | list[(str, float)] (for percentile)
The operators to compute. Can be "average", "std", "median", "percentile"
If percentile is used, then the second element of the tuple is the percentile to compute.
Returns
-------
templates: np.ndarray
The computed templates with shape (num_units, num_samples, num_channels)
"""
extension_name = "templates"
depend_on = ["random_spikes|waveforms"]
need_recording = False
use_nodepipeline = False
need_job_kwargs = True
need_backward_compatibility_on_load = True
def _handle_backward_compatibility_on_load(self):
if "ms_before" not in self.params:
# compatibility february 2024 > july 2024
self.params["ms_before"] = self.params["nbefore"] * 1000.0 / self.sorting_analyzer.sampling_frequency
if "ms_after" not in self.params:
# compatibility february 2024 > july 2024
self.params["ms_after"] = self.params["nafter"] * 1000.0 / self.sorting_analyzer.sampling_frequency
old_keys = copy(list(self.data.keys()))
for operator in old_keys:
if "pencentile" in operator:
fixed_operator = operator.replace("pencentile", "percentile")
self.data[fixed_operator] = self.data[operator]
del self.data[operator]
def _set_params(self, ms_before: float = 1.0, ms_after: float = 2.0, operators=None):
operators = operators or ["average", "std"]
assert isinstance(operators, list)
for operator in operators:
if isinstance(operator, str):
if operator not in ("average", "std", "median", "mad"):
error_msg = (
f"You have entered an operator {operator} in your `operators` argument which is "
f"not supported. Please use any of ['average', 'std', 'median', 'mad'] instead."
)
raise ValueError(error_msg)
else:
assert isinstance(operator, (list, tuple))
assert len(operator) == 2
assert operator[0] == "percentile"
waveforms_extension = self.sorting_analyzer.get_extension("waveforms")
if waveforms_extension is not None:
ms_before = waveforms_extension.params["ms_before"]
ms_after = waveforms_extension.params["ms_after"]
params = dict(
operators=operators,
ms_before=ms_before,
ms_after=ms_after,
)
return params
def _run(self, verbose=False, **job_kwargs):
self.data.clear()
if self.sorting_analyzer.has_extension("waveforms"):
self._compute_and_append_from_waveforms(self.params["operators"])
else:
if not self.sorting_analyzer.has_recording():
raise ValueError("Extension Templates requires the recording if Waveforms are not computed.")
bad_operator_list = [
operator for operator in self.params["operators"] if operator not in ("average", "std")
]
if len(bad_operator_list) > 0:
raise ValueError(
f"Computing templates with operators {bad_operator_list} requires the 'waveforms' extension"
)
recording = self.sorting_analyzer.recording
sorting = self.sorting_analyzer.sorting
unit_ids = sorting.unit_ids
# retrieve spike vector and the sampling
some_spikes = self.sorting_analyzer.get_extension("random_spikes").get_random_spikes()
return_in_uV = self.sorting_analyzer.return_in_uV
return_std = "std" in self.params["operators"]
sparsity_mask = None if self.sparsity is None else self.sparsity.mask
output = estimate_templates_with_accumulator(
recording,
some_spikes,
unit_ids,
self.nbefore,
self.nafter,
return_in_uV=return_in_uV,
return_std=return_std,
sparsity_mask=sparsity_mask,
verbose=verbose,
**job_kwargs,
)
if return_std:
templates, stds = output
data = dict(average=templates, std=stds)
else:
templates = output
data = dict(average=templates)
if self.sparsity is not None:
# make average and std dense again
for k, arr in data.items():
dense_arr = self.sparsity.densify_templates(arr)
data[k] = dense_arr
self.data.update(data)
def _compute_and_append_from_waveforms(self, operators):
if not self.sorting_analyzer.has_extension("waveforms"):
raise ValueError(f"Computing templates with operators {operators} requires the 'waveforms' extension")
unit_ids = self.sorting_analyzer.unit_ids
channel_ids = self.sorting_analyzer.channel_ids
waveforms_extension = self.sorting_analyzer.get_extension("waveforms")
waveforms = waveforms_extension.data["waveforms"]
num_samples = waveforms.shape[1]
for operator in operators:
if isinstance(operator, str) and operator in ("average", "std", "median"):
key = operator
elif isinstance(operator, (list, tuple)):
operator, percentile = operator
assert operator == "percentile"
key = f"percentile_{percentile}"
else:
raise ValueError(f"ComputeTemplates: wrong operator {operator}")
self.data[key] = np.zeros((unit_ids.size, num_samples, channel_ids.size))
# spikes = self.sorting_analyzer.sorting.to_spike_vector()
# some_spikes = spikes[self.sorting_analyzer.random_spikes_indices]
assert self.sorting_analyzer.has_extension(
"random_spikes"
), "compute 'templates' requires the random_spikes extension. You can run sorting_analyzer.compute('random_spikes')"
some_spikes = self.sorting_analyzer.get_extension("random_spikes").get_random_spikes()
for unit_index, unit_id in enumerate(unit_ids):
spike_mask = some_spikes["unit_index"] == unit_index
wfs = waveforms[spike_mask, :, :]
if wfs.shape[0] == 0:
continue
for operator in operators:
if operator == "average":
arr = np.average(wfs, axis=0)
key = operator
elif operator == "std":
arr = np.std(wfs, axis=0)
key = operator
elif operator == "median":
arr = np.median(wfs, axis=0)
key = operator
elif isinstance(operator, (list, tuple)):
operator, percentile = operator
arr = np.percentile(wfs, percentile, axis=0)
key = f"percentile_{percentile}"
if self.sparsity is None:
self.data[key][unit_index, :, :] = arr
else:
channel_indices = self.sparsity.unit_id_to_channel_indices[unit_id]
self.data[key][unit_index, :, :][:, channel_indices] = arr[:, : channel_indices.size]
@property
def nbefore(self):
nbefore = ms_to_samples(self.params["ms_before"], self.sorting_analyzer.sampling_frequency)
return nbefore
@property
def nafter(self):
nafter = ms_to_samples(self.params["ms_after"], self.sorting_analyzer.sampling_frequency)
return nafter
def _select_units_extension_data(self, unit_ids):
keep_unit_indices = np.flatnonzero(np.isin(self.sorting_analyzer.unit_ids, unit_ids))
new_data = dict()
for key, arr in self.data.items():
new_data[key] = arr[keep_unit_indices, :, :]
return new_data
def _select_channels_extension_data(self, channel_ids):
keep_channel_indices = [np.where(self.sorting_analyzer.channel_ids == id)[0][0] for id in channel_ids]
new_data = {}
for key, arr in self.data.items():
new_data[key] = arr[:, :, keep_channel_indices]
return new_data
def _merge_extension_data(
self, merge_unit_groups, new_unit_ids, new_sorting_analyzer, keep_mask=None, verbose=False, **job_kwargs
):
all_new_units = new_sorting_analyzer.unit_ids
new_data = dict()
counts = self.sorting_analyzer.sorting.count_num_spikes_per_unit()
for key, arr in self.data.items():
new_data[key] = np.zeros((len(all_new_units), arr.shape[1], arr.shape[2]), dtype=arr.dtype)
for unit_index, unit_id in enumerate(all_new_units):
if unit_id not in new_unit_ids:
keep_unit_index = self.sorting_analyzer.sorting.id_to_index(unit_id)
new_data[key][unit_index] = arr[keep_unit_index, :, :]
else:
merge_group = merge_unit_groups[list(new_unit_ids).index(unit_id)]
keep_unit_indices = self.sorting_analyzer.sorting.ids_to_indices(merge_group)
# We do a weighted sum of the templates
weights = np.zeros(len(merge_group), dtype=np.float32)
for count, merge_unit_id in enumerate(merge_group):
weights[count] = counts[merge_unit_id]
weights /= weights.sum()
new_data[key][unit_index] = (arr[keep_unit_indices, :, :] * weights[:, np.newaxis, np.newaxis]).sum(
0
)
if new_sorting_analyzer.sparsity is not None:
chan_ids = new_sorting_analyzer.sparsity.unit_id_to_channel_indices[unit_id]
mask = ~np.isin(np.arange(arr.shape[2]), chan_ids)
new_data[key][unit_index][:, mask] = 0
return new_data
def _split_extension_data(self, split_units, new_unit_ids, new_sorting_analyzer, verbose=False, **job_kwargs):
if not new_sorting_analyzer.has_extension("waveforms"):
warnings.warn(
"Splitting templates without the 'waveforms' extension will simply copy the template of the unit that "
"was split to the new split units. This is not recommended and may lead to incorrect results. It is "
"recommended to compute the 'waveforms' extension before splitting, or to use 'hard' splitting mode.",
)
new_data = dict()
for operator, arr in self.data.items():
# we first copy the unsplit units
new_array = np.zeros((len(new_sorting_analyzer.unit_ids), arr.shape[1], arr.shape[2]), dtype=arr.dtype)
new_analyzer_unit_ids = list(new_sorting_analyzer.unit_ids)
unsplit_unit_ids = [unit_id for unit_id in self.sorting_analyzer.unit_ids if unit_id not in split_units]
new_indices = np.array([new_analyzer_unit_ids.index(unit_id) for unit_id in unsplit_unit_ids])
old_indices = self.sorting_analyzer.sorting.ids_to_indices(unsplit_unit_ids)
new_array[new_indices, ...] = arr[old_indices, ...]
for split_unit_id, new_splits in zip(split_units, new_unit_ids):
if new_sorting_analyzer.has_extension("waveforms"):
for new_unit_id in new_splits:
split_unit_index = new_sorting_analyzer.sorting.id_to_index(new_unit_id)
wfs = new_sorting_analyzer.get_extension("waveforms").get_waveforms_one_unit(
new_unit_id, force_dense=True
)
if operator == "average":
arr = np.average(wfs, axis=0)
elif operator == "std":
arr = np.std(wfs, axis=0)
elif operator == "median":
arr = np.median(wfs, axis=0)
elif "percentile" in operator:
_, percentile = operator.split("_")
arr = np.percentile(wfs, float(percentile), axis=0)
new_array[split_unit_index, ...] = arr
else:
split_unit_index = self.sorting_analyzer.sorting.id_to_index(split_unit_id)
old_template = arr[split_unit_index, ...]
new_indices = new_sorting_analyzer.sorting.ids_to_indices(new_splits)
new_array[new_indices, ...] = np.tile(old_template, (len(new_splits), 1, 1))
new_data[operator] = new_array
return new_data
def _get_data(self, operator="average", percentile=None, outputs="numpy"):
if operator != "percentile":
key = operator
else:
assert percentile is not None, "You must provide percentile=... if `operator=percentile`"
key = f"percentile_{percentile}"
if key not in self.data.keys():
error_msg = (
f"You have entered `operator={key}`, but the only operators calculated are "
f"{list(self.data.keys())}. Please use one of these as your `operator` in the "
f"`get_data` function."
)
raise ValueError(error_msg)
templates_array = self.data[key]
if outputs == "numpy":
return templates_array
elif outputs == "Templates":
return Templates(
templates_array=templates_array,
sampling_frequency=self.sorting_analyzer.sampling_frequency,
nbefore=self.nbefore,
channel_ids=self.sorting_analyzer.channel_ids,
unit_ids=self.sorting_analyzer.unit_ids,
probe=self.sorting_analyzer.get_probe(),
)
else:
raise ValueError("outputs must be `numpy` or `Templates`")
def get_templates(self, unit_ids=None, operator="average", percentile=None, save=True, outputs="numpy"):
"""
Return templates (average, std, median or percentiles) for multiple units.
If not computed yet then this is computed on demand and optionally saved.
Parameters
----------
unit_ids : list or None
Unit ids to retrieve waveforms for
operator : "average" | "median" | "std" | "percentile", default: "average"
The operator to compute the templates
percentile : float, default: None
Percentile to use for operator="percentile"
save : bool, default: True
In case, the operator is not computed yet it can be saved to folder or zarr
outputs : "numpy" | "Templates", default: "numpy"
Whether to return a numpy array or a Templates object
Returns
-------
templates :np.ndarray | Templates
The returned templates (num_units, num_samples, num_channels)
"""
if operator != "percentile":
key = operator
else:
assert percentile is not None, "You must provide percentile=... if `operator='percentile'`"
key = f"percentile_{percentile}"
if key in self.data:
templates_array = self.data[key]
else:
if operator != "percentile":
self._compute_and_append_from_waveforms([operator])
self.params["operators"] += [operator]
else:
self._compute_and_append_from_waveforms([(operator, percentile)])
self.params["operators"] += [(operator, percentile)]
templates_array = self.data[key]
if save:
if not self.sorting_analyzer.is_read_only():
self.save()
if unit_ids is not None:
unit_indices = self.sorting_analyzer.sorting.ids_to_indices(unit_ids)
templates_array = templates_array[unit_indices, :, :]
else:
unit_ids = self.sorting_analyzer.unit_ids
if outputs == "numpy":
return templates_array
elif outputs == "Templates":
return Templates(
templates_array=templates_array,
sampling_frequency=self.sorting_analyzer.sampling_frequency,
nbefore=self.nbefore,
channel_ids=self.sorting_analyzer.channel_ids,
unit_ids=unit_ids,
probe=self.sorting_analyzer.get_probe(),
is_in_uV=self.sorting_analyzer.return_in_uV,
)
else:
raise ValueError("`outputs` must be 'numpy' or 'Templates'")
def get_unit_template(self, unit_id, operator="average"):
"""
Return template for a single unit.
Parameters
----------
unit_id: str | int
Unit id to retrieve waveforms for
operator: str, default: "average"
The operator to compute the templates
Returns
-------
template: np.array
The returned template (num_samples, num_channels)
"""
templates = self.data[operator]
unit_index = self.sorting_analyzer.sorting.id_to_index(unit_id)
return np.array(templates[unit_index, :, :])
compute_templates = ComputeTemplates.function_factory()
register_result_extension(ComputeTemplates)
class ComputeNoiseLevels(AnalyzerExtension):
"""
Computes the noise level associated with each recording channel.
This function will wraps the `get_noise_levels(recording)` to make the noise levels persistent
on disk (folder or zarr) as a `WaveformExtension`.
The noise levels do not depend on the unit list, only the recording, but it is a convenient way to
retrieve the noise levels directly ine the WaveformExtractor.
Note that the noise levels can be scaled or not, depending on the `return_in_uV` parameter
of the `SortingAnalyzer`.
Parameters
----------
**kwargs : dict
Additional parameters for the `spikeinterface.get_noise_levels()` function
Returns
-------
noise_levels : np.array
The noise level vector
"""
extension_name = "noise_levels"
depend_on = []
need_recording = True
use_nodepipeline = False
need_job_kwargs = True
need_backward_compatibility_on_load = True
def _set_params(self, **noise_level_params):
params = noise_level_params.copy()
return params
def _select_units_extension_data(self, unit_ids):
# this does not depend on units
return self.data
def _select_channels_extension_data(self, channel_ids):
# this does not depend on channels
channel_indices = self.sorting_analyzer.channel_ids_to_indices(channel_ids)
return dict(noise_levels=self.data["noise_levels"][channel_indices])
def _merge_extension_data(
self, merge_unit_groups, new_unit_ids, new_sorting_analyzer, keep_mask=None, verbose=False, **job_kwargs
):
# this does not depend on units
return self.data.copy()
def _split_extension_data(self, split_units, new_unit_ids, new_sorting_analyzer, verbose=False, **job_kwargs):
# this does not depend on units
return self.data.copy()
def _run(self, verbose=False, **job_kwargs):
self.data["noise_levels"] = get_noise_levels(
self.sorting_analyzer.recording,
return_in_uV=self.sorting_analyzer.return_in_uV,
**self.params,
**job_kwargs,
)
def _get_data(self):
return self.data["noise_levels"]
def _handle_backward_compatibility_on_load(self):
# The old parameters used to be params=dict(num_chunks_per_segment=20, chunk_size=10000, seed=None)
# now it is handle more explicitly using random_slices_kwargs=dict()
for key in ("num_chunks_per_segment", "chunk_size", "seed"):
if key in self.params:
if "random_slices_kwargs" not in self.params:
self.params["random_slices_kwargs"] = dict()
self.params["random_slices_kwargs"][key] = self.params.pop(key)
register_result_extension(ComputeNoiseLevels)
compute_noise_levels = ComputeNoiseLevels.function_factory()
class BaseMetric:
"""
Base class for metric-based extension
"""
metric_name = None # to be defined in subclass
metric_params = {} # to be defined in subclass
metric_columns = {} # column names and their dtypes of the dataframe
metric_descriptions = {} # descriptions of each metric column
needs_recording = False # whether the metric needs recording
needs_tmp_data = False # whether the metric needs temporary data computed with MetricExtension._prepare_data
needs_job_kwargs = False # whether the metric needs job_kwargs
supports_periods = False # whether the metric function supports periods
depend_on = [] # extensions the metric depends on
deprecated_names = [] # list of metric names used by previous versions of spikeinterface
# the metric function must have the signature:
# def metric_function(sorting_analyzer, unit_ids, **metric_params)
# or if needs_tmp_data=True
# def metric_function(sorting_analyzer, unit_ids, tmp_data, **metric_params)
# or if needs_job_kwargs=True
# def metric_function(sorting_analyzer, unit_ids, tmp_data, job_kwargs, **metric_params)
# and must return a dict ({unit_id: values}) or namedtuple with fields matching metric_columns keys
metric_function = None # to be defined in subclass
@classmethod
def compute(cls, sorting_analyzer, unit_ids, metric_params, tmp_data, job_kwargs, periods=None):
"""Compute the metric.
Parameters
----------
sorting_analyzer : SortingAnalyzer
The input sorting analyzer
unit_ids : list
List of unit ids to compute the metric for
metric_params : dict
Parameters to override the default metric parameters
tmp_data : dict
Temporary data to pass to the metric function
job_kwargs : dict
Job keyword arguments to control parallelization
periods : np.ndarray | None
Numpy array of unit periods of unit_period_dtype if supports_periods is True
Returns
-------
results: namedtuple
The results of the metric function
"""
args = (sorting_analyzer, unit_ids)
if cls.needs_tmp_data:
args += (tmp_data,)
if cls.needs_job_kwargs:
args += (job_kwargs,)
if cls.supports_periods:
args += (periods,)
results = cls.metric_function(*args, **metric_params)
# if namedtuple, check that columns are correct
if isinstance(results, tuple) and hasattr(results, "_fields"):
assert set(results._fields) == set(list(cls.metric_columns.keys())), (
f"Metric {cls.metric_name} returned columns {results._fields} "
f"but expected columns are {cls.metric_columns.keys()}"
)
return results
class BaseMetricExtension(AnalyzerExtension):
"""
AnalyzerExtension that computes a metric and store the results in a dataframe.
This depends on one or more extensions (see `depend_on` attribute of the `BaseMetric` subclass).
Returns
-------
metric_dataframe : pd.DataFrame
The computed metric dataframe.
"""
extension_name = None # to be defined in subclass
metric_class = None # to be defined in subclass
need_recording = False
use_nodepipeline = False
need_job_kwargs = True
need_backward_compatibility_on_load = False
metric_list: list[BaseMetric] = None # list of BaseMetric
tmp_data_to_save = None
def __init__(self, sorting_analyzer):
super().__init__(sorting_analyzer)
@classmethod
def get_available_metric_names(cls):
"""Get the available metric names.
Returns
-------
available_metric_names : list[str]
List of available metric names.
"""
return [m.metric_name for m in cls.metric_list]
@classmethod
def get_default_metric_params(cls):
"""Get the default metric parameters.
Returns
-------
default_metric_params : dict
Dictionary of default metric parameters for each metric.
"""
default_metric_params = {m.metric_name: deepcopy(m.metric_params) for m in cls.metric_list}
return default_metric_params