otx.cli.utils.jsonargparse#

Functions related to jsonargparse.

Functions

add_list_type_arguments(parser, baseclass, ...)

Add list type arguments to the given ArgumentParser.

apply_config(self, parser, cfg, dest, value)

Applies the configuration to the parser.

apply_override(cfg, overrides)

Overrides the provided overrides in the given configs.

flatten_dict(config[, parent_key, sep])

Flatten a nested dictionary into a single-level dictionary.

get_configuration(config_path[, subcommand])

Get the configuration from the given path.

get_defaults_with_overrides(self[, skip_check])

Returns a namespace with all default values.

get_instantiated_classes(config, work_dir, ...)

Get the instantiated classes for training.

get_short_docstring(component)

Get the short description from the docstring.

list_override(configs, key, overrides[, ...])

Overrides the nested list type in the given configs with the provided override_list.

namespace_override(configs, key, overrides)

Overrides the nested namespace type in the given configs with the provided overrides.

patch_update_configs()

Patch the update and apply_config methods of the given namespace and action_config_file objects.

update(self, value[, key, only_unset])

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.

Parameters:
  • parser (ArgumentParser) – The ArgumentParser to add the arguments to.

  • baseclass (tuple[type, ...]) – A tuple of base classes for the subclasses.

  • nested_key (str) – The nested key for the arguments.

  • skip (set[str] | None, optional) – A set of arguments to skip. Defaults to None.

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.

Parameters:
  • d (dict) – The dictionary to be flattened.

  • parent_key (str) – The parent key to be used for nested keys.

  • sep (str) – The separator to be used between parent and child keys.

Returns:

The flattened dictionary.

Return type:

dict

otx.cli.utils.jsonargparse.get_configuration(config_path: str | Path, subcommand: str = 'train', **kwargs) dict[source]#

Get the configuration from the given path.

Parameters:

config_path (str | Path) – The path to the configuration file.

Returns:

The configuration dictionary.

Return type:

dict

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:

dict

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:

str

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:
  • configs (Namespace) – The configuration object containing the key.

  • key (str) – key of the configs want to override.

  • overrides (list) – The list of dictionary item to override the existing ones.

  • convert_dict_to_namespace (bool) – Whether to convert the dictionary to Namespace. Defaults to True.

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:
  • configs (Namespace) – The configuration object containing the key.

  • key (str) – key of the configs want to override.

  • overrides (Namespace) – The configuration object to override the existing ones.

  • convert_dict_to_namespace (bool) – Whether to convert the dictionary to Namespace. Defaults to True.

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.