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 tosdata.out_shape (
tuple[int,int] |None(default:None)) – Output shape of the resulting labels layer(y,x). Will be automatically calculated if set to None viasdata[shapes_layer].geometry.total_bounds. Ifout_shapeis notNone, withx_min, y_min, x_max, y_max = sdata[shapes_layer].geometry.total_bounds, andout_shape[1]<x_maxorout_shape[0]<y_max, then shapes with coordinates outsideout_shapeswill not be in resultingoutput_layer. Forshapes_layerwith 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 (
int|None(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 (
Client|None(default:None)) – ADaskclient. If specified, a copy ofsdata[shapes_layer]will be scattered across the workers, reducing the size of the task graph. If not specified,Daskwill use the default scheduler as configured on your system.client – A Dask
Clientinstance. 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 (
Sequence[dict[str,int] |int] |None(default:None)) – Scale factors to apply for multiscale.overwrite (
bool(default:False)) – If True, overwrites theoutput_layerif it already exists insdata.
- Return type:
SpatialData- Returns:
: An updated SpatialData object with the added labels layer.
- Raises:
ValueError – If the provided
shapes_layercontains 3D polygons.ValueError – If the provided
shapes_layercontains Points.ValueError – If 0 is in the index of the
shapes_layer. As 0 is used as background in theoutput_layer.TypeError – If
chunksis notNoneand not an instance ofint.