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
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
The text was updated successfully, but these errors were encountered:
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.
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 atransform()
step in my pipeline that runsx = 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 becausenumpy.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 ifarray
is already a numpy array. Not sure exactly how to enforceorder
anddtype
, 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
The text was updated successfully, but these errors were encountered: