rapids_singlecell.gr.spatial_neighbors_from_builder#

rapids_singlecell.gr.spatial_neighbors_from_builder(adata, builder, *, spatial_key='spatial', table_key=None, elements_to_coordinate_systems=None, library_key=None, key_added='spatial', copy=False)[source]#

Build spatial graphs with a built-in or custom builder.

builder.build(coords) receives finite float32 CuPy coordinates and returns connectivity and distance matrices; uns_params() supplies the metadata. With library_key, combine(graphs, indices) merges the per-library graphs in observation order. Builders from rapids_singlecell.gr.neighbors with built-in postprocessors build all libraries in one pass.

Parameters:
adata AnnData | SpatialData

Annotated data matrix, or a SpatialData object containing the selected table. SpatialData is optional and imported only for SpatialData input.

builder GraphBuilder

Graph construction strategy. build(coords) receives finite float32 CuPy coordinates and returns connectivity and distance matrices. uns_params() supplies metadata; combine(graphs, indices) must support independent libraries when library_key is used.

spatial_key str (default: 'spatial')

Key in adata.obsm containing coordinates. For SpatialData, transformed element centroids (x, y) are written here in table observation order.

table_key str | None (default: None)

Key in SpatialData.tables. Required for SpatialData input; ignored for AnnData input.

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

Mapping from spatial element names to coordinate system names. Required for SpatialData input and must cover all regions annotated by the table. Graphs are built independently per region; ignored for AnnData input.

library_key str | None (default: None)

Categorical column in adata.obs defining independent libraries. For SpatialData, the table region key is used instead.

key_added str (default: 'spatial')

Prefix for graph keys in adata.obsp and neighbor metadata in adata.uns. For SpatialData, results are stored in the selected table.

copy bool (default: False)

Return the graph matrices instead of storing graphs and metadata. For SpatialData, centroid coordinates are still written to the table.

Return type:

SpatialNeighborsResult | None

Returns:

SpatialNeighborsResult | None With copy=True, return connectivity and distance matrices as SciPy CSR matrices. Built-in builders store float32 connectivities and distances; Squidpy stores Euclidean distances as float64. Otherwise, return None and store '{key_added}_connectivities' and '{key_added}_distances' in adata.obsp, and graph parameters in adata.uns['{key_added}_neighbors']. For SpatialData, adata here denotes the selected table.