diff --git a/monai/bundle/config_parser.py b/monai/bundle/config_parser.py index 791759fd65..b4ffc853bb 100644 --- a/monai/bundle/config_parser.py +++ b/monai/bundle/config_parser.py @@ -178,11 +178,27 @@ def get(self, id: str = "", default: Optional[Any] = None): def set(self, config: Any, id: str = ""): """ - Set config by ``id``. See also :py:meth:`__setitem__`. + Set config by ``id``. + + Args: + config: config to set at location ``id``. + id: id to specify the expected position. See also :py:meth:`__setitem__`. """ self[id] = config + def update(self, pairs: Dict[str, Any]): + """ + Set the ``id`` and the corresponding config content in pairs, see also :py:meth:`__setitem__`. + For example, ``parser.update({"train#epoch": 100, "train#lr": 0.02})`` + + Args: + pairs: dictionary of `id` and config pairs. + + """ + for k, v in pairs.items(): + self[k] = v + def __contains__(self, id: Union[str, int]) -> bool: """ Returns True if `id` is stored in this configuration. diff --git a/monai/bundle/scripts.py b/monai/bundle/scripts.py index 3f9241be09..c01498bb09 100644 --- a/monai/bundle/scripts.py +++ b/monai/bundle/scripts.py @@ -372,8 +372,7 @@ def run( parser.read_meta(f=meta_file_) # the rest key-values in the _args are to override config content - for k, v in _args.items(): - parser[k] = v + parser.update(pairs=_args) # resolve and execute the specified runner expressions in the config, return the results return [parser.get_parsed_content(i, lazy=True, eval_expr=True, instantiate=True) for i in ensure_tuple(runner_id_)] diff --git a/tests/test_config_parser.py b/tests/test_config_parser.py index 81e96d4095..f991b3f5f5 100644 --- a/tests/test_config_parser.py +++ b/tests/test_config_parser.py @@ -104,6 +104,8 @@ def test_config_content(self): # test nested ids parser["dataset#_target_"] = "Dataset" self.assertEqual(parser["dataset#_target_"], "Dataset") + parser.update({"dataset#_target_1": "Dataset1"}) + self.assertEqual(parser["dataset#_target_1"], "Dataset1") # test int id parser.set(["test1", "test2", "test3"]) parser[1] = "test4"