ai4materials.utils.unit_conversion module

Module for unit conversion routines. Currently uses the Pint unit conversion library (https://pint.readthedocs.org) to do the conversions.

Any new units and constants can be added to the text files “units.txt” and “constants.txt”.

NOTE: this is taken from python-common in nomad-lab-base. It is copied here to remove the dependency from nomad-lab-base. For more info on python-common visit: https://gitlab.mpcdf.mpg.de/nomad-lab/python-common

The author of this code is: Dr. Fawzi Roberto Mohamed E-mail: mohamed@fhi-berlin.mpg.de

class ai4materials.utils.unit_conversion.LazyF(unit, target_unit)[source]

Bases: future.types.newobject.newobject

helper class for lazy evaluation of conversion function

ai4materials.utils.unit_conversion.convert_unit(value, unit, target_unit=None)[source]

Converts the given value from the given units to the target units. For examples see the bottom section.

Args:
value: The numeric value to be converted. Accepts integers, floats,
lists and numpy arrays
unit: The units that the value is currently given in as a string. All
units that have a corresponding declaration in the “units.txt” file and combinations like “meter*second**-2” are supported.
target_unit: The target unit as string. Same rules as for the unit
argument. If this argument is not given, SI units are assumed.
Returns:
The given value in the target units. returned as the same data type as the original values.

Code author: Fawzi Mohamed <mohamed@fhi-berlin.mpg.de>

ai4materials.utils.unit_conversion.convert_unit_function(unit, target_unit=None)[source]

Returns a function that converts scalar floats from unit to target_unit if any of the unit are user defined (usr*), then the conversion is done lazily at the first call (i.e. user defined conversions might be undefined when calling this)

For more details see the convert_unit function. Could be optimized a bit caching the pint quantities

Args:
unit: The units that the value is currently given in as a string. All
units that have a corresponding declaration in the “units.txt” file and combinations like “meter*second**-2” are supported.
target_unit: The target unit as string. Same rules as for the unit
argument. If this argument is not given, SI units are assumed.
Returns:
The given value in the target units. returned as the same data type as the original values.

Code author: Fawzi Mohamed <mohamed@fhi-berlin.mpg.de>

ai4materials.utils.unit_conversion.convert_unit_function_immediate(unit, target_unit=None)[source]

Returns a function that converts scalar floats from unit to target_unit All units need to be already known.

For more details see the convert_unit function. Could be optimized a bit caching the pint quantities

Args:
unit: The units that the value is currently given in as a string. All
units that have a corresponding declaration in the “units.txt” file and combinations like “meter*second**-2” are supported.
target_unit: The target unit as string. Same rules as for the unit
argument. If this argument is not given, SI units are assumed.
Returns:
The given value in the target units. returned as the same data type as the original values.

Code author: Fawzi Mohamed <mohamed@fhi-berlin.mpg.de>

ai4materials.utils.unit_conversion.register_userdefined_quantity(quantity, units, value=1)[source]

Registers a user defined quantity, valid until redefined. The value should be equal to value using units, with value defaulting to 1