Subject

The Subject is a data structure used to store images associated with a subject and any other metadata necessary for processing.

All transforms applied to a Subject are saved in its history attribute (see Reproducibility).

class torchio.Subject(*args, **kwargs: Dict[str, Any])[source]

Bases: dict

Class to store information about the images corresponding to a subject.

Parameters
  • *args – If provided, a dictionary of items.

  • **kwargs – Items that will be added to the subject sample.

Example

>>> import torchio as tio
>>> # One way:
>>> subject = tio.Subject(
...     one_image=tio.ScalarImage('path_to_image.nii.gz'),
...     a_segmentation=tio.LabelMap('path_to_seg.nii.gz'),
...     age=45,
...     name='John Doe',
...     hospital='Hospital Juan Negrín',
... )
>>> # If you want to create the mapping before, or have spaces in the keys:
>>> subject_dict = {
...     'one image': tio.ScalarImage('path_to_image.nii.gz'),
...     'a segmentation': tio.LabelMap('path_to_seg.nii.gz'),
...     'age': 45,
...     'name': 'John Doe',
...     'hospital': 'Hospital Juan Negrín',
... }
>>> subject = tio.Subject(subject_dict)
add_image(image: torchio.data.image.Image, image_name: str)None[source]

Add an image.

apply_inverse_transform(**kwargs)torchio.data.subject.Subject[source]

Try to apply the inverse of all applied transforms, in reverse order.

Parameters

**kwargs – Keyword arguments passed on to get_inverse_transform().

get_inverse_transform(warn: bool = True, ignore_intensity: bool = True, image_interpolation: Optional[str] = None)Compose[source]

Get a reversed list of the inverses of the applied transforms.

Parameters
  • warn – Issue a warning if some transforms are not invertible.

  • ignore_intensity – If True, all instances of IntensityTransform will be ignored.

  • image_interpolation – Modify interpolation for scalar images inside transforms that perform resampling.

load()None[source]

Load images in subject on RAM.

plot(**kwargs)None[source]

Plot images using matplotlib.

Parameters

**kwargs – Keyword arguments that will be passed on to Image.

remove_image(image_name: str)None[source]

Remove an image.

property shape

Return shape of first image in subject.

Consistency of shapes across images in the subject is checked first.

property spacing

Return spacing of first image in subject.

Consistency of spacings across images in the subject is checked first.

property spatial_shape

Return spatial shape of first image in subject.

Consistency of spatial shapes across images in the subject is checked first.