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'
|
cells_to_use
|
Sequence[str] | None
|
Cell ids to restrict the kNN to; they keep the
embedding's row order. |
None
|
no_embd_to_use
|
int | None
|
Number of leading dimensions to use. |
None
|
knn_params
|
KnnGpuDefaults | None
|
The GPU kNN parameters, |
None
|
seed
|
int
|
Seed for the index build. |
42
|
verbose
|
Verbose
|
|
True
|
backend
|
Backend
|
|
'wgpu'
|
Returns:
| Type | Description |
|---|---|
KnnGraph | None
|
The kNN, indices into the rows used; |
KnnGraph | None
|
embedding is absent. |
Raises:
| Type | Description |
|---|---|
BixverseError
|
Without a GPU adapter, or with |
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'
|
no_embd_to_use
|
int | None
|
Number of leading dimensions to use. |
None
|
modality
|
CacheModality
|
|
'rna'
|
knn_params
|
KnnGpuDefaults | None
|
The GPU kNN parameters. |
None
|
graph_params
|
KnnGraphDefaults | None
|
How the kNN becomes a graph. |
None
|
seed
|
int
|
Seed for the index build. |
42
|
verbose
|
Verbose
|
|
True
|
backend
|
Backend
|
|
'wgpu'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the embedding is stale. |
BixverseError
|
Without a GPU adapter, or with |
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'
|
res
|
float
|
Modularity resolution; higher gives more communities. |
1.0
|
name
|
str
|
The obs column to write. |
'leiden_clustering'
|
modality
|
Modality
|
The graph: |
'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
|
min_gain
|
float
|
With a negative |
0.0
|
local_moving
|
LocalMoving | None
|
Local moving on the coarser levels, |
None
|
node_order
|
NodeOrder | None
|
Visit order of the queue, |
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'
|
no_embd_to_use
|
int | None
|
Number of leading dimensions to use. |
None
|
resolutions
|
Sequence[float]
|
Modularity resolutions. |
(2.0, 1.0, 0.5)
|
fc_params
|
FastClusterGpuParams | None
|
The parameters. |
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
|
|
True
|
backend
|
Backend
|
|
'wgpu'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the embedding is absent, or on an empty resolution
list or |
BixverseError
|
Without a GPU adapter, or with |
References
Wei, et al., PLoS Comput Biol, 2022.