diff options
author | Laetitia Chapel <laetitia.chapel@univ-ubs.fr> | 2020-04-09 14:14:34 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-09 14:14:34 +0200 |
commit | fff2463aafd58343c8bc2ed7875622e16a8c1cee (patch) | |
tree | b23efef253c4cc42c13bf3f7aad671f27bf43a3d /test | |
parent | 9f63ee92e281427ab3d520f75bb9c3406b547365 (diff) | |
parent | 4cd4e09f89fe6f95a07d632365612b797ab760da (diff) |
Merge branch 'master' into partial-W-and-GW
Diffstat (limited to 'test')
-rw-r--r-- | test/test_bregman.py | 7 | ||||
-rw-r--r-- | test/test_ot.py | 48 |
2 files changed, 47 insertions, 8 deletions
diff --git a/test/test_bregman.py b/test/test_bregman.py index f54ba9f..ec4388d 100644 --- a/test/test_bregman.py +++ b/test/test_bregman.py @@ -351,3 +351,10 @@ def test_screenkhorn(): # check marginals np.testing.assert_allclose(G_sink.sum(0), G_screen.sum(0), atol=1e-02) np.testing.assert_allclose(G_sink.sum(1), G_screen.sum(1), atol=1e-02) + + +def test_convolutional_barycenter_non_square(): + # test for image with height not equal width + A = np.ones((2, 2, 3)) / (2 * 3) + b = ot.bregman.convolutional_barycenter2d(A, 1e-03) + np.testing.assert_allclose(np.ones((2, 3)) / (2 * 3), b, atol=1e-02) diff --git a/test/test_ot.py b/test/test_ot.py index 47df946..0f1357f 100644 --- a/test/test_ot.py +++ b/test/test_ot.py @@ -7,11 +7,11 @@ import warnings import numpy as np +import pytest from scipy.stats import wasserstein_distance import ot from ot.datasets import make_1D_gauss as gauss -import pytest def test_emd_dimension_mismatch(): @@ -75,12 +75,12 @@ def test_emd_1d_emd2_1d(): np.testing.assert_allclose(wass, wass1d_emd2) # check loss is similar to scipy's implementation for Euclidean metric - wass_sp = wasserstein_distance(u.reshape((-1, )), v.reshape((-1, ))) + wass_sp = wasserstein_distance(u.reshape((-1,)), v.reshape((-1,))) np.testing.assert_allclose(wass_sp, wass1d_euc) # check constraints - np.testing.assert_allclose(np.ones((n, )) / n, G.sum(1)) - np.testing.assert_allclose(np.ones((m, )) / m, G.sum(0)) + np.testing.assert_allclose(np.ones((n,)) / n, G.sum(1)) + np.testing.assert_allclose(np.ones((m,)) / m, G.sum(0)) # check G is similar np.testing.assert_allclose(G, G_1d) @@ -92,6 +92,42 @@ def test_emd_1d_emd2_1d(): ot.emd_1d(u, v, [], []) +def test_emd_1d_emd2_1d_with_weights(): + # test emd1d gives similar results as emd + n = 20 + m = 30 + rng = np.random.RandomState(0) + u = rng.randn(n, 1) + v = rng.randn(m, 1) + + w_u = rng.uniform(0., 1., n) + w_u = w_u / w_u.sum() + + w_v = rng.uniform(0., 1., m) + w_v = w_v / w_v.sum() + + M = ot.dist(u, v, metric='sqeuclidean') + + G, log = ot.emd(w_u, w_v, M, log=True) + wass = log["cost"] + G_1d, log = ot.emd_1d(u, v, w_u, w_v, metric='sqeuclidean', log=True) + wass1d = log["cost"] + wass1d_emd2 = ot.emd2_1d(u, v, w_u, w_v, metric='sqeuclidean', log=False) + wass1d_euc = ot.emd2_1d(u, v, w_u, w_v, metric='euclidean', log=False) + + # check loss is similar + np.testing.assert_allclose(wass, wass1d) + np.testing.assert_allclose(wass, wass1d_emd2) + + # check loss is similar to scipy's implementation for Euclidean metric + wass_sp = wasserstein_distance(u.reshape((-1,)), v.reshape((-1,)), w_u, w_v) + np.testing.assert_allclose(wass_sp, wass1d_euc) + + # check constraints + np.testing.assert_allclose(w_u, G.sum(1)) + np.testing.assert_allclose(w_v, G.sum(0)) + + def test_wass_1d(): # test emd1d gives similar results as emd n = 20 @@ -135,7 +171,6 @@ def test_emd_empty(): def test_emd_sparse(): - n = 100 rng = np.random.RandomState(0) @@ -211,7 +246,6 @@ def test_emd2_multi(): def test_lp_barycenter(): - a1 = np.array([1.0, 0, 0])[:, None] a2 = np.array([0, 0, 1.0])[:, None] @@ -228,7 +262,6 @@ def test_lp_barycenter(): def test_free_support_barycenter(): - measures_locations = [np.array([-1.]).reshape((1, 1)), np.array([1.]).reshape((1, 1))] measures_weights = [np.array([1.]), np.array([1.])] @@ -244,7 +277,6 @@ def test_free_support_barycenter(): @pytest.mark.skipif(not ot.lp.cvx.cvxopt, reason="No cvxopt available") def test_lp_barycenter_cvxopt(): - a1 = np.array([1.0, 0, 0])[:, None] a2 = np.array([0, 0, 1.0])[:, None] |