{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Run spike sorting algorithms\n\nThis example shows the basic usage of the :py:mod:`spikeinterface.sorters` module of SpikeInterface\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import spikeinterface.extractors as se\nimport spikeinterface.sorters as ss\nfrom pprint import pprint"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "First, let's create a toy example:\n\u00a0We choose explicitly one segment because many sorters handle only recording with unique segment\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "recording, sorting_true = se.toy_example(duration=10, seed=0, num_segments=1)\nprint(recording)\nprint(sorting_true)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Check available sorters\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pprint(ss.available_sorters())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "This will list the sorters available through SpikeInterface. To see which sorters are installed on the machine\nyou can run:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pprint(ss.installed_sorters())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Change sorter parameters\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "default_TDC_params = ss.TridesclousSorter.default_params()\npprint(default_TDC_params)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Parameters can be changed either by passing a full dictionary, or by\npassing single arguments.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# tridesclous spike sorting\ndefault_TDC_params['detect_threshold'] = 5\n\n# parameters set by params dictionary\nsorting_TDC_5 = ss.run_tridesclous(recording=recording, output_folder='tmp_TDC_5',\n                                   **default_TDC_params, )"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# parameters set by params dictionary\nsorting_TDC_8 = ss.run_tridesclous(recording=recording, output_folder='tmp_TDC_8',\n                                   detect_threshold=8.)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print('Units found with threshold = 5:', sorting_TDC_5.get_unit_ids())\nprint('Units found with threshold = 8:', sorting_TDC_8.get_unit_ids())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Run other sorters\n -----------------\n\u00a0\n\u00a0Some sorters (kilosort, ironclust, hdsort, ...) need to manually set the path to the source folder\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# KiloSort spike sorting (KILOSORT_PATH and NPY_MATLAB_PATH can be set as environment variables)\n# sorting_KS = ss.run_kilosort(recording, output_folder='tmp_KS')\n# \u00a0print('Units found with Kilosort:', sorting_KS.get_unit_ids())"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Kilosort2 spike sorting (KILOSORT2_PATH and NPY_MATLAB_PATH can be set as environment variables)\n# sorting_KS2 = ss.run_kilosort2(recording, output_folder='tmp_KS2')\n# \u00a0print('Units found with Kilosort2', sorting_KS2.get_unit_ids())"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Klusta spike sorting\n# \u00a0sorting_KL = ss.run_klusta(recording, output_folder='tmp_KL')\n# print('Units found with Klusta:', sorting_KL.get_unit_ids())"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# IronClust spike sorting (IRONCLUST_PATH can be set as environment variables)\n# sorting_IC = ss.run_ironclust(recording, output_folder='tmp_IC')\n# print('Units found with Ironclust:', sorting_IC.get_unit_ids())"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Tridesclous spike sorting\n# sorting_TDC = ss.run_tridesclous(recording, output_folder='tmp_TDC')\n# print('Units found with Tridesclous:', sorting_TDC.get_unit_ids())"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.8.13"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}