clinicadl.transforms.MergeFields

class clinicadl.transforms.MergeFields(*keys: str, output_key: str, **kwargs)[source]

Transform to merge several values of a DataPoint.

The result of the merger depends on the type of values:

  • torch.Tensor and numpy.ndarray are stacked along a new dimension (so they are expected to all have the same shape);

  • torchio.Image are concatenated along the channel dimension (so they are expected to all have the same spatial shape);

  • lists and tuples are concatenated;

  • otherwise, the values are just put in a list.

This transform inherits from torchio.Transform, and therefore any argument accepted by this parent class can be passed via a keyword argument here. Particularly, you may be interested in copy to specify if the output DataPoint should be the same object as the input or a deepcopy.

Parameters:
  • *keys (str) – The keys whose values should be merged.

  • output_key (str) – The name of key in the DataPoint corresponding to the output of the merger.

  • **kwargs (Any) – Any keyword argument accepted by torchio.Transform.

Example

from clinicadl.transforms import MergeFields
from clinicadl.data.structures.examples import Colin27DataPoint
import numpy as np

data = Colin27DataPoint(age=55, sex="M", array_1=np.zeros(2), array_2=np.ones(2))
>>> MergeFields("age", "sex", output_key="label")(data)["label"]
[55, 'M']
>>> MergeFields("array_1", "array_2", output_key="label")(data)["label"]
[[0. 0.]
 [1. 1.]]
__call__(data: InputType) InputType

Transform data and return a result of the same type.

Parameters:

data (TypeVar(InputType, bound= Union[Subject, Image, Tensor, ndarray, Image, dict, Nifti1Image])) – Instance of torchio.Subject, 4D torch.Tensor or numpy.ndarray with dimensions \((C, W, H, D)\), where \(C\) is the number of channels and \(W, H, D\) are the spatial dimensions. If the input is a tensor, the affine matrix will be set to identity. Other valid input types are a SimpleITK image, a torchio.Image, a NiBabel Nifti1 image or a dict. The output type is the same as the input type.