Examples#
The examples in this section are intended to give in depth overviews of how to accomplish specific tasks with libNeuroML.
These examples are located in the neuroml/examples directory and can be tested to confirm they work by running the run_all.py script.
Creating a NeuroML morphology#
"""
Example of connecting segments together to create a
multicompartmental model of a cell.
"""
import neuroml
import neuroml.writers as writers
p = neuroml.Point3DWithDiam(x=0, y=0, z=0, diameter=50)
d = neuroml.Point3DWithDiam(x=50, y=0, z=0, diameter=50)
soma = neuroml.Segment(proximal=p, distal=d)
soma.name = "Soma"
soma.id = 0
# Make an axon with 100 compartments:
parent = neuroml.SegmentParent(segments=soma.id)
parent_segment = soma
axon_segments = []
seg_id = 1
for i in range(100):
p = neuroml.Point3DWithDiam(
x=parent_segment.distal.x,
y=parent_segment.distal.y,
z=parent_segment.distal.z,
diameter=0.1,
)
d = neuroml.Point3DWithDiam(
x=parent_segment.distal.x + 10,
y=parent_segment.distal.y,
z=parent_segment.distal.z,
diameter=0.1,
)
axon_segment = neuroml.Segment(proximal=p, distal=d, parent=parent)
axon_segment.id = seg_id
axon_segment.name = "axon_segment_" + str(axon_segment.id)
# now reset everything:
parent = neuroml.SegmentParent(segments=axon_segment.id)
parent_segment = axon_segment
seg_id += 1
axon_segments.append(axon_segment)
test_morphology = neuroml.Morphology()
test_morphology.segments.append(soma)
test_morphology.segments += axon_segments
test_morphology.id = "TestMorphology"
cell = neuroml.Cell()
cell.name = "TestCell"
cell.id = "TestCell"
cell.morphology = test_morphology
doc = neuroml.NeuroMLDocument(id="TestNeuroMLDocument")
doc.cells.append(cell)
nml_file = "tmp/testmorphwrite.nml"
writers.NeuroMLWriter.write(doc, nml_file)
print("Written morphology file to: " + nml_file)
###### Validate the NeuroML ######
from neuroml.utils import validate_neuroml2
validate_neuroml2(nml_file)
Loading and modifying a file#
"""
In this example an axon is built, a morphology is loaded, the axon is
then connected to the loadeed morphology.
"""
import neuroml
import neuroml.loaders as loaders
import neuroml.writers as writers
fn = "./test_files/Purk2M9s.nml"
doc = loaders.NeuroMLLoader.load(fn)
print("Loaded morphology file from: " + fn)
# get the parent segment:
parent_segment = doc.cells[0].morphology.segments[0]
parent = neuroml.SegmentParent(segments=parent_segment.id)
# make an axon:
seg_id = 5000 # need a way to get a unique id from a morphology
axon_segments = []
for i in range(10):
p = neuroml.Point3DWithDiam(
x=parent_segment.distal.x,
y=parent_segment.distal.y,
z=parent_segment.distal.z,
diameter=0.1,
)
d = neuroml.Point3DWithDiam(
x=parent_segment.distal.x + 10,
y=parent_segment.distal.y,
z=parent_segment.distal.z,
diameter=0.1,
)
axon_segment = neuroml.Segment(proximal=p, distal=d, parent=parent)
axon_segment.id = seg_id
axon_segment.name = "axon_segment_" + str(axon_segment.id)
# now reset everything:
parent = neuroml.SegmentParent(segments=axon_segment.id)
parent_segment = axon_segment
seg_id += 1
axon_segments.append(axon_segment)
doc.cells[0].morphology.segments += axon_segments
nml_file = "./tmp/modified_morphology.nml"
writers.NeuroMLWriter.write(doc, nml_file)
print("Saved modified morphology file to: " + nml_file)
###### Validate the NeuroML ######
from neuroml.utils import validate_neuroml2
validate_neuroml2(nml_file)
Building a network#
"""
Example to build a full spiking IaF network
through libNeuroML, save it as XML and validate it
"""
from random import random
import neuroml.writers as writers
from neuroml import (
ExplicitInput,
ExpOneSynapse,
IafCell,
Network,
NeuroMLDocument,
Population,
PulseGenerator,
SynapticConnection,
)
nml_doc = NeuroMLDocument(id="IafNet")
IafCell0 = IafCell(
id="iaf0",
C="1.0 nF",
thresh="-50mV",
reset="-65mV",
leak_conductance="10 nS",
leak_reversal="-65mV",
)
nml_doc.iaf_cells.append(IafCell0)
IafCell1 = IafCell(
id="iaf1",
C="1.0 nF",
thresh="-50mV",
reset="-65mV",
leak_conductance="20 nS",
leak_reversal="-65mV",
)
nml_doc.iaf_cells.append(IafCell1)
syn0 = ExpOneSynapse(id="syn0", gbase="65nS", erev="0mV", tau_decay="3ms")
nml_doc.exp_one_synapses.append(syn0)
net = Network(id="IafNet")
nml_doc.networks.append(net)
size0 = 5
pop0 = Population(id="IafPop0", component=IafCell0.id, size=size0)
net.populations.append(pop0)
size1 = 5
pop1 = Population(id="IafPop1", component=IafCell0.id, size=size1)
net.populations.append(pop1)
prob_connection = 0.5
for pre in range(0, size0):
pg = PulseGenerator(
id="pulseGen_%i" % pre,
delay="0ms",
duration="100ms",
amplitude="%f nA" % (0.1 * 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):
# fromxx is used since from is Python keyword
if random() <= prob_connection:
syn = SynapticConnection(
from_="%s[%i]" % (pop0.id, pre),
synapse=syn0.id,
to="%s[%i]" % (pop1.id, post),
)
net.synaptic_connections.append(syn)
nml_file = "tmp/testnet.nml"
writers.NeuroMLWriter.write(nml_doc, nml_file)
print("Written network file to: " + nml_file)
###### Validate the NeuroML ######
from neuroml.utils import validate_neuroml2
validate_neuroml2(nml_file)
Building a 3D network#
"""
Example to build a full spiking IaF network throught libNeuroML & save it as XML & validate it
"""
from random import random
import neuroml.writers as writers
from neuroml import (
Cell,
Connection,
ExpOneSynapse,
Instance,
Location,
Morphology,
Network,
NeuroMLDocument,
Point3DWithDiam,
Population,
Projection,
Property,
Segment,
SegmentParent,
)
soma_diam = 10
soma_len = 10
dend_diam = 2
dend_len = 10
dend_num = 10
def generateRandomMorphology():
morphology = Morphology()
p = Point3DWithDiam(x=0, y=0, z=0, diameter=soma_diam)
d = Point3DWithDiam(x=soma_len, y=0, z=0, diameter=soma_diam)
soma = Segment(proximal=p, distal=d, name="Soma", id=0)
morphology.segments.append(soma)
parent_seg = soma
for dend_id in range(0, dend_num):
p = Point3DWithDiam(x=d.x, y=d.y, z=d.z, diameter=dend_diam)
d = Point3DWithDiam(x=p.x, y=p.y + dend_len, z=p.z, diameter=dend_diam)
dend = Segment(proximal=p, distal=d, name="Dend_%i" % dend_id, id=1 + dend_id)
dend.parent = SegmentParent(segments=parent_seg.id)
parent_seg = dend
morphology.segments.append(dend)
morphology.id = "TestMorphology"
return morphology
def run():
cell_num = 10
x_size = 500
y_size = 500
z_size = 500
nml_doc = NeuroMLDocument(id="Net3DExample")
syn0 = ExpOneSynapse(id="syn0", gbase="65nS", erev="0mV", tau_decay="3ms")
nml_doc.exp_one_synapses.append(syn0)
net = Network(id="Net3D")
nml_doc.networks.append(net)
proj_count = 0
# conn_count = 0
for cell_id in range(0, cell_num):
cell = Cell(id="Cell_%i" % cell_id)
cell.morphology = generateRandomMorphology()
nml_doc.cells.append(cell)
pop = Population(
id="Pop_%i" % cell_id, component=cell.id, type="populationList"
)
net.populations.append(pop)
pop.properties.append(Property(tag="color", value="1 0 0"))
inst = Instance(id="0")
pop.instances.append(inst)
inst.location = Location(
x=str(x_size * random()), y=str(y_size * random()), z=str(z_size * random())
)
prob_connection = 0.5
for post in range(0, cell_num):
if post is not cell_id and random() <= prob_connection:
from_pop = "Pop_%i" % cell_id
to_pop = "Pop_%i" % post
pre_seg_id = 0
post_seg_id = 1
projection = Projection(
id="Proj_%i" % proj_count,
presynaptic_population=from_pop,
postsynaptic_population=to_pop,
synapse=syn0.id,
)
net.projections.append(projection)
connection = Connection(
id=proj_count,
pre_cell_id="%s[%i]" % (from_pop, 0),
pre_segment_id=pre_seg_id,
pre_fraction_along=random(),
post_cell_id="%s[%i]" % (to_pop, 0),
post_segment_id=post_seg_id,
post_fraction_along=random(),
)
projection.connections.append(connection)
proj_count += 1
# net.synaptic_connections.append(SynapticConnection(from_="%s[%i]"%(from_pop,0), to="%s[%i]"%(to_pop,0)))
####### Write to file ######
nml_file = "tmp/net3d.nml"
writers.NeuroMLWriter.write(nml_doc, nml_file)
print("Written network file to: " + nml_file)
###### Validate the NeuroML ######
from neuroml.utils import validate_neuroml2
validate_neuroml2(nml_file)
run()
Ion channels#
"""
Generating a Hodgkin-Huxley Ion Channel and writing it to NeuroML
"""
import neuroml
import neuroml.writers as writers
chan = neuroml.IonChannelHH(
id="na",
conductance="10pS",
species="na",
notes="This is an example voltage-gated Na channel",
)
m_gate = neuroml.GateHHRates(id="m", instances="3")
h_gate = neuroml.GateHHRates(id="h", instances="1")
m_gate.forward_rate = neuroml.HHRate(
type="HHExpRate", rate="0.07per_ms", midpoint="-65mV", scale="-20mV"
)
m_gate.reverse_rate = neuroml.HHRate(
type="HHSigmoidRate", rate="1per_ms", midpoint="-35mV", scale="10mV"
)
h_gate.forward_rate = neuroml.HHRate(
type="HHExpLinearRate", rate="0.1per_ms", midpoint="-55mV", scale="10mV"
)
h_gate.reverse_rate = neuroml.HHRate(
type="HHExpRate", rate="0.125per_ms", midpoint="-65mV", scale="-80mV"
)
chan.gate_hh_rates.append(m_gate)
chan.gate_hh_rates.append(h_gate)
doc = neuroml.NeuroMLDocument()
doc.ion_channel_hhs.append(chan)
doc.id = "ChannelMLDemo"
nml_file = "./tmp/ionChannelTest.xml"
writers.NeuroMLWriter.write(doc, nml_file)
print("Written channel file to: " + nml_file)
###### Validate the NeuroML ######
from neuroml.utils import validate_neuroml2
validate_neuroml2(nml_file)
PyNN models#
"""
Example to build a PyNN based network
"""
from random import random
import neuroml.writers as writers
from neuroml import *
from neuroml import NeuroMLDocument
######################## Build the network ####################################
nml_doc = NeuroMLDocument(id="IafNet")
pynn0 = IF_curr_alpha(
id="IF_curr_alpha_pop_IF_curr_alpha",
cm="1.0",
i_offset="0.9",
tau_m="20.0",
tau_refrac="10.0",
tau_syn_E="0.5",
tau_syn_I="0.5",
v_init="-65",
v_reset="-62.0",
v_rest="-65.0",
v_thresh="-52.0",
)
nml_doc.IF_curr_alpha.append(pynn0)
pynn1 = HH_cond_exp(
id="HH_cond_exp_pop_HH_cond_exp",
cm="0.2",
e_rev_E="0.0",
e_rev_I="-80.0",
e_rev_K="-90.0",
e_rev_Na="50.0",
e_rev_leak="-65.0",
g_leak="0.01",
gbar_K="6.0",
gbar_Na="20.0",
i_offset="0.2",
tau_syn_E="0.2",
tau_syn_I="2.0",
v_init="-65",
v_offset="-63.0",
)
nml_doc.HH_cond_exp.append(pynn1)
pynnSynn0 = ExpCondSynapse(id="ps1", tau_syn="5", e_rev="0")
nml_doc.exp_cond_synapses.append(pynnSynn0)
nml_file = "tmp/pynn_network.xml"
writers.NeuroMLWriter.write(nml_doc, nml_file)
print("Saved to: " + nml_file)
###### Validate the NeuroML ######
from neuroml.utils import validate_neuroml2
validate_neuroml2(nml_file)
Synapses#
"""
Example to create a file with multiple synapse types
"""
from random import random
import neuroml.writers as writers
from neuroml import *
from neuroml import NeuroMLDocument
nml_doc = NeuroMLDocument(id="SomeSynapses")
expOneSyn0 = ExpOneSynapse(id="ampa", tau_decay="5ms", gbase="1nS", erev="0mV")
nml_doc.exp_one_synapses.append(expOneSyn0)
expTwoSyn0 = ExpTwoSynapse(
id="gaba", tau_decay="12ms", tau_rise="3ms", gbase="1nS", erev="-70mV"
)
nml_doc.exp_two_synapses.append(expTwoSyn0)
bpSyn = BlockingPlasticSynapse(
id="blockStpSynDep", gbase="1nS", erev="0mV", tau_rise="0.1ms", tau_decay="2ms"
)
bpSyn.notes = "This is a note"
bpSyn.plasticity_mechanism = PlasticityMechanism(
type="tsodyksMarkramDepMechanism", init_release_prob="0.5", tau_rec="120 ms"
)
bpSyn.block_mechanism = BlockMechanism(
type="voltageConcDepBlockMechanism",
species="mg",
block_concentration="1.2 mM",
scaling_conc="1.920544 mM",
scaling_volt="16.129 mV",
)
nml_doc.blocking_plastic_synapses.append(bpSyn)
nml_file = "tmp/synapses.xml"
writers.NeuroMLWriter.write(nml_doc, nml_file)
print("Saved to: " + nml_file)
###### Validate the NeuroML ######
from neuroml.utils import validate_neuroml2
validate_neuroml2(nml_file)
Working with arraymorphs#
"""
Example of connecting segments together to create a
multicompartmental model of a cell.
In this case ArrayMorphology will be used rather than
Morphology - demonstrating its similarity and
ability to save in HDF5 format
"""
import neuroml
import neuroml.arraymorph as am
import neuroml.writers as writers
p = neuroml.Point3DWithDiam(x=0, y=0, z=0, diameter=50)
d = neuroml.Point3DWithDiam(x=50, y=0, z=0, diameter=50)
soma = neuroml.Segment(proximal=p, distal=d)
soma.name = "Soma"
soma.id = 0
# now make an axon with 100 compartments:
parent = neuroml.SegmentParent(segments=soma.id)
parent_segment = soma
axon_segments = []
seg_id = 1
for i in range(100):
p = neuroml.Point3DWithDiam(
x=parent_segment.distal.x,
y=parent_segment.distal.y,
z=parent_segment.distal.z,
diameter=0.1,
)
d = neuroml.Point3DWithDiam(
x=parent_segment.distal.x + 10,
y=parent_segment.distal.y,
z=parent_segment.distal.z,
diameter=0.1,
)
axon_segment = neuroml.Segment(proximal=p, distal=d, parent=parent)
axon_segment.id = seg_id
axon_segment.name = "axon_segment_" + str(axon_segment.id)
# now reset everything:
parent = neuroml.SegmentParent(segments=axon_segment.id)
parent_segment = axon_segment
seg_id += 1
axon_segments.append(axon_segment)
test_morphology = am.ArrayMorphology()
test_morphology.segments.append(soma)
test_morphology.segments += axon_segments
test_morphology.id = "TestMorphology"
cell = neuroml.Cell()
cell.name = "TestCell"
cell.id = "TestCell"
cell.morphology = test_morphology
doc = neuroml.NeuroMLDocument()
# doc.name = "Test neuroML document"
doc.cells.append(cell)
doc.id = "TestNeuroMLDocument"
nml_file = "tmp/arraymorph.nml"
writers.NeuroMLWriter.write(doc, nml_file)
print("Written morphology file to: " + nml_file)
###### Validate the NeuroML ######
from neuroml.utils import validate_neuroml2
validate_neuroml2(nml_file)
Working with Izhikevich Cells#
These examples were kindly contributed by Steve Marsh
# from neuroml import NeuroMLDocument
from neuroml import IzhikevichCell
from neuroml.loaders import NeuroMLLoader
from neuroml.utils import validate_neuroml2
def load_izhikevich(filename="./test_files/SingleIzhikevich.nml"):
nml_filename = filename
validate_neuroml2(nml_filename)
nml_doc = NeuroMLLoader.load(nml_filename)
iz_cells = nml_doc.izhikevich_cells
for i, iz in enumerate(iz_cells):
if isinstance(iz, IzhikevichCell):
neuron_string = "%d %s %s %s %s %s (%s)" % (
i,
iz.v0,
iz.a,
iz.b,
iz.c,
iz.d,
iz.id,
)
print(neuron_string)
else:
print("Error: Cell %d is not an IzhikevichCell" % i)
load_izhikevich()
from neuroml import IzhikevichCell, NeuroMLDocument
from neuroml.utils import validate_neuroml2
from neuroml.writers import NeuroMLWriter
def write_izhikevich(filename="./tmp/SingleIzhikevich_test.nml"):
nml_doc = NeuroMLDocument(id="SingleIzhikevich")
nml_filename = filename
iz0 = IzhikevichCell(
id="iz0", v0="-70mV", thresh="30mV", a="0.02", b="0.2", c="-65.0", d="6"
)
nml_doc.izhikevich_cells.append(iz0)
NeuroMLWriter.write(nml_doc, nml_filename)
validate_neuroml2(nml_filename)
write_izhikevich()