Debian Codegraph @

docs_sphinx/user/multicompartmental.rst

raw
1 Multicompartment models
2 =======================
3 .. sidebar:: For Brian 1 users
4
5 See the document :doc:`../introduction/brian1_to_2/multicompartmental` for
6 details how to convert Brian 1 code.
7
8 It is possible to create neuron models with a spatially extended morphology, using
9 the `SpatialNeuron` class. A `SpatialNeuron` is a single neuron with many compartments.
10 Essentially, it works as a `NeuronGroup` where elements are compartments instead of neurons.
11
12 A `SpatialNeuron` is specified by a morphology (see :ref:`creating_morphology`) and a set of equations for
13 transmembrane currents (see :ref:`creating_spatialneuron`).
14
15 .. _creating_morphology:
16
17 Creating a neuron morphology
18 ----------------------------
19
20 Schematic morphologies
21 ~~~~~~~~~~~~~~~~~~~~~~
22 Morphologies can be created combining geometrical objects::
23
24 soma = Soma(diameter=30*um)
25 cylinder = Cylinder(diameter=1*um, length=100*um, n=10)
26
27 The first statement creates a single iso-potential compartment (i.e. with no axial resistance within the compartment),
28 with its area calculated as the area of a sphere with the given diameter. The second one specifies a cylinder consisting
29 of 10 compartments with identical diameter and the given total length.
30
31 For more precise control over the geometry, you can specify the length and diameter of each individual compartment,
32 including the diameter at the start of the section (i.e. for ``n`` compartments: ``n`` length and ``n+1`` diameter
33 values) in a `Section` object::
34
35 section = Section(diameter=[6, 5, 4, 3, 2, 1]*um, length=[10, 10, 10, 5, 5]*um, n=5)
36
37 The individual compartments are modeled as truncated cones, changing the diameter linearly between the given diameters
38 over the length of the compartment. Note that the ``diameter`` argument specifies the values at the nodes *between* the
39 compartments, but accessing the ``diameter`` attribute of a `Morphology` object will return the diameter at the *center*
40 of the compartment (see the note below).
41
42 The following table summarizes the different options to create schematic morphologies (the black compartment before the
43 start of the section represents the parent compartment with diameter 15 μm, not specified in the code below):
44
45 +-------------+-----------------------------------------------------------------------------------+
46 | | **Example** |
47 +=============+===================================================================================+
48 |**Soma** | :: |
49 | | |
50 | | # Soma always has a single compartment |
51 | | Soma(diameter=30*um) |
52 | | |
53 | | .. image:: images/soma.* |
54 | | |
55 +-------------+-----------------------------------------------------------------------------------+
56 |**Cylinder** | :: |
57 | | |
58 | | # Each compartment has fixed length and diameter |
59 | | Cylinder(n=5, diameter=10*um, length=50*um) |
60 | | |
61 | | .. image:: images/cylinder.* |
62 | | |
63 +-------------+-----------------------------------------------------------------------------------+
64 |**Section** | :: |
65 | | |
66 | | # Length and diameter individually defined for each compartment (at start |
67 | | # and end) |
68 | | Section(n=5, diameter=[15, 5, 10, 5, 10, 5]*um, |
69 | | length=[10, 20, 5, 5, 10]*um) |
70 | | |
71 | | .. image:: images/section.* |
72 | | |
73 +-------------+-----------------------------------------------------------------------------------+
74
75 .. note::
76
77 For a `Section`, the ``diameter`` argument specifies the diameter *between* the compartments
78 (and at the beginning/end of the first/last compartment). the corresponding values can therefore be later retrieved
79 from the `Morphology` via the ``start_diameter`` and ``end_diameter`` attributes. The ``diameter`` attribute of a
80 `Morphology` does correspond to the diameter at the midpoint of the compartment. For a `Cylinder`,
81 ``start_diameter``, ``diameter``, and ``end_diameter`` are of course all identical.
82
83 The tree structure of a morphology is created by attaching `Morphology` objects together::
84
85 morpho = Soma(diameter=30*um)
86 morpho.axon = Cylinder(length=100*um, diameter=1*um, n=10)
87 morpho.dendrite = Cylinder(length=50*um, diameter=2*um, n=5)
88
89 These statements create a morphology consisting of a cylindrical axon and a dendrite attached to a spherical soma.
90 Note that the names ``axon`` and ``dendrite`` are arbitrary and chosen by the user. For example, the same morphology can
91 be created as follows::
92
93 morpho = Soma(diameter=30*um)
94 morpho.output_process = Cylinder(length=100*um, diameter=1*um, n=10)
95 morpho.input_process = Cylinder(length=50*um, diameter=2*um, n=5)
96
97 The syntax is recursive, for example two sections can be added at the end of the dendrite as follows::
98
99 morpho.dendrite.branch1 = Cylinder(length=50*um, diameter=1*um, n=3)
100 morpho.dendrite.branch2 = Cylinder(length=50*um, diameter=1*um, n=3)
101
102 Equivalently, one can use an indexing syntax::
103
104 morpho['dendrite']['branch1'] = Cylinder(length=50*um, diameter=1*um, n=3)
105 morpho['dendrite']['branch2'] = Cylinder(length=50*um, diameter=1*um, n=3)
106
107 The names given to sections are completely up to the user. However, names that consist of a single digit (``1`` to
108 ``9``) or the letters ``L`` (for left) and ``R`` (for right) allow for a special short syntax: they can be joined
109 together directly, without the needs for dots (or dictionary syntax) and therefore allow to quickly navigate through
110 the morphology tree (e.g. ``morpho.LRLLR`` is equivalent to ``morpho.L.R.L.L.R``). This short syntax can also be used to
111 create trees::
112
113 >>> morpho = Soma(diameter=30*um)
114 >>> morpho.L = Cylinder(length=10*um, diameter=1*um, n=3)
115 >>> morpho.L1 = Cylinder(length=5*um, diameter=1*um, n=3)
116 >>> morpho.L2 = Cylinder(length=5*um, diameter=1*um, n=3)
117 >>> morpho.L3 = Cylinder(length=5*um, diameter=1*um, n=3)
118 >>> morpho.R = Cylinder(length=10*um, diameter=1*um, n=3)
119 >>> morpho.RL = Cylinder(length=5*um, diameter=1*um, n=3)
120 >>> morpho.RR = Cylinder(length=5*um, diameter=1*um, n=3)
121
122 The above instructions create a dendritic tree with two main sections, three sections attached to the first section and
123 two to the second. This can be verified with the `Morphology.topology` method::
124
125 >>> morpho.topology() # doctest: +NORMALIZE_WHITESPACE
126 ( ) [root]
127 `---| .L
128 `---| .L.1
129 `---| .L.2
130 `---| .L.3
131 `---| .R
132 `---| .R.L
133 `---| .R.R
134
135 Note that an expression such as ``morpho.L`` will always refer to the entire subtree. However, accessing the attributes
136 (e.g. ``diameter``) will only return the values for the given section.
137
138 .. note::
139
140 To avoid ambiguities, do not use names for sections that can be interpreted in the abbreviated way detailed above.
141 For example, do not name a child section ``L1`` (which will be interpreted as the first child of the child ``L``)
142
143 The number of compartments in a section can be accessed with ``morpho.n`` (or ``morpho.L.n``, etc.), the number of
144 total sections and compartments in a subtree can be accessed with ``morpho.total_sections`` and
145 ``morpho.total_compartments`` respectively.
146
147 Adding coordinates
148 ++++++++++++++++++
149
150 For plotting purposes, it can be useful to add coordinates to a `Morphology` that was created using the "schematic"
151 approach described above. This can be done by calling the `~Morphology.generate_coordinates` method on a morphology,
152 which will return an identical morphology but with additional 2D or 3D coordinates. By default, this method creates a
153 morphology according to a deterministic algorithm in 2D::
154
155 new_morpho = morpho.generate_coordinates()
156
157 .. image:: images/morphology_deterministic_coords.*
158
159
160 To get more "realistic" morphologies, this function can also be used to create morphologies in 3D where the orientation
161 of each section differs from the orientation of the parent section by a random amount::
162
163 new_morpho = morpho.generate_coordinates(section_randomness=25)
164
165 =============================================== =============================================== ===============================================
166 .. image:: images/morphology_random_section_1.* .. image:: images/morphology_random_section_2.* .. image:: images/morphology_random_section_3.*
167 =============================================== =============================================== ===============================================
168
169 This algorithm will base the orientation of each section on the orientation of the parent section and then randomly
170 perturb this orientation. More precisely, the algorithm first chooses a random vector orthogonal to the orientation
171 of the parent section. Then, the section will be rotated around this orthogonal vector by a random angle, drawn from an
172 exponential distribution with the :math:`\beta` parameter (in degrees) given by ``section_randomness``. This
173 :math:`\beta` parameter specifies both the mean and the standard deviation of the rotation angle. Note that no maximum
174 rotation angle is enforced, values for ``section_randomness`` should therefore be reasonably small (e.g. using a
175 ``section_randomness`` of ``45`` would already lead to a probability of ~14% that the section will be rotated by more
176 than 90 degrees, therefore making the section go "backwards").
177
178 In addition, also the orientation of each compartment within a section can be randomly varied::
179
180 new_morpho = morpho.generate_coordinates(section_randomness=25,
181 compartment_randomness=15)
182
183 =========================================================== =========================================================== ===========================================================
184 .. image:: images/morphology_random_section_compartment_1.* .. image:: images/morphology_random_section_compartment_2.* .. image:: images/morphology_random_section_compartment_3.*
185 =========================================================== =========================================================== ===========================================================
186
187 The algorithm is the same as the one presented above, but applied individually to each compartment within a section
188 (still based on the orientation on the parent *section*, not on the orientation of the previous *compartment*).
189
190 Complex morphologies
191 ~~~~~~~~~~~~~~~~~~~~
192
193 Morphologies can also be created from information about the compartment coordinates in 3D space. Such morphologies can
194 be loaded from a ``.swc`` file (a standard format for neuronal morphologies; for a large database of morphologies in
195 this format see http://neuromorpho.org)::
196
197 morpho = Morphology.from_file('corticalcell.swc')
198
199 To manually create a morphology from a list of points in a similar format to SWC files, see `Morphology.from_points`.
200
201 Morphologies that are created in such a way will use standard names for the sections that allow for the short syntax
202 shown in the previous sections: if a section has one or two child sections, then they will be called ``L`` and ``R``,
203 otherwise they will be numbered starting at ``1``.
204
205 Morphologies with coordinates can also be created section by section, following the same syntax as for "schematic"
206 morphologies::
207
208 soma = Soma(diameter=30*um, x=50*um, y=20*um)
209 cylinder = Cylinder(n=10, x=[0, 100]*um, diameter=1*um)
210 section = Section(n=5,
211 x=[0, 10, 20, 30, 40, 50]*um,
212 y=[0, 10, 20, 30, 40, 50]*um,
213 z=[0, 10, 10, 10, 10, 10]*um,
214 diameter=[6, 5, 4, 3, 2, 1]*um)
215
216 Note that the ``x``, ``y``, ``z`` attributes of `Morphology` and `SpatialNeuron` will return the coordinates at the
217 midpoint of each compartment (as for all other attributes that vary over the length of a compartment, e.g. ``diameter``
218 or ``distance``), but during construction the coordinates refer to the start and end of the section (`Cylinder`),
219 respectively to the coordinates of the nodes between the compartments (`Section`).
220
221 A few additional remarks:
222
223 1. In the majority of simulations, coordinates are not used in the neuronal equations, therefore the coordinates are
224 purely for visualization purposes and do not affect the simulation results in any way.
225 2. Coordinate specification cannot be combined with length specification -- lengths are automatically calculated from
226 the coordinates.
227 3. The coordinate specification can also be 1- or 2-dimensional (as in the first two examples above), the unspecified
228 coordinate will use 0 μm.
229 4. All coordinates are interpreted relative to the parent compartment, i.e. the point (0 μm, 0 μm, 0 μm) refers to the
230 end point of the previous compartment. Most of the time, the first element of the coordinate specification is
231 therefore 0 μm, to continue a section where the previous one ended. However, it can be convenient to use a value
232 different from 0 μm for sections connecting to the `Soma` to make them (visually) connect to a point on the sphere
233 surface instead of the center of the sphere.
234
235 .. _creating_spatialneuron:
236
237 Creating a spatially extended neuron
238 ------------------------------------
239
240 A `SpatialNeuron` is a spatially extended neuron. It is created by specifying the morphology as a
241 `Morphology` object, the equations for transmembrane currents, and optionally the specific membrane capacitance
242 ``Cm`` and intracellular resistivity ``Ri``::
243
244 gL = 1e-4*siemens/cm**2
245 EL = -70*mV
246 eqs = '''
247 Im=gL * (EL - v) : amp/meter**2
248 I : amp (point current)
249 '''
250 neuron = SpatialNeuron(morphology=morpho, model=eqs, Cm=1*uF/cm**2, Ri=100*ohm*cm)
251 neuron.v = EL + 10*mV
252
253 Several state variables are created automatically: the `SpatialNeuron` inherits all the geometrical variables of the
254 compartments (``length``, ``diameter``, ``area``, ``volume``), as well as the ``distance`` variable that gives the
255 distance to the soma. For morphologies that use coordinates, the ``x``, ``y`` and ``z`` variables are provided as well.
256 Additionally, a state variable ``Cm`` is created. It is initialized with the value given at construction, but it can be
257 modified on a compartment per compartment basis (which is useful to model myelinated axons). The membrane potential is
258 stored in state variable ``v``.
259
260 Note that for all variable values that vary across a compartment (e.g. ``distance``, ``x``, ``y``, ``z``, ``v``), the
261 value that is reported is the value at the midpoint of the compartment.
262
263 The key state variable, which must be specified at construction, is ``Im``. It is the total transmembrane current,
264 expressed in units of current per area. This is a mandatory line in the definition of the model. The rest of the
265 string description may include other state variables (differential equations or subexpressions)
266 or parameters, exactly as in `NeuronGroup`. At every timestep, Brian integrates the state variables, calculates the
267 transmembrane current at every point on the neuronal morphology, and updates ``v`` using the transmembrane current and
268 the diffusion current, which is calculated based on the morphology and the intracellular resistivity.
269 Note that the transmembrane current is a surfacic current, not the total current in the compartment.
270 This choice means that the model equations are independent of the number of compartments chosen for the simulation.
271 The space and time constants can obtained for any point of the neuron with the ``space_constant`` respectively
272 ``time_constant`` attributes::
273
274 l = neuron.space_constant[0]
275 tau = neuron.time_constant[0]
276
277 The calculation is based on the local total conductance (not just the leak conductance), therefore, it can potentially
278 vary during a simulation (e.g. decrease during an action potential). The reported value is only correct for compartments
279 with a cylindrical geometry, though, it does not give reasonable values for compartments with strongly varying
280 diameter.
281
282 To inject a current `I` at a particular point (e.g. through an electrode or a synapse), this current must be divided by
283 the area of the compartment when inserted in the transmembrane current equation. This is done automatically when
284 the flag ``point current`` is specified, as in the example above. This flag can apply only to subexpressions or
285 parameters with amp units. Internally, the expression of the transmembrane current ``Im`` is simply augmented with
286 ``+I/area``. A current can then be injected in the first compartment of the neuron (generally the soma) as follows::
287
288 neuron.I[0] = 1*nA
289
290 State variables of the `SpatialNeuron` include all the compartments of that neuron (including subtrees).
291 Therefore, the statement ``neuron.v = EL + 10*mV`` sets the membrane potential of the entire neuron at -60 mV.
292
293 Subtrees can be accessed by attribute (in the same way as in `Morphology` objects)::
294
295 neuron.axon.gNa = 10*gL
296
297 Note that the state variables correspond to the entire subtree, not just the main section.
298 That is, if the axon had branches, then the above statement would change ``gNa`` on the main section
299 and all the sections in the subtree. To access the main section only, use the attribute ``main``::
300
301 neuron.axon.main.gNa = 10*gL
302
303 A typical use case is when one wants to change parameter values at the soma only. For example, inserting
304 an electrode current at the soma is done as follows::
305
306 neuron.main.I = 1*nA
307
308 A part of a section can be accessed as follows::
309
310 initial_segment = neuron.axon[10*um:50*um]
311
312 Finally, similar to the way that you can refer to a subset of neurons of a
313 `NeuronGroup`, you can also index the `SpatialNeuron` object itself, e.g. to
314 get a group representing only the first compartment of a cell (typically the
315 soma), you can use::
316
317 soma = neuron[0]
318
319 In the same way as for sections, you can also use slices, either with the
320 indices of compartments, or with the distance from the root::
321
322 first_compartments = neuron[:3]
323 first_compartments = neuron[0*um:30*um]
324
325 However, note that this is restricted to contiguous indices which most of the
326 time means that all compartments indexed in this way have to be part of the
327 same section. Such indices can be acquired directly from the morphology::
328
329 axon = neuron[morpho.axon.indices[:]]
330
331 or, more concisely::
332
333 axon = neuron[morpho.axon]
334
335 Synaptic inputs
336 ~~~~~~~~~~~~~~~
337 There are two methods to have synapses on `SpatialNeuron`.
338 The first one to insert synaptic equations directly in the neuron equations::
339
340 eqs='''
341 Im = gL * (EL - v) : amp/meter**2
342 Is = gs * (Es - v) : amp (point current)
343 dgs/dt = -gs/taus : siemens
344 '''
345 neuron = SpatialNeuron(morphology=morpho, model=eqs, Cm=1*uF/cm**2, Ri=100*ohm*cm)
346
347 Note that, as for electrode stimulation, the synaptic current must be defined as a point current.
348 Then we use a `Synapses` object to connect a spike source to the neuron::
349
350 S = Synapses(stimulation, neuron, on_pre='gs += w')
351 S.connect(i=0, j=50)
352 S.connect(i=1, j=100)
353
354 This creates two synapses, on compartments 50 and 100. One can specify the compartment number
355 with its spatial position by indexing the morphology::
356
357 S.connect(i=0, j=morpho[25*um])
358 S.connect(i=1, j=morpho.axon[30*um])
359
360 In this method for creating synapses,
361 there is a single value for the synaptic conductance in any compartment.
362 This means that it will fail if there are several synapses onto the same compartment and synaptic equations
363 are nonlinear.
364 The second method, which works in such cases, is to have synaptic equations in the
365 `Synapses` object::
366
367 eqs='''
368 Im = gL * (EL - v) : amp/meter**2
369 Is = gs * (Es - v) : amp (point current)
370 gs : siemens
371 '''
372 neuron = SpatialNeuron(morphology=morpho, model=eqs, Cm=1 * uF / cm ** 2, Ri=100 * ohm * cm)
373 S = Synapses(stimulation, neuron, model='''dg/dt = -g/taus : siemens
374 gs_post = g : siemens (summed)''',
375 on_pre='g += w')
376
377 Here each synapse (instead of each compartment) has an associated value ``g``, and all values of
378 ``g`` for each compartment (i.e., all synapses targeting that compartment) are collected
379 into the compartmental variable ``gs``.
380
381 Detecting spikes
382 ~~~~~~~~~~~~~~~~
383 To detect and record spikes, we must specify a threshold condition, essentially in the same
384 way as for a `NeuronGroup`::
385
386 neuron = SpatialNeuron(morphology=morpho, model=eqs, threshold='v > 0*mV', refractory='v > -10*mV')
387
388 Here spikes are detected when the membrane potential ``v`` reaches 0 mV. Because there is generally
389 no explicit reset in this type of model (although it is possible to specify one), ``v`` remains above
390 0 mV for some time. To avoid detecting spikes during this entire time, we specify a refractory period.
391 In this case no spike is detected as long as ``v`` is greater than -10 mV. Another possibility could be::
392
393 neuron = SpatialNeuron(morphology=morpho, model=eqs, threshold='m > 0.5', refractory='m > 0.4')
394
395 where ``m`` is the state variable for sodium channel activation (assuming this has been defined in the
396 model). Here a spike is detected when half of the sodium channels are open.
397
398 With the syntax above, spikes are detected in all compartments of the neuron. To detect them in a single
399 compartment, use the ``threshold_location`` keyword::
400
401 neuron = SpatialNeuron(morphology=morpho, model=eqs, threshold='m > 0.5', threshold_location=30,
402 refractory='m > 0.4')
403
404 In this case, spikes are only detecting in compartment number 30. Reset then applies locally to
405 that compartment (if a reset statement is defined).
406 Again the location of the threshold can be specified with spatial position::
407
408 neuron = SpatialNeuron(morphology=morpho, model=eqs, threshold='m > 0.5',
409 threshold_location=morpho.axon[30*um],
410 refractory='m > 0.4')
411
412 Subgroups
413 ~~~~~~~~~
414
415 In the same way that you can refer to a subset of neurons in a `NeuronGroup`,
416 you can also refer to a subset of compartments in a `SpatialNeuron`