Skip to content

Allow subclasses to pass through check_array() #23568

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

Open
Jacob-Stevens-Haas opened this issue Jun 9, 2022 · 2 comments
Open

Allow subclasses to pass through check_array() #23568

Jacob-Stevens-Haas opened this issue Jun 9, 2022 · 2 comments

Comments

@Jacob-Stevens-Haas
Copy link

Jacob-Stevens-Haas commented Jun 9, 2022

Describe the workflow you want to enable

I have a subclass of numpy.ndarray which keeps track of axis labels: which axes represent time, spatial directions, independent samples, and response variables. I have a transform() step in my pipeline that runs
x = check_array(x, accept_sparse=("csr", "csc"), ...).
It's nice that this allows me to handle both numpy arrays and sparse inputs . However, this line of scipy.utils.validation.check_array downcasts anything that isn't a sparse array to a numpy array. This is because numpy.asarray does not allow interception via __array_ufunc__ or __array_function__, so subclasses cannot dictate how to handle its behavior.

Describe your proposed solution

isinstance check to see if array is already a numpy array. Not sure exactly how to enforce order and dtype, however.

Describe alternatives you've considered, if relevant

User code instead repeats validation code from check_array. In my case, that means importing the private function _ensure_sparse_format() and skipping or copypasting the other validation checks.

Additional context

No response

@Jacob-Stevens-Haas Jacob-Stevens-Haas changed the title Allow subclasses to Allow subclasses to pass through check_array() Jun 9, 2022
@glemaitre
Copy link
Member

isinstance check to see if array is already a numpy array. Not sure exactly how to enforce order and dtype, however.

I assume that the way forward is the Array API: #22352

@thomasjpfan
Copy link
Member

I think this issue is mostly orthogonal to the Array API issue.

The most straight forward thing to do is the replace the asarray call with np.asanyarray, which preserves the subclass. Note this is technically a change in behavior for check_array, but I do not see any immediately issues with returning a ndarray subclass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants