Copy#

class Copy(keys_dict=None)[source]#

Bases: object

Duplicate keys in the sample dict under new names.

For each source -> destination entry, deep-copies data_dict[source] into data_dict[destination] in place. Numpy arrays are .copy()-ed, torch tensors are .clone().detach()-ed, and anything else is copy.deepcopy-ed, so the destination is independent of the source. The two canonical uses are preserving a pristine copy of a key before augmentation mutates it (e.g. coord -> origin_coord for test-time re-mapping) and remapping a dataset-specific label into the conventional segment name (e.g. segment_motif -> segment). Returns the same data_dict. Registered as Copy – use this string as the type in a transform=[...] config list.

Parameters:

keys_dict (dict, optional) – mapping of source key to destination key. Each source must already exist in the sample. Defaults to dict(coord="origin_coord", segment="origin_segment").

Note

Requires every source key to be present in data_dict.

Example

>>> import numpy as np
>>> from pimm.datasets.transform import Copy
>>> data = {"segment_motif": np.array([2, 3, 2])}
>>> out = Copy(keys_dict={"segment_motif": "segment"})(data)
>>> sorted(out)  # source remains; an independent "segment" copy is added
['segment', 'segment_motif']
>>> out["segment"]
array([2, 3, 2])