quimb.utils¶
Misc utility functions.
Attributes¶
Classes¶
A continuous version of tqdm, so that it can be updated with a float |
|
Decorator for making functions print their inputs. Simply for |
|
An ordered set which stores elements as the keys of dict (ordered as of |
|
Least recently used dict, which evicts old items. Taken from python |
|
Tracker for the absolute rolling mean of diffs between values, to |
|
Track the absolute rolling mean of diffs between values, weighted in a |
|
Functions¶
|
Check whether |
|
For functions that take an optional |
|
Check if library is installed. |
|
Return function to flag up a missing necessary library. |
|
Mark a function as deprecated, and indicate the new name. |
|
|
|
Make sure |
|
Iterate over each pair of neighbours in |
|
Print multiple lines, with a maximum width. |
|
Given |
|
Save an object to disk using joblib.dump. |
|
Load an object form disk using joblib.load. |
|
Generate all unique bipartitions of |
Generate all unique bipartitions of |
|
|
Register a new container type for use with |
The default function to determine if an object is a leaf. This simply |
|
|
|
|
Map |
|
|
|
Iterate over all leaves in |
|
|
|
Apply |
|
Flatten |
|
Unflatten |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
A decorator that suggests the right keyword arguments if you get them |
|
Hash a set of keyword arguments to a deterministic integer, which can be |
Module Contents¶
- quimb.utils.last¶
- quimb.utils._CHECK_OPT_MSG = "Option `{}` should be one of {}, but got '{}'."¶
- quimb.utils.check_opt(name, value, valid)[source]¶
Check whether
valuetakes one ofvalidoptions, and raise an informative error if not.
- quimb.utils.parse_info_extras(info=None, default=()) dict[source]¶
For functions that take an optional
infodict, this function populates which quantities to compute based on the keys ofinfo. Ifinfo is None, nothing is inserted, ifinfo == {}, all default keys are inserted, but ifinfois a dict with some keys, only those keys will remain, assuming they specify a manual subset.
- quimb.utils.raise_cant_find_library_function(x, extra_msg=None)[source]¶
Return function to flag up a missing necessary library.
This is simplify the task of flagging optional dependencies only at the point at which they are needed, and not earlier.
- quimb.utils.FOUND_TQDM¶
- class quimb.utils.continuous_progbar(*args, total=100, **kwargs)[source]¶
Bases:
tqdm.tqdmA continuous version of tqdm, so that it can be updated with a float within some pre-given range, rather than a number of steps.
- Parameters:
args ((stop) or (start, stop)) – Stopping point (and starting point if
len(args) == 2) of window within which to evaluate progress.total (int) – The number of steps to represent the continuous progress with.
kwargs – Supplied to
tqdm.tqdm
- range¶
- step = 1¶
- quimb.utils.deprecated(fn, old_name, new_name)[source]¶
Mark a function as deprecated, and indicate the new name.
- quimb.utils.print_multi_line(*lines, max_width=None)[source]¶
Print multiple lines, with a maximum width.
- quimb.utils.format_number_with_error(x, err)[source]¶
Given
xwith errorerr, format a string showing the relevant digits ofxwith two significant digits of the error bracketed, and overall exponent if necessary.Examples
>>> print_number_with_uncertainty(0.1542412, 0.0626653) '0.154(63)'
>>> print_number_with_uncertainty(-128124123097, 6424) '-1.281241231(64)e+11'
- quimb.utils.save_to_disk(obj, fname, **dump_opts)[source]¶
Save an object to disk using joblib.dump.
- class quimb.utils.Verbosify(fn, highlight=None, mpi=False)[source]¶
Decorator for making functions print their inputs. Simply for illustrating a MPI example in the docs.
- fn¶
- highlight = None¶
- mpi = False¶
- class quimb.utils.oset(it=())[source]¶
An ordered set which stores elements as the keys of dict (ordered as of python 3.6). ‘A few times’ slower than using a set directly for small sizes, but makes everything deterministic.
- __slots__ = ('_d',)¶
- _d¶
- class quimb.utils.LRU(maxsize, *args, **kwds)[source]¶
Bases:
collections.OrderedDictLeast recently used dict, which evicts old items. Taken from python collections OrderedDict docs.
- maxsize¶
- class quimb.utils.RollingDiffMean(size=16)[source]¶
Tracker for the absolute rolling mean of diffs between values, to assess effective convergence.
- size = 16¶
- diffs = []¶
- last_y = None¶
- dxsum = 0.0¶
- class quimb.utils.ExponentialGeometricRollingDiffMean(factor=1 / 3, initial=1.0)[source]¶
Track the absolute rolling mean of diffs between values, weighted in a exponentially decaying geometric fashion.
- y_prev = None¶
- x_prev = None¶
- dy = None¶
- value = 1.0¶
- factor = 0.3333333333333333¶
- quimb.utils.gen_bipartitions(it)[source]¶
Generate all unique bipartitions of
it. Unique meaning(1, 2), (3, 4)is considered the same as(3, 4), (1, 2).
- quimb.utils.gen_bipartitions_balanced_first(it)[source]¶
Generate all unique bipartitions of
it, yield most balanced ones first. Unique up to swapping the two sites.- Parameters:
it (iterable) – The items to bipartition.
- Yields:
(left, right) (tuple) – A bipartition of the items, with the most balanced ones first.
- quimb.utils.TREE_MAP_REGISTRY¶
- quimb.utils.TREE_APPLY_REGISTRY¶
- quimb.utils.TREE_ITER_REGISTRY¶
- quimb.utils.tree_register_container(cls, mapper, iterator, applier)[source]¶
Register a new container type for use with
tree_mapandtree_apply.- Parameters:
cls (type) – The container type to register.
mapper (callable) – A function that takes
f,treeandis_leafand returns a new tree of typeclswithfapplied to all leaves.applier (callable) – A function that takes
f,treeandis_leafand appliesfto all leaves intree.
- quimb.utils.IS_CONTAINER_CACHE¶
- quimb.utils.is_not_container(x)[source]¶
The default function to determine if an object is a leaf. This simply checks if the object is an instance of any of the registered container types.
- quimb.utils.TREE_MAPPER_CACHE¶
- quimb.utils.tree_map(f, tree, is_leaf=is_not_container)[source]¶
Map
fover all leaves intree, returning a new pytree.- Parameters:
f (callable) – A function to apply to all leaves in
tree.tree (pytree) – A nested sequence of tuples, lists, dicts and other objects.
is_leaf (callable) – A function to determine if an object is a leaf,
fis only applied to objects for whichis_leaf(x)returnsTrue.
- Return type:
pytree
- quimb.utils.TREE_ITER_CACHE¶
- quimb.utils.tree_iter(tree, is_leaf=is_not_container)[source]¶
Iterate over all leaves in
tree.- Parameters:
f (callable) – A function to apply to all leaves in
tree.tree (pytree) – A nested sequence of tuples, lists, dicts and other objects.
is_leaf (callable) – A function to determine if an object is a leaf,
fis only applied to objects for whichis_leaf(x)returnsTrue.
- quimb.utils.TREE_APPLIER_CACHE¶
- quimb.utils.tree_apply(f, tree, is_leaf=is_not_container)[source]¶
Apply
fto all leaves intree, no new pytree is built.- Parameters:
f (callable) – A function to apply to all leaves in
tree.tree (pytree) – A nested sequence of tuples, lists, dicts and other objects.
is_leaf (callable) – A function to determine if an object is a leaf,
fis only applied to objects for whichis_leaf(x)returnsTrue.
- quimb.utils.Leaf¶
- quimb.utils.tree_flatten(tree, get_ref=False, is_leaf=is_not_container)[source]¶
Flatten
treeinto a list of leaves.- Parameters:
tree (pytree) – A nested sequence of tuples, lists, dicts and other objects.
is_leaf (callable) – A function to determine if an object is a leaf, only objects for which
is_leaf(x)returnsTrueare returned in the flattened list.
- Returns:
objs (list) – The flattened list of leaf objects.
(ref_tree) (pytree) – If
get_refisTrue, a reference tree, with leaves of typeLeaf, is returned which can be used to reconstruct the original tree.
- quimb.utils.tree_unflatten(objs, tree, is_leaf=is_leaf_object)[source]¶
Unflatten
objsinto a pytree of the same structure astree.- Parameters:
objs (sequence) – A sequence of objects to be unflattened into a pytree.
tree (pytree) – A nested sequence of tuples, lists, dicts and other objects, the objs will be inserted into a new pytree of the same structure.
is_leaf (callable) – A function to determine if an object is a leaf, only objects for which
is_leaf(x)returnsTruewill have the next item fromobjsinserted. By default checks for theLeafobject inserted bytree_flatten(..., get_ref=True).
- Return type:
pytree
- quimb.utils.autocorrect_kwargs(func=None, valid_kwargs=None)[source]¶
A decorator that suggests the right keyword arguments if you get them wrong. Useful for functions with many specific options.
- Parameters:
func (callable, optional) – The function to decorate.
valid_kwargs (sequence[str], optional) – The valid keyword arguments for
func, if not given these are inferred from the function signature.