-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Better introspection for comparing numpy arrays #5347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
IMO the idiomatic solution is to use (we've run into similar issues in Hypothesis; numpy arrays are just complex enough to need special handling and papering over the differences only makes it worse in the long run 😭) |
Does pytest allow to write e.g. a numpy testing plugin that extends the assert statement bytecode rewriter? This plugin would also need to provide a new version of the |
That's an interesting proposal. We would need to figure out an API and hooks in order to do that though. This is interesting as it would allow all kinds of extensions, like the one proposed here and #3479. |
even with that, I don't think the one proposed here is a good proposal, numpy overrides |
I agree that rewriting Independently of this is the question if pytest could introspect
one could then write
which restores the pattern of starting all test conditions with |
The error is a numpy error, which you'd get when asserting the truthiness of an array regardless of whether you're using pytest or not (this falls out of the numpy implementation of |
@samueljsb As I've written in the original message Additional problem: If the dimensions are not the same, numpy currently just returns
Fundamental conclusion: Numpy arrays are too complex for attempting any simple These are implemented in
is the right way to go. Possible enhancement: A plugin system for assert statement bytecode rewriting as proposed in #5347 (comment) could allow to write
The rewrite rule |
Informative dev issue. Thanks! I ended up just doing:
seems to work for now, don't know if you guys have fixed it or have a better patch. Will be using this a lot. |
@MachinesJesus that‘s rather obscuring what is happening. assert_allclose always returns None. So the assert never fails. It‘s the assert_allclose that raises the exception and formats the error message. When using that you don‘t need the outer assert. |
Hi @timhoffm, Thanks yes I tested and found unnecessary. Will keep watching this for a better solution. It would be great to see what value of the array it failed on. Chris |
Hi all, I'm having a similar issue, except that my numpy arrays are buried deep in some custom classes (which have dicts, which have dicts, which may contain arrays). Calling e.g. np.testing methods doesn't help me. When trying to create a custom reporter ( |
Hello, this issue of using nested objects is at least partly covered by the customCompare and compareNested functions inside https://github.com/AdityaSavara/UnitTesterSG It even intentionally allows for comparing lists to arrays etc. because in scientific applications we may not be trying to compare apples to apples, and just want to know that the inside is the same. I think it would be great if the custom functions from UnitTesterSG made it into pytest in some form. Right now, UnitTesterSG is made to be compatible with pytest. A typical unit test file is shown here: https://github.com/AdityaSavara/UnitTesterSG/blob/master/test12/test_12.py |
I think we can close this now that a = np.array([1, 2, 3])
b = np.array([1, 2, 4])
assert a == pytest.approx(b) |
While this is a working workaround for comparing arrays while still seeing the difference, this opens a whole can of worms of other issues, the approx really takes it self seriously with the "approximate": np.array([1,2,3]) == approx(np.array([1,2,3]))
Out[22]: True
[] == approx(np.array([]))
Out[23]: True
0 == approx(np.array([]))
Out[24]: True
"" == approx(np.array([]))
Out[25]: True
(0,) == approx(np.array([]))
Out[26]: False
(0) == approx(np.array([]))
Out[27]: True
(0) == approx(np.zeros([2,3]))
Out[29]: True I am struggling with numpys overwritten == everyday it feels like! >>> assert np.array([]) == np.array([])
raises AssertionError
>>> assert np.array([0,1]) == np.array([0,1])
raises ValueError I can catch the value error but I obviously cant catch the assertion error. Anyway, I digress, |
Uh oh!
There was an error while loading. Please reload this page.
With plain python lists I can do:
That does not work for numpy arrays
There are two ways out:
Using numpy.testing
This has the advantage of giving good context information on the error.
But it is far from the simple assert
assert a == b
paradigm in pytest.Alternatively we can aggregate the the check to a single bool
This looks much nicer, but pytest cannot tell anymore where the error is:
Question: Would it in principle be possible with pytest's introspection capabilities to resolve the contents of
np.all()
and give a better error message? In a frist step maybe something likeThis of course, would require an assumption on the semantics of
np.all
.Alternative idea:
Could pytest be made to rewritePossibly not a good idea because it introduces a different semantic compared to a plainassert a == b
tonp.testing.assert_equal(a, b)
if either a or b is a numpy array?a == b
.The text was updated successfully, but these errors were encountered: