BUG: fix compatibility with numpy 2.0 for ndarray suclasses overriding ndarray.sort and ndarray.argsort#15929
Conversation
|
Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.
|
|
👋 Thank you for your draft pull request! Do you know that you can use |
627c294 to
7e5678a
Compare
7e5678a to
d55d0cf
Compare
astropy/units/quantity.py
Outdated
| # ensure we do not return indices as quantities | ||
| def argsort(self, axis=-1, kind="quicksort", order=None): | ||
| return self.view(np.ndarray).argsort(axis=axis, kind=kind, order=order) | ||
| def argsort(self, axis=-1, kind=None, order=None, *, stable=False): |
There was a problem hiding this comment.
For previous cases of signature changes, we did
if NUMPY_LT_2_0:
def argsort(old-signature):
...
else:
def argsort(new-signature):
...
I think that is preferable, since it means the signature is correct for the given numpy version, and there is no slowdown.
I guess an alternative would be to just pass *args, **kwargs? Especially given that "quicksort" is the default anyway.
There was a problem hiding this comment.
Oh I didn't realise that kind=None implied "quicksort". In that case, might as well 100% stick to numpy's signature.
93ed82c to
56b8f0f
Compare
|
Is this the only one left that would make devdeps green again? 🤞 Thanks for all your help! |
Yes and no: it is the only one left so far, but don't get your hopes too high: there are still a couple issues with numpy dev that require we don't have solutions to at the moment. |
|
In that case, are you able to rebase and at least fix the argsort stuff in this PR? Less traceback in the log would ease the rest of the fix. Thanks! |
56b8f0f to
42d9b57
Compare
|
rebased ! |
|
Now I am confused about the CircleCI jobs. The jobs here are green but they ran with pluggy 1.4.0 too. Are the failures not appearing all the time? 🤯 |
|
But back to the devdeps job, can you please confirm that the remaining 18 failures in https://github.com/astropy/astropy/actions/runs/7643754025/job/20826541495?pr=15929 are unrelated? Thanks! |
|
I don't get it either. My immediate instinct is to assume that pytest itself isn't fully deterministic 🤷🏻♂️ ? |
they are. I have them all on my radar:
|
|
Great! Hopefully @mhvk can review soon though the diff LGTM at a glance. Thanks! |
mhvk
left a comment
There was a problem hiding this comment.
@neutrinoceros - still some questions/suggestions for simplification...
astropy/units/quantity.py
Outdated
| else: | ||
|
|
||
| def argsort(self, axis=-1, kind=None, order=None, *, stable=False): | ||
| kwargs = dict(axis=axis, order=order) |
There was a problem hiding this comment.
Do we actually need to duplicate this check on the arguments? Why are we not just sending everything upstream?
There was a problem hiding this comment.
I think you're right, it's not needed now that I've changed the default value for kind ! thanks !
astropy/utils/masked/core.py
Outdated
| second, etc. A single field can be specified as a string, and not | ||
| all fields need be specified, but unspecified fields will still be | ||
| used, in dtype order, to break ties. | ||
| stable: bool, keyword-only |
There was a problem hiding this comment.
Since it is not actually used, in this case I think we can get away with just defining argsort for all numpy and just not passing on stable. Then, here write
Only for compatibility with np.sort (for numpy >= 2.0). Ignored.
42d9b57 to
930e0dd
Compare
|
with your suggestions in, the diff is now much smaller. Thanks @mhvk ! |
930e0dd to
5ddb2ae
Compare
mhvk
left a comment
There was a problem hiding this comment.
This is nice! Some small comments, partially just to improve it slightly more (with no change in API, so can still backport).
astropy/utils/masked/core.py
Outdated
| all fields need be specified, but unspecified fields will still be | ||
| used, in dtype order, to break ties. | ||
| stable: bool, keyword-only | ||
| Only for compatibility with np.sort. Ignored. |
There was a problem hiding this comment.
Maybe phrase just like for kind, i.e.,
stable: bool, keyword-only, ignored
Sort stability. Present only to allow subclasses to work.
There was a problem hiding this comment.
yeah that's more consistent with how kind is documented !
astropy/utils/masked/core.py
Outdated
|
|
||
| Notes | ||
| ----- | ||
| stable is only used with numpy 2.0 and newer, and is ignored otherwise. |
There was a problem hiding this comment.
Yes, good idea to have a note. But kind is ignored too... How about,
Masked items will be sorted to the end. The implementation
is via `np.lexsort` and thus ignores the ``kind`` and ``stable`` arguments;
they are present only so that subclasses can pass them on.
| # TODO: probably possible to do this faster than going through argsort! | ||
| indices = self.argsort(axis, kind=kind, order=order) | ||
| argsort_kwargs = dict(kind=kind, order=order) | ||
| if not NUMPY_LT_2_0: |
There was a problem hiding this comment.
By setting the default to np._NoValue, we can change this to
if stable is not np._NoValue:
argsort_kwargs["stable"] = stable
I do like that you pass them on, since in principle a subclass could override argsort and do something with it.
…g ndarray.sort and ndarray.argsort
5ddb2ae to
ef5748c
Compare
mhvk
left a comment
There was a problem hiding this comment.
OK, let's get it in! Thanks, @neutrinoceros!
astropy/utils/masked/core.py
Outdated
| Notes | ||
| ----- | ||
| Masked items will be sorted to the end. The implementation | ||
| is via `np.lexsort` and thus ignores the ``kind`` and ``stable`` arguments; |
There was a problem hiding this comment.
Darn, this should have been numpy.lexsort for readthedocs to be happy...
There was a problem hiding this comment.
Since it must be getting late in Europe, I took the liberty to fix this directly on this branch. Hope you all don't mind!
…ndarray suclasses overriding ndarray.sort and ndarray.argsort
|
Down to 2 failures! 🚀 |
|
Let's try again @meeseeksdev backport to v6.0.x |
|
Could not push to auto-backport-of-pr-15929-on-v6.0.x due to error, aborting. |
|
C'mone bot. Don't be mad at me. @meeseeksdev backport to v6.0.x |
|
Could not push to auto-backport-of-pr-15929-on-v6.0.x due to error, aborting. |
…t_stable BUG: fix compatibility with numpy 2.0 for ndarray suclasses overriding ndarray.sort and ndarray.argsort
Backport PR #15929 on branch v6.0.x (BUG: fix compatibility with numpy 2.0 for ndarray suclasses overriding ndarray.sort and ndarray.argsort)
|
Thanks, @pllim! |
Description
This pull request is to address one of the three failures reported in #15926
This is currently based atop #15925xref numpy/numpy#25437