Curation Tutorial

After spike sorting and computing quality metrics, you can automatically curate the spike sorting output using the quality metrics that you have calculated.

Import the modules and/or functions necessary from spikeinterface

import spikeinterface.core as si

Let’s generate a simulated dataset, and imagine that the ground-truth sorting is in fact the output of a sorter.

recording, sorting = si.generate_ground_truth_recording()
print(recording)
print(sorting)
GroundTruthRecording (InjectTemplatesRecording): 4 channels - 25.0kHz - 1 segments
                      250,000 samples - 10.00s - float32 dtype - 3.81 MiB
GroundTruthSorting (NumpySorting): 10 units - 1 segments - 25.0kHz

Create SortingAnalyzer

For this example, we will need a SortingAnalyzer and some extensions to be computed first

analyzer = si.create_sorting_analyzer(sorting=sorting, recording=recording, format="memory")
analyzer.compute(["random_spikes", "waveforms", "templates", "noise_levels"])

analyzer.compute("principal_components", n_components=3, mode="by_channel_local")
print(analyzer)
estimate_sparsity (no parallelization):   0%|          | 0/10 [00:00<?, ?it/s]
estimate_sparsity (no parallelization): 100%|██████████| 10/10 [00:00<00:00, 372.19it/s]

compute_waveforms (no parallelization):   0%|          | 0/10 [00:00<?, ?it/s]
compute_waveforms (no parallelization): 100%|██████████| 10/10 [00:00<00:00, 295.84it/s]

noise_level (no parallelization):   0%|          | 0/20 [00:00<?, ?it/s]
noise_level (no parallelization): 100%|██████████| 20/20 [00:00<00:00, 263.70it/s]

Fitting PCA:   0%|          | 0/10 [00:00<?, ?it/s]
Fitting PCA: 100%|██████████| 10/10 [00:00<00:00, 138.70it/s]

Projecting waveforms:   0%|          | 0/10 [00:00<?, ?it/s]
Projecting waveforms: 100%|██████████| 10/10 [00:00<00:00, 1303.79it/s]
SortingAnalyzer: 4 channels - 10 units - 1 segments - memory - sparse - has recording
Loaded 5 extensions: random_spikes, waveforms, templates, noise_levels, principal_components

Then we compute some quality metrics:

metrics_ext = analyzer.compute("quality_metrics", metric_names=["snr", "isi_violation", "nearest_neighbor"])
metrics = metrics_ext.get_data()
print(metrics)
         snr  isi_violations_ratio  ...  nn_hit_rate  nn_miss_rate
0  27.858968                   0.0  ...     0.881538      0.005718
1  20.337050                   0.0  ...     0.885714      0.015988
2  15.134126                   0.0  ...     0.802597      0.019104
3  11.507838                   0.0  ...     0.793464      0.028784
4  10.229632                   0.0  ...     0.803822      0.032461
5  21.573035                   0.0  ...     0.898667      0.006845
6  34.229909                   0.0  ...     0.897183      0.008876
7   1.234529                   0.0  ...     0.819481      0.018358
8  13.878169                   0.0  ...     0.835294      0.017378
9   6.017924                   0.0  ...     0.770667      0.027083

[10 rows x 5 columns]

We can now threshold each quality metric and select units based on some rules.

The easiest and most intuitive way is to use boolean masking with a dataframe.

Then create a list of unit ids that we want to keep

keep_mask = (metrics["snr"] > 7.5) & (metrics["isi_violations_ratio"] < 0.2) & (metrics["nn_hit_rate"] > 0.80)
print(keep_mask)

keep_unit_ids = keep_mask[keep_mask].index.values
keep_unit_ids = [unit_id for unit_id in keep_unit_ids]
print(keep_unit_ids)
0     True
1     True
2     True
3    False
4     True
5     True
6     True
7    False
8     True
9    False
dtype: bool
['0', '1', '2', '4', '5', '6', '8']

And now let’s create a sorting that contains only curated units and save it.

curated_sorting = sorting.select_units(keep_unit_ids)
print(curated_sorting)


curated_sorting.save(folder="curated_sorting", overwrite=True)
GroundTruthSorting (UnitsSelectionSorting): 7 units - 1 segments - 25.0kHz
NumpyFolder (NumpyFolderSorting): 7 units - 1 segments - 25.0kHz
Unit IDs
    ['0' '1' '2' '4' '5' '6' '8']
Annotations
  • name : GroundTruthSorting
Properties
    gt_unit_locations[[29.649006 4.307902 14.93651 ] [10.443031 9.73644 18.766844 ] [-3.267282 -4.9564695 28.066803 ] [ 1.0060698 4.7063975 45.862854 ] [-2.157988 25.733515 7.5541234] [23.412516 29.101961 7.251315 ] [-9.590494 6.785067 10.15103 ]]


We can also save the analyzer with only theses units

clean_analyzer = analyzer.select_units(unit_ids=keep_unit_ids, format="zarr", folder="clean_analyzer")

print(clean_analyzer)
SortingAnalyzer: 4 channels - 7 units - 1 segments - zarr - sparse - has recording
Loaded 6 extensions: random_spikes, waveforms, templates, noise_levels, principal_components, quality_metrics

Total running time of the script: (0 minutes 0.654 seconds)

Gallery generated by Sphinx-Gallery