Skip to content

Allow subclasses to pass through check_array() #23568

Open
@Jacob-Stevens-Haas

Description

@Jacob-Stevens-Haas

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions