Skip to content

Commit 3f4774a

Browse files
authored
test(distance): unit tests for distance (#216)
1 parent eb92f14 commit 3f4774a

File tree

4 files changed

+402
-0
lines changed

4 files changed

+402
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import numpy as np
2+
import pytest
3+
from scipy.sparse import csr_matrix
4+
5+
from docarray.math.distance.numpy import (
6+
cosine,
7+
euclidean,
8+
sparse_cosine,
9+
sparse_euclidean,
10+
sparse_sqeuclidean,
11+
sqeuclidean,
12+
)
13+
14+
15+
@pytest.mark.parametrize(
16+
'x_mat, y_mat, result',
17+
(
18+
(
19+
np.array([[1, 2, 3], [4, 5, 6]]),
20+
np.array([[1, 2, 3], [4, 5, 6]]),
21+
np.array(
22+
[[0.00000000e00, 2.53681537e-02], [2.53681537e-02, 2.22044605e-16]]
23+
),
24+
),
25+
(np.array([[1, 2, 3]]), np.array([[1, 2, 3]]), np.array([[0]])),
26+
(np.array([[0, 0, 0]]), np.array([[0, 0, 0]]), np.array([[0]])),
27+
(np.array([[1, 2, 3]]), np.array([[19, 53, 201]]), np.array([[0.06788693]])),
28+
),
29+
)
30+
def test_cosine(x_mat, y_mat, result):
31+
np.testing.assert_allclose(cosine(x_mat, y_mat), result, rtol=1e-5)
32+
33+
34+
@pytest.mark.parametrize(
35+
'x_mat, y_mat, result',
36+
(
37+
(
38+
csr_matrix([[1, 2, 3], [4, 5, 6]]),
39+
csr_matrix([[1, 2, 3], [4, 5, 6]]),
40+
np.array(
41+
[[0.00000000e00, 2.53681537e-02], [2.53681537e-02, 2.22044605e-16]]
42+
),
43+
),
44+
(csr_matrix([[1, 2, 3]]), csr_matrix([[1, 2, 3]]), np.array([[0]])),
45+
(csr_matrix([[0, 0, 0]]), csr_matrix([[0, 0, 0]]), np.array([[np.nan]])),
46+
(
47+
csr_matrix([[1, 2, 3]]),
48+
csr_matrix([[19, 53, 201]]),
49+
np.array([[0.06788693]]),
50+
),
51+
),
52+
)
53+
def test_sparse_cosine(x_mat, y_mat, result):
54+
np.testing.assert_allclose(sparse_cosine(x_mat, y_mat), result, rtol=1e-5)
55+
56+
57+
@pytest.mark.parametrize(
58+
'x_mat, y_mat, result',
59+
(
60+
(
61+
np.array([[1, 2, 3], [4, 5, 6]]),
62+
np.array([[1, 2, 3], [4, 5, 6]]),
63+
np.array([[0, 27], [27, 0]]),
64+
),
65+
(np.array([[1, 2, 3]]), np.array([[1, 2, 3]]), np.array([[0]])),
66+
(np.array([[0, 0, 0]]), np.array([[0, 0, 0]]), np.array([[0]])),
67+
(np.array([[1, 2, 3]]), np.array([[19, 53, 201]]), np.array([[42129]])),
68+
),
69+
)
70+
def test_sqeuclidean(x_mat, y_mat, result):
71+
np.testing.assert_allclose(sqeuclidean(x_mat, y_mat), result, rtol=1e-5)
72+
73+
74+
@pytest.mark.parametrize(
75+
'x_mat, y_mat, result',
76+
(
77+
(
78+
csr_matrix([[1, 2, 3], [4, 5, 6]]),
79+
csr_matrix([[1, 2, 3], [4, 5, 6]]),
80+
np.array([[0, 27], [27, 0]]),
81+
),
82+
(csr_matrix([[1, 2, 3]]), csr_matrix([[1, 2, 3]]), np.array([[0]])),
83+
(csr_matrix([[0, 0, 0]]), csr_matrix([[0, 0, 0]]), np.array([[0]])),
84+
(csr_matrix([[1, 2, 3]]), csr_matrix([[19, 53, 201]]), np.array([[42129]])),
85+
),
86+
)
87+
def test_sparse_sqeuclidean(x_mat, y_mat, result):
88+
np.testing.assert_allclose(sparse_sqeuclidean(x_mat, y_mat), result, rtol=1e-5)
89+
90+
91+
@pytest.mark.parametrize(
92+
'x_mat, y_mat, result',
93+
(
94+
(
95+
np.array([[1, 2, 3], [4, 5, 6]]),
96+
np.array([[1, 2, 3], [4, 5, 6]]),
97+
np.array([[0, 5.19615242], [5.19615242, 0]]),
98+
),
99+
(np.array([[1, 2, 3]]), np.array([[1, 2, 3]]), np.array([[0]])),
100+
(np.array([[0, 0, 0]]), np.array([[0, 0, 0]]), np.array([[0]])),
101+
(np.array([[1, 2, 3]]), np.array([[19, 53, 201]]), np.array([[205.2535018]])),
102+
),
103+
)
104+
def test_euclidean(x_mat, y_mat, result):
105+
np.testing.assert_allclose(euclidean(x_mat, y_mat), result, rtol=1e-5)
106+
107+
108+
@pytest.mark.parametrize(
109+
'x_mat, y_mat, result',
110+
(
111+
(
112+
csr_matrix([[1, 2, 3], [4, 5, 6]]),
113+
csr_matrix([[1, 2, 3], [4, 5, 6]]),
114+
np.array([[0, 5.19615242], [5.19615242, 0]]),
115+
),
116+
(csr_matrix([[1, 2, 3]]), csr_matrix([[1, 2, 3]]), np.array([[0]])),
117+
(csr_matrix([[0, 0, 0]]), csr_matrix([[0, 0, 0]]), np.array([[0]])),
118+
(
119+
csr_matrix([[1, 2, 3]]),
120+
csr_matrix([[19, 53, 201]]),
121+
np.array([[205.2535018]]),
122+
),
123+
),
124+
)
125+
def test_sparse_euclidean(x_mat, y_mat, result):
126+
np.testing.assert_allclose(sparse_euclidean(x_mat, y_mat), result, rtol=1e-5)
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import numpy as np
2+
import paddle
3+
import pytest
4+
5+
from docarray.math.distance.paddle import cosine, euclidean, sqeuclidean
6+
7+
8+
@pytest.mark.parametrize(
9+
'x_mat, y_mat, result',
10+
(
11+
(
12+
paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype='float32'),
13+
paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype='float32'),
14+
np.array([[1.192093e-07, 2.53681537e-02], [2.53681537e-02, 0]]),
15+
),
16+
(
17+
paddle.to_tensor([[1, 2, 3]], dtype='float32'),
18+
paddle.to_tensor([[1, 2, 3]], dtype='float32'),
19+
np.array([[1.192093e-07]]),
20+
),
21+
(
22+
paddle.to_tensor([[0, 0, 0]], dtype='float32'),
23+
paddle.to_tensor([[0, 0, 0]], dtype='float32'),
24+
np.array([[1]]),
25+
),
26+
(
27+
paddle.to_tensor([[1, 2, 3]], dtype='float32'),
28+
paddle.to_tensor([[19, 53, 201]], dtype='float32'),
29+
np.array([[0.06788693]]),
30+
),
31+
),
32+
)
33+
def test_cosine(x_mat, y_mat, result):
34+
np.testing.assert_allclose(cosine(x_mat, y_mat), result, rtol=1e-5)
35+
36+
37+
@pytest.mark.parametrize(
38+
'x_mat, y_mat, result',
39+
(
40+
(
41+
paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype='float32'),
42+
paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype='float32'),
43+
np.array([[0, 27], [27, 0]]),
44+
),
45+
(
46+
paddle.to_tensor([[1, 2, 3]], dtype='float32'),
47+
paddle.to_tensor([[1, 2, 3]], dtype='float32'),
48+
np.array([[0]]),
49+
),
50+
(
51+
paddle.to_tensor([[0, 0, 0]], dtype='float32'),
52+
paddle.to_tensor([[0, 0, 0]], dtype='float32'),
53+
np.array([[0]]),
54+
),
55+
(
56+
paddle.to_tensor([[1, 2, 3]], dtype='float32'),
57+
paddle.to_tensor([[19, 53, 201]], dtype='float32'),
58+
np.array([[42129]]),
59+
),
60+
),
61+
)
62+
def test_sqeuclidean(x_mat, y_mat, result):
63+
np.testing.assert_allclose(sqeuclidean(x_mat, y_mat), result, rtol=1e-5)
64+
65+
66+
@pytest.mark.parametrize(
67+
'x_mat, y_mat, result',
68+
(
69+
(
70+
paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype='float32'),
71+
paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype='float32'),
72+
np.array([[0, 5.19615242], [5.19615242, 0]]),
73+
),
74+
(
75+
paddle.to_tensor([[1, 2, 3]], dtype='float32'),
76+
paddle.to_tensor([[1, 2, 3]], dtype='float32'),
77+
np.array([[0]]),
78+
),
79+
(
80+
paddle.to_tensor([[0, 0, 0]], dtype='float32'),
81+
paddle.to_tensor([[0, 0, 0]], dtype='float32'),
82+
np.array([[0]]),
83+
),
84+
(
85+
paddle.to_tensor([[1, 2, 3]], dtype='float32'),
86+
paddle.to_tensor([[19, 53, 201]], dtype='float32'),
87+
np.array([[205.2535018]]),
88+
),
89+
),
90+
)
91+
def test_euclidean(x_mat, y_mat, result):
92+
np.testing.assert_allclose(euclidean(x_mat, y_mat), result, rtol=1e-5)
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import numpy as np
2+
import pytest
3+
import tensorflow as tf
4+
5+
from docarray.math.distance.tensorflow import cosine, euclidean, sqeuclidean
6+
7+
8+
@pytest.mark.parametrize(
9+
'x_mat, y_mat, result',
10+
(
11+
(
12+
tf.constant([[1, 2, 3], [4, 5, 6]], dtype=tf.float32),
13+
tf.constant([[1, 2, 3], [4, 5, 6]], dtype=tf.float32),
14+
np.array([[1.192093e-07, 2.53681537e-02], [2.53681537e-02, 0.000000e00]]),
15+
),
16+
(
17+
tf.constant([[1, 2, 3]], dtype=tf.float32),
18+
tf.constant([[1, 2, 3]], dtype=tf.float32),
19+
np.array([[1.192093e-07]]),
20+
),
21+
(
22+
tf.constant([[0, 0, 0]], dtype=tf.float32),
23+
tf.constant([[0, 0, 0]], dtype=tf.float32),
24+
np.array([[1]]),
25+
),
26+
(
27+
tf.constant([[1, 2, 3]], dtype=tf.float32),
28+
tf.constant([[19, 53, 201]], dtype=tf.float32),
29+
np.array([[0.06788693]]),
30+
),
31+
),
32+
)
33+
def test_cosine(x_mat, y_mat, result):
34+
np.testing.assert_allclose(cosine(x_mat, y_mat), result, rtol=1e-5)
35+
36+
37+
@pytest.mark.parametrize(
38+
'x_mat, y_mat, result',
39+
(
40+
(
41+
tf.constant([[1, 2, 3], [4, 5, 6]], dtype=tf.float32),
42+
tf.constant([[1, 2, 3], [4, 5, 6]], dtype=tf.float32),
43+
np.array([[0, 27], [27, 0]]),
44+
),
45+
(
46+
tf.constant([[1, 2, 3]], dtype=tf.float32),
47+
tf.constant([[1, 2, 3]], dtype=tf.float32),
48+
np.array([[0]]),
49+
),
50+
(
51+
tf.constant([[0, 0, 0]], dtype=tf.float32),
52+
tf.constant([[0, 0, 0]], dtype=tf.float32),
53+
np.array([[0]]),
54+
),
55+
(
56+
tf.constant([[1, 2, 3]], dtype=tf.float32),
57+
tf.constant([[19, 53, 201]], dtype=tf.float32),
58+
np.array([[42129]]),
59+
),
60+
),
61+
)
62+
def test_sqeuclidean(x_mat, y_mat, result):
63+
np.testing.assert_allclose(sqeuclidean(x_mat, y_mat), result, rtol=1e-5)
64+
65+
66+
@pytest.mark.parametrize(
67+
'x_mat, y_mat, result',
68+
(
69+
(
70+
tf.constant([[1, 2, 3], [4, 5, 6]], dtype=tf.float32),
71+
tf.constant([[1, 2, 3], [4, 5, 6]], dtype=tf.float32),
72+
np.array([[0, 5.19615242], [5.19615242, 0]]),
73+
),
74+
(
75+
tf.constant([[1, 2, 3]], dtype=tf.float32),
76+
tf.constant([[1, 2, 3]], dtype=tf.float32),
77+
np.array([[0]]),
78+
),
79+
(
80+
tf.constant([[0, 0, 0]], dtype=tf.float32),
81+
tf.constant([[0, 0, 0]], dtype=tf.float32),
82+
np.array([[0]]),
83+
),
84+
(
85+
tf.constant([[1, 2, 3]], dtype=tf.float32),
86+
tf.constant([[19, 53, 201]], dtype=tf.float32),
87+
np.array([[205.2535018]]),
88+
),
89+
),
90+
)
91+
def test_euclidean(x_mat, y_mat, result):
92+
np.testing.assert_allclose(euclidean(x_mat, y_mat), result, rtol=1e-5)

0 commit comments

Comments
 (0)