{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Single sweep detection with analysis window\n\nDetect spikes for a single sweep in a specified time window\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import os\nimport matplotlib.pyplot as plt\nfrom ipfx.dataset.create import create_ephys_data_set\nfrom ipfx.feature_extractor import SpikeFeatureExtractor\n\n# Download and access the experimental data from DANDI archive per instructions in the documentation\n# Example below will use an nwb file provided with the package\n\nnwb_file = os.path.join(\n    os.path.dirname(os.getcwd()),\n    \"data\",\n    \"nwb2_H17.03.008.11.03.05.nwb\"\n)\n\n# Create data set from the nwb file and choose a sweeep\ndataset = create_ephys_data_set(nwb_file=nwb_file)\nsweep = dataset.sweep(sweep_number=39)\n\n# Configure the extractor to just detect spikes in the middle of the step\next = SpikeFeatureExtractor(start=1.25, end=1.75)\nresults = ext.process(t=sweep.t, v=sweep.v, i=sweep.i)\n\n# Plot the results, showing two features of the detected spikes\nplt.plot(sweep.t, sweep.v)\nplt.plot(results[\"peak_t\"], results[\"peak_v\"], 'r.')\nplt.plot(results[\"threshold_t\"], results[\"threshold_v\"], 'k.')\n\n# Set the plot limits to highlight where spikes are and axis labels\nplt.xlim(0.5, 2.5)\nplt.xlabel(\"Time (s)\")\nplt.ylabel(\"Membrane potential (mV)\")\n\n# Show the analysis window on the plot\nplt.axvline(1.25, linestyle=\"dotted\", color=\"gray\")\nplt.axvline(1.75, linestyle=\"dotted\", color=\"gray\")\n\nplt.show()"
      ]
    }
  ],
  "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.6.8"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}