You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: v3.5.2 causing plot to crash when plotting object with __array__ method #22973
In astropy/astropy#13209 a potential bug with v3.5.2 has been found wherein a previously working (e.g. in v3.5.1) plot of one of kernel objects now fails. This failure seems to stem from plot no-longer coercing the the kernel object correctly. This seems to be related to the changes in #22141. In any case we have an object with a valid __array__ method which was previously plotting correctly, but now produces a crash.
OK, sorry about that - its pretty hard to track all the ways we can coerce to numpy, and we are working hard to consolidate them. Can astropy make a mocked up class for us that we can use for testing that should work? Currently looks like we are supporting to_numpy and then calling np.atleast_1d for some cases, but not doing it always.
Its pretty easy to add a check for __array__ to the fallbacks. I think it just becomes an issue of what takes precedence if an object has multiple array accessors. My knee jerk would be:
to_numpy > values > __array__, but I'm not sure if there is a proper convention here.
@jklymak - __array__ is the standard protocol for allowing anything to be coerced to a numpy array - it is was np.array looks for too (which is why np.atleast_1d worked - it uses np.asanyarray which is short for np.array(<input>, subok=True, copy=False). It may be worth checking whether the pandas object doesn't support it already - I wouldn't be surprised...
Thanks, that makes sense, though pandas definitely was failing, so we had to add the unpacking here (I don't know if you saw the diff for #22141 for what we were doing before that PR?)
We are completely untested on custom __array__ objects, so it would be very helpful if someone knowledgeable could mock one up for us just so we don't break this ability in the future.
We are always delicate about what point we coerce to numpy because sometimes objects we get passed have units, and we don't want to strip those out too early. Hence the desire to consolidate all this, and not rely on np.(as)(any)array in a blanket sense. But any advice on how to do this properly is gratefully received.
@jklymak - very much appreciated that you try to remove too much explicit casting to numpy arrays! It seems reasonable to leave it to objects that are passed in to try to be good enough duck types.
@WilliamJamieson Thanks a lot. We can base a test around this, and make sure we don't break you (and others presumably) in the future. And again, sorry for the hassle.
Activity
jklymak commentedon May 4, 2022
OK, sorry about that - its pretty hard to track all the ways we can coerce to numpy, and we are working hard to consolidate them. Can astropy make a mocked up class for us that we can use for testing that should work? Currently looks like we are supporting
to_numpyand then callingnp.atleast_1dfor some cases, but not doing it always.jklymak commentedon May 4, 2022
Its pretty easy to add a check for
__array__to the fallbacks. I think it just becomes an issue of what takes precedence if an object has multiple array accessors. My knee jerk would be:to_numpy>values>__array__, but I'm not sure if there is a proper convention here.tacaswell commentedon May 4, 2022
I think
__array__should be first.pllim commentedon May 4, 2022
I am going to ping @keflavich and @mhvk here in case they have insights on
astropyinterface.jklymak commentedon May 4, 2022
The private before the public?
tacaswell commentedon May 4, 2022
I would say actual protocol rather than ad-hoc protocol.
mhvk commentedon May 4, 2022
@jklymak -
__array__is the standard protocol for allowing anything to be coerced to anumpyarray - it is wasnp.arraylooks for too (which is whynp.atleast_1dworked - it usesnp.asanyarraywhich is short fornp.array(<input>, subok=True, copy=False). It may be worth checking whether the pandas object doesn't support it already - I wouldn't be surprised...jklymak commentedon May 4, 2022
Thanks, that makes sense, though pandas definitely was failing, so we had to add the unpacking here (I don't know if you saw the diff for #22141 for what we were doing before that PR?)
We are completely untested on custom
__array__objects, so it would be very helpful if someone knowledgeable could mock one up for us just so we don't break this ability in the future.We are always delicate about what point we coerce to numpy because sometimes objects we get passed have units, and we don't want to strip those out too early. Hence the desire to consolidate all this, and not rely on
np.(as)(any)arrayin a blanket sense. But any advice on how to do this properly is gratefully received.jklymak commentedon May 4, 2022
For instance, the fix in #22975 fails the unit interface tests....
mhvk commentedon May 4, 2022
@jklymak - very much appreciated that you try to remove too much explicit casting to numpy arrays! It seems reasonable to leave it to objects that are passed in to try to be good enough duck types.
WilliamJamieson commentedon May 4, 2022
Here is a minimal reproducer class based on
astropy.convolution.core.Kernel:jklymak commentedon May 4, 2022
@WilliamJamieson Thanks a lot. We can base a test around this, and make sure we don't break you (and others presumably) in the future. And again, sorry for the hassle.
oscargus commentedon May 5, 2022
Just linking with #22645
to_numpy seems not needed anymore