spatialvi.external.Starfysh#

class spatialvi.external.Starfysh(adata_spatial, adata_ref, cell_type_key, spatial_key='spatial', n_factors=5, n_hidden=128, use_histology=False)[source]#

Bases: object

Starfysh model for spatial deconvolution with histology.

Starfysh deconvolves spatial transcriptomics spots into cell type proportions using reference single-cell data. It can optionally integrate histology images for improved accuracy.

Parameters:
  • adata_spatial (AnnData) – Spatial transcriptomics AnnData.

  • adata_ref (AnnData) – Reference single-cell AnnData with cell type annotations.

  • cell_type_key (str) – Key in adata_ref.obs for cell type labels.

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

  • n_factors (int) – Number of archetypal factors per cell type.

  • n_hidden (int) – Number of hidden units.

  • use_histology (bool) – Whether to integrate histology features.

  • adata_spatial (AnnData)

  • adata_ref (AnnData)

  • cell_type_key (str)

  • spatial_key (str)

  • n_factors (int)

  • n_hidden (int)

  • use_histology (bool)

Examples

>>> import spatialvi
>>> adata_sp = spatialvi.data.synthetic_spatial()
>>> adata_sc = spatialvi.data.synthetic_scrna()
>>> model = Starfysh(adata_sp, adata_sc, cell_type_key="cell_type")
>>> model.fit()
>>> proportions = model.get_proportions()
__init__(adata_spatial, adata_ref, cell_type_key, spatial_key='spatial', n_factors=5, n_hidden=128, use_histology=False)[source]#
Parameters:

Methods

__init__(adata_spatial, adata_ref, cell_type_key)

fit([max_epochs, lr, batch_size, ...])

Fit the Starfysh model.

get_proportions([return_dataframe])

Get estimated cell type proportions.

get_reconstruction()

Get reconstructed expression.

to_adata()

Store results in spatial AnnData.

fit(max_epochs=500, lr=0.001, batch_size=256, early_stopping=True, patience=20, device='auto')[source]#

Fit the Starfysh model.

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

  • lr (float) – Learning rate.

  • batch_size (int) – Batch size for training.

  • early_stopping (bool) – Whether to use early stopping.

  • patience (int) – Patience for early stopping.

  • device (str) – Device to use (“auto”, “cpu”, “cuda”).

  • max_epochs (int)

  • lr (float)

  • batch_size (int)

  • early_stopping (bool)

  • patience (int)

  • device (str)

Return type:

Starfysh

Returns:

Self.

get_proportions(return_dataframe=True)[source]#

Get estimated cell type proportions.

Parameters:
  • return_dataframe (bool) – Whether to return as DataFrame.

  • return_dataframe (bool)

Return type:

ndarray[tuple[Any, ...], dtype[TypeVar(_ScalarT, bound= generic)]] | DataFrame

Returns:

Cell type proportions.

get_reconstruction()[source]#

Get reconstructed expression.

Return type:

ndarray[tuple[Any, ...], dtype[TypeVar(_ScalarT, bound= generic)]]

Returns:

Reconstructed expression array.

to_adata()[source]#

Store results in spatial AnnData.

Adds proportions to adata_spatial.obsm[“starfysh_proportions”].

Return type:

None