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: intThe position of the slice in the original image.
slice_direction: 0, 1 or 2The slicing direction.
squeeze: boolWhether the tensors will be squeezed to work with 2D neural networks.
Note
To select slices, use
slices,tsv_path,discarded_slices, orborders.If none of these parameters is passed, all slices will be kept.
slicesandtsv_pathcannot be used in conjunction with another slice selection parameter, butdiscarded_slicesandborderscan 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
TSVfile containing slice indices for each image. TheTSVtable must have the columns:participant_id,session_id, andslice_idx.discarded_slices (Optional[list[int]], default=None) – Indices of the slices to discard. Cannot be used with
slicesortsv_path.borders (Optional[Union[int, Tuple[int, int]]], default=None) –
The number of border slices that will be filtered out. If an integer
ais passed, the firstaslices and the lastaslices will be filtered out. If a tuple(a, b)is passed, the firstaslices and the lastbslices will be filtered out.Cannot be used with
slicesortsv_path.slice_direction (int | SliceDirection, default=0) –
The slicing direction. Can be
0,1or2.Warning
Be careful with the orientation of your image. If your image is in RAS+ (e.g. you used
ToCanonicalConfig),0refers to the sagittal direction,1to the coronal direction, and2to 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
ClinicaDLjust before putting the images in the neural network. This is because most ofClinicaDLtools 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
DataPointobject and returns a generator, or extracts a single sample and returns aDataPoint.Samples are extracted from every images and masks in the input
DataPoint.- Parameters:
- 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 suchDataPoints.- Raises:
IndexError – If
sample_indexis greater or equal to the number of samples in the image.