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
4 changes: 3 additions & 1 deletion monai/engines/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
if TYPE_CHECKING:
from ignite.engine import EventEnum
else:
EventEnum, _ = optional_import("ignite.engine", IgniteInfo.OPT_IMPORT_VERSION, min_version, "EventEnum")
EventEnum, _ = optional_import(
"ignite.engine", IgniteInfo.OPT_IMPORT_VERSION, min_version, "EventEnum", as_type="base"
)

__all__ = [
"IterationEvents",
Expand Down
2 changes: 1 addition & 1 deletion monai/engines/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from .utils import engine_apply_transform

IgniteEngine, _ = optional_import("ignite.engine", IgniteInfo.OPT_IMPORT_VERSION, min_version, "Engine")
IgniteEngine, _ = optional_import("ignite.engine", IgniteInfo.OPT_IMPORT_VERSION, min_version, "Engine", as_type="")
State, _ = optional_import("ignite.engine", IgniteInfo.OPT_IMPORT_VERSION, min_version, "State")
Events, _ = optional_import("ignite.engine", IgniteInfo.OPT_IMPORT_VERSION, min_version, "Events")

Expand Down
4 changes: 2 additions & 2 deletions monai/handlers/ignite_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
from monai.utils import min_version, optional_import

idist, _ = optional_import("ignite", IgniteInfo.OPT_IMPORT_VERSION, min_version, "distributed")
Metric, _ = optional_import("ignite.metrics", IgniteInfo.OPT_IMPORT_VERSION, min_version, "Metric")
Metric, _ = optional_import("ignite.metrics", IgniteInfo.OPT_IMPORT_VERSION, min_version, "Metric", as_type="base")
reinit__is_reduced, _ = optional_import(
"ignite.metrics.metric", IgniteInfo.OPT_IMPORT_VERSION, min_version, "reinit__is_reduced"
"ignite.metrics.metric", IgniteInfo.OPT_IMPORT_VERSION, min_version, "reinit__is_reduced", as_type="decorator"
)
if TYPE_CHECKING:
from ignite.engine import Engine
Expand Down
4 changes: 2 additions & 2 deletions monai/handlers/parameter_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ def __init__(
vc_kwargs: Dict,
epoch_level: bool = False,
name: Optional[str] = None,
event=Events.ITERATION_COMPLETED,
event=None,
):
self.epoch_level = epoch_level
self.event = event
self.event = event if event is not None else Events.ITERATION_COMPLETED

self._calculators = {
"linear": self._linear,
Expand Down
22 changes: 21 additions & 1 deletion monai/utils/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ def optional_import(
descriptor: str = OPTIONAL_IMPORT_MSG_FMT,
version_args=None,
allow_namespace_pkg: bool = False,
as_type: str = "default",
) -> Tuple[Any, bool]:
"""
Imports an optional module specified by `module` string.
Expand All @@ -323,6 +324,10 @@ def optional_import(
descriptor: a format string for the final error message when using a not imported module.
version_args: additional parameters to the version checker.
allow_namespace_pkg: whether importing a namespace package is allowed. Defaults to False.
as_type: there are cases where the optionally imported object is used as
a base class, or a decorator, the exceptions should raise accordingly. The current supported values
are "default" (call once to raise), "decorator" (call the constructor and the second call to raise),
and anything else will return a lazy class that can be used as a base class (call the constructor to raise).

Returns:
The imported module and a boolean flag indicating whether the import is successful.
Expand Down Expand Up @@ -409,7 +414,22 @@ def __call__(self, *_args, **_kwargs):
"""
raise self._exception

return _LazyRaise(), False
def __getitem__(self, item):
raise self._exception

def __iter__(self):
raise self._exception

if as_type == "default":
return _LazyRaise(), False

class _LazyCls(_LazyRaise):
def __init__(self, *_args, **kwargs):
super().__init__()
if not as_type.startswith("decorator"):
raise self._exception

return _LazyCls, False


def require_pkg(
Expand Down
5 changes: 1 addition & 4 deletions tests/min_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,7 @@ def run_testsuit():
from monai.utils.module import load_submodules

_, err_mod = load_submodules(sys.modules["monai"], True)
if err_mod:
print(err_mod)
# expecting that only engines and handlers are not imported
assert sorted(err_mod) == ["monai.engines", "monai.handlers"]
assert not err_mod

# testing all modules
test_runner = unittest.TextTestRunner(stream=sys.stdout, verbosity=2)
Expand Down