spared.spot_features.compute_patches_predictions

spared.spot_features.compute_patches_predictions(adata: AnnData, backbone: str = 'densenet', model_path: str = 'None', patch_size: int = 224) None[source]

Compute predictions for patches.

This function computes gene expression predictions for a given backbone model and adata object. It can optionally compute using a stored model in model_path or a pretrained model from pytorch. The predictions are stored in adata.obsm[f'predictions_{backbone}']. The patches must already be stored in a flattened format inside adata.obsm[f'patches_scale_{patch_scale}'] and must be of shape (n_patches, patch_size*patch_size*3). The patch_scale key can be whatever you want as long as there is only one key with the word patches_scale in the obsm keys. Normally, the key is patches_scale_1.0.

The function only modifies the AnnData object in place. The patch information should be in int format from 0 to 255. All needed transformations are done inside the function.

All models will be declared to have the same number of outputs as genes in the adata object (adata.n_vars). Please also note that if you try to predict with a model that has only been pretrained on ImageNet, the predictions will be random and not useful. So always try to use models pretrained in spatial transcriptomics datasets.

Parameters:
  • adata (ad.AnnData) – The AnnData object with the patches to process.

  • backbone (str, optional) – A string specifying the backbone model to use. Must be one of the following ['resnet', 'resnet50', 'ConvNeXt', 'EfficientNetV2', 'InceptionV3', 'MaxVit', 'MobileNetV3', 'ResNetXt', 'ShuffleNetV2', 'ViT', 'WideResnet', 'densenet', 'swin']. Defaults to 'densenet'.

  • model_path (str, optional) – The path to a stored model. If set to 'None', then an ImageNet pretrained model is used. Defaults to 'None'.

  • patch_size (int, optional) – The size of the patches. Defaults to 224.

Raises:

ValueError – If the backbone is not supported.