Skip to content
Browse files

MAINT: ma: clean up ma/test_extras.py: don't use 'import *'; PEP8 whi…

…tespace

This commit was cherry-picked from 4a7f27601.
The test for the new 'invert' option of 'in1d' was removed to resolve the conflict.

Conflicts:
	numpy/ma/tests/test_extras.py
  • Loading branch information...
1 parent ef5fa41 commit e82362736eb2380faa866ee2eb80b7e73ccebbf5 @WarrenWeckesser WarrenWeckesser committed Jun 15, 2013
Showing with 67 additions and 60 deletions.
  1. +67 −60 numpy/ma/tests/test_extras.py
View
127 numpy/ma/tests/test_extras.py
@@ -13,9 +13,19 @@
import numpy as np
from numpy.testing import TestCase, run_module_suite
-from numpy.ma.testutils import *
-from numpy.ma.core import *
-from numpy.ma.extras import *
+from numpy.ma.testutils import (rand, assert_, assert_array_equal,
+ assert_equal, assert_almost_equal)
+from numpy.ma.core import (array, arange, masked, MaskedArray, masked_array,
+ getmaskarray, shape, nomask, ones, zeros, count)
+from numpy.ma.extras import (atleast_2d, mr_, dot, polyfit,
+ cov, corrcoef, median, average,
+ unique, setxor1d, setdiff1d, union1d, intersect1d, in1d, ediff1d,
+ apply_over_axes, apply_along_axis,
+ compress_rowcols, mask_rowcols,
+ clump_masked, clump_unmasked,
+ flatnotmasked_contiguous, notmasked_contiguous, notmasked_edges,
+ masked_all, masked_all_like,
+ )
class TestGeneric(TestCase):
@@ -50,7 +60,6 @@ def test_masked_all(self):
control = array([[(1, (1, 1))]], mask=[[(1, (1, 1))]], dtype=dt)
assert_equal(test, control)
-
def test_masked_all_like(self):
"Tests masked_all"
# Standard dtype
@@ -129,10 +138,10 @@ def test_testAverage2(self):
"More tests of average."
w1 = [0, 1, 1, 1, 1, 0]
w2 = [[0, 1, 1, 1, 1, 0], [1, 0, 0, 0, 0, 1]]
- x = arange(6, dtype=float_)
+ x = arange(6, dtype=np.float_)
assert_equal(average(x, axis=0), 2.5)
assert_equal(average(x, axis=0, weights=w1), 2.5)
- y = array([arange(6, dtype=float_), 2.0 * arange(6)])
+ y = array([arange(6, dtype=np.float_), 2.0 * arange(6)])
assert_equal(average(y, None), np.add.reduce(np.arange(6)) * 3. / 12.)
assert_equal(average(y, axis=0), np.arange(6) * 3. / 2.)
assert_equal(average(y, axis=1),
@@ -164,12 +173,12 @@ def test_testAverage3(self):
a = arange(6)
b = arange(6) * 3
r1, w1 = average([[a, b], [b, a]], axis=1, returned=1)
- assert_equal(shape(r1) , shape(w1))
- assert_equal(r1.shape , w1.shape)
+ assert_equal(shape(r1), shape(w1))
+ assert_equal(r1.shape, w1.shape)
r2, w2 = average(ones((2, 2, 3)), axis=0, weights=[3, 1], returned=1)
- assert_equal(shape(w2) , shape(r2))
+ assert_equal(shape(w2), shape(r2))
r2, w2 = average(ones((2, 2, 3)), returned=1)
- assert_equal(shape(w2) , shape(r2))
+ assert_equal(shape(w2), shape(r2))
r2, w2 = average(ones((2, 2, 3)), weights=ones((2, 2, 3)), returned=1)
assert_equal(shape(w2), shape(r2))
a2d = array([[1, 2], [0, 4]], float)
@@ -203,7 +212,7 @@ def test_1d(self):
m = [1, 0, 0, 0, 0]
d = masked_array(b, mask=m)
c = mr_[d, 0, 0, d]
- self.assertTrue(isinstance(c, MaskedArray) or isinstance(c, core.MaskedArray))
+ self.assertTrue(isinstance(c, MaskedArray))
assert_array_equal(c, [1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1])
assert_array_equal(c.mask, mr_[m, 0, 0, m])
@@ -227,7 +236,6 @@ def test_2d(self):
assert_array_equal(d.mask, np.r_[m_1, m_2])
-
class TestNotMasked(TestCase):
"""
Tests notmasked_edges and notmasked_contiguous.
@@ -267,7 +275,6 @@ def test_edges(self):
assert_equal(test[0], [(0, 1, 2, 4), (0, 0, 2, 3)])
assert_equal(test[1], [(0, 1, 2, 4), (4, 2, 4, 4)])
-
def test_contiguous(self):
"Tests notmasked_contiguous"
a = masked_array(np.arange(24).reshape(3, 8),
@@ -292,12 +299,12 @@ def test_contiguous(self):
assert_equal(tmp[2][-2], slice(0, 6, None))
-
class Test2DFunctions(TestCase):
"Tests 2D functions"
def test_compress2d(self):
"Tests compress2d"
- x = array(np.arange(9).reshape(3, 3), mask=[[1, 0, 0], [0, 0, 0], [0, 0, 0]])
+ x = array(np.arange(9).reshape(3, 3),
+ mask=[[1, 0, 0], [0, 0, 0], [0, 0, 0]])
assert_equal(compress_rowcols(x), [[4, 5], [7, 8]])
assert_equal(compress_rowcols(x, 0), [[3, 4, 5], [6, 7, 8]])
assert_equal(compress_rowcols(x, 1), [[1, 2], [4, 5], [7, 8]])
@@ -313,29 +320,39 @@ def test_compress2d(self):
assert_equal(compress_rowcols(x).size, 0)
assert_equal(compress_rowcols(x, 0).size, 0)
assert_equal(compress_rowcols(x, 1).size, 0)
- #
+
def test_mask_rowcols(self):
"Tests mask_rowcols."
- x = array(np.arange(9).reshape(3, 3), mask=[[1, 0, 0], [0, 0, 0], [0, 0, 0]])
- assert_equal(mask_rowcols(x).mask, [[1, 1, 1], [1, 0, 0], [1, 0, 0]])
- assert_equal(mask_rowcols(x, 0).mask, [[1, 1, 1], [0, 0, 0], [0, 0, 0]])
- assert_equal(mask_rowcols(x, 1).mask, [[1, 0, 0], [1, 0, 0], [1, 0, 0]])
+ x = array(np.arange(9).reshape(3, 3),
+ mask=[[1, 0, 0], [0, 0, 0], [0, 0, 0]])
+ assert_equal(mask_rowcols(x).mask,
+ [[1, 1, 1], [1, 0, 0], [1, 0, 0]])
+ assert_equal(mask_rowcols(x, 0).mask,
+ [[1, 1, 1], [0, 0, 0], [0, 0, 0]])
+ assert_equal(mask_rowcols(x, 1).mask,
+ [[1, 0, 0], [1, 0, 0], [1, 0, 0]])
x = array(x._data, mask=[[0, 0, 0], [0, 1, 0], [0, 0, 0]])
- assert_equal(mask_rowcols(x).mask, [[0, 1, 0], [1, 1, 1], [0, 1, 0]])
- assert_equal(mask_rowcols(x, 0).mask, [[0, 0, 0], [1, 1, 1], [0, 0, 0]])
- assert_equal(mask_rowcols(x, 1).mask, [[0, 1, 0], [0, 1, 0], [0, 1, 0]])
+ assert_equal(mask_rowcols(x).mask,
+ [[0, 1, 0], [1, 1, 1], [0, 1, 0]])
+ assert_equal(mask_rowcols(x, 0).mask,
+ [[0, 0, 0], [1, 1, 1], [0, 0, 0]])
+ assert_equal(mask_rowcols(x, 1).mask,
+ [[0, 1, 0], [0, 1, 0], [0, 1, 0]])
x = array(x._data, mask=[[1, 0, 0], [0, 1, 0], [0, 0, 0]])
- assert_equal(mask_rowcols(x).mask, [[1, 1, 1], [1, 1, 1], [1, 1, 0]])
- assert_equal(mask_rowcols(x, 0).mask, [[1, 1, 1], [1, 1, 1], [0, 0, 0]])
- assert_equal(mask_rowcols(x, 1,).mask, [[1, 1, 0], [1, 1, 0], [1, 1, 0]])
+ assert_equal(mask_rowcols(x).mask,
+ [[1, 1, 1], [1, 1, 1], [1, 1, 0]])
+ assert_equal(mask_rowcols(x, 0).mask,
+ [[1, 1, 1], [1, 1, 1], [0, 0, 0]])
+ assert_equal(mask_rowcols(x, 1,).mask,
+ [[1, 1, 0], [1, 1, 0], [1, 1, 0]])
x = array(x._data, mask=[[1, 0, 0], [0, 1, 0], [0, 0, 1]])
self.assertTrue(mask_rowcols(x).all() is masked)
self.assertTrue(mask_rowcols(x, 0).all() is masked)
self.assertTrue(mask_rowcols(x, 1).all() is masked)
self.assertTrue(mask_rowcols(x).mask.all())
self.assertTrue(mask_rowcols(x, 0).mask.all())
self.assertTrue(mask_rowcols(x, 1).mask.all())
- #
+
def test_dot(self):
"Tests dot product"
n = np.arange(1, 7)
@@ -407,9 +424,7 @@ def test_dot(self):
assert_equal(c, np.dot(b.filled(0), a.filled(0)))
-
class TestApplyAlongAxis(TestCase):
- #
"Tests 2D functions"
def test_3d(self):
a = arange(12.).reshape(2, 2, 3)
@@ -419,21 +434,20 @@ def myfunc(b):
assert_equal(xa, [[1, 4], [7, 10]])
-
class TestApplyOverAxes(TestCase):
"Tests apply_over_axes"
def test_basic(self):
a = arange(24).reshape(2, 3, 4)
test = apply_over_axes(np.sum, a, [0, 2])
- ctrl = np.array([[[ 60], [ 92], [124]]])
+ ctrl = np.array([[[60], [92], [124]]])
assert_equal(test, ctrl)
a[(a % 2).astype(np.bool)] = masked
test = apply_over_axes(np.sum, a, [0, 2])
- ctrl = np.array([[[ 30], [ 44], [60]]])
+ ctrl = np.array([[[30], [44], [60]]])
class TestMedian(TestCase):
- #
+
def test_2d(self):
"Tests median w/ 2D"
(n, p) = (101, 30)
@@ -449,7 +463,7 @@ def test_2d(self):
assert_equal(median(z), 0)
assert_equal(median(z, axis=0), np.zeros(p))
assert_equal(median(z.T, axis=1), np.zeros(p))
- #
+
def test_2d_waxis(self):
"Tests median w/ 2D arrays and different axis."
x = masked_array(np.arange(30).reshape(10, 3))
@@ -458,7 +472,7 @@ def test_2d_waxis(self):
assert_equal(median(x, axis=0), [13.5, 14.5, 15.5])
assert_equal(median(x, axis=1), [0, 0, 0, 10, 13, 16, 19, 0, 0, 0])
assert_equal(median(x, axis=1).mask, [1, 1, 1, 0, 0, 0, 0, 1, 1, 1])
- #
+
def test_3d(self):
"Tests median w/ 3D"
x = np.ma.arange(24).reshape(3, 4, 2)
@@ -471,7 +485,6 @@ def test_3d(self):
assert_equal(median(x, 0), [[12, 10], [8, 9], [16, 17]])
-
class TestCov(TestCase):
def setUp(self):
@@ -525,16 +538,18 @@ def test_2d_w_missing(self):
valid = np.logical_not(getmaskarray(x)).astype(int)
frac = np.dot(valid, valid.T)
xf = (x - x.mean(1)[:, None]).filled(0)
- assert_almost_equal(cov(x), np.cov(xf) * (x.shape[1] - 1) / (frac - 1.))
+ assert_almost_equal(cov(x),
+ np.cov(xf) * (x.shape[1] - 1) / (frac - 1.))
assert_almost_equal(cov(x, bias=True),
np.cov(xf, bias=True) * x.shape[1] / frac)
frac = np.dot(valid.T, valid)
xf = (x - x.mean(0)).filled(0)
assert_almost_equal(cov(x, rowvar=False),
- np.cov(xf, rowvar=False) * (x.shape[0] - 1) / (frac - 1.))
+ (np.cov(xf, rowvar=False) *
+ (x.shape[0] - 1) / (frac - 1.)))
assert_almost_equal(cov(x, rowvar=False, bias=True),
- np.cov(xf, rowvar=False, bias=True) * x.shape[0] / frac)
-
+ (np.cov(xf, rowvar=False, bias=True) *
+ x.shape[0] / frac))
class TestCorrcoef(TestCase):
@@ -547,7 +562,6 @@ def test_ddof(self):
x = self.data
assert_almost_equal(np.corrcoef(x, ddof=0), corrcoef(x, ddof=0))
-
def test_1d_wo_missing(self):
"Test cov on 1D variable w/o missing values"
x = self.data
@@ -573,7 +587,8 @@ def test_1d_w_missing(self):
x -= x.mean()
nx = x.compressed()
assert_almost_equal(np.corrcoef(nx), corrcoef(x))
- assert_almost_equal(np.corrcoef(nx, rowvar=False), corrcoef(x, rowvar=False))
+ assert_almost_equal(np.corrcoef(nx, rowvar=False),
+ corrcoef(x, rowvar=False))
assert_almost_equal(np.corrcoef(nx, rowvar=False, bias=True),
corrcoef(x, rowvar=False, bias=True))
#
@@ -601,7 +616,6 @@ def test_2d_w_missing(self):
assert_almost_equal(test[:-1, :-1], control[:-1, :-1])
-
class TestPolynomial(TestCase):
#
def test_polyfit(self):
@@ -617,7 +631,8 @@ def test_polyfit(self):
y[0, 0] = y[-1, -1] = masked
#
(C, R, K, S, D) = polyfit(x, y[:, 0], 3, full=True)
- (c, r, k, s, d) = np.polyfit(x[1:], y[1:, 0].compressed(), 3, full=True)
+ (c, r, k, s, d) = np.polyfit(x[1:], y[1:, 0].compressed(), 3,
+ full=True)
for (a, a_) in zip((C, R, K, S, D), (c, r, k, s, d)):
assert_almost_equal(a, a_)
#
@@ -631,7 +646,7 @@ def test_polyfit(self):
for (a, a_) in zip((C, R, K, S, D), (c, r, k, s, d)):
assert_almost_equal(a, a_)
#
- w = np.random.rand(10) + 1
+ w = np.random.rand(10) + 1
wo = w.copy()
xs = x[1:-1]
ys = y[1:-1]
@@ -644,7 +659,7 @@ def test_polyfit(self):
class TestArraySetOps(TestCase):
- #
+
def test_unique_onlist(self):
"Test unique on list"
data = [1, 1, 1, 2, 2, 3]
@@ -693,7 +708,7 @@ def test_ediff1d(self):
assert_equal(test, control)
assert_equal(test.data, control.data)
assert_equal(test.mask, control.mask)
- #
+
def test_ediff1d_tobegin(self):
"Test ediff1d w/ to_begin"
x = masked_array(np.arange(5), mask=[1, 0, 0, 0, 1])
@@ -708,7 +723,7 @@ def test_ediff1d_tobegin(self):
assert_equal(test, control)
assert_equal(test.data, control.data)
assert_equal(test.mask, control.mask)
- #
+
def test_ediff1d_toend(self):
"Test ediff1d w/ to_end"
x = masked_array(np.arange(5), mask=[1, 0, 0, 0, 1])
@@ -723,7 +738,7 @@ def test_ediff1d_toend(self):
assert_equal(test, control)
assert_equal(test.data, control.data)
assert_equal(test.mask, control.mask)
- #
+
def test_ediff1d_tobegin_toend(self):
"Test ediff1d w/ to_begin and to_end"
x = masked_array(np.arange(5), mask=[1, 0, 0, 0, 1])
@@ -734,11 +749,12 @@ def test_ediff1d_tobegin_toend(self):
assert_equal(test.mask, control.mask)
#
test = ediff1d(x, to_end=[1, 2, 3], to_begin=masked)
- control = array([0, 1, 1, 1, 4, 1, 2, 3], mask=[1, 1, 0, 0, 1, 0, 0, 0])
+ control = array([0, 1, 1, 1, 4, 1, 2, 3],
+ mask=[1, 1, 0, 0, 1, 0, 0, 0])
assert_equal(test, control)
assert_equal(test.data, control.data)
assert_equal(test.mask, control.mask)
- #
+
def test_ediff1d_ndarray(self):
"Test ediff1d w/ a ndarray"
x = np.arange(5)
@@ -755,7 +771,6 @@ def test_ediff1d_ndarray(self):
assert_equal(test.data, control.data)
assert_equal(test.mask, control.mask)
-
def test_intersect1d(self):
"Test intersect1d"
x = array([1, 3, 3, 3], mask=[0, 0, 0, 1])
@@ -764,7 +779,6 @@ def test_intersect1d(self):
control = array([1, 3, -1], mask=[0, 0, 1])
assert_equal(test, control)
-
def test_setxor1d(self):
"Test setxor1d"
a = array([1, 2, 5, 7, -1], mask=[0, 0, 0, 0, 1])
@@ -791,7 +805,6 @@ def test_setxor1d(self):
#
assert_array_equal([], setxor1d([], []))
-
def test_in1d(self):
"Test in1d"
a = array([1, 2, 5, 7, -1], mask=[0, 0, 0, 0, 1])
@@ -806,7 +819,6 @@ def test_in1d(self):
#
assert_array_equal([], in1d([], []))
-
def test_union1d(self):
"Test union1d"
a = array([1, 2, 5, 7, 5, -1], mask=[0, 0, 0, 0, 0, 1])
@@ -817,7 +829,6 @@ def test_union1d(self):
#
assert_array_equal([], union1d([], []))
-
def test_setdiff1d(self):
"Test setdiff1d"
a = array([6, 5, 4, 7, 7, 1, 2, 1], mask=[0, 0, 0, 0, 0, 0, 0, 1])
@@ -829,17 +840,13 @@ def test_setdiff1d(self):
b = arange(8)
assert_equal(setdiff1d(a, b), array([8, 9]))
-
def test_setdiff1d_char_array(self):
"Test setdiff1d_charray"
a = np.array(['a', 'b', 'c'])
b = np.array(['a', 'b', 's'])
assert_array_equal(setdiff1d(a, b), np.array(['c']))
-
-
-
class TestShapeBase(TestCase):
#
def test_atleast2d(self):

0 comments on commit e823627

Please sign in to comment.
Something went wrong with that request. Please try again.