{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Estimate Spike Detection Parameters\n\nEstimate spike detection parameters\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import os\nfrom ipfx.data_set_utils import create_data_set\nfrom ipfx.spike_features import estimate_adjusted_detection_parameters\nfrom ipfx.feature_extractor import SpikeFeatureExtractor\nfrom ipfx.utilities import drop_failed_sweeps\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\n# Create data set from the nwb file and find the short squares\ndata_set = create_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\nshort_square_table = data_set.filtered_sweep_table(stimuli=[\"Short Square\"])\nssq_set = data_set.sweep_set(short_square_table.sweep_number)\n\n# estimate the dv cutoff and threshold fraction\ndv_cutoff, thresh_frac = estimate_adjusted_detection_parameters(\n    ssq_set.v, ssq_set.t, 1.02, 1.021\n)\n\n# detect spikes  in a given sweep number\nsweep_number = 16\nsweep = data_set.sweep(sweep_number)\next = SpikeFeatureExtractor(dv_cutoff=dv_cutoff, thresh_frac=thresh_frac)\nspikes = ext.process(t=sweep.t, v=sweep.v, i=sweep.i)\n\n# and plot them\nplt.plot(sweep.t, sweep.v)\nplt.plot(spikes[\"peak_t\"], spikes[\"peak_v\"], 'r.')\nplt.plot(spikes[\"threshold_t\"], spikes[\"threshold_v\"], 'k.')\nplt.xlim(1.018, 1.028)\nplt.xlabel('Time (s)')\nplt.ylabel('Membrane potential (mV)')\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.8"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}