Dynamics#

class brainpy.state.Dynamics(in_size, name=None)#
add_current_input(key, inp, label=None)[source]#

Add a current input function or array to the dynamics model.

Current inputs represent direct input currents that can be accessed during model updates through the sum_current_inputs() method.

Parameters:
  • key (str) – Unique identifier for this current input. Used to retrieve or reference the input later.

  • inp (Union[Callable, ArrayLike]) – The input data or function that generates input data. - If callable: Will be called during updates with arguments passed to sum_current_inputs() - If array-like: Will be applied once and then automatically removed from available inputs

  • label (Optional[str], default None) – Optional grouping label for the input. When provided, allows selective processing of inputs by label in sum_current_inputs().

Raises:

ValueError – If the key has already been used for a different current input.

Notes

  • Inputs with the same label can be processed together using the label parameter in sum_current_inputs().

  • Non-callable inputs are consumed when used (removed after first use).

  • Callable inputs persist and can be called repeatedly.

See also

sum_current_inputs

Sum all current inputs matching a given label

add_delta_input

Add a delta input function or array

add_delta_input(key, inp, label=None)[source]#

Add a delta input function or array to the dynamics model.

Delta inputs represent instantaneous changes to the model state (i.e., dX/dt contributions). This method registers a function or array that provides delta inputs which will be accessible during model updates through the sum_delta_inputs() method.

Parameters:
  • key (str) – Unique identifier for this delta input. Used to retrieve or reference the input later.

  • inp (Union[Callable, ArrayLike]) – The input data or function that generates input data. - If callable: Will be called during updates with arguments passed to sum_delta_inputs() - If array-like: Will be applied once and then automatically removed from available inputs

  • label (Optional[str], default None) – Optional grouping label for the input. When provided, allows selective processing of inputs by label in sum_delta_inputs().

Raises:

ValueError – If the key has already been used for a different delta input.

Notes

  • Inputs with the same label can be processed together using the label parameter in sum_delta_inputs().

  • Non-callable inputs are consumed when used (removed after first use).

  • Callable inputs persist and can be called repeatedly.

See also

sum_delta_inputs

Sum all delta inputs matching a given label

add_current_input

Add a current input function or array

align_pre(dyn)[source]#

Registers a dynamics module to execute after this module.

This method establishes a sequential execution relationship where the specified dynamics module will be called after this module completes its update. This creates a feed-forward connection in the computational graph.

Parameters:

dyn (Union[ParamDescriber[T], T]) – The dynamics module to be executed after this module. Can be either: - An instance of Dynamics - A ParamDescriber that can instantiate a Dynamics object

Returns:

The dynamics module that was registered, allowing for method chaining.

Return type:

T

Raises:

TypeError – If the input is not a Dynamics instance or a ParamDescriber that creates a Dynamics instance.

Examples

>>> import brainstate
>>> n1 = brainpy.state.LIF(10)
>>> n1.align_pre(brainpy.state.Expon.desc(n1.varshape))  # n2 will run after n1
property current_inputs#

Get the dictionary of current inputs registered with this dynamics model.

Current inputs represent direct input currents that flow into the model.

Returns:

A dictionary mapping keys to current input functions or values, or None if no current inputs have been registered.

Return type:

dict or None

See also

add_current_input

Register a new current input

sum_current_inputs

Apply and sum all current inputs

delta_inputs

Dictionary of instantaneous change inputs

property delta_inputs#

Get the dictionary of delta inputs registered with this dynamics model.

Delta inputs represent instantaneous changes to state variables (dX/dt).

Returns:

A dictionary mapping keys to delta input functions or values, or None if no delta inputs have been registered.

Return type:

dict or None

See also

add_delta_input

Register a new delta input

sum_delta_inputs

Apply and sum all delta inputs

current_inputs

Dictionary of direct current inputs

get_input(key)[source]#

Get a registered input function by its key.

Retrieves either a current input or a delta input function that was previously registered with the given key. This method checks both current_inputs and delta_inputs dictionaries for the specified key.

Parameters:

key (str) – The unique identifier used when the input function was registered.

Returns:

The input function or array associated with the given key.

Return type:

Callable or ArrayLike

Raises:

ValueError – If no input function is found with the specified key in either current_inputs or delta_inputs.

See also

add_current_input

Register a current input function

add_delta_input

Register a delta input function

Examples

>>> model = Dynamics(10)
>>> model.add_current_input('stimulus', lambda t: np.sin(t))
>>> input_func = model.get_input('stimulus')
>>> input_func(0.5)  # Returns sin(0.5)
sum_current_inputs(init, *args, label=None, pop=True, **kwargs)[source]#

Summarize all current inputs by applying and summing all registered current input functions.

This method iterates through all registered current input functions (from .current_inputs) and applies them to calculate the total input current for the dynamics model. It adds all results to the initial value provided.

Parameters:
  • init (Any) – The initial value to which all current inputs will be added.

  • *args – Variable length argument list passed to each current input function.

  • label (Optional[str], default None) – If provided, only process current inputs with this label prefix. When None, process all current inputs regardless of label.

  • **kwargs – Arbitrary keyword arguments passed to each current input function.

Returns:

The initial value plus all applicable current inputs summed together.

Return type:

Any

Notes

  • Non-callable current inputs are applied once and then automatically removed from the current_inputs dictionary.

  • Callable current inputs remain registered for subsequent calls.

  • When a label is provided, only current inputs with keys starting with that label are applied.

sum_delta_inputs(init, *args, label=None, pop=True, **kwargs)[source]#

Summarize all delta inputs by applying and summing all registered delta input functions.

This method iterates through all registered delta input functions (from .delta_inputs) and applies them to calculate instantaneous changes to model states. It adds all results to the initial value provided.

Parameters:
  • init (Any) – The initial value to which all delta inputs will be added.

  • *args – Variable length argument list passed to each delta input function.

  • label (Optional[str], default None) – If provided, only process delta inputs with this label prefix. When None, process all delta inputs regardless of label.

  • **kwargs – Arbitrary keyword arguments passed to each delta input function.

Returns:

The initial value plus all applicable delta inputs summed together.

Return type:

Any

Notes

  • Non-callable delta inputs are applied once and then automatically removed from the delta_inputs dictionary.

  • Callable delta inputs remain registered for subsequent calls.

  • When a label is provided, only delta inputs with keys starting with that label are applied.