-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
BUG/DEP: Make ufunclike functions more ufunc-like #8996
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,36 @@ | |
__all__ = ['fix', 'isneginf', 'isposinf'] | ||
|
||
import numpy.core.numeric as nx | ||
import warnings | ||
import functools | ||
|
||
def fix(x, y=None): | ||
def _deprecate_out_named_y(f): | ||
""" | ||
Allow the out argument to be passed as the name `y` (deprecated) | ||
|
||
In future, this decorator should be removed. | ||
""" | ||
@functools.wraps(f) | ||
def func(x, out=None, **kwargs): | ||
if 'y' in kwargs: | ||
if 'out' in kwargs: | ||
raise TypeError( | ||
"{} got multiple values for argument 'out'/'y'" | ||
.format(f.__name__) | ||
) | ||
out = kwargs.pop('y') | ||
# NumPy 1.13.0, 2017-04-26 | ||
warnings.warn( | ||
"The name of the out argument to {} has changed from `y` to " | ||
"`out`, to match other ufuncs.".format(f.__name__), | ||
DeprecationWarning, stacklevel=3) | ||
return f(x, out=out, **kwargs) | ||
|
||
return func | ||
|
||
|
||
@_deprecate_out_named_y | ||
def fix(x, out=None): | ||
""" | ||
Round to nearest integer towards zero. | ||
|
||
|
@@ -43,15 +71,18 @@ def fix(x, y=None): | |
array([ 2., 2., -2., -2.]) | ||
|
||
""" | ||
x = nx.asanyarray(x) | ||
y1 = nx.floor(x) | ||
y2 = nx.ceil(x) | ||
if y is None: | ||
y = nx.asanyarray(y1) | ||
y[...] = nx.where(x >= 0, y1, y2) | ||
return y | ||
|
||
def isposinf(x, y=None): | ||
# promote back to an array if flattened | ||
res = nx.asanyarray(nx.ceil(x, out=out)) | ||
res = nx.floor(x, out=res, where=nx.greater_equal(x, 0)) | ||
|
||
# when no out argument is passed and no subclasses are involved, flatten | ||
# scalars | ||
if out is None and type(res) is nx.ndarray: | ||
res = res[()] | ||
return res | ||
|
||
@_deprecate_out_named_y | ||
def isposinf(x, out=None): | ||
""" | ||
Test element-wise for positive infinity, return result as bool array. | ||
|
||
|
@@ -64,7 +95,7 @@ def isposinf(x, y=None): | |
|
||
Returns | ||
------- | ||
y : ndarray | ||
out : ndarray | ||
A boolean array with the same dimensions as the input. | ||
If second argument is not supplied then a boolean array is returned | ||
with values True where the corresponding element of the input is | ||
|
@@ -74,7 +105,7 @@ def isposinf(x, y=None): | |
If a second argument is supplied the result is stored there. If the | ||
type of that array is a numeric type the result is represented as zeros | ||
and ones, if the type is boolean then as False and True. | ||
The return value `y` is then a reference to that array. | ||
The return value `out` is then a reference to that array. | ||
|
||
See Also | ||
-------- | ||
|
@@ -107,27 +138,25 @@ def isposinf(x, y=None): | |
array([0, 0, 1]) | ||
|
||
""" | ||
if y is None: | ||
x = nx.asarray(x) | ||
y = nx.empty(x.shape, dtype=nx.bool_) | ||
nx.logical_and(nx.isinf(x), ~nx.signbit(x), y) | ||
return y | ||
return nx.logical_and(nx.isinf(x), ~nx.signbit(x), out) | ||
|
||
def isneginf(x, y=None): | ||
|
||
@_deprecate_out_named_y | ||
def isneginf(x, out=None): | ||
""" | ||
Test element-wise for negative infinity, return result as bool array. | ||
|
||
Parameters | ||
---------- | ||
x : array_like | ||
The input array. | ||
y : array_like, optional | ||
out : array_like, optional | ||
A boolean array with the same shape and type as `x` to store the | ||
result. | ||
|
||
Returns | ||
------- | ||
y : ndarray | ||
out : ndarray | ||
A boolean array with the same dimensions as the input. | ||
If second argument is not supplied then a numpy boolean array is | ||
returned with values True where the corresponding element of the | ||
|
@@ -137,7 +166,7 @@ def isneginf(x, y=None): | |
If a second argument is supplied the result is stored there. If the | ||
type of that array is a numeric type the result is represented as | ||
zeros and ones, if the type is boolean then as False and True. The | ||
return value `y` is then a reference to that array. | ||
return value `out` is then a reference to that array. | ||
|
||
See Also | ||
-------- | ||
|
@@ -170,8 +199,4 @@ def isneginf(x, y=None): | |
array([1, 0, 0]) | ||
|
||
""" | ||
if y is None: | ||
x = nx.asarray(x) | ||
y = nx.empty(x.shape, dtype=nx.bool_) | ||
nx.logical_and(nx.isinf(x), nx.signbit(x), y) | ||
return y | ||
return nx.logical_and(nx.isinf(x), nx.signbit(x), out) | ||
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. For a little more efficiency, we could change this to:
But that seemed out of scope for the bugfix/deprecation |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Previous commit forgot to specify a stacklevel