{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# PyNEST Microcircuit: Run Simulation\n\nThis is an example script for running the microcircuit model and generating\nbasic plots of the network activity.\n\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Import the necessary modules and start the time measurements.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from stimulus_params import stim_dict\nfrom network_params import net_dict\nfrom sim_params import sim_dict\nimport network\nimport nest\nimport numpy as np\nimport time\ntime_start = time.time()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Initialize the network with simulation, network and stimulation parameters,\nthen create and connect all nodes, and finally simulate.\nThe times for a presimulation and the main simulation are taken\nindependently. A presimulation is useful because the spike activity typically\nexhibits a startup transient. In benchmark simulations, this transient should\nbe excluded from a time measurement of the state propagation phase. Besides,\nstatistical measures of the spike activity should only be computed after the\ntransient has passed.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "net = network.Network(sim_dict, net_dict, stim_dict)\ntime_network = time.time()\n\nnet.create()\ntime_create = time.time()\n\nnet.connect()\ntime_connect = time.time()\n\nnet.simulate(sim_dict['t_presim'])\ntime_presimulate = time.time()\n\nnet.simulate(sim_dict['t_sim'])\ntime_simulate = time.time()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot a spike raster of the simulated neurons and a box plot of the firing\nrates for each population.\nFor visual purposes only, spikes 100 ms before and 100 ms after the thalamic\nstimulus time are plotted here by default.\nThe computation of spike rates discards the presimulation time to exclude\ninitialization artifacts.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "raster_plot_interval = np.array([stim_dict['th_start'] - 100.0,\n                                 stim_dict['th_start'] + 100.0])\nfiring_rates_interval = np.array([sim_dict['t_presim'],\n                                  sim_dict['t_presim'] + sim_dict['t_sim']])\nnet.evaluate(raster_plot_interval, firing_rates_interval)\ntime_evaluate = time.time()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Summarize time measurements. Rank 0 usually takes longest because of the\ndata evaluation and print calls.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(\n    '\\nTimes of Rank {}:\\n'.format(\n        nest.Rank()) +\n    '  Total time:          {:.3f} s\\n'.format(\n        time_evaluate -\n        time_start) +\n    '  Time to initialize:  {:.3f} s\\n'.format(\n        time_network -\n        time_start) +\n    '  Time to create:      {:.3f} s\\n'.format(\n        time_create -\n        time_network) +\n    '  Time to connect:     {:.3f} s\\n'.format(\n        time_connect -\n        time_create) +\n    '  Time to presimulate: {:.3f} s\\n'.format(\n        time_presimulate -\n        time_connect) +\n    '  Time to simulate:    {:.3f} s\\n'.format(\n        time_simulate -\n        time_presimulate) +\n    '  Time to evaluate:    {:.3f} s\\n'.format(\n        time_evaluate -\n        time_simulate))"
      ]
    }
  ],
  "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
}