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

argmax and argmin give incorrect results on option types when axis=None #1106

Closed
agoose77 opened this issue Sep 24, 2021 · 3 comments · Fixed by #1156
Closed

argmax and argmin give incorrect results on option types when axis=None #1106

agoose77 opened this issue Sep 24, 2021 · 3 comments · Fixed by #1156
Labels
bug The problem described is something that must be fixed good first issue Good for newcomers

Comments

@agoose77
Copy link
Collaborator

Version of Awkward Array

1.5.0

Description and code to reproduce

Consider

>>> x = ak.Array([1, 2, 3, None, 4])
>>> ak.argmax(x)
3

What is happening is that ak._util.completely_flatten is dropping the None values, consequently shifting the position of the elements before calling np.argmax.

@agoose77 agoose77 added the bug (unverified) The problem described would be a bug, but needs to be triaged label Sep 24, 2021
@agoose77 agoose77 changed the title argmax and argmin give incorrect results on masked array when axis is None argmax and argmin give incorrect results on option types when axis=None Sep 24, 2021
@jpivarski
Copy link
Member

Right. This could be fixed by having argmin/argmax with axis=None first apply ak.fill_none with axis=-1 and a fill value that is -np.inf for argmax and np.inf for argmin, before running it through ak._util.completely_flatten.

>>> ak.argmax(ak.fill_none(x, -np.inf, axis=-1))
4

This converts the numerical data into floating point numbers, which doesn't break argmin/argmax (positions of minimum/maximum are unchanged), but it's an unnecessary computation. A more sophisticated version would use np.iinfo(dtype).min and .max instead of -np.inf and np.inf, but an array could contain multiple dtypes. It's maybe best for a first fix to just use -np.inf and np.inf.

@jpivarski jpivarski added bug The problem described is something that must be fixed good first issue Good for newcomers and removed bug (unverified) The problem described would be a bug, but needs to be triaged labels Sep 24, 2021
@ianna
Copy link
Collaborator

ianna commented Nov 17, 2021

or by using axis=-1:

>>> x = ak.Array([1, 2, 3, None, 4])
>>> ak.argmax(x, axis=-1)
4

@jpivarski jpivarski linked a pull request Nov 17, 2021 that will close this issue
@jpivarski
Copy link
Member

I had forgotten about this, but it's an important bug to fix promptly. I applied the easy fix described above in both v1 and v2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug The problem described is something that must be fixed good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants