diff options
author | Antony Lee <anntzer.lee@gmail.com> | 2019-01-05 11:52:22 +0100 |
---|---|---|
committer | Antony Lee <anntzer.lee@gmail.com> | 2019-01-09 18:41:12 +0100 |
commit | 70a15c1740f21ba53e54e9d8ab35a013688b3203 (patch) | |
tree | c5327aa5b3883014fef1156dc15696757be94fee | |
parent | aef982e4482773e802cc0ef076bf5e76ff650cf9 (diff) | |
download | numpy-70a15c1740f21ba53e54e9d8ab35a013688b3203.tar.gz |
Improve error messages for non-matching shapes in concatenate.
... by including the indices and shapes of the arrays whose shape
differ.
-rw-r--r-- | numpy/core/src/multiarray/multiarraymodule.c | 19 | ||||
-rw-r--r-- | numpy/core/tests/test_shape_base.py | 16 |
2 files changed, 27 insertions, 8 deletions
diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index 166533b3f..ce6a3870f 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -409,9 +409,12 @@ PyArray_ConcatenateArrays(int narrays, PyArrayObject **arrays, int axis, npy_intp *arr_shape; if (PyArray_NDIM(arrays[iarrays]) != ndim) { - PyErr_SetString(PyExc_ValueError, - "all the input arrays must have same " - "number of dimensions"); + PyErr_Format(PyExc_ValueError, + "all the input arrays must have same number of " + "dimensions, but the array at index %d has %d " + "dimension(s) and the array at index %d has %d " + "dimension(s)", + 0, ndim, iarrays, PyArray_NDIM(arrays[iarrays])); return NULL; } arr_shape = PyArray_SHAPE(arrays[iarrays]); @@ -423,10 +426,12 @@ PyArray_ConcatenateArrays(int narrays, PyArrayObject **arrays, int axis, } /* Validate that the rest of the dimensions match */ else if (shape[idim] != arr_shape[idim]) { - PyErr_SetString(PyExc_ValueError, - "all the input array dimensions " - "except for the concatenation axis " - "must match exactly"); + PyErr_Format(PyExc_ValueError, + "all the input array dimensions for the " + "concatenation axis must match exactly, but " + "along dimension %d, the array at index %d has " + "size %d and the array at index %d has size %d", + idim, 0, shape[idim], iarrays, arr_shape[idim]); return NULL; } } diff --git a/numpy/core/tests/test_shape_base.py b/numpy/core/tests/test_shape_base.py index b996321c2..53d272fc5 100644 --- a/numpy/core/tests/test_shape_base.py +++ b/numpy/core/tests/test_shape_base.py @@ -224,13 +224,27 @@ class TestConcatenate(object): assert_raises(ValueError, concatenate, (0,)) assert_raises(ValueError, concatenate, (np.array(0),)) + # dimensionality must match + assert_raises_regex( + ValueError, + r"all the input arrays must have same number of dimensions, but " + r"the array at index 0 has 1 dimension\(s\) and the array at " + r"index 1 has 2 dimension\(s\)", + np.concatenate, (np.zeros(1), np.zeros((1, 1)))) + # test shapes must match except for concatenation axis a = np.ones((1, 2, 3)) b = np.ones((2, 2, 3)) axis = list(range(3)) for i in range(3): np.concatenate((a, b), axis=axis[0]) # OK - assert_raises(ValueError, np.concatenate, (a, b), axis=axis[1]) + assert_raises_regex( + ValueError, + "all the input array dimensions for the concatenation axis " + "must match exactly, but along dimension {}, the array at " + "index 0 has size 1 and the array at index 1 has size 2" + .format(i), + np.concatenate, (a, b), axis=axis[1]) assert_raises(ValueError, np.concatenate, (a, b), axis=axis[2]) a = np.moveaxis(a, -1, 0) b = np.moveaxis(b, -1, 0) |