opticks.utils.unit_utils

Functions

quantity_from_list(data[, unit])

Converts a list of values to a Quantity object.

split_value_and_force_unit(data, tgt_unit[, ...])

Splits the value and the unit, converting the data to the target unit.

split_value_and_unit(data)

Splits the value and the unit (if available).

quantity_from_list(data, unit=None)[source]

Converts a list of values to a Quantity object.

The list should contain only Quantity objects that can be mutually converted (for example all of them should be length). If there are different units in the list, the first unit is used. If unit is given, then the data is converted to that unit.

If the data is not a list of Quantity objects, then returns the data with no changes.

Return type:

Quantity

Parameters:
  • data (list[Quantity]) – The data to be converted

  • unit (UnitBase | FunctionUnitBase, optional) – Target unit

Returns:

Quantity – The data as a Quantity object

split_value_and_force_unit(data, tgt_unit, equivalencies=None)[source]

Splits the value and the unit, converting the data to the target unit.

If the data has no units, then the target unit is assigned to the data.

Return type:

tuple[float | ndarray, UnitBase | FunctionUnitBase]

Parameters:
  • data (Quantity | float | np.ArrayLike) – The data to be split

  • tgt_unit (_type_, optional) – target unit

  • equivalencies (list, optional) – Equivalencies to be used during conversion

Returns:

tuple[float | np.ndarray, UnitBase | FunctionUnitBase] – The value (without units) and the unit

split_value_and_unit(data)[source]

Splits the value and the unit (if available).

The input object structure is preserved. If the input is a list or ndarray then the output will also be a list or ndarray.

If the data is not a Quantity object, then the unit will be set to None. Initialising an object with Quantity(5, None) will just result in a dimensionless Quantity object.

Return type:

tuple[float | ndarray, UnitBase | FunctionUnitBase]

Parameters:

data (Quantity | float | np.ArrayLike) – The data to be split

Returns:

tuple[float | np.ndarray, UnitBase | FunctionUnitBase] – The value (without units) and the unit (if available)