|
66 | 66 | PPRINT_CONFIG_N = 5 |
67 | 67 |
|
68 | 68 |
|
69 | | -def _update_args(args: str | dict | None = None, ignore_none: bool = True, **kwargs: Any) -> dict: |
| 69 | +def update_kwargs(args: str | dict | None = None, ignore_none: bool = True, **kwargs: Any) -> dict: |
70 | 70 | """ |
71 | | - Update the `args` with the input `kwargs`. |
| 71 | + Update the `args` dictionary with the input `kwargs`. |
72 | 72 | For dict data, recursively update the content based on the keys. |
73 | 73 |
|
| 74 | + Example:: |
| 75 | +
|
| 76 | + from monai.bundle import update_kwargs |
| 77 | + update_kwargs({'exist': 1}, exist=2, new_arg=3) |
| 78 | + # return {'exist': 2, 'new_arg': 3} |
| 79 | +
|
74 | 80 | Args: |
75 | | - args: source args to update. |
| 81 | + args: source `args` dictionary (or a json/yaml filename to read as dictionary) to update. |
76 | 82 | ignore_none: whether to ignore input args with None value, default to `True`. |
77 | | - kwargs: destination args to update. |
| 83 | + kwargs: key=value pairs to be merged into `args`. |
78 | 84 |
|
79 | 85 | """ |
80 | 86 | args_: dict = args if isinstance(args, dict) else {} |
81 | 87 | if isinstance(args, str): |
82 | 88 | # args are defined in a structured file |
83 | 89 | args_ = ConfigParser.load_config_file(args) |
| 90 | + if isinstance(args, (tuple, list)) and all(isinstance(x, str) for x in args): |
| 91 | + primary, overrides = args |
| 92 | + args_ = update_kwargs(primary, ignore_none, **update_kwargs(overrides, ignore_none, **kwargs)) |
| 93 | + if not isinstance(args_, dict): |
| 94 | + return args_ |
84 | 95 | # recursively update the default args with new args |
85 | 96 | for k, v in kwargs.items(): |
86 | | - print(k, v) |
87 | 97 | if ignore_none and v is None: |
88 | 98 | continue |
89 | 99 | if isinstance(v, dict) and isinstance(args_.get(k), dict): |
90 | | - args_[k] = _update_args(args_[k], ignore_none, **v) |
| 100 | + args_[k] = update_kwargs(args_[k], ignore_none, **v) |
91 | 101 | else: |
92 | 102 | args_[k] = v |
93 | 103 | return args_ |
94 | 104 |
|
95 | 105 |
|
| 106 | +_update_args = update_kwargs # backward compatibility |
| 107 | + |
| 108 | + |
96 | 109 | def _pop_args(src: dict, *args: Any, **kwargs: Any) -> tuple: |
97 | 110 | """ |
98 | 111 | Pop args from the `src` dictionary based on specified keys in `args` and (key, default value) pairs in `kwargs`. |
@@ -318,7 +331,7 @@ def download( |
318 | 331 | so that the command line inputs can be simplified. |
319 | 332 |
|
320 | 333 | """ |
321 | | - _args = _update_args( |
| 334 | + _args = update_kwargs( |
322 | 335 | args=args_file, |
323 | 336 | name=name, |
324 | 337 | version=version, |
@@ -834,7 +847,7 @@ def verify_metadata( |
834 | 847 |
|
835 | 848 | """ |
836 | 849 |
|
837 | | - _args = _update_args( |
| 850 | + _args = update_kwargs( |
838 | 851 | args=args_file, |
839 | 852 | meta_file=meta_file, |
840 | 853 | filepath=filepath, |
@@ -958,7 +971,7 @@ def verify_net_in_out( |
958 | 971 |
|
959 | 972 | """ |
960 | 973 |
|
961 | | - _args = _update_args( |
| 974 | + _args = update_kwargs( |
962 | 975 | args=args_file, |
963 | 976 | net_id=net_id, |
964 | 977 | meta_file=meta_file, |
@@ -1127,7 +1140,7 @@ def onnx_export( |
1127 | 1140 | e.g. ``--_meta#network_data_format#inputs#image#num_channels 3``. |
1128 | 1141 |
|
1129 | 1142 | """ |
1130 | | - _args = _update_args( |
| 1143 | + _args = update_kwargs( |
1131 | 1144 | args=args_file, |
1132 | 1145 | net_id=net_id, |
1133 | 1146 | filepath=filepath, |
@@ -1242,7 +1255,7 @@ def ckpt_export( |
1242 | 1255 | e.g. ``--_meta#network_data_format#inputs#image#num_channels 3``. |
1243 | 1256 |
|
1244 | 1257 | """ |
1245 | | - _args = _update_args( |
| 1258 | + _args = update_kwargs( |
1246 | 1259 | args=args_file, |
1247 | 1260 | net_id=net_id, |
1248 | 1261 | filepath=filepath, |
@@ -1401,7 +1414,7 @@ def trt_export( |
1401 | 1414 | e.g. ``--_meta#network_data_format#inputs#image#num_channels 3``. |
1402 | 1415 |
|
1403 | 1416 | """ |
1404 | | - _args = _update_args( |
| 1417 | + _args = update_kwargs( |
1405 | 1418 | args=args_file, |
1406 | 1419 | net_id=net_id, |
1407 | 1420 | filepath=filepath, |
@@ -1614,7 +1627,7 @@ def create_workflow( |
1614 | 1627 | kwargs: arguments to instantiate the workflow class. |
1615 | 1628 |
|
1616 | 1629 | """ |
1617 | | - _args = _update_args(args=args_file, workflow_name=workflow_name, config_file=config_file, **kwargs) |
| 1630 | + _args = update_kwargs(args=args_file, workflow_name=workflow_name, config_file=config_file, **kwargs) |
1618 | 1631 | _log_input_summary(tag="run", args=_args) |
1619 | 1632 | (workflow_name, config_file) = _pop_args( |
1620 | 1633 | _args, workflow_name=ConfigWorkflow, config_file=None |
|
0 commit comments