Skip to content

Commit 159257d

Browse files
authored
Merge pull request #136 from lucasimi/develop
Added benchmark for custom metrics
2 parents 015d384 + 0521a7f commit 159257d

1 file changed

Lines changed: 103 additions & 0 deletions

File tree

tests/test_bench_metrics.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import unittest
2+
import timeit
3+
4+
import pandas as pd
5+
import numpy as np
6+
7+
import tdamapper.utils.metrics as metrics
8+
9+
10+
def euclidean_numpy(a, b):
11+
return np.sqrt(np.sum((a - b) ** 2))
12+
13+
14+
def euclidean_numpy_linalg(a, b):
15+
return np.linalg.norm(a - b)
16+
17+
18+
def manhattan_numpy(a, b):
19+
return np.sum(np.abs(a - b))
20+
21+
22+
def manhattan_numpy_linalg(a, b):
23+
return np.linalg.norm(a - b, ord=1)
24+
25+
26+
def chebyshev_numpy(a, b):
27+
return np.max(np.abs(a - b))
28+
29+
30+
def chebyshev_numpy_linalg(a, b):
31+
return np.linalg.norm(a - b, ord=np.inf)
32+
33+
34+
def eval_dist(X, d):
35+
for i in range(X.shape[0] - 1):
36+
d(X[i], X[i+1])
37+
38+
39+
def run_dist_bench(X, d):
40+
return timeit.timeit(lambda: eval_dist(X, d), number=30)
41+
42+
43+
def run_euclidean_bench(X):
44+
t_numpy = run_dist_bench(X, euclidean_numpy)
45+
t_numpy_linalg = run_dist_bench(X, euclidean_numpy_linalg)
46+
t_tdamapper = run_dist_bench(X, metrics.euclidean())
47+
return {
48+
'metric': 'euclidean',
49+
'numpy': t_numpy,
50+
'numpy_linalg': t_numpy_linalg,
51+
'tdamapper': t_tdamapper,
52+
}
53+
54+
55+
def run_chebyshev_bench(X):
56+
t_numpy = run_dist_bench(X, chebyshev_numpy)
57+
t_numpy_linalg = run_dist_bench(X, chebyshev_numpy_linalg)
58+
t_tdamapper = run_dist_bench(X, metrics.chebyshev())
59+
return {
60+
'metric': 'chebyshev',
61+
'numpy': t_numpy,
62+
'numpy_linalg': t_numpy_linalg,
63+
'tdamapper': t_tdamapper,
64+
}
65+
66+
67+
def run_manhattan_bench(X):
68+
t_numpy = run_dist_bench(X, manhattan_numpy)
69+
t_numpy_linalg = run_dist_bench(X, manhattan_numpy_linalg)
70+
t_tdamapper = run_dist_bench(X, metrics.manhattan())
71+
return {
72+
'metric': 'manhattan',
73+
'numpy': t_numpy,
74+
'numpy_linalg': t_numpy_linalg,
75+
'tdamapper': t_tdamapper,
76+
}
77+
78+
79+
def merge(d, d_part):
80+
for k, v in d_part.items():
81+
if k not in d:
82+
d[k] = []
83+
d[k].append(v)
84+
return d
85+
86+
87+
def run_bench(X):
88+
d = {}
89+
d_part = run_euclidean_bench(X)
90+
merge(d, d_part)
91+
d_part = run_chebyshev_bench(X)
92+
merge(d, d_part)
93+
d_part = run_manhattan_bench(X)
94+
merge(d, d_part)
95+
return pd.DataFrame(d)
96+
97+
98+
class TestBenchMetrics(unittest.TestCase):
99+
100+
def test_bench(self):
101+
X = np.random.rand(1000, 1000)
102+
df_bench = run_bench(X)
103+
print(df_bench)

0 commit comments

Comments
 (0)