diff --git a/monai/bundle/config_parser.py b/monai/bundle/config_parser.py index edbe4d1923..791759fd65 100644 --- a/monai/bundle/config_parser.py +++ b/monai/bundle/config_parser.py @@ -226,7 +226,8 @@ def get_parsed_content(self, id: str = "", **kwargs): kwargs: additional keyword arguments to be passed to ``_resolve_one_item``. Currently support ``lazy`` (whether to retain the current config cache, default to `True`), ``instantiate`` (whether to instantiate the `ConfigComponent`, default to `True`) and - ``eval_expr`` (whether to evaluate the `ConfigExpression`, default to `True`). + ``eval_expr`` (whether to evaluate the `ConfigExpression`, default to `True`), ``default`` + (the default config item if the `id` is not in the config content). """ if not self.ref_resolver.is_resolved(): diff --git a/monai/bundle/reference_resolver.py b/monai/bundle/reference_resolver.py index 02c6902151..7ed5ae3522 100644 --- a/monai/bundle/reference_resolver.py +++ b/monai/bundle/reference_resolver.py @@ -111,14 +111,16 @@ def _resolve_one_item(self, id: str, waiting_list: Optional[Set[str]] = None, ** waiting_list: set of ids pending to be resolved. It's used to detect circular references such as: `{"name": "A", "dep": "@B"}` and `{"name": "B", "dep": "@A"}`. - kwargs: keyword arguments to pass to ``_resolve_one_item()``. - Currently support ``instantiate`` and ``eval_expr``. Both are defaulting to True. + kwargs: keyword arguments to pass to ``_resolve_one_item()``. + Currently support ``instantiate``, ``eval_expr`` and ``default``. + `instantiate` and `eval_expr` are defaulting to True, `default` is the target config item + if the `id` is not in the config content, must be a `ConfigItem` object. """ if id in self.resolved_content: return self.resolved_content[id] try: - item = look_up_option(id, self.items, print_all_options=False) + item = look_up_option(id, self.items, print_all_options=False, default=kwargs.get("default", "no_default")) except ValueError as err: raise KeyError(f"id='{id}' is not found in the config resolver.") from err item_config = item.get_config() @@ -175,8 +177,10 @@ def get_resolved_content(self, id: str, **kwargs): Args: id: id name of the expected item. - kwargs: additional keyword arguments to be passed to ``_resolve_one_item``. - Currently support ``instantiate`` and ``eval_expr``. Both are defaulting to True. + kwargs: keyword arguments to pass to ``_resolve_one_item()``. + Currently support ``instantiate``, ``eval_expr`` and ``default``. + `instantiate` and `eval_expr` are defaulting to True, `default` is the target config item + if the `id` is not in the config content, must be a `ConfigItem` object. """ return self._resolve_one_item(id=id, **kwargs) diff --git a/tests/test_config_parser.py b/tests/test_config_parser.py index 14c1579686..81e96d4095 100644 --- a/tests/test_config_parser.py +++ b/tests/test_config_parser.py @@ -18,6 +18,7 @@ from parameterized import parameterized from monai.bundle import ConfigParser, ReferenceResolver +from monai.bundle.config_item import ConfigItem from monai.data import DataLoader, Dataset from monai.transforms import Compose, LoadImaged, RandTorchVisiond from monai.utils import min_version, optional_import @@ -129,6 +130,8 @@ def test_parse(self, config, expected_ids, output_types): root = parser.get_parsed_content(id="") for v, cls in zip(root.values(), [Compose, Dataset, DataLoader]): self.assertTrue(isinstance(v, cls)) + # test default value + self.assertEqual(parser.get_parsed_content(id="abc", default=ConfigItem(12345, "abc")), 12345) @parameterized.expand([TEST_CASE_2]) def test_function(self, config):