From 1cf304cee298e2752ce29c83e5201f593722c3af Mon Sep 17 00:00:00 2001 From: RĂ©mi Flamary Date: Mon, 24 Jul 2017 13:40:51 +0200 Subject: add tests for utils --- test/test_utils.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 test/test_utils.py diff --git a/test/test_utils.py b/test/test_utils.py new file mode 100644 index 0000000..3219fce --- /dev/null +++ b/test/test_utils.py @@ -0,0 +1,76 @@ + + +import ot +import numpy as np + +# import pytest + + +def test_parmap(): + + n = 100 + + def f(i): + return 1.0 * i * i + + a = np.arange(n) + + l1 = map(f, a) + + l2 = ot.utils.parmap(f, a) + + assert np.allclose(l1, l2) + + +def test_tic_toc(): + + import time + + ot.tic() + time.sleep(0.5) + t = ot.toc() + t2 = ot.toq() + + # test timing + assert np.allclose(0.5, t, rtol=1e-2, atol=1e-2) + + # test toc vs toq + assert np.allclose(t, t2, rtol=1e-2, atol=1e-2) + + +def test_kernel(): + + n = 100 + + x = np.random.randn(n, 2) + + K = ot.utils.kernel(x, x) + + # gaussian kernel has ones on the diagonal + assert np.allclose(np.diag(K), np.ones(n)) + + +def test_unif(): + + n = 100 + + u = ot.unif(n) + + assert np.allclose(1, np.sum(u)) + + +def test_dist(): + + n = 100 + + x = np.random.randn(n, 2) + + D = np.zeros((n, n)) + for i in range(n): + for j in range(n): + D[i, j] = np.sum(np.square(x[i, :] - x[j, :])) + + D2 = ot.dist(x, x) + + # dist shoul return squared euclidean + assert np.allclose(D, D2) -- cgit v1.2.3