Time dependend input in equation vs. parameter


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?



#!/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)


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

plt.xlabel("t [ms]")




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 :wink:
If you want to update a continuously, you’d need to use

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

Hope that clears things up.

1 Like

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.

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)



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

>>> morpho.dendrite.indices[5]

This can also use fancier indexing, e.g.

>>> 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

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.:

# 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

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?



#!/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


brian_debug_e7q3sxsg.log (92.3 KB)
brian_script_uk0tjdrt.py (627 Bytes)
brian_stderr_x31rdf7r.log (668 Bytes)

Oh no :frowning: 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.

1 Like

Does the trick for now. Thanks!

I’ve added an issue just for the record: Compilation error for SpatialNeuron, TimedArray and Cython · Issue #1427 · brian-team/brian2 · GitHub.

1 Like

Also for the record: the issue is now fixed :blush:

1 Like