rapids_singlecell.gr.spatial_neighbors_knn#

rapids_singlecell.gr.spatial_neighbors_knn(adata, *, spatial_key='spatial', table_key=None, elements_to_coordinate_systems=None, library_key=None, n_neighs=6, percentile=None, transform=None, set_diag=False, key_added='spatial', copy=False)[source]#

Build a directed kNN graph from adata.obsm[spatial_key].

n_neighs counts other observations, including coincident ones, and must be smaller than every library of the categorical library_key; libraries get independent graphs. percentile prunes distances above that percentile of each library, counting stored zeros such as the diagonal. Coincident neighbors stay connected but have no stored distance. transform rescales connectivities by column degrees ('spectral') or row similarity ('cosine') after set_diag adds self-loops. Graphs use float32. With copy=True, return SciPy CSR matrices; otherwise store '{key_added}_connectivities' and '{key_added}_distances' in adata.obsp and the parameters in adata.uns['{key_added}_neighbors'].

Parameters:
adata AnnData | SpatialData

Annotated data matrix, or a SpatialData object containing the selected table. SpatialData is optional and imported only for SpatialData input.

spatial_key str (default: 'spatial')

Key in adata.obsm containing coordinates. For SpatialData, transformed element centroids (x, y) are written here in table observation order.

table_key str | None (default: None)

Key in SpatialData.tables. Required for SpatialData input; ignored for AnnData input.

elements_to_coordinate_systems dict[str, str] | None (default: None)

Mapping from spatial element names to coordinate system names. Required for SpatialData input and must cover all regions annotated by the table. Graphs are built independently per region; ignored for AnnData input.

library_key str | None (default: None)

Categorical column in adata.obs defining independent libraries. For SpatialData, the table region key is used instead.

n_neighs int (default: 6)

Number of nearest other observations. Must be smaller than the number of observations in each library when using a kNN base graph.

percentile float | None (default: None)

Prune edges above this distance percentile (0 to 100) independently per library, after radius pruning. Stored zeros, including the diagonal, contribute to the percentile. None disables percentile pruning.

transform Literal['spectral', 'cosine'] | None (default: None)

Connectivity transform: None, "spectral" (normalization by column degrees), or "cosine" (similarity between connectivity rows). Applied after pruning and adding the requested diagonal; distances are unchanged.

set_diag bool (default: False)

Whether to set connectivity self-loops before transforming the graph. Distance diagonals are zero; cosine can create connectivity self-loops even when this is False.

key_added str (default: 'spatial')

Prefix for graph keys in adata.obsp and neighbor metadata in adata.uns. For SpatialData, results are stored in the selected table.

copy bool (default: False)

Return the graph matrices instead of storing graphs and metadata. For SpatialData, centroid coordinates are still written to the table.

Return type:

SpatialNeighborsResult | None

Returns:

SpatialNeighborsResult | None With copy=True, return connectivity and distance matrices as SciPy CSR matrices. Built-in builders store float32 connectivities and distances; Squidpy stores Euclidean distances as float64. Otherwise, return None and store '{key_added}_connectivities' and '{key_added}_distances' in adata.obsp, and graph parameters in adata.uns['{key_added}_neighbors']. For SpatialData, adata here denotes the selected table.