diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/src/umath/loops.c.src | 20 | ||||
-rw-r--r-- | numpy/core/tests/test_umath_complex.py | 134 | ||||
-rw-r--r-- | numpy/core/umath_tests.py | 15 | ||||
-rw-r--r-- | numpy/lib/histograms.py | 55 | ||||
-rw-r--r-- | numpy/lib/tests/test_index_tricks.py | 136 | ||||
-rw-r--r-- | numpy/lib/tests/test_twodim_base.py | 57 | ||||
-rw-r--r-- | numpy/polynomial/tests/test_classes.py | 114 | ||||
-rw-r--r-- | numpy/testing/decorators.py | 8 | ||||
-rw-r--r-- | numpy/testing/noseclasses.py | 9 | ||||
-rw-r--r-- | numpy/testing/nosetester.py | 8 | ||||
-rw-r--r-- | numpy/testing/utils.py | 8 |
11 files changed, 296 insertions, 268 deletions
diff --git a/numpy/core/src/umath/loops.c.src b/numpy/core/src/umath/loops.c.src index c1dfe15da..d196a8d4e 100644 --- a/numpy/core/src/umath/loops.c.src +++ b/numpy/core/src/umath/loops.c.src @@ -30,6 +30,16 @@ */ #define PW_BLOCKSIZE 128 + +/* + * largest simd vector size in bytes numpy supports + * it is currently a extremely large value as it is only used for memory + * overlap checks + */ +#ifndef NPY_MAX_SIMD_SIZE +#define NPY_MAX_SIMD_SIZE 1024 +#endif + /* * include vectorized functions and dispatchers * this file is safe to include also for generic builds @@ -180,10 +190,12 @@ do { \ /* condition allows compiler to optimize the generic macro */ \ if (IS_BINARY_CONT(tin, tout)) { \ - if (args[2] == args[0]) { \ + if (abs_ptrdiff(args[2], args[0]) == 0 && \ + abs_ptrdiff(args[2], args[1]) >= NPY_MAX_SIMD_SIZE) { \ BASE_BINARY_LOOP_INP(tin, tout, op) \ } \ - else if (args[2] == args[1]) { \ + else if (abs_ptrdiff(args[2], args[1]) == 0 && \ + abs_ptrdiff(args[2], args[0]) >= NPY_MAX_SIMD_SIZE) { \ BASE_BINARY_LOOP_INP(tin, tout, op) \ } \ else { \ @@ -191,7 +203,7 @@ } \ } \ else if (IS_BINARY_CONT_S1(tin, tout)) { \ - if (args[1] == args[2]) { \ + if (abs_ptrdiff(args[2], args[1]) == 0) { \ BASE_BINARY_LOOP_S_INP(tin, tout, in1, args[0], in2, ip2, op) \ } \ else { \ @@ -199,7 +211,7 @@ } \ } \ else if (IS_BINARY_CONT_S2(tin, tout)) { \ - if (args[0] == args[2]) { \ + if (abs_ptrdiff(args[2], args[0]) == 0) { \ BASE_BINARY_LOOP_S_INP(tin, tout, in2, args[1], in1, ip1, op) \ } \ else { \ diff --git a/numpy/core/tests/test_umath_complex.py b/numpy/core/tests/test_umath_complex.py index 659a8a09e..785ae8c57 100644 --- a/numpy/core/tests/test_umath_complex.py +++ b/numpy/core/tests/test_umath_complex.py @@ -35,11 +35,11 @@ class TestCexp(object): check = check_complex_value f = np.exp - yield check, f, 1, 0, np.exp(1), 0, False - yield check, f, 0, 1, np.cos(1), np.sin(1), False + check(f, 1, 0, np.exp(1), 0, False) + check(f, 0, 1, np.cos(1), np.sin(1), False) ref = np.exp(1) * complex(np.cos(1), np.sin(1)) - yield check, f, 1, 1, ref.real, ref.imag, False + check(f, 1, 1, ref.real, ref.imag, False) @platform_skip def test_special_values(self): @@ -49,25 +49,25 @@ class TestCexp(object): f = np.exp # cexp(+-0 + 0i) is 1 + 0i - yield check, f, np.PZERO, 0, 1, 0, False - yield check, f, np.NZERO, 0, 1, 0, False + check(f, np.PZERO, 0, 1, 0, False) + check(f, np.NZERO, 0, 1, 0, False) # cexp(x + infi) is nan + nani for finite x and raises 'invalid' FPU # exception - yield check, f, 1, np.inf, np.nan, np.nan - yield check, f, -1, np.inf, np.nan, np.nan - yield check, f, 0, np.inf, np.nan, np.nan + check(f, 1, np.inf, np.nan, np.nan) + check(f, -1, np.inf, np.nan, np.nan) + check(f, 0, np.inf, np.nan, np.nan) # cexp(inf + 0i) is inf + 0i - yield check, f, np.inf, 0, np.inf, 0 + check(f, np.inf, 0, np.inf, 0) # cexp(-inf + yi) is +0 * (cos(y) + i sin(y)) for finite y - yield check, f, -np.inf, 1, np.PZERO, np.PZERO - yield check, f, -np.inf, 0.75 * np.pi, np.NZERO, np.PZERO + check(f, -np.inf, 1, np.PZERO, np.PZERO) + check(f, -np.inf, 0.75 * np.pi, np.NZERO, np.PZERO) # cexp(inf + yi) is +inf * (cos(y) + i sin(y)) for finite y - yield check, f, np.inf, 1, np.inf, np.inf - yield check, f, np.inf, 0.75 * np.pi, -np.inf, np.inf + check(f, np.inf, 1, np.inf, np.inf) + check(f, np.inf, 0.75 * np.pi, -np.inf, np.inf) # cexp(-inf + inf i) is +-0 +- 0i (signs unspecified) def _check_ninf_inf(dummy): @@ -77,7 +77,7 @@ class TestCexp(object): if z.real != 0 or z.imag != 0: raise AssertionError(msgform % (z.real, z.imag)) - yield _check_ninf_inf, None + _check_ninf_inf(None) # cexp(inf + inf i) is +-inf + NaNi and raised invalid FPU ex. def _check_inf_inf(dummy): @@ -87,7 +87,7 @@ class TestCexp(object): if not np.isinf(z.real) or not np.isnan(z.imag): raise AssertionError(msgform % (z.real, z.imag)) - yield _check_inf_inf, None + _check_inf_inf(None) # cexp(-inf + nan i) is +-0 +- 0i def _check_ninf_nan(dummy): @@ -97,7 +97,7 @@ class TestCexp(object): if z.real != 0 or z.imag != 0: raise AssertionError(msgform % (z.real, z.imag)) - yield _check_ninf_nan, None + _check_ninf_nan(None) # cexp(inf + nan i) is +-inf + nan def _check_inf_nan(dummy): @@ -107,18 +107,18 @@ class TestCexp(object): if not np.isinf(z.real) or not np.isnan(z.imag): raise AssertionError(msgform % (z.real, z.imag)) - yield _check_inf_nan, None + _check_inf_nan(None) # cexp(nan + yi) is nan + nani for y != 0 (optional: raises invalid FPU # ex) - yield check, f, np.nan, 1, np.nan, np.nan - yield check, f, np.nan, -1, np.nan, np.nan + check(f, np.nan, 1, np.nan, np.nan) + check(f, np.nan, -1, np.nan, np.nan) - yield check, f, np.nan, np.inf, np.nan, np.nan - yield check, f, np.nan, -np.inf, np.nan, np.nan + check(f, np.nan, np.inf, np.nan, np.nan) + check(f, np.nan, -np.inf, np.nan, np.nan) # cexp(nan + nani) is nan + nani - yield check, f, np.nan, np.nan, np.nan, np.nan + check(f, np.nan, np.nan, np.nan, np.nan) # TODO This can be xfail when the generator functions are got rid of. @pytest.mark.skip(reason="cexp(nan + 0I) is wrong on most platforms") @@ -274,24 +274,28 @@ class TestClog(object): for i in range(len(xa)): assert_almost_equal(np.log(xa[i].conj()), ya[i].conj()) + class TestCsqrt(object): def test_simple(self): # sqrt(1) - yield check_complex_value, np.sqrt, 1, 0, 1, 0 + check_complex_value(np.sqrt, 1, 0, 1, 0) # sqrt(1i) - yield check_complex_value, np.sqrt, 0, 1, 0.5*np.sqrt(2), 0.5*np.sqrt(2), False + rres = 0.5*np.sqrt(2) + ires = rres + check_complex_value(np.sqrt, 0, 1, rres, ires, False) # sqrt(-1) - yield check_complex_value, np.sqrt, -1, 0, 0, 1 + check_complex_value(np.sqrt, -1, 0, 0, 1) def test_simple_conjugate(self): ref = np.conj(np.sqrt(complex(1, 1))) def f(z): return np.sqrt(np.conj(z)) - yield check_complex_value, f, 1, 1, ref.real, ref.imag, False + + check_complex_value(f, 1, 1, ref.real, ref.imag, False) #def test_branch_cut(self): # _check_branch_cut(f, -1, 0, 1, -1) @@ -304,29 +308,29 @@ class TestCsqrt(object): f = np.sqrt # csqrt(+-0 + 0i) is 0 + 0i - yield check, f, np.PZERO, 0, 0, 0 - yield check, f, np.NZERO, 0, 0, 0 + check(f, np.PZERO, 0, 0, 0) + check(f, np.NZERO, 0, 0, 0) # csqrt(x + infi) is inf + infi for any x (including NaN) - yield check, f, 1, np.inf, np.inf, np.inf - yield check, f, -1, np.inf, np.inf, np.inf + check(f, 1, np.inf, np.inf, np.inf) + check(f, -1, np.inf, np.inf, np.inf) - yield check, f, np.PZERO, np.inf, np.inf, np.inf - yield check, f, np.NZERO, np.inf, np.inf, np.inf - yield check, f, np.inf, np.inf, np.inf, np.inf - yield check, f, -np.inf, np.inf, np.inf, np.inf - yield check, f, -np.nan, np.inf, np.inf, np.inf + check(f, np.PZERO, np.inf, np.inf, np.inf) + check(f, np.NZERO, np.inf, np.inf, np.inf) + check(f, np.inf, np.inf, np.inf, np.inf) + check(f, -np.inf, np.inf, np.inf, np.inf) + check(f, -np.nan, np.inf, np.inf, np.inf) # csqrt(x + nani) is nan + nani for any finite x - yield check, f, 1, np.nan, np.nan, np.nan - yield check, f, -1, np.nan, np.nan, np.nan - yield check, f, 0, np.nan, np.nan, np.nan + check(f, 1, np.nan, np.nan, np.nan) + check(f, -1, np.nan, np.nan, np.nan) + check(f, 0, np.nan, np.nan, np.nan) # csqrt(-inf + yi) is +0 + infi for any finite y > 0 - yield check, f, -np.inf, 1, np.PZERO, np.inf + check(f, -np.inf, 1, np.PZERO, np.inf) # csqrt(inf + yi) is +inf + 0i for any finite y > 0 - yield check, f, np.inf, 1, np.inf, np.PZERO + check(f, np.inf, 1, np.inf, np.PZERO) # csqrt(-inf + nani) is nan +- infi (both +i infi are valid) def _check_ninf_nan(dummy): @@ -337,16 +341,16 @@ class TestCsqrt(object): if not (np.isnan(z.real) and np.isinf(z.imag)): raise AssertionError(msgform % (z.real, z.imag)) - yield _check_ninf_nan, None + _check_ninf_nan(None) # csqrt(+inf + nani) is inf + nani - yield check, f, np.inf, np.nan, np.inf, np.nan + check(f, np.inf, np.nan, np.inf, np.nan) # csqrt(nan + yi) is nan + nani for any finite y (infinite handled in x # + nani) - yield check, f, np.nan, 0, np.nan, np.nan - yield check, f, np.nan, 1, np.nan, np.nan - yield check, f, np.nan, np.nan, np.nan, np.nan + check(f, np.nan, 0, np.nan, np.nan) + check(f, np.nan, 1, np.nan, np.nan) + check(f, np.nan, np.nan, np.nan, np.nan) # XXX: check for conj(csqrt(z)) == csqrt(conj(z)) (need to fix branch # cuts first) @@ -425,21 +429,21 @@ class TestCabs(object): # cabs(+-nan + nani) returns nan x.append(np.nan) y.append(np.nan) - yield check_real_value, np.abs, np.nan, np.nan, np.nan + check_real_value(np.abs, np.nan, np.nan, np.nan) x.append(np.nan) y.append(-np.nan) - yield check_real_value, np.abs, -np.nan, np.nan, np.nan + check_real_value(np.abs, -np.nan, np.nan, np.nan) # According to C99 standard, if exactly one of the real/part is inf and # the other nan, then cabs should return inf x.append(np.inf) y.append(np.nan) - yield check_real_value, np.abs, np.inf, np.nan, np.inf + check_real_value(np.abs, np.inf, np.nan, np.inf) x.append(-np.inf) y.append(np.nan) - yield check_real_value, np.abs, -np.inf, np.nan, np.inf + check_real_value(np.abs, -np.inf, np.nan, np.inf) # cabs(conj(z)) == conj(cabs(z)) (= cabs(z)) def f(a): @@ -451,7 +455,7 @@ class TestCabs(object): xa = np.array(x, dtype=complex) for i in range(len(xa)): ref = g(x[i], y[i]) - yield check_real_value, f, x[i], y[i], ref + check_real_value(f, x[i], y[i], ref) class TestCarg(object): def test_simple(self): @@ -494,31 +498,32 @@ class TestCarg(object): def test_special_values(self): # carg(-np.inf +- yi) returns +-pi for finite y > 0 - yield check_real_value, ncu._arg, -np.inf, 1, np.pi, False - yield check_real_value, ncu._arg, -np.inf, -1, -np.pi, False + check_real_value(ncu._arg, -np.inf, 1, np.pi, False) + check_real_value(ncu._arg, -np.inf, -1, -np.pi, False) # carg(np.inf +- yi) returns +-0 for finite y > 0 - yield check_real_value, ncu._arg, np.inf, 1, np.PZERO, False - yield check_real_value, ncu._arg, np.inf, -1, np.NZERO, False + check_real_value(ncu._arg, np.inf, 1, np.PZERO, False) + check_real_value(ncu._arg, np.inf, -1, np.NZERO, False) # carg(x +- np.infi) returns +-pi/2 for finite x - yield check_real_value, ncu._arg, 1, np.inf, 0.5 * np.pi, False - yield check_real_value, ncu._arg, 1, -np.inf, -0.5 * np.pi, False + check_real_value(ncu._arg, 1, np.inf, 0.5 * np.pi, False) + check_real_value(ncu._arg, 1, -np.inf, -0.5 * np.pi, False) # carg(-np.inf +- np.infi) returns +-3pi/4 - yield check_real_value, ncu._arg, -np.inf, np.inf, 0.75 * np.pi, False - yield check_real_value, ncu._arg, -np.inf, -np.inf, -0.75 * np.pi, False + check_real_value(ncu._arg, -np.inf, np.inf, 0.75 * np.pi, False) + check_real_value(ncu._arg, -np.inf, -np.inf, -0.75 * np.pi, False) # carg(np.inf +- np.infi) returns +-pi/4 - yield check_real_value, ncu._arg, np.inf, np.inf, 0.25 * np.pi, False - yield check_real_value, ncu._arg, np.inf, -np.inf, -0.25 * np.pi, False + check_real_value(ncu._arg, np.inf, np.inf, 0.25 * np.pi, False) + check_real_value(ncu._arg, np.inf, -np.inf, -0.25 * np.pi, False) # carg(x + yi) returns np.nan if x or y is nan - yield check_real_value, ncu._arg, np.nan, 0, np.nan, False - yield check_real_value, ncu._arg, 0, np.nan, np.nan, False + check_real_value(ncu._arg, np.nan, 0, np.nan, False) + check_real_value(ncu._arg, 0, np.nan, np.nan, False) + + check_real_value(ncu._arg, np.nan, np.inf, np.nan, False) + check_real_value(ncu._arg, np.inf, np.nan, np.nan, False) - yield check_real_value, ncu._arg, np.nan, np.inf, np.nan, False - yield check_real_value, ncu._arg, np.inf, np.nan, np.nan, False def check_real_value(f, x1, y1, x, exact=True): z1 = np.array([complex(x1, y1)]) @@ -527,6 +532,7 @@ def check_real_value(f, x1, y1, x, exact=True): else: assert_almost_equal(f(z1), x) + def check_complex_value(f, x1, y1, x2, y2, exact=True): z1 = np.array([complex(x1, y1)]) z2 = complex(x2, y2) diff --git a/numpy/core/umath_tests.py b/numpy/core/umath_tests.py new file mode 100644 index 000000000..28e325b98 --- /dev/null +++ b/numpy/core/umath_tests.py @@ -0,0 +1,15 @@ +""" +Shim for _umath_tests to allow a deprecation period for the new name. + +""" +from __future__ import division, absolute_import, print_function + +import warnings + +# 2018-04-04, numpy 1.15.0 +warnings.warn(("numpy.core.umath_tests is an internal NumPy " + "module and should not be imported. It will " + "be removed in a future NumPy release."), + category=DeprecationWarning, stacklevel=2) + +from ._umath_tests import * diff --git a/numpy/lib/histograms.py b/numpy/lib/histograms.py index 66e2ccda1..a0346f6c5 100644 --- a/numpy/lib/histograms.py +++ b/numpy/lib/histograms.py @@ -10,6 +10,10 @@ from numpy.compat.py3k import basestring __all__ = ['histogram', 'histogramdd', 'histogram_bin_edges'] +# range is a keyword argument to many functions, so save the builtin so they can +# use it. +_range = range + def _hist_bin_sqrt(x): """ @@ -693,7 +697,7 @@ def histogram(a, bins=10, range=None, normed=False, weights=None, # large arrays, it is actually faster (for example for a 10^8 array it # is 2x as fast) and it results in a memory footprint 3x lower in the # limit of large arrays. - for i in np.arange(0, len(a), BLOCK): + for i in _range(0, len(a), BLOCK): tmp_a = a[i:i+BLOCK] if weights is None: tmp_w = None @@ -741,12 +745,12 @@ def histogram(a, bins=10, range=None, normed=False, weights=None, # Compute via cumulative histogram cum_n = np.zeros(bin_edges.shape, ntype) if weights is None: - for i in np.arange(0, len(a), BLOCK): + for i in _range(0, len(a), BLOCK): sa = np.sort(a[i:i+BLOCK]) cum_n += _search_sorted_inclusive(sa, bin_edges) else: zero = np.zeros(1, dtype=ntype) - for i in np.arange(0, len(a), BLOCK): + for i in _range(0, len(a), BLOCK): tmp_a = a[i:i+BLOCK] tmp_w = weights[i:i+BLOCK] sorting_index = np.argsort(tmp_a) @@ -873,7 +877,7 @@ def histogramdd(sample, bins=10, range=None, normed=False, weights=None): raise ValueError('range argument must have one entry per dimension') # Create edge arrays - for i in np.arange(D): + for i in _range(D): if np.ndim(bins[i]) == 0: if bins[i] < 1: raise ValueError( @@ -894,21 +898,20 @@ def histogramdd(sample, bins=10, range=None, normed=False, weights=None): nbin[i] = len(edges[i]) + 1 # includes an outlier on each end dedges[i] = np.diff(edges[i]) - nbin = np.asarray(nbin) - # Handle empty input. if N == 0: return np.zeros(nbin-2), edges # Compute the bin number each sample falls into. - Ncount = {} - for i in np.arange(D): - Ncount[i] = np.digitize(sample[:, i], edges[i]) + Ncount = tuple( + np.digitize(sample[:, i], edges[i]) + for i in _range(D) + ) # Using digitize, values that fall on an edge are put in the right bin. # For the rightmost bin, we want values equal to the right edge to be # counted in the last bin, and not as an outlier. - for i in np.arange(D): + for i in _range(D): # Rounding precision mindiff = dedges[i].min() if not np.isinf(mindiff): @@ -918,35 +921,21 @@ def histogramdd(sample, bins=10, range=None, normed=False, weights=None): on_edge = (np.around(sample[:, i], decimal) == np.around(edges[i][-1], decimal)) # Shift these points one bin to the left. - Ncount[i][np.nonzero(on_edge & not_smaller_than_edge)[0]] -= 1 - - # Flattened histogram matrix (1D) - # Reshape is used so that overlarge arrays - # will raise an error. - hist = np.zeros(nbin, float).reshape(-1) + Ncount[i][on_edge & not_smaller_than_edge] -= 1 # Compute the sample indices in the flattened histogram matrix. - ni = nbin.argsort() - xy = np.zeros(N, np.intp) - for i in np.arange(0, D-1): - xy += Ncount[ni[i]] * nbin[ni[i+1:]].prod() - xy += Ncount[ni[-1]] + # This raises an error if the array is too large. + xy = np.ravel_multi_index(Ncount, nbin) # Compute the number of repetitions in xy and assign it to the # flattened histmat. - if len(xy) == 0: - return np.zeros(nbin-2, int), edges - - flatcount = np.bincount(xy, weights) - a = np.arange(len(flatcount)) - hist[a] = flatcount + hist = np.bincount(xy, weights, minlength=nbin.prod()) # Shape into a proper matrix - hist = hist.reshape(np.sort(nbin)) - for i in np.arange(nbin.size): - j = ni.argsort()[i] - hist = hist.swapaxes(i, j) - ni[i], ni[j] = ni[j], ni[i] + hist = hist.reshape(nbin) + + # This preserves the (bad) behavior observed in gh-7845, for now. + hist = hist.astype(float, casting='safe') # Remove outliers (indices 0 and -1 for each dimension). core = D*(slice(1, -1),) @@ -955,7 +944,7 @@ def histogramdd(sample, bins=10, range=None, normed=False, weights=None): # Normalize if normed is True if normed: s = hist.sum() - for i in np.arange(D): + for i in _range(D): shape = np.ones(D, int) shape[i] = nbin[i] - 2 hist = hist / dedges[i].reshape(shape) diff --git a/numpy/lib/tests/test_index_tricks.py b/numpy/lib/tests/test_index_tricks.py index a19f3f1be..f934e952a 100644 --- a/numpy/lib/tests/test_index_tricks.py +++ b/numpy/lib/tests/test_index_tricks.py @@ -284,71 +284,77 @@ def test_c_(): assert_equal(a, [[1, 2, 3, 0, 0, 4, 5, 6]]) -def test_fill_diagonal(): - a = np.zeros((3, 3), int) - fill_diagonal(a, 5) - yield (assert_array_equal, a, - np.array([[5, 0, 0], - [0, 5, 0], - [0, 0, 5]])) - - #Test tall matrix - a = np.zeros((10, 3), int) - fill_diagonal(a, 5) - yield (assert_array_equal, a, - np.array([[5, 0, 0], - [0, 5, 0], - [0, 0, 5], - [0, 0, 0], - [0, 0, 0], - [0, 0, 0], - [0, 0, 0], - [0, 0, 0], - [0, 0, 0], - [0, 0, 0]])) - - #Test tall matrix wrap - a = np.zeros((10, 3), int) - fill_diagonal(a, 5, True) - yield (assert_array_equal, a, - np.array([[5, 0, 0], - [0, 5, 0], - [0, 0, 5], - [0, 0, 0], - [5, 0, 0], - [0, 5, 0], - [0, 0, 5], - [0, 0, 0], - [5, 0, 0], - [0, 5, 0]])) - - #Test wide matrix - a = np.zeros((3, 10), int) - fill_diagonal(a, 5) - yield (assert_array_equal, a, - np.array([[5, 0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 5, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 5, 0, 0, 0, 0, 0, 0, 0]])) - - # The same function can operate on a 4-d array: - a = np.zeros((3, 3, 3, 3), int) - fill_diagonal(a, 4) - i = np.array([0, 1, 2]) - yield (assert_equal, np.where(a != 0), (i, i, i, i)) +class TestFillDiagonal(object): + def test_basic(self): + a = np.zeros((3, 3), int) + fill_diagonal(a, 5) + assert_array_equal( + a, np.array([[5, 0, 0], + [0, 5, 0], + [0, 0, 5]]) + ) + + def test_tall_matrix(self): + a = np.zeros((10, 3), int) + fill_diagonal(a, 5) + assert_array_equal( + a, np.array([[5, 0, 0], + [0, 5, 0], + [0, 0, 5], + [0, 0, 0], + [0, 0, 0], + [0, 0, 0], + [0, 0, 0], + [0, 0, 0], + [0, 0, 0], + [0, 0, 0]]) + ) + + def test_tall_matrix_wrap(self): + a = np.zeros((10, 3), int) + fill_diagonal(a, 5, True) + assert_array_equal( + a, np.array([[5, 0, 0], + [0, 5, 0], + [0, 0, 5], + [0, 0, 0], + [5, 0, 0], + [0, 5, 0], + [0, 0, 5], + [0, 0, 0], + [5, 0, 0], + [0, 5, 0]]) + ) + + def test_wide_matrix(self): + a = np.zeros((3, 10), int) + fill_diagonal(a, 5) + assert_array_equal( + a, np.array([[5, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 5, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 5, 0, 0, 0, 0, 0, 0, 0]]) + ) + + def test_operate_4d_array(self): + a = np.zeros((3, 3, 3, 3), int) + fill_diagonal(a, 4) + i = np.array([0, 1, 2]) + assert_equal(np.where(a != 0), (i, i, i, i)) def test_diag_indices(): di = diag_indices(4) a = np.array([[1, 2, 3, 4], - [5, 6, 7, 8], - [9, 10, 11, 12], - [13, 14, 15, 16]]) + [5, 6, 7, 8], + [9, 10, 11, 12], + [13, 14, 15, 16]]) a[di] = 100 - yield (assert_array_equal, a, - np.array([[100, 2, 3, 4], - [5, 100, 7, 8], - [9, 10, 100, 12], - [13, 14, 15, 100]])) + assert_array_equal( + a, np.array([[100, 2, 3, 4], + [5, 100, 7, 8], + [9, 10, 100, 12], + [13, 14, 15, 100]]) + ) # Now, we create indices to manipulate a 3-d array: d3 = diag_indices(2, 3) @@ -356,12 +362,12 @@ def test_diag_indices(): # And use it to set the diagonal of a zeros array to 1: a = np.zeros((2, 2, 2), int) a[d3] = 1 - yield (assert_array_equal, a, - np.array([[[1, 0], - [0, 0]], - - [[0, 0], - [0, 1]]])) + assert_array_equal( + a, np.array([[[1, 0], + [0, 0]], + [[0, 0], + [0, 1]]]) + ) def test_diag_indices_from(): diff --git a/numpy/lib/tests/test_twodim_base.py b/numpy/lib/tests/test_twodim_base.py index 6e0fe0654..d3a072af3 100644 --- a/numpy/lib/tests/test_twodim_base.py +++ b/numpy/lib/tests/test_twodim_base.py @@ -244,32 +244,32 @@ class TestHistogram2d(object): def test_binparameter_combination(self): x = array( - [0, 0.09207008, 0.64575234, 0.12875982, 0.47390599, + [0, 0.09207008, 0.64575234, 0.12875982, 0.47390599, 0.59944483, 1]) y = array( - [0, 0.14344267, 0.48988575, 0.30558665, 0.44700682, + [0, 0.14344267, 0.48988575, 0.30558665, 0.44700682, 0.15886423, 1]) edges = (0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1) H, xe, ye = histogram2d(x, y, (edges, 4)) answer = array( - [[ 2., 0., 0., 0.], - [ 0., 1., 0., 0.], - [ 0., 0., 0., 0.], - [ 0., 0., 0., 0.], - [ 0., 1., 0., 0.], - [ 1., 0., 0., 0.], - [ 0., 1., 0., 0.], - [ 0., 0., 0., 0.], - [ 0., 0., 0., 0.], - [ 0., 0., 0., 1.]]) + [[2., 0., 0., 0.], + [0., 1., 0., 0.], + [0., 0., 0., 0.], + [0., 0., 0., 0.], + [0., 1., 0., 0.], + [1., 0., 0., 0.], + [0., 1., 0., 0.], + [0., 0., 0., 0.], + [0., 0., 0., 0.], + [0., 0., 0., 1.]]) assert_array_equal(H, answer) assert_array_equal(ye, array([0., 0.25, 0.5, 0.75, 1])) H, xe, ye = histogram2d(x, y, (4, edges)) answer = array( - [[ 1., 1., 0., 1., 0., 0., 0., 0., 0., 0.], - [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], - [ 0., 1., 0., 0., 1., 0., 0., 0., 0., 0.], - [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]]) + [[1., 1., 0., 1., 0., 0., 0., 0., 0., 0.], + [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], + [0., 1., 0., 0., 1., 0., 0., 0., 0., 0.], + [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]]) assert_array_equal(H, answer) assert_array_equal(xe, array([0., 0.25, 0.5, 0.75, 1])) @@ -288,11 +288,11 @@ def test_tril_triu_ndim2(): a = np.ones((2, 2), dtype=dtype) b = np.tril(a) c = np.triu(a) - yield assert_array_equal, b, [[1, 0], [1, 1]] - yield assert_array_equal, c, b.T + assert_array_equal(b, [[1, 0], [1, 1]]) + assert_array_equal(c, b.T) # should return the same dtype as the original array - yield assert_equal, b.dtype, a.dtype - yield assert_equal, c.dtype, a.dtype + assert_equal(b.dtype, a.dtype) + assert_equal(c.dtype, a.dtype) def test_tril_triu_ndim3(): @@ -314,10 +314,11 @@ def test_tril_triu_ndim3(): ], dtype=dtype) a_triu_observed = np.triu(a) a_tril_observed = np.tril(a) - yield assert_array_equal, a_triu_observed, a_triu_desired - yield assert_array_equal, a_tril_observed, a_tril_desired - yield assert_equal, a_triu_observed.dtype, a.dtype - yield assert_equal, a_tril_observed.dtype, a.dtype + assert_array_equal(a_triu_observed, a_triu_desired) + assert_array_equal(a_tril_observed, a_tril_desired) + assert_equal(a_triu_observed.dtype, a.dtype) + assert_equal(a_tril_observed.dtype, a.dtype) + def test_tril_triu_with_inf(): # Issue 4859 @@ -484,12 +485,12 @@ class TestVander(object): [16, -8, 4, -2, 1], [81, 27, 9, 3, 1]]) # Check default value of N: - yield (assert_array_equal, v, powers[:, 1:]) + assert_array_equal(v, powers[:, 1:]) # Check a range of N values, including 0 and 5 (greater than default) m = powers.shape[1] for n in range(6): v = vander(c, N=n) - yield (assert_array_equal, v, powers[:, m-n:m]) + assert_array_equal(v, powers[:, m-n:m]) def test_dtypes(self): c = array([11, -12, 13], dtype=np.int8) @@ -497,7 +498,7 @@ class TestVander(object): expected = np.array([[121, 11, 1], [144, -12, 1], [169, 13, 1]]) - yield (assert_array_equal, v, expected) + assert_array_equal(v, expected) c = array([1.0+1j, 1.0-1j]) v = vander(c, N=3) @@ -506,4 +507,4 @@ class TestVander(object): # The data is floating point, but the values are small integers, # so assert_array_equal *should* be safe here (rather than, say, # assert_array_almost_equal). - yield (assert_array_equal, v, expected) + assert_array_equal(v, expected) diff --git a/numpy/polynomial/tests/test_classes.py b/numpy/polynomial/tests/test_classes.py index 262de5fe2..738741668 100644 --- a/numpy/polynomial/tests/test_classes.py +++ b/numpy/polynomial/tests/test_classes.py @@ -8,6 +8,7 @@ from __future__ import division, absolute_import, print_function import operator as op from numbers import Number +import pytest import numpy as np from numpy.polynomial import ( Polynomial, Legendre, Chebyshev, Laguerre, Hermite, HermiteE) @@ -17,44 +18,19 @@ from numpy.testing import ( from numpy.compat import long +# +# fixtures +# + classes = ( Polynomial, Legendre, Chebyshev, Laguerre, - Hermite, HermiteE) - - -def test_class_methods(): - for Poly1 in classes: - for Poly2 in classes: - yield check_conversion, Poly1, Poly2 - yield check_cast, Poly1, Poly2 - for Poly in classes: - yield check_call, Poly - yield check_identity, Poly - yield check_basis, Poly - yield check_fromroots, Poly - yield check_fit, Poly - yield check_equal, Poly - yield check_not_equal, Poly - yield check_add, Poly - yield check_sub, Poly - yield check_mul, Poly - yield check_floordiv, Poly - yield check_truediv, Poly - yield check_mod, Poly - yield check_divmod, Poly - yield check_pow, Poly - yield check_integ, Poly - yield check_deriv, Poly - yield check_roots, Poly - yield check_linspace, Poly - yield check_mapparms, Poly - yield check_degree, Poly - yield check_copy, Poly - yield check_cutdeg, Poly - yield check_truncate, Poly - yield check_trim, Poly - yield check_ufunc_override, Poly + Hermite, HermiteE + ) +classids = tuple(cls.__name__ for cls in classes) +@pytest.fixture(params=classes, ids=classids) +def Poly(request): + return request.param # # helper functions @@ -73,11 +49,14 @@ def assert_poly_almost_equal(p1, p2, msg=""): # -# conversion methods that depend on two classes +# Test conversion methods that depend on combinations of two classes. # +Poly1 = Poly +Poly2 = Poly + -def check_conversion(Poly1, Poly2): +def test_conversion(Poly1, Poly2): x = np.linspace(0, 1, 10) coef = random((3,)) @@ -94,7 +73,7 @@ def check_conversion(Poly1, Poly2): assert_almost_equal(p2(x), p1(x)) -def check_cast(Poly1, Poly2): +def test_cast(Poly1, Poly2): x = np.linspace(0, 1, 10) coef = random((3,)) @@ -112,11 +91,11 @@ def check_cast(Poly1, Poly2): # -# methods that depend on one class +# test methods that depend on one class # -def check_identity(Poly): +def test_identity(Poly): d = Poly.domain + random((2,))*.25 w = Poly.window + random((2,))*.25 x = np.linspace(d[0], d[1], 11) @@ -126,7 +105,7 @@ def check_identity(Poly): assert_almost_equal(p(x), x) -def check_basis(Poly): +def test_basis(Poly): d = Poly.domain + random((2,))*.25 w = Poly.window + random((2,))*.25 p = Poly.basis(5, domain=d, window=w) @@ -135,7 +114,7 @@ def check_basis(Poly): assert_equal(p.coef, [0]*5 + [1]) -def check_fromroots(Poly): +def test_fromroots(Poly): # check that requested roots are zeros of a polynomial # of correct degree, domain, and window. d = Poly.domain + random((2,))*.25 @@ -154,7 +133,7 @@ def check_fromroots(Poly): assert_almost_equal(p2.coef[-1], 1) -def check_fit(Poly): +def test_fit(Poly): def f(x): return x*(x - 1)*(x - 2) @@ -198,7 +177,7 @@ def check_fit(Poly): assert_almost_equal(p2(x), p3(x)) -def check_equal(Poly): +def test_equal(Poly): p1 = Poly([1, 2, 3], domain=[0, 1], window=[2, 3]) p2 = Poly([1, 1, 1], domain=[0, 1], window=[2, 3]) p3 = Poly([1, 2, 3], domain=[1, 2], window=[2, 3]) @@ -209,7 +188,7 @@ def check_equal(Poly): assert_(not p1 == p4) -def check_not_equal(Poly): +def test_not_equal(Poly): p1 = Poly([1, 2, 3], domain=[0, 1], window=[2, 3]) p2 = Poly([1, 1, 1], domain=[0, 1], window=[2, 3]) p3 = Poly([1, 2, 3], domain=[1, 2], window=[2, 3]) @@ -220,7 +199,7 @@ def check_not_equal(Poly): assert_(p1 != p4) -def check_add(Poly): +def test_add(Poly): # This checks commutation, not numerical correctness c1 = list(random((4,)) + .5) c2 = list(random((3,)) + .5) @@ -242,7 +221,7 @@ def check_add(Poly): assert_raises(TypeError, op.add, p1, Polynomial([0])) -def check_sub(Poly): +def test_sub(Poly): # This checks commutation, not numerical correctness c1 = list(random((4,)) + .5) c2 = list(random((3,)) + .5) @@ -264,7 +243,7 @@ def check_sub(Poly): assert_raises(TypeError, op.sub, p1, Polynomial([0])) -def check_mul(Poly): +def test_mul(Poly): c1 = list(random((4,)) + .5) c2 = list(random((3,)) + .5) p1 = Poly(c1) @@ -287,7 +266,7 @@ def check_mul(Poly): assert_raises(TypeError, op.mul, p1, Polynomial([0])) -def check_floordiv(Poly): +def test_floordiv(Poly): c1 = list(random((4,)) + .5) c2 = list(random((3,)) + .5) c3 = list(random((2,)) + .5) @@ -315,7 +294,7 @@ def check_floordiv(Poly): assert_raises(TypeError, op.floordiv, p1, Polynomial([0])) -def check_truediv(Poly): +def test_truediv(Poly): # true division is valid only if the denominator is a Number and # not a python bool. p1 = Poly([1,2,3]) @@ -342,7 +321,7 @@ def check_truediv(Poly): assert_raises(TypeError, op.truediv, p2, ptype(1)) -def check_mod(Poly): +def test_mod(Poly): # This checks commutation, not numerical correctness c1 = list(random((4,)) + .5) c2 = list(random((3,)) + .5) @@ -369,7 +348,7 @@ def check_mod(Poly): assert_raises(TypeError, op.mod, p1, Polynomial([0])) -def check_divmod(Poly): +def test_divmod(Poly): # This checks commutation, not numerical correctness c1 = list(random((4,)) + .5) c2 = list(random((3,)) + .5) @@ -414,7 +393,7 @@ def check_divmod(Poly): assert_raises(TypeError, divmod, p1, Polynomial([0])) -def check_roots(Poly): +def test_roots(Poly): d = Poly.domain * 1.25 + .25 w = Poly.window tgt = np.linspace(d[0], d[1], 5) @@ -425,12 +404,12 @@ def check_roots(Poly): assert_almost_equal(res, tgt) -def check_degree(Poly): +def test_degree(Poly): p = Poly.basis(5) assert_equal(p.degree(), 5) -def check_copy(Poly): +def test_copy(Poly): p1 = Poly.basis(5) p2 = p1.copy() assert_(p1 == p2) @@ -440,7 +419,7 @@ def check_copy(Poly): assert_(p1.window is not p2.window) -def check_integ(Poly): +def test_integ(Poly): P = Polynomial # Check defaults p0 = Poly.cast(P([1*2, 2*3, 3*4])) @@ -469,7 +448,7 @@ def check_integ(Poly): assert_poly_almost_equal(p2, P([0, 0, 1, 1, 1])) -def check_deriv(Poly): +def test_deriv(Poly): # Check that the derivative is the inverse of integration. It is # assumes that the integration has been checked elsewhere. d = Poly.domain + random((2,))*.25 @@ -487,7 +466,7 @@ def check_deriv(Poly): assert_almost_equal(p2.deriv(2).coef, p1.coef) -def check_linspace(Poly): +def test_linspace(Poly): d = Poly.domain + random((2,))*.25 w = Poly.window + random((2,))*.25 p = Poly([1, 2, 3], domain=d, window=w) @@ -505,7 +484,7 @@ def check_linspace(Poly): assert_almost_equal(yres, ytgt) -def check_pow(Poly): +def test_pow(Poly): d = Poly.domain + random((2,))*.25 w = Poly.window + random((2,))*.25 tgt = Poly([1], domain=d, window=w) @@ -524,7 +503,7 @@ def check_pow(Poly): assert_raises(ValueError, op.pow, tgt, -1) -def check_call(Poly): +def test_call(Poly): P = Polynomial d = Poly.domain x = np.linspace(d[0], d[1], 11) @@ -536,7 +515,7 @@ def check_call(Poly): assert_almost_equal(res, tgt) -def check_cutdeg(Poly): +def test_cutdeg(Poly): p = Poly([1, 2, 3]) assert_raises(ValueError, p.cutdeg, .5) assert_raises(ValueError, p.cutdeg, -1) @@ -546,7 +525,7 @@ def check_cutdeg(Poly): assert_equal(len(p.cutdeg(0)), 1) -def check_truncate(Poly): +def test_truncate(Poly): p = Poly([1, 2, 3]) assert_raises(ValueError, p.truncate, .5) assert_raises(ValueError, p.truncate, 0) @@ -556,7 +535,7 @@ def check_truncate(Poly): assert_equal(len(p.truncate(1)), 1) -def check_trim(Poly): +def test_trim(Poly): c = [1, 1e-6, 1e-12, 0] p = Poly(c) assert_equal(p.trim().coef, c[:3]) @@ -564,7 +543,7 @@ def check_trim(Poly): assert_equal(p.trim(1e-5).coef, c[:1]) -def check_mapparms(Poly): +def test_mapparms(Poly): # check with defaults. Should be identity. d = Poly.domain w = Poly.window @@ -576,13 +555,18 @@ def check_mapparms(Poly): assert_almost_equal([1, 2], p.mapparms()) -def check_ufunc_override(Poly): +def test_ufunc_override(Poly): p = Poly([1, 2, 3]) x = np.ones(3) assert_raises(TypeError, np.add, p, x) assert_raises(TypeError, np.add, x, p) +# +# Test class method that only exists for some classes +# + + class TestInterpolate(object): def f(self, x): diff --git a/numpy/testing/decorators.py b/numpy/testing/decorators.py index 1988afd29..68c1554b5 100644 --- a/numpy/testing/decorators.py +++ b/numpy/testing/decorators.py @@ -3,9 +3,13 @@ Back compatibility decorators module. It will import the appropriate set of tools """ +from __future__ import division, absolute_import, print_function + import warnings -warnings.warn("Import from numpy.testing, not numpy.testing.decorators", - ImportWarning) +# 2018-04-04, numpy 1.15.0 +warnings.warn("Importing from numpy.testing.decorators is deprecated, " + "import from numpy.testing instead.", + DeprecationWarning, stacklevel=2) from ._private.decorators import * diff --git a/numpy/testing/noseclasses.py b/numpy/testing/noseclasses.py index dde62e825..e0e728a32 100644 --- a/numpy/testing/noseclasses.py +++ b/numpy/testing/noseclasses.py @@ -2,10 +2,13 @@ Back compatibility noseclasses module. It will import the appropriate set of tools """ +from __future__ import division, absolute_import, print_function + import warnings -warnings.warn("Import from numpy.testing, not numpy.testing.noseclasses", - ImportWarning) +# 2018-04-04, numpy 1.15.0 +warnings.warn("Importing from numpy.testing.noseclasses is deprecated, " + "import from numpy.testing instead", + DeprecationWarning, stacklevel=2) from ._private.noseclasses import * - diff --git a/numpy/testing/nosetester.py b/numpy/testing/nosetester.py index 772bff305..c8c7d6e68 100644 --- a/numpy/testing/nosetester.py +++ b/numpy/testing/nosetester.py @@ -3,10 +3,14 @@ Back compatibility nosetester module. It will import the appropriate set of tools """ +from __future__ import division, absolute_import, print_function + import warnings -warnings.warn("Import from numpy.testing, not numpy.testing.nosetester", - ImportWarning) +# 2018-04-04, numpy 1.15.0 +warnings.warn("Importing from numpy.testing.nosetester is deprecated, " + "import from numpy.testing instead.", + DeprecationWarning, stacklevel=2) from ._private.nosetester import * diff --git a/numpy/testing/utils.py b/numpy/testing/utils.py index 3cd89e639..78cf405cf 100644 --- a/numpy/testing/utils.py +++ b/numpy/testing/utils.py @@ -3,10 +3,14 @@ Back compatibility utils module. It will import the appropriate set of tools """ +from __future__ import division, absolute_import, print_function + import warnings -warnings.warn("Import from numpy.testing, not numpy.testing.utils", - ImportWarning) +# 2018-04-04, numpy 1.15.0 +warnings.warn("Importing from numpy.testing.utils is deprecated, " + "import from numpy.testing instead.", + ImportWarning, stacklevel=2) from ._private.utils import * |