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

Add MIRAX fixture #54

Closed
Closed
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ docs/build/**
htmlcov/**
*.egg-info/**

.venv/
venv/

**/tests/data
tiffslide/_version.py

Expand Down
3 changes: 2 additions & 1 deletion tiffslide/_kerchunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from io import StringIO
from typing import TYPE_CHECKING
from typing import Any
from typing import Union

if sys.version_info >= (3, 8):
from typing import TypedDict
Expand Down Expand Up @@ -118,7 +119,7 @@ def to_kerchunk(


def from_kerchunk(
kc: KerchunkSpec,
kc: Union[KerchunkSpec, dict[str, Any]],
*,
urlpath: str | None = None,
storage_options: dict[str, Any] | None = None,
Expand Down
51 changes: 48 additions & 3 deletions tiffslide/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import shutil
import sys
import urllib.request
import zipfile
from itertools import cycle
from itertools import groupby
from itertools import islice
Expand All @@ -19,7 +20,7 @@
from imagecodecs import imwrite

# openslide aperio test images
IMAGES_BASE_URL = "http://openslide.cs.cmu.edu/download/openslide-testdata/Aperio/"
IMAGES_BASE_URL = "http://openslide.cs.cmu.edu/download/openslide-testdata/"

try:
APERIO_JP2000_RGB = tifffile.COMPRESSION.APERIO_JP2000_RGB
Expand All @@ -40,6 +41,7 @@ class TestImageType(enum.Enum):
DOWNLOAD_SMALLEST_CMU = enum.auto()
GENERATE_PYRAMIDAL_IMG = enum.auto()
GENERATE_PYRAMIDAL_1CH_16B_SVS = enum.auto()
DOWNLOAD_MIRAX_CMU = enum.auto()


def _wsi_files():
Expand Down Expand Up @@ -93,6 +95,11 @@ def roundrobin(iterables):
TestImageType.GENERATE_PYRAMIDAL_1CH_16B_SVS,
id="generated-pyramidal-1ch-16b-svs",
),
pytest.param(
TestImageType.DOWNLOAD_MIRAX_CMU,
id="CMU-1.mrxs",
marks=pytest.mark.mirax,
),
]
return paths

Expand Down Expand Up @@ -261,16 +268,46 @@ def wsi_file(request, tmp_path_factory):

if not img_fn.is_file():
# download svs from openslide test images
url = IMAGES_BASE_URL + small_image
url = IMAGES_BASE_URL + "Aperio/" + small_image
with urllib.request.urlopen(url) as response, open(
img_fn, "wb"
) as out_file:
shutil.copyfileobj(response, out_file)

if md5(img_fn) != small_image_md5: # pragma: no cover
shutil.rmtree(img_fn)
os.remove(img_fn)
pytest.fail("incorrect md5")

elif request.param == TestImageType.DOWNLOAD_MIRAX_CMU:
mirax_image = "CMU-1.mrxs"
mirax_image_zip = "CMU-1.zip"
mirax_image_md5 = "1b82dc0364441c1f6ac9a253400f9b02"
data_dir = pathlib.Path(__file__).parent / "data"

data_dir.mkdir(parents=True, exist_ok=True)
zip_fn = data_dir / mirax_image_zip

if not zip_fn.is_file():
# download zip from openslide test images
url = IMAGES_BASE_URL + "Mirax/" + mirax_image_zip
with urllib.request.urlopen(url) as response, open(
zip_fn, "wb"
) as out_file:
shutil.copyfileobj(response, out_file)

print("file md5", md5(zip_fn), mirax_image_md5)
if md5(zip_fn) != mirax_image_md5: # pragma: no cover
# os.remove(zip_fn)
pytest.fail("incorrect md5")

img_fn = data_dir / mirax_image
if not img_fn.is_file():
with zipfile.ZipFile(zip_fn, "r") as zip_ref:
zip_ref.extractall(data_dir)

if not img_fn.exists():
pytest.fail("image file was not extracted")

elif request.param == TestImageType.GENERATE_PYRAMIDAL_IMG:
img_fn = tmp_path_factory.mktemp("_generated_test_tiffs").joinpath(
"_small_pyramid.tiff"
Expand Down Expand Up @@ -378,3 +415,11 @@ def svs_small_props():
'tiffslide.quickhash-1': None,
'tiffslide.vendor': 'aperio',
} # fmt: skip


def pytest_collection_modifyitems(config, items):
# skip unsupported Mirax tests
skip_unsupported_mirax = pytest.mark.skip(reason="not supported for Mirax")
for item in items:
if "mirax" in item.keywords and "support_mirax" not in item.keywords:
item.add_marker(skip_unsupported_mirax)
11 changes: 10 additions & 1 deletion tiffslide/tests/test_tiffslide.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@
import tiffslide
from tiffslide import TiffFileError
from tiffslide import TiffSlide
from tiffslide._kerchunk import from_kerchunk
from tiffslide._mirax import Mirax


@pytest.fixture
def slide(wsi_file):
yield TiffSlide(wsi_file)
if wsi_file.suffix == ".mrxs":
mrxs = Mirax(wsi_file)
kc = mrxs.build_reference()
yield from_kerchunk(kc)
else:
yield TiffSlide(wsi_file)


def test_image_detect_format(wsi_file):
Expand Down Expand Up @@ -107,10 +114,12 @@ def test_image_get_best_level_for_downsample(slide):
assert lvl == lvl_new


@pytest.mark.support_mirax
def test_image_read_region(slide):
assert slide.read_region((0, 0), 0, (2220, 2967)).size == (2220, 2967)


@pytest.mark.support_mirax
def test_image_read_region_as_array(slide):
assert slide.read_region((0, 0), 0, (2220, 2967), as_array=True).shape[:2] == (
2967,
Expand Down