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
Update yt for Numpy 1.13 #1422
Update yt for Numpy 1.13 #1422
Conversation
This is a pretty cool thing to do -- thank you for taking this on, @ngoldbaum ! |
I will try to look this over this week. |
# numpy 1.13 or newer | ||
from numpy import positive, divmod as divmod_, isnat, heaviside | ||
except ImportError: | ||
positive, divmod_, isnat, heaviside = (None,)*4 |
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.
Missing import statement?
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.
No, I'm setting these symbols to None
since they're not available in older versions of Numpy
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.
Ah I see now. Figured I was not seeing something :-)
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.
One plus of me reviewing PRs is that I learn something new about python (even seemingly trivial things) every time
yt/units/yt_array.py
Outdated
>>> np.ones_like(a) | ||
YTArray([1, 1, 1, 1, 1, 1, 1, 1]) g/cm**3 | ||
>>> np.abs(a) | ||
YTArray([8, 8, 8, 8, 8, 8, 8, 8]) g/cm**3 |
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.
Oops, this is wrong
Update yt for Numpy 1.13
This updates yt for changes in Numpy 1.13.
Note
Github isn't rendering the diff for yt_array.py, you need to manually expand it if you want to see the bulk of the changes in this PR.
You may be surprised by the size of the changes here - this is mostly to support a new feature in Numpy 1.13:
__array_ufunc__
. See this page for more details.Briefly, defining
__array_ufunc__
allows us to skip much of the boilerplate in the definition ofYTArray
, since almost all array operations will now be handled by__array_ufunc__
. This reduces overhead for many operations and also makes the overall implementation simpler.Unfortunately, we still need to support older numpy releases. I've decided that the best way to do this is to make a large chunk of the body of
YTArray
defined conditionally on numpy version. This means that this PR has a large net increase in code length, since__array_wrap__
and__array_ufunc__
have similar implementations. I've tried my best to refactor completely identical code in these two functions into private helper functions.You'll also see in the tests that the answers for a limited subset of the
yt.units
tests now depend on numpy version. In my opinion these are all beneficial changes, but if anyone has questions about any specific change let me know. The most significant change is that yt will no longer raiseYTUfuncUnitError
. That means it's now possible to do something like:This will not work correctly on older NumPy versions, so we won't be able to take advantage of this improvement in yt's internals for a few years.