VariableStack#

class brainpy.math.VariableStack(*args, **kwargs)[source]#

Variable stack, for collecting all Variable used in the program.

VariableStack supports all features of python dict.

add(var)[source]#

Add a new Variable.

assign(data, check=True)[source]#

Assign the value for each \(~.Variable\) according to the given data.

Parameters:
  • data (Union[Dict, Sequence]) – dict, list, tuple. The data of all variables

  • check (bool) – bool. Check whether the shape and type of the given data are consistent with original data.

assign_org_values()[source]#

Assign the original value for each variable.

call_on_subset(cond, call)[source]#

Call a function on the subset of this VariableStack.

>>> import brainpy.math as bm
>>> stack = VariableStack(a=bm.Variable(1), b=bm.random.RandomState(1))
>>> stack.call_on_subset(lambda a: isinstance(a, bm.random.RandomState),
>>>                      lambda a: a.split_key())
{'b': Array([3819641963, 2025898573], dtype=uint32)}
Parameters:
  • cond (Callable) – The function to determine whether the element belongs to the wanted subset.

  • call (Callable) – The function to call if the element belongs to the wanted subset.

Return type:

dict

Returns:

A dict containing the results of call function for each element in the cond constrained subset.

collect_values()[source]#

Collect the value of each variable once again.

dict_data()[source]#

Get all data in the collected variables with a python dict structure.

Return type:

dict

dict_data_of_subset(subset_cond)[source]#

Get data of the given subset constrained by function subset_cond.

Parameters:

subset_cond (Callable) – A function to determine whether the element is in the subset wanted.

Return type:

dict

Returns:

A dict of data for elements of the wanted subset.

instance_of(cls)#

Collect all variables which are instances of the given class type.

Return type:

VariableStack

list_data()[source]#

Get all data in the collected variables with a python list structure.

Return type:

list

not_instance_of(cls)#

Collect all variables which are not instance of the given class type.

Return type:

VariableStack

remove_by_id(*ids, error_when_absent=False)[source]#

Remove or pop variables in the stack by the given ids.

remove_var_by_id(*ids, error_when_absent=False)#

Remove or pop variables in the stack by the given ids.

separate_by_instance(cls)[source]#

Separate all variables into two groups: (variables that are instances of the given cls, variables that are not instances of the given cls).

>>> import brainpy.math as bm
>>> stack = VariableStack(a=bm.Variable(1), b=bm.random.RandomState(1))
>>> stack.separate_by_instance(bm.random.RandomState)
({'b': RandomState(key=([0, 1], dtype=uint32))},
 {'a': Variable(value=Array([0.]), dtype=float32)})
>>> stack.separate_by_instance(bm.Variable)
({'a': Variable(value=Array([0.]), dtype=float32),
  'b': RandomState(key=([0, 1], dtype=uint32))},
 {})
Parameters:

cls (type) – The class type.

Returns:

  • VariableStack of variables that are instances of the given cls

  • VariableStack of variables that are not instances of the given cls

Return type:

A tuple with two elements

subset_by_instance(cls)[source]#

Collect all variables which are instances of the given class type.

Return type:

VariableStack

subset_by_not_instance(cls)[source]#

Collect all variables which are not instance of the given class type.

Return type:

VariableStack