{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Use the spike sorting launcher\n\nThis example shows how to use the spike sorting launcher. The launcher allows to parameterize the sorter name and\nto run several sorters on one or multiple recordings.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import spikeinterface.extractors as se\nimport spikeinterface.sorters as ss"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "First, let's create the usual toy example:\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": [
        "Lets cache this recording to make it \"dumpable\"\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "recording = recording.save(name='toy')\nprint(recording)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The launcher enables to call any spike sorter with the same functions:  :code:`run_sorter` and :code:`run_sorters`.\nFor running multiple sorters on the same recording extractor or a collection of them, the :code:`run_sorters`\nfunction can be used.\n\nLet's first see how to run a single sorter, for example, Klusta:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# The sorter name can be now a parameter, e.g. chosen with a command line interface or a GUI\nsorter_name = 'herdingspikes'\nsorting_HS = ss.run_sorter(sorter_name='herdingspikes', recording=recording, output_folder='my_sorter_output', clustering_bandwidth=8)\nprint(sorting_HS.get_unit_ids())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "You can also run multiple sorters on the same recording:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "recordings = {'toy' : recording }\nsorter_list = ['herdingspikes', 'tridesclous']\nsorter_params = { 'herdingspikes': {'clustering_bandwidth' : 8} }\nsorting_output = ss.run_sorters(sorter_list, recordings, working_folder='tmp_some_sorters', \n                                mode_if_folder_exists='overwrite', sorter_params=sorter_params)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The 'mode' argument allows to 'overwrite' the 'working_folder' (if existing), 'raise' and Exception, or 'keep' the\nfolder and skip the spike sorting run.\n\nTo 'sorting_output' is a dictionary that has (recording, sorter) pairs as keys and the correspondent\n:code:`SortingExtractor` as values. It can be accessed as follows:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "for (rec_name, sorter_name), sorting in sorting_output.items():\n    print(rec_name, sorter_name, ':', sorting.get_unit_ids())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "With the same mechanism, you can run several spike sorters on many recordings, just by creating a list/dict of\n:code:`RecordingExtractor` objects (:code:`recording_list`).\n\n"
      ]
    }
  ],
  "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
}