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 |
|
Functions¶
|
Check whether |
|
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 |
|
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Wrap a function or method to use the neutral style by default. |
|
|
A decorator that suggests the right keyword arguments if you get them |
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
value
takes one ofvalid
options, and raise an informative error if not.
- 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.tqdm
A 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
x
with errorerr
, format a string showing the relevant digits ofx
with 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.OrderedDict
Least recently used dict, which evicts old items. Taken from python collections OrderedDict docs.
- maxsize¶
- class quimb.utils.ExponentialGeometricRollingDiffMean(factor=1 / 3, initial=1.0)[source]¶
- x_prev = None¶
- dx = 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.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_map
andtree_apply
.- Parameters:
cls (type) – The container type to register.
mapper (callable) – A function that takes
f
,tree
andis_leaf
and returns a new tree of typecls
withf
applied to all leaves.applier (callable) – A function that takes
f
,tree
andis_leaf
and appliesf
to 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
f
over 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,
f
is 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,
f
is 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
f
to 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,
f
is 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
tree
into 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)
returnsTrue
are returned in the flattened list.
- Returns:
objs (list) – The flattened list of leaf objects.
(ref_tree) (pytree) – If
get_ref
isTrue
, 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
objs
into 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)
returnsTrue
will have the next item fromobjs
inserted. By default checks for theLeaf
object inserted bytree_flatten(..., get_ref=True)
.
- Return type:
pytree
- quimb.utils.NEUTRAL_STYLE¶
- quimb.utils.default_to_neutral_style(fn)[source]¶
Wrap a function or method to use the neutral style by default.
- 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.