Skip to content

TST: add test cases + implementations for array functions (NEP 18) new in numpy 2.0#15691

Merged
pllim merged 1 commit into
astropy:mainfrom
neutrinoceros:units/new_array_functions
Dec 11, 2023
Merged

TST: add test cases + implementations for array functions (NEP 18) new in numpy 2.0#15691
pllim merged 1 commit into
astropy:mainfrom
neutrinoceros:units/new_array_functions

Conversation

@neutrinoceros

Copy link
Copy Markdown
Contributor

Description

Will fix #15690

This isn't finished yet but I'm opening as a draft 10min ahead of the dev call. I'll come back to this later today or tommorow.

Nothing it polished yet but early feedback is welcome nonetheless.

  • By checking this box, the PR author has requested that maintainers do NOT use the "Squash and Merge" button. Maintainers should respect this when possible; however, the final decision is at the discretion of the maintainer that merges the PR.

@neutrinoceros neutrinoceros changed the title TST: add test cases for array functions (NEP 18) new in numpy 2.0 (un… TST: add test cases + implementations for array functions (NEP 18) new in numpy 2.0 Dec 7, 2023
@github-actions github-actions Bot added the units label Dec 7, 2023
@github-actions

github-actions Bot commented Dec 7, 2023

Copy link
Copy Markdown
Contributor

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.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see instructions for rebase and squash.
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the "Extra CI" label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • Is a milestone set? Milestone must be set but we cannot check for it on Actions; do not let the green checkmark fool you.
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate "backport-X.Y.x" label(s) before merge.

@github-actions

github-actions Bot commented Dec 7, 2023

Copy link
Copy Markdown
Contributor

👋 Thank you for your draft pull request! Do you know that you can use [ci skip] or [skip ci] in your commit messages to skip running continuous integration tests until you are ready?

SUBCLASS_SAFE_FUNCTIONS |= {np.linalg.cross}

# doesn't make much sense with quantites ?
SUBCLASS_SAFE_FUNCTIONS |= {np.linalg.svdvals}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe note that It works because it just calls svd internally, which transfers the unit (quantities are fine I think)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Note that I actually didn't add a test yet, because the reference implementation lives in SciPy so it's a little bit more involved. But I'm working on it, and I'll add a comment at the same time, thanks for your suggestion !

@neutrinoceros

Copy link
Copy Markdown
Contributor Author

@mhvk @pllim I'm hesitant to call this a bug fix or a feature, where do you guys think I should add a changelog entry, if any ?

@pllim

pllim commented Dec 7, 2023

Copy link
Copy Markdown
Member

This is more like future-proofing maintenance. We usually do not add change log for this. Thanks!

@mhvk

mhvk commented Dec 7, 2023

Copy link
Copy Markdown
Contributor

@neutrinoceros - thanks so much for doing this. I'm in a meeting the rest of the week, but probably can find some time for review. Is that useful already, or are you still making changes?

@neutrinoceros

Copy link
Copy Markdown
Contributor Author

@mhvk I'd like to have allowed_failures go green before I mark this as "open for review", and I'll try to accomplish that tomorrow, but feel free to review at any point ! What I have already is unlikely to change again before your feedback :)

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

OK, had a look now. Quite a few comments, but mostly small.

Comment thread astropy/units/quantity_helper/function_helpers.py

@function_helper(module=np.linalg)
def outer(x1, x2, /, *args, **kwargs):
return (x1, x2) + args, kwargs, x1.unit * x2.unit, None

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This won't work, since only one of x1, x2 has to be a Quantity, the other can be a regular array. I'd just copy the relevant parts of np.outer (annoyingly, they have different signature, so we cannot just add to helps).

Note that you should not add the *args, **kwargs here since the numpy equivalent does not take any further arguments.

Aside: a pity the implementation does asarray rather than asanyarray since with the latter this would just have worked... See numpy/numpy#25101 (comment)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Note that you should not add the *args, **kwargs here since the numpy equivalent does not take any further arguments.

The idea is to delegate any unknown arguments to the numpy function that gets called later, since it might gain new arguments in the future. See my other comment. I can still remove those if you're not convinced this has value.

Comment thread astropy/units/quantity_helper/function_helpers.py Outdated
Comment thread astropy/units/quantity_helper/function_helpers.py Outdated
Comment thread astropy/units/quantity_helper/function_helpers.py Outdated
Comment thread astropy/units/tests/test_quantity_non_ufuncs.py Outdated
Comment thread astropy/units/tests/test_quantity_non_ufuncs.py Outdated
Comment thread astropy/units/tests/test_quantity_non_ufuncs.py Outdated
Comment thread astropy/units/tests/test_quantity_non_ufuncs.py Outdated
Comment thread astropy/units/tests/test_quantity_non_ufuncs.py Outdated
@neutrinoceros

Copy link
Copy Markdown
Contributor Author

@mhvk A good fraction of your comments are about the need (or lack thereof) for *args + **kwargs. I argued that it's probably best to keep them for forward compatibility, but I'll wait on your reply to decide what to do about it. In the mean time, I'll experiment with the simplifications you hinted to ! Thank you for your early review !

@dhomeier

dhomeier commented Dec 8, 2023

Copy link
Copy Markdown
Contributor

Haven't gone into the details, but I could verify the fixes are working locally. As of today (8 December) there are 3 more ufuncs added though, np.astype, np.linalg.matmul, np.linalg.tensordot...

@neutrinoceros

Copy link
Copy Markdown
Contributor Author

Thanks for catching those, surely they'll make the way into nightlies soon. I've added the relevant PRs to the issue's description.
I'll switch this PR to draft while I'm working on these too.

@neutrinoceros neutrinoceros marked this pull request as draft December 8, 2023 20:06
@neutrinoceros

Copy link
Copy Markdown
Contributor Author

Ok I think I covered everything @dhomeier listed. Opening for review again !

@dhomeier

dhomeier commented Dec 8, 2023

Copy link
Copy Markdown
Contributor

2.0.0.dev0+git20231206.6ef3089

Obviously updated more frequently than scientific-python-nightly-wheels, but no tensordot yet; perhaps we have to wait till next week.

@pllim

pllim commented Dec 8, 2023

Copy link
Copy Markdown
Member

Looks like it should update over the weekend. We can wait.

https://github.com/numpy/numpy/blob/a5b67bbefbf216d325d713f5610bc46d665ff088/.github/workflows/wheels.yml#L24

@dhomeier

dhomeier commented Dec 8, 2023

Copy link
Copy Markdown
Contributor

But with my git+https://github.com/numpy/numpy.git test_quantity_non_ufuncs.py is now passing!

@mhvk

mhvk commented Dec 9, 2023

Copy link
Copy Markdown
Contributor

@neutrinoceros - I think we should add *args, **kwargs only if there are actual arguments to catch. A generic problem is that unless numpy makes things keyword-only, we actually need both, which is not possible if we also have arguments with defaults. Anyway, my sense is generally to try to just mimic the signature.

More concretely, let's just follow precedent in this PR; I think I might quite easily be convinced that it is better to allow more, but best not done in a fixup PR...

@neutrinoceros

neutrinoceros commented Dec 9, 2023

Copy link
Copy Markdown
Contributor Author

More concretely, let's just follow precedent in this PR; I think I might quite easily be convinced that it is better to allow more, but best not done in a fixup PR...

Gotcha. I'll open another issue specifically to discuss this matter. Thank you !

edit: here it is #15703

Comment thread astropy/units/quantity_helper/function_helpers.py Outdated

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks! One request to reverse to what you had, one potential bug left...

def outer(x1, x2, /):
# maybe this one can be marked as subclass-safe in the near future ?
# see https://github.com/numpy/numpy/pull/25101#discussion_r1419879122
return (x1, x2), {}, x1.unit * x2.unit, None

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for adding the comment. Note that this still will break if either x1 or x2 is not a Quantity (which is allowed). You'll sadly need to follow the logic in outer.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you, indeed I forgot to follow up on your first comment. Commit incoming !

Comment thread astropy/units/tests/test_quantity_non_ufuncs.py
@neutrinoceros neutrinoceros force-pushed the units/new_array_functions branch from 010d160 to 4e57e38 Compare December 11, 2023 08:51
@neutrinoceros

Copy link
Copy Markdown
Contributor Author

rebased, but devdeps CI is still red. I'm not sure whether numpy nightlies are now caught up, so I'm thinking this should wait a little bit longer.

@dhomeier

Copy link
Copy Markdown
Contributor

No, it's at git20231208.a5b67bb now, which should have numpy/numpy#25086 merged.
matmul and tensordot are passing, but there is an additional failure for astype in utils/masked/tests/test_function_helpers.py:

    def test_basic_testing_completeness():
>       assert all_wrapped == (tested_functions | IGNORED_FUNCTIONS | UNSUPPORTED_FUNCTIONS)
E       AssertionError: assert {<function ma...338a130>, ...} == {<function ma...338a130>, ...}
E         Extra items in the left set:
E         <function astype at 0x1033945f0>

@neutrinoceros

Copy link
Copy Markdown
Contributor Author

Thanks for having a look @dhomeier, I just fixed this one locally so hopefully this is about ready now🤞🏻

@mhvk mhvk marked this pull request as ready for review December 11, 2023 19:03

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks all good now! Approving but not yet merging, since the PR was still marked as draft.

@neutrinoceros neutrinoceros force-pushed the units/new_array_functions branch from f37349d to bb3bef3 Compare December 11, 2023 20:34
@neutrinoceros

Copy link
Copy Markdown
Contributor Author

Yes I just didn't wan't to re-open before I saw CI gone green ! I squashed and rebased, now feel free to merge away !

@pllim pllim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just a small typo. Otherwise LGTM and I am so happy devdeps is green again. Thanks!

Comment thread astropy/units/quantity_helper/function_helpers.py Outdated
…type, unique_all, unique_counts, unique_inverse, unique_values, linalg.cross, linalg.outer, linalg.svdvals, linalg.tensordot, linalg.matmul)
@neutrinoceros neutrinoceros force-pushed the units/new_array_functions branch from 6b6a254 to e201579 Compare December 11, 2023 21:09
@pllim pllim merged commit 28e593f into astropy:main Dec 11, 2023
@pllim

pllim commented Dec 11, 2023

Copy link
Copy Markdown
Member

Thank you!

meeseeksmachine pushed a commit to meeseeksmachine/astropy that referenced this pull request Dec 11, 2023
@neutrinoceros neutrinoceros deleted the units/new_array_functions branch December 11, 2023 22:25
pllim added a commit that referenced this pull request Dec 11, 2023
…691-on-v6.0.x

Backport PR #15691 on branch v6.0.x (TST: add test cases + implementations for array functions (NEP 18) new in numpy 2.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TST: astropy.units' tests fail against numpy 2.0 dev

4 participants