squidpy.gr.spatial_neighbors

squidpy.gr.spatial_neighbors(adata, spatial_key='spatial', 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.

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

  • coord_type (Union[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 (Union[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 (Optional[float]) – Percentile of the distances to use as threshold. Only used when coord_type = 'generic'.

  • transform (Union[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:

Optional[tuple[csr_matrix, csr_matrix]]

Returns:

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

Otherwise, modifies the adata with the following keys: