hgrecco / pint

Operate and manipulate physical quantities in Python
http://pint.readthedocs.org/
Other
2.33k stars 459 forks source link

strange behavior when using @ureg.wraps #2030

Open SchabiDesigns opened 2 weeks ago

SchabiDesigns commented 2 weeks ago

Hi guys,

There is some strange behavior when using @ureg.wraps... When I use my function without @ureg.wraps result has correct dimension and also intermediate results are correct:

Q_ = ureg.Quantity
def v_2_g(x, amplifier = Q_("10"), sensor = Q_("25 g_0"), sensor_volt = Q_("5 volt"), amplif_volt = Q_("10 volt")):
...

this results in:

sensor      : 10 standard_gravity
sensor_range    : 5 volt
ouput       : 5 volt

without amplifier:
10.0 standard_gravity

with amplifier (10) and same volt range (5 volt):
1.0 standard_gravity

with amplifier (10) and knowing that volt range has been stretched from 5 volt to 10 volt:
0.5 standard_gravity

When I use the @ureg.wraps decorator as follows:

Q_ = ureg.Quantity
@ureg.wraps("standard_gravity", ("volt", None, "standard_gravity", "volt", "volt"))
def v_2_g(output, amplifier = Q_("10"), sensor = Q_("25 g_0"), sensor_volt = Q_("5 volt"), amplif_volt = Q_("10 volt")):
...

this results in:

sensor      : 25
sensor_range    : 5
ouput       : 5

without amplifier:
25.0

with amplifier (10) and same volt range (5):
2.5 dimensionless

with amplifier (10) and knowing that volt range has been stretched from 5 to 10:
1.25 dimensionless

which then throws an DimensionalityError: Cannot convert from 'dimensionless' (dimensionless) to 'standard_gravity'

why suddenly is this dimensionless have been calculated? well it is not wrong since volt/volt results in dimensionless, but it seems @ureg.wraps can not handle that in the end... ???

Python 3.12.0 pip 24.0 Pint 0.24.1

Regards Schabi

SchabiDesigns commented 2 weeks ago

I found a solution. When using None within ureg.wraps the value passed should not be declared as a pint.Quantity without an unit. In my case the function works when I use amplifier = 10 instead of Q_("10")