Interactive two population network example#

To run this interactive Jupyter Notebook, please click on the rocket icon 🚀 in the top panel. For more information, please see how to use this documentation. Please uncomment the line below if you use the Google Colab. (It does not include these packages by default).

#%pip install pyneuroml neuromllite NEURON
#!/usr/bin/env python3
Create a simple network with two populations.

from neuroml import NeuroMLDocument
from neuroml import Izhikevich2007Cell
from neuroml import Network
from neuroml import ExpOneSynapse
from neuroml import Population
from neuroml import Projection
from neuroml import PulseGenerator
from neuroml import ExplicitInput
from neuroml import Connection
from neuroml import Property
import neuroml.writers as writers
import random
from pyneuroml import pynml
from pyneuroml.lems import LEMSSimulation
import numpy as np

Declaring the NeuroML model#

Create a NeuroML document#

nml_doc = NeuroMLDocument(id="IzNet")

Declare the Izhikevich cell and add it to the model document#

iz0 = Izhikevich2007Cell(
    id="iz2007RS0", v0="-60mV", C="100pF", k="0.7nS_per_mV", vr="-60mV",
    vt="-40mV", vpeak="35mV", a="0.03per_ms", b="-2nS", c="-50.0mV", d="100pA")

Declare the Synapse and add it to the model document#

syn0 = ExpOneSynapse(id="syn0", gbase="65nS", erev="0mV", tau_decay="3ms")

Declare a Network and add it to the model document#

net = Network(id="IzNet")

Create two populations#

size0 = 5
pop0 = Population(id="IzPop0",, size=size0)

size1 = 5
pop1 = Population(id="IzPop1",, size=size1)

Declare projections#

proj = Projection(id='proj',,

Add the projections between populations and the external inputs#

prob_connection = 0.8
count = 0
for pre in range(0, size0):
    pg = PulseGenerator(
        id="pg_%i" % pre, delay="0ms", duration="10000ms",
        amplitude="%f nA" % (0.1 + 0.1 * random.random())

    exp_input = ExplicitInput(target="%s[%i]" % (, pre),

    for post in range(0, size1):
        if random.random() <= prob_connection:
            syn = Connection(id=count,
                             pre_cell_id="../%s[%i]" % (, pre),
                             post_cell_id="../%s[%i]" % (, post))
            count += 1

Write the NeuroML model to a NeuroML file and validate it#

nml_file = 'izhikevich2007_network.nml'
writers.NeuroMLWriter.write(nml_doc, nml_file)

print("Written network file to: " + nml_file)
Written network file to: izhikevich2007_network.nml
pyNeuroML >>> Running jnml on izhikevich2007_network.nml with pre args: [-validate], post args: [], in dir: ., verbose: True, report: True, exit on fail: False
pyNeuroML >>> Executing: (java -Xmx400M  -jar  "/home/padraig/anaconda2/envs/py37/lib/python3.7/site-packages/pyNeuroML-0.5.13-py3.7.egg/pyneuroml/lib/jNeuroML-0.11.0-jar-with-dependencies.jar" -validate "izhikevich2007_network.nml" ) in directory: .
pyNeuroML >>> Command completed. Output: 
pyNeuroML >>>   jNeuroML >>   jNeuroML v0.11.0
pyNeuroML >>>   jNeuroML >>  Validating: /home/padraig/git/Documentation/source/Userdocs/NML2_examples/izhikevich2007_network.nml
pyNeuroML >>>   jNeuroML >>  Valid against schema and all tests
pyNeuroML >>>   jNeuroML >>  No warnings
pyNeuroML >>>   jNeuroML >>  
pyNeuroML >>>   jNeuroML >>  Validated 1 files: All valid and no warnings
pyNeuroML >>>   jNeuroML >>  
pyNeuroML >>>   jNeuroML >>  
pyNeuroML >>> Successfully ran the following command using pyNeuroML v0.5.13: 
pyNeuroML >>>     java -Xmx400M  -jar  "/home/padraig/anaconda2/envs/py37/lib/python3.7/site-packages/pyNeuroML-0.5.13-py3.7.egg/pyneuroml/lib/jNeuroML-0.11.0-jar-with-dependencies.jar" -validate "izhikevich2007_network.nml" 
pyNeuroML >>> Output:
pyNeuroML >>> 
pyNeuroML >>>  jNeuroML v0.11.0
pyNeuroML >>> Validating: /home/padraig/git/Documentation/source/Userdocs/NML2_examples/izhikevich2007_network.nml
pyNeuroML >>> Valid against schema and all tests
pyNeuroML >>> No warnings
pyNeuroML >>> 
pyNeuroML >>> Validated 1 files: All valid and no warnings
pyNeuroML >>> 
pyNeuroML >>> 

Simulating the model#

Create a simulation instance of the model#

simulation_id = "example_izhikevich2007network_sim"
simulation = LEMSSimulation(sim_id=simulation_id,
                            duration=1000, dt=0.1, simulation_seed=123)

Define the output file to store spikes#

    "pop0", "%s.0.spikes.dat" % simulation_id, format='ID_TIME'
for pre in range(0, size0):
        "pop0", pre, 'IzPop0[{}]'.format(pre), 'spike')

    "pop1", "%s.1.spikes.dat" % simulation_id, format='ID_TIME'
for pre in range(0, size1):
        "pop1", pre, 'IzPop1[{}]'.format(pre), 'spike')
/home/padraig/anaconda2/envs/py37/lib/python3.7/site-packages/ipykernel/ DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.
  and should_run_async(code)

Save the simulation to a file#

lems_simulation_file = simulation.save_to_file()
pyNeuroML >>> Written LEMS Simulation example_izhikevich2007network_sim to file: LEMS_example_izhikevich2007network_sim.xml

Run the simulation using jNeuroML#

    lems_simulation_file, max_memory="2G", nogui=True, plot=False

Plot the recorded data#

# Load the data from the file and plot the spike times
# using the pynml generate_plot utility function.
data_array_0 = np.loadtxt("%s.0.spikes.dat" % simulation_id)
data_array_1 = np.loadtxt("%s.1.spikes.dat" % simulation_id)
times_0 = data_array_0[:,1]
times_1 = data_array_1[:,1]
ids_0 = data_array_0[:,0]
ids_1 = [id+size0 for id in data_array_1[:,0]]
    [times_0,times_1], [ids_0,ids_1],
    "Spike times", show_plot_already=False,
    save_figure_to="%s-spikes.png" % simulation_id,
    xaxis="time (s)", yaxis="cell ID",
    linewidths=['0','0'], markers=['.','.'],
pyNeuroML >>> Generating plot: Spike times
pyNeuroML >>> Saved image to example_izhikevich2007network_sim-spikes.png of plot: Spike times
<AxesSubplot:xlabel='time (s)', ylabel='cell ID'>