VariableOwner class

(Shortest import: from brian2 import VariableOwner)

class brian2.groups.group.VariableOwner(*args, **kw)[source]

Bases: brian2.core.names.Nameable

Mix-in class for accessing arrays by attribute.

# TODO: Overwrite the __dir__ method to return the state variables # (should make autocompletion work)

Methods

add_attribute(name)

Add a new attribute to this group.

check_variable_write(variable)

Function that can be overwritten to raise an error if writing to a variable should not be allowed.

get_states([vars, units, format, ...])

Return a copy of the current state variable values.

set_states(values[, units, format, level])

Set the state variables.

state(name[, use_units, level])

Return the state variable in a way that properly supports indexing in the context of this group

Details

add_attribute(name)[source]

Add a new attribute to this group. Using this method instead of simply assigning to the new attribute name is necessary because Brian will raise an error in that case, to avoid bugs passing unnoticed (misspelled state variable name, un-declared state variable, …).

Parameters

name : str

The name of the new attribute

Raises

AttributeError

If the name already exists as an attribute or a state variable.

check_variable_write(variable)[source]

Function that can be overwritten to raise an error if writing to a variable should not be allowed. Note that this does not deal with incorrect writes that are general to all kind of variables (incorrect units, writing to a read-only variable, etc.). This function is only used for type-specific rules, e.g. for raising an error in Synapses when writing to a synaptic variable before any connect call.

By default this function does nothing.

Parameters

variable : Variable

The variable that the user attempts to set.

get_states(vars=None, units=True, format='dict', subexpressions=False, read_only_variables=True, level=0)[source]

Return a copy of the current state variable values. The returned arrays are copies of the actual arrays that store the state variable values, therefore changing the values in the returned dictionary will not affect the state variables.

Parameters

vars : list of str, optional

The names of the variables to extract. If not specified, extract all state variables (except for internal variables, i.e. names that start with '_'). If the subexpressions argument is True, the current values of all subexpressions are returned as well.

units : bool, optional

Whether to include the physical units in the return value. Defaults to True.

format : str, optional

The output format. Defaults to 'dict'.

subexpressions: bool, optional :

Whether to return subexpressions when no list of variable names is given. Defaults to False. This argument is ignored if an explicit list of variable names is given in vars.

read_only_variables : bool, optional

Whether to return read-only variables (e.g. the number of neurons, the time, etc.). Setting it to False will assure that the returned state can later be used with set_states. Defaults to True.

level : int, optional

How much higher to go up the stack to resolve external variables. Only relevant if extracting subexpressions that refer to external variables.

Returns

values : dict or specified format

The variables specified in vars, in the specified format.

set_states(values, units=True, format='dict', level=0)[source]

Set the state variables.

Parameters

values : depends on format

The values according to format.

units : bool, optional

Whether the values include physical units. Defaults to True.

format : str, optional

The format of values. Defaults to 'dict'

level : int, optional

How much higher to go up the stack to resolve external variables. Only relevant when using string expressions to set values.

state(name, use_units=True, level=0)[source]

Return the state variable in a way that properly supports indexing in the context of this group

Parameters

name : str

The name of the state variable

use_units : bool, optional

Whether to use the state variable’s unit.

level : int, optional

How much farther to go down in the stack to find the namespace.

Returns :

——- :

var : VariableView or scalar value

The state variable’s value that can be indexed (for non-scalar values).