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

only use specified backend (or tifffile) to read mpp #227

Merged
merged 1 commit into from
Jul 11, 2024
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
14 changes: 10 additions & 4 deletions wsinfer/wsi.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ def _get_mpp_tiffslide(
slide_path: str | Path,
) -> tuple[float, float]:
"""Read MPP using TiffSlide."""
logger.debug("Attempting to read MPP using TiffSlide")

if not HAS_TIFFSLIDE:
logger.critical(
"Cannot read MPP with TiffSlide because TiffSlide is not available"
Expand Down Expand Up @@ -223,6 +225,7 @@ def _get_mpp_tiffslide(
# https://github.com/bayer-science-for-a-better-life/tiffslide/blob/8bea5a4c8e1429071ade6d4c40169ce153786d19/tiffslide/tiffslide.py#L712-L745
def _get_mpp_tifffile(slide_path: str | Path) -> tuple[float, float]:
"""Read MPP using Tifffile."""
logger.debug("Attempting to read MPP using tifffile")
with tifffile.TiffFile(slide_path) as tif:
series0 = tif.series[0]
page0 = series0[0]
Expand Down Expand Up @@ -267,20 +270,23 @@ def get_avg_mpp(slide_path: Path | str) -> float:
mppx: float
mppy: float

if HAS_OPENSLIDE:
if _BACKEND == "openslide":
try:
mppx, mppy = _get_mpp_openslide(slide_path)
return (mppx + mppy) / 2
except CannotReadSpacing:
# At this point, we want to continue to other implementations.
pass
if HAS_TIFFSLIDE:
if _BACKEND == "tiffslide":
try:
mppx, mppy = _get_mpp_tiffslide(slide_path)
return (mppx + mppy) / 2
except CannotReadSpacing:
# Our last hope to read the mpp is tifffile.
pass

logger.debug(f"Failed to read MPP using {_BACKEND}.")
logger.debug("Trying to read MPP with tifffile as last resort.")

# If tiffslide/openslide don't work, try tifffile.
try:
mppx, mppy = _get_mpp_tifffile(slide_path)
return (mppx + mppy) / 2
Expand Down
Loading