.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/brunel_exp_multisynapse_nest.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_brunel_exp_multisynapse_nest.py: Random balanced network (exp synapses, multiple time constants) --------------------------------------------------------------- This script simulates an excitatory and an inhibitory population on the basis of the network used in [1]_. The example demonstrate the usage of the multisynapse neuron model. Each spike arriving at the neuron triggers an exponential PSP. The time constant associated with the PSP is defined in the receptor type array tau_syn of each neuron. The receptor types of all connections are uniformly distributed, resulting in uniformly distributed time constants of the PSPs. When connecting the network, customary synapse models are used, which allow for querying the number of created synapses. Using spike recorders, the average firing rates of the neurons in the populations are established. The building as well as the simulation time of the network are recorded. References ~~~~~~~~~~ .. [1] Brunel N (2000). Dynamics of sparsely connected networks of excitatory and inhibitory spiking neurons. Journal of Computational Neuroscience 8, 183-208. See Also ~~~~~~~~ :doc:`brunel_alpha_nest` .. GENERATED FROM PYTHON SOURCE LINES 57-58 Import all necessary modules for simulation, analysis and plotting. .. GENERATED FROM PYTHON SOURCE LINES 58-66 .. code-block:: default import time import nest import nest.raster_plot import matplotlib.pyplot as plt nest.ResetKernel() .. GENERATED FROM PYTHON SOURCE LINES 67-69 Assigning the current time to a variable in order to determine the build time of the network. .. GENERATED FROM PYTHON SOURCE LINES 69-72 .. code-block:: default startbuild = time.time() .. GENERATED FROM PYTHON SOURCE LINES 73-74 Assigning the simulation parameters to variables. .. GENERATED FROM PYTHON SOURCE LINES 74-79 .. code-block:: default dt = 0.1 # the resolution in ms simtime = 1000.0 # Simulation time in ms delay = 1.5 # synaptic delay in ms .. GENERATED FROM PYTHON SOURCE LINES 80-82 Definition of the parameters crucial for asynchronous irregular firing of the neurons. .. GENERATED FROM PYTHON SOURCE LINES 82-87 .. code-block:: default g = 5.0 # ratio inhibitory weight/excitatory weight eta = 2.0 # external rate relative to threshold rate epsilon = 0.1 # connection probability .. GENERATED FROM PYTHON SOURCE LINES 88-90 Definition of the number of neurons in the network and the number of neurons recorded from .. GENERATED FROM PYTHON SOURCE LINES 90-97 .. code-block:: default order = 2500 NE = 4 * order # number of excitatory neurons NI = 1 * order # number of inhibitory neurons N_neurons = NE + NI # number of neurons in total N_rec = 50 # record from 50 neurons .. GENERATED FROM PYTHON SOURCE LINES 98-99 Definition of connectivity parameters .. GENERATED FROM PYTHON SOURCE LINES 99-104 .. code-block:: default CE = int(epsilon * NE) # number of excitatory synapses per neuron CI = int(epsilon * NI) # number of inhibitory synapses per neuron C_tot = int(CI + CE) # total number of synapses per neuron .. GENERATED FROM PYTHON SOURCE LINES 105-107 Initialization of the parameters of the integrate and fire neuron and the synapses. The parameters of the neuron are stored in a dictionary. .. GENERATED FROM PYTHON SOURCE LINES 107-126 .. code-block:: default tauMem = 20.0 # time constant of membrane potential in ms theta = 20.0 # membrane threshold potential in mV J = 0.1 # postsynaptic amplitude in mV nr_ports = 100 # number of receptor types # Create array of synaptic time constants for each neuron, # ranging from 0.1 to 1.09 ms. tau_syn = [0.1 + 0.01 * i for i in range(nr_ports)] neuron_params = {"C_m": 1.0, "tau_m": tauMem, "t_ref": 2.0, "E_L": 0.0, "V_reset": 0.0, "V_m": 0.0, "V_th": theta, "tau_syn": tau_syn} J_ex = J # amplitude of excitatory postsynaptic current J_in = -g * J_ex # amplitude of inhibitory postsynaptic current .. GENERATED FROM PYTHON SOURCE LINES 127-131 Definition of threshold rate, which is the external rate needed to fix the membrane potential around its threshold, the external firing rate and the rate of the poisson generator which is multiplied by the in-degree CE and converted to Hz by multiplication by 1000. .. GENERATED FROM PYTHON SOURCE LINES 131-136 .. code-block:: default nu_th = theta / (J * CE * tauMem) nu_ex = eta * nu_th p_rate = 1000.0 * nu_ex * CE .. GENERATED FROM PYTHON SOURCE LINES 137-141 Configuration of the simulation kernel by the previously defined time resolution used in the simulation. Setting ``print_time`` to `True` prints the already processed simulation time as well as its percentage of the total simulation time. .. GENERATED FROM PYTHON SOURCE LINES 141-149 .. code-block:: default nest.resolution = dt nest.print_time = True nest.overwrite_files = True nest.local_num_threads = 4 print("Building network") .. GENERATED FROM PYTHON SOURCE LINES 150-155 Creation of the nodes using ``Create``. We store the returned handles in variables for later reference. Here the excitatory and inhibitory, as well as the poisson generator and two spike recorders. The spike recorders will later be used to record excitatory and inhibitory spikes. Properties of the nodes are specified via ``params``, which expects a dictionary. .. GENERATED FROM PYTHON SOURCE LINES 155-162 .. code-block:: default nodes_ex = nest.Create("iaf_psc_exp_multisynapse", NE, params=neuron_params) nodes_in = nest.Create("iaf_psc_exp_multisynapse", NI, params=neuron_params) noise = nest.Create("poisson_generator", params={"rate": p_rate}) espikes = nest.Create("spike_recorder") ispikes = nest.Create("spike_recorder") .. GENERATED FROM PYTHON SOURCE LINES 163-167 Configuration of the spike recorders recording excitatory and inhibitory spikes by sending parameter dictionaries to ``set``. Setting the property `record_to` to *"ascii"* ensures that the spikes will be recorded to a file, whose name starts with the string assigned to the property `label`. .. GENERATED FROM PYTHON SOURCE LINES 167-173 .. code-block:: default espikes.set(label="brunel-py-ex", record_to="ascii") ispikes.set(label="brunel-py-in", record_to="ascii") print("Connecting devices") .. GENERATED FROM PYTHON SOURCE LINES 174-180 Definition of a synapse using ``CopyModel``, which expects the model name of a pre-defined synapse, the name of the customary synapse and an optional parameter dictionary. The parameters defined in the dictionary will be the default parameter for the customary synapse. Here we define one synapse for the excitatory and one for the inhibitory connections giving the previously defined weights and equal delays. .. GENERATED FROM PYTHON SOURCE LINES 180-186 .. code-block:: default nest.CopyModel("static_synapse", "excitatory", {"weight": J_ex, "delay": delay}) nest.CopyModel("static_synapse", "inhibitory", {"weight": J_in, "delay": delay}) .. GENERATED FROM PYTHON SOURCE LINES 187-198 Connecting the previously defined poisson generator to the excitatory and inhibitory neurons using the excitatory synapse. Since the poisson generator is connected to all neurons in the population the default rule (# ``all_to_all``) of ``Connect`` is used. The synaptic properties are pre-defined # in a dictionary and inserted via ``syn_spec``. As synaptic model the pre-defined synapses "excitatory" and "inhibitory" are chosen, thus setting ``weight`` and ``delay``. The receptor type is drawn from a distribution for each connection, which is specified in the synapse properties by assigning a dictionary to the keyword ``receptor_type``, which includes the specification of the distribution and the associated parameter. .. GENERATED FROM PYTHON SOURCE LINES 198-209 .. code-block:: default syn_params_ex = {"synapse_model": "excitatory", "receptor_type": nest.random.uniform_int(max=nr_ports - 1) + 1 } syn_params_in = {"synapse_model": "inhibitory", "receptor_type": nest.random.uniform_int(max=nr_ports - 1) + 1 } nest.Connect(noise, nodes_ex, syn_spec=syn_params_ex) nest.Connect(noise, nodes_in, syn_spec=syn_params_ex) .. GENERATED FROM PYTHON SOURCE LINES 210-214 Connecting the first ``N_rec`` nodes of the excitatory and inhibitory population to the associated spike recorders using excitatory synapses. Here the same shortcut for the specification of the synapse as defined above is used. .. GENERATED FROM PYTHON SOURCE LINES 214-222 .. code-block:: default nest.Connect(nodes_ex[:N_rec], espikes, syn_spec="excitatory") nest.Connect(nodes_in[:N_rec], ispikes, syn_spec="excitatory") print("Connecting network") print("Excitatory connections") .. GENERATED FROM PYTHON SOURCE LINES 223-228 Connecting the excitatory population to all neurons while distribution the ports. Here we use the previously defined parameter dictionary ``syn_params_ex``. Beforehand, the connection parameter are defined in a dictionary. Here we use the connection rule ``fixed_indegree``, which requires the definition of the indegree. .. GENERATED FROM PYTHON SOURCE LINES 228-234 .. code-block:: default conn_params_ex = {'rule': 'fixed_indegree', 'indegree': CE} nest.Connect(nodes_ex, nodes_ex + nodes_in, conn_params_ex, syn_params_ex) print("Inhibitory connections") .. GENERATED FROM PYTHON SOURCE LINES 235-239 Connecting the inhibitory population to all neurons while distribution the ports. Here we use the previously defined parameter dictionary ``syn_params_in``.The connection parameter are defined analogously to the connection from the excitatory population defined above. .. GENERATED FROM PYTHON SOURCE LINES 239-243 .. code-block:: default conn_params_in = {'rule': 'fixed_indegree', 'indegree': CI} nest.Connect(nodes_in, nodes_ex + nodes_in, conn_params_in, syn_params_in) .. GENERATED FROM PYTHON SOURCE LINES 244-245 Storage of the time point after the buildup of the network in a variable. .. GENERATED FROM PYTHON SOURCE LINES 245-248 .. code-block:: default endbuild = time.time() .. GENERATED FROM PYTHON SOURCE LINES 249-250 Simulation of the network. .. GENERATED FROM PYTHON SOURCE LINES 250-255 .. code-block:: default print("Simulating") nest.Simulate(simtime) .. GENERATED FROM PYTHON SOURCE LINES 256-257 Storage of the time point after the simulation of the network in a variable. .. GENERATED FROM PYTHON SOURCE LINES 257-260 .. code-block:: default endsimulate = time.time() .. GENERATED FROM PYTHON SOURCE LINES 261-263 Reading out the total number of spikes received from the spike recorder connected to the excitatory population and the inhibitory population. .. GENERATED FROM PYTHON SOURCE LINES 263-267 .. code-block:: default events_ex = espikes.n_events events_in = ispikes.n_events .. GENERATED FROM PYTHON SOURCE LINES 268-272 Calculation of the average firing rate of the excitatory and the inhibitory neurons by dividing the total number of recorded spikes by the number of neurons recorded from and the simulation time. The multiplication by 1000.0 converts the unit 1/ms to 1/s=Hz. .. GENERATED FROM PYTHON SOURCE LINES 272-276 .. code-block:: default rate_ex = events_ex / simtime * 1000.0 / N_rec rate_in = events_in / simtime * 1000.0 / N_rec .. GENERATED FROM PYTHON SOURCE LINES 277-280 Reading out the number of connections established using the excitatory and inhibitory synapse model. The numbers are summed up resulting in the total number of synapses. .. GENERATED FROM PYTHON SOURCE LINES 280-285 .. code-block:: default num_synapses_ex = nest.GetDefaults("excitatory")["num_connections"] num_synapses_in = nest.GetDefaults("inhibitory")["num_connections"] num_synapses = num_synapses_ex + num_synapses_in .. GENERATED FROM PYTHON SOURCE LINES 286-288 Establishing the time it took to build and simulate the network by taking the difference of the pre-defined time variables. .. GENERATED FROM PYTHON SOURCE LINES 288-292 .. code-block:: default build_time = endbuild - startbuild sim_time = endsimulate - endbuild .. GENERATED FROM PYTHON SOURCE LINES 293-294 Printing the network properties, firing rates and building times. .. GENERATED FROM PYTHON SOURCE LINES 294-305 .. code-block:: default print("Brunel network simulation (Python)") print(f"Number of neurons : {N_neurons}") print(f"Number of synapses: {num_synapses}") print(f" Excitatory : {num_synapses_ex}") print(f" Inhibitory : {num_synapses_in}") print(f"Excitatory rate : {rate_ex:.2f} Hz") print(f"Inhibitory rate : {rate_in:.2f} Hz") print(f"Building time : {build_time:.2f} s") print(f"Simulation time : {sim_time:.2f} s") .. GENERATED FROM PYTHON SOURCE LINES 306-307 Plot a raster of the excitatory neurons and a histogram. .. GENERATED FROM PYTHON SOURCE LINES 307-310 .. code-block:: default nest.raster_plot.from_device(espikes, hist=True) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.000 seconds) .. _sphx_glr_download_auto_examples_brunel_exp_multisynapse_nest.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: brunel_exp_multisynapse_nest.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: brunel_exp_multisynapse_nest.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_