unyt.exceptions module

Exception classes defined by unyt

exception unyt.exceptions.UnytError[source]

Bases: Exception

A generic exception type that all unyt exceptions are derived from. It may be raised directly in rare situations that do not fit any existing exceptions and don’t justify the creation of a new one.

Since it serves as a base class for all unyt exceptions, it may be used as a catch-all exception type in dependent code. For instance

>>> from unyt import cm, s
>>> from unyt.exceptions import UnytError
>>> a = 1 * cm
>>> b = 1 / s
>>> try:
...     a + b
... except UnytError:
...     pass

However, it is generally recommended to only capture exceptions as specific as possible.

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

Bases: UnytError, ValueError

An exception that is raised when unit operations are not allowed

Example

>>> import unyt as u
>>> 3*u.g + 4*u.m 
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.UnitInconsistencyError(*units)[source]

Bases: UnitOperationError

An error raised on forbidden numpy API expecting arrays with identical units.

Example

>>> import unyt as u
>>> import numpy as np
>>> np.concatenate([[1, 2, 3] * u.cm, [4, 5, 6] * u.s]) 
Traceback (most recent call last):
...
unyt.exceptions.UnitInconsistencyError: Expected all unyt_array arguments to
have identical units. Received mixed units (cm, s)
exception unyt.exceptions.UnitConversionError(unit1, dimension1, unit2, dimension2)[source]

Bases: UnytError

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

Example

>>> import unyt as u
>>> data = 3*u.g
>>> data.to('m')  
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: UnytError

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] 
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: UnytError

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: UnytError

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()  
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: UnytError

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)  
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: UnytError

Raised an equivalence does not apply to a unit conversion

Example

>>> import unyt as u
>>> data = 12*u.g
>>> data.to('erg', equivalence='thermal') 
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: UnytError

Raised when an operation on a unit object is not allowed

Example

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

Bases: UnytError

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']  
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: UnytError

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

Example

>>> from unyt import Unit
>>> Unit('hello')  
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: UnytError

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')  
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)])