make_tiles

make_tiles#

squidpy.experimental.im.make_tiles(sdata, *, image_key, image_mask_key=None, tissue_mask_key=None, tile_size=(224, 224), center_grid_on_tissue=False, scale='auto', min_tissue_fraction=1.0, new_shapes_key=None, preview=True)[source]#

Create a regular grid of tiles over an image, classify them by tissue coverage, and optionally render a preview.

Tiles are generated on the highest-resolution image scale and classified into three categories based on the supplied (or automatically derived) tissue mask:

  • "background": no tissue pixels in the tile.

  • "partial_tissue": some tissue but below min_tissue_fraction.

  • "tissue": at least min_tissue_fraction of the tile is tissue.

The resulting grid is stored in sdata.shapes[new_shapes_key] (default f"{image_key}_tiles") with one row per tile and columns such as pixel_y0, pixel_x0, pixel_y1, pixel_x1, and tile_classification.

Parameters:
sdata SpatialData

SpatialData object containing the image and (optionally) label masks.

image_key str

Key of the image in sdata.images to tile.

image_mask_key str | None (default: None)

Optional key of a segmentation or tissue mask in sdata.labels used purely to position the grid. If center_grid_on_tissue is True and this is not provided, the function will fall back to tissue_mask_key or f"{image_key}_tissue" if present, or automatically run detect_tissue() to create one.

tissue_mask_key str | None (default: None)

Key of the tissue mask in sdata.labels used for classification. If None, the function uses f"{image_key}_tissue" and will automatically run detect_tissue() to create this mask if it does not exist.

tile_size tuple[int, int] (default: (224, 224))

Size of tiles as (height, width) in pixels on the largest image scale.

center_grid_on_tissue bool (default: False)

If True, center the tile grid on the centroid of the mask given by image_mask_key / tissue_mask_key / f"{image_key}_tissue" (created on the fly if needed). If False, the grid starts at the top-left corner of the image.

scale str (default: 'auto')

Label scale to use when reading the mask for centering and classification. If "auto", the function chooses the label scale whose shape is closest to the full-resolution image dimensions.

min_tissue_fraction float (default: 1.0)

Minimum fraction of a tile that must be tissue for it to be considered suitable for inference. Tiles below this threshold are classified as "background" (0%) or "partial_tissue" (>0%).

new_shapes_key str | None (default: None)

Key under which to store the tile grid in sdata.shapes. Defaults to f"{image_key}_tiles".

preview bool (default: True)

If True, render a preview using sdata.pl.render_images(image_key) overlaid with the tiles colored by tile_classification.

Returns:

None – None All results are written in-place to sdata.shapes.

See also

detect_tissue

Helper used to automatically derive a tissue mask when none is provided.

make_tiles_from_spots

Create tiles centered on Visium spots instead of a regular grid.