1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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)
|