VariableView class

(Shortest import: from brian2.core.variables import VariableView)

class brian2.core.variables.VariableView(name, variable, group, dimensions=None)[source]

Bases: object

A view on a variable that allows to treat it as an numpy array while allowing special indexing (e.g. with strings) in the context of a Group.

Parameters

name : str

The name of the variable (not necessarily the same as variable.name).

variable : Variable

The variable description.

group : Group

The group through which the variable is accessed (not necessarily the same as variable.owner).

dimensions : Dimension, optional

The physical dimensions to be used for the variable, should be None when a variable is accessed without units (e.g. when accessing G.var_).

Attributes

dtype

shape

unit

The Unit of this variable

Methods

get_item(item[, level, namespace])

Get the value of this variable.

get_subexpression_with_index_array(item, ...)

get_with_expression(code, run_namespace)

Gets a variable using a string expression.

get_with_index_array(item)

set_item(item, value[, level, namespace])

Set this variable.

set_with_expression(item, code, run_namespace)

Sets a variable using a string expression.

set_with_expression_conditional(cond, code, ...)

Sets a variable using a string expression and string condition.

set_with_index_array(item, value, check_units)

Details

dtype
shape
unit

The Unit of this variable

get_item(item, level=0, namespace=None)[source]

Get the value of this variable. Called by __getitem__.

Parameters

item : slice, ndarray or string

The index for the setting operation

level : int, optional

How much farther to go up in the stack to find the implicit namespace (if used, see run_namespace).

namespace : dict-like, optional

An additional namespace that is used for variable lookup (if not defined, the implicit namespace of local variables is used).

get_subexpression_with_index_array(item, run_namespace)[source]
get_with_expression(code, run_namespace)[source]

Gets a variable using a string expression. Is called by VariableView.get_item for statements such as print(G.v['g_syn > 0']).

Parameters

code : str

An expression that states a condition for elements that should be selected. Can contain references to indices, such as i or j and to state variables. For example: 'i>3 and v>0*mV'.

run_namespace : dict-like

An additional namespace that is used for variable lookup (either an explicitly defined namespace or one taken from the local context).

get_with_index_array(item)[source]
set_item(item, value, level=0, namespace=None)[source]

Set this variable. This function is called by __setitem__ but there is also a situation where it should be called directly: if the context for string-based expressions is higher up in the stack, this function allows to set the level argument accordingly.

Parameters

item : slice, ndarray or string

The index for the setting operation

value : Quantity, ndarray or number

The value for the setting operation

level : int, optional

How much farther to go up in the stack to find the implicit namespace (if used, see run_namespace).

namespace : dict-like, optional

An additional namespace that is used for variable lookup (if not defined, the implicit namespace of local variables is used).

set_with_expression(item, code, run_namespace, check_units=True)[source]

Sets a variable using a string expression. Is called by VariableView.set_item for statements such as S.var[:, :] = 'exp(-abs(i-j)/space_constant)*nS'

Parameters

item : ndarray

The indices for the variable (in the context of this group).

code : str

The code that should be executed to set the variable values. Can contain references to indices, such as i or j

run_namespace : dict-like, optional

An additional namespace that is used for variable lookup (if not defined, the implicit namespace of local variables is used).

check_units : bool, optional

Whether to check the units of the expression.

run_namespace : dict-like, optional

An additional namespace that is used for variable lookup (if not defined, the implicit namespace of local variables is used).

set_with_expression_conditional(cond, code, run_namespace, check_units=True)[source]

Sets a variable using a string expression and string condition. Is called by VariableView.set_item for statements such as S.var['i!=j'] = 'exp(-abs(i-j)/space_constant)*nS'

Parameters

cond : str

The string condition for which the variables should be set.

code : str

The code that should be executed to set the variable values.

run_namespace : dict-like, optional

An additional namespace that is used for variable lookup (if not defined, the implicit namespace of local variables is used).

check_units : bool, optional

Whether to check the units of the expression.

set_with_index_array(item, value, check_units)[source]