Skip to content

ENH: Add an "isclose" function for element-wise approximate floating point comparison of two arrays. #224

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

Closed
wants to merge 13 commits into from

Conversation

joferkington
Copy link
Contributor

Following the discussion on the mailing list, it seems like a floating point comparison function that returns a boolean array would be useful.

@charris
Copy link
Member

charris commented Mar 3, 2012

Might be worth making this NA aware.

atol = 1e-8

def test_ip_isclose(self):
"""Parametric test factory."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docstring should be removed, otherwise np.test(verbose=2) won't show the arguments to yield (IIRC). Same for other test methods.

@rgommers
Copy link
Member

rgommers commented Mar 3, 2012

Looks good, nice test coverage. It would be useful to test against allclose to make sure that isclose().all() == allclose().

@rgommers
Copy link
Member

rgommers commented Mar 3, 2012

I do confess to not liking the name isclose, although it's hard to come up with a better one. I'd expect any name starting with "is" or "has" to return a single boolean.

@rgommers
Copy link
Member

rgommers commented Mar 3, 2012

allclose behavior after fixing http://projects.scipy.org/numpy/ticket/1905 that is. If you could review and add the proposed patch in that ticket too, that would be great.

@joferkington
Copy link
Contributor Author

I've made things MA-aware, and added isclose().all() == allclose() tests. I completely agree on the name implying a boolean return, but "approximately_equal" is a bit too long, and I can't think of anything better either.

@charris
Copy link
Member

charris commented Mar 4, 2012

Actually, I was thinking of NA, where NA means missing data:

In [1]: a = arange(5, maskna=1)

In [2]: b = arange(5)

In [3]: a[1] = NA

In [4]: isclose(a,b)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/home/charris/<ipython-input-4-512760b5689e> in <module>()
----> 1 isclose(a,b)

/home/charris/.local/lib/python2.7/site-packages/numpy/core/numeric.pyc in isclose(a, b, rtol, atol, equal_nan)
   2091     xfin = isfinite(x)
   2092     yfin = isfinite(y)
-> 2093     if all(xfin) and all(yfin):
   2094         return within_tol(x, y, atol, rtol)
   2095     else:

ValueError: numpy.NA represents an unknown missing value, so its truth value cannot be determined

There is a skipna flag in all that you might want to use in some spots. Maybe it should be an option in isclose also. You can get the mask using isna(a), and for efficiency you can also check it the array is maskna

In [6]: a.flags.maskna
Out[6]: True

.Edit: There is also some trailing whitespace.

@joferkington
Copy link
Contributor Author

Ah! Sorry, I was confused. 408f94c should make things NA-aware. Would it be useful to go ahead and make allclose NA-aware as well (and add a skipna kwarg, a la all), or should that be left for another time?

xfin[isna(x)] = False
if y.flags.maskna:
yfin[isna(y)] = False
if all(xfin) and all(yfin):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified also:

In [9]: a = arange(5, maskna=1)

In [10]: a[1] = NA

In [12]: isfinite(a) & isfinite(a)
Out[12]: array([ True, NA,  True,  True,  True], dtype=bool)

In [12]: isfinite(a) & isfinite(a)
Out[12]: array([ True, NA, True, True, True], dtype=bool)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Done in 3d28404 along with your other suggestions.

@charris
Copy link
Member

charris commented Mar 4, 2012

Hmm, that is strange. There may indeed be a bug somewhere. I just pushed this PR after squashing it a bit and adding a release note. But I'll leave it open for a while. If you can produce a simple failing example it would be helpful.

@charris
Copy link
Member

charris commented Mar 4, 2012

Oh, and how current is your numpy fork?

@joferkington
Copy link
Contributor Author

The "bug" I was running into turned out to just be because ma masked arrays don't have a skipna option in their allmethod. (Which makes sense, in retrospect.) I didn't realize that the error was being thrown with a masked array, and so I couldn't reproduce it by trying a "normal" ndarray. I probably should have added another comment instead of editing the one in question.

@joferkington
Copy link
Contributor Author

I think it's safe to close the pull request. There's no bug, I'm just easily confused. :) Thanks!

@charris
Copy link
Member

charris commented Mar 4, 2012

OK, that is a bug in ma, please open a ticket for that. Or you could make a PR with a fix ;) I'll go ahead and close this PR.

luyahan pushed a commit to plctlab/numpy that referenced this pull request Apr 25, 2024
feat: Add vst2[q]_[s8|s16|s32|f32|u8|u16|u32|s64|u64]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants