clinicadl.data.structures.DataPoint¶
- class clinicadl.data.structures.DataPoint(image: ScalarImage | Path | str, participant_id: str, session_id: str, **kwargs: Any) None[source]¶
Data structure that gathers an image and any other relevant information associated to the image.
It inherits from
torchio.Subject, which inherits itself from Python’sdict.A
DataPointcan contain any type of values, but you are encouraged to store your images intorchio.ScalarImageand your masks intorchio.LabelMap.- A DataPoint has the following attributes:
image: the image, in atorchio.ScalarImage;participant_id: the id of the participant, in astr;session_id: the id of the session, in astr.
You can easily access these elements using the attribute notation:
>>> import torchio as tio >>> import torch >>> import numpy as np >>> datapoint = DataPoint( image=tio.ScalarImage(tensor=torch.randn(1, 10, 10, 10), affine=np.eye(4)), participant_id="sub-001", session_id="ses-M000", ) >>> datapoint.session_id 'ses-M000'
To add, modify, or delete any other field, you can use the standard dictionary syntax:
>>> datapoint["age"] = 55 >>> datapoint["age"] 55
To add an image or a mask to the
DataPoint, preferadd_image()andadd_mask().To get all the images in your DataPoint, you can use
get_images_dict().If all the images and masks of your DataPoint have the same shape, voxel spacing or affine matrix, you can easily access them via the attributes
shape(orspatial_shapeto remove the channel dimension),spacingandaffinerespectively.Finally, you may also be interested in
plot()to plot images inside yourDataPoint.As
DataPointis a subclass oftorchio.Subject, you can also used all the other methods it inherits from.- Parameters:
image (Union[torchio.ScalarImage, PathType]) – The image, as a
torchio.ScalarImageor apathto a NIfTI file.participant_id (str) – The participant id.
session_id (str) – The session id.
kwargs (Any) – Any other information to store in the
DataPoint.
- property shape¶
Returns the shape of the images in the
DataPoint.Consistency of shapes across images in the
DataPointis checked first.Examples
>>> from clinicadl.data.structures.examples import Colin27DataPoint >>> datapoint = Colin27DataPoint() >>> datapoint.shape (1, 181, 217, 181)
- property spatial_shape¶
Returns the spatial shape of the images in the
DataPoint.Consistency of spatial shapes across images in the
DataPointis checked first.Examples
>>> from clinicadl.data.structures.examples import Colin27DataPoint >>> datapoint = Colin27DataPoint() >>> datapoint.spatial_shape (181, 217, 181)
- property spacing¶
Returns the voxel spacing of the images in the
DataPoint.Consistency of voxel spacings across images in the
DataPointis checked first (1e-3 relative tolerance).Examples
>>> from clinicadl.data.structures.examples import Colin27DataPoint >>> datapoint = Colin27DataPoint() >>> datapoint.spacing (1.0, 1.0, 1.0)
- property affine¶
Returns affine matrix of the images in the
DataPoint.Consistency of matrices across images in the
DataPointis checked first (1e-3 relative tolerance).Examples
>>> from clinicadl.data.structures.examples import Colin27DataPoint >>> datapoint = Colin27DataPoint() >>> datapoint.affine array([[ 1., 0., 0., -90.], [ 0., 1., 0., -126.], [ 0., 0., 1., -72.], [ 0., 0., 0., 1.]])
- get_images_dict(intensity_only: bool = True, include: Sequence[str] | None = None, exclude: Sequence[str] | None = None) dict[str, Image][source]¶
To get all the images in a
DataPoint, and their names.- Parameters:
intensity_only (bool, default=True) – To get only the images (
torchio.ScalarImage) and not the masks (torchio.LabelMap).include (Optional[Sequence[str]], default=None) – Names of the images to include. If
None, will return all the images specified byintensity_onlyand not inexclude.exclude (Optional[Sequence[str]], default=None) – Names of the images to exclude.
- Returns:
dict[str, torchio.Image] – The images and their names.
Examples
>>> from clinicadl.data.structures.examples import Colin27DataPoint >>> datapoint = Colin27DataPoint() >>> datapoint.get_images_dict() {'image': ScalarImage(shape: (1, 181, 217, 181); spacing: (1.00, 1.00, 1.00); orientation: RAS+; path: ...)}
See also
- get_masks_dict(include: Sequence[str] | None = None, exclude: Sequence[str] | None = None) dict[str, LabelMap][source]¶
To get all the masks in a
DataPoint, and their names.- Parameters:
- Returns:
dict[str, torchio.LabelMap] – The masks and their names.
Examples
>>> from clinicadl.data.structures.examples import Colin27DataPoint >>> datapoint = Colin27DataPoint() >>> datapoint.get_masks_dict() {'head': LabelMap(shape: (1, 181, 217, 181); spacing: (1.00, 1.00, 1.00); orientation: RAS+; path: ...)}
See also
- get_non_images_dict(include: Sequence[str] | None = None, exclude: Sequence[str] | None = None) dict[str, Any][source]¶
To get all the values in the
DataPointthat are not images or masks.- Parameters:
- Returns:
dict[str, Any] – The non-image values and their keys.
Examples
>>> from clinicadl.data.structures.examples import Colin27DataPoint >>> datapoint = Colin27DataPoint() >>> datapoint.get_non_images_dict() {'participant_id': 'sub-colin', 'session_id': 'ses-M000'}
See also
- get_image_tensor(image_name: str) Tensor[source]¶
Returns a copy of the tensor associated to an image that is a
torchio.Image.- Parameters:
image_name (str) – The name of the image in the
DataPoint.- Returns:
torch.Tensor – The tensor image.
- get_keys(include: Sequence[str] | None = None, exclude: Sequence[str] | None = None) list[str][source]¶
To get the list of all the keys in a
DataPoint.- Parameters:
- Returns:
list[str] – The keys.
Examples
>>> from clinicadl.data.structures.examples import Colin27DataPoint >>> datapoint = Colin27DataPoint() >>> datapoint.get_keys() ['image', 'head', 'participant_id', 'session_id'] >>> datapoint.get_keys(exclude=["image"]) ['head', 'participant_id', 'session_id'] >>> datapoint.get_keys(include=["image"]) ['image']
- add_image(image: ScalarImage | Path | str | Tensor, image_name: str) None[source]¶
To add an image to the
DataPoint.- 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 4Dtorch.Tensor(including one channel dimension). If aTensoris passed, the same affine matrix asself.imagewill be used.image_name (str) – The name that the image will take in the
DataPoint.
Examples
>>> from clinicadl.data.structures.examples import Colin27DataPoint >>> datapoint = Colin27DataPoint() >>> datapoint Colin27DataPoint(Keys: ('head', 'image', 'participant_id', 'session_id'); images: 2) >>> datapoint.add_image(datapoint.image, "image_duplicate") >>> datapoint Colin27DataPoint(Keys: ('head', 'image', 'participant_id', 'session_id', 'image_duplicate'); images: 3) >>> datapoint["image_duplicate"] ScalarImage(shape: (1, 181, 217, 181); spacing: (1.00, 1.00, 1.00); orientation: RAS+; path: ...)
See also
- add_mask(mask: ScalarImage | Path | str | Tensor, mask_name: str) None[source]¶
To add a mask to the
DataPoint.- Parameters:
mask (Union[tio.ScalarImage, PathType, torch.Tensor]) – The mask to add, as a
torchio.LabelMap, a path to the NIfTI file containing the image, or a 4Dtorch.Tensor(including one channel dimension). If aTensoris passed, the same affine matrix asself.image`will be used.mask_name (str) – The name that the mask will take in the
DataPoint.
Examples
>>> from clinicadl.data.structures.examples import Colin27DataPoint >>> datapoint = Colin27DataPoint() >>> datapoint Colin27DataPoint(Keys: ('head', 'image', 'participant_id', 'session_id'); images: 2) >>> datapoint.add_mask(datapoint["head"], "head_duplicate") >>> datapoint Colin27DataPoint(Keys: ('head', 'image', 'participant_id', 'session_id', 'head_duplicate'); images: 3) >>> datapoint["head_duplicate"] LabelMap(shape: (1, 181, 217, 181); spacing: (1.00, 1.00, 1.00); orientation: RAS+; path: ...)
See also