-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
BUG np.where half-initializes subclass of output #5095
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
hacking it in using the input object with the hightest priority should fix this and the testsuite succeeds. |
branch with the changes: @mhvk can you try if that branch fixes your issues? |
on second though |
@juliantaylor - I agree that it would remain unsafe for subclasses. I wondered whether my suggestion was much slower, but it turns out it is not too bad, at least for large arrays:
For smaller arrays, it is worse. E.g., for size=100, one becomes dominated by all the initialisation: 13.5 and 96.7 micro-sec, respectively. |
Note that implementing where() via indexing requires a special case for 0d where(True, 5, 10) works, but result[condition] = ... will fail because of how 0d indexing works. |
|
|
the C implementation cannot preserve subclass attributes, this needs a python wrapper using indexing for subclasses. closes numpygh-5095
@juliantaylor - I saw in #5105 that you hope to produce a version that handles subclasses correctly. Let me know if I can help in any way. Also, shall I raise a new issue reminding us of this? |
too bad
I wonder if thats fixable |
p.s. This is indeed why I thought of my example. Note that |
oh very nice, I'll have a look at supporting subclasses when its merged |
Sorry for resurrecting an old thread, but I finally tracked down the source of an obscure bug in one of our production systems, and I think it relates to this. The line |
I've put a preliminary |
@charris - be sure to reopen the issue, otherwise it probably won't get looked at at all! (@juliantaylor -- a proper solution would still be very welcome!) |
Fwiw would be comfortable with not considering this a 1.10 blocker and
|
@mhvk's proposed solution is interesting, but it won't work for all duck-array types:
The problem is that some of the duck-array types for which |
I should also point out that my code example used to work because the condition was a masked array. Depending on the type of |
...and looking over my company's internal bug tracker, this may be the source of another bug that was customer-facing, causing our shipped netCDF files to have very negative values in places that should have been masked, and our contours to look a bit silly. We never figured out the exact cause of the issue, but I am now going to see if an np.where() was used. |
Perhaps we should raise an error until the subclass issue is fixed. @juliantaylor Have you had any time to pursue your solution? |
I think I would be more comfortable with a warning. The sneaky nature of On Wed, Jul 29, 2015 at 4:00 PM, Charles Harris notifications@github.com
|
So this is still up in the air. Pushing off to 1.12. |
Closing since one can now override |
In numpy 1.9, the behaviour of
np.where(condition, first, second)
changed, in that the output no longer always is anndarray
instance but an instance of the subclass offirst
. However,__array_finalize__(self, obj)
is called withobj=None
, and hence the subclass can not properly initialize itself (according to the documentation [1],None
is reserved for the case where a new instance is created via__new__
). This change in behaviour has caused some problems (e.g., astropy/astropy#2958, also discussion in astropy/astropy#1274).I should add that I do not necessarily believe the old behaviour was preferable, but it would be good if the current behaviour is documented.
Going forward, an option which I think would be ideal (though not necessarily practical...), is if
np.where
would do the equivalent ofIn this way, subclasses can check if the second argument is consistent with the first (e.g., for our
Quantity
class, check the units and possibly convert). This might be done most easily if, somehow,np.where
could be turned into aufunc
and passed through the__numpy_ufunc__
mechanism.[1] http://docs.scipy.org/doc/numpy/user/basics.subclassing.html
The text was updated successfully, but these errors were encountered: