Skip to content
Merged
18 changes: 17 additions & 1 deletion monai/bundle/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions monai/bundle/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_)]
Expand Down
2 changes: 2 additions & 0 deletions tests/test_config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down