Note
Go to the end to download the full example code.
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
from spikeinterface.qualitymetrics import (
compute_snrs,
compute_firing_rates,
compute_isi_violations,
compute_quality_metrics,
)
First, let’s generate a simulated recording and sorting
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 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, 363.54it/s]
SortingAnalyzer: 4 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): 20%|██ | 2/10 [00:00<00:00, 18.22it/s]
compute_waveforms (workers: 2 processes): 100%|██████████| 10/10 [00:00<00:00, 51.21it/s]
noise_level (no parallelization): 0%| | 0/20 [00:00<?, ?it/s]
noise_level (no parallelization): 100%|██████████| 20/20 [00:00<00:00, 254.85it/s]
SortingAnalyzer: 4 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)
{np.str_('0'): 13.4, np.str_('1'): 13.0, np.str_('2'): 16.6, np.str_('3'): 14.0, np.str_('4'): 13.9, np.str_('5'): 13.8, np.str_('6'): 15.3, np.str_('7'): 13.9, np.str_('8'): 15.8, np.str_('9'): 14.3}
{np.str_('0'): np.float64(0.0), np.str_('1'): np.float64(0.0), np.str_('2'): np.float64(0.0), np.str_('3'): np.float64(0.0), np.str_('4'): np.float64(0.0), np.str_('5'): np.float64(0.0), np.str_('6'): np.float64(0.0), np.str_('7'): np.float64(0.0), np.str_('8'): np.float64(0.0), np.str_('9'): np.float64(0.0)}
{np.str_('0'): np.float64(8.813046455682326), np.str_('1'): np.float64(31.178950114650185), np.str_('2'): np.float64(12.086973259300537), np.str_('3'): np.float64(44.20333888874252), np.str_('4'): np.float64(12.114543850527564), np.str_('5'): np.float64(17.52691772918083), np.str_('6'): np.float64(28.054482398352924), np.str_('7'): np.float64(23.13445074364992), np.str_('8'): np.float64(26.02667401944571), np.str_('9'): np.float64(54.13461155620695)}
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 13.4 8.813046 NaN
1 13.0 31.178950 NaN
2 16.6 12.086973 NaN
3 14.0 44.203339 NaN
4 13.9 12.114544 NaN
5 13.8 17.526918 NaN
6 15.3 28.054482 NaN
7 13.9 23.134451 NaN
8 15.8 26.026674 NaN
9 14.3 54.134612 NaN
Some metrics are based on the principal component scores, so the exwtension must 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: 100%|██████████| 10/10 [00:00<00:00, 218.08it/s]
Projecting waveforms: 0%| | 0/10 [00:00<?, ?it/s]
Projecting waveforms: 100%|██████████| 10/10 [00:00<00:00, 1259.51it/s]
calculate pc_metrics: 0%| | 0/10 [00:00<?, ?it/s]
calculate pc_metrics: 100%|██████████| 10/10 [00:00<00:00, 352.21it/s]
isolation_distance d_prime snr amplitude_cutoff firing_rate
0 25.419220 2.004480 8.813046 NaN 13.4
1 114.651753 3.681103 31.178950 NaN 13.0
2 60.410283 1.669974 12.086973 NaN 16.6
3 611.918300 7.289233 44.203339 NaN 14.0
4 20.047994 1.537571 12.114544 NaN 13.9
5 35.354602 1.521533 17.526918 NaN 13.8
6 234.493479 4.381197 28.054482 NaN 15.3
7 457.358844 5.704074 23.134451 NaN 13.9
8 170.826441 4.279759 26.026674 NaN 15.8
9 947.211467 10.774341 54.134612 NaN 14.3
Total running time of the script: (0 minutes 0.492 seconds)