spatial_neighbors_grid

spatial_neighbors_grid#

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

Create a grid-based graph from spatial coordinates.

This is the mode used for Visium-like grid coordinates. It assumes observations lie on an approximately regular lattice, so it is usually not appropriate for continuous coordinates such as Xenium point clouds. On irregular coordinates, the resulting graph and ring distances may not have a meaningful grid interpretation.

Parameters:
adata

Annotated data object.

spatial_key str (default: 'spatial')

Key in anndata.AnnData.obsm where spatial coordinates are stored.

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

A dictionary mapping element names of the SpatialData object to coordinate systems. The elements can be either Shapes or Labels. For compatibility, the spatialdata table must annotate all regions keys. Must not be None if adata is a spatialdata.SpatialData.

table_key str | None (default: None)

Key in spatialdata.SpatialData.tables where the spatialdata table is stored. Must not be None if adata is a spatialdata.SpatialData.

library_key str | None (default: None)

If multiple library_id, column in anndata.AnnData.obs which stores mapping between library_id and obs.

n_neighs int (default: 6)

Number of neighboring tiles used to form the base grid connectivity. Defaults to 6 for Visium-like grid coordinates.

n_rings int (default: 1)

Number of rings of neighbors. Defaults to 1. n_rings=1 keeps only immediate neighbors; larger values add progressively more distant shells and encode the shell number in dst. For example, n_neighs=3 with n_rings=2 on a Visium-like grid starts from a sparse three-neighbor base graph and then adds a second graph-distance ring relative to that base connectivity.

delaunay bool (default: False)

Whether to derive the base grid connectivity from a Delaunay triangulation. This is still grid mode: unlike spatial_neighbors_delaunay(), the resulting distance matrix encodes grid or ring distances rather than Euclidean edge lengths. In practice, this changes how the first-ring connectivity is inferred, but not the meaning of the resulting distances.

percentile

Percentile of the distances to use as threshold.

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

Adjacency matrix transform ('spectral', 'cosine', or None).

set_diag bool (default: False)

Whether to set the diagonal of the connectivities to 1.0.

key_added str (default: 'spatial')

Key which controls where the results are saved if copy = False.

copy bool (default: False)

If True, return the result, otherwise save it to the adata object.

n_jobs int (default: 1)

Number of parallel jobs used to build the per-library graphs when library_key is set. Each library’s graph is computed independently, so this only has an effect for multi-library data. 1 builds the graphs sequentially and does not change behavior, and it has no effect when library_key is None. None and -1 use numba’s default thread count (NUMBA_NUM_THREADS); asking for more threads than that warns and falls back to it, while 0 and values below -1 raise, since scanpy only supports n_jobs >= -1. Speedup is sub-linear (memory-bandwidth bound), so parallelism mainly pays off for many large libraries.

Returns:

SpatialNeighborsResult | None – If copy = True, returns a SpatialNeighborsResult with the spatial connectivities and distances matrices.

Otherwise, modifies the adata with the following keys:

See also

spatial_neighbors_from_builder

Use GridBuilder directly for advanced customization.

squidpy.gr.neighbors.GridBuilder

grid-based builder class.