Description of problem
This issue involves indexing synapses. There seems to be a deviation from numpy conversion. Please look at the script below:
Minimal code to reproduce problem
import brian2 as b2
import numpy as np
import matplotlib.pyplot as plt
G = b2.NeuronGroup(1000, model='v:1')
S = b2.Synapses(G, model='w:1')
S.connect(p=0.1)
S.w = 0 # optional, it is anyway zero-initialized
idxs = S.j== 5
print(idxs.shape, sum(idxs)) # expected values: (N**2 *p, N*p) --> correct
# Here comes the weird part
S.w[idxs] = 1 # expecting N*p non-zero element
# however
print('\n\nOnly {} elements actually changed their value'.format(sum(S.w))) # always 2 !
plt.figure()
plt.plot(S.w, label='S.w')
plt.legend()
print('Also, strangely both S.w and S.w[idx] have equal shapes:')
print('Shape of S.w: {}'.format(S.w.shape))
print('Shape of S.w[idxs]: {}'.format(S.w[idxs].shape))
#plt.plot(S.w[idxs], label='S.w[idxs]')
# But somehow this can be changed. Let's reset everything
S.w = 0
S.w.__array__()[idxs] = 1 # added __array__()
print('\n\n{} are correctly changed their value'.format(sum(S.w)))
plt.figure()
plt.plot(S.w, label='S.w')
plt.legend()
print('Yet, the shapes are still equal:')
print('Shape of S.w: {}'.format(S.w.shape))
print('Shape of S.w[idxs]: {}'.format(S.w[idxs].shape))
with the following output:
(99897,) 82 # might be different for you...
Only 2.0 elements actually changed their value
Also, strangely both S.w and S.w[idx] have equal shapes:
Shape of S.w: (99897,)
Shape of S.w[idxs]: (99897,)
82.0 are correctly changed their value
Yet, the shapes are still equal:
Shape of S.w: (99897,)
Shape of S.w[idxs]: (99897,)
which shows that boolean indexing have the following mysterious behaviors:
- If
idxs
is a boolean array of sizeN
withn
True elements, andS
a Synapse objectN
synapse containing variablew
, thenS.w[idx]
does not have sizen
butN
. Why? - The operation
S.w[idxs]=<whatever_value>
, changes only the value of the first two components independent of theidxs
. Why? - Why using
__array__()
onw
fixes the issue?