How can I use system's own clock while setting input?

Description of problem

Hey everyone
I have some experience about coding and building networks but I am new in Brian and brianforum. I’m trying to create a model and I am wanting to use my old input formula because my input should change continuously over time within a certain rule but it seems like I cannot convert that into brianish :slight_smile:
There is a basic example of my code. The “t” in example should be the time that used in the operation of the system.
I would be very grateful if someone could help me with that. Thanks !

Minimal code to reproduce problem

def input_pulse():
    if t<td1 :
        return B0
    elif (t >= td1 and t < td2):
        if (t < (td1+tp)):
            return B1
        else:
            return B0
    elif (t>=td2 and t<(td2+tp)):
          return B2
    else:
        return B0

Hi @SirK , welcome to the Brian’s friends club :slight_smile:

So there are a several ways to implement something like that:

  1. You can use TimedArray if td1, td2, td1+tp, and td2+tp are constant and can be round to an integer of some dt. This is a very fast and convenient method to create a stimulus, but it something requires too much memory :wink:.
  2. If you have a few condition you can implement them as logic expression in the equation for your neurons (see here). Something like that B0 + (B1-B0)*int( (t>=td1)*(t<(td1+tp)) +(B2-B0)*int( (t>=td2)*(t<(td2+tp))). This is also pretty fast.
  3. Finally you can use user-provided function which usually slow if written in Python. So this isn’t a best option unless you code it up in Cython or C.

Hope this helps

2 Likes

Let me add one more option: you can use multiple run statements and change the stimulus strength in between. Somethin along the lines of:

neurons = NeuronGroup(..., '''dv/dt = ... + I ...''', ...)
I = B0
run(...)
I = B1
run(...)
I = B0
run(...)
I = B2
run(...)
I = B0
run(...)

PS: a minor formatting thing: try to put Python code between triple backticks for nicer display:

```
# Here comes the Python code
print('something')
```
1 Like

Thank you so much for your kind responds. It seems like TimedArray will solve my problem :+1:

2 Likes