Dynamics#

class brainstate.nn.Dynamics(in_size, name=None)#

Base class for implementing neural dynamics models in BrainState.

Dynamics classes represent the core computational units in neural simulations, implementing the differential equations or update rules that govern neural activity. This class provides infrastructure for managing neural populations, handling inputs, and coordinating updates within the simulation framework.

The Dynamics class serves several key purposes: 1. Managing neuron population geometry and size information 2. Handling current and delta (instantaneous change) inputs to neurons 3. Supporting before/after update hooks for computational dependencies 4. Providing access to delayed state variables through the prefetch mechanism 5. Establishing the execution order in neural network simulations

Parameters:
  • in_size (int | Sequence[int] | integer | Sequence[integer]) – The geometry of the neuron population. Can be an integer (e.g., 10) for 1D neuron arrays, or a tuple (e.g., (10, 10)) for multi-dimensional populations.

  • name (str | None) – Optional name identifier for this dynamics module.

in_size#

The shape/geometry of the neuron population.

Type:

tuple

out_size#

The output shape, typically matches in_size.

Type:

tuple

current_inputs#

Dictionary of registered current input functions or arrays.

Type:

Optional[Dict[str, Union[Callable, ArrayLike]]]

delta_inputs#

Dictionary of registered delta input functions or arrays.

Type:

Optional[Dict[str, Union[Callable, ArrayLike]]]

before_updates#

Dictionary of functions to call before the main update.

Type:

Optional[Dict[Hashable, Callable]]

after_updates#

Dictionary of functions to call after the main update.

Type:

Optional[Dict[Hashable, Callable]]

Notes

In the BrainState execution sequence, Dynamics modules are updated after Projection modules and before other module types, reflecting the natural flow of information in neural systems.

There are several essential attributes:

  • size: the geometry of the neuron group. For example, (10, ) denotes a line of neurons, (10, 10) denotes a neuron group aligned in a 2D space, (10, 15, 4) denotes a 3-dimensional neuron group.

  • num: the flattened number of neurons in the group. For example, size=(10, ) => num=10, size=(10, 10) => num=100, size=(10, 15, 4) => num=600.

See also

Module

Parent class providing base module functionality

Projection

Class for handling synaptic projections between neural populations

DynamicsGroup

Container for organizing multiple dynamics modules

add_after_update(key, fun)[source]#

Register a function to be executed after the module’s update.

Parameters:
  • key (Any) – A unique identifier for the update function.

  • fun (Callable) – The function to execute after the module’s update.

Raises:

KeyError – If the key is already registered in after_updates.

Notes

Internal method used by the module system to register dependencies.

add_before_update(key, fun)[source]#

Register a function to be executed before the module’s update.

Parameters:
  • key (Any) – A unique identifier for the update function.

  • fun (Callable) – The function to execute before the module’s update.

Raises:

KeyError – If the key is already registered in before_updates.

Notes

Internal method used by the module system to register dependencies.

property after_updates#

Get the dictionary of functions to execute after the module’s update.

Returns:

Dictionary mapping keys to callable functions that will be executed after the main update, or None if no after updates are registered.

Return type:

dict or None

Notes

After updates are executed in the order they were registered whenever the module is called via __call__, and may optionally receive the return value from the update method.

property before_updates#

Get the dictionary of functions to execute before the module’s update.

Returns:

Dictionary mapping keys to callable functions that will be executed before the main update, or None if no before updates are registered.

Return type:

dict or None

Notes

Before updates are executed in the order they were registered whenever the module is called via __call__.

get_after_update(key)[source]#

Retrieve a registered after-update function by its key.

Parameters:

key (Any) – The identifier of the after-update function to retrieve.

Returns:

The registered after-update function.

Return type:

Callable

Raises:

KeyError – If the key is not registered in after_updates or if after_updates is None.

get_before_update(key)[source]#

Retrieve a registered before-update function by its key.

Parameters:

key (Any) – The identifier of the before-update function to retrieve.

Returns:

The registered before-update function.

Return type:

Callable

Raises:

KeyError – If the key is not registered in before_updates or if before_updates is None.

has_after_update(key)[source]#

Check if an after-update function is registered with the given key.

Parameters:

key (Any) – The identifier to check for in the after_updates dictionary.

Returns:

True if the key is registered in after_updates, False otherwise.

Return type:

bool

has_before_update(key)[source]#

Check if a before-update function is registered with the given key.

Parameters:

key (Any) – The identifier to check for in the before_updates dictionary.

Returns:

True if the key is registered in before_updates, False otherwise.

Return type:

bool

output_delay(*time_and_index, init=None, interpolation=None, **kwargs)[source]#

Create a reference to the delayed output of the module.

This method simplifies the process of accessing a delayed version of the module’s output. It instantiates an OutputDelayAt object, which can be used to retrieve the output value at the specified delay time.

Parameters:
  • time_and_index – The amount of time to delay the output access, typically in time units (e.g., milliseconds). Defaults to None.

  • init (Callable | Array | ndarray | bool | number | bool | int | float | complex | Quantity | None) – Delay initialization function or value. If the delayed output is not yet available, this function or value will be used to provide a default.

  • interpolation (str | None) – The interpolation method to use.

Returns:

An object that provides access to the module’s output at the specified delay time.

Return type:

OutputDelayAt

prefetch(item)[source]#

Create a reference to a state or variable that may not be initialized yet.

This method allows accessing module attributes or states before they are fully defined, acting as a placeholder that will be resolved when called. Particularly useful for creating references to variables that will be defined during initialization or runtime.

Parameters:

item (str) – The name of the attribute or state to reference.

Returns:

A Prefetch object that provides access to the referenced item.

Return type:

Prefetch

Examples

>>> import brainstate
>>> import brainunit as u
>>> neuron = brainpy.state.LIF(...)
>>> v_ref = neuron.prefetch('V')  # Reference to voltage
>>> v_value = v_ref()  # Get current value
>>> delayed_v = v_ref.delay.at(5.0 * u.ms)  # Get delayed value
prefetch_delay(state, *time_and_index, init=None, interpolation=None, **kwargs)[source]#

Create a reference to a delayed state or variable in the module.

This method simplifies the process of accessing a delayed version of a state or variable within the module. It first creates a prefetch reference to the specified state, then specifies the delay time for accessing this state.

Parameters:
  • state (str) – The name of the state or variable to reference.

  • time_and_index – The amount of time to delay the variable access, typically in time units (e.g., milliseconds).

  • init (Callable | Array | ndarray | bool | number | bool | int | float | complex | Quantity | None) – An optional initialization function to provide a default value if the delayed state is not yet available.

  • interpolation (str | None) – The interpolation method to use.

Returns:

An object that provides access to the variable at the specified delay time.

Return type:

PrefetchDelayAt

property varshape#

Get the shape of variables in the neuron group.

This property provides access to the geometry (shape) of the neuron population, which determines how variables and states are structured.

Returns:

A tuple representing the dimensional shape of the neuron group, matching the in_size parameter provided during initialization.

Return type:

tuple

See also

in_size

The input geometry specification for the neuron group