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)
compute_waveforms (no parallelization):   0%|          | 0/10 [00:00<?, ?it/s]
compute_waveforms (no parallelization): 100%|██████████| 10/10 [00:00<00:00, 321.29it/s]

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

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

Projecting waveforms:   0%|          | 0/10 [00:00<?, ?it/s]
Projecting waveforms: 100%|██████████| 10/10 [00:00<00:00, 972.32it/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  28.485379                   0.0  ...     0.915854      0.008123
1  26.386939                   0.0  ...     0.851064      0.009488
2   8.005805                   0.0  ...     0.838037      0.019296
3  42.255894                   0.0  ...     0.975540      0.003158
4   7.997795                   0.0  ...     0.808219      0.025246
5  16.197258                   0.0  ...     0.762044      0.024324
6  23.761547                   0.0  ...     0.936054      0.009380
7  22.165261                   0.0  ...     0.879487      0.014623
8  11.679941                   0.0  ...     0.788406      0.022389
9  10.701705                   0.0  ...     0.740580      0.028550

[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     True
4     True
5    False
6     True
7     True
8    False
9    False
dtype: bool
['0', '1', '2', '3', '4', '6', '7']

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' '3' '4' '6' '7']
Annotations
  • name : GroundTruthSorting
Properties
    gt_unit_locations[[28.893656 27.031961 17.021585 ] [ 7.3501096 -2.1806474 8.176445 ] [28.810722 -7.4131804 48.003193 ] [18.613377 -4.2252903 25.05212 ] [-7.9965672 9.044611 15.209823 ] [18.521198 22.323235 43.76862 ] [-4.357006 29.29354 40.189873 ]]
    main_channel_id['3' '0' '2' '2' '0' '3' '1']


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.550 seconds)

Gallery generated by Sphinx-Gallery