otx.cli.utils.jsonargparse#
Functions related to jsonargparse.
Functions
|
Add list type arguments to the given ArgumentParser. |
|
Applies the configuration to the parser. |
|
Overrides the provided overrides in the given configs. |
|
Flatten a nested dictionary into a single-level dictionary. |
|
Get the configuration from the given path. |
|
Returns a namespace with all default values. |
|
Get the instantiated classes for training. |
|
Get the short description from the docstring. |
|
Overrides the nested list type in the given configs with the provided override_list. |
|
Overrides the nested namespace type in the given configs with the provided overrides. |
Patch the update and apply_config methods of the given namespace and action_config_file objects. |
|
|
Sets or replaces all items from the given nested namespace. |
- otx.cli.utils.jsonargparse.add_list_type_arguments(parser: ArgumentParser, baseclass: tuple[type, ...], nested_key: str, skip: set[str] | None = None) None [source]#
Add list type arguments to the given ArgumentParser.
From python >= 3.11, add_subclass_arguments no longer allows adding arguments of the form list[Class]. Modify it to bypass class checking, allowing you to use the list argument. Copy from jsonargparse._signatures.SignatureArguments.add_subclass_arguments.
- otx.cli.utils.jsonargparse.apply_config(self: ActionConfigFile, parser: ArgumentParser, cfg: Namespace, dest: str, value: str) None [source]#
Applies the configuration to the parser.
- Parameters:
parser – The parser object.
cfg – The configuration object.
dest – The destination attribute.
value – The value to be applied.
- Returns:
None
- otx.cli.utils.jsonargparse.apply_override(cfg: Namespace, overrides: Namespace) None [source]#
Overrides the provided overrides in the given configs.
- Parameters:
configs (Namespace) – The configuration object containing the key.
overrides (Namespace) – The configuration object to override the existing ones.
- otx.cli.utils.jsonargparse.flatten_dict(config: dict, parent_key: str = '', sep: str = '.') dict [source]#
Flatten a nested dictionary into a single-level dictionary.
- otx.cli.utils.jsonargparse.get_configuration(config_path: str | Path, subcommand: str = 'train', **kwargs) dict [source]#
Get the configuration from the given path.
- otx.cli.utils.jsonargparse.get_defaults_with_overrides(self: ArgumentParser, skip_check: bool = False) Namespace [source]#
Returns a namespace with all default values.
- Parameters:
skip_check – Whether to skip check if configuration is valid.
- Returns:
An object with all default values as attributes.
- otx.cli.utils.jsonargparse.get_instantiated_classes(config: str | Path | PathLike, work_dir: str | Path | PathLike | None, data_root: str | Path | PathLike | None, **kwargs) tuple[dict, dict] [source]#
Get the instantiated classes for training.
- Parameters:
config (PathLike) – Path to the configuration file.
work_dir (PathLike) – Path to the working directory.
data_root (PathLike) – Path to the data root directory.
- Returns:
The instantiated classes for training.
- Return type:
- otx.cli.utils.jsonargparse.get_short_docstring(component: TypeVar) str [source]#
Get the short description from the docstring.
- Parameters:
component (TypeVar) – The component to get the docstring from
- Returns:
The short description
- Return type:
- otx.cli.utils.jsonargparse.list_override(configs: Namespace, key: str, overrides: list, convert_dict_to_namespace: bool = True) None [source]#
Overrides the nested list type in the given configs with the provided override_list.
- Parameters:
Example
>>> configs = [ ... ... ... Namespace( ... class_path='lightning.pytorch.callbacks.EarlyStopping', ... init_args=Namespace(patience=10, ...), ... ), ... ... ... ] >>> override_callbacks = [ ... ... ... { ... 'class_path': 'lightning.pytorch.callbacks.EarlyStopping', ... 'init_args': {'patience': 3}, ... }, ... ... ... ] >>> list_override(configs=configs, key="callbacks", overrides=override_callbacks) >>> configs = [ ... ... ... Namespace( ... class_path='lightning.pytorch.callbacks.EarlyStopping', ... init_args=Namespace(patience=3, ...), ... ), ... ... ... ] >>> append_callbacks = [ ... { ... 'class_path': 'new_callbacks', ... }, ... ] >>> list_override(configs=configs, key="callbacks", overrides=append_callbacks) >>> configs = [ ... ... ... Namespace(class_path='new_callbacks'), ... ] >>> append_callbacks_as_dict = [ ... { ... 'class_path': 'new_callbacks1', ... }, ... ] >>> list_override( ... configs=configs, key="callbacks", overrides=append_callbacks_as_dict, convert_dict_to_namespace=False ... ) >>> configs = [ ... ... ... {'class_path': 'new_callbacks1'}, ... ]
- otx.cli.utils.jsonargparse.namespace_override(configs: Namespace, key: str, overrides: Namespace, convert_dict_to_namespace: bool = True) None [source]#
Overrides the nested namespace type in the given configs with the provided overrides.
- Parameters:
- otx.cli.utils.jsonargparse.patch_update_configs() Iterator[None] [source]#
Patch the update and apply_config methods of the given namespace and action_config_file objects.
- otx.cli.utils.jsonargparse.update(self: Namespace, value: Any, key: str | None = None, only_unset: bool = False) Namespace [source]#
Sets or replaces all items from the given nested namespace.
- Parameters:
value – A namespace to update multiple values or other type to set in a single key.
key – Branch key where to set the value. Required if value is not namespace.
only_unset – Whether to only set the value if not set in namespace.