clinicadl.transforms.extraction.Slice

class clinicadl.transforms.extraction.Slice(slices: list[int] | None = None, tsv_path: Path | str | None = None, discarded_slices: list[int] | None = None, borders: int | Tuple[int, int] | None = None, slice_direction: int | SliceDirection = SliceDirection.ZERO, squeeze: bool = True) None[source]

Transform class to extract slices from an image in a specified direction.

Adds the following keys to the input DataPoint:

  • sample_type : "slice"

  • sample_position: int

    The position of the slice in the original image.

  • slice_direction: 0, 1 or 2

    The slicing direction.

  • squeeze: bool

    Whether the tensors will be squeezed to work with 2D neural networks.

Note

To select slices, use slices, tsv_path, discarded_slices, or borders.

  • If none of these parameters is passed, all slices will be kept.

  • slices and tsv_path cannot be used in conjunction with another slice selection parameter, but discarded_slices and borders can be passed together.

Parameters:
  • slices (Optional[list[int]], default=None) – The slices to select. The slices selected will be the same for all images; if you want a different selection for each image, use tsv_path.

  • tsv_path (Optional[PathType], default=None) – Path to a TSV file containing slice indices for each image. The TSV table must have the columns: participant_id, session_id, and slice_idx.

  • discarded_slices (Optional[list[int]], default=None) – Indices of the slices to discard. Cannot be used with slices or tsv_path.

  • borders (Optional[Union[int, Tuple[int, int]]], default=None) –

    The number of border slices that will be filtered out. If an integer a is passed, the first a slices and the last a slices will be filtered out. If a tuple (a, b) is passed, the first a slices and the last b slices will be filtered out.

    Cannot be used with slices or tsv_path.

  • slice_direction (int | SliceDirection, default=0) –

    The slicing direction. Can be 0, 1 or 2.

    Warning

    Be careful with the orientation of your image. If your image is in RAS+ (e.g. you used ToCanonicalConfig), 0 refers to the sagittal direction, 1 to the coronal direction, and 2 to the axial direction.

  • squeeze (bool, default=True) –

    Whether to later squeeze slices to have images with 2 spatial dimensions. If False, slices will still have 3 spatial dimensions.

    Note

    Squeezing will be performed by ClinicaDL just before putting the images in the neural network. This is because most of ClinicaDL tools work with 3D images.

Examples

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

data = Colin27DataPoint()
slices = Slice(borders=10, slice_direction=1)
>>> data.spatial_shape
(181, 217, 181)
>>> patch.num_samples_per_image(data)
197
>>> slices(data, sample_index=0).spatial_shape
(181, 1, 181)
>>> next(iter(patch(data))).sample_position
10      # because of 'borders' the 10 first slices are ignored
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.