Structuring the scripts

I am thinking about how to structure the scripts in a way that extending the code need the minimum effort and also avoid possible errors.

A simple example:
Consider we have two neuron model, each have a many parameters.
One approach is to define a function to define the each neuron model.
e.g. neuron A has parameter E_L, also neuron B has some parameters with the same name and different values.


def neuronA(paramsA):
      eqs = '''
          dv/dt = -gL(v-EL)/tau_L
      '''
      neuron_a = b2.NeuronGroup(...)
      return neuron_a

def neuronB(paramsB):
    eqs = '''
    dv/dt = -gL(v-EL)/tau_L
    '''
    neuron_b = b2.NeuronGroup(...)
    return neuron_b
S =b2.Synapses(neuron_a, neuron_b)
...

The problem is that when we want to define the synapse, the same parameter names conflict. Am I right?

Another approach is to define a large function and put all the required neurons inside the big function and change the parameter name to be different, e.g. E_L_A, E_L_B , …
Do you have any idea?

Hi, I think there won’t be any names conflict, the variable value will choose the latest one.
For example,

eqs = '''
          dv/dt = -gL(v-EL)/tau_L
      '''
EL = value_a
neuron_a = b2.NeuronGroup(...)
EL = value_b
neuron_a = b2.NeuronGroup(...)

will have different values, with same name. Also more of a parametric way (useful esp. when different parameter values for different neurons in same NeuronGroup) also works,

eqs = '''
          dv/dt = -gL(v-EL)/tau_L
          EL :<unit>
      '''
neuron_a.EL = value_a
neuron_b.EL = value_b
2 Likes

As a little addition/correction: The first example is not quite correct, both NeuronGroups will share the same value for EL with this approach. External constants are resolved at the time of the run call, so only the second EL value will be used.

Putting EL as a parameter in the equations is a good solution. I’d additionally put the (constant) flag to make clear that this is not a value that gets changed during the simulation. If the value is the same across neurons, it is more efficient to also state this fact by making it a “shared” parameter (i.e. use (constant, shared) as flags).

There are two other options:

  1. You can replace values directly in the equations string by using something like eqs = Equations('dv/dt = -gL*(v - EL)/tau_L : volt', EL=-70*mV, tau_L=10*ms) – this basically does string replacement, i.e. it puts the values directly into the equations.
  2. You can use the namespace mechanism, and make constants specific to each of the NeuronGroups:
neuron_a = b2.NeuronGroup(10, eqs,
                          namespace={'EL': -70*mV, 'tau_L': 10*ms})
2 Likes