Warning

You are reading the documentation related to the development version. Go here if you are looking for the documentation of the stable release.

spectrochempy.Quantity

class Quantity(value, units=None)[source]

Attributes Summary

default_format

Default formatting string.

dimensionality

returns: dict -- Dimensionality of the PlainQuantity, e.g.

dimensionless

m

PlainQuantity's magnitude.

magnitude

PlainQuantity's magnitude.

u

PlainQuantity's units.

unitless

units

PlainQuantity's units.

Methods Summary

check(dimension)

Return true if the quantity's dimension matches passed dimension.

compute(**kwargs)

Compute the Dask array wrapped by pint.PlainQuantity.

dot(b)

Dot product of two arrays.

from_list(quant_list[, units])

Transforms a list of Quantities into an numpy.array quantity.

from_sequence(cls, seq[, units])

Transforms a sequence of Quantities into an numpy.array quantity.

is_compatible_with(self, other, *contexts, ...)

check if the other object is compatible

ito([other])

Inplace rescale to different units.

ito_base_units()

Return PlainQuantity rescaled to plain units.

ito_preferred([preferred_units])

Return Quantity converted to a unit composed of the preferred units.

ito_reduced_units()

Return PlainQuantity scaled in place to reduced units, i.e. one unit per dimension.

ito_root_units()

Return PlainQuantity rescaled to root units.

m_as(units)

PlainQuantity's magnitude expressed in particular units.

persist(**kwargs)

Persist the Dask Array wrapped by pint.PlainQuantity.

prod(_prod_dispatcher)def prod(a[, axis, ...])

Return the product of quantity elements over a given axis

to([other])

Return PlainQuantity rescaled to different units.

to_base_units()

Return PlainQuantity rescaled to plain units.

to_compact(quantity, unit)

"Return PlainQuantity rescaled to compact, human-readable units.

to_preferred([preferred_units])

Return Quantity converted to a unit composed of the preferred units.

to_reduced_units()

Return PlainQuantity scaled in place to reduced units, i.e. one unit per dimension.

to_root_units()

Return PlainQuantity rescaled to root units.

visualize(**kwargs)

Produce a visual representation of the Dask graph.

Attributes Documentation

default_format = '~P'

Default formatting string.

dimensionality

returns: dict – Dimensionality of the PlainQuantity, e.g. {length: 1, time: -1}

dimensionless
m

PlainQuantity’s magnitude. Short form for magnitude

magnitude

PlainQuantity’s magnitude. Long form for m

u

PlainQuantity’s units. Short form for units

unitless
units

PlainQuantity’s units. Long form for u

Methods Documentation

check(dimension)[source]

Return true if the quantity’s dimension matches passed dimension.

compute(**kwargs)[source]

Compute the Dask array wrapped by pint.PlainQuantity.

Parameters

**kwargs (dict) – Any keyword arguments to pass to dask.compute.

Returns

pint.PlainQuantity – A pint.PlainQuantity wrapped numpy array.

dot(b)[source]

Dot product of two arrays.

Wraps np.dot().

classmethod from_list(quant_list, units=None)[source]

Transforms a list of Quantities into an numpy.array quantity. If no units are specified, the unit of the first element will be used. Same as from_sequence.

If units is not specified and list is empty, the unit cannot be determined and a ValueError is raised.

Parameters
  • quant_list (list of pint.PlainQuantity) – list of pint.PlainQuantity

  • units (UnitsContainer, str or pint.PlainQuantity) – units of the physical quantity to be created (Default value = None)

Returns

pint.PlainQuantity

classmethod from_sequence(cls, seq: Sequence[PlainQuantity[MagnitudeT]], units=None ) -> PlainQuantity[MagnitudeT]: """Transforms a sequence of Quantities into an numpy.array quantity. If no units are specified, the unit of the first element will be used. If units is not specified and sequence is empty, the unit cannot be determined and a ValueError is raised. Parameters ---------- seq : sequence of pint.PlainQuantity sequence of pint.PlainQuantity units : UnitsContainer, str or pint.PlainQuantity units of the physical quantity to be created (Default value = None) Returns ------- pint.PlainQuantity """ len_seq = len(seq) if units is None: if len_seq: units = seq[0].u else: raise ValueError("Cannot determine units from empty sequence!") a = np.empty(len_seq) for i, seq_i in enumerate(seq)[source]

Transforms a sequence of Quantities into an numpy.array quantity. If no units are specified, the unit of the first element will be used.

If units is not specified and sequence is empty, the unit cannot be determined and a ValueError is raised.

Parameters
  • seq (sequence of pint.PlainQuantity) – sequence of pint.PlainQuantity

  • units (UnitsContainer, str or pint.PlainQuantity) – units of the physical quantity to be created (Default value = None)

Returns

pint.PlainQuantity

is_compatible_with(self, other: Any, *contexts: Union[str, Context], **ctx_kwargs: Any ) -> bool: """check if the other object is compatible Parameters ---------- other The object to check. Treated as dimensionless if not a PlainQuantity, Unit or str. *contexts : str or pint.Context Contexts to use in the transformation. **ctx_kwargs : Values for the Context/s Returns ------- bool """ from .unit import PlainUnit if contexts or self._REGISTRY._active_ctx: try: self.to(other, *contexts, **ctx_kwargs) return True except DimensionalityError: return False if isinstance(other, (PlainQuantity, PlainUnit))[source]

check if the other object is compatible

Parameters
  • other – The object to check. Treated as dimensionless if not a PlainQuantity, Unit or str.

  • *contexts (str or pint.Context) – Contexts to use in the transformation.

  • **ctx_kwargs – Values for the Context/s

Returns

bool

ito(other=None, *contexts, **ctx_kwargs)[source]

Inplace rescale to different units.

Parameters
  • other (pint.PlainQuantity, str or dict) – Destination units. (Default value = None)

  • *contexts (str or pint.Context) – Contexts to use in the transformation.

  • **ctx_kwargs – Values for the Context/s

ito_base_units()[source]

Return PlainQuantity rescaled to plain units.

ito_preferred(preferred_units=None)[source]

Return Quantity converted to a unit composed of the preferred units.

Examples

>>> import pint
>>> ureg = pint.UnitRegistry()
>>> (1*ureg.acre).to_preferred([ureg.meters])
<Quantity(4046.87261, 'meter ** 2')>
>>> (1*(ureg.force_pound*ureg.m)).to_preferred([ureg.W])
<Quantity(4.44822162, 'second * watt')>
ito_reduced_units()[source]

Return PlainQuantity scaled in place to reduced units, i.e. one unit per dimension. This will not reduce compound units (e.g., ‘J/kg’ will not be reduced to m**2/s**2), nor can it make use of contexts at this time.

ito_root_units()[source]

Return PlainQuantity rescaled to root units.

m_as(units)[source]

PlainQuantity’s magnitude expressed in particular units.

Parameters

units (pint.PlainQuantity, str or dict) – destination units

persist(**kwargs)[source]

Persist the Dask Array wrapped by pint.PlainQuantity.

Parameters

**kwargs (dict) – Any keyword arguments to pass to dask.persist.

Returns

pint.PlainQuantity – A pint.PlainQuantity wrapped Dask array.

prod(_prod_dispatcher)def prod(a, axis=None, dtype=None, out=None, keepdims=np._NoValue, initial=np._NoValue, where=np._NoValue)[source]

Return the product of quantity elements over a given axis

Wraps np.prod().

to(other=None, *contexts, **ctx_kwargs)[source]

Return PlainQuantity rescaled to different units.

Parameters
  • other (pint.PlainQuantity, str or dict) – destination units. (Default value = None)

  • *contexts (str or pint.Context) – Contexts to use in the transformation.

  • **ctx_kwargs – Values for the Context/s

Returns

pint.PlainQuantity

to_base_units()[source]

Return PlainQuantity rescaled to plain units.

to_compact(quantity: PlainQuantity, unit: Optional[UnitsContainer] = None) -> PlainQuantity: """ "Return PlainQuantity rescaled to compact, human-readable units. To get output in terms of a different unit, use the unit parameter. Examples -------- >>> import pint >>> ureg = pint.UnitRegistry() >>> (200e-9*ureg.s).to_compact() <Quantity(200.0, 'nanosecond')> >>> (1e-2*ureg('kg m/s^2')).to_compact('N') <Quantity(10.0, 'millinewton')> """ if not isinstance(quantity.magnitude, numbers.Number)[source]

“Return PlainQuantity rescaled to compact, human-readable units.

To get output in terms of a different unit, use the unit parameter.

Examples

>>> import pint
>>> ureg = pint.UnitRegistry()
>>> (200e-9*ureg.s).to_compact()
<Quantity(200.0, 'nanosecond')>
>>> (1e-2*ureg('kg m/s^2')).to_compact('N')
<Quantity(10.0, 'millinewton')>
to_preferred(preferred_units=None)[source]

Return Quantity converted to a unit composed of the preferred units.

Examples

>>> import pint
>>> ureg = pint.UnitRegistry()
>>> (1*ureg.acre).to_preferred([ureg.meters])
<Quantity(4046.87261, 'meter ** 2')>
>>> (1*(ureg.force_pound*ureg.m)).to_preferred([ureg.W])
<Quantity(4.44822162, 'second * watt')>
to_reduced_units()[source]

Return PlainQuantity scaled in place to reduced units, i.e. one unit per dimension. This will not reduce compound units (intentionally), nor can it make use of contexts at this time.

to_root_units()[source]

Return PlainQuantity rescaled to root units.

visualize(**kwargs)[source]

Produce a visual representation of the Dask graph.

The graphviz library is required.

Parameters

**kwargs (dict) – Any keyword arguments to pass to dask.visualize.

Examples using spectrochempy.Quantity

Units manipulation examples

Units manipulation examples