otx.api.configuration.cfg_helper#

This module contains the configuration helper functions.

These can be used to create, convert or interact with OTX configuration objects or dictionaries, yaml strings or yaml files representing those objects.

Functions

create(input_config)

Create a configuration object from a yaml string, yaml file path, dictionary or OmegaConf DictConfig object.

config_to_bytes(config)

Converts ConfigurableParameters to bytes.

validate(config)

Validate a configuration object.

convert(config, target[, enum_to_str, ...])

Convert a configuration object to either a yaml string, a dictionary or an OmegaConf DictConfig object.

substitute_values(config, value_input[, ...])

Substitutes values from value_input into the config object.

substitute_values_for_lifecycle(config, ...)

Substitutes values from value_input into the config object.

flatten_config_values(config)

Extracts the "value" field from any nested config.

merge_a_into_b(dict_a, dict_b)

Inspired by mmcv.Config.merge_a_into_b by merging dict a into dict b (non-inplace).

otx.api.configuration.cfg_helper.config_to_bytes(config: ConfigurableParameters) bytes#

Converts ConfigurableParameters to bytes.

Parameters:

config – configurable parameters

Retruns:

JSON in bytes

otx.api.configuration.cfg_helper.convert(config: ConfigurableParameters, target: Type[ConvertTypeVar], enum_to_str: bool = False, id_to_str: bool = False, values_only: bool = False) Any#

Convert a configuration object to either a yaml string, a dictionary or an OmegaConf DictConfig object.

Parameters:
  • config (ConfigurableParameters) – ConfigurableParameters object to convert

  • target (Type[ConvertTypeVar]) – target type to convert to. Options are [str, dict, DictConfig]

  • enum_to_str (bool) – Boolean specifying whether to convert enums within the config to their string representation. For conversion to yaml, enums are automatically converted and this option is disregarded.

  • id_to_str (bool) – True to convert the id of the configurable parameters to a string representation, False to leave it as an ID object

  • values_only (bool) – True to keep only the parameter values, and remove all meta data from the target output

Raises:

ValueError – if an unsupported conversion target is supplied

Returns:

Result of the conversion, the configuration specified in config in the

representation specified in target

Return type:

ConvertTypeVar

otx.api.configuration.cfg_helper.create(input_config: str | DictConfig | dict) ConfigurableParameters#

Create a configuration object from a yaml string, yaml file path, dictionary or OmegaConf DictConfig object.

Parameters:

input_config – yaml string, dictionary, DictConfig or filepath describing a configuration.

Returns:

ConfigurableParameters object

otx.api.configuration.cfg_helper.flatten_config_values(config: dict)#

Extracts the “value” field from any nested config.

Flattening the structure of the config dictionary. The original config dictionary is modified in-place.

Parameters:

config (dict) – config dictionary

otx.api.configuration.cfg_helper.merge_a_into_b(dict_a, dict_b)#

Inspired by mmcv.Config.merge_a_into_b by merging dict a into dict b (non-inplace).

Values in a will overwrite b. b is copied first to avoid in-place modifications.

Parameters:
  • dict_a (dict) – The source dict to be merged into b.

  • dict_b (dict) – The origin dict to be fetch keys from a.

Returns:

The modified dict of b using a.

Return type:

dict

Examples

# Normally merge a into b. >>> merge_a_into_b({‘a’: {‘d’: 5, ‘e’: 6}, ‘b’: 4}, {‘a’: {‘c’: 1, ‘d’: 4}, ‘b’: 3}) {‘a’: {‘c’: 1, ‘d’: 5, ‘e’: 6}, ‘b’: 4}

otx.api.configuration.cfg_helper.substitute_values(config: ConfigurableParameters, value_input: str | DictConfig | dict | ConfigurableParameters, allow_missing_values: bool = False)#

Substitutes values from value_input into the config object.

The structures of value_input and config have to match in order for the values to be substituted correctly.

Values are substituted in place.

Parameters:
  • config (ConfigurableParameters) – ConfigurableParameter object to substitute values into.

  • value_input (Union[str, DictConfig, dict, ConfigurableParameters]) – ConfigurableParameters (either in object, dict, yaml or DictConfig representation) to take the values to be substituted from.

  • allow_missing_values (bool) – True to allow missing values in the configuration, i.e. if a value is found in value_input, but not in config, it will silently be ignored. If set to False, an AttributeError will be raised. Defaults to False.

otx.api.configuration.cfg_helper.substitute_values_for_lifecycle(config: ConfigurableParameters, value_input: ConfigurableParameters, model_lifecycle: ModelLifecycle | Sequence[ModelLifecycle], allow_missing_values: bool = True)#

Substitutes values from value_input into the config object.

The structures of value_input and config have to match in order for the values to be substituted correctly. If the argument model_lifecycle is provided, only parameters that affect the specified phase in the model lifecycle will be substituted.

Values are substituted in place.

Parameters:
  • config (ConfigurableParameters) – ConfigurableParameter object to substitute values into

  • value_input (ConfigurableParameters) – ConfigurableParameters to take the values to be substituted from.

  • model_lifecycle (Union[ModelLifecycle, Sequence[ModelLifecycle]]) – Phase or list of phases in the model lifecycle to carry out the substitution for. For example, if model_lifecycle = ModelLifecycle.INFERENCE is passed, only parameters that affect inference will be updated, and the rest of the parameters will remain untouched.

  • allow_missing_values (bool) – True to allow missing values in the configuration, i.e. if a value is found in value_input, but not in config, it will silently be ignored. If set to False, an AttributeError will be raised. Defaults to True.

otx.api.configuration.cfg_helper.validate(config: ParameterGroup) bool#

Validate a configuration object.

Parameters:

config – Configuration to validate

Returns:

True if config is valid