diff options
author | Rémi Flamary <remi.flamary@gmail.com> | 2017-07-24 13:40:51 +0200 |
---|---|---|
committer | Rémi Flamary <remi.flamary@gmail.com> | 2017-07-24 13:40:51 +0200 |
commit | 1cf304cee298e2752ce29c83e5201f593722c3af (patch) | |
tree | 6d1750602b1ffa43ca71aa4ec385c99a014761f8 | |
parent | d9205c886219d5410bc4705b46d9f14710c81ddd (diff) |
add tests for utils
-rw-r--r-- | test/test_utils.py | 76 |
1 files changed, 76 insertions, 0 deletions
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) |