mask_graph

mask_graph#

squidpy.gr.mask_graph(sdata, *, table_key, polygon_mask, negative_mask=False, spatial_key='spatial', key_added='mask', copy=False)[source]#

Mask the graph based on a polygon mask.

Given a spatial graph stored in anndata.AnnData.obsp ['{spatial_key}_connectivities'] and spatial coordinates stored in anndata.AnnData.obsm ['{spatial_key}'], it masks the graph so that only edges fully contained in the polygons are kept.

Parameters:
sdata SpatialData

The spatial data object.

table_key str

The key of the table containing the spatial data.

polygon_mask Polygon | MultiPolygon

The shapely.Polygon or shapely.MultiPolygon to be used as mask.

negative_mask bool (default: False)

Whether to keep the edges within the polygon mask or outside. Note that when negative_mask = True, only the edges fully contained in the polygon are removed. If edges are partially contained in the polygon, they are kept.

spatial_key str (default: 'spatial')

Key in anndata.AnnData.obsm where spatial coordinates are stored.

key_added str (default: 'mask')

Key which controls where the results are saved if copy = False.

copy bool (default: False)

If True, return the result, otherwise save it to the adata object.

Returns:

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

Otherwise, modifies the adata with the following keys:

Notes

The polygon_mask must be in the same coordinate_systems of the spatial graph, but no check is performed to assess this.