Config

Inheritance Diagram

Inheritance diagram of tenpy.tools.params.Config

Methods

Config.__init__(config, name)

Config.any_nonzero(keys[, log_msg])

Check for any non-zero or non-equal entries in some parameters.

Config.as_dict()

Return a copy of the options as a dictionary.

Config.clear()

Config.copy([share_unused])

Make a shallow copy, as for a dictionary.

Config.deprecated_alias(old_key, new_key[, ...])

Config.from_hdf5(hdf5_loader, h5gr, subpath)

Load instance from a HDF5 file.

Config.from_yaml(filename[, name])

Load a Config instance from a YAML file containing the options.

Config.get(key, default[, expect_type])

Find the value of key; really more like setdefault of a dict.

Config.has_nonzero(key)

Check whether self contains key, and if self[key] is nontrivial.

Config.items()

Config.keys()

Config.log(option[, action, use_default])

Print out option if verbosity and other conditions are met.

Config.pop(k[,d])

If key is not found, d is returned if given, otherwise KeyError is raised.

Config.popitem()

as a 2-tuple; but raise KeyError if D is empty.

Config.save_hdf5(hdf5_saver, h5gr, subpath)

Export self into a HDF5 file.

Config.save_yaml(filename)

Save the parameters to filename as a YAML file.

Config.setdefault(key, default)

Set a default value without reading it out.

Config.silent_get(key, default)

Find the value of key, but don't set as default value and don't print.

Config.subconfig(key[, default])

Get self[key] as a Config.

Config.touch(*keys)

Mark keys as read out to suppress warnings about those keys being unused.

Config.update([E, ]**F)

If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

Config.values()

Config.warn_unused([recursive])

Warn about (so far) unused options.

class tenpy.tools.params.Config(config, name)[source]

Bases: MutableMapping

Dict-like wrapper class for parameter/configuration dictionaries.

This class behaves mostly like a dictionary of option keys/values (together making the whole “config”) with some additional features:

  • Logging of the options the first time they get used.

  • get() acts more like dict.setdefault() such that after the algorithm, all the used default values are known and can be saved for future reference.

  • Keeping track of unused options to detect typos in the keys.

  • Nicer formatting with print(config)

  • Import/export to yaml and hdf5 files.

config Config
[No options defined for this config]
Parameters:
  • config (dict) – Dictionary containing the actual option keys and values.

  • name (str) – Descriptive name of the config used for logging.

name

Name of the dictionary, for output statements. For example, when using a Config class for DMRG, name='DMRG'.

Type:

str

options

Dictionary containing the actual option keys and values.

Type:

dict

unused

Keeps track of any options not yet used.

Type:

set

copy(share_unused=True)[source]

Make a shallow copy, as for a dictionary.

Parameters:

share_unused (bool) – Whether the unused set should be shared.

as_dict()[source]

Return a copy of the options as a dictionary.

Subconfigs are recursively converted to dict.

save_yaml(filename)[source]

Save the parameters to filename as a YAML file.

Parameters:

filename (str) – Name of the resulting YAML file.

classmethod from_yaml(filename, name=None)[source]

Load a Config instance from a YAML file containing the options.

The yaml file can have additional !py_eval tags, see load_yaml_with_py_eval().

Warning

Like pickle, it is not safe to load a yaml file from an untrusted source! A malicious file can call any Python function and should thus be treated with extreme caution.

Parameters:
  • filename (str) – Name of the YAML file

  • name (str | None) – Name of the resulting Config instance. If None, default to (the basename of) filename.

Returns:

obj – A Config object, loaded from file.

Return type:

Config

save_hdf5(hdf5_saver, h5gr, subpath)[source]

Export self into a HDF5 file.

This method saves all the data it needs to reconstruct self with from_hdf5().

This implementation saves the content of __dict__ with save_dict_content(), storing the format under the attribute 'format'.

Parameters:
  • hdf5_saver (Hdf5Saver) – Instance of the saving engine.

  • h5gr (:class`Group`) – HDF5 group which is supposed to represent self.

  • subpath (str) – The name of h5gr with a '/' in the end.

classmethod from_hdf5(hdf5_loader, h5gr, subpath)[source]

Load instance from a HDF5 file.

This method reconstructs a class instance from the data saved with save_hdf5().

Parameters:
  • hdf5_loader (Hdf5Loader) – Instance of the loading engine.

  • h5gr (Group) – HDF5 group which is represent the object to be constructed.

  • subpath (str) – The name of h5gr with a '/' in the end.

Returns:

obj – Newly generated class instance containing the required data.

Return type:

cls

warn_unused(recursive=False)[source]

Warn about (so far) unused options.

This can help to detect typos in the option keys. It is automatically called upon deletion of self, but this might be a bit later than you intended.

Parameters:

recursive (bool) – If True, check the values of self for other Config and warn in them as well.

keys() a set-like object providing a view on D's keys[source]
get(key, default, expect_type=None)[source]

Find the value of key; really more like setdefault of a dict.

If no value is set, return default and set the value of key to default internally.

Parameters:
  • option (str) – Key for the option being read out.

  • default – Default value for the parameter.

  • expect_type (str | (sequence of) type) –

    If given, we check if the returned value is an instance of any of the given types. If it is not, we issue a UserWarning. As an exception, the value None is always allowed and never triggers a warning. The following string short-hands are accepted as well:

    'real': ``numbers.Real``
    'complex': ``numbers.Complex``
    'array': ``[list, numpy.ndarray]``
    'real_or_array`: ``[numbers.Real, list, numpy.ndarray]``
    'complex_or_array`: ``[numbers.Complex, list, numpy.ndarray]``
    

Returns:

The value for option if it existed, default otherwise.

Return type:

val

silent_get(key, default)[source]

Find the value of key, but don’t set as default value and don’t print.

Same as dict.get, i.e. just return self[key] if existent, else default, without memorizing/logging the access. Does not count as read-out for the unused parameters.

setdefault(key, default)[source]

Set a default value without reading it out.

Parameters:
  • key (str) – Key name for the option being set.

  • default – The value to be set by default if the option is not yet set.

subconfig(key, default=None)[source]

Get self[key] as a Config.

touch(*keys)[source]

Mark keys as read out to suppress warnings about those keys being unused.

Parameters:

*keys (str) – Each key is marked as read out.

log(option, action='Option', use_default=False)[source]

Print out option if verbosity and other conditions are met.

Parameters:
  • option (str) – Key/option name for the parameter being read out.

  • action (str, optional) – Use to adapt log message to specific actions (e.g. “Deleting”)

any_nonzero(keys, log_msg=None)[source]

Check for any non-zero or non-equal entries in some parameters.

Parameters:
  • keys (list of {key | tuple of keys}) – For a single key, check self[key] for non-zero entries. For a tuple of keys, all the self[key] have to be equal (as numpy arrays). It is assumed that the default values for the keys are 0!

  • log_msg (None | str) – If not None, logger.debug this message with the reason if True is returned.

Returns:

match – False, if all self[key] are zero or None and True, if any of the self[key] for single key in keys, or if any of the entries for a tuple of keys

Return type:

bool

has_nonzero(key)[source]

Check whether self contains key, and if self[key] is nontrivial.

Parameters:

key (str) – Key for the parameter to check

Returns:

True if self has key key with a nontrivial value. False otherwise.

Return type:

bool

clear() None.  Remove all items from D.
items() a set-like object providing a view on D's items
pop(k[, d]) v, remove specified key and return the corresponding value.

If key is not found, d is returned if given, otherwise KeyError is raised.

popitem() (k, v), remove and return some (key, value) pair

as a 2-tuple; but raise KeyError if D is empty.

update([E, ]**F) None.  Update D from mapping/iterable E and F.

If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

values() an object providing a view on D's values