topobench.utils.logging_utils module#

Utilities for logging hyperparameters.

class Any(*args, **kwargs)#

Bases: object

Special type indicating an unconstrained type.

  • Any is compatible with every type.

  • Any assumed to have all methods.

  • All values assumed to be instances of Any.

Note that all the above statements are true from the point of view of static type checkers. At runtime, Any should not be used with instance checks.

class OmegaConf#

Bases: object

OmegaConf primary class

__init__()#
static clear_cache(conf)#
classmethod clear_resolver(name)#

Clear(remove) any resolver only if it exists.

Returns a bool: True if resolver is removed and False if not removed.

Parameters:

name (str) – Name of the resolver.

Returns:

A bool (True if resolver is removed, False if not found before removing).

Return type:

bool

static clear_resolvers()#

Clear(remove) all OmegaConf resolvers, then re-register OmegaConf’s default resolvers.

static copy_cache(from_config, to_config)#
static create(obj: str, parent: BaseContainer | None = None, flags: Dict[str, bool] | None = None) DictConfig | ListConfig#
static create(obj: List[Any] | Tuple[Any, ...], parent: BaseContainer | None = None, flags: Dict[str, bool] | None = None) ListConfig
static create(obj: DictConfig, parent: BaseContainer | None = None, flags: Dict[str, bool] | None = None) DictConfig
static create(obj: ListConfig, parent: BaseContainer | None = None, flags: Dict[str, bool] | None = None) ListConfig
static create(obj: Dict[Any, Any] | None = None, parent: BaseContainer | None = None, flags: Dict[str, bool] | None = None) DictConfig
static from_cli(args_list=None)#
static from_dotlist(dotlist)#

Creates config from the content sys.argv or from the specified args list of not None

Parameters:

dotlist (List[str]) – A list of dotlist-style strings, e.g. ["foo.bar=1", "baz=qux"].

Returns:

A DictConfig object created from the dotlist.

Return type:

DictConfig

static get_cache(conf)#
static get_type(obj, key=None)#
classmethod has_resolver(name)#
static is_config(obj)#
static is_dict(obj)#
static is_interpolation(node, key=None)#
static is_list(obj)#
static is_missing(cfg, key)#
static is_readonly(conf)#
static is_struct(conf)#
static legacy_register_resolver(name, resolver)#
static load(file_)#
static masked_copy(conf, keys)#

Create a masked copy of of this config that contains a subset of the keys

Parameters:
Returns:

The masked DictConfig object.

Return type:

DictConfig

static merge(*configs)#

Merge a list of previously created configs into a single one

Parameters:

configs (DictConfig | ListConfig | Dict[str | bytes | int | Enum | float | bool, Any] | List[Any] | Tuple[Any, ...] | Any) – Input configs

Returns:

the merged config object.

Return type:

ListConfig | DictConfig

static missing_keys(cfg)#

Returns a set of missing keys in a dotlist style.

Parameters:

cfg (Any) – An OmegaConf.Container, or a convertible object via OmegaConf.create (dict, list, …).

Returns:

set of strings of the missing keys.

Raises:

ValueError – On input not representing a config.

Return type:

Set[str]

static register_new_resolver(name, resolver, *, replace=False, use_cache=False)#

Register a resolver.

Parameters:
  • name (str) – Name of the resolver.

  • resolver (Callable[[...], Any]) – Callable whose arguments are provided in the interpolation, e.g., with ${foo:x,0,${y.z}} these arguments are respectively “x” (str), 0 (int) and the value of y.z.

  • replace (bool) – If set to False (default), then a ValueError is raised if an existing resolver has already been registered with the same name. If set to True, then the new resolver replaces the previous one. NOTE: The cache on existing config objects is not affected, use OmegaConf.clear_cache(cfg) to clear it.

  • use_cache (bool) – Whether the resolver’s outputs should be cached. The cache is based only on the string literals representing the resolver arguments, e.g., ${foo:${bar}} will always return the same value regardless of the value of bar if the cache is enabled for foo.

static register_resolver(name, resolver)#
static resolve(cfg)#

Resolves all interpolations in the given config object in-place.

Parameters:

cfg (Container) – An OmegaConf container (DictConfig, ListConfig) Raises a ValueError if the input object is not an OmegaConf container.

static save(config, f, resolve=False)#

Save as configuration object to a file

Parameters:
  • config (Any) – omegaconf.Config object (DictConfig or ListConfig).

  • f (str | Path | IO[Any]) – filename or file object

  • resolve (bool) – True to save a resolved config (defaults to False)

static select(cfg, key, *, default=_DEFAULT_MARKER_, throw_on_resolution_failure=True, throw_on_missing=False)#
Parameters:
  • cfg (Container) – Config node to select from

  • key (str) – Key to select

  • default (Any) – Default value to return if key is not found

  • throw_on_resolution_failure (bool) – Raise an exception if an interpolation resolution error occurs, otherwise return None

  • throw_on_missing (bool) – Raise an exception if an attempt to select a missing key (with the value ‘???’) is made, otherwise return None

Returns:

selected value or None if not found.

Return type:

Any

static set_cache(conf, cache)#
static set_readonly(conf, value)#
static set_struct(conf, value)#
static structured(obj, parent=None, flags=None)#
static to_container(cfg, *, resolve=False, throw_on_missing=False, enum_to_str=False, structured_config_mode=SCMode.DICT)#

Resursively converts an OmegaConf config to a primitive container (dict or list).

Parameters:
  • cfg (Any) – the config to convert

  • resolve (bool) – True to resolve all values

  • throw_on_missing (bool) – When True, raise MissingMandatoryValue if any missing values are present. When False (the default), replace missing values with the string “???” in the output container.

  • enum_to_str (bool) – True to convert Enum keys and values to strings

  • structured_config_mode (SCMode) –

    Specify how Structured Configs (DictConfigs backed by a dataclass) are handled.
    • By default (structured_config_mode=SCMode.DICT) structured configs are converted to plain dicts.

    • If structured_config_mode=SCMode.DICT_CONFIG, structured config nodes will remain as DictConfig.

    • If structured_config_mode=SCMode.INSTANTIATE, this function will instantiate structured configs (DictConfigs backed by a dataclass), by creating an instance of the underlying dataclass.

    See also OmegaConf.to_object.

Returns:

A dict or a list representing this config as a primitive container.

Return type:

Dict[str | bytes | int | Enum | float | bool, Any] | List[Any] | None | str | Any

static to_object(cfg)#

Resursively converts an OmegaConf config to a primitive container (dict or list). Any DictConfig objects backed by dataclasses or attrs classes are instantiated as instances of those backing classes.

This is an alias for OmegaConf.to_container(…, resolve=True, throw_on_missing=True,

structured_config_mode=SCMode.INSTANTIATE)

Parameters:

cfg (Any) – the config to convert

Returns:

A dict or a list or dataclass representing this config.

Return type:

Dict[str | bytes | int | Enum | float | bool, Any] | List[Any] | None | str | Any

static to_yaml(cfg, *, resolve=False, sort_keys=False)#

returns a yaml dump of this config object.

Parameters:
  • cfg (Any) – Config object, Structured Config type or instance

  • resolve (bool) – if True, will return a string with the interpolations resolved, otherwise interpolations are preserved

  • sort_keys (bool) – If True, will print dict keys in sorted order. default False.

Returns:

A string containing the yaml representation.

Return type:

str

static unsafe_merge(*configs)#

Merge a list of previously created configs into a single one This is much faster than OmegaConf.merge() as the input configs are not copied. However, the input configs must not be used after this operation as will become inconsistent.

Parameters:

configs (DictConfig | ListConfig | Dict[str | bytes | int | Enum | float | bool, Any] | List[Any] | Tuple[Any, ...] | Any) – Input configs

Returns:

the merged config object.

Return type:

ListConfig | DictConfig

static update(cfg, key, value=None, *, merge=True, force_add=False)#

Updates a dot separated key sequence to a value

Parameters:
  • cfg (Container) – input config to update

  • key (str) – key to update (can be a dot separated path)

  • value (Any) – value to set, if value if a list or a dict it will be merged or set depending on merge_config_values

  • merge (bool) – If value is a dict or a list, True (default) to merge into the destination, False to replace the destination.

  • force_add (bool) – insert the entire path regardless of Struct flag or Structured Config nodes.

log_hyperparameters(object_dict)#

Control which config parts are saved by Lightning loggers.

Additionally saves:
  • Number of model parameters

Parameters:
object_dictdict[str, Any]
A dictionary containing the following objects:
  • “cfg”: A DictConfig object containing the main config.

  • “model”: The Lightning model.

  • “trainer”: The Lightning trainer.

rank_zero_only(fn, default=None)#

Wrap a function to call internal function only in rank zero.

Function that can be used as a decorator to enable a function/method being called only on global rank 0.