用于浮点集合的Python单元测试中的assertAlmostEqual

时间:2012-08-27 05:46:42

标签: python unit-testing collections

assertAlmostEqual(x, y)中的Python's unit testing framework方法测试xy是否大致相等,假设它们是浮点数。

assertAlmostEqual()的问题在于它只适用于浮点数。我正在寻找像assertAlmostEqual()这样的方法,它可用于浮点数,浮点数集,浮点数字典,浮点数元组,浮点元组列表,浮点数列表等。

例如,让x = 0.1234567890y = 0.1234567891xy几乎相等,因为除了最后一个数字之外,它们同意每个数字。因此self.assertAlmostEqual(x, y)True,因为assertAlmostEqual()适用于花车。

我正在寻找更通用的assertAlmostEquals(),它还评估以下对True的调用:

  • self.assertAlmostEqual_generic([x, x, x], [y, y, y])
  • self.assertAlmostEqual_generic({1: x, 2: x, 3: x}, {1: y, 2: y, 3: y})
  • self.assertAlmostEqual_generic([(x,x)], [(y,y)])

是否有这样的方法或我必须自己实施?

澄清:

  • assertAlmostEquals()有一个名为places的可选参数,通过计算舍入到小数places的差值来比较数字。默认情况下为places=7,因此self.assertAlmostEqual(0.5, 0.4)为False而self.assertAlmostEqual(0.12345678, 0.12345679)为True。我的推测assertAlmostEqual_generic()应具有相同的功能。

  • 如果两个列表在完全相同的顺序中具有几乎相等的数字,则认为它们几乎相等。正式地,for i in range(n): self.assertAlmostEqual(list1[i], list2[i])

  • 同样,如果两个集合可以转换为几乎相等的列表(通过为每个集合分配一个订单),则认为它们几乎相等。

  • 类似地,如果每个字典的密钥集几乎等于另一个字典的密钥集,则认为两个字典几乎相等,并且对于每个这样几乎相等的密钥对,它们具有相应的几乎相等的值。

  • 一般情况下:我认为两个集合几乎相等,只要它们相等,除了一些相应的浮点数几乎相等。换句话说,我想比较对象,但在比较浮点数时,它们具有较低的(自定义)精度。

10 个答案:

答案 0 :(得分:51)

如果您不介意使用NumPy(Python(x,y)附带),您可能需要查看np.testing模块,该模块定义了assert_almost_equal函数等。

签名为np.testing.assert_almost_equal(actual, desired, decimal=7, err_msg='', verbose=True)

>>> x = 1.000001
>>> y = 1.000002
>>> np.testing.assert_almost_equal(x, y)
AssertionError: 
Arrays are not almost equal to 7 decimals
ACTUAL: 1.000001
DESIRED: 1.000002
>>> np.testing.assert_almost_equal(x, y, 5)
>>> np.testing.assert_almost_equal([x, x, x], [y, y, y], 5)
>>> np.testing.assert_almost_equal((x, x, x), (y, y, y), 5)

答案 1 :(得分:7)

以下是我实施通用is_almost_equal(first, second)功能的方法

首先,复制您需要比较的对象(firstsecond),但不要制作精确的副本:剪切在对象内遇到的任何浮动的无效小数位。< / p>

现在您已经删除了无效小数位的firstsecond副本,只需使用first运算符比较second==

假设我们有cut_insignificant_digits_recursively(obj, places)函数复制obj,但只留下原始places中每个浮点数的obj个最高有效十进制数字。这是is_almost_equals(first, second, places)的工作实现:

from insignificant_digit_cutter import cut_insignificant_digits_recursively

def is_almost_equal(first, second, places):
    '''returns True if first and second equal. 
    returns true if first and second aren't equal but have exactly the same
    structure and values except for a bunch of floats which are just almost
    equal (floats are almost equal if they're equal when we consider only the
    [places] most significant digits of each).'''
    if first == second: return True
    cut_first = cut_insignificant_digits_recursively(first, places)
    cut_second = cut_insignificant_digits_recursively(second, places)
    return cut_first == cut_second

这是cut_insignificant_digits_recursively(obj, places)的工作实现:

def cut_insignificant_digits(number, places):
    '''cut the least significant decimal digits of a number, 
    leave only [places] decimal digits'''
    if  type(number) != float: return number
    number_as_str = str(number)
    end_of_number = number_as_str.find('.')+places+1
    if end_of_number > len(number_as_str): return number
    return float(number_as_str[:end_of_number])

def cut_insignificant_digits_lazy(iterable, places):
    for obj in iterable:
        yield cut_insignificant_digits_recursively(obj, places)

def cut_insignificant_digits_recursively(obj, places):
    '''return a copy of obj except that every float loses its least significant 
    decimal digits remaining only [places] decimal digits'''
    t = type(obj)
    if t == float: return cut_insignificant_digits(obj, places)
    if t in (list, tuple, set):
        return t(cut_insignificant_digits_lazy(obj, places))
    if t == dict:
        return {cut_insignificant_digits_recursively(key, places):
                cut_insignificant_digits_recursively(val, places)
                for key,val in obj.items()}
    return obj

代码及其单元测试可在此处获取:https://github.com/snakile/approximate_comparator。我欢迎任何改进和错误修复。

答案 2 :(得分:7)

从python 3.5开始,您可以使用

进行比较
math.isclose(a, b, rel_tol=1e-9, abs_tol=0.0)

pep-0485所述。 实现应该等同于

abs(a-b) <= max( rel_tol * max(abs(a), abs(b)), abs_tol )

答案 3 :(得分:4)

没有这样的方法,你必须自己做。

对于列表和元组,定义很明显,但请注意,您提到的其他情况并不明显,因此难怪没有提供这样的函数。例如,{1.00001: 1.00002}几乎等于{1.00002: 1.00001}?处理此类案件需要选择是否依赖于键或值或两者。对于集合,您不太可能找到有意义的定义,因为集合是无序的,因此没有“对应”元素的概念。

答案 4 :(得分:4)

如果您不介意使用numpy软件包,则numpy.testing会使用assert_array_almost_equal方法。

这适用于array_like个对象,因此它适用于浮点数的数组,列表和元组,但它不适用于集合和字典。

文档为here

答案 5 :(得分:1)

你可能必须自己实现它,而真正的列表和集合可以以相同的方式迭代,字典是一个不同的故事,你迭代他们的键而不是值,第三个例子对我来说似乎有点模糊,做您的意思是比较集合中的每个值,或每个集合中的每个值。

这是一个简单的代码片段。

def almost_equal(value_1, value_2, accuracy = 10**-8):
    return abs(value_1 - value_2) < accuracy

x = [1,2,3,4]
y = [1,2,4,5]
assert all(almost_equal(*values) for values in zip(x, y))

答案 6 :(得分:0)

另一种方法是通过将每个浮点数转换为具有固定精度的字符串来将数据转换为可比较的形式。

def comparable(data):
    """Converts `data` to a comparable structure by converting any floats to a string with fixed precision."""
    if isinstance(data, (int, str)):
        return data
    if isinstance(data, float):
        return '{:.4f}'.format(data)
    if isinstance(data, list):
        return [comparable(el) for el in data]
    if isinstance(data, tuple):
        return tuple([comparable(el) for el in data])
    if isinstance(data, dict):
        return {k: comparable(v) for k, v in data.items()}

然后您可以:

self.assertEquals(comparable(value1), comparable(value2))

答案 7 :(得分:0)

这些答案都不对我有用。以下代码适用于python集合,类,数据类和namedtuple。我可能已经忘记了一些东西,但是到目前为止,这对我有用。

import unittest
from collections import namedtuple, OrderedDict
from dataclasses import dataclass
from typing import Any


def are_almost_equal(o1: Any, o2: Any, max_abs_ratio_diff: float, max_abs_diff: float) -> bool:
    """
    Compares two objects by recursively walking them trough. Equality is as usual except for floats.
    Floats are compared according to the two measures defined below.

    :param o1: The first object.
    :param o2: The second object.
    :param max_abs_ratio_diff: The maximum allowed absolute value of the difference.
    `abs(1 - (o1 / o2)` and vice-versa if o2 == 0.0. Ignored if < 0.
    :param max_abs_diff: The maximum allowed absolute difference `abs(o1 - o2)`. Ignored if < 0.
    :return: Whether the two objects are almost equal.
    """
    if type(o1) != type(o2):
        return False

    composite_type_passed = False

    if hasattr(o1, '__slots__'):
        if len(o1.__slots__) != len(o2.__slots__):
            return False
        if any(not are_almost_equal(getattr(o1, s1), getattr(o2, s2),
                                    max_abs_ratio_diff, max_abs_diff)
            for s1, s2 in zip(sorted(o1.__slots__), sorted(o2.__slots__))):
            return False
        else:
            composite_type_passed = True

    if hasattr(o1, '__dict__'):
        if len(o1.__dict__) != len(o2.__dict__):
            return False
        if any(not are_almost_equal(k1, k2, max_abs_ratio_diff, max_abs_diff)
            or not are_almost_equal(v1, v2, max_abs_ratio_diff, max_abs_diff)
            for ((k1, v1), (k2, v2))
            in zip(sorted(o1.__dict__.items()), sorted(o2.__dict__.items()))
            if not k1.startswith('__')):  # avoid infinite loops
            return False
        else:
            composite_type_passed = True

    if isinstance(o1, dict):
        if len(o1) != len(o2):
            return False
        if any(not are_almost_equal(k1, k2, max_abs_ratio_diff, max_abs_diff)
            or not are_almost_equal(v1, v2, max_abs_ratio_diff, max_abs_diff)
            for ((k1, v1), (k2, v2)) in zip(sorted(o1.items()), sorted(o2.items()))):
            return False

    elif any(issubclass(o1.__class__, c) for c in (list, tuple, set)):
        if len(o1) != len(o2):
            return False
        if any(not are_almost_equal(v1, v2, max_abs_ratio_diff, max_abs_diff)
            for v1, v2 in zip(o1, o2)):
            return False

    elif isinstance(o1, float):
        if o1 == o2:
            return True
        else:
            if max_abs_ratio_diff > 0:  # if max_abs_ratio_diff < 0, max_abs_ratio_diff is ignored
                if o2 != 0:
                    if abs(1.0 - (o1 / o2)) > max_abs_ratio_diff:
                        return False
                else:  # if both == 0, we already returned True
                    if abs(1.0 - (o2 / o1)) > max_abs_ratio_diff:
                        return False
            if 0 < max_abs_diff < abs(o1 - o2):  # if max_abs_diff < 0, max_abs_diff is ignored
                return False
            return True

    else:
        if not composite_type_passed:
            return o1 == o2

    return True


class EqualityTest(unittest.TestCase):

    def test_floats(self) -> None:
        o1 = ('hi', 3, 3.4)
        o2 = ('hi', 3, 3.400001)
        self.assertTrue(are_almost_equal(o1, o2, 0.0001, 0.0001))
        self.assertFalse(are_almost_equal(o1, o2, 0.00000001, 0.00000001))

    def test_ratio_only(self):
        o1 = ['hey', 10000, 123.12]
        o2 = ['hey', 10000, 123.80]
        self.assertTrue(are_almost_equal(o1, o2, 0.01, -1))
        self.assertFalse(are_almost_equal(o1, o2, 0.001, -1))

    def test_diff_only(self):
        o1 = ['hey', 10000, 1234567890.12]
        o2 = ['hey', 10000, 1234567890.80]
        self.assertTrue(are_almost_equal(o1, o2, -1, 1))
        self.assertFalse(are_almost_equal(o1, o2, -1, 0.1))

    def test_both_ignored(self):
        o1 = ['hey', 10000, 1234567890.12]
        o2 = ['hey', 10000, 0.80]
        o3 = ['hi', 10000, 0.80]
        self.assertTrue(are_almost_equal(o1, o2, -1, -1))
        self.assertFalse(are_almost_equal(o1, o3, -1, -1))

    def test_different_lengths(self):
        o1 = ['hey', 1234567890.12, 10000]
        o2 = ['hey', 1234567890.80]
        self.assertFalse(are_almost_equal(o1, o2, 1, 1))

    def test_classes(self):
        class A:
            d = 12.3

            def __init__(self, a, b, c):
                self.a = a
                self.b = b
                self.c = c

        o1 = A(2.34, 'str', {1: 'hey', 345.23: [123, 'hi', 890.12]})
        o2 = A(2.34, 'str', {1: 'hey', 345.231: [123, 'hi', 890.121]})
        self.assertTrue(are_almost_equal(o1, o2, 0.1, 0.1))
        self.assertFalse(are_almost_equal(o1, o2, 0.0001, 0.0001))

        o2.hello = 'hello'
        self.assertFalse(are_almost_equal(o1, o2, -1, -1))

    def test_namedtuples(self):
        B = namedtuple('B', ['x', 'y'])
        o1 = B(3.3, 4.4)
        o2 = B(3.4, 4.5)
        self.assertTrue(are_almost_equal(o1, o2, 0.2, 0.2))
        self.assertFalse(are_almost_equal(o1, o2, 0.001, 0.001))

    def test_classes_with_slots(self):
        class C(object):
            __slots__ = ['a', 'b']

            def __init__(self, a, b):
                self.a = a
                self.b = b

        o1 = C(3.3, 4.4)
        o2 = C(3.4, 4.5)
        self.assertTrue(are_almost_equal(o1, o2, 0.3, 0.3))
        self.assertFalse(are_almost_equal(o1, o2, -1, 0.01))

    def test_dataclasses(self):
        @dataclass
        class D:
            s: str
            i: int
            f: float

        @dataclass
        class E:
            f2: float
            f4: str
            d: D

        o1 = E(12.3, 'hi', D('hello', 34, 20.01))
        o2 = E(12.1, 'hi', D('hello', 34, 20.0))
        self.assertTrue(are_almost_equal(o1, o2, -1, 0.4))
        self.assertFalse(are_almost_equal(o1, o2, -1, 0.001))

        o3 = E(12.1, 'hi', D('ciao', 34, 20.0))
        self.assertFalse(are_almost_equal(o2, o3, -1, -1))

    def test_ordereddict(self):
        o1 = OrderedDict({1: 'hey', 345.23: [123, 'hi', 890.12]})
        o2 = OrderedDict({1: 'hey', 345.23: [123, 'hi', 890.0]})
        self.assertTrue(are_almost_equal(o1, o2, 0.01, -1))
        self.assertFalse(are_almost_equal(o1, o2, 0.0001, -1))

答案 8 :(得分:0)

我仍然会使用self.assertEqual(),因为当狗屎撞到风扇时,它保持了最丰富的信息。您可以通过四舍五入来做到这一点,例如。

self.assertEqual(round_tuple((13.949999999999999, 1.121212), 2), (13.95, 1.12))

round_tuple在哪里

def round_tuple(t: tuple, ndigits: int) -> tuple:
    return tuple(round(e, ndigits=ndigits) for e in t)

def round_list(l: list, ndigits: int) -> list:
    return [round(e, ndigits=ndigits) for e in l]

根据python文档(请参阅https://stackoverflow.com/a/41407651/1031191),您可以摆脱13.94999999之类的舍入问题,因为13.94999999 == 13.95True

答案 9 :(得分:0)

使用熊猫

另一种方法是将两个字典等中的每一个转换为熊猫数据帧,然后使用 pd.testing.assert_frame_equal() 比较两者。我已经成功地使用它来比较字典列表。

以前的答案通常不适用于涉及字典的结构,但这个答案应该。我还没有在高度嵌套的结构上对此进行详尽的测试,但想象一下 Pandas 会正确处理它们。

示例 1:比较两个字典

为了说明这一点,我将使用您的 dict 示例数据,因为其他方法不适用于 dict。你的字典是:

x, y = 0.1234567890, 0.1234567891
{1: x, 2: x, 3: x}, {1: y, 2: y, 3: y}

然后我们可以这样做:

pd.testing.assert_frame_equal(      
   pd.DataFrame.from_dict({1: x, 2: x, 3: x}, orient='index')   ,         
   pd.DataFrame.from_dict({1: y, 2: y, 3: y}, orient='index')   )

这不会引发错误,这意味着它们等于一定程度的精度。

但是如果我们要这样做

pd.testing.assert_frame_equal(      
   pd.DataFrame.from_dict({1: x, 2: x, 3: x}, orient='index')   ,         
   pd.DataFrame.from_dict({1: y, 2: y, 3: y + 1}, orient='index')   ) #add 1 to last value

然后我们会收到以下信息性消息:

AssertionError: DataFrame.iloc[:, 0] (column name="0") are different

DataFrame.iloc[:, 0] (column name="0") values are different (33.33333 %)
[index]: [1, 2, 3]
[left]:  [0.123456789, 0.123456789, 0.123456789]
[right]: [0.1234567891, 0.1234567891, 1.1234567891]

有关详细信息,请参阅 pd.testing.assert_frame_equal documentation ,特别是参数 check_exactrtolatol 以了解有关如何指定所需的相对精度或实际精度的信息。

示例 2:字典的嵌套字典

a = {i*10 : {1:1.1,2:2.1} for i in range(4)}
b = {i*10 : {1:1.1000001,2:2.100001} for i in range(4)}
# a = {0: {1: 1.1, 2: 2.1}, 10: {1: 1.1, 2: 2.1}, 20: {1: 1.1, 2: 2.1}, 30: {1: 1.1, 2: 2.1}}
# b = {0: {1: 1.1000001, 2: 2.100001}, 10: {1: 1.1000001, 2: 2.100001}, 20: {1: 1.1000001, 2: 2.100001}, 30: {1: 1.1000001, 2: 2.100001}}

然后做

pd.testing.assert_frame_equal(   pd.DataFrame(a), pd.DataFrame(b) )

- 它不会引发错误:所有值都非常相似。 但是,如果我们更改一个值,例如

b[30][2] += 1
#  b = {0: {1: 1.1000001, 2: 2.1000001}, 10: {1: 1.1000001, 2: 2.1000001}, 20: {1: 1.1000001, 2: 2.1000001}, 30: {1: 1.1000001, 2: 3.1000001}}

然后运行相同的测试,我们得到以下明确的错误信息:

AssertionError: DataFrame.iloc[:, 3] (column name="30") are different

DataFrame.iloc[:, 3] (column name="30") values are different (50.0 %)
[index]: [1, 2]
[left]:  [1.1, 2.1]
[right]: [1.1000001, 3.1000001]