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
ENH: Please consider adding a very simple abstract base class that promises the array interface #20459
Comments
I am fine with adding an ABC if there is buy-in from those libraries to register/inherit with it. You could also have the ABC check for In fact, you could just write such an ABC yourself and show us how nice it is ;). You probably need to clearly define what defines an array which can register? Is it one with |
As requested (Along with some default behaviour from the docs): import numpy as np
import jax.numpy as jnp
from abc import ABC
from typing import Type, Any, Literal, Union, Optional, TypeVar
T = TypeVar('T', bound='AbstractArray')
class AbstractArray(ABC):
@classmethod
def __subclasshook__(cls, subclass: Type[Any]) -> Union[bool, Literal[NotImplemented]]:
if hasattr(subclass, '__array_ufunc__') or hasattr(subclass, '__array_priority__'):
return True
return super().__subclasshook__(subclass)
def __array_finalize__(self) -> None:
pass
def __array_prepare__(self: T, context: Optional[Any] = None) -> T:
return self
def __array_wrap__(self: T, context: Optional[Any] = None) -> T:
return self
__array_priority__ = 0
print(issubclass(np.ndarray, AbstractArray)) # True
print(issubclass(jnp.ndarray, AbstractArray)) # False, but can be fixed with AbstractArray.register(jnp.ndarray)
print(isinstance(jnp.zeros(10), AbstractArray)) # True I guess this might fit in
It appears that Jax does neither of these things, so does it make sense to block them? |
It just occurred to me that a much simpler solution would be to simply make It would then be possible to do: np.ndarray.register(jnp.ndarray) # Boom! The only downside would be that |
The other downside is that it would break everything :)? You can't adhere to an |
So what exactly is the intended purpose of this ABC and/or protocol? Are we talking about recognizing objects that can be coerced into an array? |
@seberg Sorry, could you elaborate so that I can understand better? @BvB93 The point is to fix the case—in the nicest way possible—whereby Jax arrays that implement the numpy array interface are not recognized or treated as such by libraries like Seaborn and Pandas. Either we have to convince packages like Pandas to replace their The other option would be to convince the Jax team to have all of their array classes inherit from |
@NeilGirdhar did you ever see an ABC being a concrete class? The answer is "no" because it doesn't make sense (possibly
OK, but if To be convincing, you either need to show that pandas devs really want this, or that it is obvious useful in some generic way. Since people can also have it (by using Right now it feels very much not thought through what exactly you want this ABC to mean, and whether it is enough/useful.
Again, this is simply impossible. |
True, I haven't. But all that And I've seen plenty of concrete classes that inherit from the collections ABCs. So, these ordinary concrete classes become usable as ABCs whether you want them to be or not. This surprised me when I realized this, but that's what led me to propose having
That's not realistically going to happen. They would have to make the ABC, and users would have to register the Jax classes with that ABC. They'll never buy into something like that.
But they don't care about this; I do. Because I happen to use Jax with Pandas and Seaborn. So this my problem—not theirs. I just want the nicest solution. My current solution is to carefully cast all Jax arrays to Numpy arrays before passing them. This is annoying. It would be awesome if everything just worked.
The goal is that by doing the replacement that I suggest in my last comment, passing Jax arrays to Pandas and Seaborn would be possible. Currently, it causes various crashing bugs.
Oh you know Jax well? Great, I didn't realize that 😄 |
Maybe, but we probably could still have a proof of concept, or a: "yeah, that sounds interesting" from pandas. I simply do not know whether or in what form this would be useful for pandas.
I thought you can write that ABC based on Is it Nailing this down is not trivial, it is a reasonable request, but I am not sure anyone at NumPy can nail it down without downstream input.
Well enough to know that even if it was possible (for the record, I am pretty sure JAX will store data on the GPU, which would proof it most definitely is not) it would be too much of a stretch, but I guess you agree. |
Right.
Okay, if you help me understand the exact downstream questions, maybe I can find some time to ask the right people for input? Thanks, by the way, for taking the time helping me to wrap my head around this.
Oh, I don't know the Jax internals well at all. I imagine there's some reason they don't inherit from |
It's not because no-one answered yet (pandas-dev/pandas#44617), that "we" don't care. I personally think it is a reasonable feature request that we check for array-likes in more places. For example for checking this in the DataFrame constructor, there was just another issue reported as well (pandas-dev/pandas#44616). We might need some discussion (on the pandas side) to which places in the API surface to limit this to, though. |
Sorry! I think this came out wrong. I was just trying to be modest in my proposal. I wasn't sure how big of an issue this would be for pandas 😄 |
Because that doesn't make sense - JAX's
Your original request unfortunately isn't well-defined. Pandas needs either an The answer at google/jax#8701 (comment) seems correct to me. If it would have made sense for other libraries to yield |
I am wondering if an |
The problem is, that there are a few subtle possibilities for
What I am unclear about is, that we should add such an ABC/function unless we know that the same version will be useful across various use-cases. Especially, since a simple However, I have not done a survey, or specific use-cases in mind. If |
Agreed with @seberg. To add to his list: arrays that implement the buffer protocol but not |
@rgommers I just learned about the array API standard today when I was talking with @cgarciae . Looks like a fantastic project that essentially supplants this issue completely. I guess I had the same goal, but I mistakenly thought it could be solved with a simple ABC. I can see now that it really can't. Feel free to close this suggestion, and thank you everyone for the illuminating discussion. I learned a lot. |
Closing, if you want to read more, see also: https://numpy.org/neps/nep-0022-ndarray-duck-typing-overview.html maybe ;). |
Proposed new feature or change:
Many libraries (pandas-dev/pandas#44617) are checking for the array interface like so:
isinstance(x, np.ndarray)
, but this fails forjax.numpy.ndarray
despite it implementing the array interface (google/jax#8701).Instead of trying to convince everyone to check for members, could we get an abstract base class that people could inherit from if they want to promise to support the numpy array interface? For example,
numpy.AbstractArray
? It doesn't need to have any abstract members, but could for example provide a default__array_interface__
.Then, Pandas and Seaborn could replace their
isinstance(x, np.ndarray)
withisinstance(x, np.AbstractArray)
. And all Jax has to do is make their variousjax.numpy.ndarray
classes inherit fromAbstractArray
. Both of these are easy changes.Related: #19752. Although this proposal is for an extremely slim (possibly empty) abstract base class.
The text was updated successfully, but these errors were encountered: