# Structuring the scripts

**URL:** <https://brian.discourse.group/t/structuring-the-scripts/112>\
**Category:** Support\
**Created:** [27 August 2020 15:57 UTC](https://brian.discourse.group/t/structuring-the-scripts/112 "2020-08-27T15:57:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ziaeemehr](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/ziaeemehr/32/12_2.png) [@Ziaeemehr](https://brian.discourse.group/u/Ziaeemehr)\
**Post date:** [27 August 2020 15:57 UTC](https://brian.discourse.group/t/structuring-the-scripts/112/1 "2020-08-27T15:57:22Z")

</div>

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.

```python

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?

---

<div class="post-metadata">

**Author:** ![Vigneswaran](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/vigneswaran/32/14_2.png) [@Vigneswaran](https://brian.discourse.group/u/Vigneswaran)\
**Post date:** [27 August 2020 17:07 UTC](https://brian.discourse.group/t/structuring-the-scripts/112/2 "2020-08-27T17:07:57Z")

</div>

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

```python
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,

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

```

---

<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:** [28 August 2020 08:37 UTC](https://brian.discourse.group/t/structuring-the-scripts/112/3 "2020-08-28T08:37:40Z")

</div>

As a little addition/correction: The first example is not quite correct, both `NeuronGroup`s 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](https://brian2.readthedocs.io/en/latest/advanced/namespaces.html), and make constants specific to each of the `NeuronGroups`:

```auto
neuron_a = b2.NeuronGroup(10, eqs,
                          namespace={'EL': -70*mV, 'tau_L': 10*ms})

```
