# SynapticSubgroup doesn't support viewing/assigning variables / Best practice?

**URL:** <https://brian.discourse.group/t/synapticsubgroup-doesnt-support-viewing-assigning-variables-best-practice/1017>\
**Category:** Support\
**Created:** [16 June 2023 18:58 UTC](https://brian.discourse.group/t/synapticsubgroup-doesnt-support-viewing-assigning-variables-best-practice/1017 "2023-06-16T18:58:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kjohnsen](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/kjohnsen/32/200_2.png) [@kjohnsen](https://brian.discourse.group/u/kjohnsen)\
**Post date:** [16 June 2023 18:58 UTC](https://brian.discourse.group/t/synapticsubgroup-doesnt-support-viewing-assigning-variables-best-practice/1017/1 "2023-06-16T18:58:31Z")

</div>

# Description of problem

I thought I could view/change variables of a synaptic subgroup [as is possible for a neuron Subgroup](https://brian2.readthedocs.io/en/stable/user/models.html#subgroups), but I cannot. I don’t see this mentioned in the docs anywhere.

I can work around this, but I just thought I’d share something that was counterintuitive to me, **as well as ask a quick question** :

_From what I understand, it’s more efficient to have a single NeuronGroup when different types share equations, then use SubGroups for each. Does the same go for Synapses? Should I use a single Synapses object when possible between two neuron groups, rather than different Synapses objects between each pair of subgroups?_

# Minimal code to reproduce problem

```python
#%%
from brian2 import *

#%%
ng1 = NeuronGroup(2, 'dv/dt = -v/(10*ms) : 1', threshold='v>1', reset='v=0')
ng2 = NeuronGroup(2, 'dv/dt = -v/(10*ms) : 1', threshold='v>1', reset='v=0')
syn = Synapses(ng1, ng2, 'w : 1', on_pre='v_post += w')
syn.connect()

print(syn.w) # [0, 0, 0, 0]

syn.w[0] = 1
print(syn.w) # [1, 0, 0, 0]

synsub = syn[:, 1]
# print(synsub.w) # <-- gives an error

synsub.w = 2
print(synsub.w) # 2
print(syn.w) # [1, 0, 0, 0]

# workaround:
syn.w[:, 1] = 3
print(syn.w) # [1, 3, 0, 3]

```

---

<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:** [19 June 2023 16:19 UTC](https://brian.discourse.group/t/synapticsubgroup-doesnt-support-viewing-assigning-variables-best-practice/1017/2 "2023-06-19T16:19:24Z")

</div>

Hi @kjohnsen I agree that this is confusing. The `SynapticSubgroup` is currently of very limited use, it is only a placeholder for indices. E.g. you could monitor all weights targetting neuron 1 by using `StateMonitor(syn, 'w', record=syn[:, 1])`.  
The good news is that I have been working on fixing this as part of a major overhaul of the indexing system ([Non-contiguous subgroups by mstimberg · Pull Request #1229 · brian-team/brian2 · GitHub](https://github.com/brian-team/brian2/pull/1229)) – it is still not quite ready to be merged, but I am getting there. As you saw, using `syn.w[...]` instead of `syn[...].w` is currently the way to go – after merging the PR, both would work in the same way.

> [@kjohnsen](#):
>
> From what I understand, it’s more efficient to have a single NeuronGroup when different types share equations, then use SubGroups for each. Does the same go for Synapses? Should I use a single Synapses object when possible between two neuron groups, rather than different Synapses objects between each pair of subgroups?

Yes, in general this is preferable. It is mostly affecting compilation times, though (due to many additional near-identical source code files that need to be compiled); the simulation time shouldn’t change much for reasonably large networks.

---

<div class="post-metadata">

**Author:** ![kjohnsen](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/kjohnsen/32/200_2.png) [@kjohnsen](https://brian.discourse.group/u/kjohnsen)\
**Post date:** [19 June 2023 22:41 UTC](https://brian.discourse.group/t/synapticsubgroup-doesnt-support-viewing-assigning-variables-best-practice/1017/3 "2023-06-19T22:41:39Z")

</div>

All right, that answers my questions. Thanks! Good luck on the PR.
