{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\nRamp Analysis\n====================\n\nDetect ramp features\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import os\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom allensdk.api.queries.cell_types_api import CellTypesApi\nfrom ipfx.data_set_utils import create_data_set\nfrom ipfx.epochs import get_stim_epoch\n\nfrom ipfx.feature_extractor import (\n    SpikeFeatureExtractor, SpikeTrainFeatureExtractor\n)\nfrom ipfx.stimulus_protocol_analysis import RampAnalysis\n\n# Download and access the experimental data\nct = CellTypesApi()\nnwb_file = os.path.join(\n    os.path.dirname(os.getcwd()), \n    \"data\",\n    \"nwb2_H17.03.008.11.03.05.nwb\"\n)\nspecimen_id = 595570553\nsweep_info = ct.get_ephys_sweeps(specimen_id)\n\n# Build the data set and find the ramp sweeps\ndata_set = create_data_set(sweep_info=sweep_info, nwb_file=nwb_file)\nramp_table = data_set.filtered_sweep_table(\n    stimuli=data_set.ontology.ramp_names\n)\nramp_sweeps = data_set.sweep_set(ramp_table.sweep_number)\n\n# find the start and end time of the stimulus \n# (treating the first sweep as representative)\nstim_start_index, stim_end_index = get_stim_epoch(ramp_sweeps.i[0])\nstim_start_time = ramp_sweeps.t[0][stim_start_index]\nstim_end_time = ramp_sweeps.t[0][stim_end_index]\n\nspx = SpikeFeatureExtractor(start=stim_start_time, end=None)\nsptrx = SpikeTrainFeatureExtractor(start=stim_start_time, end=None)\n\n# Run the analysis\nramp_analysis = RampAnalysis(spx, sptrx)\nresults = ramp_analysis.analyze(ramp_sweeps)\n\n# Plot the sweeps and the latency to the first spike of each\nsns.set_style(\"white\")\nfor swp in ramp_sweeps.sweeps:\n    plt.plot(swp.t, swp.v, linewidth=0.5)\nsns.rugplot(results[\"spiking_sweeps\"][\"latency\"].values + stim_start_time)\n\n# Set the plot limits to highlight where spikes are and axis labels\nplt.xlim(stim_start_time, stim_end_time)\nplt.xlabel(\"Time (s)\")\nplt.ylabel(\"Membrane potential (mV)\")\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.7.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}