unyt.exceptions module

Exception classes defined by unyt

exception unyt.exceptions.UnitOperationError(operation, unit1, unit2=None)[source]

Bases: ValueError

An exception that is raised when unit operations are not allowed

Example

>>> import unyt as u
>>> 3*u.g + 4*u.m # doctest: +IGNORE_EXCEPTION_DETAIL +NORMALIZE_WHITESPACE
Traceback (most recent call last):
...
unyt.exceptions.UnitOperationError: The <ufunc 'add'> operator
for unyt_arrays with units "g" (dimensions "(mass)") and
"m" (dimensions "(length)") is not well defined.
exception unyt.exceptions.UnitConversionError(unit1, dimension1, unit2, dimension2)[source]

Bases: Exception

An error raised when converting to a unit with different dimensions.

Example

>>> import unyt as u
>>> data = 3*u.g
>>> data.to('m')  # doctest: +IGNORE_EXCEPTION_DETAIL +NORMALIZE_WHITESPACE
Traceback (most recent call last):
...
unyt.exceptions.UnitConversionError: Cannot convert between 'g'
(dim '(mass)') and 'm' (dim '(length)').
exception unyt.exceptions.MissingMKSCurrent(unit_system_name)[source]

Bases: Exception

Raised when querying a unit system for MKS current dimensions

Since current is a base dimension for SI or SI-like unit systems but not in CGS or CGS-like unit systems, dimensions that include the MKS current dimension (the dimension of ampere) are not representable in CGS-like unit systems. When a CGS-like unit system is queried for such a dimension, this error is raised.

Example

>>> from unyt.unit_systems import cgs_unit_system as us
>>> from unyt import ampere
>>> us[ampere.dimensions] # doctest: +IGNORE_EXCEPTION_DETAIL +NORMALIZE_WHITESPACE
Traceback (most recent call last):
...
unyt.exceptions.MissingMKSCurrent: The cgs unit system does not
have a MKS current base unit
exception unyt.exceptions.MKSCGSConversionError[source]

Bases: Exception

Raised when conversion between MKS and CGS units cannot be performed

This error is raised and caught internally and will expose itself to the user as part of a chained exception leading to a UnitConversionError.

exception unyt.exceptions.UnitsNotReducible(unit, units_base)[source]

Bases: Exception

Raised when a unit cannot be safely represented in a unit system

Example

>>> from unyt import A, cm
>>> data = 12*A/cm
>>> data.in_cgs()  # doctest: +IGNORE_EXCEPTION_DETAIL +NORMALIZE_WHITESPACE
Traceback (most recent call last):
...
unyt.exceptions.UnitsNotReducible: The unit "A/cm" (dimensions
"(current_mks)/(length)") cannot be reduced to an expression
within the cgs system of units.
exception unyt.exceptions.IterableUnitCoercionError(op)[source]

Bases: Exception

Raised when an iterable cannot be converted to a unyt_array

Example

>>> from unyt import g, cm, unyt_array
>>> data = [2*cm, 3*g]
>>> unyt_array(data)  # doctest: +IGNORE_EXCEPTION_DETAIL +NORMALIZE_WHITESPACE
Traceback (most recent call last):
...
unyt.exceptions.IterableUnitCoercionError: Received an input
or operand that cannot be converted to a unyt_array with uniform
units:
[unyt_quantity(2., 'cm'), unyt_quantity(3., 'g')]
exception unyt.exceptions.InvalidUnitEquivalence(equiv, unit1, unit2)[source]

Bases: Exception

Raised an equivalence does not apply to a unit conversion

Example

>>> import unyt as u
>>> data = 12*u.g
>>> data.to('erg', equivalence='thermal') # doctest: +IGNORE_EXCEPTION_DETAIL +NORMALIZE_WHITESPACE
Traceback (most recent call last):
...
unyt.exceptions.InvalidUnitEquivalence: The unit equivalence
'thermal' does not exist for the units 'g' and 'erg'.
exception unyt.exceptions.InvalidUnitOperation[source]

Bases: Exception

Raised when an operation on a unit object is not allowed

Example

>>> from unyt import cm, g
>>> cm + g  # doctest: +IGNORE_EXCEPTION_DETAIL +NORMALIZE_WHITESPACE
Traceback (most recent call last):
...
unyt.exceptions.InvalidUnitOperation: addition with unit objects
is not allowed
exception unyt.exceptions.SymbolNotFoundError[source]

Bases: Exception

Raised when a unit name is not available in a unit registry

Example

>>> from unyt.unit_registry import default_unit_registry
>>> default_unit_registry['made_up_unit']  # doctest: +IGNORE_EXCEPTION_DETAIL +NORMALIZE_WHITESPACE
Traceback (most recent call last):
...
unyt.exceptions.SymbolNotFoundError: The symbol 'made_up_unit'
does not exist in this registry.
exception unyt.exceptions.UnitParseError[source]

Bases: Exception

Raised when a string unit name is not parseable as a valid unit

Example

>>> from unyt import Unit
>>> Unit('hello')  # doctest: +IGNORE_EXCEPTION_DETAIL +NORMALIZE_WHITESPACE
Traceback (most recent call last):
...
unyt.exceptions.UnitParseError: Could not find unit symbol
'hello' in the provided symbols.
exception unyt.exceptions.IllDefinedUnitSystem(units_map)[source]

Bases: Exception

Raised when the dimensions of the base units of a unit system are inconsistent.

Example

>>> from unyt.unit_systems import UnitSystem
>>> UnitSystem('atomic', 'nm', 'fs', 'nK', 'rad')  # doctest: +IGNORE_EXCEPTION_DETAIL +NORMALIZE_WHITESPACE
Traceback (most recent call last):
...
unyt.exceptions.IllDefinedUnitSystem: Cannot create unit system
with inconsistent mapping from
dimensions to units. Received:
OrderedDict([((length), nm), ((mass), fs), ((time), nK),
             ((temperature), rad), ((angle), rad),
             ((current_mks), A), ((luminous_intensity), cd)])