[Numpy-svn] r8540 - in branches/1.5.x/numpy/testing: . tests

numpy-svn at scipy.org numpy-svn at scipy.org
Wed Jul 28 18:32:55 EDT 2010


Author: ptvirtan
Date: 2010-07-28 17:32:55 -0500 (Wed, 28 Jul 2010)
New Revision: 8540

Modified:
   branches/1.5.x/numpy/testing/tests/test_utils.py
   branches/1.5.x/numpy/testing/utils.py
Log:
ENH: (backport r8539) testing: add assert_tol_equal for testing array equality with specified tolerances

Modified: branches/1.5.x/numpy/testing/tests/test_utils.py
===================================================================
--- branches/1.5.x/numpy/testing/tests/test_utils.py	2010-07-28 22:31:01 UTC (rev 8539)
+++ branches/1.5.x/numpy/testing/tests/test_utils.py	2010-07-28 22:32:55 UTC (rev 8540)
@@ -336,6 +336,29 @@
         if failed:
             raise AssertionError("wrong warning caught by assert_warn")
 
+class TestAssertAllclose(unittest.TestCase):
+    def test_simple(self):
+        x = 1e-3
+        y = 1e-9
+
+        assert_allclose(x, y, atol=1)
+        self.assertRaises(AssertionError, assert_allclose, x, y)
+
+        a = np.array([x, y, x, y])
+        b = np.array([x, y, x, x])
+
+        assert_allclose(a, b, atol=1)
+        self.assertRaises(AssertionError, assert_allclose, a, b)
+
+        b[-1] = y * (1 + 1e-8)
+        assert_allclose(a, b)
+        self.assertRaises(AssertionError, assert_allclose, a, b,
+                          rtol=1e-9)
+
+        assert_allclose(6, 10, rtol=0.5)
+        self.assertRaises(AssertionError, assert_allclose, 10, 6, rtol=0.5)
+
+
 class TestArrayAlmostEqualNulp(unittest.TestCase):
     def test_simple(self):
         dev = np.random.randn(10)

Modified: branches/1.5.x/numpy/testing/utils.py
===================================================================
--- branches/1.5.x/numpy/testing/utils.py	2010-07-28 22:31:01 UTC (rev 8539)
+++ branches/1.5.x/numpy/testing/utils.py	2010-07-28 22:32:55 UTC (rev 8540)
@@ -16,7 +16,7 @@
            'decorate_methods', 'jiffies', 'memusage', 'print_assert_equal',
            'raises', 'rand', 'rundocs', 'runstring', 'verbose', 'measure',
            'assert_', 'assert_array_almost_equal_nulp',
-           'assert_array_max_ulp', 'assert_warns']
+           'assert_array_max_ulp', 'assert_warns', 'assert_allclose']
 
 verbose = 0
 
@@ -1093,6 +1093,42 @@
 
     assert(sys.getrefcount(i) >= rc)
 
+def assert_allclose(actual, desired, rtol=1e-7, atol=0,
+                    err_msg='', verbose=True):
+    """
+    Raise an assertion if two objects are not equal up to desired tolerance.
+
+    The test is equivalent to ``allclose(actual, desired, rtol, atol)``
+
+    Parameters
+    ----------
+    actual : array_like
+        Array obtained.
+    desired : array_like
+        Array desired
+    rtol : float, optional
+        Relative tolerance
+    atol : float, optional
+        Absolute tolerance
+    err_msg : string
+        The error message to be printed in case of failure.
+    verbose : bool
+        If True, the conflicting values are appended to the error message.
+
+    Raises
+    ------
+    AssertionError
+        If actual and desired are not equal up to specified precision.
+
+    """
+    import numpy as np
+    def compare(x, y):
+        return np.allclose(x, y, rtol=rtol, atol=atol)
+    actual, desired = np.asanyarray(actual), np.asanyarray(desired)
+    header = 'Not equal to tolerance rtol=%g, atol=%g' % (rtol, atol)
+    assert_array_compare(compare, actual, desired, err_msg=str(err_msg),
+                         verbose=verbose, header=header)
+
 def assert_array_almost_equal_nulp(x, y, nulp=1):
     """
     Compare two arrays relatively to their spacing.




More information about the Numpy-svn mailing list