Note
Click here to download the full example code
Run spike sorting algorithms¶
This example shows the basic usage of the spikeinterface.sorters
module of SpikeInterface
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
recording, sorting_true = se.toy_example(duration=10, seed=0, num_segments=1)
print(recording)
print(sorting_true)
NumpyRecording: 4 channels - 1 segments - 30.0kHz - 10.000s
NumpySorting: 10 units - 1 segments - 30.0kHz
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())
RUNNING SHELL SCRIPT: /tmp/tmp_shellscriptrhd2hkbb/script.sh
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.95.0/lib/python3.8/site-packages/spikeinterface/sorters/hdsort/hdsort.py:92: ResourceWarning: unclosed file <_io.TextIOWrapper name=5 encoding='UTF-8'>
if cls.check_compiled():
RUNNING SHELL SCRIPT: /tmp/tmp_shellscriptzn6txupb/script.sh
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.95.0/lib/python3.8/site-packages/spikeinterface/sorters/ironclust/ironclust.py:125: ResourceWarning: unclosed file <_io.TextIOWrapper name=5 encoding='UTF-8'>
if cls.check_compiled():
RUNNING SHELL SCRIPT: /tmp/tmp_shellscriptaohaomvz/script.sh
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.95.0/lib/python3.8/site-packages/spikeinterface/sorters/kilosort/kilosort.py:75: ResourceWarning: unclosed file <_io.TextIOWrapper name=5 encoding='UTF-8'>
if cls.check_compiled():
RUNNING SHELL SCRIPT: /tmp/tmp_shellscript_qgku4qe/script.sh
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.95.0/lib/python3.8/site-packages/spikeinterface/sorters/kilosort2/kilosort2.py:85: ResourceWarning: unclosed file <_io.TextIOWrapper name=5 encoding='UTF-8'>
if cls.check_compiled():
RUNNING SHELL SCRIPT: /tmp/tmp_shellscriptj20jdoro/script.sh
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.95.0/lib/python3.8/site-packages/spikeinterface/sorters/kilosort2_5/kilosort2_5.py:100: ResourceWarning: unclosed file <_io.TextIOWrapper name=5 encoding='UTF-8'>
if cls.check_compiled():
RUNNING SHELL SCRIPT: /tmp/tmp_shellscriptiiy3y0a4/script.sh
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.95.0/lib/python3.8/site-packages/spikeinterface/sorters/kilosort3/kilosort3.py:98: ResourceWarning: unclosed file <_io.TextIOWrapper name=5 encoding='UTF-8'>
if cls.check_compiled():
RUNNING SHELL SCRIPT: /tmp/tmp_shellscriptivqo936p/script.sh
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.95.0/lib/python3.8/site-packages/spikeinterface/sorters/waveclus/waveclus.py:125: ResourceWarning: unclosed file <_io.TextIOWrapper name=5 encoding='UTF-8'>
if cls.check_compiled():
RUNNING SHELL SCRIPT: /tmp/tmp_shellscript3hd5c03a/script.sh
/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.95.0/lib/python3.8/site-packages/spikeinterface/sorters/waveclus/waveclus_snippets.py:83: ResourceWarning: unclosed file <_io.TextIOWrapper name=5 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',
'chunk_memory': None,
'chunk_size': None,
'common_ref_removal': False,
'detect_sign': -1,
'detect_threshold': 5,
'freq_max': 5000.0,
'freq_min': 400.0,
'n_jobs': 1,
'nested_params': None,
'progress_bar': True,
'total_memory': None}
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, )
Traceback (most recent call last):
File "/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/checkouts/0.95.0/examples/modules/sorters/plot_1_sorters_example.py", line 49, in <module>
sorting_TDC_5 = ss.run_tridesclous(recording=recording, output_folder='tmp_TDC_5',
File "/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.95.0/lib/python3.8/site-packages/spikeinterface/sorters/runsorter.py", line 619, in run_tridesclous
return run_sorter('tridesclous', *args, **kwargs)
File "/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.95.0/lib/python3.8/site-packages/spikeinterface/sorters/runsorter.py", line 142, in run_sorter
return run_sorter_local(**common_kwargs)
File "/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.95.0/lib/python3.8/site-packages/spikeinterface/sorters/runsorter.py", line 162, in run_sorter_local
SorterClass.run_from_folder(output_folder, raise_error, verbose)
File "/home/docs/checkouts/readthedocs.org/user_builds/spikeinterface/conda/0.95.0/lib/python3.8/site-packages/spikeinterface/sorters/basesorter.py", line 261, in run_from_folder
raise SpikeSortingError(
spikeinterface.sorters.utils.misc.SpikeSortingError: Spike sorting failed. You can inspect the runtime trace in tmp_TDC_5/spikeinterface_log.json
# parameters set by params dictionary
sorting_TDC_8 = ss.run_tridesclous(recording=recording, output_folder='tmp_TDC_8',
detect_threshold=8.)
print('Units found with threshold = 5:', sorting_TDC_5.get_unit_ids())
print('Units found with threshold = 8:', sorting_TDC_8.get_unit_ids())
- 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 0.701 seconds)