clinicadl.data.structures.Sample2D

class clinicadl.data.structures.Sample2D(image: ScalarImage | Path | str, participant_id: str, session_id: str, file_type: BidsFileType, image_path: Path, sample_position: int, slice_direction: int, squeeze: bool, **kwargs: Any)[source]

A Sample corresponding to a 2D slice.

Here sample_type="slice" and sample_position is the position of the slice in the original image.

Besides, there are two additional attributes:

slice_directionint

The slicing direction (0, 1 or 2).

squeezebool

Whether the tensors will be later squeezed to work with 2D slice, or whether the slices will stay 3D (with one dummy dimension). The attribute is useful for some ClinicaDL operations.

get_image_tensor(image_name: str) Tensor[source]

Returns a copy of the tensor associated to a field that is a torchio.Image.

If squeeze=True, the output tensor will be squeezed.

Parameters:

image_name (str) – The name of the image in the Sample2D.

Returns:

torch.Tensor – The tensor image.

add_image(image: ScalarImage | Path | str | Tensor, image_name: str) None[source]

To add an image to the Sample.

Parameters:
  • image (Union[tio.ScalarImage, PathType, torch.Tensor]) –

    The image to add, as a torchio.ScalarImage`, a path to the NIfTI file containing the image, or a torch.Tensor. In the latter case, it is expected to be a 4D Tensor (including one channel dimension) if squeeze=False, or a 3D Tensor if squeeze=True.

    If a Tensor is passed, the same affine matrix as self.image will be used.

  • image_name (str) – The name that the image will take in the Sample.

add_mask(mask: LabelMap | Path | str, mask_name: str) None[source]

To add a mask to the Sample.

Parameters:
  • mask (Union[tio.ScalarImage, PathType, torch.Tensor]) –

    The mask to add, as a torchio.LabelMap, a path to the NIfTI file containing the mask, or a torch.Tensor. In the latter case, it is expected to be a 4D Tensor (including one channel dimension) if squeeze=False, or a 3D Tensor if squeeze=True.

    If a Tensor is passed, the same affine matrix as self.image will be used.

  • mask_name (str) – The name that the image will take in the Sample.