{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Ramp Analysis\n\nCalculate features of Ramp sweeps\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import os\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom ipfx.epochs import get_stim_epoch\nfrom ipfx.dataset.create import create_ephys_data_set\nfrom ipfx.utilities import drop_failed_sweeps\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 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# Create Ephys Data Set\ndata_set = create_ephys_data_set(nwb_file=nwb_file)\n\n# Drop failed sweeps: sweeps with incomplete recording or failing QC criteria\ndrop_failed_sweeps(data_set)\n\n# get sweep table of Ramp sweeps\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# Select epoch corresponding to the actual recording from the sweeps\n# and align sweeps so that the experiment would start at the same time\nramp_sweeps.select_epoch(\"recording\")\nramp_sweeps.align_to_start_of_epoch(\"experiment\")\n\n# Select epoch corresponding to the actual recording from the sweeps\n# and align sweeps so that the experiment would start at the same time\nramp_sweeps.select_epoch(\"recording\")\nramp_sweeps.align_to_start_of_epoch(\"experiment\")\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)\nsptfx = SpikeTrainFeatureExtractor(start=stim_start_time, end=None)\n\n# Run the analysis\nramp_analysis = RampAnalysis(spx, sptfx)\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.6.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}