Quality Metrics Tutorial

After spike sorting, you might want to validate the ‘goodness’ of the sorted units. This can be done using the qualitymetrics submodule, which computes several quality metrics of the sorted units.

import spikeinterface.core as si
import spikeinterface.extractors as se
from spikeinterface.postprocessing import compute_principal_components
from spikeinterface.qualitymetrics import (
    compute_snrs,
    compute_firing_rates,
    compute_isi_violations,
    calculate_pc_metrics,
    compute_quality_metrics,
)

First, let’s download a simulated dataset from the repo ‘https://gin.g-node.org/NeuralEnsemble/ephy_testing_data

local_path = si.download_dataset(remote_path="mearec/mearec_test_10s.h5")
recording, sorting = se.read_mearec(local_path)
print(recording)
print(sorting)
MEArecRecordingExtractor: 32 channels - 32.0kHz - 1 segments - 320,000 samples - 10.00s
                          float32 dtype - 39.06 MiB
  file_path: /home/docs/spikeinterface_datasets/ephy_testing_data/mearec/mearec_test_10s.h5
MEArecSortingExtractor: 10 units - 1 segments - 32.0kHz
  file_path: /home/docs/spikeinterface_datasets/ephy_testing_data/mearec/mearec_test_10s.h5

Create SortingAnalyzer

For quality metrics we need first to create a SortingAnalyzer.

analyzer = si.create_sorting_analyzer(sorting=sorting, recording=recording, format="memory")
print(analyzer)
estimate_sparsity (no parallelization):   0%|          | 0/10 [00:00<?, ?it/s]
estimate_sparsity (no parallelization): 100%|██████████| 10/10 [00:00<00:00, 989.50it/s]
SortingAnalyzer: 32 channels - 10 units - 1 segments - memory - sparse - has recording
Loaded 0 extensions

Depending on which metrics we want to compute we will need first to compute some necessary extensions. (if not computed an error message will be raised)

analyzer.compute("random_spikes", method="uniform", max_spikes_per_unit=600, seed=2205)
analyzer.compute("waveforms", ms_before=1.3, ms_after=2.6, n_jobs=2)
analyzer.compute("templates", operators=["average", "median", "std"])
analyzer.compute("noise_levels")

print(analyzer)
compute_waveforms (workers: 2 processes):   0%|          | 0/10 [00:00<?, ?it/s]
compute_waveforms (workers: 2 processes):  40%|████      | 4/10 [00:00<00:00, 37.62it/s]
compute_waveforms (workers: 2 processes): 100%|██████████| 10/10 [00:00<00:00, 64.28it/s]

noise_level (no parallelization):   0%|          | 0/20 [00:00<?, ?it/s]
noise_level (no parallelization):  30%|███       | 6/20 [00:00<00:00, 52.78it/s]
noise_level (no parallelization):  60%|██████    | 12/20 [00:00<00:00, 52.61it/s]
noise_level (no parallelization):  90%|█████████ | 18/20 [00:00<00:00, 52.74it/s]
noise_level (no parallelization): 100%|██████████| 20/20 [00:00<00:00, 52.67it/s]
SortingAnalyzer: 32 channels - 10 units - 1 segments - memory - sparse - has recording
Loaded 4 extensions: random_spikes, waveforms, templates, noise_levels

The spikeinterface.qualitymetrics submodule has a set of functions that allow users to compute metrics in a compact and easy way. To compute a single metric, one can simply run one of the quality metric functions as shown below. Each function has a variety of adjustable parameters that can be tuned.

firing_rates = compute_firing_rates(analyzer)
print(firing_rates)
isi_violation_ratio, isi_violations_count = compute_isi_violations(analyzer)
print(isi_violation_ratio)
snrs = compute_snrs(analyzer)
print(snrs)
{'#0': 5.3, '#1': 5.0, '#2': 4.3, '#3': 3.0, '#4': 4.8, '#5': 3.7, '#6': 5.1, '#7': 11.1, '#8': 19.5, '#9': 12.9}
{'#0': 0.0, '#1': 0.0, '#2': 0.0, '#3': 0.0, '#4': 0.0, '#5': 0.0, '#6': 0.0, '#7': 0.0, '#8': 0.0, '#9': 0.0}
{'#0': 23.660395884696833, '#1': 25.50878645855212, '#2': 13.774257422244498, '#3': 22.05857988580683, '#4': 7.4551692896444095, '#5': 7.484452767762033, '#6': 20.950501962119713, '#7': 7.366340187368807, '#8': 8.071346883859498, '#9': 8.930637639995314}

To compute more than one metric at once, we can use the compute_quality_metrics function and indicate which metrics we want to compute. This will return a pandas dataframe:

metrics = compute_quality_metrics(analyzer, metric_names=["firing_rate", "snr", "amplitude_cutoff"])
print(metrics)
    firing_rate        snr  amplitude_cutoff
#0          5.3  23.660396               NaN
#1          5.0  25.508786               NaN
#2          4.3  13.774257               NaN
#3          3.0  22.058580               NaN
#4          4.8   7.455169               NaN
#5          3.7   7.484453               NaN
#6          5.1  20.950502               NaN
#7         11.1   7.366340               NaN
#8         19.5   8.071347               NaN
#9         12.9   8.930638               NaN

Some metrics are based on the principal component scores, so the exwtension need to be computed before. For instance:

analyzer.compute("principal_components", n_components=3, mode="by_channel_global", whiten=True)

metrics = compute_quality_metrics(
    analyzer,
    metric_names=[
        "isolation_distance",
        "d_prime",
    ],
)
print(metrics)
Fitting PCA:   0%|          | 0/10 [00:00<?, ?it/s]
Fitting PCA:  90%|█████████ | 9/10 [00:00<00:00, 72.25it/s]
Fitting PCA: 100%|██████████| 10/10 [00:00<00:00, 62.81it/s]

Projecting waveforms:   0%|          | 0/10 [00:00<?, ?it/s]
Projecting waveforms: 100%|██████████| 10/10 [00:00<00:00, 238.39it/s]

calculate pc_metrics:   0%|          | 0/10 [00:00<?, ?it/s]
calculate pc_metrics:  40%|████      | 4/10 [00:00<00:00, 39.96it/s]
calculate pc_metrics: 100%|██████████| 10/10 [00:00<00:00, 60.47it/s]
    isolation_distance    d_prime  amplitude_cutoff        snr  firing_rate
#0        1.116311e+17  29.314861               NaN  23.660396          5.3
#1        2.250397e+04  27.426984               NaN  25.508786          5.0
#2        1.260674e+17  24.759110               NaN  13.774257          4.3
#3        8.952986e+17  30.909950               NaN  22.058580          3.0
#4        1.674176e+17  28.448716               NaN   7.455169          4.8
#5        8.661268e+16  20.730191               NaN   7.484453          3.7
#6        2.142995e+18  37.228947               NaN  20.950502          5.1
#7        8.878391e+03  28.968743               NaN   7.366340         11.1
#8        5.827407e+03  20.999116               NaN   8.071347         19.5
#9        7.100391e+03  30.249492               NaN   8.930638         12.9

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

Gallery generated by Sphinx-Gallery