Skip to content

Neighbours and clusters (GPU)

The CPU twin is bixverse.graph.

graph

GPU kNN, neighbour graphs and clustering.

The functions write what their bixverse.graph twins write: the kNN and the neighbour graph to the cache, stamped, the cluster labels to obs. The kNN runs on the GPU; the graph built from it is the CPU one.

>>> find_neighbours(sc)
>>> find_clusters(sc, res=1.0)

generate_knn

generate_knn(
    sc: SingleCells,
    *,
    embd_to_use: str = "pca",
    cells_to_use: Sequence[str] | None = None,
    no_embd_to_use: int | None = None,
    knn_params: KnnGpuDefaults | None = None,
    seed: int = 42,
    verbose: Verbose = True,
    backend: Backend = "wgpu",
) -> KnnGraph | None

A GPU kNN over an embedding, without touching the experiment.

Methods: "nndescent" (the default: the descent graph, handed back without a search), "exhaustive" (exact), "ivf" and "cagra" (the graph, beam searched). Euclidean distances are true L2.

Parameters:

Name Type Description Default
sc SingleCells

The experiment.

required
embd_to_use str

The embedding, "pca" or a name in sc.cache.embeddings.

'pca'
cells_to_use Sequence[str] | None

Cell ids to restrict the kNN to; they keep the embedding's row order. None for all kept cells.

None
no_embd_to_use int | None

Number of leading dimensions to use. None for all.

None
knn_params KnnGpuDefaults | None

The GPU kNN parameters, k included. None runs "nndescent" with the defaults.

None
seed int

Seed for the index build.

42
verbose Verbose

0 (False) silent, 1 (True) progress, 2 extra detail where the method has it.

True
backend Backend

"wgpu" (any GPU), or "mlx" (Apple Silicon, needs a source build with the mlx feature; see mlx_available()).

'wgpu'

Returns:

Type Description
KnnGraph | None

The kNN, indices into the rows used; None (with a warning) if the

KnnGraph | None

embedding is absent.

Raises:

Type Description
BixverseError

Without a GPU adapter, or with backend="mlx" on a build without the MLX twins.

find_neighbours

find_neighbours(
    sc: ScOrMc,
    *,
    embd_to_use: str = "pca",
    no_embd_to_use: int | None = None,
    modality: CacheModality = "rna",
    knn_params: KnnGpuDefaults | None = None,
    graph_params: KnnGraphDefaults | None = None,
    seed: int = 42,
    verbose: Verbose = True,
    backend: Backend = "wgpu",
) -> None

The GPU kNN and its neighbour graph over an embedding.

The kNN goes to the modality's cache, stamped as derived from the embedding, and its symmetrisation (knn_sym) to its connectivities, stamped as derived from "knn", as bixverse.graph.find_neighbours.

Parameters:

Name Type Description Default
sc ScOrMc

The experiment.

required
embd_to_use str

The embedding, "pca" or a name in the cache's embeddings.

'pca'
no_embd_to_use int | None

Number of leading dimensions to use. None for all.

None
modality CacheModality

"rna", or "adt" / "atac" on a SingleCellsMultiModal.

'rna'
knn_params KnnGpuDefaults | None

The GPU kNN parameters. None runs "nndescent" with the defaults.

None
graph_params KnnGraphDefaults | None

How the kNN becomes a graph. None uses the defaults.

None
seed int

Seed for the index build.

42
verbose Verbose

0 (False) silent, 1 (True) progress, 2 extra detail where the method has it.

True
backend Backend

"wgpu" (any GPU), or "mlx" (Apple Silicon, needs a source build with the mlx feature; see mlx_available()).

'wgpu'

Raises:

Type Description
ValueError

If the embedding is stale.

BixverseError

Without a GPU adapter, or with backend="mlx" on a build without the MLX twins.

find_clusters

find_clusters(
    sc: ScOrMc,
    *,
    cluster_algorithm: ClusterAlgorithm = "leiden",
    res: float = 1.0,
    name: str = "leiden_clustering",
    modality: Modality = "rna",
    seed: int = 42,
    beta: float = 0.01,
    n_iterations: int | None = None,
    min_gain: float = 0.0,
    local_moving: LocalMoving | None = None,
    node_order: NodeOrder | None = None,
) -> None

Leiden or Louvain on the neighbour graph, level-0 local moving on the GPU.

Writes what bixverse.graph.find_clusters writes. Graphs below 50,000 nodes, or with a node of very high degree, run fully on the CPU with coloured local moving, so the labels can differ from the CPU twin's queue-based ones. On one device the result depends on the seed only.

Parameters:

Name Type Description Default
sc ScOrMc

The experiment, with a neighbour graph over the kept cells.

required
cluster_algorithm ClusterAlgorithm

"leiden" or "louvain".

'leiden'
res float

Modularity resolution; higher gives more communities.

1.0
name str

The obs column to write.

'leiden_clustering'
modality Modality

The graph: "rna", or "adt" / "atac" / "wnn" on a SingleCellsMultiModal.

'rna'
seed int

Seed.

42
beta float

Randomness of the Leiden refinement. Ignored by Louvain.

0.01
n_iterations int | None

Full iterations; negative iterates until stable. None is 2.

None
min_gain float

With a negative n_iterations, also stop once an iteration gains no more than this.

0.0
local_moving LocalMoving | None

Local moving on the coarser levels, "queue" or "coloured". None is "queue" for Leiden and "coloured" for Louvain.

None
node_order NodeOrder | None

Visit order of the queue, "bfs" or "shuffle". None is "bfs" for Leiden and "shuffle" for Louvain.

None

Raises:

Type Description
ValueError

If the graph is stale or does not cover the kept cells.

BixverseError

Without a GPU adapter that runs plane operations.

References

Traag, Waltman & van Eck, Sci Rep, 2019.

fast_cluster

fast_cluster(
    sc: ScOrScSubset,
    *,
    embd_to_use: str = "pca",
    no_embd_to_use: int | None = None,
    resolutions: Sequence[float] = (2.0, 1.0, 0.5),
    fc_params: FastClusterGpuParams | None = None,
    n_seeds: int = 1,
    return_kmeans: bool = False,
    seed: int = 42,
    verbose: Verbose = True,
    backend: Backend = "wgpu",
) -> FastClusters

k-means coarsening on the GPU, a centroid graph and Louvain / Leiden on it.

Returns what bixverse.graph.fast_cluster returns. Only the k-means (and the cell-mediated top-m search) run on the GPU; it is full-batch Lloyd's, so there are no mini-batch knobs. MLX runs every iteration and holds the data at f32, so it ignores fixed and quantise.

Parameters:

Name Type Description Default
sc ScOrScSubset

The experiment.

required
embd_to_use str

The embedding, "pca" or a name in sc.cache.embeddings.

'pca'
no_embd_to_use int | None

Number of leading dimensions to use. None for all.

None
resolutions Sequence[float]

Modularity resolutions.

(2.0, 1.0, 0.5)
fc_params FastClusterGpuParams | None

The parameters. None uses the defaults; a None n_centroids becomes floor(4 * sqrt(n_cells)).

None
n_seeds int

Seeds per resolution; 1 skips the stability statistics.

1
return_kmeans bool

Return the k-means assignments and centroids.

False
seed int

Seed.

42
verbose Verbose

0 (False) silent, 1 (True) progress, 2 extra detail where the method has it.

True
backend Backend

"wgpu" (any GPU), or "mlx" (Apple Silicon, needs a source build with the mlx feature; see mlx_available()).

'wgpu'

Raises:

Type Description
ValueError

If the embedding is absent, or on an empty resolution list or n_seeds < 1.

BixverseError

Without a GPU adapter, or with backend="mlx" on a build without the MLX twins.

References

Wei, et al., PLoS Comput Biol, 2022.