Skip to content

Commit

Permalink
only use specified backend (or tifffile) to read mpp (#227)
Browse files Browse the repository at this point in the history
Previously, even if tiffslide was the chosen backend, openslide
would still be used first to get mpp. This could cause panics
if a slide could not be read by openslide. This panic could
confuse users because they asked to use tiffslide. This commit
changes the mpp reading function to use the chosen backend or
tifffile if that backend fails.
  • Loading branch information
kaczmarj authored Jul 11, 2024
1 parent 386a2a7 commit 06ef6b8
Showing 1 changed file with 10 additions and 4 deletions.
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

0 comments on commit 06ef6b8

Please sign in to comment.