Unit handling

metatrain.utils.units.get_gradient_units(base_unit: str, gradient_name: str, length_unit: str) → str[source]

Get the gradient units based on the unit of the base quantity.

For example, if the base unit is “<unit>” and the gradient name is “positions”, the gradient unit will be “<unit>/<length_unit>”.

Parameters:
  • base_unit (str) – The unit of the base quantity.

  • gradient_name (str) – The name of the gradient.

  • length_unit (str) – The unit of lengths.

Returns:

The unit of the gradient.

Return type:

str

metatrain.utils.units.ev_to_mev(value: float, unit: str) → Tuple[float, str][source]

If the unit starts with eV, converts the value and its corresponding unit to meV. Otherwise, returns the input.

Parameters:
  • value (float) – The value (potentially in eV or a derived quantity of eV).

  • unit (str) – The unit of the value.

Returns:

If the value is in meV (or a derived quantity), the value and the corresponding unit where eV is converted to meV. Otherwise, the input.

Return type:

Tuple[float, str]

metatrain.utils.units.units_are_equivalent(unit: str, other_unit: str) → bool[source]

Check whether two unit expressions represent the same numerical unit.

Equivalent units have a conversion factor of exactly one, so their data can be combined without rescaling. Unknown units (empty strings) are only equivalent to other unknown units.

Parameters:
  • unit (str) – The first unit expression.

  • other_unit (str) – The second unit expression.

Returns:

Whether the units are equivalent without rescaling.

Return type:

bool