Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions monai/transforms/io/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,10 @@ def __init__(
the_reader = look_up_option(_r.lower(), SUPPORTED_READERS)
try:
self.register(the_reader(*args, **kwargs))
except OptionalImportError:
warnings.warn(
except OptionalImportError as e:
raise OptionalImportError(
f"required package for reader {_r} is not installed, or the version doesn't match requirement."
)
) from e
except TypeError: # the reader doesn't have the corresponding args/kwargs
warnings.warn(f"{_r} is not supported with the given parameters {args} {kwargs}.")
self.register(the_reader())
Expand Down
13 changes: 13 additions & 0 deletions tests/transforms/test_load_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,5 +498,18 @@ def test_correct(self, input_param, expected_shape, track_meta):
self.assertFalse(hasattr(r, "affine"))


class TestLoadImageReaderNotInstalled(unittest.TestCase):
def test_raises_when_user_specified_reader_not_installed(self):
"""LoadImage must raise OptionalImportError when a user-specified reader's package is missing."""
from unittest.mock import patch

from monai.utils import OptionalImportError

# Patch ITKReader.__init__ to simulate the package not being installed
with patch("monai.data.image_reader.ITKReader.__init__", side_effect=OptionalImportError("itk not installed")):
with self.assertRaises(OptionalImportError):
LoadImage(reader="ITKReader")


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