diff options
author | Eric Wieser <wieser.eric@gmail.com> | 2017-02-24 16:46:58 +0000 |
---|---|---|
committer | Eric Wieser <wieser.eric@gmail.com> | 2017-02-24 16:46:58 +0000 |
commit | 48783e5ceb7f60c33db81ab72e5024f42b220990 (patch) | |
tree | 2284b780e521418b0e73fd7283403d3e7e28da50 | |
parent | 5f5ccecbfc116284ed8c8d53cd8b203ceef5f7c7 (diff) | |
download | numpy-48783e5ceb7f60c33db81ab72e5024f42b220990.tar.gz |
MAINT: replace len(x.shape) with x.ndim
-rw-r--r-- | numpy/core/arrayprint.py | 2 | ||||
-rw-r--r-- | numpy/core/einsumfunc.py | 2 | ||||
-rw-r--r-- | numpy/core/numeric.py | 6 | ||||
-rw-r--r-- | numpy/core/records.py | 4 | ||||
-rw-r--r-- | numpy/core/shape_base.py | 12 | ||||
-rw-r--r-- | numpy/core/tests/test_ufunc.py | 2 | ||||
-rw-r--r-- | numpy/fft/helper.py | 4 | ||||
-rw-r--r-- | numpy/lib/arraypad.py | 2 | ||||
-rw-r--r-- | numpy/lib/arrayterator.py | 4 | ||||
-rw-r--r-- | numpy/lib/function_base.py | 8 | ||||
-rw-r--r-- | numpy/lib/npyio.py | 2 | ||||
-rw-r--r-- | numpy/lib/polynomial.py | 4 | ||||
-rw-r--r-- | numpy/lib/shape_base.py | 10 | ||||
-rw-r--r-- | numpy/lib/user_array.py | 4 | ||||
-rw-r--r-- | numpy/linalg/linalg.py | 12 | ||||
-rw-r--r-- | numpy/linalg/tests/test_linalg.py | 2 | ||||
-rw-r--r-- | numpy/ma/core.py | 8 | ||||
-rw-r--r-- | numpy/ma/mrecords.py | 6 | ||||
-rw-r--r-- | numpy/matrixlib/defmatrix.py | 2 |
19 files changed, 48 insertions, 48 deletions
diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py index 349f8ea39..318ad5495 100644 --- a/numpy/core/arrayprint.py +++ b/numpy/core/arrayprint.py @@ -331,7 +331,7 @@ def _array2string(a, max_line_width, precision, suppress_small, separator=' ', # skip over array( next_line_prefix += " "*len(prefix) - lst = _formatArray(a, format_function, len(a.shape), max_line_width, + lst = _formatArray(a, format_function, a.ndim, max_line_width, next_line_prefix, separator, _summaryEdgeItems, summary_insert)[:-1] return lst diff --git a/numpy/core/einsumfunc.py b/numpy/core/einsumfunc.py index 0b15c213b..c54a4a263 100644 --- a/numpy/core/einsumfunc.py +++ b/numpy/core/einsumfunc.py @@ -360,7 +360,7 @@ def _parse_einsum_input(operands): if operands[num].shape == (): ellipse_count = 0 else: - ellipse_count = max(len(operands[num].shape), 1) + ellipse_count = max(operands[num].ndim, 1) ellipse_count -= (len(sub) - 3) if ellipse_count > longest: diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index d4d4045a0..896ad7f6a 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -1354,9 +1354,9 @@ def tensordot(a, b, axes=2): a, b = asarray(a), asarray(b) as_ = a.shape - nda = len(a.shape) + nda = a.ndim bs = b.shape - ndb = len(b.shape) + ndb = b.ndim equal = True if na != nb: equal = False @@ -1461,7 +1461,7 @@ def roll(a, shift, axis=None): else: broadcasted = broadcast(shift, axis) - if len(broadcasted.shape) > 1: + if broadcasted.ndim > 1: raise ValueError( "'shift' and 'axis' should be scalars or 1D sequences") shifts = {ax: 0 for ax in range(a.ndim)} diff --git a/numpy/core/records.py b/numpy/core/records.py index 91b70614c..7ad0c111a 100644 --- a/numpy/core/records.py +++ b/numpy/core/records.py @@ -613,8 +613,8 @@ def fromarrays(arrayList, dtype=None, shape=None, formats=None, shape = shape[:-nn] for k, obj in enumerate(arrayList): - nn = len(descr[k].shape) - testshape = obj.shape[:len(obj.shape) - nn] + nn = descr[k].ndim + testshape = obj.shape[:obj.ndim - nn] if testshape != shape: raise ValueError("array-shape mismatch in array %d" % k) diff --git a/numpy/core/shape_base.py b/numpy/core/shape_base.py index 58b0dcaac..22ed17836 100644 --- a/numpy/core/shape_base.py +++ b/numpy/core/shape_base.py @@ -49,7 +49,7 @@ def atleast_1d(*arys): res = [] for ary in arys: ary = asanyarray(ary) - if len(ary.shape) == 0: + if ary.ndim == 0: result = ary.reshape(1) else: result = ary @@ -99,9 +99,9 @@ def atleast_2d(*arys): res = [] for ary in arys: ary = asanyarray(ary) - if len(ary.shape) == 0: + if ary.ndim == 0: result = ary.reshape(1, 1) - elif len(ary.shape) == 1: + elif ary.ndim == 1: result = ary[newaxis,:] else: result = ary @@ -163,11 +163,11 @@ def atleast_3d(*arys): res = [] for ary in arys: ary = asanyarray(ary) - if len(ary.shape) == 0: + if ary.ndim == 0: result = ary.reshape(1, 1, 1) - elif len(ary.shape) == 1: + elif ary.ndim == 1: result = ary[newaxis,:, newaxis] - elif len(ary.shape) == 2: + elif ary.ndim == 2: result = ary[:,:, newaxis] else: result = ary diff --git a/numpy/core/tests/test_ufunc.py b/numpy/core/tests/test_ufunc.py index f7b66f90c..8a5e7f603 100644 --- a/numpy/core/tests/test_ufunc.py +++ b/numpy/core/tests/test_ufunc.py @@ -1013,7 +1013,7 @@ class TestUfunc(TestCase): MyThing.getitem_count += 1 if not isinstance(i, tuple): i = (i,) - if len(i) > len(self.shape): + if len(i) > self.ndim: raise IndexError("boo") return MyThing(self.shape[len(i):]) diff --git a/numpy/fft/helper.py b/numpy/fft/helper.py index 0832bc5a4..0856d6759 100644 --- a/numpy/fft/helper.py +++ b/numpy/fft/helper.py @@ -64,7 +64,7 @@ def fftshift(x, axes=None): """ tmp = asarray(x) - ndim = len(tmp.shape) + ndim = tmp.ndim if axes is None: axes = list(range(ndim)) elif isinstance(axes, integer_types): @@ -113,7 +113,7 @@ def ifftshift(x, axes=None): """ tmp = asarray(x) - ndim = len(tmp.shape) + ndim = tmp.ndim if axes is None: axes = list(range(ndim)) elif isinstance(axes, integer_types): diff --git a/numpy/lib/arraypad.py b/numpy/lib/arraypad.py index 15e3ed957..2dad99c34 100644 --- a/numpy/lib/arraypad.py +++ b/numpy/lib/arraypad.py @@ -1338,7 +1338,7 @@ def pad(array, pad_width, mode, **kwargs): function = mode # Create a new padded array - rank = list(range(len(narray.shape))) + rank = list(range(narray.ndim)) total_dim_increase = [np.sum(pad_width[i]) for i in rank] offset_slices = [slice(pad_width[i][0], pad_width[i][0] + narray.shape[i]) diff --git a/numpy/lib/arrayterator.py b/numpy/lib/arrayterator.py index fb52ada86..f2d4fe9fd 100644 --- a/numpy/lib/arrayterator.py +++ b/numpy/lib/arrayterator.py @@ -106,7 +106,7 @@ class Arrayterator(object): if not isinstance(index, tuple): index = (index,) fixed = [] - length, dims = len(index), len(self.shape) + length, dims = len(index), self.ndim for slice_ in index: if slice_ is Ellipsis: fixed.extend([slice(None)] * (dims-length+1)) @@ -186,7 +186,7 @@ class Arrayterator(object): start = self.start[:] stop = self.stop[:] step = self.step[:] - ndims = len(self.var.shape) + ndims = self.var.ndim while True: count = self.buf_size or reduce(mul, self.shape) diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index fc49a6fd7..1cf57d617 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -1674,7 +1674,7 @@ def gradient(f, *varargs, **kwargs): S0025-5718-1988-0935077-0/S0025-5718-1988-0935077-0.pdf>`_. """ f = np.asanyarray(f) - N = len(f.shape) # number of dimensions + N = f.ndim # number of dimensions axes = kwargs.pop('axis', None) if axes is None: @@ -1900,7 +1900,7 @@ def diff(a, n=1, axis=-1): raise ValueError( "order must be non-negative but got " + repr(n)) a = asanyarray(a) - nd = len(a.shape) + nd = a.ndim slice1 = [slice(None)]*nd slice2 = [slice(None)]*nd slice1[axis] = slice(1, None) @@ -2144,7 +2144,7 @@ def unwrap(p, discont=pi, axis=-1): """ p = asarray(p) - nd = len(p.shape) + nd = p.ndim dd = diff(p, axis=axis) slice1 = [slice(None, None)]*nd # full slices slice1[axis] = slice(1, None) @@ -4488,7 +4488,7 @@ def trapz(y, x=None, dx=1.0, axis=-1): d = d.reshape(shape) else: d = diff(x, axis=axis) - nd = len(y.shape) + nd = y.ndim slice1 = [slice(None)]*nd slice2 = [slice(None)]*nd slice1[axis] = slice(1, None) diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index c575cc030..0dee6b333 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -925,7 +925,7 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, flat_dt, flat_packing = flatten_dtype(tp) types.extend(flat_dt) # Avoid extra nesting for subarrays - if len(tp.shape) > 0: + if tp.ndim > 0: packing.extend(flat_packing) else: packing.append((len(flat_dt), flat_packing)) diff --git a/numpy/lib/polynomial.py b/numpy/lib/polynomial.py index 281d79ec5..f00d95b6c 100644 --- a/numpy/lib/polynomial.py +++ b/numpy/lib/polynomial.py @@ -201,7 +201,7 @@ def roots(p): """ # If input is scalar, this makes it an array p = atleast_1d(p) - if len(p.shape) != 1: + if p.ndim != 1: raise ValueError("Input must be a rank-1 array.") # find non-zero array entries @@ -1051,7 +1051,7 @@ class poly1d(object): if r: c_or_r = poly(c_or_r) c_or_r = atleast_1d(c_or_r) - if len(c_or_r.shape) > 1: + if c_or_r.ndim > 1: raise ValueError("Polynomial must be 1d only.") c_or_r = trim_zeros(c_or_r, trim='f') if len(c_or_r) == 0: diff --git a/numpy/lib/shape_base.py b/numpy/lib/shape_base.py index 62798286f..8ebcf04b4 100644 --- a/numpy/lib/shape_base.py +++ b/numpy/lib/shape_base.py @@ -390,7 +390,7 @@ def dstack(tup): def _replace_zero_by_x_arrays(sub_arys): for i in range(len(sub_arys)): - if len(_nx.shape(sub_arys[i])) == 0: + if _nx.ndim(sub_arys[i]) == 0: sub_arys[i] = _nx.empty(0, dtype=sub_arys[i].dtype) elif _nx.sometrue(_nx.equal(_nx.shape(sub_arys[i]), 0)): sub_arys[i] = _nx.empty(0, dtype=sub_arys[i].dtype) @@ -577,9 +577,9 @@ def hsplit(ary, indices_or_sections): [[ 6., 7.]]])] """ - if len(_nx.shape(ary)) == 0: + if _nx.ndim(ary) == 0: raise ValueError('hsplit only works on arrays of 1 or more dimensions') - if len(ary.shape) > 1: + if ary.ndim > 1: return split(ary, indices_or_sections, 1) else: return split(ary, indices_or_sections, 0) @@ -631,7 +631,7 @@ def vsplit(ary, indices_or_sections): [ 6., 7.]]])] """ - if len(_nx.shape(ary)) < 2: + if _nx.ndim(ary) < 2: raise ValueError('vsplit only works on arrays of 2 or more dimensions') return split(ary, indices_or_sections, 0) @@ -676,7 +676,7 @@ def dsplit(ary, indices_or_sections): array([], dtype=float64)] """ - if len(_nx.shape(ary)) < 3: + if _nx.ndim(ary) < 3: raise ValueError('dsplit only works on arrays of 3 or more dimensions') return split(ary, indices_or_sections, 2) diff --git a/numpy/lib/user_array.py b/numpy/lib/user_array.py index 62398fc3c..f1510a7b1 100644 --- a/numpy/lib/user_array.py +++ b/numpy/lib/user_array.py @@ -34,7 +34,7 @@ class container(object): self.array = array(data, dtype, copy=copy) def __repr__(self): - if len(self.shape) > 0: + if self.ndim > 0: return self.__class__.__name__ + repr(self.array)[len("array"):] else: return self.__class__.__name__ + "(" + repr(self.array) + ")" @@ -183,7 +183,7 @@ class container(object): return self._rc(invert(self.array)) def _scalarfunc(self, func): - if len(self.shape) == 0: + if self.ndim == 0: return func(self[0]) else: raise TypeError( diff --git a/numpy/linalg/linalg.py b/numpy/linalg/linalg.py index 6002c63b9..84e450b12 100644 --- a/numpy/linalg/linalg.py +++ b/numpy/linalg/linalg.py @@ -192,15 +192,15 @@ def _fastCopyAndTranspose(type, *arrays): def _assertRank2(*arrays): for a in arrays: - if len(a.shape) != 2: + if a.ndim != 2: raise LinAlgError('%d-dimensional array given. Array must be ' - 'two-dimensional' % len(a.shape)) + 'two-dimensional' % a.ndim) def _assertRankAtLeast2(*arrays): for a in arrays: - if len(a.shape) < 2: + if a.ndim < 2: raise LinAlgError('%d-dimensional array given. Array must be ' - 'at least two-dimensional' % len(a.shape)) + 'at least two-dimensional' % a.ndim) def _assertSquareness(*arrays): for a in arrays: @@ -231,7 +231,7 @@ def tensorsolve(a, b, axes=None): It is assumed that all indices of `x` are summed over in the product, together with the rightmost indices of `a`, as is done in, for example, - ``tensordot(a, x, axes=len(b.shape))``. + ``tensordot(a, x, axes=b.ndim)``. Parameters ---------- @@ -1917,7 +1917,7 @@ def lstsq(a, b, rcond=-1): import math a, _ = _makearray(a) b, wrap = _makearray(b) - is_1d = len(b.shape) == 1 + is_1d = b.ndim == 1 if is_1d: b = b[:, newaxis] _assertRank2(a, b) diff --git a/numpy/linalg/tests/test_linalg.py b/numpy/linalg/tests/test_linalg.py index fc4f98ed7..31fde186f 100644 --- a/numpy/linalg/tests/test_linalg.py +++ b/numpy/linalg/tests/test_linalg.py @@ -743,7 +743,7 @@ class TestLstsq(LinalgSquareTestCase, LinalgNonsquareTestCase): expect_resids = ( np.asarray(abs(np.dot(a, x) - b)) ** 2).sum(axis=0) expect_resids = np.asarray(expect_resids) - if len(np.asarray(b).shape) == 1: + if np.asarray(b).ndim == 1: expect_resids.shape = (1,) assert_equal(residuals.shape, expect_resids.shape) else: diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 30ef5dbfc..3b2b39b18 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -3204,7 +3204,7 @@ class MaskedArray(ndarray): # If we're indexing a multidimensional field in a # structured array (such as dtype("(2,)i2,(2,)i1")), # dimensionality goes up (M[field].ndim == M.ndim + - # len(M.dtype[field].shape)). That's fine for + # M.dtype[field].ndim). That's fine for # M[field] but problematic for M[field].fill_value # which should have shape () to avoid breaking several # methods. There is no great way out, so set to @@ -3846,7 +3846,7 @@ class MaskedArray(ndarray): Literal string representation. """ - n = len(self.shape) + n = self.ndim if self._baseclass is np.ndarray: name = 'array' else: @@ -7319,9 +7319,9 @@ def inner(a, b): """ fa = filled(a, 0) fb = filled(b, 0) - if len(fa.shape) == 0: + if fa.ndim == 0: fa.shape = (1,) - if len(fb.shape) == 0: + if fb.ndim == 0: fb.shape = (1,) return np.inner(fa, fb).view(MaskedArray) inner.__doc__ = doc_note(np.inner.__doc__, diff --git a/numpy/ma/mrecords.py b/numpy/ma/mrecords.py index 45359cc81..ef5f5fd53 100644 --- a/numpy/ma/mrecords.py +++ b/numpy/ma/mrecords.py @@ -625,7 +625,7 @@ def fromrecords(reclist, dtype=None, shape=None, formats=None, names=None, maskrecordlength = len(mask.dtype) if maskrecordlength: mrec._mask.flat = mask - elif len(mask.shape) == 2: + elif mask.ndim == 2: mrec._mask.flat = [tuple(m) for m in mask] else: mrec.__setmask__(mask) @@ -646,9 +646,9 @@ def _guessvartypes(arr): """ vartypes = [] arr = np.asarray(arr) - if len(arr.shape) == 2: + if arr.ndim == 2: arr = arr[0] - elif len(arr.shape) > 2: + elif arr.ndim > 2: raise ValueError("The array should be 2D at most!") # Start the conversion loop. for f in arr: diff --git a/numpy/matrixlib/defmatrix.py b/numpy/matrixlib/defmatrix.py index 6c7640cb8..bd14846c6 100644 --- a/numpy/matrixlib/defmatrix.py +++ b/numpy/matrixlib/defmatrix.py @@ -169,7 +169,7 @@ def matrix_power(M, n): """ M = asanyarray(M) - if len(M.shape) != 2 or M.shape[0] != M.shape[1]: + if M.ndim != 2 or M.shape[0] != M.shape[1]: raise ValueError("input must be a square array") if not issubdtype(type(n), int): raise TypeError("exponent must be an integer") |