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

from spikeinterface.qualitymetrics import compute_quality_metrics

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

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

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

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

Projecting waveforms:   0%|          | 0/10 [00:00<?, ?it/s]
Projecting waveforms: 100%|██████████| 10/10 [00:00<00:00, 2628.67it/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 = compute_quality_metrics(analyzer, metric_names=["snr", "isi_violation", "nearest_neighbor"])
print(metrics)
calculate pc_metrics:   0%|          | 0/10 [00:00<?, ?it/s]
calculate pc_metrics:  70%|███████   | 7/10 [00:00<00:00, 61.47it/s]
calculate pc_metrics: 100%|██████████| 10/10 [00:00<00:00, 61.39it/s]
         snr  isi_violations_ratio  ...  nn_hit_rate  nn_miss_rate
0   3.440702                   0.0  ...     0.788217      0.028444
1  27.194571                   0.0  ...     0.930147      0.005141
2  21.511744                   0.0  ...     0.908898      0.008077
3   6.302877                   0.0  ...     0.796552      0.023773
4   8.326862                   0.0  ...     0.785484      0.028594
5   1.545711                   0.0  ...     0.790909      0.026285
6   8.726305                   0.0  ...     0.794304      0.020914
7  10.016860                   0.0  ...     0.782946      0.021970
8  38.791628                   0.0  ...     0.934564      0.006923
9  46.477512                   0.0  ...     0.952555      0.004954

[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.90)
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     True
2     True
3    False
4    False
5    False
6    False
7    False
8     True
9     True
dtype: bool
['1', '2', '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")
GroundTruthSorting (UnitsSelectionSorting): 4 units - 1 segments - 25.0kHz
NumpyFolder (NumpyFolderSorting): 4 units - 1 segments - 25.0kHz
Unit IDs
    ['1' '2' '8' '9']
Annotations
  • name : GroundTruthSorting
Properties
    gt_unit_locations[[-3.1788092 -0.66498977 5.4686303 ] [-1.8473644 27.096647 24.395226 ] [14.247956 20.412434 5.571841 ] [19.723257 -6.637303 8.449161 ]]


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 - 4 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.489 seconds)

Gallery generated by Sphinx-Gallery