vis4d.config

Config modules.

class FieldConfigDict(initial_dictionary=None, type_safe=True, convert_dict=True)[source]

A configuration dict which allows to access fields via dot notation.

This class is a subclass of ConfigDict and overwrites the dot notation to return a FieldReference instead of a dict.

For more information on the ConfigDict class, see:

ml_collections.ConfigDict.

Examples of using the ref and value mode:
>>> config = FieldConfigDict({"a": 1, "b": 2})
>>> type(config.a)
<class 'ml_collections.field_reference.FieldReference'>
>>> config.value_mode() # Set the config to return values
>>> type(config.a)
<class 'int'>

Creates an instance of FieldConfigDict.

Parameters:
  • initial_dictionary (Optional[Mapping[str, Any]]) –

    May be one of the following:

    1) dict. In this case, all values of initial_dictionary that are dictionaries are also be converted to ConfigDict. However, dictionaries within values of non-dict type are untouched.

    2) ConfigDict. In this case, all attributes are uncopied, and only the top-level object (self) is re-addressed. This is the same behavior as Python dict, list, and tuple.

    3) FrozenConfigDict. In this case, initial_dictionary is converted to a ConfigDict version of the initial dictionary for the FrozenConfigDict (reversing any mutability changes FrozenConfigDict made).

  • type_safe (bool) – If set to True, once an attribute value is assigned, its type cannot be overridden without .ignore_type() context manager.

  • convert_dict (bool) – If set to True, all dict used as value in the ConfigDict will automatically be converted to ConfigDict.

__getitem__(key)[source]

Returns the reference for the given key.

Return type:

FieldReference

dump(output_path)[source]

Writes the config to a .yaml file.

Parameters:

output_path (str) – The path to the output file.

Return type:

None

classmethod from_yaml(path)[source]

Creates a config from a .yaml file.

Parameters:

path (str) – The path to the .yaml file that should be loaded.

Return type:

FieldConfigDict

ref_mode()[source]

Sets the config to return references instead of values.

Return type:

FieldConfigDict

set_ref_mode(ref_mode)[source]

Sets the config to return references instead of values.

Return type:

None

to_yaml(**kwargs)[source]

Returns a YAML representation of the object.

ConfigDict serializes types of fields as well as the values of fields themselves. Deserializing the YAML representation hence requires using YAML’s UnsafeLoader:

` yaml.load(cfg.to_yaml(), Loader=yaml.UnsafeLoader) `

or equivalently:

` yaml.unsafe_load(cfg.to_yaml()) `

Please see the PyYAML documentation and https://msg.pyyaml.org/load for more details on the consequences of this.

Parameters:

**kwargs (Any) – Keyword arguments for yaml.dump.

Return type:

str

Returns:

YAML representation of the object.

value_mode()[source]

Sets the config to return values instead of references.

Return type:

FieldConfigDict

class_config(clazz, **kwargs)[source]

Creates a configuration which can be instantiated as a class.

This function creates a configuration dict which can be passed to ‘instantiate_classes’ to create a instance of the given class or functor.

Example: >>> class_cfg_obj = class_config(“your.module.Module”, arg1=”arg1”, arg2=2) >>> print(class_cfg_obj) >>> # Prints : >>> class_path: your.module.Module >>> init_args: >>> arg1: arg1 >>> arg2: 2

>>> # instantiate object
>>> inst_obj = instantiate_classes(class_cfg_obj)
>>> print(type(inst_obj)) # -> Will print <class 'your.module.Module'>
>>> # Example by directly passing objects:
>>> class MyClass:
>>>     def __init__(self, name: str, age: int):
>>>         self.name = name
>>>         self.age = age
>>> class_cfg_obj = class_config(MyClass, name="John", age= 25)
>>> print(class_cfg_obj)
>>> # Prints :
>>> class_path: __main__.MyClass
>>> init_args:
>>>   name: John
>>>   age: 25
>>> # instantiate object
>>> inst_obj = instantiate_classes(class_cfg_obj)
>>> print(type(inst_obj)) # -> Will print <class '__main__.MyClass'>
>>> print(inst_obj.name) # -> Will print John
Parameters:
  • clazz (type | Callable[[Any], Any] | str) – class type or functor or class string path.

  • **kwargs (ArgsType) – Kwargs to pass to the class constructor.

Returns:

_description_

Return type:

ConfigDict

copy_and_resolve_references(data, visit_map=None)[source]

Returns a ConfigDict copy with FieldReferences replaced by values.

If the object is a FrozenConfigDict, the copy returned is also a FrozenConfigDict. However, note that FrozenConfigDict should already have FieldReferences resolved to values, so this method effectively produces a deep copy.

Note: This method is overwritten from the ConfigDict class and allows to also resolve FieldReferences in list, tuple and dict.

Parameters:
  • data (Any) – object to copy.

  • visit_map (dict[int, Any]) – A mapping from ConfigDict object ids to their copy. Method is recursive in nature, and it will call “copy_and_resolve_references(visit_map)” on each encountered object, unless it is already in visit_map.

Returns:

ConfigDict copy with previous FieldReferences replaced by values.

Return type:

Any

delay_instantiation(instantiable)[source]

Delays the instantiation of the given configuration object.

This is a somewhat hacky way to delay the initialization of the optimizer configuration object. It works by replacing the class_path with _class_path which basically tells the instantiate_classes function to not instantiate the class. Instead, it returns a function that can be called to instantiate the class

Parameters:

instantiable (ConfigDict) – The configuration object to delay the instantiation of.

Return type:

ConfigDict

instantiate_classes(data, **kwargs)[source]

Instantiates all classes in a given ConfigDict.

This function iterates over the configuration data and instantiates all classes. Class defintions are provided by a config dict that has the following structure:

{

‘data_path’: ‘path.to.my.class.Class’, ‘init_args’: ConfigDict(

{

‘arg1’: ‘value1’, ‘arg2’: ‘value2’,

}

)

}

Parameters:
  • data (ConfigDict | FieldReference) – The general configuration object.

  • **kwargs (ArgsType) – Additional arguments to pass to the class constructor.

Returns:

The instantiated objects.

Return type:

ConfigDict | Any

Modules

vis4d.config.config_dict

Config dict module.

vis4d.config.registry

Utility function for registering config files.

vis4d.config.replicator

Replication methods to perform different parameters sweeps.

vis4d.config.show_connection

Show connected components in the config.

vis4d.config.sweep

Helper functions for creating sweep configurations.

vis4d.config.typing

Type definitions for configuration files.