-
-
Notifications
You must be signed in to change notification settings - Fork 11k
ENH/DEP: Use a ufunc under the hood for ndarray.clip #12519
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
Changes from all commits
dfe7b9d
6492f63
ed825fb
f76fa21
31f0bb1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1961,12 +1961,12 @@ def compress(condition, a, axis=None, out=None): | |
return _wrapfunc(a, 'compress', condition, axis=axis, out=out) | ||
|
||
|
||
def _clip_dispatcher(a, a_min, a_max, out=None): | ||
def _clip_dispatcher(a, a_min, a_max, out=None, **kwargs): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that by becoming a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree - what's left of clip is just a ufunc selector based on argument presence, there's no reason to allow customization of that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, the current promise is that everything directly exposed by NumPy gets its own dispatcher, either via There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we consider There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd think that p.s. An analogy here may be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The rest of I'm also reluctant to do this on an ad-hoc basis. I don't think it's worth the complexity of having exactly one special function that is neither a ufunc nor with a direct override via There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, perhaps not that simple. Maybe for now best to finish this PR without removing the |
||
return (a, a_min, a_max) | ||
|
||
|
||
@array_function_dispatch(_clip_dispatcher) | ||
def clip(a, a_min, a_max, out=None): | ||
def clip(a, a_min, a_max, out=None, **kwargs): | ||
""" | ||
Clip (limit) the values in an array. | ||
|
||
|
@@ -1975,6 +1975,9 @@ def clip(a, a_min, a_max, out=None): | |
is specified, values smaller than 0 become 0, and values larger | ||
than 1 become 1. | ||
|
||
Equivalent to but faster than ``np.maximum(a_min, np.minimum(a, a_max))``. | ||
No check is performed to ensure ``a_min < a_max``. | ||
|
||
Parameters | ||
---------- | ||
a : array_like | ||
|
@@ -1992,6 +1995,11 @@ def clip(a, a_min, a_max, out=None): | |
The results will be placed in this array. It may be the input | ||
array for in-place clipping. `out` must be of the right shape | ||
to hold the output. Its type is preserved. | ||
**kwargs | ||
For other keyword-only arguments, see the | ||
:ref:`ufunc docs <ufuncs.kwargs>`. | ||
|
||
.. versionadded:: 1.17.0 | ||
|
||
Returns | ||
------- | ||
|
@@ -2020,7 +2028,7 @@ def clip(a, a_min, a_max, out=None): | |
array([3, 4, 2, 3, 4, 5, 6, 7, 8, 8]) | ||
|
||
""" | ||
return _wrapfunc(a, 'clip', a_min, a_max, out=out) | ||
return _wrapfunc(a, 'clip', a_min, a_max, out=out, **kwargs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was about to similarly suggest we remove I guess the question then becomes whether having such a |
||
|
||
|
||
def _sum_dispatcher(a, axis=None, dtype=None, out=None, keepdims=None, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The infinity substitutions above on scalar
np.nan
clip limits would seem to preclude thenpy_isnan()
checks inclip.c.src
MIN and MAX preprocessor directives aimed at propagating nans.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct - but these substitutions only occur in the scalar case, not the array case. Unfortunately this was explicitly tested-for behavior, so I felt I had to preserve it.