From 3fb541ef51ca60cf5a903ed83f69120fe3f5373a Mon Sep 17 00:00:00 2001 From: Ralf Gommers Date: Wed, 28 Dec 2011 10:30:08 +0100 Subject: TST: meshgrid: test expected shapes for Cartesian and matrix indexing. --- numpy/lib/function_base.py | 16 +++++++++++----- numpy/lib/tests/test_function_base.py | 13 ++++++++++++- 2 files changed, 23 insertions(+), 6 deletions(-) (limited to 'numpy/lib') diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 8e2fc375e..39d1d1c87 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -3215,6 +3215,7 @@ def meshgrid(*xi, **kwargs): 1-D arrays representing the coordinates of a grid. indexing : {'xy', 'ij'}, optional Cartesian ('xy', default) or matrix ('ij') indexing of output. + See Notes for more details. sparse : bool, optional If True a sparse grid is returned in order to conserve memory. Default is False. @@ -3238,9 +3239,13 @@ def meshgrid(*xi, **kwargs): Notes ----- This function supports both indexing conventions through the indexing keyword - argument. Giving the string 'ij' returns a meshgrid with matrix indexing, - while 'xy' returns a meshgrid with Cartesian indexing. The difference is - illustrated by the following code snippet:: + argument. Giving the string 'ij' returns a meshgrid with matrix indexing, + while 'xy' returns a meshgrid with Cartesian indexing. In the 2-D case + with inputs of length M and N, the outputs are of shape (N, M) for 'xy' + indexing and (M, N) for 'ij' indexing. In the 3-D case with inputs of + length M, N and P, outputs are of shape (N, M, P) for 'xy' indexing and (M, + N, P) for 'ij' indexing. The difference is illustrated by the following + code snippet:: xv, yv = meshgrid(x, y, sparse=False, indexing='ij') for i in range(nx): @@ -3287,8 +3292,6 @@ def meshgrid(*xi, **kwargs): >>> h = plt.contourf(x,y,z) """ - copy_ = kwargs.get('copy', True) - if len(xi) < 2: msg = 'meshgrid() takes 2 or more arguments (%d given)' % int(len(xi) > 0) raise ValueError(msg) @@ -3296,8 +3299,11 @@ def meshgrid(*xi, **kwargs): args = np.atleast_1d(*xi) ndim = len(args) + copy_ = kwargs.get('copy', True) sparse = kwargs.get('sparse', False) indexing = kwargs.get('indexing', 'xy') + if not indexing in ['xy', 'ij']: + raise ValueError("Valid values for `indexing` are 'xy' and 'ij'.") s0 = (1,) * ndim output = [x.reshape(s0[:i] + (-1,) + s0[i + 1::]) for i, x in enumerate(args)] diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index eda919df6..57bfee61b 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -1030,7 +1030,9 @@ class TestMeshgrid(TestCase): assert_raises(ValueError, meshgrid, np.arange(5)) def test_indexing(self): - [X, Y] = meshgrid([1, 2, 3], [4, 5, 6, 7], indexing='ij') + x = [1, 2, 3] + y = [4, 5, 6, 7] + [X, Y] = meshgrid(x, y, indexing='ij') assert_(all(X == array([[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]]))) @@ -1038,6 +1040,15 @@ class TestMeshgrid(TestCase): [4, 5, 6, 7], [4, 5, 6, 7]]))) + # Test expected shapes: + z = [8, 9] + assert_(meshgrid(x, y)[0].shape == (4, 3)) + assert_(meshgrid(x, y, indexing='ij')[0].shape == (3, 4)) + assert_(meshgrid(x, y, z)[0].shape == (4, 3, 2)) + assert_(meshgrid(x, y, z, indexing='ij')[0].shape == (3, 4, 2)) + + assert_raises(ValueError, meshgrid, x, y, indexing='notvalid') + def test_sparse(self): [X, Y] = meshgrid([1, 2, 3], [4, 5, 6, 7], sparse=True) assert_(all(X == array([[1, 2, 3]]))) -- cgit v1.2.1