-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Use __array_function__ to make most numpy functions work on Quantity #8808
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
Conversation
Codecov Report
@@ Coverage Diff @@
## master #8808 +/- ##
==========================================
- Coverage 86.99% 86.66% -0.33%
==========================================
Files 400 401 +1
Lines 59469 59803 +334
Branches 1100 1100
==========================================
+ Hits 51736 51830 +94
- Misses 7092 7332 +240
Partials 641 641
Continue to review full report at Codecov.
|
f083b61 to
80bd52e
Compare
adrn
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 In general, this looks great - thanks @mhvk for the hard work! I did a preliminary review and didn't catch anything major, but will give this a more detailed look later today.
| np.save, np.savez, np.savetxt, np.savez_compressed, | ||
| # Polynomials | ||
| np.poly, np.polyadd, np.polyder, np.polydiv, np.polyfit, np.polyint, | ||
| np.polymul, np.polysub, np.polyval, np.roots, np.vander, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bummer! Conceptually, it would be nice to have support for these -- e.g., polyfit(x, y) could return the coefficients with units (y.unit, y.unit/x.unit, y.unit/x.unit**2, ...etc) -- but many of these functions would require support for multiple / broadcasted units within the same array object first...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, and similarly for cov below.
|
@adrn - thanks for the initial look! I now also updated the docs, and think this is getting there. The main issue that I see with this is that it breaks plotting, which means people either have to be very careful with using Note that in numpy 1.17 at least, it is still possible to turn off the whole overrides by setting an environment variable. Possibly, I should document that... |
42374f5 to
35aabe6
Compare
|
OK, plotting is solved (see #8818), though it is still the case that with this PR, it will become necessary to always use |
|
p.s. One note: I am not yet including functions inside other modules (such as |
|
Very much off topic question, but shall we also drop numpy versions for v4.0? E.g. will it be 1.14+ based on the "last 4 releases" rule? |
|
For a LTS release, it arguably makes sense to insist on a relatively recent numpy version. It might make some sense to exclude 1.14 as well, given that we have work-arounds even for different versions of 1.14; for reference, 1.15 will be a year old by then. But overall, the work-arounds are all rather small - the only thing that might make a big difference is if we were to require >=1.16, which simplifies Anyway, probably best discussed elsewhere! |
1.5 year old. I'll open a new issue, but also put on the tentative agenda of the next dev telecon. |
adrn
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just had a much closer look at this PR, and it still looks to be in great shape! I had a few minor inline comments, but I think they will be very easy to address or discuss. I want to note to others that I did a fairly close read through the tests and didn't see any obvious holes, but this is big enough that it could use another set of eyes if anyone has time.
|
On the larger question of how to review this monster PR: I think we'll almost certainly need to get some experience with this. This is partially why I think it is good to have it in just after a release, so that our affiliated packages can play around with it well ahead of 4.0 (of course, numpy 1.17 still has to be released as well). |
|
@mhvk - there are a few packages that are testing against astropy-dev. Afaik less against numpy-dev, but we also send out an e-mail to astropy-dev that they might want to consider adding that combination to their CI, preferable as a job that also runs from cron. |
Creates a list of all functions that can be overridden with __array_function__ and breaks them up into groups. Add a few tests for coverage, many of which are currently failing.
Be more specific about numpy 1.17
|
I suppose this will also close #1273 |
Yes! 🎆 (and added to top so it auto-closes) |
|
Would this also mean addressing #6897, or just laying ground works for it? |
|
Would this also address #8610? |
|
This will also fix #5842 |
|
@mhvk - I don't have the time or courage to give this full review, but I skimmed the crux of the new functionality and it looks great. Just want to congratulate you and numpy for bringing the whole numpy duck array concept to fruition. That's huge! And I do hope that "duck array" sticks, it's much better than any of the alternatives in the NEP. |
|
I agree that this will be great to have! One big-picture question I have - is it conceivable that in future we could migrate to no longer subclassing ndarray as a result of this? |
I don't really see any downside to this? For time_support it would be trickier since it has arguments, but I can't think of any reason to not enable quantity_support by default. |
|
Overall this looks great, and I like the way it's implemented. I reviewed the main |
|
@bsipocz - yes, fixes #5842; #8610 is really follow-up. I'll merge it with the other follow-up issue. @astrofrog, @taldcroft - I agree with the sentiment of merging this soon so we can test it out for a couple of months; the review of |
|
@astrofrog - yes, in principle with the combination of Also, within numpy, the discussion about introducing a new |
|
@taldcroft - my personal preference is "array mimic" as it doesn't require knowing the "if it quacks like a duck, ..." etc. But I've gotten little traction. 😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm happy with this! It sounds like a few of us have taken a close look at the __array_function__ implementation, and at least a first look at the tests. I agree that merging this soon would be great, as subtle issues will hopefully get shaken our during field testing...Given that, I'd be happy to see this merged now!
As you wish. |
|
Thank you @mhvk, this is such a great improvement! 🎉 🎆 |
With `numpy` < 1.17 or `astropy` < 4.0 calling `np.concatenate()` on `Quantity` instances produced an output without a unit. Two workarounds have persisted in the code until now, despite having been obsolete for quite some time. See also pull request astropy#8808 and 82ffaad that updated the relevant documentation.
It used to be the case that the output of `np.concatenate()` called with `Quantity` instances lost the unit, but the workarounds have not been needed since support for `numpy` version 1.16 was dropped. See also pull request astropy#8808 and 82ffaad that updated the relevant documentation.
It used to be the case that the output of `np.concatenate()` called with `Quantity` instances lost the unit, but the workarounds have not been needed since support for `numpy` version 1.16 was dropped. See also pull request astropy#8808 and 82ffaad that updated the relevant documentation.
It used to be the case that the output of `np.concatenate()` called with `Quantity` instances lost the unit, but the workarounds have not been needed since support for `numpy` version 1.16 was dropped. See also pull request astropy#8808 and 82ffaad that updated the relevant documentation.
In numpy 1.17, a new
__array_function__protocol is being introduced that allows one to overwrite every function in numpy. This PR applies it toQuantity, so that things likenp.contatenate(q1, q2)"just work"!I think it is mostly complete, though (EDIT)
I need to be a bit smarter about how to not support things like polynomials, financial functions, etc.functions inlinalg, etc., are not yet included - those I'd like to do separately.Have a look especially at
test_quantity_non_ufuncsto see what now works.One thing I've noticed so far is that this breaks straight plotting with quantities with different units for x and y (at least
plt.scatter) since that internally tries to stick the 2 axes together. We may need to turn onquantity_supportby default...(EDIT, solved)
There are also a number of failures inmodels- I'm hoping those are just smoking out minor bugs in the implementation, but haven't looked in detail yet.fixes #1273, fixes #5842