Skip to content

Commit 1cf304c

Browse files
committed
add tests for utils
1 parent d9205c8 commit 1cf304c

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

test/test_utils.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
3+
import ot
4+
import numpy as np
5+
6+
# import pytest
7+
8+
9+
def test_parmap():
10+
11+
n = 100
12+
13+
def f(i):
14+
return 1.0 * i * i
15+
16+
a = np.arange(n)
17+
18+
l1 = map(f, a)
19+
20+
l2 = ot.utils.parmap(f, a)
21+
22+
assert np.allclose(l1, l2)
23+
24+
25+
def test_tic_toc():
26+
27+
import time
28+
29+
ot.tic()
30+
time.sleep(0.5)
31+
t = ot.toc()
32+
t2 = ot.toq()
33+
34+
# test timing
35+
assert np.allclose(0.5, t, rtol=1e-2, atol=1e-2)
36+
37+
# test toc vs toq
38+
assert np.allclose(t, t2, rtol=1e-2, atol=1e-2)
39+
40+
41+
def test_kernel():
42+
43+
n = 100
44+
45+
x = np.random.randn(n, 2)
46+
47+
K = ot.utils.kernel(x, x)
48+
49+
# gaussian kernel has ones on the diagonal
50+
assert np.allclose(np.diag(K), np.ones(n))
51+
52+
53+
def test_unif():
54+
55+
n = 100
56+
57+
u = ot.unif(n)
58+
59+
assert np.allclose(1, np.sum(u))
60+
61+
62+
def test_dist():
63+
64+
n = 100
65+
66+
x = np.random.randn(n, 2)
67+
68+
D = np.zeros((n, n))
69+
for i in range(n):
70+
for j in range(n):
71+
D[i, j] = np.sum(np.square(x[i, :] - x[j, :]))
72+
73+
D2 = ot.dist(x, x)
74+
75+
# dist shoul return squared euclidean
76+
assert np.allclose(D, D2)

0 commit comments

Comments
 (0)