FoldReducer

class FoldReducer(step_time: float, duration: float, inclusive: bool = False, inplace: bool = False, fill: Any = 0)[source]

Bases: RecordReducer, ABC

Subclassable reducer performing a fold operation between previous state and an observation.

Parameters:
  • step_time (float) – length of time between observations.

  • duration (float) – length of time for which observations should be stored.

  • inclusive (bool, optional) – if the duration should be inclusive. Defaults to False.

  • inplace (bool, optional) – if write operations should be performed in-place. Defaults to False.

  • fill (Any, optional) – value with which to fill the stored record on clearing and initialization. Defaults to 0.

clear(keepshape=False, **kwargs) None[source]

Reinitializes the reducer’s state.

Parameters:

keepshape (bool, optional) – if the underlying storage shape should be preserved. Defaults to False.

property data: Tensor

Length of the simulation time step, in milliseconds.

The shape must be equivalent to the original, this allows for calls to be made to methods such as to().

Parameters:

value (torch.Tensor) – new data storage tensor.

Returns:

data storage tensor.

Return type:

torch.Tensor

Important

The order of the data tensor is not equivalent to the historical order. Use dump() for this.

dump(**kwargs) Tensor | None[source]

Returns the reducer’s state over all observations.

Returns:

state over all observations, if state exists.

Return type:

torch.Tensor | None

Note

Before any samples have been added and before the data tensor has been initialized, this will return None.

Note

Results are temporally ordered from most recent to oldest, along the first dimension.

abstract fold(*args: Tensor | None) Tensor[source]

Calculation of the next state given an observation and prior state.

Parameters:

*args (torch.Tensor | None) – positional arguments for folding, all but the last will be observations, the final will be the reduced state.

Raises:

NotImplementedError – abstract methods must be implemented by subclass.

Returns:

state for the current time step.

Return type:

torch.Tensor

forward(*inputs: Tensor, **kwargs) None[source]

Initializes state and incorporates inputs into the reducer’s state.

This performs any required initialization steps, maps the inputs, and pushes the new data.

Parameters:

*inputs (torch.Tensor) – inputs to be mapped, then pushed.

abstract interpolate(prev_data: Tensor, next_data: Tensor, sample_at: Tensor, step_time: float | Tensor) Tensor[source]

Manner of sampling state between observations.

Parameters:
  • prev_data (torch.Tensor) – most recent observation prior to sample time.

  • next_data (torch.Tensor) – most recent observation subsequent to sample time.

  • sample_at (torch.Tensor) – relative time at which to sample data.

  • step_time (float | torch.Tensor) – length of time between the prior and subsequent observations.

Raises:

NotImplementedError – abstract methods must be implemented by subclass.

Returns:

interpolated data at sample time.

Return type:

torch.Tensor

peek(**kwargs) Tensor | None[source]

Returns the reducer’s current state.

Before any samples have been added and before the data tensor has been initialized, this will return None.

Returns:

current state, if state exists.

Return type:

torch.Tensor | None

push(inputs: Tensor, **kwargs) None[source]

Incorporates inputs into the reducer’s state.

Parameters:

inputs (torch.Tensor) – new observation to incorporate into state.

view(time: float | Tensor, tolerance: float = 1e-07) Tensor | None[source]

Returns the reducer’s state at a given time.

Before any samples have been added and before the data tensor has been initialized, this will return None. This will fail if any values in time fall outside of the possible range.

Parameters:
  • time (float | torch.Tensor) – times, measured before present, at which to select from.

  • tolerance (float, optional) – maximum difference in time from a discrete sample to consider it at the same time as that sample. Defaults to 1e-7.

Returns:

temporally indexed and interpolated state.

Return type:

torch.Tensor | None

Shape

time:

\(S_0 \times \cdots \times [D]\)

return:

\(S_0 \times \cdots \times [D]\)

Where:
  • \(S_0, \ldots\) are the dimensions of each observation, given by the shape of the data.

  • \(D\) are the number of distinct observations to select.