Skip to content
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

Merged
merged 7 commits into from Jun 20, 2017
Merged

Conversation

ngoldbaum
Copy link
Member

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 of YTArray, 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 raise YTUfuncUnitError. That means it's now possible to do something like:

from yt.units import km, m

np.add([3, 4]*km, [5, 6]*m)

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.

@ngoldbaum ngoldbaum requested a review from jzuhone May 24, 2017 02:14
@matthewturk
Copy link
Member

This is a pretty cool thing to do -- thank you for taking this on, @ngoldbaum !

@jzuhone
Copy link
Contributor

jzuhone commented May 29, 2017

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
Copy link
Member

@lindsayad lindsayad Jun 8, 2017

Choose a reason for hiding this comment

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

Missing import statement?

Copy link
Member Author

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

Copy link
Member

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 :-)

Copy link
Member

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

>>> 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
Copy link
Member Author

Choose a reason for hiding this comment

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

Oops, this is wrong

@matthewturk matthewturk merged commit fdd6ad4 into yt-project:master Jun 20, 2017
matthewturk added a commit to matthewturk/yt that referenced this pull request Apr 17, 2018
@ngoldbaum ngoldbaum deleted the numpy-1.13 branch December 11, 2018 15:34
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.

None yet

5 participants