{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Using CSA for connection setup\n\nThis example sets up a simple network in NEST using the Connection Set\nAlgebra (CSA) instead of using the built-in connection routines.\n\nUsing the CSA requires NEST to be compiled with support for\nlibneurosim. For details, see [1]_.\n\n## See Also\n\n:doc:`csa_spatial_example`\n\n## References\n\n.. [1] Djurfeldt M, Davison AP and Eppler JM (2014). Efficient generation of\n       connectivity in neuronal networks from simulator-independent\n       descriptions, Front. Neuroinform.\n       http://dx.doi.org/10.3389/fninf.2014.00043\n\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "First, we import all necessary modules for simulation and plotting.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import nest\nfrom nest import voltage_trace\nfrom nest import visualization"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Next, we check for the availability of the CSA Python module. If it does\nnot import, we exit with an error message.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "try:\n    import csa\n    haveCSA = True\nexcept ImportError:\n    print(\"This example requires CSA to be installed in order to run.\\n\" +\n          \"Please make sure you compiled NEST using\\n\" +\n          \"  -Dwith-libneurosim=[OFF|ON|</path/to/libneurosim>]\\n\" +\n          \"and CSA and libneurosim are available.\")\n    import sys\n    sys.exit(1)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "To set up the connectivity, we create a ``random`` connection set with a\nprobability of 0.1 and two associated values (10000.0 and 1.0) used as\nweight and delay, respectively.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "cs = csa.cset(csa.random(0.1), 10000.0, 1.0)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Using the ``Create`` command from PyNEST, we create the neurons of the pre-\nand postsynaptic populations, each of which containing 16 neurons.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pre = nest.Create(\"iaf_psc_alpha\", 16)\npost = nest.Create(\"iaf_psc_alpha\", 16)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We can now connect the populations using the ``CGConnect`` function. It takes\nthe IDs of pre- and postsynaptic neurons (``pre`` and ``post``),\nthe connection set (``cs``) and a dictionary that maps the parameters\nweight and delay to positions in the value set associated with the\nconnection set.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.CGConnect(pre, post, cs, {\"weight\": 0, \"delay\": 1})"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "To stimulate the network, we create a ``poisson_generator`` and set it up to\nfire with a rate of 100000 spikes per second. It is connected to the\nneurons of the pre-synaptic population.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pg = nest.Create(\"poisson_generator\", params={\"rate\": 100000.0})\nnest.Connect(pg, pre, \"all_to_all\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "To measure and record the membrane potentials of the neurons, we create a\n``voltmeter`` and connect it to all post-synaptic nodes.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "vm = nest.Create(\"voltmeter\")\nnest.Connect(vm, post, \"all_to_all\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We save the whole connection graph of the network as a PNG image using the\n``plot_network`` function of the ``visualization`` submodule of PyNEST.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "allnodes = pg + pre + post + vm\nvisualization.plot_network(allnodes, \"csa_example_graph.png\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Finally, we simulate the network for 50 ms. The voltage traces of the\npost-synaptic nodes are plotted.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.Simulate(50.0)\nvoltage_trace.from_device(vm)\nvoltage_trace.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
}