sparrow.im.rasterize

Contents

sparrow.im.rasterize#

sparrow.im.rasterize(sdata, shapes_layer, output_layer, out_shape=None, chunks=None, client=None, scale_factors=None, overwrite=False)#

Given a shapes layer in a SpatialData object, corresponding masks are created, and added as a labels layer to the SpatialData object.

The index of the shapes layer will be used as the label in the resulting labels layer (output_layer).

Parameters:
  • sdata (SpatialData) – The SpatialData object.

  • shapes_layer (str) – The shapes layer to be converted to a labels layer.

  • output_layer (str) – Name of the resulting labels layer that will be added to sdata.

  • out_shape (Optional[tuple[int, int]] (default: None)) – Output shape of the resulting labels layer (y,x). Will be automatically calculated if set to None via sdata[shapes_layer].geometry.total_bounds. If out_shape is not None, with x_min, y_min, x_max, y_max = sdata[shapes_layer].geometry.total_bounds, and out_shape[1]<x_max or out_shape[0]<y_max, then shapes with coordinates outside out_shapes will not be in resulting output_layer. For shapes_layer with large offset (y_min, x_min), we recommend translating the shapes to the origin, and add the offset via a translation (spatialdata.transformations.Translation).

  • chunks (Optional[int] (default: None)) – If provided, creation of the labels layer will be done in a chunked manner, with data divided into chunks for efficient computation.

  • client (Optional[Client] (default: None)) – A Dask client. If specified, a copy of sdata[shapes_layer] will be scattered across the workers, reducing the size of the task graph. If not specified, Dask will use the default scheduler as configured on your system.

  • client – A Dask Client instance. If specified, a copy of the GeoDataFrame (sdata[shapes_layer]) will be scattered across the workers. This reduces the size of the task graph and can improve performance by minimizing data transfer overhead during computation. If not specified, Dask will use the default scheduler as configured on your system (e.g., single-threaded, multithreaded, or a global client if one is running).

  • scale_factors (Optional[Sequence[Union[dict[str, int], int]]] (default: None)) – Scale factors to apply for multiscale.

  • overwrite (bool (default: False)) – If True, overwrites the output_layer if it already exists in sdata.

Return type:

SpatialData

Returns:

: An updated SpatialData object with the added labels layer.

Raises:
  • ValueError – If the provided shapes_layer contains 3D polygons.

  • ValueError – If the provided shapes_layer contains Points.

  • ValueError – If 0 is in the index of the shapes_layer. As 0 is used as background in the output_layer.

  • TypeError – If chunks is not None and not an instance of int.