Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Signed-off-by: Wenqi Li <[email protected]>
  • Loading branch information
wyli committed Mar 15, 2022
1 parent d8121d2 commit 29243d2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
17 changes: 11 additions & 6 deletions monai/bundle/config_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,22 @@ class ConfigComponent(ConfigItem, Instantiable):
Subclass of :py:class:`monai.bundle.ConfigItem`, this class uses a dictionary with string keys to
represent a component of `class` or `function` and supports instantiation.
Currently, two special keys (strings surrounded by ``_``) are defined and interpreted beyond the regular literals:
- class or function identifier of the python module, specified by one of the two keys.
- ``"_target_"``: indicates build-in python classes or functions such as "LoadImageDict",
or full module name, such as "monai.transforms.LoadImageDict".
Currently, three special keys (strings surrounded by ``_``) are defined and interpreted beyond the regular literals:
- class or function identifier of the python module, specified by ``"_target_"``,
indicating a build-in python class or function such as ``"LoadImageDict"``,
or a full module name, such as ``"monai.transforms.LoadImageDict"``.
- ``"_requires_"``: specifies reference IDs (string starts with ``"@"``) or ``ConfigExpression``
of the dependencies for this ``ConfigComponent`` object. These dependencies will be
evaluated/instantiated before this object is instantiated.
- ``"_disabled_"``: a flag to indicate whether to skip the instantiation.
Other fields in the config content are input arguments to the python module.
.. code-block:: python
from monai.bundle import ComponentLocator, ConfigComponent
locator = ComponentLocator(excludes=["modules_to_exclude"])
config = {
"_target_": "LoadImaged",
Expand All @@ -195,7 +200,7 @@ class ConfigComponent(ConfigItem, Instantiable):
"""

non_arg_keys = {"_target_", "_disabled_"}
non_arg_keys = {"_target_", "_disabled_", "_requires_"}

def __init__(
self,
Expand Down
12 changes: 8 additions & 4 deletions monai/bundle/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

__all__ = ["ConfigParser"]

_default_globals = {"monai": "monai", "torch": "torch", "np": "numpy", "numpy": "numpy"}


class ConfigParser:
"""
Expand Down Expand Up @@ -74,7 +76,7 @@ class ConfigParser:
so that expressions, for example, ``"$monai.data.list_data_collate"`` can use ``monai`` modules.
The current supported globals and alias names are
``{"monai": "monai", "torch": "torch", "np": "numpy", "numpy": "numpy"}``.
These are MONAI's minimal dependencies.
These are MONAI's minimal dependencies. Additional packages could be included with `globals={"itk": "itk"}`.
See also:
Expand All @@ -96,9 +98,11 @@ def __init__(
):
self.config = None
self.globals: Dict[str, Any] = {}
globals = {"monai": "monai", "torch": "torch", "np": "numpy", "numpy": "numpy"} if globals is None else globals
if globals is not None:
for k, v in globals.items():
_globals = _default_globals.copy()
if isinstance(_globals, dict) and globals is not None:
_globals.update(globals)
if _globals is not None:
for k, v in _globals.items():
self.globals[k] = importlib.import_module(v) if isinstance(v, str) else v

self.locator = ComponentLocator(excludes=excludes)
Expand Down
7 changes: 7 additions & 0 deletions tests/testing_data/inference.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"device": "$torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')",
"set_seed": "$monai.utils.set_determinism(0)",
"print_test_name": "$print('json_test')",
"network_def": {
"_target_": "UNet",
"spatial_dims": 3,
Expand Down Expand Up @@ -93,6 +95,11 @@
},
"evaluator": {
"_target_": "SupervisedEvaluator",
"_requires_": [
"@set_seed",
"@print_test_name",
"$print('test_in_line_json')"
],
"device": "@device",
"val_data_loader": "@dataloader",
"network": "@network",
Expand Down
6 changes: 6 additions & 0 deletions tests/testing_data/inference.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
---
device: "$torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')"
set_seed: "$monai.utils.set_determinism(0)"
print_test_name: "$print('yaml_test')"
network_def:
_target_: UNet
spatial_dims: 3
Expand Down Expand Up @@ -66,6 +68,10 @@ postprocessing:
output_dir: "@_meta_#output_dir"
evaluator:
_target_: SupervisedEvaluator
_requires_:
- "$print('test_in_line_yaml')"
- "@set_seed"
- "@print_test_name"
device: "@device"
val_data_loader: "@dataloader"
network: "@network"
Expand Down

0 comments on commit 29243d2

Please sign in to comment.