Skip to content

ratiopath.tiling.read_slide_tiles

read_openslide_tiles(path, **kwargs)

Read batch of tiles from a whole-slide image using OpenSlide.

Source code in ratiopath/tiling/read_slide_tiles.py
19
20
21
22
def read_openslide_tiles(path: str, **kwargs: Unpack[ReadTilesArguments]) -> np.ndarray:
    """Read batch of tiles from a whole-slide image using OpenSlide."""
    with OpenSlide(path) as slide:
        return _read_openslide_tiles(slide, **kwargs)

read_slide_tiles(path, tile_x, tile_y, tile_extent_x, tile_extent_y, level)

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

Parameters:

Name Type Description Default
path StringArray

A pyarrow array of whole-slide image paths.

required
tile_x IntegerArray

A pyarrow array of tile x-coordinates.

required
tile_y IntegerArray

A pyarrow array of tile y-coordinates.

required
tile_extent_x IntegerArray

A pyarrow array of tile extents in the x-dimension.

required
tile_extent_y IntegerArray

A pyarrow array of tile extents in the y-dimension.

required
level IntegerArray

A pyarrow array of slide levels to read the tiles from.

required

Returns:

Type Description
Array

A pyarrow array of numpy arrays containing the read tiles.

Source code in ratiopath/tiling/read_slide_tiles.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
@udf(return_dtype=DataType(np.ndarray))
def read_slide_tiles(
    path: pa.StringArray,
    tile_x: pa.IntegerArray,
    tile_y: pa.IntegerArray,
    tile_extent_x: pa.IntegerArray,
    tile_extent_y: pa.IntegerArray,
    level: pa.IntegerArray,
) -> pa.Array:
    """Reads a batch of tiles from a whole-slide image using either OpenSlide or tifffile.

    Args:
        path: A pyarrow array of whole-slide image paths.
        tile_x: A pyarrow array of tile x-coordinates.
        tile_y: A pyarrow array of tile y-coordinates.
        tile_extent_x: A pyarrow array of tile extents in the x-dimension.
        tile_extent_y: A pyarrow array of tile extents in the y-dimension.
        level: A pyarrow array of slide levels to read the tiles from.

    Returns:
        A pyarrow array of numpy arrays containing the read tiles.
    """
    import pyarrow.compute as pc

    tiles = np.empty(len(tile_x), dtype=object)

    for p, group in _pyarrow_group_indices(path).items():
        assert isinstance(p, str)

        kwargs = {
            "tile_x": pc.take(tile_x, group),
            "tile_y": pc.take(tile_y, group),
            "tile_extent_x": pc.take(tile_extent_x, group),
            "tile_extent_y": pc.take(tile_extent_y, group),
            "level": pc.take(level, group),
        }

        # Check if it's an OME-TIFF file
        if p.lower().endswith((".ome.tiff", ".ome.tif")):
            tiles[group] = list(read_tifffile_tiles(p, **kwargs))
        else:
            tiles[group] = list(read_openslide_tiles(p, **kwargs))

    return pa.array(TensorArray(tiles))

read_tifffile_tiles(path, **kwargs)

Read batch of tiles from an OME-TIFF file using tifffile.

Source code in ratiopath/tiling/read_slide_tiles.py
25
26
27
28
def read_tifffile_tiles(path: str, **kwargs: Unpack[ReadTilesArguments]) -> np.ndarray:
    """Read batch of tiles from an OME-TIFF file using tifffile."""
    with TiffFile(path) as slide:
        return _read_tifffile_tiles(slide, **kwargs)