Skip to content

ratiopath.tiling.read_slide_tiles

read_slide_tiles(batch)

Reads a batch of tiles from a whole-slide image using either OpenSlide or tifffile.

Parameters:

Name Type Description Default
batch dict[str, Any]
  • tile_x: X coordinates of tiles relative to the level
  • tile_y: Y coordinates of tiles relative to the level
  • level: Pyramid levels
  • tile_extent_x: Widths of the tiles
  • tile_extent_y: Heights of the tiles
required

Returns:

Type Description
dict[str, Any]

The input batch with an added tile key containing the list of numpy array tiles.

Source code in ratiopath/tiling/read_slide_tiles.py
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
def read_slide_tiles(batch: dict[str, Any]) -> dict[str, Any]:
    """Reads a batch of tiles from a whole-slide image using either OpenSlide or tifffile.

    Args:
        batch:
            - tile_x: X coordinates of tiles relative to the level
            - tile_y: Y coordinates of tiles relative to the level
            - level: Pyramid levels
            - tile_extent_x: Widths of the tiles
            - tile_extent_y: Heights of the tiles

    Returns:
        The input batch with an added `tile` key containing the list of numpy array tiles.
    """
    # Check if it's an OME-TIFF file
    df = pd.DataFrame(batch)
    for path, group in df.groupby("path"):
        if str(path).lower().endswith((".ome.tiff", ".ome.tif")):
            df.loc[group.index, "tile"] = _read_tifffile_tiles(path, group)
        else:
            df.loc[group.index, "tile"] = _read_openslide_tiles(path, group)

    batch["tile"] = df["tile"].tolist()
    return batch