braintools.cogtask.identity

Contents

braintools.cogtask.identity#

braintools.cogtask.identity(key, default=None)[source]#

Create an identity encoding that passes through values directly.

Useful when the context already contains properly formatted arrays that should be used directly without transformation.

Parameters:
  • key (str) – Context key containing the array value.

  • default (Array | None) – Default value if key not found. If None, returns zeros.

Returns:

Encoder function (ctx, feature) -> np.ndarray

Return type:

Callable[[Context, Feature], Array]

Examples

>>> import numpy as np
>>> from braintools.cogtask import Context, Feature, identity
>>> ctx = Context()
>>> # The context holds a pre-computed encoding
>>> ctx['custom_encoding'] = np.array([0.1, 0.5, 0.8, 0.3])
>>> identity('custom_encoding')(ctx, Feature(4, 'stimulus'))
Array([0.1, 0.5, 0.8, 0.3], dtype=float32)
>>> identity('missing')(ctx, Feature(2, 'stimulus'))  # absent key: zeros
Array([0., 0.], dtype=float32)