Run spike sorting algorithms

This example shows the basic usage of the spikeinterface.sorters module of SpikeInterface

import spikeinterface as si
import spikeinterface.extractors as se
import spikeinterface.sorters as ss
from pprint import pprint

First, let’s create a toy example:  We choose explicitly one segment because many sorters handle only recording with unique segment

local_path = si.download_dataset(remote_path='mearec/mearec_test_10s.h5')
recording, sorting_true = se.read_mearec(local_path)
print(recording)
print(sorting_true)
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

Check available sorters

pprint(ss.available_sorters())
['combinato',
 'hdsort',
 'herdingspikes',
 'ironclust',
 'kilosort',
 'kilosort2',
 'kilosort2_5',
 'kilosort3',
 'klusta',
 'mountainsort4',
 'pykilosort',
 'spykingcircus',
 'spykingcircus2',
 'tridesclous',
 'tridesclous2',
 'waveclus',
 'waveclus_snippets',
 'yass']

This will list the sorters available through SpikeInterface. To see which sorters are installed on the machine you can run:

pprint(ss.installed_sorters())
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface-test/conda/stable/lib/python3.8/site-packages/spikeinterface/sorters/external/hdsort.py:92: ResourceWarning: unclosed file <_io.TextIOWrapper name=9 encoding='UTF-8'>
  if cls.check_compiled():
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface-test/conda/stable/lib/python3.8/site-packages/spikeinterface/sorters/external/ironclust.py:125: ResourceWarning: unclosed file <_io.TextIOWrapper name=9 encoding='UTF-8'>
  if cls.check_compiled():
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface-test/conda/stable/lib/python3.8/site-packages/spikeinterface/sorters/external/kilosort.py:75: ResourceWarning: unclosed file <_io.TextIOWrapper name=9 encoding='UTF-8'>
  if cls.check_compiled():
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface-test/conda/stable/lib/python3.8/site-packages/spikeinterface/sorters/external/kilosort2.py:85: ResourceWarning: unclosed file <_io.TextIOWrapper name=9 encoding='UTF-8'>
  if cls.check_compiled():
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface-test/conda/stable/lib/python3.8/site-packages/spikeinterface/sorters/external/kilosort2_5.py:100: ResourceWarning: unclosed file <_io.TextIOWrapper name=9 encoding='UTF-8'>
  if cls.check_compiled():
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface-test/conda/stable/lib/python3.8/site-packages/spikeinterface/sorters/external/kilosort3.py:98: ResourceWarning: unclosed file <_io.TextIOWrapper name=9 encoding='UTF-8'>
  if cls.check_compiled():
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface-test/conda/stable/lib/python3.8/site-packages/spikeinterface/sorters/external/waveclus.py:125: ResourceWarning: unclosed file <_io.TextIOWrapper name=9 encoding='UTF-8'>
  if cls.check_compiled():
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface-test/conda/stable/lib/python3.8/site-packages/spikeinterface/sorters/external/waveclus_snippets.py:83: ResourceWarning: unclosed file <_io.TextIOWrapper name=9 encoding='UTF-8'>
  if cls.check_compiled():
['herdingspikes',
 'mountainsort4',
 'spykingcircus2',
 'tridesclous',
 'tridesclous2']

Change sorter parameters

default_TDC_params = ss.TridesclousSorter.default_params()
pprint(default_TDC_params)
{'chunk_duration': '1s',
 'common_ref_removal': False,
 'detect_sign': -1,
 'detect_threshold': 5,
 'freq_max': 5000.0,
 'freq_min': 400.0,
 'n_jobs': 2,
 'nested_params': None,
 'progress_bar': True}

Parameters can be changed either by passing a full dictionary, or by passing single arguments.

# tridesclous spike sorting
default_TDC_params['detect_threshold'] = 5

# parameters set by params dictionary
sorting_TDC_5 = ss.run_tridesclous(recording=recording, output_folder='tmp_TDC_5',
                                   **default_TDC_params, )
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface-test/conda/stable/lib/python3.8/site-packages/tridesclous/dataio.py:175: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
  v1 = distutils.version.LooseVersion(tridesclous_version).version
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface-test/conda/stable/lib/python3.8/site-packages/tridesclous/dataio.py:176: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
  v2 = distutils.version.LooseVersion(self.info['tridesclous_version']).version
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface-test/conda/stable/lib/python3.8/site-packages/tridesclous/dataio.py:175: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
  v1 = distutils.version.LooseVersion(tridesclous_version).version
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface-test/conda/stable/lib/python3.8/site-packages/tridesclous/dataio.py:176: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
  v2 = distutils.version.LooseVersion(self.info['tridesclous_version']).version
# parameters set by params dictionary
sorting_TDC_8 = ss.run_tridesclous(recording=recording, output_folder='tmp_TDC_8',
                                   detect_threshold=8.)
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface-test/conda/stable/lib/python3.8/site-packages/tridesclous/dataio.py:175: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
  v1 = distutils.version.LooseVersion(tridesclous_version).version
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface-test/conda/stable/lib/python3.8/site-packages/tridesclous/dataio.py:176: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
  v2 = distutils.version.LooseVersion(self.info['tridesclous_version']).version
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface-test/conda/stable/lib/python3.8/site-packages/tridesclous/dataio.py:175: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
  v1 = distutils.version.LooseVersion(tridesclous_version).version
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface-test/conda/stable/lib/python3.8/site-packages/tridesclous/dataio.py:176: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
  v2 = distutils.version.LooseVersion(self.info['tridesclous_version']).version
print('Units found with threshold = 5:', sorting_TDC_5.get_unit_ids())
print('Units found with threshold = 8:', sorting_TDC_8.get_unit_ids())
Units found with threshold = 5: [0 1 2 3 4 5 6 7 8]
Units found with threshold = 8: [0 1 2 3 4 5 6 7]
Run other sorters

 Some sorters (kilosort, ironclust, hdsort, …) need to manually set the path to the source folder

# KiloSort spike sorting (KILOSORT_PATH and NPY_MATLAB_PATH can be set as environment variables)
# sorting_KS = ss.run_kilosort(recording, output_folder='tmp_KS')
#  print('Units found with Kilosort:', sorting_KS.get_unit_ids())
# Kilosort2 spike sorting (KILOSORT2_PATH and NPY_MATLAB_PATH can be set as environment variables)
# sorting_KS2 = ss.run_kilosort2(recording, output_folder='tmp_KS2')
#  print('Units found with Kilosort2', sorting_KS2.get_unit_ids())
# Klusta spike sorting
#  sorting_KL = ss.run_klusta(recording, output_folder='tmp_KL')
# print('Units found with Klusta:', sorting_KL.get_unit_ids())
# IronClust spike sorting (IRONCLUST_PATH can be set as environment variables)
# sorting_IC = ss.run_ironclust(recording, output_folder='tmp_IC')
# print('Units found with Ironclust:', sorting_IC.get_unit_ids())
# Tridesclous spike sorting
# sorting_TDC = ss.run_tridesclous(recording, output_folder='tmp_TDC')
# print('Units found with Tridesclous:', sorting_TDC.get_unit_ids())

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

Gallery generated by Sphinx-Gallery