{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Testing the adapting exponential integrate and fire model in NEST (Brette and Gerstner Fig 3D)\n\nThis example tests the adaptive integrate and fire model (AdEx) according to\nBrette and Gerstner [1]_ reproduces Figure 3D of the paper.\n\nNote that Brette and Gerstner give the value for `b` in `nA`.\nTo be consistent with the other parameters in the equations, `b` must be\nconverted to `pA` (pico Ampere).\n\n## References\n\n.. [1] Brette R and Gerstner W (2005). Adaptive exponential integrate-and-fire model as an effective\n       description of neuronal activity J. Neurophysiology. https://doi.org/10.1152/jn.00686.2005\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import nest\nimport nest.voltage_trace\nimport matplotlib.pyplot as plt\n\nnest.ResetKernel()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "First we make sure that the resolution of the simulation is 0.1 ms. This is\nimportant, since the slop of the action potential is very steep.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "res = 0.1\nnest.SetKernelStatus({\"resolution\": res})\nneuron = nest.Create(\"aeif_cond_exp\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Set the parameters of the neuron according to the paper.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "neuron.set(V_peak=20., E_L=-60.0, a=80.0, b=80.5, tau_w=720.0)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create and configure the stimulus which is a step current.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "dc = nest.Create(\"dc_generator\")\n\ndc.set(amplitude=-800.0, start=0.0, stop=400.0)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We connect the DC generators.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.Connect(dc, neuron, 'all_to_all')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "And add a ``voltmeter`` to sample the membrane potentials from the neuron\nin intervals of 0.1 ms.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "voltmeter = nest.Create(\"voltmeter\", params={'interval': 0.1})\nnest.Connect(voltmeter, neuron)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Finally, we simulate for 1000 ms and plot a voltage trace to produce the\nfigure.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.Simulate(1000.0)\n\nnest.voltage_trace.from_device(voltmeter)\nplt.axis([0, 1000, -85, 0])\nnest.voltage_trace.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
}