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
ibecomes the input to transformi + 1. An empty sequence acts as the identity.A
Composeis 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 asAtomicData(sample transform) orBatch(batch transform); untyped callables are accepted and take on the kind established by the typed ones. Mixing sample and batch transforms in the sameComposeraisesTypeError. 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#
Trueif this is a batch composition,Falseif it is a sample composition.Falsefor 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
AtomicDatanorBatch, or if typed callables disagree on the composition kind.RuntimeError – (At call time) If any transform raises; the original exception is wrapped in a
RuntimeErroridentifying 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)