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 (
Optional
[tuple
[int
,int
]] (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_shape
is notNone
, withx_min, y_min, x_max, y_max = sdata[shapes_layer].geometry.total_bounds
, andout_shape[1]<x_max
orout_shape[0]<y_max
, then shapes with coordinates outsideout_shapes
will not be in resultingoutput_layer
. Forshapes_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
)) – ADask
client. If specified, a copy ofsdata[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 theoutput_layer
if it already exists insdata
.
- 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 theoutput_layer
.TypeError – If
chunks
is notNone
and not an instance ofint
.