is_boolean_expression function

(Shortest import: from brian2.parsing.expressions import is_boolean_expression)

brian2.parsing.expressions.is_boolean_expression(expr, variables)[source]

Determines if an expression is of boolean type or not

Parameters:

expr : str

The expression to test

variables : dict-like of Variable

The variables used in the expression.

Returns:

isbool : bool

Whether or not the expression is boolean.

Raises

SyntaxError
If the expression ought to be boolean but is not, for example x<y and z where z is not a boolean variable.

Notes

We test the following cases recursively on the abstract syntax tree:

  • The node is a boolean operation. If all the subnodes are boolean expressions we return True, otherwise we raise the SyntaxError.
  • The node is a function call, we return True or False depending on whether the function description has the _returns_bool attribute set.
  • The node is a variable name, we return True or False depending on whether is_boolean attribute is set or if the name is True or False.
  • The node is a comparison, we return True.
  • The node is a unary operation, we return True if the operation is not, otherwise False.
  • Otherwise we return False.