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_neighscounts other observations, including coincident ones, and must be smaller than every library of the categoricallibrary_key; libraries get independent graphs.percentileprunes distances above that percentile of each library, counting stored zeros such as the diagonal. Coincident neighbors stay connected but have no stored distance.transformrescales connectivities by column degrees ('spectral') or row similarity ('cosine') afterset_diagadds self-loops. Graphs use float32. Withcopy=True, return SciPy CSR matrices; otherwise store'{key_added}_connectivities'and'{key_added}_distances'inadata.obspand the parameters inadata.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.obsmcontaining 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.obsdefining 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.
Nonedisables 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.obspand neighbor metadata inadata.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.
- adata
- Return type:
- 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, returnNoneand store'{key_added}_connectivities'and'{key_added}_distances'inadata.obsp, and graph parameters inadata.uns['{key_added}_neighbors']. For SpatialData,adatahere denotes the selected table.