Compose#
- class Compose(cfg=None)[source]#
Bases:
objectBuild a transform pipeline from config dicts and run it in order.
Constructed from a list of config dictionaries (the
transform=[...]list used throughout pimm). At init each entry is materialized through theTRANSFORMSregistry viaTRANSFORMS.build– so every step’stypemust name a registered transform – and build failures are re-raised with the offending step index and type. Calling the instance threads a singledata_dictthrough every transform in sequence, where a failure is re-raised with the failing step’s index, class name, and the keys available at that point. This is the pipeline runner itself, not a registered transform; do not putComposein a config list – pass the raw list of dicts and let datasets build it internally.- Parameters:
cfg (Sequence[dict], optional) – ordered list of transform config dicts, each carrying a
typeplus that transform’s keyword arguments. Defaults to an empty list (an identity pipeline).
Note
Most transforms mutate and return the same
data_dict, but a few (e.g.GridSampleinmode="test") return a different object such as a list of fragment dicts; the runner simply forwards whatever each step returns to the next.Example
>>> import numpy as np >>> from pimm.datasets.transform.base import Compose >>> pipeline = Compose([ ... dict(type="NormalizeCoord", center=[500., 0., 0.], scale=500.), ... dict(type="ToTensor"), ... dict(type="Collect", keys=("coord",), feat_keys=("coord",)), ... ]) >>> data = {"coord": np.array([[0., 0., 0.], [1000., 0., 0.]], dtype="f4")} >>> out = pipeline(data) # normalize -> tensor -> collect, run in order >>> sorted(out) ['coord', 'feat', 'offset'] >>> out["coord"] # NormalizeCoord mapped [0,1000] -> [-1,1]; ToTensor made it a tensor tensor([[-1., 0., 0.], [ 1., 0., 0.]]) >>> out["offset"] tensor([2])