brian2 icon indicating copy to clipboard operation
brian2 copied to clipboard

Macro definitions in equations

Open thesamovar opened this issue 10 years ago • 5 comments

e.g. define E(vT)=exp((E-v+vT)/tau) and then use E(15*mV), E(40*mV) in the rest of the equations.

thesamovar avatar Dec 11 '14 15:12 thesamovar

@thesamovar I am interested in working on the issue. How do I start?

souravsingh avatar Nov 24 '16 07:11 souravsingh

Thanks for offering to be involved!

For something like this, we normally start with a discussion of use cases, then proposals for the syntax, and then finally move on to implementation. So I'd start by coming up with some suggestions as to how this feature might be used, and ideas for what the user syntax might look like.

thesamovar avatar Nov 24 '16 14:11 thesamovar

Hi! I was hoping to continue with this. Use cases would include all the mathematical expressions and differential equations. (In differential equations this would allow us to change values of constants, like the threshold without changing the entire equation). Coming to the syntax, If the equation is f(x,y) = x+y I think we should allow syntax like: f(3,2) and f(x=2,y=4) Some guidance with this would be great! Thanks!

Dyex719 avatar Jan 01 '18 15:01 Dyex719

I think the best thing for now would be to come up with a concrete use case, and show how the new syntax would make things clearer. For example, take the equations from one of the Brian examples and show how a macro definition would make them clearer/more readable.

Note that there is potential for confusion what this issue is about: this is not about having a system just to change constants in equations (we already have several mechanisms for that, see e.g. http://brian2.readthedocs.io/en/stable/user/equations.html#equation-objects and http://brian2.readthedocs.io/en/stable/advanced/namespaces.html). It is rather about shortening/cleaning up equation strings by having a macro for expressions that occur repeatedly (with small variations such as changed constants) across several equations of an equations string.

Oh, and I edited Dan's example in the initial post, it used "x" instead of "vT" by mistake.

mstimberg avatar Jan 04 '18 18:01 mstimberg

Hello!

taking into account rate constants equations from 'COBAHH' example https://brian2.readthedocs.io/en/stable/examples/COBAHH.html:

I am not sure it is small enough changes between three repeated equations, but may be it is useful to use macro in situations with 'exprel' function.

before:

beta_m = 0.28*(mV**-1)*5*mV/exprel((v-VT-40*mV)/(5*mV))/ms : Hz
alpha_h = 0.128*exp((17*mV-v+VT)/(18*mV))/ms : Hz
beta_h = 4./(1+exp((40*mV-v+VT)/(5*mV)))/ms : Hz
alpha_n = 0.032*(mV**-1)*5*mV/exprel((15*mV-v+VT)/(5*mV))/ms : Hz
beta_n = .5*exp((10*mV-v+VT)/(40*mV))/ms : Hz

after:

R(Vnum, Vdenum) = Vdenum/exprel(Vnum/Vdenum)  # R for rate constant
E(Vsh, Vdenum) = exp((Vsh-v+VT)/Vdenum)
alpha_m = 0.32*(mV**-1)*R(13*mV-v+VT, 4*mV)/ms : Hz
beta_m = 0.28*(mV**-1)*R(v-VT-40*mV, 5*mV)/ms : Hz
alpha_h = 0.128*E(17*mV, 18*mV)/ms : Hz
beta_h = 4./(1+E(40*mV, 5*mV))/ms : Hz
alpha_n = 0.032*(mV**-1)*R(15*mV-v+VT, 5*mV)/ms : Hz
beta_n = .5*E(10*mV, 40*mV)/ms : Hz

Also possible usage with gate dynamics:

before:

dm/dt = alpha_m*(1-m)-beta_m*m : 1
dn/dt = alpha_n*(1-n)-beta_n*n : 1
dh/dt = alpha_h*(1-h)-beta_h*h : 1

after:

G((alpha, beta, x) = alpha*(1-x)-beta*(x)
dm/dt = G(alpha_m, beta_m, m) : 1
dn/dt = G(alpha_n, beta_n, n) : 1
dh/dt = G(alpha_h, beta_h, h) : 1

Hope I understood properly.

upd:

Shorter but more tricky with parameters version of R:

R(Vsh, Vsc) = Vsc/exprel((Vsh-v+VT)/Vsc)
alpha_m = 0.32*(mV**-1)*R(13*mV, 4*mV)/ms : Hz
beta_m = 0.28*(mV**-1)*R(40*mV, -5*mV)/ms : Hz
alpha_n = 0.032*(mV**-1)*R(15*mV, 5*mV)/ms : Hz

shcecter avatar Apr 27 '20 10:04 shcecter