topobench.utils.config_resolvers module#

Configuration resolvers for the topobench package.

class OmegaConf#

Bases: object

OmegaConf primary class

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

classmethod has_resolver(name)#
static clear_cache(conf)#
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=_DEFAULT_MARKER_, parent=None, flags=None)#
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)#
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.

__init__()#
class defaultdict#

Bases: dict

defaultdict(default_factory=None, /, […]) –> dict with default factory

The default factory is called without arguments to produce a new value when a key is not present, in __getitem__ only. A defaultdict compares equal to a dict with the same items. All remaining arguments are treated the same as if they were passed to the dict constructor, including keyword arguments.

__init__(*args, **kwargs)#
copy() a shallow copy of D.#
default_factory#

Factory for default value called by __missing__().

check_fes_in_transforms(transforms)#

Check if there are feature encodings in the transforms.

Parameters:
transformsDictConfig

Configuration parameters for the transforms.

Returns:
int

Count of the number of features added by the encodings.

check_pses_in_transforms(transforms)#

Check if there are positional or structural encodings in the transforms.

Parameters:
transformsDictConfig

Configuration parameters for the transforms.

Returns:
int

Count of the number of features added by the encodings.

define_task_level(dataset_task_level, learning_setting)#

Define the task level for a given dataset task level and learning setting.

Parameters:
dataset_task_levelstr

Task level defined in the dataset configuration file.

learning_settingstr

Learning setting defined in the dataset split parameters.

Returns:
str

Task level for the model.

Raises:
ValueError

If the dataset task level or learning setting is invalid.

get_all_encoding_dimensions(encodings, parameters)#

Get dimensions of all encodings (PSEs and FEs) in order.

Parameters:
encodingslist

List of all encodings (both PSEs and FEs).

parametersdict

Dictionary of parameters for all encodings.

Returns:
list

List with dimensions of all encodings in the same order as input.

get_default_metrics(task, num_classes, metrics=None)#

Get default metrics for a given task.

Parameters:
taskstr

Task, either “classification” or “regression”.

num_classesint

Number of classes, relevant for multilabel and multioutput tasks.

metricslist, optional

List of metrics to be used. If None, the default metrics will be used.

Returns:
list

List of default metrics.

Raises:
ValueError

If the task is invalid.

get_default_trainer()#

Get default trainer configuration.

Returns:
str

Default trainer configuration file name.

get_default_transform(dataset, model)#

Get default transform for a given data domain and model.

Parameters:
datasetstr

Dataset name. Should be in the format “data_domain/name”.

modelstr

Model name. Should be in the format “model_domain/name”.

Returns:
str

Default transform.

get_fes_dimensions(encodings, parameters)#

Get dimensions of feature encodings.

Parameters:
encodingslist

List of feature encodings.

parametersdict

Dictionary of parameters for the feature encodings.

Returns:
list

List with dimensions of the feature encodings.

get_flattened_channels(num_nodes, channels)#

Get the output dimension of flattening a feature matrix.

Parameters:
num_nodesint

Hidden dimension for the first layer.

channelsint

Channel dimension.

Returns:
int

Flatenned cchannels dimension.

get_list_element(list, index)#

Get element of a list.

Parameters:
listlist

List of elements.

indexint

Index of the element to get.

Returns:
any

Element of the list.

get_monitor_metric(task, metric)#

Get monitor metric for a given task.

Parameters:
taskstr

Task, either “classification” or “regression”.

metricstr

Name of the metric function.

Returns:
str

Monitor metric.

Raises:
ValueError

If the task is invalid.

get_monitor_mode(task)#

Get monitor mode for a given task.

Parameters:
taskstr

Task, either “classification” or “regression”.

Returns:
str

Monitor mode, either “max” or “min”.

Raises:
ValueError

If the task is invalid.

get_non_relational_out_channels(num_nodes, channels, task_level)#

Get the output dimension for a non-relational model.

Parameters:
num_nodesint

Number of nodes in the input graph.

channelsint

Channel dimension.

task_levelint

Task level for the model.

Returns:
int

Output dimension.

get_pse_dimensions(encodings, parameters)#

Get dimensions of positional or structural encodings.

Parameters:
encodingslist

List of positional or structural encodings.

parametersdict

Dictionary of parameters for the positional or structural encodings, which should contain the key “parameters” with the parameters for each encoding.

Returns:
list

List with dimensions of the positional or structural encodings.

get_required_lifting(data_domain, model)#

Get required transform for a given data domain and model.

Parameters:
data_domainstr

Dataset domain.

modelstr

Model name. Should be in the format “model_domain/name”.

Returns:
str

Required transform.

get_routes_from_neighborhoods(neighborhoods)#

Get the routes from the neighborhoods.

Combination of src_rank, dst_rank. ex: [[0, 0], [1, 0], [1, 1], [1, 1], [2, 1]].

Parameters:
neighborhoodslist

List of neighborhoods of interest.

Returns:
list

List of routes.

infer_in_channels(dataset, transforms)#

Infer the number of input channels for a given dataset.

Parameters:
datasetDictConfig

Configuration parameters for the dataset.

transformsDictConfig

Configuration parameters for the transforms.

Returns:
list

List with dimensions of the input channels.

infer_in_hasse_graph_agg_dim(neighborhoods, dim_pses, complex_dim, max_hop, dim_in, dim_hidden_graph, dim_hidden_node, copy_initial, use_edge_attr)#

Compute which input dimensions need to changed based on if they are the output of a neighborhood.

Set the list of dimensions as outputs to the hasse graph as a GNN

Parameters:
neighborhoodsList[str]

List of strings representing the neighborhood.

dim_psesList[int]

List of dimensions of the positional or structural encodings.

complex_dimint

Maximum dimension of the complex.

max_hopint

Maximum number of hops (counting the intial features).

dim_inint

The dataset feature input dimension.

dim_hidden_graphint

The output hidden dimension of the GNN over the Hasse Graph aggregation.

dim_hidden_nodeint

The output hidden dimension of the GNN over the Hasse Graph for each node.

copy_initialbool

If the initial features should be copied as the 0-th hop.

use_edge_attrbool

If the edge attributes are used as features in the 1-cells and should be considered for channel inference.

Returns:
np.ndarray

A 2D array where.

infer_in_khop_feature_dim(dataset_in_channels, max_hop, complex_dim=None)#

Infer the dimension of the feature vector in the SANN k-hop model.

Parameters:
dataset_in_channelsnp.ndarray

1D array of input channels for the dataset.

max_hopint

Maximum hop distance.

complex_dimint, optional

Number of cell ranks processed by the transform. When provided, dataset_in_channels is truncated to this length so the recursive formula only considers ranks that actually appear in the k-hop feature computation.

Returns:
int

Dimension of the feature vector in the SANN k-hop model.

infer_list_length(list)#

Infer the length of a list.

Parameters:
listlist

List.

Returns:
int

Length of the input list.

infer_list_length_plus_one(list)#

Infer the length of a list plus one.

Parameters:
listlist

List.

Returns:
int

Length of the input list plus one.

infer_num_cell_dimensions(selected_dimensions, in_channels)#

Infer the length of a list.

Parameters:
selected_dimensionslist

List of selected dimensions. If not None it will be used to infer the length.

in_channelslist

List of input channels. If selected_dimensions is None, this list will be used to infer the length.

Returns:
int

Length of the input list.

infer_topotune_num_cell_dimensions(neighborhoods)#

Infer the length of a list.

Parameters:
neighborhoodslist

List of neighborhoods.

Returns:
int

Length of the input list.

register_all_resolvers()#

Register all custom OmegaConf resolvers.

This centralizes resolver registration to avoid duplication across modules. Should be called before Hydra initialization in any script that uses configs.

set_preserve_edge_attr(model_name, default=True)#

Set the preserve_edge_attr parameter of datasets depending on the model.

Parameters:
model_namestr

Model name.

defaultbool, optional

Default value for the parameter. Defaults to True.

Returns:
bool

Default if the model can preserve edge attributes, False otherwise.