Equations class

(Shortest import: from brian2 import Equations)

class brian2.equations.equations.Equations(eqns, **kwds)[source]

Bases: _abcoll.Mapping

Container that stores equations from which models can be created.

String equations can be of any of the following forms:

  1. dx/dt = f : unit (flags) (differential equation)
  2. x = f : unit (flags) (equation)
  3. x : unit (flags) (parameter)

String equations can span several lines and contain Python-style comments starting with #

Parameters:

eqs : str or list of SingleEquation objects

A multiline string of equations (see above) – for internal purposes also a list of SingleEquation objects can be given. This is done for example when adding new equations to implement the refractory mechanism. Note that in this case the variable names are not checked to allow for “internal names”, starting with an underscore.

kwds: keyword arguments :

Keyword arguments can be used to replace variables in the equation string. Arguments have to be of the form varname=replacement, where varname has to correspond to a variable name in the given equation. The replacement can be either a string (replacing a name with a new name, e.g. tau='tau_e') or a value (replacing the variable name with the value, e.g. tau=tau_e or tau=10*ms).

Attributes

diff_eq_expressions A list of (variable name, expression) tuples of all differential equations.
diff_eq_names All differential equation names.
eq_expressions A list of (variable name, expression) tuples of all equations.
eq_names All equation names (including subexpressions).
identifier_checks A set of functions that are used to check identifiers (class attribute).
identifiers Set of all identifiers used in the equations, excluding the variables defined in the equations
is_stochastic Whether the equations are stochastic.
names All variable names defined in the equations.
ordered A list of all equations, sorted according to the order in which they should be updated
parameter_names All parameter names.
stochastic_type Returns the type of stochastic differential equations (additivive or multiplicative).
stochastic_variables
subexpr_names All subexpression names.
substituted_expressions Return a list of (varname, expr) tuples, containing all differential equations with all the subexpression variables substituted with the respective expressions.
units Dictionary of all internal variables and their corresponding units.

Methods

check_flags(allowed_flags) Check the list of flags.
check_identifier(identifier) Perform all the registered checks.
check_identifiers() Check all identifiers for conformity with the rules.
check_units(group[, run_namespace, level]) Check all the units for consistency.
register_identifier_check(func) Register a function for checking identifiers.

Details

diff_eq_expressions

A list of (variable name, expression) tuples of all differential equations.

diff_eq_names

All differential equation names.

eq_expressions

A list of (variable name, expression) tuples of all equations.

eq_names

All equation names (including subexpressions).

identifier_checks

A set of functions that are used to check identifiers (class attribute). Functions can be registered with the static method Equations.register_identifier_check and will be automatically used when checking identifiers

identifiers

Set of all identifiers used in the equations, excluding the variables defined in the equations

is_stochastic

Whether the equations are stochastic.

names

All variable names defined in the equations.

ordered

A list of all equations, sorted according to the order in which they should be updated

parameter_names

All parameter names.

stochastic_type

Returns the type of stochastic differential equations (additivive or multiplicative). The system is only classified as additive if all equations have only additive noise (or no noise).

Returns:

type : str

Either None (no noise variables), 'additive' (factors for all noise variables are independent of other state variables or time), 'multiplicative' (at least one of the noise factors depends on other state variables and/or time).

stochastic_variables
subexpr_names

All subexpression names.

substituted_expressions

Return a list of (varname, expr) tuples, containing all differential equations with all the subexpression variables substituted with the respective expressions.

Returns:

expr_tuples : list of (str, CodeString)

A list of (varname, expr) tuples, where expr is a CodeString object with all subexpression variables substituted with the respective expression.

units

Dictionary of all internal variables and their corresponding units.

check_flags(allowed_flags)[source]

Check the list of flags.

Parameters:

allowed_flags : dict

A dictionary mapping equation types (PARAMETER, DIFFERENTIAL_EQUATION, SUBEXPRESSION) to a list of strings (the allowed flags for that equation type)

Raises

ValueError
If any flags are used that are not allowed.

Notes

Not specifying allowed flags for an equation type is the same as specifying an empty list for it.

static check_identifier(identifier)[source]

Perform all the registered checks. Checks can be registered via Equations.register_identifier_check.

Parameters:

identifier : str

The identifier that should be checked

Raises

ValueError
If any of the registered checks fails.
check_identifiers()[source]

Check all identifiers for conformity with the rules.

Raises

ValueError
If an identifier does not conform to the rules.

See also

Equations.check_identifier
The function that is called for each identifier.
check_units(group, run_namespace=None, level=0)[source]

Check all the units for consistency.

Parameters:

group : Group

The group providing the context

run_namespace : dict, optional

A namespace provided to the Network.run() function.

level : int, optional

How much further to go up in the stack to find the calling frame

Raises

DimensionMismatchError
In case of any inconsistencies.
static register_identifier_check(func)[source]

Register a function for checking identifiers.

Parameters:

func : callable

The function has to receive a single argument, the name of the identifier to check, and raise a ValueError if the identifier violates any rule.

Tutorials and examples using this