Config

Inheritance Diagram

Inheritance diagram of tenpy.tools.params.Config

Methods

Config.__init__(config, name)

Initialize self.

Config.any_nonzero(keys[, verbose_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)

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.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.print_if_verbose(option[, action, …])

Print out option if verbosity and other conditions are met.

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.subconfig(key[, default])

Get self[key] as a Config.

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()

Warn about so-far unused options.

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

Bases: collections.abc.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:

  • Printing of the options used, with the level of how much should be printed adjustable by the option verbose.

  • 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
option summary

verbose

How much to print what's being done; higher means print more. [...]

option verbose: int | float = 1

How much to print what’s being done; higher means print more. Sub-configs default to having the parent verbose divided by 10.

Parameters
  • config (dict) – Dictionary containing the actual option keys and values.

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

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

verbose

Verbosity level for output statements.

Type

float

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 recursivley 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.

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()[source]

Warn about so-far unused options.

This can help to detect typos in the option keys.

keys() → a set-like object providing a view on D’s keys[source]
get(key, default)[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.

Returns

The value for option if it existed, default otherwise.

Return type

val

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.

print_if_verbose(option, action=None, 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 printout message to specific actions (e.g. “Deleting”)

any_nonzero(keys, verbose_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).

  • verbose_msg (None | str) – If verbose >= 1, we print verbose_msg before checking, and a short notice with the key, if a non-zero entry is found.

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