Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unit dB not hashable when using decimal as non_int_type when numpy is installed #1936

Open
PlasmaHH opened this issue Feb 15, 2024 · 0 comments

Comments

@PlasmaHH
Copy link

Running code like this:

#!/usr/bin/python3
import pint
import decimal

ureg=pint.UnitRegistry( non_int_type = decimal.Decimal )
db=ureg("dB")
uset=set()
uset.add(db)

leads to the error:

AttributeError: 'decimal.Decimal' object has no attribute 'log'. Did you mean: 'logb'?

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/plasmahh/src/python/./uhash.py", line 8, in <module>
    uset.add(db)
  File "/home/plasmahh/.local/lib/python3.11/site-packages/pint/facets/plain/quantity.py", line 283, in __hash__
    self_base = self.to_base_units()
                ^^^^^^^^^^^^^^^^^^^^
  File "/home/plasmahh/.local/lib/python3.11/site-packages/pint/facets/plain/quantity.py", line 566, in to_base_units
    magnitude = self._convert_magnitude_not_inplace(other)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/plasmahh/.local/lib/python3.11/site-packages/pint/facets/plain/quantity.py", line 472, in _convert_magnitude_not_inplace
    return self._REGISTRY.convert(self._magnitude, self._units, other)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/plasmahh/.local/lib/python3.11/site-packages/pint/facets/plain/registry.py", line 1036, in convert
    return self._convert(value, src, dst, inplace)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/plasmahh/.local/lib/python3.11/site-packages/pint/facets/context/registry.py", line 404, in _convert
    return super()._convert(value, src, dst, inplace)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/plasmahh/.local/lib/python3.11/site-packages/pint/facets/nonmultiplicative/registry.py", line 266, in _convert
    value = self._units[src_offset_unit].converter.to_reference(value, inplace)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/plasmahh/.local/lib/python3.11/site-packages/pint/facets/nonmultiplicative/definitions.py", line 115, in to_reference
    value = self.scale * exp(log(self.logbase) * (value / self.logfactor))
                             ^^^^^^^^^^^^^^^^^
TypeError: loop of ufunc does not support argument 0 of type decimal.Decimal which has no callable log method

when numpy is used as numpy.log does not seem to have decimal support. Using math.log instead works:

>>> import decimal,numpy,math
>>> d=decimal.Decimal(1)
>>> math.log(d)
0.0
>>> numpy.log(d)
AttributeError: 'decimal.Decimal' object has no attribute 'log'. Did you mean: 'logb'?

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: loop of ufunc does not support argument 0 of type decimal.Decimal which has no callable log method
>>> 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant