master
Per.Andreas.Brodtkorb 11 years ago
parent b9031f8a22
commit c070dd4dc4

@ -14,11 +14,13 @@ import wafo.stats.mstats as mstats
from wafo import stats from wafo import stats
from numpy.testing import TestCase, run_module_suite from numpy.testing import TestCase, run_module_suite
from numpy.ma.testutils import (assert_equal, assert_almost_equal, from numpy.ma.testutils import (assert_equal, assert_almost_equal,
assert_array_almost_equal, assert_array_almost_equal_nulp, assert_, assert_array_almost_equal,
assert_array_almost_equal_nulp, assert_,
assert_allclose, assert_raises) assert_allclose, assert_raises)
class TestMquantiles(TestCase): class TestMquantiles(TestCase):
def test_mquantiles_limit_keyword(self): def test_mquantiles_limit_keyword(self):
# Regression test for Trac ticket #867 # Regression test for Trac ticket #867
data = np.array([[6., 7., 1.], data = np.array([[6., 7., 1.],
@ -40,6 +42,7 @@ class TestMquantiles(TestCase):
class TestGMean(TestCase): class TestGMean(TestCase):
def test_1D(self): def test_1D(self):
a = (1, 2, 3, 4) a = (1, 2, 3, 4)
actual = mstats.gmean(a) actual = mstats.gmean(a)
@ -76,6 +79,7 @@ class TestGMean(TestCase):
class TestHMean(TestCase): class TestHMean(TestCase):
def test_1D(self): def test_1D(self):
a = (1, 2, 3, 4) a = (1, 2, 3, 4)
actual = mstats.hmean(a) actual = mstats.hmean(a)
@ -113,17 +117,23 @@ class TestRanking(TestCase):
def test_ranking(self): def test_ranking(self):
x = ma.array([0, 1, 1, 1, 2, 3, 4, 5, 5, 6, ]) x = ma.array([0, 1, 1, 1, 2, 3, 4, 5, 5, 6, ])
assert_almost_equal(mstats.rankdata(x),[1,3,3,3,5,6,7,8.5,8.5,10]) assert_almost_equal(
mstats.rankdata(x), [1, 3, 3, 3, 5, 6, 7, 8.5, 8.5, 10])
x[[3, 4]] = masked x[[3, 4]] = masked
assert_almost_equal(mstats.rankdata(x),[1,2.5,2.5,0,0,4,5,6.5,6.5,8]) assert_almost_equal(
mstats.rankdata(x), [1, 2.5, 2.5, 0, 0, 4, 5, 6.5, 6.5, 8])
assert_almost_equal(mstats.rankdata(x, use_missing=True), assert_almost_equal(mstats.rankdata(x, use_missing=True),
[1, 2.5, 2.5, 4.5, 4.5, 4, 5, 6.5, 6.5, 8]) [1, 2.5, 2.5, 4.5, 4.5, 4, 5, 6.5, 6.5, 8])
x = ma.array([0, 1, 5, 1, 2, 4, 3, 5, 1, 6, ]) x = ma.array([0, 1, 5, 1, 2, 4, 3, 5, 1, 6, ])
assert_almost_equal(mstats.rankdata(x),[1,3,8.5,3,5,7,6,8.5,3,10]) assert_almost_equal(
mstats.rankdata(x), [1, 3, 8.5, 3, 5, 7, 6, 8.5, 3, 10])
x = ma.array([[0, 1, 1, 1, 2], [3, 4, 5, 5, 6, ]]) x = ma.array([[0, 1, 1, 1, 2], [3, 4, 5, 5, 6, ]])
assert_almost_equal(mstats.rankdata(x),[[1,3,3,3,5],[6,7,8.5,8.5,10]]) assert_almost_equal(
assert_almost_equal(mstats.rankdata(x,axis=1),[[1,3,3,3,5],[1,2,3.5,3.5,5]]) mstats.rankdata(x), [[1, 3, 3, 3, 5], [6, 7, 8.5, 8.5, 10]])
assert_almost_equal(mstats.rankdata(x,axis=0),[[1,1,1,1,1],[2,2,2,2,2,]]) assert_almost_equal(
mstats.rankdata(x, axis=1), [[1, 3, 3, 3, 5], [1, 2, 3.5, 3.5, 5]])
assert_almost_equal(
mstats.rankdata(x, axis=0), [[1, 1, 1, 1, 1], [2, 2, 2, 2, 2, ]])
class TestCorr(TestCase): class TestCorr(TestCase):
@ -163,7 +173,8 @@ class TestCorr(TestCase):
# Tests some computations of Spearman's rho # Tests some computations of Spearman's rho
(x, y) = ([5.05, 6.75, 3.21, 2.66], [1.65, 2.64, 2.64, 6.95]) (x, y) = ([5.05, 6.75, 3.21, 2.66], [1.65, 2.64, 2.64, 6.95])
assert_almost_equal(mstats.spearmanr(x, y)[0], -0.6324555) assert_almost_equal(mstats.spearmanr(x, y)[0], -0.6324555)
(x, y) = ([5.05,6.75,3.21,2.66,np.nan],[1.65,2.64,2.64,6.95,np.nan]) (x, y) = ([5.05, 6.75, 3.21, 2.66, np.nan],
[1.65, 2.64, 2.64, 6.95, np.nan])
(x, y) = (ma.fix_invalid(x), ma.fix_invalid(y)) (x, y) = (ma.fix_invalid(x), ma.fix_invalid(y))
assert_almost_equal(mstats.spearmanr(x, y)[0], -0.6324555) assert_almost_equal(mstats.spearmanr(x, y)[0], -0.6324555)
@ -209,8 +220,8 @@ class TestCorr(TestCase):
[0.18, 0.53, 0.20, 0.04]) [0.18, 0.53, 0.20, 0.04])
def test_pointbiserial(self): def test_pointbiserial(self):
x = [1,0,1,1,1,1,0,1,0,0,0,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,0, x = [1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0,
0,0,0,0,1,-1] 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, -1]
y = [14.8, 13.8, 12.4, 10.1, 7.1, 6.1, 5.8, 4.6, 4.3, 3.5, 3.3, 3.2, 3.0, y = [14.8, 13.8, 12.4, 10.1, 7.1, 6.1, 5.8, 4.6, 4.3, 3.5, 3.3, 3.2, 3.0,
2.8, 2.8, 2.5, 2.4, 2.3, 2.1, 1.7, 1.7, 1.5, 1.3, 1.3, 1.2, 1.2, 1.1, 2.8, 2.8, 2.5, 2.4, 2.3, 2.1, 1.7, 1.7, 1.5, 1.3, 1.3, 1.2, 1.2, 1.1,
0.8, 0.7, 0.6, 0.5, 0.2, 0.2, 0.1, np.nan] 0.8, 0.7, 0.6, 0.5, 0.2, 0.2, 0.1, np.nan]
@ -223,7 +234,8 @@ class TestTrimming(TestCase):
a = ma.arange(10) a = ma.arange(10)
assert_equal(mstats.trim(a), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) assert_equal(mstats.trim(a), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
a = ma.arange(10) a = ma.arange(10)
assert_equal(mstats.trim(a,(2,8)), [None,None,2,3,4,5,6,7,8,None]) assert_equal(
mstats.trim(a, (2, 8)), [None, None, 2, 3, 4, 5, 6, 7, 8, None])
a = ma.arange(10) a = ma.arange(10)
assert_equal(mstats.trim(a, limits=(2, 8), inclusive=(False, False)), assert_equal(mstats.trim(a, limits=(2, 8), inclusive=(False, False)),
[None, None, None, 3, 4, 5, 6, 7, None, None]) [None, None, None, 3, 4, 5, 6, 7, None, None])
@ -260,7 +272,8 @@ class TestTrimming(TestCase):
x[50:70] = masked x[50:70] = masked
trimx = mstats.trimboth(x) trimx = mstats.trimboth(x)
assert_equal(trimx.count(), 48) assert_equal(trimx.count(), 48)
assert_equal(trimx._mask, [1]*16 + [0]*34 + [1]*20 + [0]*14 + [1]*16) assert_equal(
trimx._mask, [1] * 16 + [0] * 34 + [1] * 20 + [0] * 14 + [1] * 16)
x._mask = nomask x._mask = nomask
x.shape = (10, 10) x.shape = (10, 10)
assert_equal(mstats.trimboth(x).count(), 60) assert_equal(mstats.trimboth(x).count(), 60)
@ -388,15 +401,21 @@ class TestMoments(TestCase):
assert_equal(mstats.mode(ma2, axis=None), (0, 3)) assert_equal(mstats.mode(ma2, axis=None), (0, 3))
assert_equal(mstats.mode(a3, axis=None), (1, 1)) assert_equal(mstats.mode(a3, axis=None), (1, 1))
assert_equal(mstats.mode(ma3, axis=None), (2, 1)) assert_equal(mstats.mode(ma3, axis=None), (2, 1))
assert_equal(mstats.mode(a2, axis=0), ([[0,0,0,1,1]], [[1,1,1,1,1]])) assert_equal(
assert_equal(mstats.mode(ma2, axis=0), ([[0,0,0,1,1]], [[1,1,1,1,1]])) mstats.mode(a2, axis=0), ([[0, 0, 0, 1, 1]], [[1, 1, 1, 1, 1]]))
assert_equal(mstats.mode(a2, axis=-1), ([[0],[3],[3]], [[3],[3],[1]])) assert_equal(
assert_equal(mstats.mode(ma2, axis=-1), ([[0],[1],[0]], [[3],[1],[0]])) mstats.mode(ma2, axis=0), ([[0, 0, 0, 1, 1]], [[1, 1, 1, 1, 1]]))
assert_equal(
mstats.mode(a2, axis=-1), ([[0], [3], [3]], [[3], [3], [1]]))
assert_equal(
mstats.mode(ma2, axis=-1), ([[0], [1], [0]], [[3], [1], [0]]))
assert_equal(mstats.mode(ma4, axis=0), ([[3, 2]], [[1, 1]])) assert_equal(mstats.mode(ma4, axis=0), ([[3, 2]], [[1, 1]]))
assert_equal(mstats.mode(ma4, axis=-1), ([[2],[3],[5]], [[1],[1],[1]])) assert_equal(
mstats.mode(ma4, axis=-1), ([[2], [3], [5]], [[1], [1], [1]]))
class TestPercentile(TestCase): class TestPercentile(TestCase):
def setUp(self): def setUp(self):
self.a1 = [3, 4, 5, 10, -3, -5, 6] self.a1 = [3, 4, 5, 10, -3, -5, 6]
self.a2 = [3, -6, -2, 8, 7, 4, 2, 1] self.a2 = [3, -6, -2, 8, 7, 4, 2, 1]
@ -418,6 +437,7 @@ class TestPercentile(TestCase):
class TestVariability(TestCase): class TestVariability(TestCase):
""" Comparison numbers are found using R v.1.5.1 """ Comparison numbers are found using R v.1.5.1
note that length(testcase) = 4 note that length(testcase) = 4
""" """

Loading…
Cancel
Save