Skip to content

PCA

PCA

Bases: IntensityTransform

Compute principal component analysis (PCA) of an image.

PCA can be useful to visualize embeddings generated by a neural network. See for example Figure 8 in Cluster and Predict Latent Patches for Improved Masked Image Modeling .

Parameters:

Name Type Description Default
num_components int

Number of components to compute.

3
whiten bool

If True, the components are normalized to have unit variance.

True
normalize bool

If True, all components are divided by the standard deviation of the first component.

True
make_skewness_positive bool

If True, the skewness of each component is made positive by multiplying the component by -1 if its skewness is negative.

True
values_range tuple[float, float] | None

If not None, these values are linearly mapped to \([0, 1]\).

(-2.3, 2.3)
clip bool

If True, the output values are clipped to \([0, 1]\).

True
pca_kwargs dict[str, Any] | None

Additional keyword arguments to pass to [sklearn.decomposition.PCA][].

None

Examples:

import torchio as tio from torchio.visualization import build_image_from_reference ct = my_preprocessed_ct_image # Assume this is a preprocessed CT image ct ScalarImage(shape: (1, 240, 480, 480); spacing: (1.50, 0.75, 0.75); orientation: SLP+; dtype: torch.FloatTensor; memory: 210.9 MiB) embedding_tensor = model(ct.data[None])[0] # model is some pre-trained neural network embedding_image = ToReferenceSpace(ct)(embedding_tensor) embedding_image ScalarImage(shape: (512, 24, 24, 24); spacing: (15.00, 15.00, 15.00); orientation: SLP+; dtype: torch.FloatTensor; memory: 27.0 MiB) pca = tio.PCA()(embedding_image) pca ScalarImage(shape: (3, 24, 24, 24); spacing: (15.00, 15.00, 15.00); orientation: SLP+; dtype: torch.FloatTensor; memory: 162.0 KiB)

__call__(data)

Transform data and return a result of the same type.

Parameters:

Name Type Description Default
data InputType

Instance of torchio.Subject, 4D torch.Tensor or numpy.ndarray with dimensions \((C, W, H, D)\), where \(C\) is the number of channels and \(W, H, D\) are the spatial dimensions. If the input is a tensor, the affine matrix will be set to identity. Other valid input types are a SimpleITK image, a torchio.Image, a NiBabel Nifti1 image or a dict. The output type is the same as the input type.

required

get_base_args()

Provides easy access to the arguments used to instantiate the base class (Transform) of any transform.

This method is particularly useful when a new transform can be represented as a variant of an existing transform (e.g. all random transforms), allowing for seamless instantiation of the existing transform with the same arguments as the new transform during apply_transform.

Note

The p argument (probability of applying the transform) is excluded to avoid multiplying the probability of both existing and new transform.

add_base_args(arguments, overwrite_on_existing=False)

Add the init args to existing arguments

validate_keys_sequence(keys, name) staticmethod

Ensure that the input is not a string but a sequence of strings.

to_hydra_config()

Return a dictionary representation of the transform for Hydra instantiation.

arguments_are_dict()

Check if main arguments are dict.

Return True if the type of all attributes specified in the args_names have dict type.