{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Repeated Stimulation\n\nSimple example for how to repeat a stimulation protocol\nusing the ``origin`` property of devices.\n\nIn this example, a ``poisson_generator`` generates a spike train that is\nrecorded directly by a ``spike_recorder``, using the following paradigm:\n\n1. A single trial last for 1000 ms.\n2. Within each trial, the ``poisson_generator`` is active from 100 ms to 500 ms.\n\nWe achieve this by defining the `start` and `stop` properties of the\ngenerator to 100 ms and 500 ms, respectively, and setting the ``origin`` to the\nsimulation time at the beginning of each trial. Start and stop are interpreted\nrelative to the ``origin``.\n\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "First, the modules needed for simulation and analyis are imported.\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, we set the parameters so the ``poisson_generator`` generates 1000\nspikes per second and is active from 100 to 500 ms\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "rate = 1000.0  # generator rate in spikes/s\nstart = 100.0  # start of simulation relative to trial start, in ms\nstop = 500.0  # end of simulation relative to trial start, in ms"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The simulation is supposed to take 1s (1000 ms) and is repeated 5 times\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "trial_duration = 1000.0  # trial duration, in ms\nnum_trials = 5      # number of trials to perform"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Third, the network is set up.  We reset the kernel and create a\n``poisson_generator``, in which the handle is stored in `pg`.\n\nThe parameters for rate and start and stop of activity are given as optional\nparameters in the form of a dictionary.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.ResetKernel()\npg = nest.Create('poisson_generator',\n                 params={'rate': rate,\n                         'start': start,\n                         'stop': stop}\n                 )"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The ``spike_recorder`` is created and the handle stored in `sr`.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sr = nest.Create('spike_recorder')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The ``Connect`` function connects the nodes so spikes from pg are collected by\nthe ``spike_recorder`` `sr`\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.Connect(pg, sr)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Before each trial, we set the ``origin`` of the ``poisson_generator`` to the\ncurrent simulation time. This automatically sets the start and stop time of\nthe ``poisson_generator`` to the specified times with respect to the origin.\nThe simulation is then carried out for the specified time in trial_duration.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "for n in range(num_trials):\n    pg.origin = nest.GetKernelStatus('time')\n    nest.Simulate(trial_duration)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now we plot the result, including a histogram using the ``nest.raster_plot``\nfunction. Note: The histogram will show spikes seemingly located before\n100 ms into each trial. This is due to sub-optimal automatic placement of\nhistogram bin borders.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.raster_plot.from_device(sr, hist=True, hist_binwidth=100.,\n                             title='Repeated stimulation by Poisson generator')\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
}