spatialvi.external.Lambda#

class spatialvi.external.Lambda(adata, location=None, organism=None, provider='openai', num_parallel=10, n_top_genes=50, resolution=0.5, **kwargs)[source]#

Bases: object

Language-model based cell type annotation.

LAMBDA uses large language models to automatically annotate cell clusters based on differentially expressed marker genes.

Parameters:
  • adata (AnnData) – AnnData with log-normalised expression data; gene symbols must be present in adata.var.index.

  • location (str | None) – Optional text describing the anatomical location of the sample (e.g. “cerebral cortex”). Passed to the LLM to guide annotation.

  • organism (str | None) – Optional species name (e.g. “human” or “mouse”). Passed to the LLM.

  • provider (str) – Backend used for the language model. Possible values include “openai” and “google”. Ensure that the corresponding API key is set in the environment.

  • num_parallel (int) – Number of clusters to annotate concurrently.

  • n_top_genes (int) – Number of differentially expressed genes used for annotation.

  • resolution (float) – Resolution parameter for Leiden clustering when LAMBDA performs automatic clustering.

  • **kwargs (Any) – Additional arguments passed directly to Agent.

  • adata (AnnData)

  • location (str | None)

  • organism (str | None)

  • provider (str)

  • num_parallel (int)

  • n_top_genes (int)

  • resolution (float)

  • kwargs (Any)

Examples

>>> import scanpy as sc
>>> from spatialvi.external import Lambda
>>> adata = sc.read("data.h5ad")
>>> model = Lambda(adata, organism="human", location="brain")
>>> model.train()  # Prepares the agent
>>> adata = model.predict()  # Annotates clusters
>>> print(adata.obs["lambda_0"].value_counts())
__init__(adata, location=None, organism=None, provider='openai', num_parallel=10, n_top_genes=50, resolution=0.5, **kwargs)[source]#
Parameters:
Return type:

None

Methods

__init__(adata[, location, organism, ...])

get_annotations([level])

Get the annotation results for a specific level.

predict([group_key, groups, ...])

Annotate clusters of the dataset with cell type names.

train(*args, **kwargs)

Prepare the LAMBDA agent.

train(*args, **kwargs)[source]#

Prepare the LAMBDA agent.

LAMBDA does not have a trainable model in the usual sense. This method instantiates an Agent and stores it for subsequent predictions.

Return type:

None

Parameters:
predict(group_key='leiden', groups=None, key_is_hierarchical=False, level=0, store_key_prefix='lambda', **kwargs)[source]#

Annotate clusters of the dataset with cell type names.

Parameters:
  • group_key (str | Iterable[str] | None) – Key(s) in adata.obs defining the clusters to annotate. If None, the agent will perform its own clustering. If a sequence of strings is provided, it encodes a hierarchy of levels.

  • groups (str | Iterable[str] | None) – Subset of groups to annotate. None means annotate all groups.

  • key_is_hierarchical (bool) – Whether group_key encodes a hierarchy.

  • level (int) – If a hierarchy is present, index of the level to return results for.

  • store_key_prefix (str) – Prefix under which to store the annotations in adata.obs. Two columns will be created: f"{store_key_prefix}_{level}" for the raw label and f"{store_key_prefix}_score_{level}" for the confidence score.

  • **kwargs (Any) – Additional keyword arguments forwarded to Agent.annotate.

  • group_key (str | Iterable[str] | None)

  • groups (str | Iterable[str] | None)

  • key_is_hierarchical (bool)

  • level (int)

  • store_key_prefix (str)

  • kwargs (Any)

Return type:

AnnData

Returns:

The input AnnData with annotation columns added to obs.

get_annotations(level=0)[source]#

Get the annotation results for a specific level.

Parameters:
  • level (int) – The hierarchical level to retrieve.

  • level (int)

Return type:

dict[str, Any]

Returns:

Dictionary containing annotation results.