trieste.utils#

This package contains library utilities.

Package Contents#

class DEFAULTS[source]#

Default constants used in Trieste.

JITTER: typing_extensions.Final[float] = 1e-06#

The default jitter, typically used to stabilise computations near singular points, such as in Cholesky decomposition.

class Err(exc: Exception)[source]#

Bases: Result[NoReturn]

Wraps the exception that occurred during a failed evaluation.

Parameters:

exc – The exception that occurred.

property is_ok: bool#

False always.

unwrap() NoReturn[source]#
Raises:

Exception – Always. Raises the wrapped exception.

K[source]#

An unbound type variable.

class Ok(value: ResultType)[source]#

Bases: Result[ResultType]

Wraps the result of a successful evaluation.

Parameters:

value – The result of a successful evaluation.

property is_ok: bool#

True always.

unwrap() ResultType[source]#
Returns:

The wrapped value.

class Result[source]#

Bases: Generic[ResultType], abc.ABC

Represents the result of an operation that can fail with an exception. It contains either the operation return value (in an Ok), or the exception raised (in an Err).

To check whether instances such as

>>> res = Ok(1)
>>> other_res = Err(ValueError("whoops"))

contain a value, use is_ok (or is_err)

>>> res.is_ok
True
>>> other_res.is_ok
False

We can access the value if it is_ok using unwrap().

>>> res.unwrap()
1

Trying to access the value of a failed Result, or Err, will raise the wrapped exception

>>> other_res.unwrap()
Traceback (most recent call last):
    ...
ValueError: whoops

Note: This class is not intended to be subclassed other than by Ok and Err.

abstract property is_ok: bool#

True if this Result contains a value, else False.

property is_err: bool#

True if this Result contains an error, else False. The opposite of is_ok.

abstract unwrap() ResultType[source]#
Returns:

The contained value, if it exists.

Raises:

Exception – If there is no contained value.

ResultType[source]#

An unbounded covariant type variable.

class Timer[source]#

Functionality for timing chunks of code. For example: >>> from time import sleep >>> with Timer() as timer: sleep(2.0) >>> timer.time # doctest: +SKIP 2.0

U[source]#

An unbound type variable.

V[source]#

An unbound type variable.

flatten_leading_dims(x: trieste.types.TensorType, output_dims: int = 2) Tuple[trieste.types.TensorType, Callable[[trieste.types.TensorType], trieste.types.TensorType]][source]#

Flattens the leading dimensions of x (all but the last output_dims dimensions), and returns a function that can be used to restore them (typically after first manipulating the flattened tensor).

jit(apply: bool = True, **optimize_kwargs: Any) Callable[[C], C][source]#

A decorator that conditionally wraps a function with tf.function.

Parameters:
  • apply – If True, the decorator is equivalent to tf.function. If False, the decorator does nothing.

  • optimize_kwargs – Additional arguments to tf.function.

Returns:

The decorator.

map_values(f: Callable[[U], V], mapping: Mapping[K, U]) Mapping[K, V][source]#

Apply f to each value in mapping and return the result. If f does not modify its argument, map_values() does not modify mapping. For example:

>>> import math
>>> squares = {'a': 1, 'b': 4, 'c': 9}
>>> map_values(math.sqrt, squares)['b']
2.0
>>> squares
{'a': 1, 'b': 4, 'c': 9}
Parameters:
  • f – The function to apply to the values in mapping.

  • mapping – A mapping.

Returns:

A new mapping, whose keys are the same as mapping, and values are the result of applying f to each value in mapping.

shapes_equal(this: trieste.types.TensorType, that: trieste.types.TensorType) trieste.types.TensorType[source]#

Return a scalar tensor containing: True if this and that have equal runtime shapes, else False.

to_numpy(t: trieste.types.TensorType) numpy.ndarray[Any, Any][source]#
Parameters:

t – An array-like object.

Returns:

t as a NumPy array.