question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

np.testing.assert_allclose does not match np.allclose

See original GitHub issue

Here is the reproduced bug below from running iPython to reproduce what happened in the code. Basically, np.testing.assert_allclose does not match np.allclose. Meaning np.allclose returned true but np.testing.assert_allclose raised an error in this case.

Python 3.5.1 |Continuum Analytics, Inc.| (default, Dec  7 2015, 11:24:55)
IPython 4.2.0
numpy 1.11.0 py35_1
 ---------------------------------------------------------------------------------------------------------------------------------------
In [38]: a = np.array([6.938894e-18, -3.469447e-18, -3.469447e-18])

In [39]: b = np.zeros(3)

In [40]: np.allclose(a, b, rtol=1e-07)
Out[40]: True

In [41]: np.testing.assert_allclose(a, b, rtol=1e-07)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-41-3be1e226d188> in <module>()
----> 1 np.testing.assert_allclose(a, b, rtol=1e-07)

/Users/mlyle/anaconda/envs/python3/lib/python3.5/site-packages/numpy/testing/utils.py in assert_allclose(actual, desired, rtol, atol, equal_nan, err_msg, verbose)
   1389     header = 'Not equal to tolerance rtol=%g, atol=%g' % (rtol, atol)
   1390     assert_array_compare(compare, actual, desired, err_msg=str(err_msg),
-> 1391                          verbose=verbose, header=header)
   1392
   1393 def assert_array_almost_equal_nulp(x, y, nulp=1):

/Users/mlyle/anaconda/envs/python3/lib/python3.5/site-packages/numpy/testing/utils.py in assert_array_compare(comparison, x, y, err_msg, verbose, header, precision)
    731                                 names=('x', 'y'), precision=precision)
    732             if not cond:
--> 733                 raise AssertionError(msg)
    734     except ValueError:
    735         import traceback

AssertionError:
Not equal to tolerance rtol=1e-07, atol=0

(mismatch 100.0%)
 x: array([  6.938894e-18,  -3.469447e-18,  -3.469447e-18])
 y: array([ 0.,  0.,  0.])

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
njsmithcommented, Jun 10, 2016

Yes, they have different defaults for the atol and rtol parameters.

There’s some long discussion we had about this that you can probably find if you search… IIRC it got bogged down with the folks who use allclose saying that if we changed the defaults to match assert_allclose then it would break their test suites so we definitely shouldn’t do that, and the folks who use assert_allclose saying that if we changed the defaults to match allclose then it would break their test suite so we definitely shouldn’t do that. Both sides have a point I guess, but it is unfortunate 😦

1reaction
raaaaaymondcommented, Feb 5, 2021

Suggestion, maybe something like the following?

“Due to different default parameter values, its behaviour is different to allclose, but they are functionally equivalent.”

Read more comments on GitHub >

github_iconTop Results From Across the Web

Test if two numpy arrays are (close to) equal, including shape
What's happening is that allclose broadcasts its input. This allows comparisons with similarly shaped arrays (e.g. 3 and [3, 3, ...
Read more >
numpy.testing.assert_allclose — NumPy v1.24 Manual
Raises an AssertionError if two objects are not equal up to desired tolerance. Given two array_like objects, check that their shapes and all...
Read more >
numpy.testing.assert_allclose — NumPy v1.9 Manual
Raises an AssertionError if two objects are not equal up to desired tolerance. The test is equivalent to allclose(actual, desired, rtol, atol).
Read more >
numpy.testing.assert_allclose - omz:software
Raise an assertion if two objects are not equal up to desired tolerance. The test is equivalent to allclose(actual, desired, rtol, atol). It ......
Read more >
Testing for near equality with “allclose”
A common idiom in NumPy is to use the np.allclose function, which checks ... pi to 7 decimal places not exactly equal to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found