# Indexing SpatialNeuron

**URL:** <https://brian.discourse.group/t/indexing-spatialneuron/487>\
**Category:** Support\
**Created:** [6 October 2021 07:51 UTC](https://brian.discourse.group/t/indexing-spatialneuron/487 "2021-10-06T07:51:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![schmitts](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/schmitts/32/191_2.png) [@schmitts](https://brian.discourse.group/u/schmitts)\
**Post date:** [6 October 2021 07:51 UTC](https://brian.discourse.group/t/indexing-spatialneuron/487/1 "2021-10-06T07:51:13Z")

</div>

Hi!

I’m working with Brian2 (2.4.2) for multi-compartment modelling. I’ve come across an oddity wrt. indexing the SpatialNeuron. The code below

```auto
from brian2 import *

eqs='''
gL : siemens/meter**2
Im = gL * (EL - v) : amp/meter**2
'''

morpho = Soma(diameter=30*um)
morpho.dendrite = Cylinder(length=10*um, diameter=1*um, n=2)

neuron = SpatialNeuron(morphology=morpho, model=eqs, Cm=1*uF/cm**2, Ri=100*ohm*cm)

gL = 1 * siemens/cm**2
neuron.gL = [1*gL, 2*gL, 3*gL]

print(neuron.gL)
print()

print(neuron.dendrite.gL)
print()

print(neuron.dendrite[0].gL)
print(neuron.dendrite[1].gL)

```

yields the output

```auto
<spatialneuron.gL: array([10000., 20000., 30000.]) * siemens / meter ** 2>

<spatialneuron_subgroup.gL: array([20000., 30000.]) * siemens / meter ** 2>

<spatialneuron_subgroup_1.gL: array([10000.]) * siemens / meter ** 2>
<spatialneuron_subgroup_1.gL: array([20000.]) * siemens / meter ** 2>

```

I would have expected the last two lines to give 20k and 30k respectively as I’m indexing the 0th and 1st compartment of the dendrite.

Anything wrong in the code or my mental model?

Thanks in advance,

Sebastian

---

<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:** [6 October 2021 08:23 UTC](https://brian.discourse.group/t/indexing-spatialneuron/487/2 "2021-10-06T08:23:21Z")

</div>

Hmm, this looks indeed like a bug, but I’ll have a closer look.  
In the meantime, you can always index the variable instead of the neuron, i.e. use:

```python
print(neuron.dendrite.gL[0])
print(neuron.dendrite.gL[1])

```

---

<div class="post-metadata">

**Author:** ![schmitts](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/schmitts/32/191_2.png) [@schmitts](https://brian.discourse.group/u/schmitts)\
**Post date:** [8 October 2021 07:01 UTC](https://brian.discourse.group/t/indexing-spatialneuron/487/3 "2021-10-08T07:01:52Z")

</div>

> <https://github.com/brian-team/brian2/pull/1358>
>
> As noted by @schmitts in the \[discussion forum\](https://brian.discourse.group/t/…indexing-spatialneuron/487), indexing with numbers does not work correctly in branches of \`SpatialNeuron\`, if done on the object itself instead of on the variable. As an example:
> \`\`\`python
> neuron.dendrite\[0\].v
> \`\`\`
> would not give the correct value (but \`neuron.dendrite.v\[0\]\` or indexing with distances would work).
> 
> This PR fixes this issue and adds additional tests verifying it.

Thanks!

---

<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:** [8 October 2021 08:43 UTC](https://brian.discourse.group/t/indexing-spatialneuron/487/4 "2021-10-08T08:43:57Z")

</div>

You are welcome. I just merged the PR, let us know if you come across any other issues. The multicompartmental module has a lot fewer users than the rest of Brian, so bugs can more easily go unnoticed.
