squidpy.gr.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 ['{{key_added}}_{{spatial_key}}_connectivities'] and spatial coordinates stored in anndata.AnnData.obsp ['{{spatial_key}}'], it maskes 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) – 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) – Key in anndata.AnnData.obsm where spatial coordinates are stored.

  • 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:

SpatialData

Returns:

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.