# Time dependend input in equation vs. parameter

**URL:** <https://brian.discourse.group/t/time-dependend-input-in-equation-vs-parameter/753>\
**Category:** Support\
**Created:** [26 July 2022 07:15 UTC](https://brian.discourse.group/t/time-dependend-input-in-equation-vs-parameter/753 "2022-07-26T07:15:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![schmitts](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/schmitts/32/191_2.png) [@schmitts](https://brian.discourse.group/u/schmitts)\
**Post date:** [26 July 2022 07:15 UTC](https://brian.discourse.group/t/time-dependend-input-in-equation-vs-parameter/753/1 "2022-07-26T07:15:29Z")

</div>

Hi!

This is probably an obvious problem but somehow I don’t see it.

I have two boolean variables `a` and `b`. From the code below I expect them to behave identically. But only `b` works as expected (turns true after 100 ms), `a` is stuck to false.

Can you please give me a hint?

Thanks,

Sebastian

```auto
#!/usr/bin/env python3

from brian2 import *

eqs = '''
a : boolean
b = int(t>100*ms): boolean
'''

neuron = NeuronGroup(1, eqs)

neuron.a = "int(t>100*ms)"

statemon = StateMonitor(neuron, ['a', 'b'], record=True)

run(200*ms)

plt.plot(statemon.t/ms, statemon.a[0], label="a")
plt.plot(statemon.t/ms, statemon.b[0], label="b")

plt.xlabel("t [ms]")
plt.ylabel("boolean")

plt.legend()

plt.show()

```

![grafik](https://global.discourse-cdn.com/free1/uploads/brian/original/1X/a22c9102f3361b1b60864651ad260c9771158bb6.png)

---

<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:** [26 July 2022 08:38 UTC](https://brian.discourse.group/t/time-dependend-input-in-equation-vs-parameter/753/2 "2022-07-26T08:38:03Z")

</div>

Hi @schmitts . When you use `neuron.a = ...` you perform an initialization at the beginning of the simulation. This evaluates to `False` (or rather 0 – you don’t need to put `int` around it when you use a boolean variable, though). It will not be executed during the simulation. Otherwise, writing something like `neuron.v = 'E_L + rand()*10*mV'` at the beginning of your simulation would screw up your results quite badly 😉  
If you want to update `a` continuously, you’d need to use

```python
neuron.run_regularly('a = t>100*ms')

```

Hope that clears things up.

---

<div class="post-metadata">

**Author:** ![schmitts](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/schmitts/32/191_2.png) [@schmitts](https://brian.discourse.group/u/schmitts)\
**Post date:** [26 July 2022 09:03 UTC](https://brian.discourse.group/t/time-dependend-input-in-equation-vs-parameter/753/3 "2022-07-26T09:03:33Z")

</div>

Hi @mstimberg!

Thanks. As a followup question. What do you suggest to use for time dependend stimulus that also depends on the compartment? For a NeuronGroup I would probably use a 2D `TimedArray`. But for a multi-compartment neuron I’m less sure about the correct indexing. I would prefer to express it more explicitely: `neuron.dendrite[5] = ...` to set up the stimulus, but as I’ve re-learned this would get evaluated too early.

```auto
morpho = Soma(diameter=10*um)
morpho.dendrite = Cylinder(length=400*um, diameter=4*um, n=10)

eqs = "... I : amp (point current) ..."

neuron = SpatialNeuron(morphology=morpho, model=eqs)

```

Thanks,

Sebastian

---

<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:** [26 July 2022 09:56 UTC](https://brian.discourse.group/t/time-dependend-input-in-equation-vs-parameter/753/4 "2022-07-26T09:56:09Z")

</div>

You can get the indices for compartments by using the `indices` attribute, e.g. with your morphology from above:

```nohighlight
>>> morpho.dendrite.indices[5]
6

```

This can also use fancier indexing, e.g.

```nohighlight
>>> morpho.dendrite.indices[100*um:300*um]
array([3, 4, 5, 6, 7, 8])

```

So if each compartment should have its own stimulus, then I’d use the index `i` and create the 2D `TimedArray` with the columns for each compartment in the correct way. But I guess you might instead have only a few different stimuli. I’d then put these into a `TimedArray` and for convenience also a “stimulus” that is 0 for all the compartments that shouldn’t receive any stimulus. Then, you’d add an integer variable to your model

```python
eqs = '''... stimulus_idx : integer (constant)'''

```

and refer to that index with `stimulus(t, stimulus_idx)`, with `stimulus` being the name of the `TimedArray`. You can then quite intuitively set the stimulus for certain compartments, e.g.:

```python
# Soma gets stimulus type 1 (note the .main to only refer to the soma):
neuron.main.stimulus_idx = 1
# Single dendrite compartment gets stimulus 2:
neuron.dendrite.stimulus_idx[5] = 2 
# All other compartments get stimulus 0

```

---

<div class="post-metadata">

**Author:** ![schmitts](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/schmitts/32/191_2.png) [@schmitts](https://brian.discourse.group/u/schmitts)\
**Post date:** [26 July 2022 12:15 UTC](https://brian.discourse.group/t/time-dependend-input-in-equation-vs-parameter/753/5 "2022-07-26T12:15:05Z")

</div>

Hi @mstimberg!

Thanks. I’m trying to follow the second way, but I’m getting a float division by zero.

Do you spot the mistake?

Thanks,

Sebastian

```auto
#!/usr/bin/env python3

from brian2 import *

morpho = Soma(diameter=10*um)
morpho.dendrite = Cylinder(length=400*um, diameter=4*um, n=1)

EL = -70*mV
gL = 1e-4*siemens/cm**2

eqs = '''
Im=gL * (EL - v) : amp/meter**2
I = stimulus(t, stimulus_index) : amp (point current)
stimulus_index : integer (constant)
'''

neuron = SpatialNeuron(morphology=morpho, model=eqs)

stimulus = TimedArray([[0, 0],
                       [0, 10],
                       [0, 0]]*uA, dt=100*ms)

neuron.main.stimulus_index = 0
neuron.dendrite.stimulus_index[0] = 1

run(1000*ms)

```

[brian\_debug\_e7q3sxsg.log](https://brian.discourse.group/uploads/short-url/vi8D6Us0q4LkKAH8rkBkgOFq3gK.log) (92.3 KB)  
[brian\_script\_uk0tjdrt.py](https://brian.discourse.group/uploads/short-url/lpCrLbnoacya0bR4PXkIu4aCHRn.py) (627 Bytes)  
[brian\_stderr\_x31rdf7r.log](https://brian.discourse.group/uploads/short-url/6J7J6FnP37JgMcja57XjFSuPFhQ.log) (668 Bytes)

---

<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:** [26 July 2022 14:57 UTC](https://brian.discourse.group/t/time-dependend-input-in-equation-vs-parameter/753/6 "2022-07-26T14:57:12Z")

</div>

Oh no ☹ The actual error is the compilation error, there seems to be an issue with `SpatialNeuron`, functions like `TimedArray` and Cython… The details are a bit tricky to explain, but I think I have a basic idea what went wrong. You should be able to use `prefs.codegen.target = 'numpy'` to use Python code generation or `set_device('cpp_standalone')` to use C++ standalone mode as a workaround for now.

---

<div class="post-metadata">

**Author:** ![schmitts](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/schmitts/32/191_2.png) [@schmitts](https://brian.discourse.group/u/schmitts)\
**Post date:** [26 July 2022 18:57 UTC](https://brian.discourse.group/t/time-dependend-input-in-equation-vs-parameter/753/7 "2022-07-26T18:57:09Z")

</div>

> [@mstimberg](#):
>
> prefs.codegen.target = ‘numpy’

Does the trick for now. Thanks!

---

<div class="post-metadata">

**Author:** ![schmitts](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/schmitts/32/191_2.png) [@schmitts](https://brian.discourse.group/u/schmitts)\
**Post date:** [27 July 2022 09:32 UTC](https://brian.discourse.group/t/time-dependend-input-in-equation-vs-parameter/753/8 "2022-07-27T09:32:06Z")

</div>

I’ve added an issue just for the record: [Compilation error for SpatialNeuron, TimedArray and Cython · Issue #1427 · brian-team/brian2 · GitHub](https://github.com/brian-team/brian2/issues/1427).

---

<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:** [27 July 2022 12:11 UTC](https://brian.discourse.group/t/time-dependend-input-in-equation-vs-parameter/753/9 "2022-07-27T12:11:22Z")

</div>

Also for the record: the issue is now fixed 😊

> <https://github.com/brian-team/brian2/pull/1428>
>
> Support code has to be included for \`before\_run\` blocks, otherwise variables nee…ded for functions that refer to namespaces (e.g. \`TimedArray\`) will not be correctly defined. This is a simple fix, in the longer run we should think about custom functions, namespaces etc. in \`before\_run\` block. Currently, these blocks do not actually need any of all this, since they don't run user-defined code (but potentially that might change in the future?).
> 
> Fixes #1427, see https://brian.discourse.group/t/time-dependend-input-in-equation-vs-parameter/753/5 for example code that failed previously.
