squidpy.gr.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) – Key inanndata.AnnData.obsmwhere spatial coordinates are stored.elements_to_coordinate_systems (
dict[str,str] |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 beNoneifadatais aspatialdata.SpatialData.table_key (
str|None) – Key inspatialdata.SpatialData.tableswhere the spatialdata table is stored. Must not beNoneifadatais aspatialdata.SpatialData.library_key (
str|None) – If multiple library_id, column inanndata.AnnData.obswhich stores mapping betweenlibrary_idand obs.n_neighs (
int) – Number of neighboring tiles used to form the base grid connectivity. Defaults to6for Visium-like grid coordinates.n_rings (
int) – Number of rings of neighbors. Defaults to1.n_rings=1keeps only immediate neighbors; larger values add progressively more distant shells and encode the shell number indst. For example,n_neighs=3withn_rings=2on 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) – Whether to derive the base grid connectivity from a Delaunay triangulation. This is still grid mode: unlikespatial_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 (
str|Transform|None) – Adjacency matrix transform ('spectral','cosine', orNone).set_diag (
bool) – Whether to set the diagonal of the connectivities to1.0.key_added (
str) – Key which controls where the results are saved ifcopy = False.copy (
bool) – IfTrue, return the result, otherwise save it to theadataobject.n_jobs (
int) – Number of parallel jobs used to build the per-library graphs whenlibrary_keyis set. Each library’s graph is computed independently, so this only has an effect for multi-library data.1(default) builds the graphs sequentially and does not change behavior;-1uses all available CPUs. Has no effect whenlibrary_keyisNone. Speedup is sub-linear (memory-bandwidth bound), and process-based backends pay a one-time worker start-up cost, so parallelism mainly pays off for many large libraries.
- Return type:
- Returns:
If
copy = True, returns aSpatialNeighborsResultwith the spatial connectivities and distances matrices.Otherwise, modifies the
adatawith the following keys:anndata.AnnData.obsp['{key_added}_connectivities']- the spatial connectivities.anndata.AnnData.obsp['{key_added}_distances']- the spatial distances.anndata.AnnData.uns['{key_added}']-dictcontaining parameters.
See also
spatial_neighbors_from_builderUse
GridBuilderdirectly for advanced customization.squidpy.gr.neighbors.GridBuildergrid-based builder class.