Skip to content
Merged
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
16 changes: 9 additions & 7 deletions monai/data/image_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
from itk import Image # type: ignore
from nibabel.nifti1 import Nifti1Image
from PIL import Image as PILImage

has_itk = has_nib = has_pil = True
else:
itk, _ = optional_import("itk", allow_namespace_pkg=True)
itk, has_itk = optional_import("itk", allow_namespace_pkg=True)
Image, _ = optional_import("itk", allow_namespace_pkg=True, name="Image")
nib, _ = optional_import("nibabel")
nib, has_nib = optional_import("nibabel")
Nifti1Image, _ = optional_import("nibabel.nifti1", name="Nifti1Image")
PILImage, _ = optional_import("PIL.Image")
PILImage, has_pil = optional_import("PIL.Image")


class ImageReader(ABC):
Expand Down Expand Up @@ -121,7 +123,7 @@ class ITKReader(ImageReader):
def __init__(self, **kwargs):
super().__init__()
self.kwargs = kwargs
if int(itk.Version.GetITKMajorVersion()) == 5 and int(itk.Version.GetITKMinorVersion()) < 2:
if has_itk and int(itk.Version.GetITKMajorVersion()) == 5 and int(itk.Version.GetITKMinorVersion()) < 2:
# warning the ITK LazyLoading mechanism was not threadsafe until version 5.2.0,
# requesting access to the itk.imread function triggers the lazy loading of the relevant itk modules
# before the parallel use of the function.
Expand All @@ -136,7 +138,7 @@ def verify_suffix(self, filename: Union[Sequence[str], str]) -> bool:
if a list of files, verify all the suffixes.

"""
return True
return has_itk

def read(self, data: Union[Sequence[str], str], **kwargs):
"""
Expand Down Expand Up @@ -307,7 +309,7 @@ def verify_suffix(self, filename: Union[Sequence[str], str]) -> bool:

"""
suffixes: Sequence[str] = ["nii", "nii.gz"]
return is_supported_format(filename, suffixes)
return has_nib and is_supported_format(filename, suffixes)

def read(self, data: Union[Sequence[str], str], **kwargs):
"""
Expand Down Expand Up @@ -522,7 +524,7 @@ def verify_suffix(self, filename: Union[Sequence[str], str]) -> bool:
if a list of files, verify all the suffixes.
"""
suffixes: Sequence[str] = ["png", "jpg", "bmp"]
return is_supported_format(filename, suffixes)
return has_pil and is_supported_format(filename, suffixes)

def read(self, data: Union[Sequence[str], str, np.ndarray], **kwargs):
"""
Expand Down
11 changes: 6 additions & 5 deletions monai/transforms/io/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,14 @@ def __init__(
if reader is not None:
if isinstance(reader, str):
supported_readers = {
"NibabelReader": NibabelReader,
"PILReader": PILReader,
"ITKReader": ITKReader,
"NumpyReader": NumpyReader,
"nibabelreader": NibabelReader,
"pilreader": PILReader,
"itkreader": ITKReader,
"numpyreader": NumpyReader,
}
reader = reader.lower()
if reader not in supported_readers:
raise ValueError(f"unsupported reader type: {reader}.")
raise ValueError(f"unsupported reader type: {reader}, available options: {supported_readers}.")
self.register(supported_readers[reader](*args, **kwargs))
else:
self.register(reader)
Expand Down
48 changes: 48 additions & 0 deletions tests/test_init_reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright 2020 MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

from monai.data import ITKReader, NibabelReader, NumpyReader, PILReader
from monai.transforms import LoadImage, LoadImaged


class TestInitLoadImage(unittest.TestCase):
def test_load_image(self):
instance1 = LoadImage(image_only=False, dtype=None)
instance2 = LoadImage(image_only=True, dtype=None)
self.assertIsInstance(instance1, LoadImage)
self.assertIsInstance(instance2, LoadImage)

for r in ["NibabelReader", "PILReader", "ITKReader", "NumpyReader", None]:
inst = LoadImaged("image", reader=r)
self.assertIsInstance(inst, LoadImaged)

def test_readers(self):
inst = ITKReader()
self.assertIsInstance(inst, ITKReader)

inst = NibabelReader()
self.assertIsInstance(inst, NibabelReader)
inst = NibabelReader(as_closest_canonical=True)
self.assertIsInstance(inst, NibabelReader)

inst = NumpyReader()
self.assertIsInstance(inst, NumpyReader)
inst = NumpyReader(npz_keys="test")
self.assertIsInstance(inst, NumpyReader)

inst = PILReader()
self.assertIsInstance(inst, PILReader)


if __name__ == "__main__":
unittest.main()