summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarten van Kerkwijk <mhvk@astro.utoronto.ca>2018-03-15 22:34:26 (GMT)
committerGitHub <noreply@github.com>2018-03-15 22:34:26 (GMT)
commit01541f2822d0d4b37b96f6b42e35963b132f1947 (patch)
treebab227db0f97f70cd02f1d22b7b0c19726b7fbff
parentfc26f49cca51207596862ad2df986d03ec56b19b (diff)
parente441c291b2e10c8de85a9d950d0add552d0ebd83 (diff)
downloadpython-numpy-01541f2822d0d4b37b96f6b42e35963b132f1947.zip
python-numpy-01541f2822d0d4b37b96f6b42e35963b132f1947.tar.gz
python-numpy-01541f2822d0d4b37b96f6b42e35963b132f1947.tar.bz2
Merge pull request #10618 from eric-wieser/avoid-nontuple-indices
MAINT: Stop using non-tuple indices internally
-rw-r--r--numpy/core/tests/test_indexing.py2
-rw-r--r--numpy/core/tests/test_memmap.py2
-rw-r--r--numpy/fft/fftpack.py4
-rw-r--r--numpy/lib/arraypad.py6
-rw-r--r--numpy/lib/function_base.py42
-rw-r--r--numpy/lib/histograms.py2
-rw-r--r--numpy/ma/core.py1
-rw-r--r--numpy/ma/extras.py3
8 files changed, 33 insertions, 29 deletions
diff --git a/numpy/core/tests/test_indexing.py b/numpy/core/tests/test_indexing.py
index 26882c5..3a02c9f 100644
--- a/numpy/core/tests/test_indexing.py
+++ b/numpy/core/tests/test_indexing.py
@@ -499,7 +499,7 @@ class TestIndexing(object):
arro = np.zeros((4, 4))
arr = arro[::-1, ::-1]
- slices = [slice(None), [0, 1, 2, 3]]
+ slices = (slice(None), [0, 1, 2, 3])
arr[slices] = 10
assert_array_equal(arr, 10.)
diff --git a/numpy/core/tests/test_memmap.py b/numpy/core/tests/test_memmap.py
index 1cd09ab..c00867c 100644
--- a/numpy/core/tests/test_memmap.py
+++ b/numpy/core/tests/test_memmap.py
@@ -126,7 +126,7 @@ class TestMemmap(object):
def test_indexing_drops_references(self):
fp = memmap(self.tmpfp, dtype=self.dtype, mode='w+',
shape=self.shape)
- tmp = fp[[(1, 2), (2, 3)]]
+ tmp = fp[(1, 2), (2, 3)]
if isinstance(tmp, memmap):
assert_(tmp._mmap is not fp._mmap)
diff --git a/numpy/fft/fftpack.py b/numpy/fft/fftpack.py
index bd116b9..e17e1cb 100644
--- a/numpy/fft/fftpack.py
+++ b/numpy/fft/fftpack.py
@@ -69,13 +69,13 @@ def _raw_fft(a, n=None, axis=-1, init_function=fftpack.cffti,
if s[axis] > n:
index = [slice(None)]*len(s)
index[axis] = slice(0, n)
- a = a[index]
+ a = a[tuple(index)]
else:
index = [slice(None)]*len(s)
index[axis] = slice(0, s[axis])
s[axis] = n
z = zeros(s, a.dtype.char)
- z[index] = a
+ z[tuple(index)] = a
a = z
if axis != -1:
diff --git a/numpy/lib/arraypad.py b/numpy/lib/arraypad.py
index cdc354a..daaa68d 100644
--- a/numpy/lib/arraypad.py
+++ b/numpy/lib/arraypad.py
@@ -1346,9 +1346,9 @@ def pad(array, pad_width, mode, **kwargs):
# Create a new padded array
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])
- for i in rank]
+ offset_slices = tuple(
+ slice(pad_width[i][0], pad_width[i][0] + narray.shape[i])
+ for i in rank)
new_shape = np.array(narray.shape) + total_dim_increase
newmat = np.zeros(new_shape, narray.dtype)
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py
index bff3798..1eb197c 100644
--- a/numpy/lib/function_base.py
+++ b/numpy/lib/function_base.py
@@ -975,7 +975,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:]
@@ -987,7 +987,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:
@@ -996,14 +996,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:
@@ -1022,7 +1022,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
@@ -1039,7 +1039,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)
@@ -1377,6 +1377,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
@@ -3359,6 +3360,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:
@@ -3751,12 +3753,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
@@ -4045,7 +4047,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
@@ -4053,7 +4055,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
@@ -4063,9 +4065,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:
@@ -4092,11 +4094,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)
@@ -4128,7 +4130,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)
@@ -4299,13 +4301,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
@@ -4334,8 +4336,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)
diff --git a/numpy/lib/histograms.py b/numpy/lib/histograms.py
index ccc6c06..cf40fcf 100644
--- a/numpy/lib/histograms.py
+++ b/numpy/lib/histograms.py
@@ -860,7 +860,7 @@ def histogramdd(sample, bins=10, range=None, normed=False, weights=None):
ni[i], ni[j] = ni[j], ni[i]
# Remove outliers (indices 0 and -1 for each dimension).
- core = D*[slice(1, -1)]
+ core = D*(slice(1, -1),)
hist = hist[core]
# Normalize if normed is True
diff --git a/numpy/ma/core.py b/numpy/ma/core.py
index 698d39c..6deff0e 100644
--- a/numpy/ma/core.py
+++ b/numpy/ma/core.py
@@ -5538,6 +5538,7 @@ class MaskedArray(ndarray):
else:
idx = list(np.ix_(*[np.arange(x) for x in self.shape]))
idx[axis] = sidx
+ idx = tuple(idx)
self[...] = self[idx]
diff --git a/numpy/ma/extras.py b/numpy/ma/extras.py
index 99f5234..e247fe1 100644
--- a/numpy/ma/extras.py
+++ b/numpy/ma/extras.py
@@ -724,6 +724,7 @@ def _median(a, axis=None, out=None, overwrite_input=False):
# as median (which is mean of empty slice = nan)
indexer = [slice(None)] * asorted.ndim
indexer[axis] = slice(0, 0)
+ indexer = tuple(indexer)
return np.ma.mean(asorted[indexer], axis=axis, out=out)
if asorted.ndim == 1:
@@ -1716,7 +1717,7 @@ def notmasked_contiguous(a, axis=None):
#
for i in range(a.shape[other]):
idx[other] = i
- result.append(flatnotmasked_contiguous(a[idx]) or None)
+ result.append(flatnotmasked_contiguous(a[tuple(idx)]) or None)
return result