{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\nShort Square Analysis\n=====================\n\nDetect short square features\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import os\nimport matplotlib.pyplot as plt\nfrom allensdk.api.queries.cell_types_api import CellTypesApi\nfrom ipfx.data_set_utils import create_data_set\nfrom ipfx.feature_extractor import (\n    SpikeFeatureExtractor, SpikeTrainFeatureExtractor\n)\nfrom ipfx.stimulus_protocol_analysis import ShortSquareAnalysis\nfrom ipfx.spike_features import estimate_adjusted_detection_parameters\nfrom ipfx.epochs import get_stim_epoch\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# build a data set and find the short squares\ndata_set = create_data_set(sweep_info=sweep_info, nwb_file=nwb_file)\nshsq_table = data_set.filtered_sweep_table(\n    stimuli=data_set.ontology.short_square_names\n)\nsweeps = data_set.sweep_set(shsq_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(sweeps.i[0])\nstim_start_time = sweeps.t[0][stim_start_index]\nstim_end_time = sweeps.t[0][stim_end_index]\n\n# Estimate the dv cutoff and threshold fraction \ndv_cutoff, thresh_frac = estimate_adjusted_detection_parameters(\n    sweeps.v, \n    sweeps.t, \n    stim_start_time,\n    stim_start_time + 0.001\n)\n# Build the extractors \n\nspx = SpikeFeatureExtractor(\n    start=stim_start_time, dv_cutoff=dv_cutoff, thresh_frac=thresh_frac\n)\nsptrx = SpikeTrainFeatureExtractor(start=stim_start_time, end=None)\n\n# Run the analysis\nshsq_analysis = ShortSquareAnalysis(spx, sptrx)\nresults = shsq_analysis.analyze(sweeps)\n\n# Plot the sweeps at the lowest amplitude that evoked the most spikes\nfor i, swp in enumerate(sweeps.sweeps):\n    if i in results[\"common_amp_sweeps\"].index:\n        plt.plot(swp.t, swp.v, linewidth=0.5, color=\"steelblue\")\n\n# Set the plot limits to highlight where spikes are and axis labels\nplt.xlim(stim_start_time - 0.05, stim_end_time + 0.05)\nplt.xlabel(\"Time (s)\")\nplt.ylabel(\"Membrane potential (mV)\")\nplt.title(\"Lowest amplitude spiking sweeps\")\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
}