Skip to content

API: Refactor argmax and argmin - #614

Merged
hameerabbasi merged 2 commits into
pydata:masterfrom
mtsokol:new-argmax-argmin
Jan 2, 2024
Merged

hameerabbasi merged 2 commits into
pydata:masterfrom
mtsokol:new-argmax-argmin

Conversation

@mtsokol

@mtsokol mtsokol commented Dec 22, 2023 •

Copy link
Copy Markdown
Collaborator

Hi @hameerabbasi,

This PR refactors the current implementation of argmax and argmin. It removes "only 0.0 fill value" and "all non-fill values greater/less than the fill value" constraints. It also makes the core of the function jittable.

First #613 needs to be merged.

@mtsokol mtsokol self-assigned this Dec 22, 2023
@codecov

codecov Bot commented Dec 22, 2023 •

Copy link
Copy Markdown

Codecov Report

Merging #614 (cae0fe7) into master (f522926) will decrease coverage by 0.69%.
The diff coverage is 55.38%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #614      +/-   ##
==========================================
- Coverage   91.55%   90.86%   -0.69%     
==========================================
  Files          20       20              
  Lines        3516     3546      +30     
==========================================
+ Hits         3219     3222       +3     
- Misses        297      324      +27     

Comment thread sparse/_coo/common.py Fixed
Comment thread sparse/_coo/common.py Resolved
Comment thread sparse/_coo/common.py Fixed
@hameerabbasi

Copy link
Copy Markdown
Collaborator

I think the comments from the GitHub bot are valid in this PR and should be addressed.

@mtsokol
mtsokol force-pushed the new-argmax-argmin branch from 4f56751 to cae0fe7 Compare January 2, 2024 09:21
@mtsokol

mtsokol commented Jan 2, 2024

Copy link
Copy Markdown
Collaborator Author

I think the comments from the GitHub bot are valid in this PR and should be addressed.

Hi @hameerabbasi,
Now PR is ready from my side.

@hameerabbasi
hameerabbasi merged commit b12d51d into pydata:master Jan 2, 2024
@mtsokol
mtsokol deleted the new-argmax-argmin branch January 2, 2024 09:29
@hameerabbasi

Copy link
Copy Markdown
Collaborator

Thanks for your awesome work on this @mtsokol.

hameerabbasi pushed a commit that referenced this pull request Oct 7, 2026
`_arg_minmax_common` never normalized the axis, and its bounds check only
covered one side (`axis >= x.ndim`), so a negative axis reached the
transpose and reshape logic untouched. The initial `list.pop(axis)`
happens to work for `-1`, but the final un-transpose does
`new_transpose.insert(axis, new_transpose.pop(0))`, which for `-1`
inserts before the last element instead of at the end. The reduced axis
ends up in front.

Measured against numpy on a (2, 3) array:

    axis=1   numpy [[1], [0]] (2, 1)   sparse [[1], [0]] (2, 1)
    axis=-1  numpy [[1], [0]] (2, 1)   sparse [[1, 0]]   (1, 2)

A 1-D array with `axis=-1` raised `ValueError: shape must be an
non-negative integer...` where numpy returns a value, and `axis=-5` on a
2-D array leaked `IndexError: pop index out of range` instead of a
message naming the bounds.

`normalize_axis` is already imported in this module and every other
axis-taking function in it uses it: `concatenate`, `stack`, `roll`,
`diagonal`, `expand_dims` and `flip`. This was the only one left out,
which also matches the history: the reductions got this treatment in
#118/#119 and `diagonal` in #955, while `argmax`/`argmin` arrived later
in #614 and were never covered. The existing tests parametrize `axis`
over `[None, 0, 1, 2]`, so no negative axis was ever exercised.

The wording of the out-of-bounds `ValueError` is kept so that
`test_argmax_argmin_constraint` still passes, including the 0-D case.
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