diff options
author | Keewis <keewis@posteo.de> | 2022-02-10 21:11:01 +0100 |
---|---|---|
committer | Keewis <keewis@posteo.de> | 2022-02-10 21:11:01 +0100 |
commit | 7a69bcc01e8fd3575afbed55de72f561ce3d3e09 (patch) | |
tree | 80e9fbeb66c9679d067c4c0b4029e0f88187108c /pint/quantity.py | |
parent | f9c5c22f89aade818861c6fa2f9be8f1365a07a1 (diff) | |
parent | 5403f46ecf636d0749cf54cddf725d177d60af61 (diff) | |
download | pint-7a69bcc01e8fd3575afbed55de72f561ce3d3e09.tar.gz |
Merge branch 'master' into testing
Diffstat (limited to 'pint/quantity.py')
-rw-r--r-- | pint/quantity.py | 69 |
1 files changed, 34 insertions, 35 deletions
diff --git a/pint/quantity.py b/pint/quantity.py index 7fc2f6e..ac95da1 100644 --- a/pint/quantity.py +++ b/pint/quantity.py @@ -9,7 +9,6 @@ from __future__ import annotations import bisect -import contextlib import copy import datetime import functools @@ -168,20 +167,6 @@ def check_dask_array(f): return wrapper -@contextlib.contextmanager -def printoptions(*args, **kwargs): - """Numpy printoptions context manager released with version 1.15.0 - https://docs.scipy.org/doc/numpy/reference/generated/numpy.printoptions.html - """ - - opts = np.get_printoptions() - try: - np.set_printoptions(*args, **kwargs) - yield np.get_printoptions() - finally: - np.set_printoptions(**opts) - - # Workaround to bypass dynamically generated Quantity with overload method Magnitude = TypeVar("Magnitude") @@ -413,7 +398,9 @@ class Quantity(PrettyIPython, SharedRegistryObject, Generic[_MagnitudeType]): allf = plain_allf = "{} {}" mstr = formatter.format(obj.magnitude) else: - with printoptions(formatter={"float_kind": formatter.format}): + with np.printoptions( + formatter={"float_kind": formatter.format} + ): mstr = ( "<pre>" + format(obj.magnitude).replace("\n", "<br>") @@ -440,7 +427,7 @@ class Quantity(PrettyIPython, SharedRegistryObject, Generic[_MagnitudeType]): if obj.magnitude.ndim == 0: mstr = formatter.format(obj.magnitude) else: - with printoptions(formatter={"float_kind": formatter.format}): + with np.printoptions(formatter={"float_kind": formatter.format}): mstr = format(obj.magnitude).replace("\n", "") else: mstr = format(obj.magnitude, mspec).replace("\n", "") @@ -765,6 +752,23 @@ class Quantity(PrettyIPython, SharedRegistryObject, Generic[_MagnitudeType]): return self.__class__(magnitude, other) + def _get_reduced_units(self, units): + # loop through individual units and compare to each other unit + # can we do better than a nested loop here? + for unit1, exp in units.items(): + # make sure it wasn't already reduced to zero exponent on prior pass + if unit1 not in units: + continue + for unit2 in units: + # get exponent after reduction + exp = units[unit1] + if unit1 != unit2: + power = self._REGISTRY._get_dimensionality_ratio(unit1, unit2) + if power: + units = units.add(unit2, exp / power).remove([unit1]) + break + return units + def ito_reduced_units(self) -> None: """Return Quantity scaled in place to reduced units, i.e. one unit per dimension. This will not reduce compound units (e.g., 'J/kg' will not @@ -777,21 +781,10 @@ class Quantity(PrettyIPython, SharedRegistryObject, Generic[_MagnitudeType]): if len(self._units) == 1: return None - newunits = self._units.copy() - # loop through individual units and compare to each other unit - # can we do better than a nested loop here? - for unit1, exp in self._units.items(): - # make sure it wasn't already reduced to zero exponent on prior pass - if unit1 not in newunits: - continue - for unit2 in newunits: - if unit1 != unit2: - power = self._REGISTRY._get_dimensionality_ratio(unit1, unit2) - if power: - newunits = newunits.add(unit2, exp / power).remove([unit1]) - break + units = self._units.copy() + new_units = self._get_reduced_units(units) - return self.ito(newunits) + return self.ito(new_units) def to_reduced_units(self) -> Quantity[_MagnitudeType]: """Return Quantity scaled in place to reduced units, i.e. one unit per @@ -799,10 +792,16 @@ class Quantity(PrettyIPython, SharedRegistryObject, Generic[_MagnitudeType]): can it make use of contexts at this time. """ - # can we make this more efficient? - newq = copy.copy(self) - newq.ito_reduced_units() - return newq + # shortcuts in case we're dimensionless or only a single unit + if self.dimensionless: + return self.to({}) + if len(self._units) == 1: + return self + + units = self._units.copy() + new_units = self._get_reduced_units(units) + + return self.to(new_units) def to_compact(self, unit=None) -> Quantity[_MagnitudeType]: """ "Return Quantity rescaled to compact, human-readable units. |