brainmass.process_sequence

brainmass.process_sequence#

brainmass.process_sequence(data, mode='stack')[source]#

Aggregate a sequence of data items along the leading dimension.

The sequence is first stacked along a new leading axis and then reduced according to mode. The reduction is applied recursively over arbitrary PyTrees (dicts, tuples, lists, and nested combinations thereof).

Parameters:
  • data (PyTree) – A stacked PyTree of items, with the items enumerated along the leading axis of every leaf. All leaves must share a compatible structure.

  • mode (str | Callable) –

    How to reduce along the leading axis:

    • 'stack' : return the data unchanged (identity).

    • 'last' / 'first' : take the last/first item.

    • 'avg' / 'mean' : mean over the leading axis.

    • 'max' / 'min' : max/min over the leading axis.

    • callable : apply the callable to each leaf.

Returns:

Aggregated data whose structure matches the input leaves:

  • mode='stack' : structure with the new leading dimension retained.

  • mode='last' / 'first' : a single item.

  • mode='avg'/'mean'/'max'/'min' : reduced PyTree.

  • mode callable : the result of applying the callable.

Return type:

Array | ndarray | bool | number | bool | int | float | complex | Quantity | Tuple | List | Dict | Sequence

Raises:

ValueError – If mode is an unknown string.

Notes

  • All leaves must share the same type and structure.

  • Dictionary keys (and tuple/list lengths) must match across elements.

  • The reduction is recursive and handles nested structures, e.g. a dict of tuples of arrays.