{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\nLong Square Analysis\n====================\n\nDetect Long Square Features\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from ipfx.data_set_utils import create_data_set\nfrom ipfx.feature_extractor import (\n    SpikeFeatureExtractor, SpikeTrainFeatureExtractor\n)\nimport ipfx.stimulus_protocol_analysis as spa\nfrom ipfx.epochs import get_stim_epoch\n\nfrom allensdk.api.queries.cell_types_api import CellTypesApi\n\nimport os\nimport matplotlib.pyplot as plt\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 a data set and find the long squares\ndata_set = create_data_set(sweep_info=sweep_info, nwb_file=nwb_file)\nlsq_table = data_set.filtered_sweep_table(\n    stimuli=data_set.ontology.long_square_names\n)\nlsq_set = data_set.sweep_set(lsq_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(lsq_set.i[0])\nstim_start_time = lsq_set.t[0][stim_start_index]\nstim_end_time = lsq_set.t[0][stim_end_index]\n\n# build the extractors\nspx = SpikeFeatureExtractor(start=stim_start_time, end=stim_end_time)\nspfx = SpikeTrainFeatureExtractor(start=stim_start_time, end=stim_end_time)\n\n# run the analysis and print out a few of the features\nlsqa = spa.LongSquareAnalysis(spx, spfx, subthresh_min_amp=-100.0)\ndata = lsqa.analyze(lsq_set)\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 stim amp 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.7.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}