scvi.utils.attrdict#

class scvi.utils.attrdict(*args, **kwargs)[source]#

A dictionary that allows for attribute-style access.

Dummy class that allows for backwards compatibility with the previous custom attrdict. Inherits from FrozenConfigDict.

Attributes table#

convert_dict

Returns True if it is converting dicts to ConfigDict automatically.

is_locked

Returns True if object is locked.

is_type_safe

Returns True if config dict is type safe.

Methods table#

as_configdict()

copy_and_resolve_references([visit_map])

Returns a ConfigDict copy with FieldReferences replaced by values.

eq_as_configdict(other)

Type-invariant equals.

get(key[, default])

Returns value if key is present, or a user defined value otherwise.

get_oneway_ref(key)

Returns a one-way FieldReference.

get_ref(key)

Returns a FieldReference initialized on key's value.

get_type(key)

Returns type of the field associated with a key.

ignore_type()

Context manager which temporarily turns off type safety recursively.

items([preserve_field_references])

Returns list of dictionary key, value pairs, sorted by key.

iteritems([preserve_field_references])

Deterministically iterates over dictionary key, value pairs.

iterkeys()

Deterministically iterates over dictionary keys, in sorted order.

itervalues([preserve_field_references])

Deterministically iterates over values in a config, sorted by their keys.

keys()

Returns the sorted list of all the keys defined in a config.

lock()

Locks object, preventing user from adding new fields.

to_dict([visit_map, preserve_field_references])

Converts ConfigDict to regular dict recursively with valid references.

to_json([json_encoder_cls])

Returns a JSON representation of the object, fails if there is a cycle.

to_json_best_effort(**kwargs)

Returns a best effort JSON representation of the object.

to_yaml(**kwargs)

Returns a YAML representation of the object.

unlock()

Grants user the ability to add new fields to ConfigDict.

unlocked()

Context manager which temporarily unlocks a ConfigDict.

update(*other, **kwargs)

Update values based on matching keys in another dict-like object.

update_from_flattened_dict(flattened_dict[, ...])

In-place updates values taken from a flattened dict.

values([preserve_field_references])

Returns the list of all values in a config, sorted by their keys.

Attributes#

attrdict.convert_dict[source]#

Returns True if it is converting dicts to ConfigDict automatically.

attrdict.is_locked[source]#

Returns True if object is locked.

attrdict.is_type_safe[source]#

Returns True if config dict is type safe.

Methods#

attrdict.as_configdict()[source]#
attrdict.copy_and_resolve_references(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: As with __eq__() and __init__(), this may not behave as expected on a ConfigDict with self-references contained in lists, tuples, or custom types.

Parameters

visit_map (default: None) – 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.

attrdict.eq_as_configdict(other)[source]#

Type-invariant equals.

This is like __eq__, except it does not distinguish FrozenConfigDict from ConfigDict. For example:

cd = ConfigDict()
fcd = FrozenConfigDict()
fcd.eq_as_configdict(cd)  # Returns True
Parameters

other – Object to compare self to.

Returns

True if self == other after conversion to ConfigDict.

Return type

same

attrdict.get(key, default=None)[source]#

Returns value if key is present, or a user defined value otherwise.

attrdict.get_oneway_ref(key)[source]#

Returns a one-way FieldReference.

Example:

cfg = ml_collections.ConfigDict(dict(a=1))
cfg.b = cfg.get_oneway_ref('a')

cfg.a = 2
print(cfg.b)  # 2

cfg.b = 3
print(cfg.a)  # 2 (would have been 3 if using get_ref())
print(cfg.b)  # 3
Parameters

key – Key for field we want to reference.

attrdict.get_ref(key)[source]#

Returns a FieldReference initialized on key’s value.

attrdict.get_type(key)[source]#

Returns type of the field associated with a key.

attrdict.ignore_type()[source]#

Context manager which temporarily turns off type safety recursively.

attrdict.items(preserve_field_references=False)[source]#

Returns list of dictionary key, value pairs, sorted by key.

Parameters

preserve_field_references (default: False) – (bool) Whether to preserve FieldReferences if the ConfigDict has them. By default, False: any FieldReferences will be resolved in the result.

Returns

The key, value pairs in the config, sorted by key.

attrdict.iteritems(preserve_field_references=False)[source]#

Deterministically iterates over dictionary key, value pairs.

Parameters

preserve_field_references (default: False) – (bool) Whether to preserve FieldReferences if the ConfigDict has them. By default, False: any FieldReferences will be resolved in the result.

Yields

The key, value pairs in the config, sorted by key.

attrdict.iterkeys()[source]#

Deterministically iterates over dictionary keys, in sorted order.

attrdict.itervalues(preserve_field_references=False)[source]#

Deterministically iterates over values in a config, sorted by their keys.

Parameters

preserve_field_references (default: False) – (bool) Whether to preserve FieldReferences if the ConfigDict has them. By default, False: any FieldReferences will be resolved in the result.

Yields

The values in the config, sorted by their corresponding keys.

attrdict.keys()[source]#

Returns the sorted list of all the keys defined in a config.

attrdict.lock()[source]#

Locks object, preventing user from adding new fields.

Returns

self

attrdict.to_dict(visit_map=None, preserve_field_references=False)[source]#

Converts ConfigDict to regular dict recursively with valid references.

By default, the output dict will not contain FieldReferences, any present in the ConfigDict will be resolved. However, if preserve_field_references is True, the output dict will contain FieldReferences where the original ConfigDict has them. They will not be the same as the ConfigDict’s, and their ops will be applied and dropped.

Note: As with __eq__() and __init__(), this may not behave as expected on a ConfigDict with self-references contained in lists, tuples, or custom types.

Parameters
  • visit_map (default: None) – A mapping from object ids to their dict representation. Method is recursive in nature, and it will call “.to_dict(visit_map)” on each encountered object, unless it is already in visit_map.

  • preserve_field_references (default: False) – (bool) Whether the output dict should have FieldReferences if the ConfigDict has them. By default, False: any FieldReferences will be resolved and the result will go to the dict.

Returns

Dictionary with the same values and references structure as a calling

ConfigDict.

attrdict.to_json(json_encoder_cls=None, **kwargs)[source]#

Returns a JSON representation of the object, fails if there is a cycle.

Parameters
  • json_encoder_cls (default: None) – An optional JSON encoder class to customize JSON serialization.

  • **kwargs – Keyword arguments for json.dumps. They cannot contain “cls” as this method specifies it on its own.

Returns

JSON representation of the object.

Raises

TypeError – If self contains set, frozenset, custom type fields or any other objects that are not JSON serializable.

attrdict.to_json_best_effort(**kwargs)[source]#

Returns a best effort JSON representation of the object.

Tries to serialize objects not inherently supported by JSON encoder. This may result in the configdict being partially serialized, skipping the unserializable bits. Ensures that no errors are thrown. Fails if there is a cycle.

Parameters

**kwargs – Keyword arguments for json.dumps. They cannot contain “cls” as this method specifies it on its own.

Returns

JSON representation of the object.

attrdict.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 – Keyword arguments for yaml.dump.

Returns

YAML representation of the object.

attrdict.unlock()[source]#

Grants user the ability to add new fields to ConfigDict.

In most cases, the unlocked() context manager should be preferred to the direct use of the unlock method.

Returns

self

attrdict.unlocked()[source]#

Context manager which temporarily unlocks a ConfigDict.

attrdict.update(*other, **kwargs)[source]#

Update values based on matching keys in another dict-like object.

Mimics the built-in dict’s update method: iterates over a given mapping object and adds/overwrites keys with the given mapping’s values for those keys.

Differs from dict.update in that it operates recursively on existing keys that are already a ConfigDict (i.e. calls their update() on the corresponding value from other), and respects the ConfigDict’s type safety status.

If keyword arguments are specified, the ConfigDict is updated with those key/value pairs.

Parameters
  • *other – A (single) dict-like container, e.g. a dict or ConfigDict.

  • **kwargs – Additional keyword arguments to update the ConfigDict.

Raises

TypeError – if more than one value for other is specified.

attrdict.update_from_flattened_dict(flattened_dict, strip_prefix='')[source]#

In-place updates values taken from a flattened dict.

This allows a potentially nested source ConfigDict of the following form:

cfg = ConfigDict({
    'a': 1,
    'b': {
        'c': {
            'd': 2
        }
    }
})

to be updated from a dict containing paths navigating to child items, of the following form:

updates = {
    'a': 2,
    'b.c.d': 3
}

This filters paths_dict to only contain paths starting with strip_prefix and strips the prefix when applying the update.

For example, consider we have the following values returned as flags:

flags = {
    'flag1': x,
    'flag2': y,
    'config': 'some_file.py',
    'config.a.b': 1,
    'config.a.c': 2
}

config = ConfigDict({
    'a': {
        'b': 0,
        'c': 0
    }
})

config.update_from_flattened_dict(flags, 'config.')

Then we will now have:

config = ConfigDict({
    'a': {
        'b': 1,
        'c': 2
    }
})
Parameters
  • flattened_dict – A mapping (key path) -> value.

  • strip_prefix (default: '') – A prefix to be stripped from path. If specified, only paths matching strip_prefix will be processed.

Raises

KeyError – if any of the key paths can’t be found.

attrdict.values(preserve_field_references=False)[source]#

Returns the list of all values in a config, sorted by their keys.

Parameters

preserve_field_references (default: False) – (bool) Whether to preserve FieldReferences if the ConfigDict has them. By default, False: any FieldReferences will be resolved in the result.

Returns

The values in the config, sorted by their corresponding keys.