nvalchemi.data.transforms.Compose#

class nvalchemi.data.transforms.Compose(transforms)[source]#

Compose a sequence of transforms into a single callable.

Transforms are applied left-to-right: the output of transform i becomes the input to transform i + 1. An empty sequence acts as the identity.

A Compose is either a sample composition or a batch composition, inferred at construction from the type annotations of the supplied callables. Each typed callable must declare its first positional parameter as AtomicData (sample transform) or Batch (batch transform); untyped callables are accepted and take on the kind established by the typed ones. Mixing sample and batch transforms in the same Compose raises TypeError. A composition with no typed callables defaults to sample semantics.

Parameters:

transforms (Sequence[SampleTransform | BatchTransform]) – Transforms to compose in application order. Any iterable is accepted and eagerly materialized into an immutable tuple. Each entry must be callable.

transforms#

The composed transforms, stored in application order.

Type:

tuple[SampleTransform, …] | tuple[BatchTransform, …]

is_batch#

True if this is a batch composition, False if it is a sample composition. False for an empty / all-untyped composition (defaults to sample semantics).

Type:

bool

Raises:
  • TypeError – If any entry is not callable, if a typed first parameter is neither AtomicData nor Batch, or if typed callables disagree on the composition kind.

  • RuntimeError – (At call time) If any transform raises; the original exception is wrapped in a RuntimeError identifying the failing index, with the original exception attached via __cause__.

Parameters:

transforms (Sequence[SampleTransform | BatchTransform])

Examples

Per-sample composition:

>>> def shift_positions(data, metadata):
...     new_data = data.model_copy(update={"positions": data.positions + 1.0})
...     return new_data, metadata
>>> def tag_origin(data, metadata):
...     metadata["origin"] = "shifted"
...     return data, metadata
>>> compose = Compose([shift_positions, tag_origin])
>>> new_data, new_meta = compose(data, {})

Per-batch composition:

>>> def scale_positions(batch):
...     batch.positions = batch.positions * 2.0
...     return batch
>>> compose = Compose([scale_positions])
>>> new_batch = compose(batch)