Skip to content

ratiopath.openslide.OpenSlide

Bases: OpenSlide

A wrapper around the OpenSlide library to provide additional functionality.

Source code in ratiopath/openslide.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
class OpenSlide(openslide.OpenSlide):
    """A wrapper around the OpenSlide library to provide additional functionality."""

    def closest_level(self, mpp: float | tuple[float, float]) -> int:
        """Finds the closest slide level to match the desired MPP.

        This method compares the desired MPP (µm/px) with the MPP of the
        available levels in the slide and selects the level with the closest match.

        Args:
            mpp: The desired µm/px value.

        Returns:
            The index of the level with the closest µm/px resolution to the desired value.
        """
        return closest_level(
            mpp,
            (
                float(self.properties[PROPERTY_NAME_MPP_X]),
                float(self.properties[PROPERTY_NAME_MPP_Y]),
            ),
            list(self.level_downsamples),
        )

    def slide_resolution(self, level: int) -> tuple[float, float]:
        """Returns the resolution of the slide in µm/px at the given level.

        Args:
            level: The level of the slide to calculate the resolution.

        Returns:
            The [x, y] resolution of the slide in µm/px.
        """
        slide_mpp_x = float(self.properties[PROPERTY_NAME_MPP_X])
        slide_mpp_y = float(self.properties[PROPERTY_NAME_MPP_Y])

        return (
            slide_mpp_x * self.level_downsamples[level],
            slide_mpp_y * self.level_downsamples[level],
        )

    def read_region_relative(
        self, location: tuple[int, int], level: int, size: tuple[int, int]
    ) -> Image:
        """Reads a region from the slide with coordinates relative to the specified level.

        This method adjusts the coordinates based on the level's downsampling factor
        before reading the region from the slide.

        Args:
            location: The (x, y) coordinates at the specified level.
            level: The level of the slide to read from.
            size: The (width, height) of the region to read.

        Returns:
            The image of the requested region.
        """
        downsample = self.level_downsamples[level]
        location = (int(location[0] * downsample), int(location[1] * downsample))

        return super().read_region(location, level, size)

closest_level(mpp)

Finds the closest slide level to match the desired MPP.

This method compares the desired MPP (µm/px) with the MPP of the available levels in the slide and selects the level with the closest match.

Parameters:

Name Type Description Default
mpp float | tuple[float, float]

The desired µm/px value.

required

Returns:

Type Description
int

The index of the level with the closest µm/px resolution to the desired value.

Source code in ratiopath/openslide.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def closest_level(self, mpp: float | tuple[float, float]) -> int:
    """Finds the closest slide level to match the desired MPP.

    This method compares the desired MPP (µm/px) with the MPP of the
    available levels in the slide and selects the level with the closest match.

    Args:
        mpp: The desired µm/px value.

    Returns:
        The index of the level with the closest µm/px resolution to the desired value.
    """
    return closest_level(
        mpp,
        (
            float(self.properties[PROPERTY_NAME_MPP_X]),
            float(self.properties[PROPERTY_NAME_MPP_Y]),
        ),
        list(self.level_downsamples),
    )

read_region_relative(location, level, size)

Reads a region from the slide with coordinates relative to the specified level.

This method adjusts the coordinates based on the level's downsampling factor before reading the region from the slide.

Parameters:

Name Type Description Default
location tuple[int, int]

The (x, y) coordinates at the specified level.

required
level int

The level of the slide to read from.

required
size tuple[int, int]

The (width, height) of the region to read.

required

Returns:

Type Description
Image

The image of the requested region.

Source code in ratiopath/openslide.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
def read_region_relative(
    self, location: tuple[int, int], level: int, size: tuple[int, int]
) -> Image:
    """Reads a region from the slide with coordinates relative to the specified level.

    This method adjusts the coordinates based on the level's downsampling factor
    before reading the region from the slide.

    Args:
        location: The (x, y) coordinates at the specified level.
        level: The level of the slide to read from.
        size: The (width, height) of the region to read.

    Returns:
        The image of the requested region.
    """
    downsample = self.level_downsamples[level]
    location = (int(location[0] * downsample), int(location[1] * downsample))

    return super().read_region(location, level, size)

slide_resolution(level)

Returns the resolution of the slide in µm/px at the given level.

Parameters:

Name Type Description Default
level int

The level of the slide to calculate the resolution.

required

Returns:

Type Description
tuple[float, float]

The [x, y] resolution of the slide in µm/px.

Source code in ratiopath/openslide.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def slide_resolution(self, level: int) -> tuple[float, float]:
    """Returns the resolution of the slide in µm/px at the given level.

    Args:
        level: The level of the slide to calculate the resolution.

    Returns:
        The [x, y] resolution of the slide in µm/px.
    """
    slide_mpp_x = float(self.properties[PROPERTY_NAME_MPP_X])
    slide_mpp_y = float(self.properties[PROPERTY_NAME_MPP_Y])

    return (
        slide_mpp_x * self.level_downsamples[level],
        slide_mpp_y * self.level_downsamples[level],
    )