Create an object instance or call a callable object from a class or function represented by ``_path``. `kwargs` will be part of the input arguments to the class constructor or function. The target component must be a class or a function, if not, return the component directly. Args:
(__path: str, __mode: str, **kwargs: Any)
| 205 | |
| 206 | |
| 207 | def instantiate(__path: str, __mode: str, **kwargs: Any) -> Any: |
| 208 | """ |
| 209 | Create an object instance or call a callable object from a class or function represented by ``_path``. |
| 210 | `kwargs` will be part of the input arguments to the class constructor or function. |
| 211 | The target component must be a class or a function, if not, return the component directly. |
| 212 | |
| 213 | Args: |
| 214 | __path: if a string is provided, it's interpreted as the full path of the target class or function component. |
| 215 | If a callable is provided, ``__path(**kwargs)`` will be invoked and returned for ``__mode="default"``. |
| 216 | For ``__mode="callable"``, the callable will be returned as ``__path`` or, if ``kwargs`` are provided, |
| 217 | as ``functools.partial(__path, **kwargs)`` for future invoking. |
| 218 | |
| 219 | __mode: the operating mode for invoking the (callable) ``component`` represented by ``__path``: |
| 220 | |
| 221 | - ``"default"``: returns ``component(**kwargs)`` |
| 222 | - ``"callable"``: returns ``component`` or, if ``kwargs`` are provided, ``functools.partial(component, **kwargs)`` |
| 223 | - ``"debug"``: returns ``pdb.runcall(component, **kwargs)`` |
| 224 | |
| 225 | kwargs: keyword arguments to the callable represented by ``__path``. |
| 226 | |
| 227 | """ |
| 228 | from monai.utils.enums import CompInitMode |
| 229 | |
| 230 | component = locate(__path) if isinstance(__path, str) else __path |
| 231 | if component is None: |
| 232 | raise ModuleNotFoundError(f"Cannot locate class or function path: '{__path}'.") |
| 233 | m = look_up_option(__mode, CompInitMode) |
| 234 | try: |
| 235 | if kwargs.pop("_debug_", False) or run_debug: |
| 236 | warnings.warn( |
| 237 | f"\n\npdb: instantiating component={component}, mode={m}\n" |
| 238 | f"See also Debugger commands documentation: https://docs.python.org/3/library/pdb.html\n" |
| 239 | ) |
| 240 | breakpoint() |
| 241 | if not callable(component): |
| 242 | warnings.warn(f"Component {component} is not callable when mode={m}.") |
| 243 | return component |
| 244 | if m == CompInitMode.DEFAULT: |
| 245 | return component(**kwargs) |
| 246 | if m == CompInitMode.CALLABLE: |
| 247 | return partial(component, **kwargs) if kwargs else component |
| 248 | if m == CompInitMode.DEBUG: |
| 249 | warnings.warn( |
| 250 | f"\n\npdb: instantiating component={component}, mode={m}\n" |
| 251 | f"See also Debugger commands documentation: https://docs.python.org/3/library/pdb.html\n" |
| 252 | ) |
| 253 | return pdb.runcall(component, **kwargs) |
| 254 | except Exception as e: |
| 255 | raise RuntimeError( |
| 256 | f"Failed to instantiate component '{__path}' with keywords: {','.join(kwargs.keys())}" |
| 257 | f"\n set '_mode_={CompInitMode.DEBUG}' to enter the debugging mode." |
| 258 | ) from e |
| 259 | |
| 260 | warnings.warn(f"Component to instantiate must represent a valid class or function, but got {__path}.") |
| 261 | return component |
| 262 | |
| 263 | |
| 264 | def get_full_type_name(typeobj): |
no test coverage detected
searching dependent graphs…