Skip to content

Commit

Permalink
Merge pull request numpy#10797 from tylerjereddy/issue_10586_unravel_…
Browse files Browse the repository at this point in the history
…index

DEP: Updated unravel_index() to deprecate `dims` kwarg in favor of `shape`
  • Loading branch information
mattip authored Oct 17, 2018
2 parents 39388fc + 11b9f6c commit 5e6ff3f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
6 changes: 6 additions & 0 deletions doc/release/1.16.0-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ More in detail, the complex64 type now has the same alignment as a C-struct
``struct {float r, i;}``, according to the compiler used to compile numpy, and
similarly for the complex128 and complex256 types.

``np.unravel_index`` now accepts ``shape`` keyword argument
-----------------------------------------------------------
Previously, only the ``dims`` keyword argument was accepted
for specification of the shape of the array to be used
for unraveling. ``dims`` remains supported, but is now deprecated.


C API changes
=============
Expand Down
10 changes: 7 additions & 3 deletions numpy/core/_add_newdocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5410,7 +5410,7 @@ def luf(lamdaexpr, *args, **kwargs):

add_newdoc('numpy.core.multiarray', 'unravel_index',
"""
unravel_index(indices, dims, order='C')
unravel_index(indices, shape, order='C')
Converts a flat index or array of flat indices into a tuple
of coordinate arrays.
Expand All @@ -5419,10 +5419,14 @@ def luf(lamdaexpr, *args, **kwargs):
----------
indices : array_like
An integer array whose elements are indices into the flattened
version of an array of dimensions ``dims``. Before version 1.6.0,
version of an array of dimensions ``shape``. Before version 1.6.0,
this function accepted just one index value.
dims : tuple of ints
shape : tuple of ints
The shape of the array to use for unraveling ``indices``.
.. versionchanged:: 1.16.0
Renamed from ``dims`` to ``shape``.
order : {'C', 'F'}, optional
Determines whether the indices should be viewed as indexing in
row-major (C-style) or column-major (Fortran-style) order.
Expand Down
22 changes: 21 additions & 1 deletion numpy/core/src/multiarray/compiled_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,27 @@ arr_unravel_index(PyObject *self, PyObject *args, PyObject *kwds)
int i, ret_ndim;
npy_intp ret_dims[NPY_MAXDIMS], ret_strides[NPY_MAXDIMS];

char *kwlist[] = {"indices", "dims", "order", NULL};
char *kwlist[] = {"indices", "shape", "order", NULL};

/* Continue to support the older "dims" argument in place
* of the "shape" argument. Issue an appropriate warning
* if "dims" is detected in keywords, then replace it with
* the new "shape" argument and continue processing as usual */


if (kwds) {
PyObject *dims_item, *shape_item;
dims_item = PyDict_GetItemString(kwds, "dims");
shape_item = PyDict_GetItemString(kwds, "shape");
if (dims_item != NULL && shape_item == NULL) {
if (DEPRECATE("'shape' argument should be"
" used instead of 'dims'") < 0) {
return NULL;
}
PyDict_SetItemString(kwds, "shape", dims_item);
PyDict_DelItemString(kwds, "dims");
}
}

if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&|O&:unravel_index",
kwlist,
Expand Down
30 changes: 29 additions & 1 deletion numpy/lib/tests/test_index_tricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import numpy as np
from numpy.testing import (
assert_, assert_equal, assert_array_equal, assert_almost_equal,
assert_array_almost_equal, assert_raises, assert_raises_regex
assert_array_almost_equal, assert_raises, assert_raises_regex,
assert_warns
)
from numpy.lib.index_tricks import (
mgrid, ogrid, ndenumerate, fill_diagonal, diag_indices, diag_indices_from,
Expand All @@ -16,6 +17,33 @@
class TestRavelUnravelIndex(object):
def test_basic(self):
assert_equal(np.unravel_index(2, (2, 2)), (1, 0))

# test backwards compatibility with older dims
# keyword argument; see Issue #10586
with assert_warns(DeprecationWarning):
# we should achieve the correct result
# AND raise the appropriate warning
# when using older "dims" kw argument
assert_equal(np.unravel_index(indices=2,
dims=(2, 2)),
(1, 0))

# test that new shape argument works properly
assert_equal(np.unravel_index(indices=2,
shape=(2, 2)),
(1, 0))

# test that an invalid second keyword argument
# is properly handled
with assert_raises(TypeError):
np.unravel_index(indices=2, hape=(2, 2))

with assert_raises(TypeError):
np.unravel_index(2, hape=(2, 2))

with assert_raises(TypeError):
np.unravel_index(254, ims=(17, 94))

assert_equal(np.ravel_multi_index((1, 0), (2, 2)), 2)
assert_equal(np.unravel_index(254, (17, 94)), (2, 66))
assert_equal(np.ravel_multi_index((2, 66), (17, 94)), 254)
Expand Down

0 comments on commit 5e6ff3f

Please sign in to comment.