spatialvi.external.SPARL#

class spatialvi.external.SPARL(adata, spatial_key='spatial', layer=None, n_latent=32, **model_params)[source]#

Bases: object

Interface to the SPARL representation learning model.

SPARL learns spatial-aware representations of protein expression data from spatial proteomics technologies.

Parameters:
  • adata (AnnData) – AnnData object containing spatial proteomics data. The main expression matrix (X or a layer) should contain protein measurements. Spatial coordinates should be in obsm.

  • spatial_key (str) – Key in adata.obsm for spatial coordinates.

  • layer (str | None) – Layer in adata.layers to use. If None, uses adata.X.

  • n_latent (int) – Dimensionality of the latent space.

  • **model_params (Any) – Additional parameters passed to the SPARL model.

  • adata (AnnData)

  • spatial_key (str)

  • layer (str | None)

  • n_latent (int)

  • model_params (Any)

Notes

SPARL is distributed separately from spatialvi-tools. To use this wrapper you must install the sparl package.

Examples

>>> from spatialvi.external import SPARL
>>> # Load CODEX data
>>> adata = sc.read("codex_data.h5ad")
>>> model = SPARL(adata, n_latent=20)
>>> model.train(max_epochs=100)
>>> # Get latent representation
>>> adata.obsm["X_sparl"] = model.get_latent_representation()
__init__(adata, spatial_key='spatial', layer=None, n_latent=32, **model_params)[source]#
Parameters:
Return type:

None

Methods

__init__(adata[, spatial_key, layer, n_latent])

get_latent_representation([adata, batch_size])

Get latent representations for cells.

impute_proteins([adata, store_layer])

Impute missing protein values using the trained model.

predict([adata, store_key])

Compute and store latent representations.

reconstruct([adata, batch_size])

Reconstruct protein expression from latent space.

train([max_epochs, batch_size, lr])

Train the SPARL model.

train(max_epochs=100, batch_size=128, lr=0.001, **train_kwargs)[source]#

Train the SPARL model.

Parameters:
  • max_epochs (int) – Maximum number of training epochs.

  • batch_size (int) – Batch size for training.

  • lr (float) – Learning rate.

  • **train_kwargs (Any) – Additional keyword arguments for training.

  • max_epochs (int)

  • batch_size (int)

  • lr (float)

  • train_kwargs (Any)

Return type:

None

Notes

Currently this wrapper provides a placeholder implementation. Full training functionality will be added when the SPARL API stabilizes.

get_latent_representation(adata=None, batch_size=256)[source]#

Get latent representations for cells.

Parameters:
  • adata (AnnData | None) – AnnData object. If None, uses the training data.

  • batch_size (int) – Batch size for inference.

  • adata (AnnData | None)

  • batch_size (int)

Return type:

ndarray

Returns:

Latent representation array of shape (n_cells, n_latent).

predict(adata=None, store_key='X_sparl')[source]#

Compute and store latent representations.

Parameters:
  • adata (AnnData | None) – AnnData object. If None, uses the training data.

  • store_key (str) – Key to store latent representation in obsm.

  • adata (AnnData | None)

  • store_key (str)

Return type:

AnnData

Returns:

AnnData with latent representation stored in obsm.

reconstruct(adata=None, batch_size=256)[source]#

Reconstruct protein expression from latent space.

Parameters:
  • adata (AnnData | None) – AnnData object. If None, uses the training data.

  • batch_size (int) – Batch size for inference.

  • adata (AnnData | None)

  • batch_size (int)

Return type:

ndarray

Returns:

Reconstructed expression array of shape (n_cells, n_proteins).

impute_proteins(adata=None, store_layer='sparl_imputed')[source]#

Impute missing protein values using the trained model.

Parameters:
  • adata (AnnData | None) – AnnData object. If None, uses the training data.

  • store_layer (str) – Key to store imputed expression in layers.

  • adata (AnnData | None)

  • store_layer (str)

Return type:

AnnData

Returns:

AnnData with imputed expression stored in layers.