Skip to content

Commit 0eb0536

Browse files
committed
fixes cucim compatibility
Signed-off-by: Wenqi Li <[email protected]>
1 parent ed2d12c commit 0eb0536

File tree

7 files changed

+22
-13
lines changed

7 files changed

+22
-13
lines changed

monai/data/image_reader.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,8 +673,10 @@ def __init__(self, reader_lib: str = "OpenSlide"):
673673
if has_osl:
674674
self.wsi_reader = openslide.OpenSlide
675675
elif self.reader_lib == "cucim":
676-
if has_cim:
676+
if has_cim and hasattr(cucim, "CuImage"):
677677
self.wsi_reader = cucim.CuImage
678+
else:
679+
raise ImportError("Please check the cuCIM installation.")
678680
else:
679681
raise ValueError('`reader_lib` should be either "cuCIM" or "OpenSlide"')
680682

requirements-dev.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ Sphinx==3.5.3
3131
recommonmark==0.6.0
3232
sphinx-autodoc-typehints==1.11.1
3333
sphinx-rtd-theme==0.5.2
34-
cucim~=0.19.0; platform_system == "Linux"
34+
cucim>=21.8.2; platform_system == "Linux"
3535
openslide-python==1.1.2
36+
imagecodecs; platform_system == "Linux"
37+
tifffile; platform_system == "Linux"
3638
pandas
3739
requests
3840
einops

setup.cfg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ all =
4040
tqdm>=4.47.0
4141
lmdb
4242
psutil
43-
cucim~=0.19.0
43+
cucim>=21.8.2
4444
openslide-python==1.1.2
4545
pandas
4646
einops
@@ -68,7 +68,7 @@ lmdb =
6868
psutil =
6969
psutil
7070
cucim =
71-
cucim~=0.19.0
71+
cucim>=21.8.2
7272
openslide =
7373
openslide-python==1.1.2
7474
pandas =

tests/test_cuimage_reader.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
from monai.data.image_reader import WSIReader
2222
from monai.utils import optional_import
2323

24-
_, has_cim = optional_import("cucim")
24+
cucim, has_cucim = optional_import("cucim")
25+
has_cucim = has_cucim and hasattr(cucim, "CuImage")
2526
PILImage, has_pil = optional_import("PIL.Image")
2627

2728
FILE_URL = "http://openslide.cs.cmu.edu/download/openslide-testdata/Generic-TIFF/CMU-1.tiff"
@@ -83,7 +84,7 @@
8384

8485

8586
class TestCuCIMReader(unittest.TestCase):
86-
@skipUnless(has_cim, "Requires CuCIM")
87+
@skipUnless(has_cucim, "Requires CuCIM")
8788
def setUp(self):
8889
download_url(FILE_URL, FILE_PATH, "5a3cfd4fd725c50578ddb80b517b759f")
8990

@@ -112,6 +113,7 @@ def test_read_patches(self, file_path, patch_info, expected_img):
112113

113114
@parameterized.expand([TEST_CASE_RGB_0, TEST_CASE_RGB_1])
114115
@skipUnless(has_pil, "Requires PIL")
116+
@skipUnless(has_cucim and cucim.__version__ == "0.19.0", "Skipped for cicum>0.19.0")
115117
def test_read_rgba(self, img_expected):
116118
image = {}
117119
reader = WSIReader("cuCIM")

tests/test_lesion_froc.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@
1919
from monai.apps.pathology.metrics import LesionFROC
2020
from monai.utils import optional_import
2121

22-
_, has_cucim = optional_import("cucim")
22+
_cucim, has_cucim = optional_import("cucim")
23+
has_cucim = has_cucim and hasattr(_cucim, "CuImage")
2324
_, has_skimage = optional_import("skimage.measure")
2425
_, has_sp = optional_import("scipy.ndimage")
25-
PILImage, has_pil = optional_import("PIL.Image")
26+
imwrite, has_tif = optional_import("tifffile", name="imwrite")
2627

2728

2829
def save_as_tif(filename, array):
2930
array = array[::-1, ...] # Upside-down
30-
img = PILImage.fromarray(array)
3131
if not filename.endswith(".tif"):
3232
filename += ".tif"
33-
img.save(os.path.join("tests", "testing_data", filename))
33+
file_path = os.path.join("tests", "testing_data", filename)
34+
imwrite(file_path, array, compress="jpeg", tile=(16, 16))
3435

3536

3637
def around(val, interval=3):
@@ -301,7 +302,7 @@ class TestEvaluateTumorFROC(unittest.TestCase):
301302
@skipUnless(has_cucim, "Requires cucim")
302303
@skipUnless(has_skimage, "Requires skimage")
303304
@skipUnless(has_sp, "Requires scipy")
304-
@skipUnless(has_pil, "Requires PIL")
305+
@skipUnless(has_tif, "Requires tifffile")
305306
def setUp(self):
306307
prepare_test_data()
307308

tests/test_patch_wsi_dataset.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
from monai.apps.utils import download_url
2222
from monai.utils import optional_import
2323

24-
_, has_cim = optional_import("cucim")
24+
_cucim, has_cim = optional_import("cucim")
25+
has_cim = has_cim and hasattr(_cucim, "CuImage")
2526
_, has_osl = optional_import("openslide")
2627

2728
FILE_URL = "http://openslide.cs.cmu.edu/download/openslide-testdata/Generic-TIFF/CMU-1.tiff"

tests/test_smartcache_patch_wsi_dataset.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
from monai.apps.utils import download_url
2222
from monai.utils import optional_import
2323

24-
_, has_cim = optional_import("cucim")
24+
_cucim, has_cim = optional_import("cucim")
25+
has_cim = has_cim and hasattr(_cucim, "CuImage")
2526

2627
FILE_URL = "http://openslide.cs.cmu.edu/download/openslide-testdata/Generic-TIFF/CMU-1.tiff"
2728
FILE_PATH = os.path.join(os.path.dirname(__file__), "testing_data", "temp_" + os.path.basename(FILE_URL))

0 commit comments

Comments
 (0)