Curation Tutorial

After spike sorting and computing quality metrics, you can automatically curate the spike sorting output using the quality metrics.

import spikeinterface as si
import spikeinterface.extractors as se
import spikeinterface.toolkit as st
import spikeinterface.sorters as ss
First, let’s download a simulated dataset

from the repo ‘https://gin.g-node.org/NeuralEnsemble/ephy_testing_data

Let’s imagine that the ground-truth sorting is in fact the output of a sorter.

local_path = si.download_dataset(remote_path='mearec/mearec_test_10s.h5')
recording = se.MEArecRecordingExtractor(local_path)
sorting = se.MEArecSortingExtractor(local_path)
print(recording)
print(sorting)

Out:

/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/MEArec/tools.py:339: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
  if StrictVersion(mearec_version) >= '1.5.0':
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/MEArec/tools.py:339: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
  if StrictVersion(mearec_version) >= '1.5.0':
MEArecRecordingExtractor: 32 channels - 1 segments - 32.0kHz - 10.000s
  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

First, we extract waveforms and compute their PC scores:

folder = 'waveforms_mearec'
we = si.extract_waveforms(recording, sorting, folder,
                          load_if_exists=True,
                          ms_before=1, ms_after=2., max_spikes_per_unit=500,
                          n_jobs=1, chunk_size=30000)
print(we)

pc = st.compute_principal_components(we, load_if_exists=True,
                                     n_components=3, mode='by_channel_local')
print(pc)

Out:

/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/MEArec/tools.py:339: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
  if StrictVersion(mearec_version) >= '1.5.0':
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/MEArec/tools.py:339: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
  if StrictVersion(mearec_version) >= '1.5.0':
WaveformExtractor: 32 channels - 10 units - 1 segments
  before:32 after:64 n_per_units:500
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/MEArec/tools.py:339: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
  if StrictVersion(mearec_version) >= '1.5.0':
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/MEArec/tools.py:339: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
  if StrictVersion(mearec_version) >= '1.5.0':
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_1.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_2.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_3.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_4.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_5.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_6.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_7.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_8.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_9.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_10.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_11.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_12.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_13.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_14.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_15.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_16.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_17.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_18.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_19.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_20.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_21.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_22.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_23.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_24.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_25.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_26.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_27.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_28.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_29.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_30.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_31.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_32.pkl'>
  pca = pickle.load(pca_file.open("rb"))
WaveformPrincipalComponent: 32 channels - 1 segments
  mode:by_channel_local n_components:3

Then we compute some quality metrics:

metrics = st.compute_quality_metrics(we, metric_names=['snr', 'isi_violation', 'nearest_neighbor'])
print(metrics)

Out:

/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/MEArec/tools.py:339: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
  if StrictVersion(mearec_version) >= '1.5.0':
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/MEArec/tools.py:339: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
  if StrictVersion(mearec_version) >= '1.5.0':
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_1.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_2.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_3.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_4.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_5.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_6.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_7.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_8.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_9.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_10.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_11.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_12.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_13.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_14.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_15.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_16.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_17.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_18.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_19.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_20.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_21.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_22.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_23.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_24.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_25.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_26.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_27.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_28.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_29.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_30.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_31.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:118: ResourceWarning: unclosed file <_io.BufferedReader name='waveforms_mearec/principal_components/pca_model_by_channel_local_32.pkl'>
  pca = pickle.load(pca_file.open("rb"))
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:193: DeprecationWarning: The 'get_all_components()' function has been substituted by the 'get_all_projections()' function and it will be removed in the next release
  warnings.warn("The 'get_all_components()' function has been substituted by the 'get_all_projections()' "
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:193: DeprecationWarning: The 'get_all_components()' function has been substituted by the 'get_all_projections()' function and it will be removed in the next release
  warnings.warn("The 'get_all_components()' function has been substituted by the 'get_all_projections()' "
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:193: DeprecationWarning: The 'get_all_components()' function has been substituted by the 'get_all_projections()' function and it will be removed in the next release
  warnings.warn("The 'get_all_components()' function has been substituted by the 'get_all_projections()' "
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:193: DeprecationWarning: The 'get_all_components()' function has been substituted by the 'get_all_projections()' function and it will be removed in the next release
  warnings.warn("The 'get_all_components()' function has been substituted by the 'get_all_projections()' "
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:193: DeprecationWarning: The 'get_all_components()' function has been substituted by the 'get_all_projections()' function and it will be removed in the next release
  warnings.warn("The 'get_all_components()' function has been substituted by the 'get_all_projections()' "
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:193: DeprecationWarning: The 'get_all_components()' function has been substituted by the 'get_all_projections()' function and it will be removed in the next release
  warnings.warn("The 'get_all_components()' function has been substituted by the 'get_all_projections()' "
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:193: DeprecationWarning: The 'get_all_components()' function has been substituted by the 'get_all_projections()' function and it will be removed in the next release
  warnings.warn("The 'get_all_components()' function has been substituted by the 'get_all_projections()' "
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:193: DeprecationWarning: The 'get_all_components()' function has been substituted by the 'get_all_projections()' function and it will be removed in the next release
  warnings.warn("The 'get_all_components()' function has been substituted by the 'get_all_projections()' "
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:193: DeprecationWarning: The 'get_all_components()' function has been substituted by the 'get_all_projections()' function and it will be removed in the next release
  warnings.warn("The 'get_all_components()' function has been substituted by the 'get_all_projections()' "
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.93.0/lib/python3.8/site-packages/spikeinterface/toolkit/postprocessing/principal_component.py:193: DeprecationWarning: The 'get_all_components()' function has been substituted by the 'get_all_projections()' function and it will be removed in the next release
  warnings.warn("The 'get_all_components()' function has been substituted by the 'get_all_projections()' "
          snr  isi_violations_ratio  ...  nn_hit_rate  nn_miss_rate
#0  23.449940                   0.0  ...     0.996226      0.003468
#1  25.565849                   0.0  ...     0.960000      0.001727
#2  11.997738                   0.0  ...     0.925581      0.003989
#3  21.852982                   0.0  ...     0.993333      0.000000
#4   6.896532                   0.0  ...     0.975000      0.000861
#5   5.176554                   0.0  ...     0.978378      0.001130
#6  19.937574                   0.0  ...     0.980392      0.000576
#7   7.456619                   0.0  ...     0.932727      0.006929
#8   6.640268                   0.0  ...     0.972165      0.018512
#9   7.247384                   0.0  ...     0.945736      0.009091

[10 rows x 6 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 dataframe:

keep_mask = (metrics['snr'] > 7.5) & (metrics['isi_violations_rate'] < 0.05) & (metrics['nn_hit_rate'] > 0.90)
print(keep_mask)

keep_unit_ids = keep_mask[keep_mask].index.values
print(keep_unit_ids)

Out:

#0     True
#1     True
#2     True
#3     True
#4    False
#5    False
#6     True
#7    False
#8    False
#9    False
dtype: bool
['#0' '#1' '#2' '#3' '#6']

And now let’s create a sorting that contains only curated units and save it, for example to an NPZ file.

curated_sorting = sorting.select_units(keep_unit_ids)
print(curated_sorting)
se.NpzSortingExtractor.write_sorting(curated_sorting, 'curated_sorting.pnz')

Out:

UnitsSelectionSorting: 5 units - 1 segments - 32.0kHz

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

Gallery generated by Sphinx-Gallery