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, 398.78it/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): 70%|███████ | 7/10 [00:00<00:00, 61.57it/s]
compute_waveforms (workers: 2 processes): 100%|██████████| 10/10 [00:00<00:00, 78.86it/s]
noise_level (no parallelization): 0%| | 0/20 [00:00<?, ?it/s]
noise_level (no parallelization): 100%|██████████| 20/20 [00:00<00:00, 284.55it/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'): 14.2, np.str_('1'): 15.8, np.str_('2'): 14.1, np.str_('3'): 15.3, np.str_('4'): 14.4, np.str_('5'): 16.4, np.str_('6'): 14.3, np.str_('7'): 12.5, np.str_('8'): 14.3, np.str_('9'): 14.2}
{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(7.057331210324123), np.str_('1'): np.float64(7.952639139858965), np.str_('2'): np.float64(24.39620373538077), np.str_('3'): np.float64(19.797953778743743), np.str_('4'): np.float64(11.141984909365796), np.str_('5'): np.float64(15.625148982867573), np.str_('6'): np.float64(27.66876462754578), np.str_('7'): np.float64(6.80275519893571), np.str_('8'): np.float64(19.18407739605691), np.str_('9'): np.float64(42.27177014828746)}
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 14.2 7.057331 NaN
1 15.8 7.952639 NaN
2 14.1 24.396204 NaN
3 15.3 19.797954 NaN
4 14.4 11.141985 NaN
5 16.4 15.625149 NaN
6 14.3 27.668765 NaN
7 12.5 6.802755 NaN
8 14.3 19.184077 NaN
9 14.2 42.271770 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, 189.22it/s]
Projecting waveforms: 0%| | 0/10 [00:00<?, ?it/s]
Projecting waveforms: 100%|██████████| 10/10 [00:00<00:00, 2405.96it/s]
calculate pc_metrics: 0%| | 0/10 [00:00<?, ?it/s]
calculate pc_metrics: 100%|██████████| 10/10 [00:00<00:00, 540.02it/s]
isolation_distance d_prime snr firing_rate amplitude_cutoff
0 19.110960 2.775378 7.057331 14.2 NaN
1 23.528726 2.396316 7.952639 15.8 NaN
2 121.887451 3.447779 24.396204 14.1 NaN
3 184.337724 7.040257 19.797954 15.3 NaN
4 34.127789 1.342211 11.141985 14.4 NaN
5 97.350919 3.405871 15.625149 16.4 NaN
6 156.053961 5.489057 27.668765 14.3 NaN
7 24.314433 2.502411 6.802755 12.5 NaN
8 45.907389 1.810996 19.184077 14.3 NaN
9 114.298171 7.891842 42.271770 14.2 NaN
Total running time of the script: (0 minutes 0.383 seconds)