auto_uncertainties.uncertainty.uncertainty_containers

Attributes

UType

Type alias for the underlying data types supporting Uncertainty objects.

VectorUncertainty

Alias for Uncertainty to maintain backward compatibility.

ScalarUncertainty

Alias for Uncertainty to maintain backward compatibility.

Classes

Uncertainty

Representation of a central value and its associated uncertainty.

Functions

nominal_values(…)

Return the central value of an Uncertainty object if it is one, otherwise returns the object.

std_devs(…)

Return the uncertainty of an Uncertainty object if it is one, otherwise returns zero.

Module Contents

class auto_uncertainties.uncertainty.uncertainty_containers.UType[source]

Type alias for the underlying data types supporting Uncertainty objects.

TYPE ALIAS:

class auto_uncertainties.uncertainty.uncertainty_containers.Uncertainty(value: int, error: ErrT | None = ...)[source]
class auto_uncertainties.uncertainty.uncertainty_containers.Uncertainty(value, error=...)
class auto_uncertainties.uncertainty.uncertainty_containers.Uncertainty(value, error=...)
class auto_uncertainties.uncertainty.uncertainty_containers.Uncertainty(value)
class auto_uncertainties.uncertainty.uncertainty_containers.Uncertainty(value)
class auto_uncertainties.uncertainty.uncertainty_containers.Uncertainty(value)
class auto_uncertainties.uncertainty.uncertainty_containers.Uncertainty(value, error=...)
class auto_uncertainties.uncertainty.uncertainty_containers.Uncertainty(value, error=..., skip=...)

Bases: Generic[T], auto_uncertainties.UncertaintyDisplay

Inheritance diagram of auto_uncertainties.uncertainty.uncertainty_containers.Uncertainty

Representation of a central value and its associated uncertainty.

Parameters can be numbers, sequences, numpy arrays, pint.Quantity objects, other Uncertainty objects, or lists / tuples of Uncertainty objects.

The Uncertainty class automatically determines which methods should be implemented based on whether it represents a vector uncertainty, or a scalar uncertainty. When instantiated with sequences or numpy arrays, vector-based operations are enabled; when instantiated with scalars, only scalar operations are permitted.

Uncertainty objects only support float-based data types. If integers or integer arrays are passed as parameters to the Uncertainty constructor, they will be cast to float (or numpy.float64 if a numpy.integer subclass is detected).

Parameters:
  • value – The central value(s)

  • error – The uncertainty value(s). Zero if not provided.

Raises:
  • NegativeStdDevError – If err is negative, or contains negative values

  • TypeError – If the parameters are of incompatible types

  • ValueError – If the parameters have incomaptible values (e.g., misaligned array sizes)

Returns:

An initialized Uncertainty object

Example
>>> u1 = Uncertainty(1.25, 0.25)
>>> u2 = Uncertainty([1.4, 2.8, 0.09], [0.1, 0.14, 0.12])
>>> u3 = Uncertainty([1.4, 2.8, 0.09], 0.1)
>>> u4 = Uncertainty(u1)
>>> u5 = Uncertainty(np.array([1.4, 2.8]), np.array([0.1, 0.14]))
>>> u6 = Uncertainty(np.array([1.4, 2.8]), 0.1)

>>> u3.value
array([1.4 , 2.8 , 0.09])

>>> u3.error
array([0.1, 0.1, 0.1])

>>> np.cos(u1)
0.315322 +/- 0.237246

>>> u4 == u1
True
Pint Quantity Example
>>> from pint import Quantity
>>> val = Quantity(2.24, 'kg')
>>> err = Quantity(0.208, 'kg')
>>> new_quantity = Uncertainty(val, err)
>>> new_quantity
<Quantity(2.24 +/- 0.208, 'kilogram')>

Note

  • If sequences (not NumPy arrays) are supplied for value and error, their numeric values will always be converted to numpy.float64.

  • If pint.Quantity objects are supplied for either parameter, the behavior is exactly as described in the from_quantities method.

  • If an Uncertainty is supplied for value, its error attribute will always override any error argument (if it is supplied).

  • If the error parameter is not finite, the resulting Uncertainty object will have its error attribute set to zero.

See also

_nom: T[source]
_err: T[source]
_init_seq(value, error)[source]
Parameters:
Return type:

None

_init_vec(value, error)[source]
Parameters:
Return type:

None

property is_vector: bool[source]

Whether the current object is a vector uncertainty.

Return type:

bool

property value: T[source]

The central value of the Uncertainty object.

Return type:

T

property error: T[source]

The uncertainty (error) of the Uncertainty object.

Return type:

T

property relative: T[source]

The relative uncertainty of the Uncertainty object.

Return type:

T

property rel: T[source]

Alias for relative.

Return type:

T

property rel2: T[source]

The square of the relative uncertainty of the Uncertainty object.

Return type:

T

plus_minus(error)[source]

Add an error to the Uncertainty object.

Returns a new instance.

Parameters:

error (UType) – Error value to add

Return type:

Uncertainty[T]

classmethod from_string(string)[source]

Create an Uncertainty object from a string representation of the value and error.

Parameters:

string (str) – A string representation of the value and error. The error can be represented as “+/-” or “±”. For instance, 5.0 +- 1.0 or 5.0 ± 1.0.

Return type:

Uncertainty

classmethod from_quantities(value, error=None)[source]

Create a pint.Quantity object with uncertainty from one or more Quantity objects.

Warning

Static type inference is hindered when using this method. Call Uncertainty(value, error) instead for full typing support.

Parameters:
  • value – The central value(s) of the Uncertainty object

  • error – The uncertainty value(s) of the Uncertainty object (Default = None)

Note

It is not necessary (and not advised) to call this method explicitly. Instantiating an Uncertainty object with Uncertainty(value, error) will automatically use from_quantities if Quantity objects are supplied as parameters.

Note

classmethod from_sequence(value, error=None)[source]

Creates either an Uncertainty object or a pint.Quantity object from a supported sequence.

The primary purpose of this method is to intercept sequences containing Quantity objects, reformat them, and then continue the instantiation process.

Warning

Static type inference is hindered when using this method. Call Uncertainty(value, error) instead for full typing support.

Parameters:
  • value – The central value(s)

  • error – The uncertainty value(s). Zero if not provided. (Default = None)

Note

It is not necessary (and not advised) to call this method explicitly. Instantiating an Uncertainty object with Uncertainty(value, error) will automatically use from_sequence if sequences are supplied as parameters.

classmethod from_list(value, error=None)[source]

Alias for from_sequence.

Warning

This method is deprecated.

Parameters:
  • value (ValT)

  • error (ErrT | None)

Return type:

Uncertainty

__getstate__()[source]
Return type:

dict[str, T]

__setstate__(state)[source]
Return type:

None

__getnewargs__()[source]
Return type:

tuple[T, T]

__copy__()[source]
Return type:

Uncertainty[T]

__deepcopy__(memo)[source]
Return type:

Uncertainty[T]

_HANDLED_TYPES[source]
__add__(other)[source]
__radd__[source]
__sub__(other)[source]
__rsub__(other)[source]
__mul__(other)[source]
__rmul__[source]
__truediv__(other)[source]
__rtruediv__(other)[source]
__div__[source]
__rdiv__[source]
__floordiv__(other)[source]
__rfloordiv__(other)[source]
__mod__(other)[source]
__rmod__(other)[source]
__divmod__(other)[source]
__rdivmod__(other)[source]
__pow__(other)[source]
__rpow__(other)[source]
__abs__()[source]
__pos__()[source]
__neg__()[source]
_compare(other, op)[source]
__lt__[source]
__le__[source]
__ge__[source]
__gt__[source]
__bool__()[source]
Return type:

bool

__nonzero__[source]
__ne__(other)[source]
__eq__(other)[source]
__round__(ndigits)[source]
__hash__()[source]
Return type:

int

__array_function__(func, types, args, kwargs)[source]
__array_ufunc__(ufunc, method, *args, **kwargs)[source]
__array__(dtype=None, *, copy=None)[source]
Return type:

numpy.ndarray

__getattr__(item)[source]
Return type:

Any

__bytes__()[source]
Return type:

bytes

__apply_to_both_ndarray__ = ('flatten', 'real', 'imag', 'astype', 'T', 'reshape')[source]
__ndarray_attributes__ = ('dtype', 'ndim', 'size')[source]
__array_priority__ = 18[source]
clip(*args, **kwargs)[source]

NumPy clip implementation.

Parameters:
  • min

  • max

  • out

Return type:

Uncertainty[T]

Note

Implemented only for vector uncertainty objects.

fill(value)[source]

NumPy fill implementation.

Parameters:

value

Return type:

None

Note

Implemented only for vector uncertainty objects.

put(indices, values, mode='raise')[source]

NumPy put implementation.

Parameters:
  • indices

  • values

  • mode (Literal['raise', 'wrap', 'clip']) – (Default = 'raise')

Return type:

None

Note

Implemented only for vector uncertainty objects.

copy()[source]

Return a copy of the Uncertainty object.

Note

Implemented only for vector uncertainty objects.

Return type:

Uncertainty[T]

property flat[source]

NumPy flat implementation.

Note

Implemented only for vector uncertainty objects.

property shape[source]

NumPy shape implemenetation.

Note

Implemented only for vector uncertainty objects.

property nbytes[source]

NumPy nbytes implementation.

Note

Implemented only for vector uncertainty objects.

searchsorted(v, side='left', sorter=None)[source]

NumPy searchsorted implementation.

Note

Implemented only for vector uncertainty objects.

Parameters:

side (Literal['left', 'right'])

tolist()[source]

NumPy tolist implementation.

Note

Implemented only for vector uncertainty objects.

view()[source]

NumPy view implementation.

Note

Implemented only for vector uncertainty objects.

__iter__()[source]
__len__()[source]
Return type:

int

__getitem__(key)[source]
Parameters:

key (int)

Return type:

Uncertainty

__setitem__(key, value)[source]
Parameters:
Return type:

None

__float__()[source]
__int__()[source]
__complex__()[source]
auto_uncertainties.uncertainty.uncertainty_containers.VectorUncertainty[source]

Alias for Uncertainty to maintain backward compatibility.

auto_uncertainties.uncertainty.uncertainty_containers.ScalarUncertainty[source]

Alias for Uncertainty to maintain backward compatibility.

auto_uncertainties.uncertainty.uncertainty_containers.nominal_values(x: Uncertainty[UType]) UType[source]
auto_uncertainties.uncertainty.uncertainty_containers.nominal_values(x: G) G

Return the central value of an Uncertainty object if it is one, otherwise returns the object.

auto_uncertainties.uncertainty.uncertainty_containers.std_devs(x: Uncertainty[UType]) UType[source]
auto_uncertainties.uncertainty.uncertainty_containers.std_devs(x: G) G

Return the uncertainty of an Uncertainty object if it is one, otherwise returns zero.