02. Branched morphologies#
In this tutorial, we will explore how to use Dendra to simulate branched neuron morphologies. Branched morphologies are essential for accurately modeling the complex structures of real neurons, which often have multiple dendritic branches and axonal projections.
General principles#
Branched morphologies inherit the same “array of compartments in a Population” abstraction as unbranched morphologies. Each compartment can still be treated as a single electrical unit, with its own membrane potential and ionic currents. The key difference is that branched morphologies allow for more complex connectivity between compartments, enabling the simulation of signal propagation through dendritic trees and axonal arbors.
To define a branched morphology in Dendra, we use the Tree class, which allows us to specify the structure of the neuron using a tree-like representation. Use a native Morphology to author a tree or load an SWC or ASC file. Use from_NEURON() to capture an existing NEURON Section tree. ASC loading uses NEURON to parse the geometry; compartment selection and simulation use Dendra’s native representation.
Example: 3D morphology with HH-dynamics#
We will simulate a single neuron with a branched morphology using Hodgkin-Huxley dynamics. Dendra implements various convenience functions to read in morphologies from standard file formats like SWC or from NEURON.
This example uses L23_PC_cADpyr229_1.asc, a Blue Brain Project L2/3 pyramidal morphology (Copyright © 2015 EPFL-BBP), from the Blue Brain cell package, described by Markram et al. (2015). The morphology is licensed under CC BY-NC-SA 4.0; see License for source and license notices.
import dendra as dn
from dendra.models.mod import hh
dn.set_jit_enabled(True) # True to enable jit compilation globally
model = dn.Tree.from_asc(
"L23_PC_cADpyr229_1.asc",
v_init=-65.0,
celsius=6.3,
rhoa=100.0,
principal_axis=[0, 1, 0],
) # Load morphology from file
# this automatically applies the D-lambda rule to set compartment lengths, with
# d_lambda=0.1 at 100 Hz. This morphology API takes freq as a raw Hz value,
# rather than Dendra's usual kHz simulation-frequency convention, e.g.:
# model = dn.Tree.from_asc("L23_PC_cADpyr229_1.asc", d_lambda=0.05, freq=200.0)
# The default axial resistivity and membrane capacitance is 35.4 Ω·cm
# and 1.0 µF/cm² (consistent with NEURON), but you can change this with the
# 'rhoa' and 'cm' arguments if desired.
# Note also that the principal_axis argument can be used to specify the main
# orientation of the tree morphology. Here we specify that the principal axis
# is along the y-axis (default is z-axis, [0, 0, 1]).
Let’s visualize:
from dendra.models.visualization import vis_2d
vis_2d(model, view="x")
vis_2d(model, view="y")
vis_2d(model, view="z")
(<Figure size 1600x1600 with 1 Axes>, <Axes: xlabel='x (µm)', ylabel='y (µm)'>)
One thing to note at this point is that Dendra automatically constructs labeled Slice’s for branched morphologies, which can be used to easily reference specific parts of the morphology (e.g., soma, dendrites, axon). This is particularly useful for applying stimuli or recording from specific regions of the neuron:
Morphology-aware name matching#
find() and slice() understand common morphology naming conventions. Bare queries are token-aware, while * requests explicit glob-style matching.
cell.find("soma") # soma, soma[0], soma[1], MelnickSG_soma, Cell.soma[3]
cell.find("soma[0]") # only soma[0]
cell.find("soma[*]") # indexed soma[...] sections only
cell.find("myelin") # myelin, not unmyelin
cell.find("unmyelin") # unmyelin, not myelin
cell.find("*myelin") # suffix match: myelin and unmyelin
Use loc when you want one location per named section rather than every discretized compartment in that section:
cell.slice("soma", loc=0.5)
cell.slice("apic", loc=1.0)
Importing morphology directly from NEURON#
Dendra can also use NEURON as a morphology frontend. This is useful when a model already defines its sections in HOC/Python and you want Dendra to reproduce that topology:
from neuron import h
import dendra as dn
soma = h.Section(name="soma")
dend = h.Section(name="dend")
dend.connect(soma(0))
soma.L = soma.diam = 20.0
dend.L = 200.0
dend.diam = 1.0
cell = dn.Tree.from_NEURON(root_sec=soma, N=1, v_init=-65.0)
cell.slice("soma").label("soma")
cell.slice("dend").label("dend")
The imported section names are searchable with the same find(...) / slice(...) contract described above.
print(model.soma.v)
print(model.dend.v)
tensor([[-65.]])
tensor([[-65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65.,
-65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65.,
-65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65.,
-65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65.,
-65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65.,
-65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65.,
-65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65.,
-65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65.,
-65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65.,
-65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65.,
-65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65.,
-65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65.,
-65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65.,
-65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65.,
-65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65.,
-65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65.,
-65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65.,
-65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65.,
-65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65., -65.,
-65., -65., -65., -65.]])
For modeling extracellular stimulation, it is especially important to be able to control where and in what orientiation the morphology is placed in 3D space. Dendra provides several convenience functions to help with this, such as move_to() to move the soma, recentre() to center the morphology at the origin (equivalent to moving the soma to [0, 0, 0]), and rotate_into_direction() to align the morphology along a specified axis.
print(model.soma.x.item(), model.soma.y.item(), model.soma.z.item())
model.move_to(100.0, 0, 0)
print(model.soma.x.item(), model.soma.y.item(), model.soma.z.item())
model.recentre()
print(model.soma.x.item(), model.soma.y.item(), model.soma.z.item())
-0.5143091678619385 0.22424888610839844 0.0
100.0 0.0 0.0
0.0 0.0 0.0
model = model.rotate_into_direction([[0, 0, 1]]) # Rotate to align along z-axis
for axis in ["x", "y", "z"]:
vis_2d(model, view=axis)
The exact same API is used to specify biophysical properties, stimuli, and recording as for unbranched morphologies. For example, we can insert Hodgkin-Huxley dynamics into the entire morphology using the insert() method:
model.insert(hh)
And then stimulate e.g. with a current source injected into the soma:
from dendra.units import nA
model.soma.inject(dn.mono_rect(amp=2.0 * nA, pw=1.0, delay=0.1))
rec = dn.callbacks.RecorderLambda(
{
"soma": lambda cell: cell.soma.v,
"dend": lambda cell: cell.dend[0, -1].v,
"axon": lambda cell: cell.axon[0, -1].v,
}
)
tstop, dt = 10.0, 0.025
rec.reset()
model.initialize()
model.run(tstop=10.0, dt=0.025, callbacks=[rec])
import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0, tstop+dt, dt)
plt.figure(figsize=(5, 3), dpi=200)
plt.plot(t, rec.numpy('soma').flatten(), label="soma")
plt.plot(t, rec.numpy('dend').flatten(), label="dend[0, -1]")
plt.plot(t, rec.numpy('axon').flatten(), label="axon[0, -1]")
plt.xlabel("Time (ms)")
plt.ylabel("Membrane potential (mV)")
plt.legend()
plt.show()
The exact same API for extracellular stimulation is also valid:
from dendra.units import mA, kHz
stim_source = dn.isotropic_point(x=400.0, y=-400.0, z=200.0) # coordinates in μm
ve_stim = stim_source(model)
i_stim = dn.sin(amp=0.5 * mA, freq=1.0 * kHz) # 0.5 mA, 1 kHz sine wave
tstop, dt = 50.0, 0.001
# run as before, but supply extracellular potential for stimulation
# for longer simulations, we recommend using longrun with chunking
rec.reset()
model.delete_injections() # remove all previous current injection(s)
model.initialize()
model.longrun(
tstop=tstop, dt=dt, chunklength=100,
extra=(ve_stim, i_stim), callbacks=[rec],
progressbar=False
)
import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0, tstop+dt, dt)
plt.figure(figsize=(5, 3), dpi=200)
plt.plot(t, rec.numpy('soma').flatten(), label="soma")
plt.plot(t, rec.numpy('dend').flatten(), label="dend[0, -1]")
plt.plot(t, rec.numpy('axon').flatten(), label="axon[0, -1]")
plt.xlabel("Time (ms)")
plt.ylabel("Membrane potential (mV)")
plt.legend()
plt.show()
And, as before, we can use the same API for LFP recording, and stimulating and recording from populations of branched morphologies as well. To get a population, we use the same from_NEURON() or from_swc() methods, but specify N>1:
with dn.ctx(IMEM=1): # to record membrane currents for LFP
pop = dn.Tree.from_asc(
"path/to/morphology.asc",
N=100,
)
The rest of the simulation code remains unchanged.