{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "equipped-executive",
   "metadata": {},
   "source": [
    "# Demo for Spike Sorting with NWB Files\n",
    "\n",
    "Requirements:\n",
    "- spikeinterface\n",
    "\n",
    "Github: https://github.com/SpikeInterface/spikeextractors\n",
    "\n",
    "Documentation: https://spikeinterface.readthedocs.io/en/latest/index.html\n",
    "\n",
    "SpikeInterface Code Notes:\n",
    "- The arguments in the docs are mis-ordered for `get_snippets`. \n",
    "- Looks to be the same for args in `get_traces`. \n",
    "- Not sure if this is a mistake. Could ask /  PR this fix."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "upper-spell",
   "metadata": {},
   "outputs": [],
   "source": [
    "%config Completer.use_jedi = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "painful-simpson",
   "metadata": {},
   "outputs": [],
   "source": [
    "import spikeinterface.extractors as se\n",
    "import spikeinterface.sorters as ss"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "former-mileage",
   "metadata": {},
   "source": [
    "## Set up NWB File\n",
    "\n",
    "SpikeInterface links to files through the 'extractors' sub-module."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "fitted-stanford",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define the file name of the NMB file\n",
    "nwb_filename = 'nwb_local_data.nwb'\n",
    "\n",
    "# Define the name of the relevant field data\n",
    "data_field = 'field_data_1'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "simple-wallace",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Attach the NWB recording extractor to the file\n",
    "se_obj = se.NwbRecordingExtractor(nwb_filename, data_field)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "instructional-netherlands",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'NWBFile': {'session_description': 'XX',\n",
       "  'identifier': 'XX',\n",
       "  'session_start_time': datetime.datetime(2021, 3, 31, 16, 9, 15, 74519, tzinfo=tzoffset(None, -14400)),\n",
       "  'institution': 'Columbia',\n",
       "  'lab': 'U01 Group'},\n",
       " 'Ecephys': {'Device': [{'name': 'RECORDING DEVICE'}],\n",
       "  'ElectrodeGroup': [{'name': 'A-microwires-chnum',\n",
       "    'description': 'Behnke Fried/Micro Inner Wire Bundle....ADD DETAILS.',\n",
       "    'location': 'WHERE',\n",
       "    'device': 'RECORDING DEVICE'}],\n",
       "  'ElectricalSeries': [{'name': 'field_data_1', 'description': '...'}]}}"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check the NWB metadata from the loaded file\n",
    "se_obj.nwb_metadata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "burning-request",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1000.0\n",
      "[0]\n"
     ]
    }
   ],
   "source": [
    "# Check the number of channels\n",
    "print(se_obj.get_num_channels())\n",
    "\n",
    "print(se_obj.get_sampling_frequency())\n",
    "\n",
    "print(se_obj.get_channel_ids())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "genetic-weekend",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Grab some data!\n",
    "#se_obj.get_traces(0, 1, 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "minute-theta",
   "metadata": {},
   "source": [
    "In theory, the above grabs some actual field data. \n",
    "\n",
    "It doesn't work right now. I think we have a quirk in the packing of field data (perhaps including not mapping electrodes -> data)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "younger-layout",
   "metadata": {},
   "source": [
    "## Spike Sorting\n",
    "\n",
    "Note that spike sorters need to be (separately) installed.\n",
    "Relevant tutorial: https://spikeinterface.readthedocs.io/en/latest/sortersinfo.html\n",
    "\n",
    "Here, I installed `klusta`, as it seemed to be an easy-to-install Python option.\n",
    "\n",
    "This should also all work with `combinato` (it just looked a bit more annoying to install).\n",
    "\n",
    "Other spike sortes look to require other langauges (ex: Matlab)\n",
    "\n",
    "Relevant tutorial: https://spikeinterface.readthedocs.io/en/latest/modules/sorters/plot_1_sorters_example.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "enclosed-white",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['combinato', 'hdsort', 'herdingspikes', 'ironclust', 'kilosort', 'kilosort2', 'kilosort2_5', 'kilosort3', 'klusta', 'mountainsort4', 'spykingcircus', 'tridesclous', 'waveclus', 'yass']\n"
     ]
    }
   ],
   "source": [
    "# Check the list of available spike sorters in SpikeInterface\n",
    "print(ss.available_sorters())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "innovative-peoples",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['klusta']\n"
     ]
    }
   ],
   "source": [
    "# Check the list of installed spike sorters\n",
    "print(ss.installed_sorters())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "occupied-thomson",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run a specified spike sorting on the data\n",
    "#ss.run_sorter('klusta', se_obj)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "extended-designer",
   "metadata": {},
   "source": [
    "^This doesn't actually work right now, probably do to the general LFP data storage issue. "
   ]
  }
 ],
 "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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}