简体   繁体   English

测试品脱的单位等效性

[英]Test for unit equivalence in pint

How do I test for unit equivalence in Pint?如何在 Pint 中测试单位等效性? For example, nM is equivalent to nmol/L and L is equivalent to dm^3 , but they are not equal according to Pint.例如, nM等价于nmol/LL等价于dm^3 ,但根据品脱,它们不相等。 I don't want compatibility, which Pint provides via the is_compatible_with method.我不想要兼容性,Pint 通过is_compatible_with方法提供。 For example, s is compatible with ms , but they are not equivalent.例如, sms兼容,但它们并不等效。

import pint
ureg = pint.UnitRegistry()

nM = ureg.Unit('nM')
nmol_L = ureg.Unit('nmol/L')
m = ureg.Unit('m')
ft = ureg.Unit('ft')

nM == nmol_L  # False
m == ft  # False

nM.is_compatible_with(nmol_L)  # True
m.is_compatible_with(ft)  # True

# What operation does this?
# nM equivalent to nmol  # Should be True
# m equivalent to ft  # Should be False

One workaround is to convert the units to quantities, take their ratio, drop it to base units, and then test that the quantity is dimensionless and its quantity is equal to 1. Note that due to rounding error, you can't just do a regular equals;一种解决方法是将单位转换为数量,取它们的比率,将其降为基本单位,然后测试该数量是否为无量纲且其数量等于 1。请注意,由于舍入误差,您不能只执行常规等于; you need to do an approximately equals and hope you never encounter any units are almost exactly the same, but not quite.你需要做一个近似等于并希望你永远不会遇到任何单位几乎完全相同,但不完全相同。

from math import isclose
import pint
ureg = pint.UnitRegistry()

nM = ureg.Unit('nM')
nmol_L = ureg.Unit('nmol/L')
m = ureg.Unit('m')
ft = ureg.Unit('ft')

def is_equivalent(first: pint.Unit, second: pint.Unit):
    ratio = ((1 * first) / (1 * second)).to_base_units()
    return ratio.dimensionless and isclose(ratio.magnitude, 1)

is_equivalent(nM, nmol_L)  # True
is_equivalent(m, ft)  # False

One workaround is to use the UnitRegistry.convert(number, current_unit, new_unit) method.一种解决方法是使用UnitRegistry.convert(number, current_unit, new_unit)方法。 If you try to convert a 1 from one unit to another, the result will still be 1 if the units are equivalent.如果您尝试将 1 从一种单位转换为另一种单位,如果单位相等,则结果仍为 1。 It will raise a DimensionalityError error if the units are not compatible.如果单位不兼容,它将引发DimensionalityError错误。 Note that due to rounding error, you can't just do a regular equals on the 1s;请注意,由于舍入错误,您不能只对 1 执行常规等于; you need to do an approximately equals and hope you never encounter any units are almost exactly the same, but not quite.你需要做一个近似等于并希望你永远不会遇到任何单位几乎完全相同,但不完全相同。

from math import isclose
import pint
ureg = pint.UnitRegistry()

nM = ureg.Unit('nM')
nmol_L = ureg.Unit('nmol/L')
m = ureg.Unit('m')
ft = ureg.Unit('ft')

def is_equivalent(first: pint.Unit, second: pint.Unit):
    try:
        factor = ureg.convert(1, first, second)
    except pint.DimensionalityError:
        return False
    return isclose(factor, 1)

is_equivalent(nM, nmol_L)  # True
is_equivalent(m, ft)  # False

Note that it is not documented that it is legal to supply a pint.Unit to convert .请注意,没有记录表明提供pint.Unitconvert是合法的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM