{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Sinusoidal poisson generator example\n\nThis script demonstrates the use of the ``sinusoidal_poisson_generator``\nand its different parameters and modes. The source code of the model\ncan be found in ``models/sinusoidal_poisson_generator.h``.\n\nThe script is structured into two parts and creates one common figure.\nIn Part 1, two instances of the ``sinusoidal_poisson_generator`` are\ncreated with different parameters. Part 2 illustrates the effect of\nthe ``individual_spike_trains`` switch.\n\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We import the modules required to simulate, analyze and plot this example.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import nest\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nnest.ResetKernel()   # in case we run the script multiple times from iPython"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We create two instances of the ``sinusoidal_poisson_generator`` with two\ndifferent parameter sets using ``Create``. Moreover, we create devices to\nrecord firing rates (``multimeter``) and spikes (``spike_recorder``) and connect\nthem to the generators using ``Connect``.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.SetKernelStatus({'resolution': 0.01})\n\ng = nest.Create('sinusoidal_poisson_generator', n=2,\n                params=[{'rate': 10000.0,\n                         'amplitude': 5000.0,\n                         'frequency': 10.0,\n                         'phase': 0.0},\n                        {'rate': 0.0,\n                         'amplitude': 10000.0,\n                         'frequency': 5.0,\n                         'phase': 90.0}])\n\nm = nest.Create('multimeter', 2, {'interval': 0.1, 'record_from': ['rate']})\ns = nest.Create('spike_recorder', 2)\n\nnest.Connect(m, g, 'one_to_one')\nnest.Connect(g, s, 'one_to_one')\nprint(m.get())\nnest.Simulate(200)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "After simulating, the spikes are extracted from the ``spike_recorder`` using\n``GetStatus`` and plots are created with panels for the PST and ISI histograms.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "colors = ['b', 'g']\n\nfor j in range(2):\n\n    ev = m[j].events\n    t = ev['times']\n    r = ev['rate']\n\n    sp = nest.GetStatus(s[j])[0]['events']['times']\n    plt.subplot(221)\n    h, e = np.histogram(sp, bins=np.arange(0., 201., 5.))\n    plt.plot(t, r, color=colors[j])\n    plt.step(e[:-1], h * 1000 / 5., color=colors[j], where='post')\n    plt.title('PST histogram and firing rates')\n    plt.ylabel('Spikes per second')\n\n    plt.subplot(223)\n    plt.hist(np.diff(sp), bins=np.arange(0., 1.005, 0.02),\n             histtype='step', color=colors[j])\n    plt.title('ISI histogram')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The kernel is reset and the number of threads set to 4.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.ResetKernel()\nnest.SetKernelStatus({'local_num_threads': 4})"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "A ``sinusoidal_poisson_generator`` with  ``individual_spike_trains`` set to\n`True` is created and connected to 20 parrot neurons whose spikes are\nrecorded by a ``spike_recorder``. After simulating, a raster plot of the spikes\nis created.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "g = nest.Create('sinusoidal_poisson_generator',\n                params={'rate': 100.0, 'amplitude': 50.0,\n                        'frequency': 10.0, 'phase': 0.0,\n                        'individual_spike_trains': True})\np = nest.Create('parrot_neuron', 20)\ns = nest.Create('spike_recorder')\n\nnest.Connect(g, p, 'all_to_all')\nnest.Connect(p, s, 'all_to_all')\n\nnest.Simulate(200)\nev = s.events\nplt.subplot(222)\nplt.plot(ev['times'], ev['senders'] - min(ev['senders']), 'o')\nplt.ylim([-0.5, 19.5])\nplt.yticks([])\nplt.title('Individual spike trains for each target')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The kernel is reset again and the whole procedure is repeated for a\n``sinusoidal_poisson_generator`` with `individual_spike_trains` set to\n`False`. The plot shows that in this case, all neurons receive the same\nspike train from the ``sinusoidal_poisson_generator``.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.ResetKernel()\nnest.SetKernelStatus({'local_num_threads': 4})\n\ng = nest.Create('sinusoidal_poisson_generator',\n                params={'rate': 100.0, 'amplitude': 50.0,\n                        'frequency': 10.0, 'phase': 0.0,\n                        'individual_spike_trains': False})\np = nest.Create('parrot_neuron', 20)\ns = nest.Create('spike_recorder')\n\nnest.Connect(g, p, 'all_to_all')\nnest.Connect(p, s, 'all_to_all')\n\nnest.Simulate(200)\nev = s.events\nplt.subplot(224)\nplt.plot(ev['times'], ev['senders'] - min(ev['senders']), 'o')\nplt.ylim([-0.5, 19.5])\nplt.yticks([])\nplt.title('One spike train for all targets')\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
}