Run spike sorting algorithms

This example shows the basic usage of the 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)


NumpyRecording: 4 channels - 1 segments - 30.0kHz - 10.000s
NumpySorting: 10 units - 1 segments - 30.0kHz

Check available sorters




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



['herdingspikes', 'mountainsort4', 'tridesclous']

Change sorter parameters

default_TDC_params = ss.TridesclousSorter.default_params()


{'common_ref_removal': False,
 'detect_sign': -1,
 'detect_threshold': 5,
 'freq_max': 5000.0,
 'freq_min': 400.0,
 'n_jobs_bin': 1,
 'nested_params': None,
 'total_memory': '500M'}

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/", line 49, in <module>
    sorting_TDC_5 = ss.run_tridesclous(recording=recording, output_folder='tmp_TDC_5',
  File "/home/docs/checkouts/", line 328, in run_tridesclous
    return run_sorter('tridesclous', *args, **kwargs)
  File "/home/docs/checkouts/", line 57, in run_sorter
    sorting = run_sorter_local(sorter_name, recording, output_folder=output_folder,
  File "/home/docs/checkouts/", line 86, in run_sorter_local
    SorterClass.run_from_folder(output_folder, raise_error, verbose)
  File "/home/docs/checkouts/", line 236, 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',
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.728 seconds)

Gallery generated by Sphinx-Gallery