Quantity

Abstract interfaces

class proteka.quantity.quantity.BaseQuantity(value, unit='dimensionless')[source]

Numbers or numpy arrays with a unit field. Retrive the value by in_unit_of method for unit compatibility.

Parameters:
  • value (numpy.ndarray or any array-like input) – The raw value of the quantity.

  • unit (str, optional) – The unit of the quantity, by default “dimensionless”

Notes

This is only a wrapper around the actual array, so changing the array will change the quantity value as well, and vise versa.

convert_unit_to_(target_unit='dimensionless')[source]

Convert the quantity to target unit. If not possible, raise ValueError. Comparing to to_quantity_with_unit, the operation is in-place and thus no new Quantity will be created.

Parameters:

target_unit (str, optional) – The unit string to convert the current quantity into, by default “dimensionless”.

Returns:

self in target_unit.

Return type:

(Base)Quantity

Raises:

ValueError – When the current quantity is not convertible to target_unit.

in_unit_of(target_unit='dimensionless')[source]

Return quantity value in target unit. If not possible, raise ValueError.

Parameters:

target_unit (str, optional) – The value of the current quantity will be returned in this unit string, by default “dimensionless”

Returns:

The value of the current quantity in target_unit.

Return type:

numpy.ndarray

Raises:

ValueError – When the current quantity is not convertible to target_unit.

is_unit_convertible_with(unit_or_quantity)[source]

Check whether it has convertible units with input units or Quantity object.

Parameters:

unit_or_quantity (str | (Base)Quantity) – A unit string or a quantity with unit

Returns:

True if the unit is interconvertible.

Return type:

bool

property raw_value

The raw array inside the quantity object.

Return type:

numpy.ndarray

to_quantity_with_unit(target_unit='dimensionless')[source]

Convert the quantity to target unit. If not possible, raise ValueError. Comparing to in_unit_of, a Quantity object is returned.

Parameters:

target_unit (str, optional) – The unit string to convert the current quantity into, by default “dimensionless”.

Returns:

A new quantity in target_unit.

Return type:

(Base)Quantity

Raises:

ValueError – When the current quantity is not convertible to target_unit.

property unit

The unit of the quantity object.

Return type:

str

Implementations

class proteka.quantity.quantity.Quantity(value, unit='dimensionless', metadata=None, copy=False)[source]

Quantity (numpy.ndarray + unit str) with metadata (metadata). Specialized MetaArray with unit as a mandatory field in the metadata. Can be converted to and from a HDF5 Dataset.

Parameters:
  • value (numpy.ndarray or any array-like input) – The raw value of the quantity.

  • unit (str, optional) – The unit of the quantity, by default “dimensionless”

  • metadata (dict, optional) – Metadata to be wrapped, a mapping from str to str or arrays, by default None

Raises:

ValueError – When the unit is a key in metadata and the value is not the identical as the argument. Recommended to double check and either remove the unit in metadata or make it identical with the argument.

static from_hdf5(h5dt, offset=None, stride=None, suppress_unit_warn=False)[source]

Create an instance from the content of HDF5 dataset h5dt. If no unit is present in the HDF5 attributes, then assumed as “dimensionless” with warning. For non-scalar dataset, offset and stride can be set, such that the slice h5dt[offset::stride] will be read to the memory. For scalar dataset, offset and stride will be simply ignored.

Parameters:
  • h5dt (h5py.Dataset) – A HDF5 Dataset.

  • offset (None | int, optional) – The offset for loading from the HDF5 file. Default is None.

  • stride (None | int, optional) – The stride for loading from the HDF5 file. Default is None.

  • suppress_unit_warn (bool, optional) – Whether to allow silently setting unit to “dimensionless”, by default False

Returns:

The quantity implied by the content in the input HDF5 Dataset.

Return type:

Quantity

property unit

The unit of the quantity object.

Return type:

str