MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / ConfigComponent

Class ConfigComponent

monai/bundle/config_item.py:164–292  ·  view source on GitHub ↗

Subclass of :py:class:`monai.bundle.ConfigItem`, this class uses a dictionary with string keys to represent a component of `class` or `function` and supports instantiation. Currently, three special keys (strings surrounded by ``_``) are defined and interpreted beyond the regular litera

Source from the content-addressed store, hash-verified

162
163
164class ConfigComponent(ConfigItem, Instantiable):
165 """
166 Subclass of :py:class:`monai.bundle.ConfigItem`, this class uses a dictionary with string keys to
167 represent a component of `class` or `function` and supports instantiation.
168
169 Currently, three special keys (strings surrounded by ``_``) are defined and interpreted beyond the regular literals:
170
171 - class or function identifier of the python module, specified by ``"_target_"``,
172 indicating a monai built-in Python class or function such as ``"LoadImageDict"``,
173 or a full module name, e.g. ``"monai.transforms.LoadImageDict"``, or a callable, e.g. ``"$@model.forward"``.
174 - ``"_requires_"`` (optional): specifies reference IDs (string starts with ``"@"``) or ``ConfigExpression``
175 of the dependencies for this ``ConfigComponent`` object. These dependencies will be
176 evaluated/instantiated before this object is instantiated. It is useful when the
177 component doesn't explicitly depend on the other `ConfigItems` via its arguments,
178 but requires the dependencies to be instantiated/evaluated beforehand.
179 - ``"_disabled_"`` (optional): a flag to indicate whether to skip the instantiation.
180 - ``"_desc_"`` (optional): free text descriptions of the component for code readability.
181 - ``"_mode_"`` (optional): operating mode for invoking the callable ``component`` defined by ``"_target_"``:
182
183 - ``"default"``: returns ``component(**kwargs)``
184 - ``"callable"``: returns ``component`` or, if ``kwargs`` are provided, ``functools.partial(component, **kwargs)``
185 - ``"debug"``: returns ``pdb.runcall(component, **kwargs)``
186
187 Other fields in the config content are input arguments to the python module.
188
189 .. code-block:: python
190
191 from monai.bundle import ComponentLocator, ConfigComponent
192
193 locator = ComponentLocator(excludes=["modules_to_exclude"])
194 config = {
195 "_target_": "LoadImaged",
196 "keys": ["image", "label"]
197 }
198
199 configer = ConfigComponent(config, id="test", locator=locator)
200 image_loader = configer.instantiate()
201 print(image_loader) # <monai.transforms.io.dictionary.LoadImaged object at 0x7fba7ad1ee50>
202
203 Args:
204 config: content of a config item.
205 id: name of the current config item, defaults to empty string.
206 locator: a ``ComponentLocator`` to convert a module name string into the actual python module.
207 if `None`, a ``ComponentLocator(excludes=excludes)`` will be used.
208 excludes: if ``locator`` is None, create a new ``ComponentLocator`` with ``excludes``.
209 See also: :py:class:`monai.bundle.ComponentLocator`.
210
211 """
212
213 non_arg_keys = {"_target_", "_disabled_", "_requires_", "_desc_", "_mode_"}
214
215 def __init__(
216 self,
217 config: Any,
218 id: str = "",
219 locator: ComponentLocator | None = None,
220 excludes: Sequence[str] | str | None = None,
221 ) -> None:

Callers 5

_do_parseMethod · 0.90
test_resolveMethod · 0.90
test_componentMethod · 0.90

Calls

no outgoing calls

Tested by 4

test_resolveMethod · 0.72
test_componentMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…