clinicadl.transforms.TransformsHandler

class clinicadl.transforms.TransformsHandler(extraction: Extraction | None = None, image_transforms: Sequence[Callable[[DataPointT], DataPointT] | TransformConfig] = (), sample_transforms: Sequence[Callable[[DataPointT], DataPointT] | TransformConfig] = (), augmentations: Sequence[Callable[[DataPointT], DataPointT] | TransformConfig] = ())[source]

Handles the transformation pipeline applied to images.

ClinicaDL defines 4 types of transforms:

  • extraction: defines on what type of elements of the image one wants to work (the whole image, patches or slices).

  • image_transforms: transforms applied on the whole image, before the potential extraction is applied. This is typically where you want to do normalization (to normalize on the whole image and not only on a patch or a slice).

  • sample_transforms: transforms applied on a sample (a patch or a slice), after extraction. This is typically where you want to resize your sample so that it fits in your network.

  • augmentations: transforms applied after image_transforms, extraction and sample_transforms, only during training.

For image_transforms, sample_transforms and augmentations, the transforms must be passed as sequences. TransformsHandler will compose the transforms in these sequences. So, the order in the sequences is important.

Parameters:
  • extraction (Optional[Extraction], default=None) – The extraction applied. See clinicadl.transforms.extraction. If None, Image is used (which is equivalent to no extraction).

  • image_transforms (Sequence[TransformOrConfig], default=()) – A sequence of transforms to apply on the whole image, before extraction. Passed as callables that take as input and return a DataPoint, or configuration class.

  • sample_transforms (Sequence[TransformOrConfig], default=()) – A sequence of transforms to apply on samples (patches or slices). Passed as callables that take as input and return a DataPoint, or configuration class.

  • augmentations (Sequence[TransformOrConfig], default=()) – A sequence of augmentation transforms, to apply on samples, only during training. Passed as callables that take as input and return a DataPoint, or configuration class.

Examples

>>> from clinicadl.transforms import TransformsHandler
>>> from clinicadl.transforms.extraction import Patch
>>> from clinicadl.transforms.config import ZNormalizationConfig, RandomFlipConfig
>>> import torchio
>>> transforms = TransformsHandler(
        extraction=Patch(patch_size=32, stride=32),
        image_transforms=[ZNormalizationConfig(), torchio.CropOrPad(64)],
        sample_transforms=[],
        augmentations=[RandomFlipConfig(flip_probability=0.3)],
    )
apply_image_transforms(datapoint: DataPointT) DataPointT[source]

Applies the transforms passed in image_transforms and returns the output.

Parameters:

datapoint (DataPoint) – The input DataPoint.

Returns:

DataPoint – The transformed DataPoint.

extract_sample(datapoint: DataPointT, sample_index: int) DataPointT[source]

Extracts the sample.

See: clinicadl.transforms.extraction.Extraction.

Parameters:
  • datapoint (DataPoint) – The input DataPoint.

  • sample_index (int) – Index of the sample to extract.

Returns:

DataPoint – The sample in a DataPoint.

apply_sample_transforms(datapoint: DataPointT) DataPointT[source]

Applies the transforms passed in sample_transforms and returns the output.

Parameters:

datapoint (DataPoint) – The input DataPoint.

Returns:

DataPoint – The transformed DataPoint.

apply_augmentations(datapoint: DataPointT) DataPointT[source]

Applies the transforms passed in augmentations and returns the output.

Parameters:

datapoint (DataPoint) – The input DataPoint.

Returns:

DataPoint – The transformed DataPoint.