diff options
author | Eric Wieser <wieser.eric@gmail.com> | 2018-02-16 21:36:38 -0800 |
---|---|---|
committer | Eric Wieser <wieser.eric@gmail.com> | 2018-02-16 22:23:51 -0800 |
commit | e441c291b2e10c8de85a9d950d0add552d0ebd83 (patch) | |
tree | e407fc2b65b888c13f17bcb1e2654f990c2932b2 /numpy/lib/function_base.py | |
parent | 5c5a215fa1101479ae9b8d127be32679c9f3f105 (diff) | |
download | numpy-e441c291b2e10c8de85a9d950d0add552d0ebd83.tar.gz |
MAINT: Stop using non-tuple indices internally
By not using this type of indexing, it becomes easier for subclasses to override indexing in a way that works correctly with numpy functions.
These locations were found by deprecating the behavior in question, which is deliberately not part of this commit
Diffstat (limited to 'numpy/lib/function_base.py')
-rw-r--r-- | numpy/lib/function_base.py | 42 |
1 files changed, 22 insertions, 20 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 391c47a06..0434eb472 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -970,7 +970,7 @@ def gradient(f, *varargs, **kwargs): slice4[axis] = slice(2, None) if uniform_spacing: - out[slice1] = (f[slice4] - f[slice2]) / (2. * ax_dx) + out[tuple(slice1)] = (f[tuple(slice4)] - f[tuple(slice2)]) / (2. * ax_dx) else: dx1 = ax_dx[0:-1] dx2 = ax_dx[1:] @@ -982,7 +982,7 @@ def gradient(f, *varargs, **kwargs): shape[axis] = -1 a.shape = b.shape = c.shape = shape # 1D equivalent -- out[1:-1] = a * f[:-2] + b * f[1:-1] + c * f[2:] - out[slice1] = a * f[slice2] + b * f[slice3] + c * f[slice4] + out[tuple(slice1)] = a * f[tuple(slice2)] + b * f[tuple(slice3)] + c * f[tuple(slice4)] # Numerical differentiation: 1st order edges if edge_order == 1: @@ -991,14 +991,14 @@ def gradient(f, *varargs, **kwargs): slice3[axis] = 0 dx_0 = ax_dx if uniform_spacing else ax_dx[0] # 1D equivalent -- out[0] = (f[1] - f[0]) / (x[1] - x[0]) - out[slice1] = (f[slice2] - f[slice3]) / dx_0 + out[tuple(slice1)] = (f[tuple(slice2)] - f[tuple(slice3)]) / dx_0 slice1[axis] = -1 slice2[axis] = -1 slice3[axis] = -2 dx_n = ax_dx if uniform_spacing else ax_dx[-1] # 1D equivalent -- out[-1] = (f[-1] - f[-2]) / (x[-1] - x[-2]) - out[slice1] = (f[slice2] - f[slice3]) / dx_n + out[tuple(slice1)] = (f[tuple(slice2)] - f[tuple(slice3)]) / dx_n # Numerical differentiation: 2nd order edges else: @@ -1017,7 +1017,7 @@ def gradient(f, *varargs, **kwargs): b = (dx1 + dx2) / (dx1 * dx2) c = - dx1 / (dx2 * (dx1 + dx2)) # 1D equivalent -- out[0] = a * f[0] + b * f[1] + c * f[2] - out[slice1] = a * f[slice2] + b * f[slice3] + c * f[slice4] + out[tuple(slice1)] = a * f[tuple(slice2)] + b * f[tuple(slice3)] + c * f[tuple(slice4)] slice1[axis] = -1 slice2[axis] = -3 @@ -1034,7 +1034,7 @@ def gradient(f, *varargs, **kwargs): b = - (dx2 + dx1) / (dx1 * dx2) c = (2. * dx2 + dx1) / (dx2 * (dx1 + dx2)) # 1D equivalent -- out[-1] = a * f[-3] + b * f[-2] + c * f[-1] - out[slice1] = a * f[slice2] + b * f[slice3] + c * f[slice4] + out[tuple(slice1)] = a * f[tuple(slice2)] + b * f[tuple(slice3)] + c * f[tuple(slice4)] outvals.append(out) @@ -1385,6 +1385,7 @@ def unwrap(p, discont=pi, axis=-1): dd = diff(p, axis=axis) slice1 = [slice(None, None)]*nd # full slices slice1[axis] = slice(1, None) + slice1 = tuple(slice1) ddmod = mod(dd + pi, 2*pi) - pi _nx.copyto(ddmod, pi, where=(ddmod == -pi) & (dd > 0)) ph_correct = ddmod - dd @@ -3367,6 +3368,7 @@ def _median(a, axis=None, out=None, overwrite_input=False): indexer[axis] = slice(index, index+1) else: indexer[axis] = slice(index-1, index+1) + indexer = tuple(indexer) # Check if the array contains any nan's if np.issubdtype(a.dtype, np.inexact) and sz > 0: @@ -3732,12 +3734,12 @@ def trapz(y, x=None, dx=1.0, axis=-1): slice1[axis] = slice(1, None) slice2[axis] = slice(None, -1) try: - ret = (d * (y[slice1] + y[slice2]) / 2.0).sum(axis) + ret = (d * (y[tuple(slice1)] + y[tuple(slice2)]) / 2.0).sum(axis) except ValueError: # Operations didn't work, cast to ndarray d = np.asarray(d) y = np.asarray(y) - ret = add.reduce(d * (y[slice1]+y[slice2])/2.0, axis) + ret = add.reduce(d * (y[tuple(slice1)]+y[tuple(slice2)])/2.0, axis) return ret @@ -4026,7 +4028,7 @@ def delete(arr, obj, axis=None): pass else: slobj[axis] = slice(None, start) - new[slobj] = arr[slobj] + new[tuple(slobj)] = arr[tuple(slobj)] # copy end chunck if stop == N: pass @@ -4034,7 +4036,7 @@ def delete(arr, obj, axis=None): slobj[axis] = slice(stop-numtodel, None) slobj2 = [slice(None)]*ndim slobj2[axis] = slice(stop, None) - new[slobj] = arr[slobj2] + new[tuple(slobj)] = arr[tuple(slobj2)] # copy middle pieces if step == 1: pass @@ -4044,9 +4046,9 @@ def delete(arr, obj, axis=None): slobj[axis] = slice(start, stop-numtodel) slobj2 = [slice(None)]*ndim slobj2[axis] = slice(start, stop) - arr = arr[slobj2] + arr = arr[tuple(slobj2)] slobj2[axis] = keep - new[slobj] = arr[slobj2] + new[tuple(slobj)] = arr[tuple(slobj2)] if wrap: return wrap(new) else: @@ -4073,11 +4075,11 @@ def delete(arr, obj, axis=None): newshape[axis] -= 1 new = empty(newshape, arr.dtype, arrorder) slobj[axis] = slice(None, obj) - new[slobj] = arr[slobj] + new[tuple(slobj)] = arr[tuple(slobj)] slobj[axis] = slice(obj, None) slobj2 = [slice(None)]*ndim slobj2[axis] = slice(obj+1, None) - new[slobj] = arr[slobj2] + new[tuple(slobj)] = arr[tuple(slobj2)] else: if obj.size == 0 and not isinstance(_obj, np.ndarray): obj = obj.astype(intp) @@ -4109,7 +4111,7 @@ def delete(arr, obj, axis=None): keep[obj, ] = False slobj[axis] = keep - new = arr[slobj] + new = arr[tuple(slobj)] if wrap: return wrap(new) @@ -4280,13 +4282,13 @@ def insert(arr, obj, values, axis=None): newshape[axis] += numnew new = empty(newshape, arr.dtype, arrorder) slobj[axis] = slice(None, index) - new[slobj] = arr[slobj] + new[tuple(slobj)] = arr[tuple(slobj)] slobj[axis] = slice(index, index+numnew) - new[slobj] = values + new[tuple(slobj)] = values slobj[axis] = slice(index+numnew, None) slobj2 = [slice(None)] * ndim slobj2[axis] = slice(index, None) - new[slobj] = arr[slobj2] + new[tuple(slobj)] = arr[tuple(slobj2)] if wrap: return wrap(new) return new @@ -4315,8 +4317,8 @@ def insert(arr, obj, values, axis=None): slobj2 = [slice(None)]*ndim slobj[axis] = indices slobj2[axis] = old_mask - new[slobj] = values - new[slobj2] = arr + new[tuple(slobj)] = values + new[tuple(slobj2)] = arr if wrap: return wrap(new) |