Collect#
- class Collect(keys, offset_keys_dict=None, **kwargs)[source]#
Bases:
objectFinal projection of a sample dict into the model-facing batch contract.
Keeps only the requested keys, builds per-sample
offsetcount tensors, and concatenates feature groups (e.g.feat_keys->feat) into the fused input tensors a backbone consumes. This is normally the last transform in a pipeline, run afterToTensor. Reads the keys named inkeysand in every*_keysgroup; the length used foroffsetis read from the source key’s first dimension (coordby default). Returns a new dict containing only the collected outputs. Registered asCollect– use this string as thetypein atransform=[...]config list.Each
*_keyskeyword argument names a list of source keys; the suffix_keysis stripped and the named arrays are concatenated alongdim=1(cast to float) under the resulting key. For examplefeat_keysproducesfeat– whose total channel width sets the backbonein_channels– andoffset_keys_dictmaps each output key to the source key whose row count becomes its[N]offset tensor.- Parameters:
keys (str | Sequence[str]) – keys to copy through verbatim into the output dict. A bare string is wrapped into a single-element list.
offset_keys_dict (dict, optional) – mapping of output offset key to the source key whose number of rows is recorded as a length-1 tensor. Defaults to
dict(offset="coord").**kwargs – feature-group specifications. Every keyword ending in
_keys(e.g.feat_keys=["coord", "energy"]) names a sequence of keys that are concatenated along the channel dimension into a key with the_keyssuffix removed (e.g.feat).
Note
Requires that the source key behind each
offsetentry (coordby default) and every key referenced inkeys/*_keysis present. The concatenated feature tensor’s channel width must match the model’sin_channels.Example
>>> import numpy as np >>> from pimm.datasets.transform import Collect, ToTensor >>> data = {"coord": np.array([[1., 2., 3.], [4., 5., 6.]], dtype="f4"), ... "energy": np.array([[10.], [20.]], dtype="f4")} >>> data = ToTensor()(data) # Collect concatenates tensors, so run after ToTensor >>> out = Collect(keys=("coord",), feat_keys=("coord", "energy"))(data) >>> sorted(out) # only kept keys, plus the built feat / offset ['coord', 'feat', 'offset'] >>> out["feat"] # coord (3 ch) and energy (1 ch) fused -> 4 channels tensor([[ 1., 2., 3., 10.], [ 4., 5., 6., 20.]]) >>> out["offset"] # number of points in this sample tensor([2])