{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Population of GIF neuron model with oscillatory behavior\n\nThis script simulates a population of generalized integrate-and-fire (GIF)\nmodel neurons driven by noise from a group of Poisson generators.\n\nDue to spike-frequency adaptation, the GIF neurons tend to show oscillatory\nbehavior on the time scale comparable with the time constant of adaptation\nelements (stc and sfa).\n\nPopulation dynamics are visualized by raster plot and as average firing rate.\n\n## References\n\n.. [1] Schwalger T, Degert M, Gerstner W (2017). Towards a theory of cortical columns: From spiking\n       neurons to interacting neural populations of finite size. PLoS Comput Biol.\n       https://doi.org/10.1371/journal.pcbi.1005507\n\n.. [2] Mensi S, Naud R, Pozzorini C, Avermann M, Petersen CC and\n       Gerstner W (2012). Parameter extraction and classification of\n       three cortical neuron types reveals two distinct adaptation\n       mechanisms. Journal of Neurophysiology. 107(6), pp.1756-1775.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Import all necessary modules for simulation and plotting.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import nest\nimport nest.raster_plot\nimport matplotlib.pyplot as plt\n\nnest.ResetKernel()"
      ]
    },
    {
      "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\nsimtime = 2000.0"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Definition of neural parameters for the GIF model. These parameters are\nextracted by fitting the model to experimental data [2]_.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "neuron_params = {\"C_m\": 83.1,\n                 \"g_L\": 3.7,\n                 \"E_L\": -67.0,\n                 \"Delta_V\": 1.4,\n                 \"V_T_star\": -39.6,\n                 \"t_ref\": 4.0,\n                 \"V_reset\": -36.7,\n                 \"lambda_0\": 1.0,\n                 \"q_stc\": [56.7, -6.9],\n                 \"tau_stc\": [57.8, 218.2],\n                 \"q_sfa\": [11.7, 1.8],\n                 \"tau_sfa\": [53.8, 640.0],\n                 \"tau_syn_ex\": 10.0,\n                 }"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Definition of the parameters for the population of GIF neurons.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "N_ex = 100  # size of the population\np_ex = 0.3  # connection probability inside the population\nw_ex = 30.0  # synaptic weights inside the population (pA)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Definition of the parameters for the Poisson group and its connection with\nGIF neurons population.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "N_noise = 50  # size of Poisson group\nrate_noise = 10.0  # firing rate of Poisson neurons (Hz)\nw_noise = 20.0  # synaptic weights from Poisson to population neurons (pA)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Configuration of the simulation kernel with the previously defined time\nresolution.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.resolution = dt"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Building a population of GIF neurons, a group of Poisson neurons and a\nspike recorder device for capturing spike times of the population.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "population = nest.Create(\"gif_psc_exp\", N_ex, params=neuron_params)\n\nnoise = nest.Create(\"poisson_generator\", N_noise, params={'rate': rate_noise})\n\nspike_det = nest.Create(\"spike_recorder\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Build connections inside the population of GIF neurons population, between\nPoisson group and the population, and also connecting spike recorder to\nthe population.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.Connect(\n    population, population, {'rule': 'pairwise_bernoulli', 'p': p_ex},\n    syn_spec={\"weight\": w_ex}\n)\n\nnest.Connect(noise, population, 'all_to_all', syn_spec={\"weight\": w_noise})\n\nnest.Connect(population, spike_det)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Simulation of the network.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.Simulate(simtime)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plotting the results of simulation including raster plot and histogram of\npopulation activity.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.raster_plot.from_device(spike_det, hist=True)\nplt.title('Population dynamics')\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
}