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.Tensoror anumpy.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 inincludeorexcludeto specify the keys whose values should be modified, andcopyto specify if the outputDataPointshould 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, anumpy.dtype, or astr(e.g., “float32”, “int64”). IfNone, 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 anintor a sequence ofints.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 oftorchio.Subject, 4Dtorch.Tensorornumpy.ndarraywith 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, atorchio.Image, a NiBabel Nifti1 image or adict. The output type is the same as the input type.