Skip to content

Commit ea435eb

Browse files
author
Giorgio Savastano
committed
testing
1 parent 36e1067 commit ea435eb

4 files changed

Lines changed: 75 additions & 63 deletions

File tree

Cargo.lock

Lines changed: 14 additions & 48 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ crate-type = ["cdylib"]
1111
pyo3 = { version = "0.21.2", features = ["extension-module", "multiple-pymethods"] }
1212
ndarray = { version = "0.15.6", features = ["rayon"] }
1313
numpy = "0.21.0"
14-
pathfinding = "4.9.1"
14+
pathfinding = "4.10.0"
1515
ordered-float = "4.2.0"
1616

1717
[build-dependencies]

src/emd_classification.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,18 @@ pub fn compute_emd_between_2dtensors(
109109
x: ArrayView2<'_, f64>,
110110
y: ArrayView2<'_, f64>,
111111
) -> Result<OrderedFloat<f64>, MatrixFormatError> {
112-
let costs = euclidean_rdist_rust(x, y);
113-
let weights = Matrix::from_vec(costs.nrows(), costs.ncols(), costs.into_raw_vec())?;
112+
let x_rows = x.nrows();
113+
let y_rows = y.nrows();
114+
115+
let mut costs = Vec::with_capacity(x_rows * y_rows);
116+
117+
for row_x in x.rows() {
118+
for row_y in y.rows() {
119+
let dist = euclidean_distance(&row_x, &row_y);
120+
costs.push(OrderedFloat::from(dist));
121+
}
122+
}
123+
let weights = Matrix::from_vec(x_rows, y_rows, costs)?;
114124
let (emd_dist, _) = kuhn_munkres_min(&weights);
115125
Ok(emd_dist)
116126
}

tests/test_benchmark.py

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,63 @@ def compute_earth_mover_dist(first, second):
1313
return d[row_ind, col_ind].sum()
1414

1515

16-
def setup_data():
16+
def setup_data(rows, cols):
1717
"""Generates random data for benchmarking."""
1818
rng = np.random.default_rng()
19-
data1 = rng.random((50, 50))
20-
data2 = rng.random((50, 50))
19+
data1 = rng.random((rows, cols), dtype=np.float64)
20+
data2 = rng.random((rows, cols), dtype=np.float64)
2121
return data1, data2
2222

2323

24-
def test_rust_emd_benchmark(benchmark):
25-
"""Benchmark the Rust-backed EMD calculation."""
26-
data1, data2 = setup_data()
24+
def test_rust_emd_benchmark_small(benchmark):
25+
"""Benchmark the Rust-backed EMD calculation on small data."""
26+
data1, data2 = setup_data(17, 11)
2727
benchmark(compute_earth_movers_distance_2d, data1, data2, False)
2828

2929

30-
def test_rust_emd_par_benchmark(benchmark):
31-
"""Benchmark the Rust-backed EMD calculation."""
32-
data1, data2 = setup_data()
30+
def test_rust_emd_benchmark_medium(benchmark):
31+
"""Benchmark the Rust-backed EMD calculation on medium data."""
32+
data1, data2 = setup_data(50, 50)
33+
benchmark(compute_earth_movers_distance_2d, data1, data2, False)
34+
35+
36+
def test_rust_emd_benchmark_large(benchmark):
37+
"""Benchmark the Rust-backed EMD calculation on large data."""
38+
data1, data2 = setup_data(100, 100)
39+
benchmark(compute_earth_movers_distance_2d, data1, data2, False)
40+
41+
42+
def test_rust_emd_par_benchmark_small(benchmark):
43+
"""Benchmark the Rust-backed EMD calculation with parallel processing on small data."""
44+
data1, data2 = setup_data(17, 11)
45+
benchmark(compute_earth_movers_distance_2d, data1, data2, True)
46+
47+
48+
def test_rust_emd_par_benchmark_medium(benchmark):
49+
"""Benchmark the Rust-backed EMD calculation with parallel processing on medium data."""
50+
data1, data2 = setup_data(50, 50)
3351
benchmark(compute_earth_movers_distance_2d, data1, data2, True)
3452

3553

36-
def test_numpy_emd_benchmark(benchmark):
37-
"""Benchmark the numpy/scipy EMD calculation."""
38-
data1, data2 = setup_data()
54+
def test_rust_emd_par_benchmark_large(benchmark):
55+
"""Benchmark the Rust-backed EMD calculation with parallel processing on large data."""
56+
data1, data2 = setup_data(100, 100)
57+
benchmark(compute_earth_movers_distance_2d, data1, data2, True)
58+
59+
60+
def test_numpy_emd_benchmark_small(benchmark):
61+
"""Benchmark the numpy/scipy EMD calculation on small data."""
62+
data1, data2 = setup_data(17, 11)
63+
benchmark(compute_earth_mover_dist, data1, data2)
64+
65+
66+
def test_numpy_emd_benchmark_medium(benchmark):
67+
"""Benchmark the numpy/scipy EMD calculation on medium data."""
68+
data1, data2 = setup_data(50, 50)
69+
benchmark(compute_earth_mover_dist, data1, data2)
70+
71+
72+
def test_numpy_emd_benchmark_large(benchmark):
73+
"""Benchmark the numpy/scipy EMD calculation on large data."""
74+
data1, data2 = setup_data(100, 100)
3975
benchmark(compute_earth_mover_dist, data1, data2)

0 commit comments

Comments
 (0)