clinicadl.transforms.extraction.Patch

class clinicadl.transforms.extraction.Patch(patch_size: int | tuple[int, int, int], overlap: float | tuple[float, float, float] = 0.0, pad_mode: str | PadMode | None = PadMode.CONSTANT, pad_value: float = 0.0) None[source]

Transform class to extract patches from an image.

The image is divided into smaller patches using a sliding window approach.

Adds the following keys to the input DataPoint:

  • sample_type : "patch"

  • sample_position: tuple[int, int, int]

    The position of the patch in the image, which is defined as the position of its upper left voxel. The origin is defined at the upper left voxel of the image.

Parameters:
  • patch_size (Union[int, tuple[int, int, int]]) – The size of the patches. If a single value is passed, the same patch size will be used for the three spatial dimensions.

  • overlap (Union[float, tuple[float, float, float]], default=0.0) – A float in \([0.0, 1.0)\) that defines relative patch overlap in each dimension. If a single value is passed, the same overlap will be used for the three spatial dimensions.

  • pad_mode (Optional[str | PadMode], default="constant") – A padding mode accepted by torch.nn.functional.pad(), i.e. one of "constant", "reflect", "replicate" or "circular". If None, no padding will be applied, so the patches that cross the border of the image will be dropped.

  • pad_value (float, default=0.0) – The value for "constant" padding.

Examples

from clinicadl.transforms.extraction import Patch
from clinicadl.data.structures.examples import Colin27DataPoint

data = Colin27DataPoint()
patch = Patch(patch_size=64)
>>> data.spatial_shape
(181, 217, 181)
>>> patch.num_samples_per_image(data)
36
>>> patch(data, sample_index=0).spatial_shape
(64, 64, 64)
>>> next(iter(patch(data))).sample_position
(0, 0, 0)
property sample_type: str

The type of the sample returned by this extraction, among {“image”, “slice”, “patch”}.

__call__(data_point: DataPointT, sample_index: int | None = None) DataPointT | Generator[DataPointT, None, None]

Extracts samples from a DataPoint object and returns a generator, or extracts a single sample and returns a DataPoint.

Samples are extracted from every images and masks in the input DataPoint.

Parameters:
  • data_point (DataPoint) – The images to perform extraction on.

  • sample_index (Optional[int], default=None) – Potential index indicating the sample to extract. If None, a generator of all the samples will be returned.

Returns:

Union[DataPoint, Generator[DataPoint, None, None]] – A new DataPoint, with the extracted sample, and some new information about the extraction (e.g., the sample position), or a generator of such DataPoints.

Raises:

IndexError – If sample_index is greater or equal to the number of samples in the image.

num_samples_per_image(data_point: DataPoint) int

Returns the number of samples that can be extracted from an image.

Parameters:

data_point (DataPoint) – The data containing the images to perform extraction on.

Returns:

int – The number of samples in the image.