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
|
normalize
|
bool
|
If |
True
|
make_skewness_positive
|
bool
|
If |
True
|
values_range
|
tuple[float, float] | None
|
If not |
(-2.3, 2.3)
|
clip
|
bool
|
If |
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] #
modelis 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 |
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.