Skip to content
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

Supporting arrays for approx #1994

Closed
DerWeh opened this issue Oct 9, 2016 · 7 comments
Closed

Supporting arrays for approx #1994

DerWeh opened this issue Oct 9, 2016 · 7 comments

Comments

@DerWeh
Copy link

@DerWeh DerWeh commented Oct 9, 2016

The current syntax of pytest.approx doesn't work good with arrays. np.allclose must be used to compare arrays.
In

def test_array():
    assert np.allclose(np.array([1., 2.]), np.array([1., 2.]))
    assert np.all(np.array([1., 2.]) == pytest.approx(np.array([1., 2.])))

the first assert evaluates to true, the second to false.
Something like

assert np.array([1., 2.]) == pytest.approx(np.array([1., 2.])) 

Would be the most intuitive if it worked, as it also works with lists.

@jorgsk
Copy link

@jorgsk jorgsk commented Oct 17, 2016

I have been using Numpy's own testing functions for this, for example assert_almost_equal and assert_approx_equal: http://docs.scipy.org/doc/numpy/reference/routines.testing.html

@nicoddemus
Copy link
Member

@nicoddemus nicoddemus commented Oct 19, 2016

Pinging @edisongustavo

@RonnyPfannschmidt
Copy link
Member

@RonnyPfannschmidt RonnyPfannschmidt commented Oct 19, 2016

current pytest behaviour is wrong, should we a) support numpy, or b) fail and defer to numpy functions

im for b

@edisongustavo
Copy link
Contributor

@edisongustavo edisongustavo commented Oct 19, 2016

At work we implemented a fixture (named: almost_equal) with the same idea as pytest.approx (maybe we could help add it upstream?) which supports numpy arrays as numeric code is festered with numpy arrays.

So it supports the following:

import numpy as np
def test(almost_equal):
    assert almost_equal([1.2345, 12.3456], places=3) == [1.2349, 12.3459]
    assert almost_equal(np.array([1.2345, 12.3456]), places=3) == [1.2349, 12.3459]
    assert almost_equal(np.array([1.2345, 12.3456]), places=3) == np.array([1.2349, 12.3459])

It also supports floats (dhuh).

@nicoddemus
Copy link
Member

@nicoddemus nicoddemus commented Oct 19, 2016

@edisongustavo

maybe we could help add it upstream?

Sure, a PR improving pytest.approx would be most welcome! 😁

Pinging @kalekundert as well

@kalekundert
Copy link
Contributor

@kalekundert kalekundert commented Jun 6, 2017

Sorry for the slow response. This looks really useful; I'll try to put a PR together this weekend.

In response to @RonnyPfannschmidt, I think natively supporting numpy would be better than deferring to the numpy functions. The reason is that numpy uses a slightly different algorithm to determine whether two numbers are close or not. The difference is described in the docs, and is small enough that you'd probably never notice it, but I think it still makes sense to use the same algorithm for every type of input.

@RonnyPfannschmidt
Copy link
Member

@RonnyPfannschmidt RonnyPfannschmidt commented Jun 6, 2017

precisely because numpy does approximation the numpy way is why i want to defer to numpy approximations for numpy data - so that tests testing numpy are consistent with numpy logic

from my point of view having testing and real code use different approximation algorithmics is simply the worst thing you can inflict to numeric code as it pretty much puts correctness at stake

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants