{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Spike synchronization through subthreshold oscillation\n\nThis script reproduces the spike synchronization behavior\nof integrate-and-fire neurons in response to a subthreshold\noscillation. This phenomenon is shown in Fig. 1 of [1]_\n\nNeurons receive a weak 35 Hz oscillation, a gaussian noise current\nand an increasing DC. The time-locking capability is shown to\ndepend on the input current given. The result is then plotted using\nmatplotlib. All parameters are taken from the above paper.\n\n## References\n\n.. [1] Brody CD and Hopfield JJ (2003). Simple networks for\n       spike-timing-based computation, with application to olfactory\n       processing. Neuron 37, 843-852.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "First, we import all necessary modules for simulation, analysis, 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"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Second, the simulation parameters are assigned to variables.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "N = 1000           # number of neurons\nbias_begin = 140.  # minimal value for the bias current injection [pA]\nbias_end = 200.    # maximal value for the bias current injection [pA]\nT = 600            # simulation time (ms)\n\n# parameters for the alternating-current generator\ndriveparams = {'amplitude': 50., 'frequency': 35.}\n# parameters for the noise generator\nnoiseparams = {'mean': 0.0, 'std': 200.}\nneuronparams = {'tau_m': 20.,  # membrane time constant\n                'V_th': 20.,  # threshold potential\n                'E_L': 10.,  # membrane resting potential\n                't_ref': 2.,  # refractory period\n                'V_reset': 0.,  # reset potential\n                'C_m': 200.,  # membrane capacitance\n                'V_m': 0.}      # initial membrane potential"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Third, the nodes are created using ``Create``. We store the returned handles\nin variables for later reference.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "neurons = nest.Create('iaf_psc_alpha', N)\nsr = nest.Create('spike_recorder')\nnoise = nest.Create('noise_generator')\ndrive = nest.Create('ac_generator')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Set the parameters specified above for the generators using ``set``.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "drive.set(driveparams)\nnoise.set(noiseparams)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Set the parameters specified above for the neurons. Neurons get an internal\ncurrent. The first neuron additionally receives the current with amplitude\n`bias_begin`, the last neuron with amplitude `bias_end`.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "neurons.set(neuronparams)\nneurons.I_e = [(n * (bias_end - bias_begin) / N + bias_begin)\n               for n in range(1, len(neurons) + 1)]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Connect alternating current and noise generators as well as\n`spike_recorder`s to neurons\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.Connect(drive, neurons)\nnest.Connect(noise, neurons)\nnest.Connect(neurons, sr)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Simulate the network for time `T`.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.Simulate(T)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot the raster plot of the neuronal spiking activity.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.raster_plot.from_device(sr, hist=True)\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
}