-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
DEP: Deprecate incorrect behavior of expand_dims. #9132
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
DEP: Deprecate incorrect behavior of expand_dims. #9132
Conversation
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.
Thanks for fixing this - I had a much more careless patch in my head!
numpy/lib/shape_base.py
Outdated
axis = normalize_axis_index(axis, a.ndim + 1) | ||
if axis > a.ndim or axis < -a.ndim - 1: | ||
# 2017-05-17, 1.13.0 | ||
warnings.warn("Both axis > a.ndim and axis < a.ndim - 1 are " |
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.
Missing minus in axis < a.ndim - 1
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.
Fixed.
if axis < 0: | ||
axis = axis + a.ndim + 1 | ||
# and uncomment the following line. | ||
# axis = normalize_axis_index(axis, a.ndim + 1) |
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.
I'd be tempted to go with
try:
axis = normalize_axis_index(axis, a.ndim + 1)
except AxisError:
warnings.warn(...)
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.
I think the current code is an easier way to preserve the current behavior.
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.
It's a balance between being sure not to introduce a regression now vs when we remove this deprecation.
c507c1a
to
a474013
Compare
assert_(b.shape[axis] == 1) | ||
assert_(np.squeeze(b).shape == s) | ||
|
||
def test_deprecations(self): |
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.
This should have a 1.13 ...
comment, right?
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.
fixed.
Expand_dims works as documented when the index of the inserted NewAxis in the resulting array satisfies -a.ndim - 1 <= index <= a.ndim. However, when index > a.ndim index is replaced by a.ndim and, when index < -a.ndim - 1, it is replaced by index + a.ndim + 1, which may be negative and results in incorrect placement. The latter two cases are now deprecated. Closes numpy#9100.
a474013
to
beac50c
Compare
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.
LGTM, modulo tests still running.
Expand_dims works as documented when the index of the inserted NewAxis
in the resulting array satisfies
-a.ndim - 1 <= index <= a.ndim
.However, when
index > a.ndim
, index is replaced by a.ndim and, whenindex < -a.ndim - 1
, index is replaced byindex + a.ndim + 1
, which may benegative and results in incorrect placement. The latter two cases are
now deprecated.
Closes #9100.