Skip to content

Utils

closest_level(mpp, slide_mpp, downsamples)

Finds the closest downsample level to the given MPP (microns per pixel) value.

Parameters:

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

The MPP value(s) to match.

required
slide_mpp float | tuple[float, float]

The MPP value(s) of the slide.

required
downsamples list[float]

A list of available downsample levels.

required

Returns:

Type Description
int

The index of the closest downsample level.

Source code in ratiopath/utils/closest_level.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
def closest_level(
    mpp: float | tuple[float, float],
    slide_mpp: float | tuple[float, float],
    downsamples: list[float],
) -> int:
    """Finds the closest downsample level to the given MPP (microns per pixel) value.

    Args:
        mpp: The MPP value(s) to match.
        slide_mpp: The MPP value(s) of the slide.
        downsamples: A list of available downsample levels.

    Returns:
        The index of the closest downsample level.
    """
    scale_factor = np.mean(np.asarray(mpp) / np.asarray(slide_mpp))

    return np.abs(np.asarray(downsamples) - scale_factor).argmin().item()