1# -*- coding: utf-8 -*-
2#
3# csa_example.py
4#
5# This file is part of NEST.
6#
7# Copyright (C) 2004 The NEST Initiative
8#
9# NEST is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 2 of the License, or
12# (at your option) any later version.
13#
14# NEST is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with NEST.  If not, see <http://www.gnu.org/licenses/>.
21
22"""
23Using CSA for connection setup
24------------------------------
25
26This example sets up a simple network in NEST using the Connection Set
27Algebra (CSA) instead of using the built-in connection routines.
28
29Using the CSA requires NEST to be compiled with support for
30libneurosim. For details, see [1]_.
31
32See Also
33~~~~~~~~
34
35:doc:`csa_spatial_example`
36
37References
38~~~~~~~~~~
39
40.. [1] Djurfeldt M, Davison AP and Eppler JM (2014). Efficient generation of
41       connectivity in neuronal networks from simulator-independent
42       descriptions, Front. Neuroinform.
43       http://dx.doi.org/10.3389/fninf.2014.00043
44
45"""
46
47###############################################################################
48# First, we import all necessary modules for simulation and plotting.
49
50import nest
51from nest import voltage_trace
52from nest import visualization
53import matplotlib.pyplot as plt
54
55###############################################################################
56# Next, we check for the availability of the CSA Python module. If it does
57# not import, we exit with an error message.
58
59try:
60    import csa
61    haveCSA = True
62except ImportError:
63    print("This example requires CSA to be installed in order to run.\n" +
64          "Please make sure you compiled NEST using\n" +
65          "  -Dwith-libneurosim=[OFF|ON|</path/to/libneurosim>]\n" +
66          "and CSA and libneurosim are available.")
67    import sys
68    sys.exit(1)
69
70###############################################################################
71# To set up the connectivity, we create a ``random`` connection set with a
72# probability of 0.1 and two associated values (10000.0 and 1.0) used as
73# weight and delay, respectively.
74
75cg = csa.cset(csa.random(0.1), 10000.0, 1.0)
76
77###############################################################################
78# Using the ``Create`` command from PyNEST, we create the neurons of the pre-
79# and postsynaptic populations, each of which containing 16 neurons.
80
81pre = nest.Create("iaf_psc_alpha", 16)
82post = nest.Create("iaf_psc_alpha", 16)
83
84###############################################################################
85# We can now connect the populations using the ``Connect`` function
86# with the ``conngen`` rule. It takes the IDs of pre- and postsynaptic
87# neurons (``pre`` and ``post``), the connection set (``cg``) and a
88# dictionary that maps the parameters weight and delay to positions in
89# the value set associated with the connection set (``params_map``).
90
91params_map = {"weight": 0, "delay": 1}
92connspec = {"rule": "conngen", "cg": cg, "params_map": params_map}
93nest.Connect(pre, post, connspec)
94
95###############################################################################
96# To stimulate the network, we create a ``poisson_generator`` and set it up to
97# fire with a rate of 100000 spikes per second. It is connected to the
98# neurons of the pre-synaptic population.
99
100pg = nest.Create("poisson_generator", params={"rate": 100000.0})
101nest.Connect(pg, pre, "all_to_all")
102
103###############################################################################
104# To measure and record the membrane potentials of the neurons, we create a
105# ``voltmeter`` and connect it to all postsynaptic nodes.
106
107vm = nest.Create("voltmeter")
108nest.Connect(vm, post, "all_to_all")
109
110###############################################################################
111# We save the whole connection graph of the network as a PNG image using the
112# ``plot_network`` function of the ``visualization`` submodule of PyNEST.
113
114allnodes = pg + pre + post + vm
115visualization.plot_network(allnodes, "csa_example_graph.png")
116
117###############################################################################
118# Finally, we simulate the network for 50 ms. The voltage traces of the
119# postsynaptic nodes are plotted.
120
121nest.Simulate(50.0)
122voltage_trace.from_device(vm)
123plt.show()
124