rapids_singlecell.gr.spatial_neighbors_grid#

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

Build a lattice graph whose distances count shortest directed hops.

The kNN base keeps edges shorter than 1.3 times the median distance; use n_neighs=6 for hexagonal and 4 for square grids. n_rings adds hops; delaunay=True uses Delaunay edges as the base instead.

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.

n_rings int (default: 1)

Number of grid hops to include. Distances count shortest directed hops.

delaunay bool (default: False)

Use Delaunay edges as the grid base instead of kNN edges filtered to distances below 1.3 times the per-library median.

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 use float32. 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.