{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Network of linear rate neurons\n\nThis script simulates an excitatory and an inhibitory population\nof ``lin_rate_ipn`` neurons with delayed excitatory and instantaneous\ninhibitory connections. The rate of all neurons is recorded using\na multimeter. The resulting rate for one excitatory and one\ninhibitory neuron is plotted.\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": [
        "Assigning the simulation parameters to variables.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "dt = 0.1  # the resolution in ms\nT = 100.0  # Simulation time in ms"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Definition of the number of neurons\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "order = 50\nNE = int(4 * order)    # number of excitatory neurons\nNI = int(1 * order)    # number of inhibitory neurons\nN = int(NE + NI)       # total number of neurons"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Definition of the connections\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "d_e = 5.   # delay of excitatory connections in ms\ng = 5.0  # ratio inhibitory weight/excitatory weight\nepsilon = 0.1  # connection probability\nw = 0.1 / numpy.sqrt(N)  # excitatory connection strength\n\nKE = int(epsilon * NE)  # number of excitatory synapses per neuron (outdegree)\nKI = int(epsilon * NI)  # number of inhibitory synapses per neuron (outdegree)\nK_tot = int(KI + KE)  # total number of synapses per neuron\nconnection_rule = 'fixed_outdegree'  # connection rule"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Definition of the neuron model and its neuron parameters\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "neuron_model = 'lin_rate_ipn'  # neuron model\nneuron_params = {'linear_summation': True,\n                 # type of non-linearity (not affecting linear rate models)\n                 'tau': 10.0,\n                 # time constant of neuronal dynamics in ms\n                 'mu': 2.0,\n                 # mean input\n                 'sigma': 5.\n                 # noise parameter\n                 }"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Configuration of the simulation kernel by the previously defined time\nresolution used in the simulation. Setting ``print_time`` to True prints\nthe already processed simulation time as well as its percentage of the\ntotal simulation time.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.ResetKernel()\nnest.resolution = dt\nnest.use_wfr = False\nnest.print_time = True\nnest.overwrite_files = True\n\nprint(\"Building network\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Creation of the nodes using ``Create``.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "n_e = nest.Create(neuron_model, NE, neuron_params)\nn_i = nest.Create(neuron_model, NI, neuron_params)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "To record from the rate neurons a ``multimeter`` is created and the parameter\n``record_from`` is set to `rate` as well as the recording interval to `dt`\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "mm = nest.Create('multimeter', params={'record_from': ['rate'],\n                                       'interval': dt})"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Specify synapse and connection dictionaries:\nConnections originating from excitatory neurons are associated\nwith a delay `d` (``rate_connection_delayed``).\nConnections originating from inhibitory neurons are not associated\nwith a delay (``rate_connection_instantaneous``).\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "syn_e = {'weight': w, 'delay': d_e, 'synapse_model': 'rate_connection_delayed'}\nsyn_i = {'weight': -g * w, 'synapse_model': 'rate_connection_instantaneous'}\nconn_e = {'rule': connection_rule, 'outdegree': KE}\nconn_i = {'rule': connection_rule, 'outdegree': KI}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Connect rate units\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.Connect(n_e, n_e, conn_e, syn_e)\nnest.Connect(n_i, n_i, conn_i, syn_i)\nnest.Connect(n_e, n_i, conn_i, syn_e)\nnest.Connect(n_i, n_e, conn_e, syn_i)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Connect recording device to rate units\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.Connect(mm, n_e + n_i)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Simulate the network\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.Simulate(T)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot rates of one excitatory and one inhibitory neuron\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "data = mm.events\nsenders = data['senders']\nrate = data['rate']\ntimes = data['times']\n\nne_0_id = n_e[0].global_id\nni_0_id = n_i[0].global_id\nwhere_sender_is_ne_0 = numpy.where(senders == ne_0_id)\nwhere_sender_is_ni_0 = numpy.where(senders == ni_0_id)\n\nrate_ex = rate[where_sender_is_ne_0]\nrate_in = rate[where_sender_is_ni_0]\ntimes = times[where_sender_is_ne_0]\n\nplt.figure()\nplt.plot(times, rate_ex, label='excitatory')\nplt.plot(times, rate_in, label='inhibitory')\nplt.xlabel('time (ms)')\nplt.ylabel('rate (a.u.)')\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.8.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}