Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieve MPP from WSIs #432

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/eva/vision/data/wsi/backends/openslide.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,26 @@ def level_downsamples(self) -> Sequence[float]:
@override
def mpp(self) -> float:
# TODO: add overwrite_mpp class attribute to allow setting a default value
x_mpp = float(self._wsi.properties["openslide.mpp-x"])
y_mpp = float(self._wsi.properties["openslide.mpp-y"])
if self._wsi.properties.get(openslide.PROPERTY_NAME_MPP_X) and self._wsi.properties.get(
openslide.PROPERTY_NAME_MPP_Y
):
x_mpp = float(self._wsi.properties[openslide.PROPERTY_NAME_MPP_X])
y_mpp = float(self._wsi.properties[openslide.PROPERTY_NAME_MPP_Y])
elif (
self._wsi.properties.get("tiff.XResolution")
roman807 marked this conversation as resolved.
Show resolved Hide resolved
and self._wsi.properties.get("tiff.YResolution")
and self._wsi.properties.get("tiff.ResolutionUnit")
):
unit = self._wsi.properties.get("tiff.ResolutionUnit")
if unit not in _conversion_factor_to_micrometer:
raise ValueError(f"Unit {unit} not supported.")

conversion_factor = float(_conversion_factor_to_micrometer.get(unit)) # type: ignore
x_mpp = conversion_factor / float(self._wsi.properties["tiff.XResolution"])
y_mpp = conversion_factor / float(self._wsi.properties["tiff.YResolution"])
else:
raise ValueError("`mpp` cannot be obtained for this slide.")

return (x_mpp + y_mpp) / 2.0

@override
Expand All @@ -58,3 +76,15 @@ def read_region(
data[data[:, :, 3] == 0] = 255

return data[:, :, :3]


_conversion_factor_to_micrometer = {
"meter": 10**6,
"decimeter": 10**5,
"centimeter": 10**4,
"millimeter": 10**3,
"micrometer": 1,
"nanometer": 10**-3,
"picometer": 10**-6,
"femtometer": 10**-9,
}