{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Long Square Analysis\n\nCalculate Features of Long Square sweeps\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from ipfx.feature_extractor import (\n    SpikeFeatureExtractor, SpikeTrainFeatureExtractor\n)\nimport ipfx.stimulus_protocol_analysis as spa\nfrom ipfx.epochs import get_stim_epoch\nfrom ipfx.dataset.create import create_ephys_data_set\nfrom ipfx.utilities import drop_failed_sweeps\n\nimport os\nimport matplotlib.pyplot as plt\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 Long Square sweeps\nlong_square_table = data_set.filtered_sweep_table(\n    stimuli=data_set.ontology.long_square_names\n)\nlong_square_sweeps = data_set.sweep_set(long_square_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\nlong_square_sweeps.select_epoch(\"recording\")\nlong_square_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(long_square_sweeps.i[0])\nstim_start_time = long_square_sweeps.t[0][stim_start_index]\nstim_end_time = long_square_sweeps.t[0][stim_end_index]\n\n# build the extractors\nspfx = SpikeFeatureExtractor(start=stim_start_time, end=stim_end_time)\nsptfx = SpikeTrainFeatureExtractor(start=stim_start_time, end=stim_end_time)\n\n# run the analysis and print out a few of the features\nlong_square_analysis = spa.LongSquareAnalysis(spfx, sptfx, subthresh_min_amp=-100.0)\ndata = long_square_analysis.analyze(long_square_sweeps)\n\nfields_to_print = [\n    'tau',\n    'v_baseline',\n    'input_resistance',\n    'vm_for_sag',\n    'fi_fit_slope',\n    'sag',\n    'rheobase_i'\n]\n\nfor field in fields_to_print:\n    print(\"%s: %s\" % (field, str(data[field])))\n\n# Plot stimulus amplitude vs. firing rate\nspiking_sweeps = data['spiking_sweeps'].sort_values(by='stim_amp')\nplt.plot(spiking_sweeps.stim_amp,\n         spiking_sweeps.avg_rate)\nplt.xlabel('Stimulus amplitude (pA)')\nplt.ylabel('Average firing rate (Hz)')\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
}