8000 Quantity.__array_ufunc__ by mhvk · Pull Request #2583 · astropy/astropy · GitHub
[go: up one dir, main page]

Skip to content

Quantity.__array_ufunc__ #2583

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

Merged
merged 12 commits into from
Jun 18, 2017
Merged

Quantity.__array_ufunc__ #2583

merged 12 commits into from
Jun 18, 2017

Conversation

mhvk
Copy link
Contributor
@mhvk mhvk commented May 30, 2014

EDIT (28-Apr-2017): in the end, the numpy implementation is called __array_ufunc__.

Following issue #2492, here is an implementation of __numpy_ufunc__ that at least locally passes all the tests (and allows some previous xfails to be removed!). It also addresses #2483, so hopefully will help set us up for testing against numpy 1.9 in travis.

Notes:

  • this requires With quantity output, ensure non-quantity input is treated as dimensionless #2571.
  • Rather rough is the implementation of __numpy_ufunc__ with methods other than __call__ (e.g., accumulate, reduce). Right now, I only allow two-argument ufuncs that do not change the unit (this is in the new scales_and_result_unit function, which is in quantity_helper). Do comment if this is wrong! Or if it is true for only some of those methods.
  • I let overrides of some methods depend on the numpy version; let me know if this is done the right way.

@eteq eteq added this to the v0.4.0 milestone May 30, 2014
@astrofrog
Copy link
Member

@mhvk - thanks! Should this be 1.0.0 rather than 0.4.0? I will try and review this soon.

@mhvk mhvk modified the milestones: Future, v0.4.0 Jun 2, 2014
@mhvk
Copy link
Contributor Author
mhvk commented Jun 2, 2014

@astrofrog - I had not explicitly set a milestone -- as long as we do not care about numpy-1.9 for 0.4, I think we're fine. For now, I changed the milestone to "future".

@mdboom
Copy link
Contributor
mdboom commented Jun 3, 2014

@mhvk: Since you had asked in a mailing list thread: This PR shows no significant performance change in any of the existing benchmarks. We have only one benchmark that really exercises ufuncs, though:

def time_quantity_ufunc_sin():
    np.sin(np.arange(10000) * u.degree)

Is there a better benchmark we could use?

@mhvk
Copy link
Contributor Author
mhvk commented Jun 3, 2014

@mdboom - did you test with numpy-1.9? I get almost a factor two gain! (as expected if the sine calculation dominates and one removes one of the two calculations).

old:

In [1]: import astropy, astropy.units as u, numpy as np

In [2]: np.version.version, astropy.version.version
Out[2]: ('1.8.1', '0.4.dev8948')

In [3]: %timeit np.sin(np.arange(10000) * u.degree)
1000 loops, best of 3: 813 µs per loop

new:

In [1]: import astropy, astropy.units as u, numpy as np

In [2]: np.version.version, astropy.version.version
Out[2]: ('1.9.0.dev-9dd46ee', '0.4.dev8948')

In [3]: %timeit np.sin(np.arange(10000) * u.degree)
1000 loops, best of 3: 441 µs per loop

@mdboom
Copy link
Contributor
mdboom commented Jun 3, 2014

No I tested with 1.8 -- I should have read the PR to understand how much Numpy 1.9 matters... ;)

@astrofrog
Copy link
Member

@mhvk - could you check the test failures? It looks like some of them are legitimate? Given that Numpy 1.9 is close to release, I think we should at least make sure we have it ready in case we want to get it in 0.4 (and otherwise we can get it in 0.4.1)

@mhvk
Copy link
Contributor Author
mhvk commented Jun 14, 2014

@astrofrog - I have been adding some extra tests, but am currently observing and will not get to this until later this week; think we'll have to aim for 0.4.1, at which time we may also be setting up travis to test with 1.9.

@mhvk
Copy link
Contributor Author
mhvk commented Jun 16, 2014

@astrofrog - I realised that the tests I was writing were all for ufunc methods (np.add.reduce, etc.), which currently do not work anyway. So, I thought I'd better just try to get this PR to work as is; I think I've now done so, although there is a single failure that I do not understand. One issue: in order to merge this, we will need to have a numpy-1.9 test case on travis. Should this be set up already, or better wait for the official release? More generally, while it does seem to work for me, it would probably be good if you tested it as well...

@astrofrog
Copy link
Member

@mwcraig - do you think it would be easy to create a binstar package for Numpy 1.9.0b1 for Python 2.6, 2.7, 3.1, 3.2, and 3.3? (http://sourceforge.net/projects/numpy/files/NumPy/1.9.0b1/). One the final Numpy 1.9 is out we can remove those, but it would be great to have them for testing in the mean time. If you don't have time, I can look into it later this week.

@mwcraig
Copy link
Member
mwcraig commented Jun 17, 2014

@astrofrog -- I think so...will report back later today either way. If I don't get to it today I won't get to it until the end of the week.

@mwcraig
Copy link
Member
mwcraig commented Jun 17, 2014

@astrofrog -- done and uploaded for: 2.6, 2.7, 3.2, 3.3, 3.4 (no 3.1 -- that isn't one of the pythons currently checked on travis)

@astrofrog
Copy link
Member

@mwcraig - thanks!

@mhvk - can you add two entries for numpy 1.9.0b1 under the other numpy section in the travis file, one for python 2.7 and one for 3.4?

@mhvk
Copy link
Contributor Author
mhvk commented Jun 17, 2014

@mhvk - can you add two entries for numpy 1.9.0b1 under the other numpy section in the travis file, one for python 2.7 and one for 3.4?

Just to be sure I do not screw up, this is in .travis.yml in the main
directory, correct?

@mhvk
Copy link
Contributor Author
mhvk commented Jun 17, 2014

@astrofrog - never mind, I just went ahead and tried. What could possibly go wrong...

@mhvk
Copy link
Contributor Author
mhvk commented Jun 17, 2014

Well, travis now immediately breaks, without an error message. I'm guessing this is because it cannot find the numpy version. Is it indeed called 1.9.0b1? (Don't know how to check...)

.travis.yml Outdated
@@ -55,6 +55,11 @@ matrix:
env: NUMPY_VERSION=1.6 SETUP_CMD='test'
- python: 2.7
env: NUMPY_VERSION=1.5 SETUP_CMD='test'
# try bleeding edge numpy version
Copy link
Member

Choose a reason for hiding this comment

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

@mhvk - I think there is a space missing in front of #

Copy link
Member

Choose a reason for hiding this comment

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

@mhvk -- actually, I think the problem is that you have a tab instead of spaces in front of the #.

Copy link
Member

Choose a reason for hiding this comment

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

@mwcraig - good catch!

Copy link
Member

Choose a reason for hiding this comment

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

Turns out that when you view the file github automatically highlights tabs!

@mhvk
Copy link
Contributor Author
mhvk commented Jun 17, 2014

@astrofrog, @mwcraig - thanks, that does seem to have done it! I've gotten so used to emacs converting tabs to spaces automatically that I had forgotten it depends on the mode it is in; clearly, it thinks that for .yml files tabs are OK...

@astrofrog
Copy link
Member

Ugh, it looks like we are running into Numpy segfaults again. I thought @embray had fixed this upstream in Numpy (numpy/numpy#4494) and it looks like his fix did make it to 1.9.0b1, so this must be a new issue :(

@embray, since you've investigated this kind of issue before, any idea what is going on with the failures with Numpy 1.9.0b1? I am seeing those failures locally but I think not with the latest master, so I'm wondering whether something hasn't been backported?

@mhvk
Copy link
Contributor Author
mhvk commented Jun 19, 2014

I uploaded a new version that makes correct changes to get Angle and its subclasses working, and a hack to avoid the segfaults; this really needs a bug report to numpy, but I wanted tests to be able to proceed (and EarthLocation will change anyway). There is still another issue in Angle related to subclasses not being dealt with quite right (see numpy/numpy#4815) and one in Time because __array_priority__ does not seem to be respected any more. Plus one in nddata that I haven't had time to investigate. It's all a bit more messy than hoped...

@mhvk
Copy link
Contributor Author
mhvk commented Jul 9, 2014

Now also the segfault issue has been reported to numpy, making it three bug reports based on this PR:

At the moment, the first two are also the two remaining blockers for 1.9, and I fear the third will be one too. I guess the positive side of this is that we are helping improve numpy as well!

@astrofrog
Copy link
Member

@mhvk - thanks for identifying and reporting all these issues! On the plus side, if we weed out the bugs from 1.9, we won't need any version-specific workarounds.

@mhvk
Copy link
Contributor Author
mhvk commented Sep 12, 2014

@astrofrog - I'm trying to get back to implementing __numpy_ufunc__, but to actually test it on travis, it would need to have the development version of numpy. Would it be possible in principle to set up travis to just download and compile numpy? (Just a pointer would be helpful! I tried to understand how the travis setup works, but fear I'm rather clueless.)

@astrofrog
Copy link
Member

@mhvk - numpy 1.9 appears to now be in conda, so maybe the issue is moot :)

One was wrong in-place multiplication of an array with quantity,
the other was that __array_ufunc__ got units of Parameters, but
did not correctly apply these.
@mhvk mhvk force-pushed the quantity-numpy-ufunc branch from 0bca765 to 7a2b305 Compare June 17, 2017 22:26
@mhvk
Copy link
Contributor Author
mhvk commented Jun 17, 2017

Rebased, and fixed two regressions introduced by the modeling unit work -- a bug each in modeling and in my implementation. I think this is ready to go in....

Copy link
Member
@adrn adrn left a comment

Choose a reason for hiding this comment

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

I have two tiny nitpick comments, otherwise this is ready to merge when you add a changelog entry.

if unit is None:
raise TypeError("Cannot store non-quantity output{0} in {1} "
"instance".format(
("from {0} function".format(function.__name__)
Copy link
Member

Choose a reason for hiding this comment

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

space before "from"?

raise UnitTypeError(
"Cannot store output with unit '{0}'{1} "
"in {2} instance. Use {3} instance instead."
.format(unit, ("from {0} function".format(function.__name__)
10000 Copy link
Member

Choose a reason for hiding this comment

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

again, space before "from"?

@mhvk
Copy link
Contributor Author
mhvk commented Jun 18, 2017

OK, fixed those. Since that's all untested strings, skipping CI and merging...

@mhvk mhvk merged commit 5b18ebc into astropy:master Jun 18, 2017
@mhvk mhvk deleted the quantity-numpy-ufunc branch June 18, 2017 13:19
@pllim
Copy link
Member
pllim commented Jun 19, 2017

After 3 years! 🎆

@mhvk
Copy link
Contributor Author
mhvk commented Jun 19, 2017

Indeed, it still needed a lot of work and to-and-fro on the numpy side....

@taldcroft
Copy link
Member

Congratulations @mhvk and thank you so much for your persistence and numpy cross-project work! 🎉

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.

10 participants
0