How to index a specified compartment of a downloaded spatial neuron

Description of problem
Hi everyone. I’m trying to simulate a spatial neuron model from neuromorpho.org. The schematic image is from the file H15-06-016-01-03-02_549378277_m.CNG.swc. If I create a synapse at the compartment pointed by the black arrow, then how can I know its id number to index (i.e, which subtree and what serial number)? Or vice versa, if I index a compartment with a specified id number, then how can I know its location on the graph? Although the swc file contains coordinates, there seems no “name” for each compartment on the graph for indexing.

Minimal code

from brian2 import *
from brian2tools import*
from mayavi import*

morpho = Morphology.from_file('file path')

figure()
plot_morphology(morpho, plot_3d=True, show_diameter=True)

Hi @czx,
working with complex morphologies like this is unfortunately still a bit of a hassle in Brian. When you use the plot_morphology function, there is a way to encode values in the color of the plot. You can use this to get the index of the compartment, but it is not convenient:

plot_morphology(morpho, plot_3d=True, show_diameter=True,
                values=np.arange(morpho.total_compartments))

You have to use the mayavi “picker” tool (press “p”) while pointing at the compartment you are interested in. The display is written in white, though, so quite hard to read (I guess this might be configurable somewhere):
Screenshot from 2024-09-03 12-36-05

If you look closely, this says scalar: 1.665e+04 in the bottom left corner. This is the index, but mayavi does not expect integer values there, so the value might be cut off as in this example (the actual index is 16554). If you go in the “Edit properties” menu, you can find the exact value in the picker history:

All this is obviously not very convenient. If you want to manually search for compartments and get their indices, the best way is to convert the Morphology object (which consists of linked objects) into a FlatMorphology, the internal representation used in Brian:

from brian2.spatialneuron.spatialneuron import FlatMorphology
flat_morpho = FlatMorphology(morpho)

This object contains a single array of values (x, y, diameter, etc.). For example, with the following code you could create a file that is similar to an SWC file (without the type/children information), but contains Brian’s index in the first column:

with open("brian_morphology.txt", "w") as f:
    f.write("# idx x y z radius\n")
    for idx, (x, y, z, diam) in enumerate(zip(flat_morpho.x, flat_morpho.y, flat_morpho.z, flat_morpho.diameter)):
        f.write(f"{idx} {x*1e6:.2f} {y*1e6:.2f} {z*1e6:.2f} {diam*1e6/2:.4f}\n")

This gives something like

# idx x y z radius
0 0.00 0.00 0.00 7.4360
1 0.82 3.31 -3.02 0.1144
2 1.77 7.18 -5.96 0.1258
3 2.00 8.30 -5.82 0.1373
4 2.16 9.42 -5.69 0.1373
5 2.31 10.55 -5.56 0.1373
6 2.49 11.67 -5.42 0.1373
7 2.65 12.79 -5.28 0.1373
8 2.71 13.92 -5.15 0.1373
9 2.54 15.02 -5.02 0.1373
10 2.12 16.06 -4.89 0.1373
...

Using/finding the names (e.g. morpho.dend.dend2) is possible in general, but I’m not sure it is very useful for such a detailed morphology. Here’s some code that prints out the names + indices for each section:

def _print_section(section, name=""):
    print(f"{name} {section.indices[0]}–{section.indices[-1]}")
    for child in section.children:
        _print_section(child, name=f"{name}.{section.children.name(child)}")

_print_section(morpho)

But this will also generate lines such as:

.axon.axon2.axon2.axon2.axon2.axon2.axon2.axon2.axon2.axon2.axon2.axon2.axon2.axon2.axon2.axon2.axon2.axon.axon.axon.axon.axon2.axon 14821–14977

For the index from above, the relevant entry is more manageable, though:

.dend3 16647–16661

Hope that these pointers were helpful!

PS: Please also note that Brian does use the compartments in an SWC as they are, i.e. in contrast to a software like NEURON, you cannot change the spatial resolution during simulations. If you want to simulate this neuron, you should most likely reduce the number of compartments first.

Hi @mstimberg ,
Thanks for your detailed illustration. It helps a lot! I’ll have a try again.