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 in anndata.AnnData.obsm where spatial coordinates are stored. If adata is a spatialdata.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 a spatialdata.SpatialData.

  • table_key (str | None) – Key in spatialdata.SpatialData.tables where the spatialdata table is stored. Must not be None if adata is a spatialdata.SpatialData.

  • mask_polygon – The Polygon or MultiPolygon element.

  • library_key (str | None) – If multiple library_id, column in anndata.AnnData.obs which stores mapping between library_id and obs.

  • coord_type (str | CoordType | None) –

    Type of coordinate system. Valid options are:

    • ’grid’ - grid coordinates.

    • ’generic’ - generic coordinates.

    • None - ‘grid’ if spatial_key is in anndata.AnnData.uns with n_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:

    • float - compute the graph based on neighborhood radius.

    • tuple - prune the final graph to only contain edges in interval [min(radius), max(radius)].

  • delaunay (bool) – Whether to compute the graph from Delaunay triangulation. Only used when coord_type = 'generic'.

  • n_rings (int) – Number of rings of neighbors for grid data. Only used when coord_type = 'grid'.

  • percentile (float | None) – Percentile of the distances to use as threshold. Only used when coord_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 if copy = False.

  • copy (bool) – If True, return the result, otherwise save it to the adata object.

Return type:

SpatialNeighborsResult | None

Returns:

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

Otherwise, modifies the adata with the following keys: