95

Python 的单元测试框架中的assertAlmostEqual(x, y)方法在假设它们是浮点数的情况下测试它们是否并且近似相等。xy

问题assertAlmostEqual()在于它只适用于浮点数。我正在寻找一种assertAlmostEqual()适用于浮点数列表、浮点数集、浮点数字典、浮点数元组、浮点数元组列表、浮点数列表集等的方法。

例如,让x = 0.1234567890, y = 0.1234567891x并且y几乎相等,因为除了最后一个数字之外,它们在每个数字上都一致。因此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]).

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

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

  • 一般来说:我认为两个集合几乎相等,如果它们相等,除了一些对应的浮点数几乎相等。换句话说,我想真正比较对象,但在比较沿途的浮点数时精度较低(自定义)。

4

11 回答 11

81

如果您不介意使用 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)
于 2012-08-27T10:04:01.033 回答
13

从 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 )
于 2016-07-01T18:33:24.857 回答
9

这是我实现通用is_almost_equal(first, second)功能的方式:

首先,复制您需要比较的对象 (firstsecond),但不要进行精确复制:剪掉您在对象内遇到的任何浮点数的无关紧要的十进制数字。

现在您已经有了 和 的副本,first其中second无意义的十进制数字已经消失,只需比较firstsecond使用==运算符即可。

假设我们有一个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 。我欢迎任何改进和错误修复。

于 2012-08-27T13:37:19.073 回答
5

如果您不介意使用该numpy软件包,则可以使用numpy.testingassert_array_almost_equal方法。

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

文档在这里

于 2014-02-11T12:00:02.390 回答
4

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

对于列表和元组,定义是显而易见的,但请注意,您提到的其他情况并不明显,因此没有提供这样的功能也就不足为奇了。例如,{1.00001: 1.00002}几乎等于{1.00002: 1.00001}?处理这种情况需要选择是否接近取决于键或值或两者。对于集合,您不太可能找到有意义的定义,因为集合是无序的,因此没有“对应”元素的概念。

于 2012-08-27T06:06:28.917 回答
2

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

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

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))
于 2012-08-27T06:08:49.477 回答
2

这些答案都不适合我。以下代码应该适用于 python 集合、类、数据类和命名元组。我可能忘记了一些东西,但到目前为止这对我有用。

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))
于 2019-02-12T12:18:26.267 回答
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.95因为True.

于 2019-06-22T22:42:00.473 回答
0

使用熊猫

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

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

示例 1:比较两个字典

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

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文档 ,尤其是参数check_exact,以获取有关如何指定相对或实际所需精度的信息。rtolatol

示例 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]
于 2021-05-09T19:16:15.263 回答
0

您还可以unittest.assertAlmostEquals()通过向单元测试添加方法来递归调用已经存在的元素并跟踪您正在比较的元素。

例如,对于列表列表和浮点元组列表:

def assertListAlmostEqual(self, first, second, delta=None, context=None):
    """Asserts lists of lists or tuples to check if they compare and 
       shows which element is wrong when comparing two lists
    """
    self.assertEqual(len(first), len(second), msg="List have different length")
    context = [first, second] if context is None else context
    for i in range(0, len(first)):
        if isinstance(first[0], tuple):
            context.append(i)
            self.assertListAlmostEqual(first[i], second[i], delta, context=context)
        if isinstance(first[0], list):
            context.append(i)
            self.assertListAlmostEqual(first[i], second[i], delta, context=context)
        elif isinstance(first[0], float):
            msg = "Difference in \n{} and \n{}\nFaulty element index={}".format(context[0], context[1], context[2:]+[i]) \
                if context is not None else None
            self.assertAlmostEqual(first[i], second[i], delta, msg=msg)

输出类似:

line 23, in assertListAlmostEqual
    self.assertAlmostEqual(first[i], second[i], delta, msg=msg)
AssertionError: 5.0 != 6.0 within 7 places (1.0 difference) : Difference in 
[(0.0, 5.0), (8.0, 2.0), (10.0, 1.999999), (11.0, 1.9999989090909092)] and 
[(0.0, 6.0), (8.0, 2.0), (10.0, 1.999999), (11.0, 1.9999989)]
Faulty element index=[0, 1]
于 2021-08-19T16:21:41.427 回答
-1

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

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))
于 2018-08-23T15:51:49.390 回答