squidpy.gr.spatial_neighbors
- squidpy.gr.spatial_neighbors(adata, spatial_key='spatial', elements_to_coordinate_systems=None, table_key=None, library_key=None, coord_type=None, n_neighs=6, radius=None, delaunay=False, n_rings=1, percentile=None, transform=None, set_diag=False, key_added='spatial', copy=False)[source]
Create a graph from spatial coordinates.
- Parameters:
adata (
AnnData|SpatialData) – Annotated data object.spatial_key (
str) – Key inanndata.AnnData.obsmwhere spatial coordinates are stored. If adata is aspatialdata.SpatialData, the coordinates of the centroids will be stored in the adata with this key.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 be None if adata is aspatialdata.SpatialData.table_key (
str|None) – Key inspatialdata.SpatialData.tableswhere the spatialdata table is stored. Must not be None if adata is aspatialdata.SpatialData.mask_polygon – The Polygon or MultiPolygon element.
library_key (
str|None) – If multiple library_id, column inanndata.AnnData.obswhich stores mapping betweenlibrary_idand obs.coord_type (
str|CoordType|None) –Type of coordinate system. Valid options are:
’grid’ - grid coordinates.
’generic’ - generic coordinates.
None - ‘grid’ if
spatial_keyis inanndata.AnnData.unswithn_neighs = 6(Visium), otherwise use ‘generic’.
n_neighs (
int) –Depending on the
coord_type:’grid’ - number of neighboring tiles.
’generic’ - number of neighborhoods for non-grid data. Only used when
delaunay = False.
radius (
float|tuple[float,float] |None) –Only available when
coord_type = 'generic'. Depending on the type:delaunay (
bool) – Whether to compute the graph from Delaunay triangulation. Only used whencoord_type = 'generic'.n_rings (
int) – Number of rings of neighbors for grid data. Only used whencoord_type = 'grid'.percentile (
float|None) – Percentile of the distances to use as threshold. Only used whencoord_type = 'generic'.transform (
str|Transform|None) –Type of adjacency matrix transform. Valid options are:
’spectral’ - spectral transformation of the adjacency matrix.
’cosine’ - cosine transformation of the adjacency matrix.
None - no transformation of the adjacency matrix.
set_diag (
bool) – Whether to set the diagonal of the spatial connectivities to 1.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.
- 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.