Setting neuron-neuron connections from a connectivity matrix of synapses

Description of problem

I’ve got a connectivity matrix, where the entries describes the number of synapses connecting one neuron to another. So, given the following 3 x 3 matrix,

 [
    [0, 5, 0],
    [0, 0, -2],
    [0, 0, 0],
]

we say that the 1st neuron connects to the second via 5 excitatory synapse connections. We then say that the 2nd neuron connects to the third via 2 inhibitory synapse connections. I think that my code accomplishes this (or is close to it, based on Github issue - Setting Neuron Weights), but when I try to set the actual weights, I run into an error: about DimensionMismatchError(error_message, dim1, dim2) ?

I’ve looked around and stumbled on the DimensionMismatchErrorClass page, but it’s not clear to me why the issue only pops up when I try to set the weights, not before. In my minimal code below, I specifically call out the line where the issue occurs.

Minimal code to reproduce problem

import numpy as np
from brian2 import Synapses, NeuronGroup, ms, mV, Hz

params = {
    'v_0'       : -52 * mV,               # resting potential
    'v_rst'     : -52 * mV,               # reset potential after spike
    'v_th'      : -45 * mV,               # threshold for spiking
    't_mbr'     :  20 * ms,               # membrane time scale (capacitance * resistance = .002 * uF * 10. * Mohm)
    't_rfc'     : 2.2 * ms,
    'tau'       : 5 * ms,                 # time constant
}

if __name__ == '__main__':
    connectivity_matrix = np.asarray([
        [0, 5, 0],
        [0, 0, -2],
        [0, 0, 0],
    ])
    eqs = '''
    dv/dt = (v_0 - v + g) / t_mbr : volt (unless refractory)
    dg/dt = -g / tau               : volt (unless refractory) 
    rfc                            : second
    '''

    neu = NeuronGroup(  # create neurons
        N=len(connectivity_matrix),
        model=eqs,
        method='linear',
        threshold='v > v_th',
        reset='v = v_rst; w = 0; g = 0 * mV',
        refractory='rfc',
        name='default_neurons',
        namespace=params
    )
    neu.v =-52 * mV
    neu.g = 0
    neu.rfc = 2.2 * ms

    sources, targets = connectivity_matrix.nonzero()
    syn = Synapses(neu, neu, 'w : volt', on_pre='g += w', delay=1.8 * ms, name='default_synapses')
    syn.connect(i=sources, j=targets)

    
    syn.w[:] = connectivity_matrix[(sources, targets)]  # <- the offending line
    print(syn)

What you have aready tried

Adding a debugger to the line and stepping through. Having said that, I don’t really know what I’m looking for.

Full traceback of error (if relevant)

  File "X/streamlined_model.py", line 38, in <module>
    syn.w[:] = connectivity_matrix[(sources, targets)]
  File "/opt/homebrew/anaconda3/envs/test/lib/python3.10/site-packages/brian2/core/variables.py", line 995, in __setitem__
    self.set_item(item, value, level=1)
  File "/opt/homebrew/anaconda3/envs/test/lib/python3.10/site-packages/brian2/core/variables.py", line 982, in set_item
    self.set_with_index_array(slice(None), value, check_units=check_units)
  File "/opt/homebrew/anaconda3/envs/test/lib/python3.10/site-packages/brian2/core/base.py", line 335, in device_override_decorated_function
    return func(*args, **kwds)
  File "/opt/homebrew/anaconda3/envs/test/lib/python3.10/site-packages/brian2/core/variables.py", line 1301, in set_with_index_array
    fail_for_dimension_mismatch(
  File "/opt/homebrew/anaconda3/envs/test/lib/python3.10/site-packages/brian2/units/fundamentalunits.py", line 260, in fail_for_dimension_mismatch
    raise DimensionMismatchError(error_message, dim1, dim2)
brian2.units.fundamentalunits.DimensionMismatchError: Incorrect unit for setting variable w (units are V and 1).

Hi @itq . In your model, the synaptic weight w has units of volts, but your connectivity matrix contains dimensionless values. When you set

syn.w[:] = connectivity_matrix[(sources, targets)]

Brian therefore complains. You need to decide what the strength of each individual synapse should be, e.g. if it is one mV, then you could write:

syn.w[:] = connectivity_matrix[(sources, targets)] * mV

A more general solution would be something like:

weight_per_synapse = 0.5*mV
syn.w[:] = connectivity_matrix[(sources, targets)] * weight_per_synapse

Hope that helps!