clinicadl.transforms.Format

class clinicadl.transforms.Format(dtype: dtype | dtype | str | None = None, squeeze: bool | int | Sequence[int] = False, unsqueeze: int | None = None, **kwargs)[source]

Transform to modify the shape or the type of some values in a DataPoint.

To be transformed, the values are expected to be torch.Tensor or a numpy.ndarray.

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 include or exclude to specify the keys whose values should be modified, and copy to specify if the output DataPoint should be the same object as the input or a deepcopy.

Parameters:
  • dtype (Optional[torch.dtype], default=None) – The wanted data type, passed as a torch.dtype, a numpy.dtype, or a str (e.g., “float32”, “int64”). If None, input’s dtype will be kept.

  • squeeze (Union[bool, int, Sequence[int]], default=False) – Whether to squeeze the tensor/array, i.e. removing dimension(s) of size 1. If True, all such dimensions will be removed. Specific dimension(s) to remove can be specified via an int or a sequence of ints.

  • unsqueeze (Optional[int], default=None) –

    The position where to insert the new dimension. If None, no unsqueezing will be performed.

    Note

    Squeezing is performed before unsqueezing.

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

Example

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

data = Colin27DataPoint(age=55.0, array=np.array([1, 2]))
>>> Format(dtype="int64", include=["age"])(data)["age"]
55
>>> Format(unsqueeze=1, include=["array"])(data)["array"]
[[1]
 [2]]
__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.