# How to index a specified compartment of a downloaded spatial neuron

**URL:** <https://brian.discourse.group/t/how-to-index-a-specified-compartment-of-a-downloaded-spatial-neuron/1286>\
**Category:** Support\
**Tags:** multicompartment, morphology, swc\
**Created:** [29 August 2024 08:27 UTC](https://brian.discourse.group/t/how-to-index-a-specified-compartment-of-a-downloaded-spatial-neuron/1286 "2024-08-29T08:27:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![czx](https://avatars.discourse-cdn.com/v4/letter/c/7ba0ec/32.png) [@czx](https://brian.discourse.group/u/czx)\
**Post date:** [29 August 2024 08:27 UTC](https://brian.discourse.group/t/how-to-index-a-specified-compartment-of-a-downloaded-spatial-neuron/1286/1 "2024-08-29T08:27:09Z")

</div>

Description of problem  
Hi everyone. I’m trying to simulate a spatial neuron model from _[neuromorpho.org](http://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.

 ![image](https://global.discourse-cdn.com/free1/uploads/brian/original/1X/da4ea69874ac1f465ff806fadc79535097e5580b.jpeg)

Minimal code

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![mstimberg](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/mstimberg/32/11_2.png) [@mstimberg](https://brian.discourse.group/u/mstimberg)\
**Post date:** [3 September 2024 12:36 UTC](https://brian.discourse.group/t/how-to-index-a-specified-compartment-of-a-downloaded-spatial-neuron/1286/2 "2024-09-03T12:36:21Z")

</div>

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:

```python
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](https://global.discourse-cdn.com/free1/uploads/brian/original/1X/51e8dca8ce2a9a85f25cb4009a82042c65ef58f5.png)

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:

 ![Screenshot from 2024-09-03 12-36-42](https://global.discourse-cdn.com/free1/uploads/brian/original/1X/d93e35a75386a38cffb683b82897fa34576f4a84.png)

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:

```python
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:

```python
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

```auto
# 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:

```python
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:

```auto
.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:

```auto
.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.

---

<div class="post-metadata">

**Author:** ![czx](https://avatars.discourse-cdn.com/v4/letter/c/7ba0ec/32.png) [@czx](https://brian.discourse.group/u/czx)\
**Post date:** [4 September 2024 06:51 UTC](https://brian.discourse.group/t/how-to-index-a-specified-compartment-of-a-downloaded-spatial-neuron/1286/3 "2024-09-04T06:51:33Z")

</div>

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