The primary configuration parser. It traverses a structured config (in the form of nested Python dict or list), creates ``ConfigItem``, and assign unique IDs according to the structures. This class provides convenient access to the set of ``ConfigItem`` of the config by ID. A typic
| 235 | |
| 236 | |
| 237 | class ConfigParser: |
| 238 | """ |
| 239 | The primary configuration parser. It traverses a structured config (in the form of nested Python dict or list), |
| 240 | creates ``ConfigItem``, and assign unique IDs according to the structures. |
| 241 | |
| 242 | This class provides convenient access to the set of ``ConfigItem`` of the config by ID. |
| 243 | A typical workflow of config parsing is as follows: |
| 244 | |
| 245 | - Initialize ``ConfigParser`` with the ``config`` source. |
| 246 | - Call ``get_parsed_content()`` to get expected component with `id`. |
| 247 | |
| 248 | .. code-block:: python |
| 249 | |
| 250 | from monai.bundle import ConfigParser |
| 251 | |
| 252 | config = { |
| 253 | "my_dims": 2, |
| 254 | "dims_1": "$@my_dims + 1", |
| 255 | "my_xform": {"_target_": "LoadImage"}, |
| 256 | "my_net": {"_target_": "BasicUNet", "spatial_dims": "@dims_1", "in_channels": 1, "out_channels": 4}, |
| 257 | "trainer": {"_target_": "SupervisedTrainer", "network": "@my_net", "preprocessing": "@my_xform"} |
| 258 | } |
| 259 | # in the example $@my_dims + 1 is an expression, which adds 1 to the value of @my_dims |
| 260 | parser = ConfigParser(config) |
| 261 | |
| 262 | # get/set configuration content, the set method should happen before calling parse() |
| 263 | print(parser["my_net"]["in_channels"]) # original input channels 1 |
| 264 | parser["my_net"]["in_channels"] = 4 # change input channels to 4 |
| 265 | print(parser["my_net"]["in_channels"]) |
| 266 | |
| 267 | # instantiate the network component |
| 268 | parser.parse(True) |
| 269 | net = parser.get_parsed_content("my_net", instantiate=True) |
| 270 | print(net) |
| 271 | |
| 272 | # also support to get the configuration content of parsed `ConfigItem` |
| 273 | trainer = parser.get_parsed_content("trainer", instantiate=False) |
| 274 | print(trainer) |
| 275 | |
| 276 | Args: |
| 277 | config: input config source to parse. |
| 278 | excludes: when importing modules to instantiate components, |
| 279 | excluding components from modules specified in ``excludes``. |
| 280 | globals: pre-import packages as global variables to ``ConfigExpression``, |
| 281 | so that expressions, for example, ``"$monai.data.list_data_collate"`` can use ``monai`` modules. |
| 282 | The current supported globals and alias names are |
| 283 | ``{"monai": "monai", "torch": "torch", "np": "numpy", "numpy": "numpy"}``. |
| 284 | These are MONAI's minimal dependencies. Additional packages could be included with `globals={"itk": "itk"}`. |
| 285 | Set it to ``False`` to disable `self.globals` module importing. |
| 286 | |
| 287 | See also: |
| 288 | |
| 289 | - :py:class:`monai.bundle.ConfigItem` |
| 290 | - :py:class:`monai.bundle.scripts.run` |
| 291 | |
| 292 | """ |
| 293 | |
| 294 | suffixes = ("json", "yaml", "yml") |
no outgoing calls
searching dependent graphs…