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

to_compact is broken for Measurements and for Quantitys with uncertainties #2044

Open
doronbehar opened this issue Jul 23, 2024 · 1 comment · May be fixed by #2069
Open

to_compact is broken for Measurements and for Quantitys with uncertainties #2044

doronbehar opened this issue Jul 23, 2024 · 1 comment · May be fixed by #2069

Comments

@doronbehar
Copy link

doronbehar commented Jul 23, 2024

I'm using uncertainties 3.2.2, and pint 0.24.3, and I want to use to_compact for measurements, and it fails. Here's a MWE demonstrating two methods I tried:

import uncertainties

import pint
ureg = pint.UnitRegistry()

v = 1.24
s = 0.01

# Fails
print((v * ureg.Hz).plus_minus(s).to_compact())
# Also fails
print((uncertainties.ufloat(v, s)*ureg.Hz).to_compact())

Fails with:

Traceback (most recent call last):
  File "/home/doron/repos/lab-ion-trap-simulations/mwe.py", line 12, in <module>
    print((uncertainties.ufloat(v, s)*ureg.Hz).to_compact())
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/zw5lasilw5gacg56s0rgnazdng3vby2i-python3-3.12.4-env/lib/python3.12/site-packages/pint/facets/plain/qto.py", line 116, in to_compact
    or math.isnan(quantity.magnitude)
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/zw5lasilw5gacg56s0rgnazdng3vby2i-python3-3.12.4-env/lib/python3.12/site-packages/uncertainties/core.py", line 2611, in raise_error
    raise TypeError("can't convert an affine function (%s)"
TypeError: can't convert an affine function (<class 'uncertainties.core.AffineScalarFunc'>) to float; use x.nominal_value

I tested this MWE with multiple uncertainties versions 3.2.2, 3.2.0 and 3.1.7 and all of them didn't fail when pint was downgraded to 0.23, and failed when pint was at version 0.24 and 0.24.1.

@doronbehar doronbehar changed the title to_compact is broken for Measurements and for Quantitys with uncertainties to_compact is broken for Measurements and for Quantitys with uncertainties Jul 23, 2024
doronbehar added a commit to doronbehar/pint that referenced this issue Oct 26, 2024
@doronbehar doronbehar linked a pull request Oct 26, 2024 that will close this issue
5 tasks
doronbehar added a commit to doronbehar/pint that referenced this issue Oct 26, 2024
@doronbehar
Copy link
Author

Can't believe it took me so long to fix this easy, and small annoying issue. Fix is in #2069.

doronbehar added a commit to doronbehar/pint that referenced this issue Oct 26, 2024
doronbehar added a commit to doronbehar/pint that referenced this issue Oct 26, 2024
doronbehar added a commit to doronbehar/pint that referenced this issue Oct 26, 2024
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

Successfully merging a pull request may close this issue.

1 participant