Thanks to visit codestin.com
Credit goes to github.com

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/release/1.13.0-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ Deprecations
with ``np.minimum``.
* Calling ``ndarray.conjugate`` on non-numeric dtypes is deprecated (it
should match the behavior of ``np.conjugate``, which throws an error).
* Calling ``expand_dims`` when the ``axis`` keyword does not satisfy
``-a.ndim - 1 <= axis <= a.ndim``, where ``a`` is the array being reshaped,
is deprecated.


Future Changes
Expand Down
21 changes: 18 additions & 3 deletions numpy/lib/shape_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,20 @@ def expand_dims(a, axis):
"""
Expand the shape of an array.

Insert a new axis, corresponding to a given position in the array shape.
Insert a new axis that will appear at the `axis` position in the expanded
array shape.

.. note:: Previous to NumPy 1.13.0, neither ``axis < -a.ndim - 1`` nor
``axis > a.ndim`` raised errors or put the new axis where documented.
Those axis values are now deprecated and will raise an AxisError in the
future.

Parameters
----------
a : array_like
Input array.
axis : int
Position (amongst axes) where new axis is to be inserted.
Position in the expanded axes where the new axis is placed.

Returns
-------
Expand Down Expand Up @@ -291,7 +297,16 @@ def expand_dims(a, axis):
"""
a = asarray(a)
shape = a.shape
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 "
"deprecated and will raise an AxisError in the future.",
DeprecationWarning, stacklevel=2)
# When the deprecation period expires, delete this if block,
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.

return a.reshape(shape[:axis] + (1,) + shape[axis:])

row_stack = vstack
Expand Down
23 changes: 22 additions & 1 deletion numpy/lib/tests/test_shape_base.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import division, absolute_import, print_function

import numpy as np
import warnings

from numpy.lib.shape_base import (
apply_along_axis, apply_over_axes, array_split, split, hsplit, dsplit,
vsplit, dstack, column_stack, kron, tile
vsplit, dstack, column_stack, kron, tile, expand_dims,
)
from numpy.testing import (
run_module_suite, TestCase, assert_, assert_equal, assert_array_equal,
Expand Down Expand Up @@ -182,6 +184,25 @@ def test_simple(self):
assert_array_equal(aoa_a, np.array([[[60], [92], [124]]]))


class TestExpandDims(TestCase):
def test_functionality(self):
s = (2, 3, 4, 5)
a = np.empty(s)
for axis in range(-5, 4):
b = expand_dims(a, axis)
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.

# 2017-05-17, 1.13.0
s = (2, 3, 4, 5)
a = np.empty(s)
with warnings.catch_warnings():
warnings.simplefilter("always")
assert_warns(DeprecationWarning, expand_dims, a, -6)
assert_warns(DeprecationWarning, expand_dims, a, 5)


class TestArraySplit(TestCase):
def test_integer_0_split(self):
a = np.arange(10)
Expand Down