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, 296.63it/s]

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

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

Projecting waveforms:   0%|          | 0/10 [00:00<?, ?it/s]
Projecting waveforms: 100%|██████████| 10/10 [00:00<00:00, 971.92it/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  22.143328                   0.0  ...     0.793023      0.024733
1   9.810913                   0.0  ...     0.790789      0.027068
2  27.241290                   0.0  ...     0.827200      0.017244
3   2.407499                   0.0  ...     0.850407      0.012215
4  14.732249                   0.0  ...     0.806977      0.033130
5  19.761894                   0.0  ...     0.823358      0.013978
6  24.953517                   0.0  ...     0.848000      0.019970
7  29.940475                   0.0  ...     0.886391      0.011577
8  43.882032                   0.0  ...     0.951773      0.008799
9  37.989480                   0.0  ...     0.849645      0.007755

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

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
    ['2' '4' '5' '6' '7' '8' '9']
Annotations
  • name : GroundTruthSorting
Properties
    gt_unit_locations[[23.383041 20.382872 36.87392 ] [-9.224725 -7.2354617 48.019665 ] [-3.9684417 20.863522 9.926489 ] [26.830572 0.5480238 45.958157 ] [-6.6226335 0.5590158 6.8864646] [14.677214 8.6232605 20.317032 ] [24.610794 -8.622152 8.125591 ]]
    main_channel_id['3' '0' '1' '2' '0' '2' '2']


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 1.037 seconds)

Gallery generated by Sphinx-Gallery