8000 DEP: Deprecate incorrect behavior of expand_dims. by charris · Pull Request #9132 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 1 commit into from
May 18, 2017

Conversation

charris
Copy link
Member
@charris charris commented May 17, 2017

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, index is replaced by index + a.ndim + 1, which may be
negative and results in incorrect placement. The latter two cases are
now deprecated.

Closes #9100.

@charris charris added this to the 1.13.0 release milestone May 17, 2017
@charris charris requested a review from eric-wieser May 17, 2017 23:46
Copy link
Member
@eric-wieser eric-wieser left a 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!

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 "
Copy link
Member

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

Copy link
Member Author

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)
Copy link
Member

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(...)

Copy link
Member Author

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.

Copy link
Member

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.

@charris charris force-pushed the deprecate-expand_dims-bad-axis branch from c507c1a to a474013 Compare May 18, 2017 00:13
assert_(b.shape[axis] == 1)
assert_(np.squeeze(b).shape == s)

def test_deprecations(self):
Copy link
Member

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?

Copy link
Member Author

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.
@charris charris force-pushed the deprecate-expand_dims-bad-axis branch from a474013 to beac50c Compare May 18, 2017 01:03
Copy link
Member
@eric-wieser eric-wieser left a 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.

@eric-wieser eric-wieser merged commit fe16f83 into numpy:master May 18, 2017
@charris charris deleted the deprecate-expand_dims-bad-axis branch May 18, 2017 02:12
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label May 18, 2017
@charris charris removed this from the 1.13.0 release milestone May 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0