{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Initial membrane voltage\n\nPlot several runs of the ``iaf_cond_exp_sfa_rr`` neuron without input for various\ninitial values of the membrane potential.\n\n## References\n\n.. [1] Dayan, P. and Abbott, L.F. (2001) Theoretical neuroscience,\n       MIT Press, page 166\n\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "First, the necessary modules for simulation and plotting are imported.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import nest\nimport numpy\nimport matplotlib.pyplot as plt"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "A loop runs over a range of initial membrane voltages.\n\nIn the beginning of each iteration, the simulation kernel is put back to\nits initial state using `ResetKernel`.\n\nNext, a neuron is instantiated with ``Create``. The used neuron model\n``iaf_cond_exp_sfa_rr`` is an implementation of a spiking neuron with\nintegrate-and-fire dynamics, conductance-based synapses, an additional\nspike-frequency adaptation and relative refractory mechanisms as described\nin [1]_. Incoming spike events induce a post-synaptic change of\nconductance  modeled  by an  exponential  function. ``SetStatus`` allows to\nassign the initial membrane voltage of the current loop run to the neuron.\n\n``Create`` is used once more to instantiate a ``voltmeter`` as recording device\nwhich is subsequently connected to the neuron with ``Connect``.\n\nThen, a simulation with a duration of 75 ms is started with ``Simulate``.\n\nWhen the simulation has finished, the recorded times and membrane voltages\nare read from the voltmeter via ``GetStatus`` where they can be accessed\nthrough the key ``events`` of the status dictionary.\n\nFinally, the time course of the membrane voltages is plotted for each of\nthe different inital values.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "for vinit in numpy.arange(-100, -50, 10, float):\n\n    nest.ResetKernel()\n\n    cbn = nest.Create(\"iaf_cond_exp_sfa_rr\")\n\n    nest.SetStatus(cbn, \"V_m\", vinit)\n\n    voltmeter = nest.Create(\"voltmeter\")\n    nest.Connect(voltmeter, cbn)\n\n    nest.Simulate(75.0)\n\n    t = voltmeter.get(\"events\", \"times\")\n    v = voltmeter.get(\"events\", \"V_m\")\n\n    plt.plot(t, v, label=\"initial V_m = %.2f mV\" % vinit)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Set the legend and the labels for the plot outside of the loop.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plt.legend(loc=4)\nplt.xlabel(\"time (ms)\")\nplt.ylabel(\"V_m (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.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}