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

Issue with Units around temperature #387

Open
slebras opened this issue Jan 4, 2021 · 10 comments
Open

Issue with Units around temperature #387

slebras opened this issue Jan 4, 2021 · 10 comments

Comments

@slebras
Copy link
Contributor

slebras commented Jan 4, 2021

I've had issues with unit expressions around temperature. The error is thrown on this line

the error is:
pint.errors.OffsetUnitCalculusError: Ambiguous operation with offset unit (degree_Celsius, ). See https://pint.readthedocs.io/en/latest/nonmult.html for guidance.

The multiplication on the line shared above between units and 1 does not work with temperatures in pint. "These temperature units are expressed in a system with a reference point, and relations between temperature units include not only a scaling factor but also an offset." in their words.

@MrCreosote
Copy link
Member

To reproduce:

In [1]: import pint                                                             

In [2]: ur = pint.UnitRegistry()                                                

In [3]: unit = ur.parse_expression('degF')                                      

In [4]: 1 *  unit                                                               
---------------------------------------------------------------------------
OffsetUnitCalculusError                   Traceback (most recent call last)
<ipython-input-4-69f3d3a309b4> in <module>
----> 1 1 *  unit
*snip*

OffsetUnitCalculusError: Ambiguous operation with offset unit (degree_Fahrenheit, ). See https://pint.readthedocs.io/en/latest/nonmult.html for guidance.

@MrCreosote
Copy link
Member

In [7]: unit = ur.parse_expression('degK')                                      

In [8]: 1 * unit                                                                
Out[8]: 1 <Unit('kelvin')>

@MrCreosote
Copy link
Member

@MrCreosote
Copy link
Member

MrCreosote commented Jan 4, 2021

Looks like the incoming units have to be absolute. Does it work with K or R?

@MrCreosote
Copy link
Member

@MrCreosote
Copy link
Member

As an alternative to raising an error, pint can be configured to work more relaxed via setting the UnitRegistry parameter autoconvert_offset_to_baseunit to true. In this mode, pint behaves differently:

    Multiplication of a quantity with a single offset unit with order +1 by a number or ndarray yields the quantity in the given unit.

>>> ureg = UnitRegistry(autoconvert_offset_to_baseunit = True)
>>> T = 25.4 * ureg.degC
>>> T
<Quantity(25.4, 'degree_Celsius')>

@MrCreosote
Copy link
Member

So a couple potential fixes, I think:

  1. Only allow absolute temperature units in input, and throw a better error if they're not.
  2. The option above, which probably needs to be understood better before implementation.

@slebras
Copy link
Contributor Author

slebras commented Jan 5, 2021

I'm leaning towards option 2 above, If we only ever use units * 1, it should not effect the incoming data. I also don't think we should alter any of the data, outside of unit conversions, within the SampleService, so that shouldn't be an issue going forward

@slebras
Copy link
Contributor Author

slebras commented Jan 7, 2021

PR addressing this: #388

@eapearson
Copy link
Collaborator

Another solution, with no other side effects, is to use the quantity api.

pint.quantity.Quantity(1, unit).to(req_unit)

which is utilized in #410

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

3 participants