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

Description of problem

I thought I could view/change variables of a synaptic subgroup as is possible for a neuron Subgroup, 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

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')

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]

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 ( – 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.

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.

1 Like

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

1 Like