From ead204e4fd3b8bf44840b03b1e0887ab1575e9ee Mon Sep 17 00:00:00 2001 From: pbrod Date: Thu, 26 May 2016 19:00:47 +0200 Subject: [PATCH] Fixed some tests --- wafo/integrate_oscillating.py | 2 +- wafo/tests/test_integrate_oscillating.py | 38 ++--- wafo/tests/test_kdetools.py | 182 +++++++++++------------ 3 files changed, 107 insertions(+), 115 deletions(-) diff --git a/wafo/integrate_oscillating.py b/wafo/integrate_oscillating.py index 2b329a3..62a1640 100644 --- a/wafo/integrate_oscillating.py +++ b/wafo/integrate_oscillating.py @@ -266,7 +266,7 @@ class QuadOsc(object): # Successive bisection of intervals nq = len(xq) n = nq - for k in xrange(1, max_iter): + for k in range(1, max_iter): n += nq*2**k hh = hh / 2 diff --git a/wafo/tests/test_integrate_oscillating.py b/wafo/tests/test_integrate_oscillating.py index 2242151..f33f1a8 100644 --- a/wafo/tests/test_integrate_oscillating.py +++ b/wafo/tests/test_integrate_oscillating.py @@ -351,25 +351,25 @@ class LevinQuadrature(unittest.TestCase): zdg = zeros_dg(beta) ab = (a, ) + zdg + (b, ) true_val, _err = mp.quadts(ftot, ab, maxdegree=9, error=True) - - import matplotlib.pyplot as plt - t = np.linspace(a, b, 5*513) - plt.subplot(2, 1, 1) - f2 = f(t, beta, z1)*np.exp(1j*R*g(t, beta, z1)) - - true_val2 = np.trapz(f2, t) - plt.plot(t, f2.real, t, f2.imag, 'r') - plt.title('integral=%g+1j%g, ' - '(%g+1j%g)' % (true_val2.real, true_val2.imag, - true_val.real, true_val.imag)) - plt.subplot(2, 1, 2) - plt.plot(t, dg(t, beta, z1), 'r') - plt.plot(t, g(t, beta, z1)) - plt.hlines(0, a, b) - plt.axis([a, b, -5, 5]) - plt.title('beta=%g' % beta) - print(np.trapz(f2, t)) - plt.show('hold') + if False: + import matplotlib.pyplot as plt + t = np.linspace(a, b, 5*513) + plt.subplot(2, 1, 1) + f2 = f(t, beta, z1)*np.exp(1j*R*g(t, beta, z1)) + + true_val2 = np.trapz(f2, t) + plt.plot(t, f2.real, t, f2.imag, 'r') + plt.title('integral=%g+1j%g, ' + '(%g+1j%g)' % (true_val2.real, true_val2.imag, + true_val.real, true_val.imag)) + plt.subplot(2, 1, 2) + plt.plot(t, dg(t, beta, z1), 'r') + plt.plot(t, g(t, beta, z1)) + plt.hlines(0, a, b) + plt.axis([a, b, -5, 5]) + plt.title('beta=%g' % beta) + print(np.trapz(f2, t)) + plt.show('hold') # true_val = 0.00253186684281+0.004314054498j # s = 15 for quadfun in [QuadOsc, EvansWebster, AdaptiveLevin]: diff --git a/wafo/tests/test_kdetools.py b/wafo/tests/test_kdetools.py index 2e4f793..9db6d6a 100644 --- a/wafo/tests/test_kdetools.py +++ b/wafo/tests/test_kdetools.py @@ -58,8 +58,8 @@ def test1_TKDE1D(): array([ 1.03982714, 0.45839018, 0.39514782, 0.32860602, 0.26433318, 0.20717946, 0.15907684, 0.1201074 , 0.08941027, 0.06574882]) - >>> np.trapz(f, x) - 0.94787730659349068 + >>> np.allclose(np.trapz(f, x), 0.94787730659349068) + True h1 = plb.plot(x, f) # 1D probability density plot ''' @@ -78,12 +78,12 @@ def test1_KDE1D(): >>> x = np.linspace(0, max(data.ravel()) + 1, 10) >>> kde = wk.KDE(data, hs=0.5) >>> f = kde(x) - >>> f - array([ 0.2039735 , 0.40252503, 0.54595078, 0.52219649, 0.3906213 , - 0.26381501, 0.16407362, 0.08270612, 0.02991145, 0.00720821]) - - >>> np.trapz(f, x) - 0.92576174424281876 + >>> np.allclose(f, [ 0.2039735 , 0.40252503, 0.54595078, 0.52219649, + ... 0.3906213, 0.26381501, 0.16407362, 0.08270612, 0.02991145, + ... 0.00720821]) + True + >>> np.allclose(np.trapz(f, x), 0.92576174424281876) + True h1 = plb.plot(x, f) # 1D probability density plot ''' @@ -103,12 +103,12 @@ def test2_KDE1D(): >>> x = np.linspace(0, max(data.ravel()) + 1, 10) >>> kde = wk.KDE(data, hs=0.5) >>> f = kde(x) - >>> f - array([ 0.0541248 , 0.16555235, 0.33084399, 0.45293325, 0.48345808, - 0.48345808, 0.45293325, 0.33084399, 0.16555235, 0.0541248 ]) - - >>> np.trapz(f, x) - 0.97323338046725172 + >>> np.allclose(f, + ... [ 0.0541248 , 0.16555235, 0.33084399, 0.45293325, 0.48345808, + ... 0.48345808, 0.45293325, 0.33084399, 0.16555235, 0.0541248 ]) + True + >>> np.allclose(np.trapz(f, x), 0.97323338046725172) + True h1 = plb.plot(x, f) # 1D probability density plot ''' @@ -127,12 +127,12 @@ def test1a_KDE1D(): >>> x = np.linspace(0, max(data.ravel()) + 1, 10) >>> kde = wk.KDE(data, hs=0.5, alpha=0.5) >>> f = kde(x) - >>> f - array([ 0.17252055, 0.41014271, 0.61349072, 0.57023834, 0.37198073, - 0.21409279, 0.12738463, 0.07460326, 0.03956191, 0.01887164]) - - >>> np.trapz(f, x) - 0.92938023659047952 + >>> np.allclose(f, + ... [ 0.17252055, 0.41014271, 0.61349072, 0.57023834, 0.37198073, + ... 0.21409279, 0.12738463, 0.07460326, 0.03956191, 0.01887164]) + True + >>> np.allclose(np.trapz(f, x), 0.92938023659047952) + True h1 = plb.plot(x, f) # 1D probability density plot ''' @@ -152,12 +152,12 @@ def test2a_KDE1D(): >>> x = np.linspace(0, max(data.ravel()) + 1, 10) >>> kde = wk.KDE(data, hs=0.5, alpha=0.5) >>> f = kde(x) - >>> f - array([ 0.0541248 , 0.16555235, 0.33084399, 0.45293325, 0.48345808, - 0.48345808, 0.45293325, 0.33084399, 0.16555235, 0.0541248 ]) - - >>> np.trapz(f, x) - 0.97323338046725172 + >>> np.allclose(f, + ... [ 0.0541248 , 0.16555235, 0.33084399, 0.45293325, 0.48345808, + ... 0.48345808, 0.45293325, 0.33084399, 0.16555235, 0.0541248 ]) + True + >>> np.allclose(np.trapz(f, x), 0.97323338046725172) + True h1 = plb.plot(x, f) # 1D probability density plot ''' @@ -183,15 +183,16 @@ def test_KDE2D(): >>> kde0 = wk.KDE(data, hs=0.5, alpha=0.0, inc=16) - >>> kde0.eval_grid(x, x) - array([[ 3.27260963e-02, 4.21654678e-02, 5.85338634e-04], - [ 6.78845466e-02, 1.42195839e-01, 1.41676003e-03], - [ 1.39466746e-04, 4.26983850e-03, 2.52736185e-05]]) - >>> kde0.eval_grid_fast(x, x) - array([[ 0.04435061, 0.06433531, 0.00413538], - [ 0.07218297, 0.12358196, 0.00928889], - [ 0.00161333, 0.00794858, 0.00058748]]) - + >>> np.allclose(kde0.eval_grid(x, x), + ... [[ 3.27260963e-02, 4.21654678e-02, 5.85338634e-04], + ... [ 6.78845466e-02, 1.42195839e-01, 1.41676003e-03], + ... [ 1.39466746e-04, 4.26983850e-03, 2.52736185e-05]]) + True + >>> np.allclose(kde0.eval_grid_fast(x, x), + ... [[ 0.04435061, 0.06433531, 0.00413538], + ... [ 0.07218297, 0.12358196, 0.00928889], + ... [ 0.00161333, 0.00794858, 0.00058748]]) + True ''' @@ -251,9 +252,10 @@ def test_gridcount_1D(): >>> x = np.linspace(0, max(data.ravel()) + 1, 10) >>> dx = x[1] - x[0] >>> c = wk.gridcount(data, x) - >>> c - array([ 0.78762626, 1.77520717, 7.99190087, 4.04054449, 1.67156643, - 2.38228499, 1.05933195, 0.29153785, 0. , 0. ]) + >>> np.allclose(c, + ... [ 0.78762626, 1.77520717, 7.99190087, 4.04054449, 1.67156643, + ... 2.38228499, 1.05933195, 0.29153785, 0. , 0. ]) + True h = plb.plot(x, c, '.') # 1D histogram @@ -281,12 +283,13 @@ def test_gridcount_2D(): >>> dx = x[1] - x[0] >>> X = np.vstack((x, x)) >>> c = wk.gridcount(data, X) - >>> c - array([[ 0.38922806, 0.8987982 , 0.34676493, 0.21042807, 0. ], - [ 1.15012203, 5.16513541, 3.19250588, 0.55420752, 0. ], - [ 0.74293418, 3.42517219, 1.97923195, 0.76076621, 0. ], - [ 0.02063536, 0.31054405, 0.71865964, 0.13486633, 0. ], - [ 0. , 0. , 0. , 0. , 0. ]]) + >>> np.allclose(c, + ... [[ 0.38922806, 0.8987982 , 0.34676493, 0.21042807, 0. ], + ... [ 1.15012203, 5.16513541, 3.19250588, 0.55420752, 0. ], + ... [ 0.74293418, 3.42517219, 1.97923195, 0.76076621, 0. ], + ... [ 0.02063536, 0.31054405, 0.71865964, 0.13486633, 0. ], + ... [ 0. , 0. , 0. , 0. , 0. ]]) + True h = plb.plot(x, c, '.') # 1D histogram @@ -318,19 +321,17 @@ def test_gridcount_3D(): >>> dx = x[1] - x[0] >>> X = np.vstack((x, x, x)) >>> c = wk.gridcount(data, X) - >>> c - array([[[ 8.74229894e-01, 1.27910940e+00, 1.42033973e-01], - [ 1.94778915e+00, 2.59536282e+00, 3.28213680e-01], - [ 1.08429416e-01, 1.69571495e-01, 7.48896775e-03]], - - [[ 1.44969128e+00, 2.58396370e+00, 2.45459949e-01], - [ 2.28951650e+00, 4.49653348e+00, 2.73167915e-01], - [ 1.10905565e-01, 3.18733817e-01, 1.12880816e-02]], - - [[ 7.49265424e-02, 2.18142488e-01, 0.00000000e+00], - [ 8.53886762e-02, 3.73415131e-01, 0.00000000e+00], - [ 4.16196568e-04, 1.62218824e-02, 0.00000000e+00]]]) - + >>> np.allclose(c, + ... [[[ 8.74229894e-01, 1.27910940e+00, 1.42033973e-01], + ... [ 1.94778915e+00, 2.59536282e+00, 3.28213680e-01], + ... [ 1.08429416e-01, 1.69571495e-01, 7.48896775e-03]], + ... [[ 1.44969128e+00, 2.58396370e+00, 2.45459949e-01], + ... [ 2.28951650e+00, 4.49653348e+00, 2.73167915e-01], + ... [ 1.10905565e-01, 3.18733817e-01, 1.12880816e-02]], + ... [[ 7.49265424e-02, 2.18142488e-01, 0.00000000e+00], + ... [ 8.53886762e-02, 3.73415131e-01, 0.00000000e+00], + ... [ 4.16196568e-04, 1.62218824e-02, 0.00000000e+00]]]) + True ''' @@ -352,44 +353,35 @@ def test_gridcount_4D(): >>> dx = x[1] - x[0] >>> X = np.vstack((x, x, x, x)) >>> c = wk.gridcount(data, X) - >>> c - array([[[[ 1.77163904e-01, 1.87720108e-01, 0.00000000e+00], - [ 5.72573585e-01, 6.09557834e-01, 0.00000000e+00], - [ 3.48549923e-03, 4.05931870e-02, 0.00000000e+00]], - - [[ 1.83770124e-01, 2.56357594e-01, 0.00000000e+00], - [ 4.35845892e-01, 6.14958970e-01, 0.00000000e+00], - [ 3.07662204e-03, 3.58312786e-02, 0.00000000e+00]], - - [[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00], - [ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00], - [ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00]]], - - - [[[ 3.41883175e-01, 5.97977973e-01, 0.00000000e+00], - [ 5.72071865e-01, 8.58566538e-01, 0.00000000e+00], - [ 3.46939323e-03, 4.04056116e-02, 0.00000000e+00]], - - [[ 3.58861043e-01, 6.28962785e-01, 0.00000000e+00], - [ 8.80697705e-01, 1.47373158e+00, 0.00000000e+00], - [ 2.22868504e-01, 1.18008528e-01, 0.00000000e+00]], - - [[ 2.91835067e-03, 2.60268355e-02, 0.00000000e+00], - [ 3.63686503e-02, 1.07959459e-01, 0.00000000e+00], - [ 1.88555613e-02, 7.06358976e-03, 0.00000000e+00]]], - - - [[[ 3.13810608e-03, 2.11731327e-02, 0.00000000e+00], - [ 6.71606255e-03, 4.53139824e-02, 0.00000000e+00], - [ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00]], - - [[ 7.05946179e-03, 5.44614852e-02, 0.00000000e+00], - [ 1.09099593e-01, 1.95935584e-01, 0.00000000e+00], - [ 6.61257395e-02, 2.47717418e-02, 0.00000000e+00]], - - [[ 6.38695629e-04, 5.69610302e-03, 0.00000000e+00], - [ 1.00358265e-02, 2.44053065e-02, 0.00000000e+00], - [ 5.67244468e-03, 2.12498697e-03, 0.00000000e+00]]]]) + >>> np.allclose(c, + ... [[[[ 1.77163904e-01, 1.87720108e-01, 0.00000000e+00], + ... [ 5.72573585e-01, 6.09557834e-01, 0.00000000e+00], + ... [ 3.48549923e-03, 4.05931870e-02, 0.00000000e+00]], + ... [[ 1.83770124e-01, 2.56357594e-01, 0.00000000e+00], + ... [ 4.35845892e-01, 6.14958970e-01, 0.00000000e+00], + ... [ 3.07662204e-03, 3.58312786e-02, 0.00000000e+00]], + ... [[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00], + ... [ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00], + ... [ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00]]], + ... [[[ 3.41883175e-01, 5.97977973e-01, 0.00000000e+00], + ... [ 5.72071865e-01, 8.58566538e-01, 0.00000000e+00], + ... [ 3.46939323e-03, 4.04056116e-02, 0.00000000e+00]], + ... [[ 3.58861043e-01, 6.28962785e-01, 0.00000000e+00], + ... [ 8.80697705e-01, 1.47373158e+00, 0.00000000e+00], + ... [ 2.22868504e-01, 1.18008528e-01, 0.00000000e+00]], + ... [[ 2.91835067e-03, 2.60268355e-02, 0.00000000e+00], + ... [ 3.63686503e-02, 1.07959459e-01, 0.00000000e+00], + ... [ 1.88555613e-02, 7.06358976e-03, 0.00000000e+00]]], + ... [[[ 3.13810608e-03, 2.11731327e-02, 0.00000000e+00], + ... [ 6.71606255e-03, 4.53139824e-02, 0.00000000e+00], + ... [ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00]], + ... [[ 7.05946179e-03, 5.44614852e-02, 0.00000000e+00], + ... [ 1.09099593e-01, 1.95935584e-01, 0.00000000e+00], + ... [ 6.61257395e-02, 2.47717418e-02, 0.00000000e+00]], + ... [[ 6.38695629e-04, 5.69610302e-03, 0.00000000e+00], + ... [ 1.00358265e-02, 2.44053065e-02, 0.00000000e+00], + ... [ 5.67244468e-03, 2.12498697e-03, 0.00000000e+00]]]]) + True h = plb.plot(x, c, '.') # 1D histogram