-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Inconsistent behavior for ufuncs in Numpy v1.10.X #7122
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
Comments
Looks like 3c6b6ba is the problem commit. |
@mhvk Thoughts? |
This is getting a little deep for me at this point, though I will try to help if I can. While reverting to v1.9.3 solves my current issues, it seems to me that the behavior is incorrect even in the 1.9.3 release. Calling One issue compounded the other leaving me very confused I think. |
@jsolbrig Binary ops are handled a differently from ufuncs as they interact with Python, i.e., Python makes the call to the subtract method. The proper way to handle that is a current topic of |
@charris Wouldn't that mean that the documentation is incorrect, then? The subtraction operator still calls a ufunc (albeit an overridden one), but array_prepare and array_wrap are not called as specified in the documentation. I would expect the behavior of the operator and the function to be exactly the same from the standpoint of subclassing from MaskedArray. |
The thing is that ufunc wrapping works on the EDIT: Of course you can pass them into the ufuncs, but their operators don't, they have their own API in this regard (and it basically means that subclasses trying to do a lot of things need to do even more magic probably). But I think subclasses were somewhat anticipated, so I am not quite sure. |
Okay, yeah, that makes sense. It does make me wonder, though, if array_prepare and array_wrap should be called inside the call method of _MaskedBinaryOperation and 8000 other similar classes in order to keep consistent behavior for subclassing and make it easier for others in the future. |
Sure, maybe? It's basically impossible to fix ndarray subclassing to work well, and then MaskedArray has a giant pile of hacks to make it kinda-sorta-work as an ndarray subclass, and then you're trying to subclass that. So... if there's an additional hack that will make your particular use case work, then might as well try it; it's unlikely to make things worse :-). (And the accidental regression versus 1.9 is certainly not OK; sorry about that.) As a piece of general advice though I'd strongly recommend trying to find some way to solve your problem that doesn't involve subclassing any of these things :-) |
Yep, I've basically come to the same conclusion that subclassing MaskedArray is probably the wrong solution. I'm going to have to think about how best to go about it, though. Using MaskedArray the way I have been has been so convenient other than this problem. I now have a workaround for this problem and everything seems to work the way I would hope, but it's a little worrisome that it was so difficult to achieve. After looking at it a bit more, though, it does appear that if called in the appropriate location in _MaskedBinaryOperation, array_prepare and array_wrap would actually work the way they are supposed to. Do you think it would be worth me looking into how to implement the numpy-consistent behavior? |
My personal policy is to leave np.ma maintenance to those who care enough to fix things. So really it's up to you to decide whether it's a worthwhile use of your time :-). We would probably merge a pull request that fixed the regression, at least. |
@jsolbrig - just as a background: the PR that broke things for you tried to fix the case where the |
Just so it doesn't get lost, in the thread on numpy-dev, @seberg wrote:
|
@mhvk Ping... |
Overwhelmed by my day job right now, in particular graduate admission, but a break is at least in sight... Hopefully still this week. |
@mhvk Ping! I'd like to get another beta out this weekend, what does your schedule look like? |
The break took longer in coming than expected, but now do have some time. Hopefully this weekend or Monday. |
They always do ;) |
@jsolbrig - as noted by @seberg and @njsmith, in general one cannot expect a |
@mhvk - Thanks for looking at this. In my time looking at this I had started wondering why |
@mhvk - Thanks for pointing out that thread. I'll work through it and start participating in the conversation at minimum. Hopefully I can help create a decent solution, too. Finding a good solution for this issue would be very helpful for one of my current projects and I wouldn't mind spending some time on it extra time presents i 8000 tself. |
That's a very long one -- though in the mailing list we had concluded that, really, to make progress on |
I'll see what I can do. It would be nice to get involved in one of the On Mon, Feb 22, 2016 at 2:30 PM Marten van Kerkwijk <
|
That's how it started for me too... And |
I am copying this from a discussion on the mailing list.
I am attempting to subclass numpy.ma.MaskedArray. I am currently using Python v2.7.10. The problem discussed below does not occur in Numpy v1.9.2, but does occur in all versions of Numpy v1.10.x including v1.10.4.
Using mathematical operators on my subclass behaves differently than using the analogous ufunc. When using the ufunc directly (e.g. np.subtract(arr1,
arr2)), array_prepare, array_finalize, and array_wrap are all called as expected, however, when using the symbolic operator (e.g. arr1-arr2) only array_finalize is called. As a consequence, I lose any information stored in arr._optinfo when a mathematical operator is used.
Here is a code snippet that illustrates the issue.
If run, the output looks like this:
Currently I have simply downgraded to 1.9.2 to solve the problem for myself, but have been having difficulty figuring out where the difference lies between 1.9.2 and 1.10.0.
The text was updated successfully, but these errors were encountered: