Interactive two population network example

You can experiment with the example code we used in the previous section in this interactive notebook. Use the launcher button above to open this notebook in Binder or Google Colab. 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
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")
nml_doc.izhikevich2007_cells.append(iz0)

Declare the Synapse and add it to the model document

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

Declare a Network and add it to the model document

net = Network(id="IzNet")
nml_doc.networks.append(net)

Create two populations

size0 = 5
pop0 = Population(id="IzPop0", component=iz0.id, size=size0)
net.populations.append(pop0)

size1 = 5
pop1 = Population(id="IzPop1", component=iz0.id, size=size1)
net.populations.append(pop1)

Declare projections

proj = Projection(id='proj', presynaptic_population=pop0.id,
                  postsynaptic_population=pop1.id, synapse=syn0.id)
net.projections.append(proj)

Add the projections between populations and the external inputs

random.seed(921)
prob_connection = 0.5
count = 0
for pre in range(0, size0):
    pg = PulseGenerator(
        id="pulseGen_%i" % pre, delay="0ms", duration="10000ms",
        amplitude="%f nA" % (0.1 * random.random())
    )
    nml_doc.pulse_generators.append(pg)

    exp_input = ExplicitInput(target="%s[%i]" % (pop0.id, pre), input=pg.id)
    net.explicit_inputs.append(exp_input)

    for post in range(0, size1):
        if random.random() <= prob_connection:
            syn = Connection(id=count,
                             pre_cell_id="../%s[%i]" % (pop0.id, pre),
                             synapse=syn0.id,
                             post_cell_id="../%s[%i]" % (pop1.id, post))
            proj.connections.append(syn)
            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)
pynml.validate_neuroml2(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  "/usr/share/java/jNeuroML-0.10.1.jar" -validate "izhikevich2007_network.nml" ) in directory: .
pyNeuroML >>> Command completed. Output: 
pyNeuroML >>>   jNeuroML >>   jNeuroML v0.10.1
pyNeuroML >>>   jNeuroML >>  Validating: /home/asinha/Documents/02_Code/00_mine/2020-OSB/NeuroML-Documentation/source/Userdocs/NML2_examples/izhikevich2007_network.nml
pyNeuroML >>>   jNeuroML >>  WARNING: An illegal reflective access operation has occurred
pyNeuroML >>>   jNeuroML >>  WARNING: Illegal reflective access by com.sun.xml.bind.v2.runtime.reflect.opt.Injector (file:/usr/share/java/jNeuroML-0.10.1.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int)
pyNeuroML >>>   jNeuroML >>  WARNING: Please consider reporting this to the maintainers of com.sun.xml.bind.v2.runtime.reflect.opt.Injector
pyNeuroML >>>   jNeuroML >>  WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
pyNeuroML >>>   jNeuroML >>  WARNING: All illegal access operations will be denied in a future release
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.6: 
pyNeuroML >>>     java -Xmx400M  -jar  "/usr/share/java/jNeuroML-0.10.1.jar" -validate "izhikevich2007_network.nml" 
pyNeuroML >>> Output:
pyNeuroML >>> 
pyNeuroML >>>  jNeuroML v0.10.1
pyNeuroML >>> Validating: /home/asinha/Documents/02_Code/00_mine/2020-OSB/NeuroML-Documentation/source/Userdocs/NML2_examples/izhikevich2007_network.nml
pyNeuroML >>> WARNING: An illegal reflective access operation has occurred
pyNeuroML >>> WARNING: Illegal reflective access by com.sun.xml.bind.v2.runtime.reflect.opt.Injector (file:/usr/share/java/jNeuroML-0.10.1.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int)
pyNeuroML >>> WARNING: Please consider reporting this to the maintainers of com.sun.xml.bind.v2.runtime.reflect.opt.Injector
pyNeuroML >>> WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
pyNeuroML >>> WARNING: All illegal access operations will be denied in a future release
pyNeuroML >>> Valid against schema and all tests
pyNeuroML >>> No warnings
pyNeuroML >>> 
pyNeuroML >>> Validated 1 files: All valid and no warnings
pyNeuroML >>> 
pyNeuroML >>> 
True

Simulating the model

Create a simulation instance of the model

simulation_id = "example_izhikevich2007network_sim"
simulation = LEMSSimulation(sim_id=simulation_id,
                            duration=10000, dt=0.1, simulation_seed=123)
simulation.assign_simulation_target(net.id)
simulation.include_neuroml2_file(nml_file)

Define the output file to store spikes

simulation.create_event_output_file(
    "pop0", "%s.spikes.dat" % simulation_id, format='ID_TIME'
)

for pre in range(0, size0):
    simulation.add_selection_to_event_output_file(
        "pop0", pre, 'IzPop0[{}]'.format(pre), 'spike')
/usr/lib/python3.9/site-packages/ipykernel/ipkernel.py:287: 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 NEURON

pynml.run_lems_with_jneuroml_neuron(
    lems_simulation_file, max_memory="2G", nogui=True, plot=False
)
True

Plot the recorded data

# Load the data from the file and plot the spike times
# using the pynml generate_plot utility function.
data_array = np.loadtxt("%s.spikes.dat" % simulation_id)
pynml.generate_plot(
    [data_array[:, 1]], [data_array[:, 0]],
    "Spike times", show_plot_already=False,
    save_figure_to="%s-spikes.png" % simulation_id,
    xaxis="time (s)", yaxis="cell ID",
    linestyles='', linewidths='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'>
../../_images/IzhikevichNetwork_30_2.png