campmc.partition

Contents

campmc.partition#

campmc.partition(adata, *, method='camp1', gamma=None, gammas=None, key_added='camp', batch_key=None, reduction_key='X_pca', random_state=0, copy=False)#

Partition cells into metacells with CAMP1-4.

Samples a coreset of seed cells (size m n / gamma), then assigns every cell to a seed using the selected method. Results are written to adata.obs and run metadata to adata.uns.

Parameters:
adata AnnData

Annotated data matrix. Requires adata.obsm[reduction_key] (typically PCA from preprocess()).

method Literal['camp1', 'camp2', 'camp3', 'camp4'] (default: 'camp1')

Assignment algorithm: "camp1" (k-means from seeds), "camp2" (cosine nearest seed), "camp3" (adaptive RBF), or "camp4" (SEACells kernel graph).

gamma int | None (default: None)

Target cells per metacell. Mutually exclusive with gammas. Writes labels to adata.obs[key_added] and seed flags to adata.obs[f"{key_added}_is_seed"].

gammas Sequence[int] | None (default: None)

Multiple gamma values. Writes adata.obs[f"{key_added}_{g}"] (and matching _is_seed columns) for each g.

key_added str (default: 'camp')

Prefix for result columns and for adata.uns[key_added] metadata.

batch_key str | None (default: None)

Optional adata.obs column. When set, partitioning runs independently within each batch.

reduction_key str (default: 'X_pca')

Key in adata.obsm used for coreset sampling and assignment.

random_state int (default: 0)

Random seed for coreset sampling (and CAMP1 k-means).

copy bool (default: False)

If True, operate on a copy and return it; otherwise modify adata in place and return None.

Return type:

AnnData | None

Returns:

AnnData | None Updated object if copy=True, else None.

Examples

import campmc as cp
import scanpy as sc
adata = sc.datasets.pbmc68k_reduced()
cp.partition(adata, method="camp3", gamma=50, random_state=0)
adata.obs["camp"].value_counts().head()
adata2 = sc.datasets.pbmc68k_reduced()
cp.partition(adata2, method="camp3", gammas=[25, 50], random_state=0)
adata2.obs["camp_25"].value_counts().head()
camp_25
seed19-25-__all__    104
seed0-25-__all__      57
seed25-25-__all__     57
seed21-25-__all__     52
seed13-25-__all__     47
Name: count, dtype: int64