Config¶
full name: tenpy.tools.params.Config
parent module:
tenpy.tools.params
type: class
Inheritance Diagram
Methods
|
Initialize self. |
|
Check for any non-zero or non-equal entries in some parameters. |
Return a copy of the options as a dictionary. |
|
|
Make a shallow copy, as for a dictionary. |
|
|
|
Load instance from a HDF5 file. |
|
Load a Config instance from a YAML file containing the |
|
Find the value of key; really more like setdefault of a |
|
Check whether self contains key, and if self[key] is nontrivial. |
|
If key is not found, d is returned if given, otherwise KeyError is raised. |
as a 2-tuple; but raise KeyError if D is empty. |
|
|
Print out option if verbosity and other conditions are met. |
|
Export self into a HDF5 file. |
|
Save the parameters to filename as a YAML file. |
|
Set a default value without reading it out. |
|
Get |
|
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 |
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 likedict.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 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.
-
option
- Parameters
-
name
¶ Name of the dictionary, for output statements. For example, when using a Config class for DMRG,
name='DMRG'
.- Type
-
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.
-
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__
withsave_dict_content()
, storing the format under the attribute'format'
.
-
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()
.
-
warn_unused
()[source]¶ Warn about so-far unused options.
This can help to detect typos in the option keys.
-
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.
-
print_if_verbose
(option, action=None, use_default=False)[source]¶ Print out option if verbosity and other conditions are met.
-
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 theself[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 theself[key]
for single key in keys, or if any of the entries for a tuple of keys- Return type
-
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¶