There is a problem of sympy in the use of brian2

Traceback (most recent call last):
  File "C:\Users\San\Desktop\src\", line 178, in <module>
    connections['Xe1Ae1'] = Synapses(input_groups['Xe1'], neuron_groups['Ae1'],method="euler",
  File "D:\anaconda3\lib\site-packages\brian2\synapses\", line 735, in __init__
    model = Equations(model)
  File "D:\anaconda3\lib\site-packages\brian2\equations\", line 554, in __init__
    self._equations = parse_string_equations(eqns)
  File "D:\anaconda3\lib\site-packages\brian2\utils\", line 101, in cached_func
    func._cache[cache_key] = func(*args, **kwds)
  File "D:\anaconda3\lib\site-packages\brian2\equations\", line 366, in parse_string_equations
    expression = Expression(p.sub(' ', expression))
  File "D:\anaconda3\lib\site-packages\brian2\equations\", line 107, in __init__
  File "D:\anaconda3\lib\site-packages\brian2\parsing\", line 76, in str_to_sympy
    return _str_to_sympy(expr)
  File "D:\anaconda3\lib\site-packages\brian2\utils\", line 101, in cached_func
    func._cache[cache_key] = func(*args, **kwds)
  File "D:\anaconda3\lib\site-packages\brian2\parsing\", line 84, in _str_to_sympy
    raise SyntaxError(f"Error during evaluation of sympy expression "
SyntaxError: Error during evaluation of sympy expression '-(0.00035 * unblock_mg_function(voltage) * on + 0.00095 * ov)*(voltage -140) * (ca < 1)/ms - ca / t_ca ': Relational cannot be used in Mul

Hi @Erhong,
from the error message, I think the problem is that your equation multiplies with (ca < 1). You probably want this to multiply with 0 or 1 depending on the value of ca, but this is not “mathematically correct”. Confusingly, this will often work the way you wrote it, since in the generated code it will be interpreted correctly. But as you can see in this example, we use sympy to deal with the equations mathematically, and during these transformations this formulation can lead to issues. Instead, write it as int(ca < 1) to explicitly tell Brian/sympy to convert the boolean value into a number.

Hope that works,

Thanks very much for your prompt reply. By changing it into int(ca<1), I have solved this problem.

Kind regards,

Erhong Long

1 Like