squidpy.gr.spatial_neighbors_from_builder
- squidpy.gr.spatial_neighbors_from_builder(data, builder, *, spatial_key='spatial', elements_to_coordinate_systems=None, table_key=None, library_key=None, key_added='spatial', copy=False, n_jobs=1)[source]
Create a graph from spatial coordinates using an explicit builder instance.
This function is the bridge between the high-level API (e.g.,
spatial_neighbors_knn(),spatial_neighbors_radius()) and advanced customization via builder classes. Use this when you need to:Stack or chain builder behaviors
Pass pre-configured builder instances multiple times
Implement custom builders (see Extensibility)
- Parameters:
adata – Annotated data object.
builder (
GraphBuilder[Any,Any]) – Graph construction strategy to execute. Built-in builders subclass {{class}}`~squidpy.gr.neighbors.GraphBuilderCSR`, while custom backends can implement the more generic {{class}}`~squidpy.gr.neighbors.GraphBuilder` interface directly. Reusable post-build operations are also exposed viaDistanceIntervalPostprocessor,PercentilePostprocessor, andTransformPostprocessor. Custom builders only need to implement multi-library support when usinglibrary_key; otherwise leavingcombine()unimplemented is fine.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.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_knnk-nearest-neighbor graphs (wraps
KNNBuilder).spatial_neighbors_radiusradius-based graphs (wraps
RadiusBuilder).spatial_neighbors_delaunayDelaunay triangulation graphs (wraps
DelaunayBuilder).spatial_neighbors_gridgrid-based graphs (wraps
GridBuilder).squidpy.gr.neighbors.GraphBuilderBase builder interface. Inherit from this or
GraphBuilderCSRto implement custom graph construction.