From b2f91f24796a996a82db41e91f56ba6a51989159 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Mon, 24 Jul 2017 14:26:25 +0200 Subject: full coveragre utils --- Makefile | 4 ++-- test/test_gpu.py | 18 ++++++++++++++---- test/test_ot.py | 4 +++- test/test_utils.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 577bbbe..98f5614 100644 --- a/Makefile +++ b/Makefile @@ -38,10 +38,10 @@ pep8 : flake8 examples/ ot/ test/ test : FORCE pep8 - python -m py.test -v test/ + python -m py.test -v test/ --cov=ot --cov-report html:cov_html pytest : FORCE - python -m py.test -v test/ + python -m py.test -v test/ --cov=ot uploadpypi : #python setup.py register diff --git a/test/test_gpu.py b/test/test_gpu.py index 312a2d4..49b98d0 100644 --- a/test/test_gpu.py +++ b/test/test_gpu.py @@ -3,8 +3,14 @@ import numpy as np import time import pytest +try: # test if cudamat installed + import ot.gpu + nogpu = False +except ImportError: + nogpu = True + -@pytest.mark.skip(reason="No way to test GPU on travis yet") +@pytest.mark.skipif(nogpu, reason="No GPU available") def test_gpu_sinkhorn(): import ot.gpu @@ -12,7 +18,7 @@ def test_gpu_sinkhorn(): print("min:{:.3E}, max::{:.3E}, mean::{:.3E}, std::{:.3E}".format( np.min(r), np.max(r), np.mean(r), np.std(r))) - for n in [5000]: + for n in [50, 100, 500, 1000]: print(n) a = np.random.rand(n // 4, 100) b = np.random.rand(n, 100) @@ -30,14 +36,16 @@ def test_gpu_sinkhorn(): print(" GPU sinkhorn, time: {:6.2f} sec ".format(time3 - time2)) describeRes(G2) + assert np.allclose(G1, G2, rtol=1e-5, atol=1e-5) -@pytest.mark.skip(reason="No way to test GPU on travis yet") + +@pytest.mark.skipif(nogpu, reason="No GPU available") def test_gpu_sinkhorn_lpl1(): def describeRes(r): print("min:{:.3E}, max:{:.3E}, mean:{:.3E}, std:{:.3E}" .format(np.min(r), np.max(r), np.mean(r), np.std(r))) - for n in [5000]: + for n in [50, 100, 500, 1000]: print(n) a = np.random.rand(n // 4, 100) labels_a = np.random.randint(10, size=(n // 4)) @@ -57,3 +65,5 @@ def test_gpu_sinkhorn_lpl1(): print(" GPU sinkhorn lpl1, time: {:6.2f} sec ".format( time3 - time2)) describeRes(G2) + + assert np.allclose(G1, G2, rtol=1e-5, atol=1e-5) diff --git a/test/test_ot.py b/test/test_ot.py index 3fa1bc4..16fd510 100644 --- a/test/test_ot.py +++ b/test/test_ot.py @@ -31,9 +31,11 @@ def test_emd_emd2(): # check G is identity assert np.allclose(G, np.eye(n) / n) + # check constratints + assert np.allclose(u, G.sum(1)) # cf convergence sinkhorn + assert np.allclose(u, G.sum(0)) # cf convergence sinkhorn w = ot.emd2(u, u, M) - # check loss=0 assert np.allclose(w, 0) diff --git a/test/test_utils.py b/test/test_utils.py index 3219fce..e85e5b7 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -71,6 +71,52 @@ def test_dist(): D[i, j] = np.sum(np.square(x[i, :] - x[j, :])) D2 = ot.dist(x, x) + D3 = ot.dist(x) # dist shoul return squared euclidean assert np.allclose(D, D2) + assert np.allclose(D, D3) + + +def test_dist0(): + + n = 100 + M = ot.utils.dist0(n, method='lin_square') + + # dist0 default to linear sampling with quadratic loss + assert np.allclose(M[0, -1], (n - 1) * (n - 1)) + + +def test_dots(): + + n1, n2, n3, n4 = 100, 50, 200, 100 + + A = np.random.randn(n1, n2) + B = np.random.randn(n2, n3) + C = np.random.randn(n3, n4) + + X1 = ot.utils.dots(A, B, C) + + X2 = A.dot(B.dot(C)) + + assert np.allclose(X1, X2) + + +def test_clean_zeros(): + + n = 100 + nz = 50 + nz2 = 20 + u1 = ot.unif(n) + u1[:nz] = 0 + u1 = u1 / u1.sum() + u2 = ot.unif(n) + u2[:nz2] = 0 + u2 = u2 / u2.sum() + + M = ot.utils.dist0(n) + + a, b, M2 = ot.utils.clean_zeros(u1, u2, M) + + assert len(a) == n - nz + assert len(b) == n - nz2 -- cgit v1.2.3 From c6e648fbebd1297428f514d7bd48d3eb8814aafd Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Mon, 24 Jul 2017 14:31:00 +0200 Subject: test parmap python 3.5 --- test/test_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_utils.py b/test/test_utils.py index e85e5b7..1a1ab02 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -15,9 +15,9 @@ def test_parmap(): a = np.arange(n) - l1 = map(f, a) + l1 = np.array(map(f, a)) - l2 = ot.utils.parmap(f, a) + l2 = np.array(ot.utils.parmap(f, a)) assert np.allclose(l1, l2) -- cgit v1.2.3 From a31d3c2375ffec7eb3754ab4b66f75ce9a51eddd Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Mon, 24 Jul 2017 14:35:20 +0200 Subject: map to list --- test/test_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_utils.py b/test/test_utils.py index 1a1ab02..0883a8e 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -15,9 +15,9 @@ def test_parmap(): a = np.arange(n) - l1 = np.array(map(f, a)) + l1 = list(map(f, a)) - l2 = np.array(ot.utils.parmap(f, a)) + l2 = list(ot.utils.parmap(f, a)) assert np.allclose(l1, l2) -- cgit v1.2.3 From f8e822c48eff02a3d65fc83d09dc0471bc9555aa Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Mon, 24 Jul 2017 14:49:14 +0200 Subject: test sinkhorn with empty marginals --- test/test_bregman.py | 31 ++++++++++++++++++++++++++++++- test/test_ot.py | 23 +++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/test/test_bregman.py b/test/test_bregman.py index fd2c972..b65de11 100644 --- a/test/test_bregman.py +++ b/test/test_bregman.py @@ -23,6 +23,33 @@ def test_sinkhorn(): assert np.allclose(u, G.sum(0), atol=1e-05) # cf convergence sinkhorn +def test_sinkhorn_empty(): + # test sinkhorn + n = 100 + np.random.seed(0) + + x = np.random.randn(n, 2) + u = ot.utils.unif(n) + + M = ot.dist(x, x) + + G = ot.sinkhorn([], [], M, 1, stopThr=1e-10) + # check constratints + assert np.allclose(u, G.sum(1), atol=1e-05) # cf convergence sinkhorn + assert np.allclose(u, G.sum(0), atol=1e-05) # cf convergence sinkhorn + + G = ot.sinkhorn([], [], M, 1, stopThr=1e-10, method='sinkhorn_stabilized') + # check constratints + assert np.allclose(u, G.sum(1), atol=1e-05) # cf convergence sinkhorn + assert np.allclose(u, G.sum(0), atol=1e-05) # cf convergence sinkhorn + + G = ot.sinkhorn( + [], [], M, 1, stopThr=1e-10, method='sinkhorn_epsilon_scaling') + # check constratints + assert np.allclose(u, G.sum(1), atol=1e-05) # cf convergence sinkhorn + assert np.allclose(u, G.sum(0), atol=1e-05) # cf convergence sinkhorn + + def test_sinkhorn_variants(): # test sinkhorn n = 100 @@ -37,7 +64,9 @@ def test_sinkhorn_variants(): Gs = ot.sinkhorn(u, u, M, 1, method='sinkhorn_stabilized', stopThr=1e-10) Ges = ot.sinkhorn( u, u, M, 1, method='sinkhorn_epsilon_scaling', stopThr=1e-10) + Gerr = ot.sinkhorn(u, u, M, 1, method='do_not_exists', stopThr=1e-10) - # check constratints + # check values assert np.allclose(G0, Gs, atol=1e-05) assert np.allclose(G0, Ges, atol=1e-05) + assert np.allclose(G0, Gerr) diff --git a/test/test_ot.py b/test/test_ot.py index 16fd510..3897397 100644 --- a/test/test_ot.py +++ b/test/test_ot.py @@ -40,6 +40,29 @@ def test_emd_emd2(): assert np.allclose(w, 0) +def test_emd_empty(): + # test emd and emd2 for simple identity + n = 100 + np.random.seed(0) + + x = np.random.randn(n, 2) + u = ot.utils.unif(n) + + M = ot.dist(x, x) + + G = ot.emd([], [], M) + + # check G is identity + assert np.allclose(G, np.eye(n) / n) + # check constratints + assert np.allclose(u, G.sum(1)) # cf convergence sinkhorn + assert np.allclose(u, G.sum(0)) # cf convergence sinkhorn + + w = ot.emd2([], [], M) + # check loss=0 + assert np.allclose(w, 0) + + def test_emd2_multi(): from ot.datasets import get_1D_gauss as gauss -- cgit v1.2.3 From 7d9c5e7ef81cfb1cd4725058c09a7f683ca03eef Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Mon, 24 Jul 2017 14:58:15 +0200 Subject: add test optim --- test/test_optim.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 test/test_optim.py diff --git a/test/test_optim.py b/test/test_optim.py new file mode 100644 index 0000000..43cba7d --- /dev/null +++ b/test/test_optim.py @@ -0,0 +1,65 @@ + + +import ot +import numpy as np + +# import pytest + + +def test_conditional_gradient(): + + n = 100 # nb bins + + # bin positions + x = np.arange(n, dtype=np.float64) + + # Gaussian distributions + a = ot.datasets.get_1D_gauss(n, m=20, s=5) # m= mean, s= std + b = ot.datasets.get_1D_gauss(n, m=60, s=10) + + # loss matrix + M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1))) + M /= M.max() + + def f(G): + return 0.5 * np.sum(G**2) + + def df(G): + return G + + reg = 1e-1 + + G, log = ot.optim.cg(a, b, M, reg, f, df, verbose=True, log=True) + + assert np.allclose(a, G.sum(1)) + assert np.allclose(b, G.sum(0)) + + +def test_generalized_conditional_gradient(): + + n = 100 # nb bins + + # bin positions + x = np.arange(n, dtype=np.float64) + + # Gaussian distributions + a = ot.datasets.get_1D_gauss(n, m=20, s=5) # m= mean, s= std + b = ot.datasets.get_1D_gauss(n, m=60, s=10) + + # loss matrix + M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1))) + M /= M.max() + + def f(G): + return 0.5 * np.sum(G**2) + + def df(G): + return G + + reg1 = 1e-3 + reg2 = 1e-1 + + G, log = ot.optim.gcg(a, b, M, reg1, reg2, f, df, verbose=True, log=True) + + assert np.allclose(a, G.sum(1), atol=1e-05) + assert np.allclose(b, G.sum(0), atol=1e-05) -- cgit v1.2.3 From 709d8cbc9f9961a5175eb64ae497b854e0b9b184 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Mon, 24 Jul 2017 15:14:59 +0200 Subject: add dr tests --- test/test_dr.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/test_gpu.py | 53 +++++++++++++++++++++++---------------------- test/test_optim.py | 4 ++-- 3 files changed, 93 insertions(+), 27 deletions(-) create mode 100644 test/test_dr.py diff --git a/test/test_dr.py b/test/test_dr.py new file mode 100644 index 0000000..24ccaa1 --- /dev/null +++ b/test/test_dr.py @@ -0,0 +1,63 @@ +import ot +import numpy as np +import pytest + +try: # test if cudamat installed + import ot.dr + nogo = False +except ImportError: + nogo = True + + +@pytest.mark.skipif(nogo, reason="Missing modules (autograd or pymanopt)") +def test_fda(): + + n = 100 # nb samples in source and target datasets + nz = 0.2 + np.random.seed(0) + + # generate circle dataset + t = np.random.rand(n) * 2 * np.pi + ys = np.floor((np.arange(n) * 1.0 / n * 3)) + 1 + xs = np.concatenate( + (np.cos(t).reshape((-1, 1)), np.sin(t).reshape((-1, 1))), 1) + xs = xs * ys.reshape(-1, 1) + nz * np.random.randn(n, 2) + + nbnoise = 8 + + xs = np.hstack((xs, np.random.randn(n, nbnoise))) + + p = 2 + + Pfda, projfda = ot.dr.fda(xs, ys, p) + + projfda(xs) + + assert np.allclose(np.sum(Pfda**2, 0), np.ones(p)) + + +@pytest.mark.skipif(nogo, reason="Missing modules (autograd or pymanopt)") +def test_wda(): + + n = 100 # nb samples in source and target datasets + nz = 0.2 + np.random.seed(0) + + # generate circle dataset + t = np.random.rand(n) * 2 * np.pi + ys = np.floor((np.arange(n) * 1.0 / n * 3)) + 1 + xs = np.concatenate( + (np.cos(t).reshape((-1, 1)), np.sin(t).reshape((-1, 1))), 1) + xs = xs * ys.reshape(-1, 1) + nz * np.random.randn(n, 2) + + nbnoise = 8 + + xs = np.hstack((xs, np.random.randn(n, nbnoise))) + + p = 2 + + Pwda, projwda = ot.dr.wda(xs, ys, p, maxiter=10) + + projwda(xs) + + assert np.allclose(np.sum(Pwda**2, 0), np.ones(p)) diff --git a/test/test_gpu.py b/test/test_gpu.py index 49b98d0..9cc39d7 100644 --- a/test/test_gpu.py +++ b/test/test_gpu.py @@ -12,7 +12,8 @@ except ImportError: @pytest.mark.skipif(nogpu, reason="No GPU available") def test_gpu_sinkhorn(): - import ot.gpu + + np.random.seed(0) def describeRes(r): print("min:{:.3E}, max::{:.3E}, mean::{:.3E}, std::{:.3E}".format( @@ -41,29 +42,31 @@ def test_gpu_sinkhorn(): @pytest.mark.skipif(nogpu, reason="No GPU available") def test_gpu_sinkhorn_lpl1(): - def describeRes(r): - print("min:{:.3E}, max:{:.3E}, mean:{:.3E}, std:{:.3E}" - .format(np.min(r), np.max(r), np.mean(r), np.std(r))) + np.random.seed(0) + + def describeRes(r): + print("min:{:.3E}, max:{:.3E}, mean:{:.3E}, std:{:.3E}" + .format(np.min(r), np.max(r), np.mean(r), np.std(r))) - for n in [50, 100, 500, 1000]: - print(n) - a = np.random.rand(n // 4, 100) - labels_a = np.random.randint(10, size=(n // 4)) - b = np.random.rand(n, 100) - time1 = time.time() - transport = ot.da.OTDA_lpl1() - transport.fit(a, labels_a, b) - G1 = transport.G - time2 = time.time() - transport = ot.gpu.da.OTDA_lpl1() - transport.fit(a, labels_a, b) - G2 = transport.G - time3 = time.time() - print("Normal sinkhorn lpl1, time: {:6.2f} sec ".format( - time2 - time1)) - describeRes(G1) - print(" GPU sinkhorn lpl1, time: {:6.2f} sec ".format( - time3 - time2)) - describeRes(G2) + for n in [50, 100, 500, 1000]: + print(n) + a = np.random.rand(n // 4, 100) + labels_a = np.random.randint(10, size=(n // 4)) + b = np.random.rand(n, 100) + time1 = time.time() + transport = ot.da.OTDA_lpl1() + transport.fit(a, labels_a, b) + G1 = transport.G + time2 = time.time() + transport = ot.gpu.da.OTDA_lpl1() + transport.fit(a, labels_a, b) + G2 = transport.G + time3 = time.time() + print("Normal sinkhorn lpl1, time: {:6.2f} sec ".format( + time2 - time1)) + describeRes(G1) + print(" GPU sinkhorn lpl1, time: {:6.2f} sec ".format( + time3 - time2)) + describeRes(G2) - assert np.allclose(G1, G2, rtol=1e-5, atol=1e-5) + assert np.allclose(G1, G2, rtol=1e-5, atol=1e-5) diff --git a/test/test_optim.py b/test/test_optim.py index 43cba7d..a77a37c 100644 --- a/test/test_optim.py +++ b/test/test_optim.py @@ -9,7 +9,7 @@ import numpy as np def test_conditional_gradient(): n = 100 # nb bins - + np.random.seed(0) # bin positions x = np.arange(n, dtype=np.float64) @@ -38,7 +38,7 @@ def test_conditional_gradient(): def test_generalized_conditional_gradient(): n = 100 # nb bins - + np.random.seed(0) # bin positions x = np.arange(n, dtype=np.float64) -- cgit v1.2.3 From 83ecc6df836d1a6b05bd641dfef465cc02b25b8f Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Mon, 24 Jul 2017 15:23:14 +0200 Subject: bregman coverage --- test/test_bregman.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/test_bregman.py b/test/test_bregman.py index b65de11..78666c7 100644 --- a/test/test_bregman.py +++ b/test/test_bregman.py @@ -33,18 +33,20 @@ def test_sinkhorn_empty(): M = ot.dist(x, x) - G = ot.sinkhorn([], [], M, 1, stopThr=1e-10) + G, log = ot.sinkhorn([], [], M, 1, stopThr=1e-10, verbose=True, log=True) # check constratints assert np.allclose(u, G.sum(1), atol=1e-05) # cf convergence sinkhorn assert np.allclose(u, G.sum(0), atol=1e-05) # cf convergence sinkhorn - G = ot.sinkhorn([], [], M, 1, stopThr=1e-10, method='sinkhorn_stabilized') + G, log = ot.sinkhorn([], [], M, 1, stopThr=1e-10, + method='sinkhorn_stabilized', verbose=True, log=True) # check constratints assert np.allclose(u, G.sum(1), atol=1e-05) # cf convergence sinkhorn assert np.allclose(u, G.sum(0), atol=1e-05) # cf convergence sinkhorn - G = ot.sinkhorn( - [], [], M, 1, stopThr=1e-10, method='sinkhorn_epsilon_scaling') + G, log = ot.sinkhorn( + [], [], M, 1, stopThr=1e-10, method='sinkhorn_epsilon_scaling', + verbose=True, log=True) # check constratints assert np.allclose(u, G.sum(1), atol=1e-05) # cf convergence sinkhorn assert np.allclose(u, G.sum(0), atol=1e-05) # cf convergence sinkhorn -- cgit v1.2.3 From 64cf2fc4f9a9331d510afd93e9bd3b8963ff879e Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Mon, 24 Jul 2017 15:28:43 +0200 Subject: tets barycenter --- test/test_bregman.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/test_bregman.py b/test/test_bregman.py index 78666c7..2dd3498 100644 --- a/test/test_bregman.py +++ b/test/test_bregman.py @@ -72,3 +72,32 @@ def test_sinkhorn_variants(): assert np.allclose(G0, Gs, atol=1e-05) assert np.allclose(G0, Ges, atol=1e-05) assert np.allclose(G0, Gerr) + + +def test_bary(): + + n = 100 # nb bins + + # bin positions + x = np.arange(n, dtype=np.float64) + + # Gaussian distributions + a1 = ot.datasets.get_1D_gauss(n, m=30, s=10) # m= mean, s= std + a2 = ot.datasets.get_1D_gauss(n, m=40, s=10) + + # creating matrix A containing all distributions + A = np.vstack((a1, a2)).T + n_distributions = A.shape[1] + + # loss matrix + normalization + M = ot.utils.dist0(n) + M /= M.max() + + alpha = 0.5 # 0<=alpha<=1 + weights = np.array([1 - alpha, alpha]) + + # wasserstein + reg = 1e-3 + bary_wass = ot.bregman.barycenter(A, M, reg, weights) + + assert np.allclose(1, np.sum(bary_wass)) -- cgit v1.2.3 From 33f3d309209baa8c5e127d02f00aae0660ed7bfb Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Mon, 24 Jul 2017 15:29:48 +0200 Subject: clean pep8 --- test/test_bregman.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/test_bregman.py b/test/test_bregman.py index 2dd3498..b204fe4 100644 --- a/test/test_bregman.py +++ b/test/test_bregman.py @@ -78,16 +78,12 @@ def test_bary(): n = 100 # nb bins - # bin positions - x = np.arange(n, dtype=np.float64) - # Gaussian distributions a1 = ot.datasets.get_1D_gauss(n, m=30, s=10) # m= mean, s= std a2 = ot.datasets.get_1D_gauss(n, m=40, s=10) # creating matrix A containing all distributions A = np.vstack((a1, a2)).T - n_distributions = A.shape[1] # loss matrix + normalization M = ot.utils.dist0(n) -- cgit v1.2.3 From bd705ed847dd7e43082e9d2771a59e539d6b7440 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Mon, 24 Jul 2017 15:45:09 +0200 Subject: add test yunmlix and bary --- test/test_bregman.py | 34 ++++++++++++++++++++++++++++++++++ test/test_gpu.py | 2 +- test/test_ot.py | 2 +- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/test/test_bregman.py b/test/test_bregman.py index b204fe4..025568c 100644 --- a/test/test_bregman.py +++ b/test/test_bregman.py @@ -97,3 +97,37 @@ def test_bary(): bary_wass = ot.bregman.barycenter(A, M, reg, weights) assert np.allclose(1, np.sum(bary_wass)) + + ot.bregman.barycenter(A, M, reg, log=True, verbose=True) + + +def test_unmix(): + + n = 50 # nb bins + + # Gaussian distributions + a1 = ot.datasets.get_1D_gauss(n, m=20, s=10) # m= mean, s= std + a2 = ot.datasets.get_1D_gauss(n, m=40, s=10) + + a = ot.datasets.get_1D_gauss(n, m=30, s=10) + + # creating matrix A containing all distributions + D = np.vstack((a1, a2)).T + + # loss matrix + normalization + M = ot.utils.dist0(n) + M /= M.max() + + M0 = ot.utils.dist0(2) + M0 /= M0.max() + h0 = ot.unif(2) + + # wasserstein + reg = 1e-3 + um = ot.bregman.unmix(a, D, M, M0, h0, reg, 1, alpha=0.01,) + + assert np.allclose(1, np.sum(um), rtol=1e-03, atol=1e-03) + assert np.allclose([0.5, 0.5], um, rtol=1e-03, atol=1e-03) + + ot.bregman.unmix(a, D, M, M0, h0, reg, + 1, alpha=0.01, log=True, verbose=True) diff --git a/test/test_gpu.py b/test/test_gpu.py index 9cc39d7..24797f2 100644 --- a/test/test_gpu.py +++ b/test/test_gpu.py @@ -48,7 +48,7 @@ def test_gpu_sinkhorn_lpl1(): print("min:{:.3E}, max:{:.3E}, mean:{:.3E}, std:{:.3E}" .format(np.min(r), np.max(r), np.mean(r), np.std(r))) - for n in [50, 100, 500, 1000]: + for n in [50, 100, 500]: print(n) a = np.random.rand(n // 4, 100) labels_a = np.random.randint(10, size=(n // 4)) diff --git a/test/test_ot.py b/test/test_ot.py index 3897397..5bf65c6 100644 --- a/test/test_ot.py +++ b/test/test_ot.py @@ -76,7 +76,7 @@ def test_emd2_multi(): # Gaussian distributions a = gauss(n, m=20, s=5) # m= mean, s= std - ls = np.arange(20, 1000, 10) + ls = np.arange(20, 1000, 20) nb = len(ls) b = np.zeros((n, nb)) for i in range(nb): -- cgit v1.2.3 From f204e983d969ed38c46d0bc85d0868a84c585db0 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Mon, 24 Jul 2017 16:02:15 +0200 Subject: add test da 58% coverage --- test/test_da.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 test/test_da.py diff --git a/test/test_da.py b/test/test_da.py new file mode 100644 index 0000000..50d3aba --- /dev/null +++ b/test/test_da.py @@ -0,0 +1,67 @@ + + +import ot +import numpy as np + +# import pytest + + +def test_OTDA(): + + n = 150 # nb bins + + xs, ys = ot.datasets.get_data_classif('3gauss', n) + xt, yt = ot.datasets.get_data_classif('3gauss2', n) + + a, b = ot.unif(n), ot.unif(n) + + # LP problem + da_emd = ot.da.OTDA() # init class + da_emd.fit(xs, xt) # fit distributions + da_emd.interp() # interpolation of source samples + da_emd.predict(xs) # interpolation of source samples + + assert np.allclose(a, np.sum(da_emd.G, 1)) + assert np.allclose(b, np.sum(da_emd.G, 0)) + + # sinkhorn regularization + lambd = 1e-1 + da_entrop = ot.da.OTDA_sinkhorn() + da_entrop.fit(xs, xt, reg=lambd) + da_entrop.interp() + da_entrop.predict(xs) + + assert np.allclose(a, np.sum(da_entrop.G, 1), rtol=1e-3, atol=1e-3) + assert np.allclose(b, np.sum(da_entrop.G, 0), rtol=1e-3, atol=1e-3) + + # non-convex Group lasso regularization + reg = 1e-1 + eta = 1e0 + da_lpl1 = ot.da.OTDA_lpl1() + da_lpl1.fit(xs, ys, xt, reg=reg, eta=eta) + da_lpl1.interp() + da_lpl1.predict(xs) + + assert np.allclose(a, np.sum(da_lpl1.G, 1), rtol=1e-3, atol=1e-3) + assert np.allclose(b, np.sum(da_lpl1.G, 0), rtol=1e-3, atol=1e-3) + + # True Group lasso regularization + reg = 1e-1 + eta = 2e0 + da_l1l2 = ot.da.OTDA_l1l2() + da_l1l2.fit(xs, ys, xt, reg=reg, eta=eta, numItermax=20, verbose=True) + da_l1l2.interp() + da_l1l2.predict(xs) + + assert np.allclose(a, np.sum(da_l1l2.G, 1), rtol=1e-3, atol=1e-3) + assert np.allclose(b, np.sum(da_l1l2.G, 0), rtol=1e-3, atol=1e-3) + + # linear mapping + da_emd = ot.da.OTDA_mapping_linear() # init class + da_emd.fit(xs, xt, numItermax=10) # fit distributions + da_emd.predict(xs) # interpolation of source samples + + # nonlinear mapping + da_emd = ot.da.OTDA_mapping_kernel() # init class + da_emd.fit(xs, xt, numItermax=10) # fit distributions + da_emd.predict(xs) # interpolation of source samples -- cgit v1.2.3 From 5aad08aff3e1a171ef9263af4488d175139085a0 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Mon, 24 Jul 2017 16:30:57 +0200 Subject: add test plot --- test/test_plot.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 test/test_plot.py diff --git a/test/test_plot.py b/test/test_plot.py new file mode 100644 index 0000000..8916a85 --- /dev/null +++ b/test/test_plot.py @@ -0,0 +1,42 @@ + + +import ot +import numpy as np + +# import pytest + + +def test_plot1D_mat(): + + n = 100 # nb bins + + # bin positions + x = np.arange(n, dtype=np.float64) + + # Gaussian distributions + a = ot.datasets.get_1D_gauss(n, m=20, s=5) # m= mean, s= std + b = ot.datasets.get_1D_gauss(n, m=60, s=10) + + # loss matrix + M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1))) + M /= M.max() + + ot.plot.plot1D_mat(a, b, M, 'Cost matrix M') + + +def test_plot2D_samples_mat(): + + n = 50 # nb samples + + mu_s = np.array([0, 0]) + cov_s = np.array([[1, 0], [0, 1]]) + + mu_t = np.array([4, 4]) + cov_t = np.array([[1, -.8], [-.8, 1]]) + + xs = ot.datasets.get_2D_samples_gauss(n, mu_s, cov_s) + xt = ot.datasets.get_2D_samples_gauss(n, mu_t, cov_t) + + G = 1.0 * (np.random.rand(n, n) < 0.01) + + ot.plot.plot2D_samples_mat(xs, xt, G, thr=1e-5) -- cgit v1.2.3 From a8d7301c132a225b5e4d78cae64683a5e08eae7f Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Mon, 24 Jul 2017 16:39:19 +0200 Subject: add test plot and dataset --- test/test_dr.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/test/test_dr.py b/test/test_dr.py index 24ccaa1..3da7705 100644 --- a/test/test_dr.py +++ b/test/test_dr.py @@ -12,22 +12,17 @@ except ImportError: @pytest.mark.skipif(nogo, reason="Missing modules (autograd or pymanopt)") def test_fda(): - n = 100 # nb samples in source and target datasets - nz = 0.2 + n = 90 # nb samples in source and target datasets np.random.seed(0) # generate circle dataset - t = np.random.rand(n) * 2 * np.pi - ys = np.floor((np.arange(n) * 1.0 / n * 3)) + 1 - xs = np.concatenate( - (np.cos(t).reshape((-1, 1)), np.sin(t).reshape((-1, 1))), 1) - xs = xs * ys.reshape(-1, 1) + nz * np.random.randn(n, 2) + xs, ys = ot.datasets.get_data_classif('gaussrot', n) nbnoise = 8 xs = np.hstack((xs, np.random.randn(n, nbnoise))) - p = 2 + p = 1 Pfda, projfda = ot.dr.fda(xs, ys, p) -- cgit v1.2.3 From e11b1d1a77f201896fd3f70bc5b910e99610e951 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Mon, 24 Jul 2017 17:08:25 +0200 Subject: test plot with no X --- test/test_plot.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/test/test_plot.py b/test/test_plot.py index 8916a85..69789fa 100644 --- a/test/test_plot.py +++ b/test/test_plot.py @@ -1,13 +1,14 @@ -import ot import numpy as np - -# import pytest +import matplotlib +matplotlib.use('Agg') def test_plot1D_mat(): + import ot + n = 100 # nb bins # bin positions @@ -26,6 +27,8 @@ def test_plot1D_mat(): def test_plot2D_samples_mat(): + import ot + n = 50 # nb samples mu_s = np.array([0, 0]) -- cgit v1.2.3 From 11f065237982516c08f91e7757dd7a81c9c525d6 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Mon, 24 Jul 2017 18:26:48 +0200 Subject: matplotlib travis --- .travis.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.travis.yml b/.travis.yml index dc415a9..ec2b3d2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,6 +13,10 @@ matrix: python: 2.7 before_install: - ./.travis/before_install.sh +before_script: # configure a headless display to test plot generation + - "export DISPLAY=:99.0" + - "sh -e /etc/init.d/xvfb start" + - sleep 3 # give xvfb some time to start # command to install dependencies install: - pip install -r requirements.txt -- cgit v1.2.3 From 46f297f678de0051dc6d5067291d1e1046b4705e Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Wed, 26 Jul 2017 11:26:08 +0200 Subject: import nmpy before ot --- test/test_bregman.py | 4 ++-- test/test_da.py | 4 ++-- test/test_dr.py | 5 +++-- test/test_gpu.py | 3 ++- test/test_optim.py | 4 ++-- test/test_ot.py | 4 ++-- 6 files changed, 13 insertions(+), 11 deletions(-) diff --git a/test/test_bregman.py b/test/test_bregman.py index 025568c..aaa2efc 100644 --- a/test/test_bregman.py +++ b/test/test_bregman.py @@ -1,7 +1,7 @@ - -import ot import numpy as np +import ot + # import pytest diff --git a/test/test_da.py b/test/test_da.py index 50d3aba..0d92b95 100644 --- a/test/test_da.py +++ b/test/test_da.py @@ -1,7 +1,7 @@ - -import ot import numpy as np +import ot + # import pytest diff --git a/test/test_dr.py b/test/test_dr.py index 3da7705..3faba48 100644 --- a/test/test_dr.py +++ b/test/test_dr.py @@ -1,8 +1,9 @@ -import ot + import numpy as np +import ot import pytest -try: # test if cudamat installed +try: # test if autograd and pymanopt are installed import ot.dr nogo = False except ImportError: diff --git a/test/test_gpu.py b/test/test_gpu.py index 24797f2..5184a6c 100644 --- a/test/test_gpu.py +++ b/test/test_gpu.py @@ -1,5 +1,6 @@ -import ot + import numpy as np +import ot import time import pytest diff --git a/test/test_optim.py b/test/test_optim.py index a77a37c..d5c4ad0 100644 --- a/test/test_optim.py +++ b/test/test_optim.py @@ -1,7 +1,7 @@ - -import ot import numpy as np +import ot + # import pytest diff --git a/test/test_ot.py b/test/test_ot.py index 5bf65c6..a30491d 100644 --- a/test/test_ot.py +++ b/test/test_ot.py @@ -1,7 +1,7 @@ - -import ot import numpy as np +import ot + # import pytest -- cgit v1.2.3 From 68d74902bcd3d988fff8cb7713314063f04c0089 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Wed, 26 Jul 2017 11:34:11 +0200 Subject: numpy assert + n_bins --- test/test_bregman.py | 63 ++++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/test/test_bregman.py b/test/test_bregman.py index aaa2efc..1638ef6 100644 --- a/test/test_bregman.py +++ b/test/test_bregman.py @@ -3,15 +3,12 @@ import numpy as np import ot -# import pytest - - def test_sinkhorn(): # test sinkhorn n = 100 - np.random.seed(0) + rng = np.random.RandomState(0) - x = np.random.randn(n, 2) + x = rng.randn(n, 2) u = ot.utils.unif(n) M = ot.dist(x, x) @@ -19,45 +16,47 @@ def test_sinkhorn(): G = ot.sinkhorn(u, u, M, 1, stopThr=1e-10) # check constratints - assert np.allclose(u, G.sum(1), atol=1e-05) # cf convergence sinkhorn - assert np.allclose(u, G.sum(0), atol=1e-05) # cf convergence sinkhorn + np.testing.assert_allclose( + u, G.sum(1), atol=1e-05) # cf convergence sinkhorn + np.testing.assert_allclose( + u, G.sum(0), atol=1e-05) # cf convergence sinkhorn def test_sinkhorn_empty(): # test sinkhorn n = 100 - np.random.seed(0) + rng = np.random.RandomState(0) - x = np.random.randn(n, 2) + x = rng.randn(n, 2) u = ot.utils.unif(n) M = ot.dist(x, x) G, log = ot.sinkhorn([], [], M, 1, stopThr=1e-10, verbose=True, log=True) # check constratints - assert np.allclose(u, G.sum(1), atol=1e-05) # cf convergence sinkhorn - assert np.allclose(u, G.sum(0), atol=1e-05) # cf convergence sinkhorn + np.testing.assert_allclose(u, G.sum(1), atol=1e-05) + np.testing.assert_allclose(u, G.sum(0), atol=1e-05) G, log = ot.sinkhorn([], [], M, 1, stopThr=1e-10, method='sinkhorn_stabilized', verbose=True, log=True) # check constratints - assert np.allclose(u, G.sum(1), atol=1e-05) # cf convergence sinkhorn - assert np.allclose(u, G.sum(0), atol=1e-05) # cf convergence sinkhorn + np.testing.assert_allclose(u, G.sum(1), atol=1e-05) + np.testing.assert_allclose(u, G.sum(0), atol=1e-05) G, log = ot.sinkhorn( [], [], M, 1, stopThr=1e-10, method='sinkhorn_epsilon_scaling', verbose=True, log=True) # check constratints - assert np.allclose(u, G.sum(1), atol=1e-05) # cf convergence sinkhorn - assert np.allclose(u, G.sum(0), atol=1e-05) # cf convergence sinkhorn + np.testing.assert_allclose(u, G.sum(1), atol=1e-05) + np.testing.assert_allclose(u, G.sum(0), atol=1e-05) def test_sinkhorn_variants(): # test sinkhorn n = 100 - np.random.seed(0) + rng = np.random.RandomState(0) - x = np.random.randn(n, 2) + x = rng.randn(n, 2) u = ot.utils.unif(n) M = ot.dist(x, x) @@ -69,24 +68,24 @@ def test_sinkhorn_variants(): Gerr = ot.sinkhorn(u, u, M, 1, method='do_not_exists', stopThr=1e-10) # check values - assert np.allclose(G0, Gs, atol=1e-05) - assert np.allclose(G0, Ges, atol=1e-05) - assert np.allclose(G0, Gerr) + np.testing.assert_allclose(G0, Gs, atol=1e-05) + np.testing.assert_allclose(G0, Ges, atol=1e-05) + np.testing.assert_allclose(G0, Gerr) def test_bary(): - n = 100 # nb bins + n_bins = 100 # nb bins # Gaussian distributions - a1 = ot.datasets.get_1D_gauss(n, m=30, s=10) # m= mean, s= std - a2 = ot.datasets.get_1D_gauss(n, m=40, s=10) + a1 = ot.datasets.get_1D_gauss(n_bins, m=30, s=10) # m= mean, s= std + a2 = ot.datasets.get_1D_gauss(n_bins, m=40, s=10) # creating matrix A containing all distributions A = np.vstack((a1, a2)).T # loss matrix + normalization - M = ot.utils.dist0(n) + M = ot.utils.dist0(n_bins) M /= M.max() alpha = 0.5 # 0<=alpha<=1 @@ -96,26 +95,26 @@ def test_bary(): reg = 1e-3 bary_wass = ot.bregman.barycenter(A, M, reg, weights) - assert np.allclose(1, np.sum(bary_wass)) + np.testing.assert_allclose(1, np.sum(bary_wass)) ot.bregman.barycenter(A, M, reg, log=True, verbose=True) def test_unmix(): - n = 50 # nb bins + n_bins = 50 # nb bins # Gaussian distributions - a1 = ot.datasets.get_1D_gauss(n, m=20, s=10) # m= mean, s= std - a2 = ot.datasets.get_1D_gauss(n, m=40, s=10) + a1 = ot.datasets.get_1D_gauss(n_bins, m=20, s=10) # m= mean, s= std + a2 = ot.datasets.get_1D_gauss(n_bins, m=40, s=10) - a = ot.datasets.get_1D_gauss(n, m=30, s=10) + a = ot.datasets.get_1D_gauss(n_bins, m=30, s=10) # creating matrix A containing all distributions D = np.vstack((a1, a2)).T # loss matrix + normalization - M = ot.utils.dist0(n) + M = ot.utils.dist0(n_bins) M /= M.max() M0 = ot.utils.dist0(2) @@ -126,8 +125,8 @@ def test_unmix(): reg = 1e-3 um = ot.bregman.unmix(a, D, M, M0, h0, reg, 1, alpha=0.01,) - assert np.allclose(1, np.sum(um), rtol=1e-03, atol=1e-03) - assert np.allclose([0.5, 0.5], um, rtol=1e-03, atol=1e-03) + np.testing.assert_allclose(1, np.sum(um), rtol=1e-03, atol=1e-03) + np.testing.assert_allclose([0.5, 0.5], um, rtol=1e-03, atol=1e-03) ot.bregman.unmix(a, D, M, M0, h0, reg, 1, alpha=0.01, log=True, verbose=True) -- cgit v1.2.3 From 67b011a2a6a0cb8dffbb7a2619875f0e0d79588c Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Wed, 26 Jul 2017 11:38:17 +0200 Subject: numpy assert test_da --- test/test_da.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/test/test_da.py b/test/test_da.py index 0d92b95..8df4795 100644 --- a/test/test_da.py +++ b/test/test_da.py @@ -6,9 +6,10 @@ import ot # import pytest -def test_OTDA(): +def test_otda(): - n = 150 # nb bins + n = 150 # nb samples + np.random.seed(0) xs, ys = ot.datasets.get_data_classif('3gauss', n) xt, yt = ot.datasets.get_data_classif('3gauss2', n) @@ -21,8 +22,8 @@ def test_OTDA(): da_emd.interp() # interpolation of source samples da_emd.predict(xs) # interpolation of source samples - assert np.allclose(a, np.sum(da_emd.G, 1)) - assert np.allclose(b, np.sum(da_emd.G, 0)) + np.testing.assert_allclose(a, np.sum(da_emd.G, 1)) + np.testing.assert_allclose(b, np.sum(da_emd.G, 0)) # sinkhorn regularization lambd = 1e-1 @@ -31,8 +32,8 @@ def test_OTDA(): da_entrop.interp() da_entrop.predict(xs) - assert np.allclose(a, np.sum(da_entrop.G, 1), rtol=1e-3, atol=1e-3) - assert np.allclose(b, np.sum(da_entrop.G, 0), rtol=1e-3, atol=1e-3) + np.testing.assert_allclose(a, np.sum(da_entrop.G, 1), rtol=1e-3, atol=1e-3) + np.testing.assert_allclose(b, np.sum(da_entrop.G, 0), rtol=1e-3, atol=1e-3) # non-convex Group lasso regularization reg = 1e-1 @@ -42,8 +43,8 @@ def test_OTDA(): da_lpl1.interp() da_lpl1.predict(xs) - assert np.allclose(a, np.sum(da_lpl1.G, 1), rtol=1e-3, atol=1e-3) - assert np.allclose(b, np.sum(da_lpl1.G, 0), rtol=1e-3, atol=1e-3) + np.testing.assert_allclose(a, np.sum(da_lpl1.G, 1), rtol=1e-3, atol=1e-3) + np.testing.assert_allclose(b, np.sum(da_lpl1.G, 0), rtol=1e-3, atol=1e-3) # True Group lasso regularization reg = 1e-1 @@ -53,8 +54,8 @@ def test_OTDA(): da_l1l2.interp() da_l1l2.predict(xs) - assert np.allclose(a, np.sum(da_l1l2.G, 1), rtol=1e-3, atol=1e-3) - assert np.allclose(b, np.sum(da_l1l2.G, 0), rtol=1e-3, atol=1e-3) + np.testing.assert_allclose(a, np.sum(da_l1l2.G, 1), rtol=1e-3, atol=1e-3) + np.testing.assert_allclose(b, np.sum(da_l1l2.G, 0), rtol=1e-3, atol=1e-3) # linear mapping da_emd = ot.da.OTDA_mapping_linear() # init class -- cgit v1.2.3 From 347e6288b87cbeef9b8fbc1a08cd130b96de1d61 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Wed, 26 Jul 2017 11:42:35 +0200 Subject: n to n_samples --- test/test_da.py | 11 ++++------- test/test_dr.py | 25 ++++++++++--------------- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/test/test_da.py b/test/test_da.py index 8df4795..a38390f 100644 --- a/test/test_da.py +++ b/test/test_da.py @@ -3,18 +3,15 @@ import numpy as np import ot -# import pytest - - def test_otda(): - n = 150 # nb samples + n_samples = 150 # nb samples np.random.seed(0) - xs, ys = ot.datasets.get_data_classif('3gauss', n) - xt, yt = ot.datasets.get_data_classif('3gauss2', n) + xs, ys = ot.datasets.get_data_classif('3gauss', n_samples) + xt, yt = ot.datasets.get_data_classif('3gauss2', n_samples) - a, b = ot.unif(n), ot.unif(n) + a, b = ot.unif(n_samples), ot.unif(n_samples) # LP problem da_emd = ot.da.OTDA() # init class diff --git a/test/test_dr.py b/test/test_dr.py index 3faba48..e3d1e6b 100644 --- a/test/test_dr.py +++ b/test/test_dr.py @@ -13,15 +13,15 @@ except ImportError: @pytest.mark.skipif(nogo, reason="Missing modules (autograd or pymanopt)") def test_fda(): - n = 90 # nb samples in source and target datasets + n_samples = 90 # nb samples in source and target datasets np.random.seed(0) - # generate circle dataset - xs, ys = ot.datasets.get_data_classif('gaussrot', n) + # generate gaussian dataset + xs, ys = ot.datasets.get_data_classif('gaussrot', n_samples) - nbnoise = 8 + n_features_noise = 8 - xs = np.hstack((xs, np.random.randn(n, nbnoise))) + xs = np.hstack((xs, np.random.randn(n_samples, n_features_noise))) p = 1 @@ -35,20 +35,15 @@ def test_fda(): @pytest.mark.skipif(nogo, reason="Missing modules (autograd or pymanopt)") def test_wda(): - n = 100 # nb samples in source and target datasets - nz = 0.2 + n_samples = 100 # nb samples in source and target datasets np.random.seed(0) - # generate circle dataset - t = np.random.rand(n) * 2 * np.pi - ys = np.floor((np.arange(n) * 1.0 / n * 3)) + 1 - xs = np.concatenate( - (np.cos(t).reshape((-1, 1)), np.sin(t).reshape((-1, 1))), 1) - xs = xs * ys.reshape(-1, 1) + nz * np.random.randn(n, 2) + # generate gaussian dataset + xs, ys = ot.datasets.get_data_classif('gaussrot', n_samples) - nbnoise = 8 + n_features_noise = 8 - xs = np.hstack((xs, np.random.randn(n, nbnoise))) + xs = np.hstack((xs, np.random.randn(n_samples, n_features_noise))) p = 2 -- cgit v1.2.3 From 4a45135dfa3f1aeae8b3bdf0c42422f0f60426e8 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Wed, 26 Jul 2017 11:47:29 +0200 Subject: dr +gpu numpy assert --- test/test_dr.py | 4 ++-- test/test_gpu.py | 34 +++++++++++++++++----------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/test/test_dr.py b/test/test_dr.py index e3d1e6b..bdb920e 100644 --- a/test/test_dr.py +++ b/test/test_dr.py @@ -29,7 +29,7 @@ def test_fda(): projfda(xs) - assert np.allclose(np.sum(Pfda**2, 0), np.ones(p)) + np.testing.assert_allclose(np.sum(Pfda**2, 0), np.ones(p)) @pytest.mark.skipif(nogo, reason="Missing modules (autograd or pymanopt)") @@ -51,4 +51,4 @@ def test_wda(): projwda(xs) - assert np.allclose(np.sum(Pwda**2, 0), np.ones(p)) + np.testing.assert_allclose(np.sum(Pwda**2, 0), np.ones(p)) diff --git a/test/test_gpu.py b/test/test_gpu.py index 5184a6c..7ae159b 100644 --- a/test/test_gpu.py +++ b/test/test_gpu.py @@ -16,14 +16,14 @@ def test_gpu_sinkhorn(): np.random.seed(0) - def describeRes(r): + def describe_res(r): print("min:{:.3E}, max::{:.3E}, mean::{:.3E}, std::{:.3E}".format( np.min(r), np.max(r), np.mean(r), np.std(r))) - for n in [50, 100, 500, 1000]: - print(n) - a = np.random.rand(n // 4, 100) - b = np.random.rand(n, 100) + for n_samples in [50, 100, 500, 1000]: + print(n_samples) + a = np.random.rand(n_samples // 4, 100) + b = np.random.rand(n_samples, 100) time1 = time.time() transport = ot.da.OTDA_sinkhorn() transport.fit(a, b) @@ -34,26 +34,26 @@ def test_gpu_sinkhorn(): G2 = transport.G time3 = time.time() print("Normal sinkhorn, time: {:6.2f} sec ".format(time2 - time1)) - describeRes(G1) + describe_res(G1) print(" GPU sinkhorn, time: {:6.2f} sec ".format(time3 - time2)) - describeRes(G2) + describe_res(G2) - assert np.allclose(G1, G2, rtol=1e-5, atol=1e-5) + np.testing.assert_allclose(G1, G2, rtol=1e-5, atol=1e-5) @pytest.mark.skipif(nogpu, reason="No GPU available") def test_gpu_sinkhorn_lpl1(): np.random.seed(0) - def describeRes(r): + def describe_res(r): print("min:{:.3E}, max:{:.3E}, mean:{:.3E}, std:{:.3E}" .format(np.min(r), np.max(r), np.mean(r), np.std(r))) - for n in [50, 100, 500]: - print(n) - a = np.random.rand(n // 4, 100) - labels_a = np.random.randint(10, size=(n // 4)) - b = np.random.rand(n, 100) + for n_samples in [50, 100, 500]: + print(n_samples) + a = np.random.rand(n_samples // 4, 100) + labels_a = np.random.randint(10, size=(n_samples // 4)) + b = np.random.rand(n_samples, 100) time1 = time.time() transport = ot.da.OTDA_lpl1() transport.fit(a, labels_a, b) @@ -65,9 +65,9 @@ def test_gpu_sinkhorn_lpl1(): time3 = time.time() print("Normal sinkhorn lpl1, time: {:6.2f} sec ".format( time2 - time1)) - describeRes(G1) + describe_res(G1) print(" GPU sinkhorn lpl1, time: {:6.2f} sec ".format( time3 - time2)) - describeRes(G2) + describe_res(G2) - assert np.allclose(G1, G2, rtol=1e-5, atol=1e-5) + np.testing.assert_allclose(G1, G2, rtol=1e-5, atol=1e-5) -- cgit v1.2.3 From 2bc41ad8bb54c76bade6db2c0e04fa387ff29500 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Wed, 26 Jul 2017 11:48:13 +0200 Subject: rng gpu --- test/test_gpu.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/test/test_gpu.py b/test/test_gpu.py index 7ae159b..98f59f7 100644 --- a/test/test_gpu.py +++ b/test/test_gpu.py @@ -14,7 +14,7 @@ except ImportError: @pytest.mark.skipif(nogpu, reason="No GPU available") def test_gpu_sinkhorn(): - np.random.seed(0) + rng = np.random.RandomState(0) def describe_res(r): print("min:{:.3E}, max::{:.3E}, mean::{:.3E}, std::{:.3E}".format( @@ -22,8 +22,8 @@ def test_gpu_sinkhorn(): for n_samples in [50, 100, 500, 1000]: print(n_samples) - a = np.random.rand(n_samples // 4, 100) - b = np.random.rand(n_samples, 100) + a = rng.rand(n_samples // 4, 100) + b = rng.rand(n_samples, 100) time1 = time.time() transport = ot.da.OTDA_sinkhorn() transport.fit(a, b) @@ -43,7 +43,8 @@ def test_gpu_sinkhorn(): @pytest.mark.skipif(nogpu, reason="No GPU available") def test_gpu_sinkhorn_lpl1(): - np.random.seed(0) + + rng = np.random.RandomState(0) def describe_res(r): print("min:{:.3E}, max:{:.3E}, mean:{:.3E}, std:{:.3E}" @@ -51,9 +52,9 @@ def test_gpu_sinkhorn_lpl1(): for n_samples in [50, 100, 500]: print(n_samples) - a = np.random.rand(n_samples // 4, 100) + a = rng.rand(n_samples // 4, 100) labels_a = np.random.randint(10, size=(n_samples // 4)) - b = np.random.rand(n_samples, 100) + b = rng.rand(n_samples, 100) time1 = time.time() transport = ot.da.OTDA_lpl1() transport.fit(a, labels_a, b) -- cgit v1.2.3 From 6a02db058e24914cd79b638f15be9a90bce7e4f3 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Wed, 26 Jul 2017 11:51:07 +0200 Subject: test_optim --- test/test_optim.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/test/test_optim.py b/test/test_optim.py index d5c4ad0..bc0b706 100644 --- a/test/test_optim.py +++ b/test/test_optim.py @@ -3,22 +3,20 @@ import numpy as np import ot -# import pytest - def test_conditional_gradient(): - n = 100 # nb bins + n_bins = 100 # nb bins np.random.seed(0) # bin positions - x = np.arange(n, dtype=np.float64) + x = np.arange(n_bins, dtype=np.float64) # Gaussian distributions - a = ot.datasets.get_1D_gauss(n, m=20, s=5) # m= mean, s= std - b = ot.datasets.get_1D_gauss(n, m=60, s=10) + a = ot.datasets.get_1D_gauss(n_bins, m=20, s=5) # m= mean, s= std + b = ot.datasets.get_1D_gauss(n_bins, m=60, s=10) # loss matrix - M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1))) + M = ot.dist(x.reshape((n_bins, 1)), x.reshape((n_bins, 1))) M /= M.max() def f(G): @@ -37,17 +35,17 @@ def test_conditional_gradient(): def test_generalized_conditional_gradient(): - n = 100 # nb bins + n_bins = 100 # nb bins np.random.seed(0) # bin positions - x = np.arange(n, dtype=np.float64) + x = np.arange(n_bins, dtype=np.float64) # Gaussian distributions - a = ot.datasets.get_1D_gauss(n, m=20, s=5) # m= mean, s= std - b = ot.datasets.get_1D_gauss(n, m=60, s=10) + a = ot.datasets.get_1D_gauss(n_bins, m=20, s=5) # m= mean, s= std + b = ot.datasets.get_1D_gauss(n_bins, m=60, s=10) # loss matrix - M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1))) + M = ot.dist(x.reshape((n_bins, 1)), x.reshape((n_bins, 1))) M /= M.max() def f(G): -- cgit v1.2.3 From 86418ebf5adc11879c580e88e3eaa02691de30e7 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Wed, 26 Jul 2017 11:51:45 +0200 Subject: test_optim allclose --- test/test_optim.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/test_optim.py b/test/test_optim.py index bc0b706..2840cad 100644 --- a/test/test_optim.py +++ b/test/test_optim.py @@ -29,8 +29,8 @@ def test_conditional_gradient(): G, log = ot.optim.cg(a, b, M, reg, f, df, verbose=True, log=True) - assert np.allclose(a, G.sum(1)) - assert np.allclose(b, G.sum(0)) + np.testing.assert_allclose(a, G.sum(1)) + np.testing.assert_allclose(b, G.sum(0)) def test_generalized_conditional_gradient(): @@ -59,5 +59,5 @@ def test_generalized_conditional_gradient(): G, log = ot.optim.gcg(a, b, M, reg1, reg2, f, df, verbose=True, log=True) - assert np.allclose(a, G.sum(1), atol=1e-05) - assert np.allclose(b, G.sum(0), atol=1e-05) + np.testing.assert_allclose(a, G.sum(1), atol=1e-05) + np.testing.assert_allclose(b, G.sum(0), atol=1e-05) -- cgit v1.2.3 From 286de0a955bbb7e26079a8dc75abf622bf461523 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Wed, 26 Jul 2017 11:52:38 +0200 Subject: clean test_ot --- test/test_ot.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/test_ot.py b/test/test_ot.py index a30491d..9c0acab 100644 --- a/test/test_ot.py +++ b/test/test_ot.py @@ -3,8 +3,6 @@ import numpy as np import ot -# import pytest - def test_doctest(): -- cgit v1.2.3 From 81118f22197cdf4553427038526c8f730be256d7 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Wed, 26 Jul 2017 11:55:57 +0200 Subject: test_ot random state --- test/test_ot.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/test/test_ot.py b/test/test_ot.py index 9c0acab..7fe665f 100644 --- a/test/test_ot.py +++ b/test/test_ot.py @@ -18,9 +18,9 @@ def test_doctest(): def test_emd_emd2(): # test emd and emd2 for simple identity n = 100 - np.random.seed(0) + rng = np.random.RandomState(0) - x = np.random.randn(n, 2) + x = rng.randn(n, 2) u = ot.utils.unif(n) M = ot.dist(x, x) @@ -28,22 +28,22 @@ def test_emd_emd2(): G = ot.emd(u, u, M) # check G is identity - assert np.allclose(G, np.eye(n) / n) + np.testing.assert_allclose(G, np.eye(n) / n) # check constratints - assert np.allclose(u, G.sum(1)) # cf convergence sinkhorn - assert np.allclose(u, G.sum(0)) # cf convergence sinkhorn + np.testing.assert_allclose(u, G.sum(1)) # cf convergence sinkhorn + np.testing.assert_allclose(u, G.sum(0)) # cf convergence sinkhorn w = ot.emd2(u, u, M) # check loss=0 - assert np.allclose(w, 0) + np.testing.assert_allclose(w, 0) def test_emd_empty(): # test emd and emd2 for simple identity n = 100 - np.random.seed(0) + rng = np.random.RandomState(0) - x = np.random.randn(n, 2) + x = rng.randn(n, 2) u = ot.utils.unif(n) M = ot.dist(x, x) @@ -51,14 +51,14 @@ def test_emd_empty(): G = ot.emd([], [], M) # check G is identity - assert np.allclose(G, np.eye(n) / n) + np.testing.assert_allclose(G, np.eye(n) / n) # check constratints - assert np.allclose(u, G.sum(1)) # cf convergence sinkhorn - assert np.allclose(u, G.sum(0)) # cf convergence sinkhorn + np.testing.assert_allclose(u, G.sum(1)) # cf convergence sinkhorn + np.testing.assert_allclose(u, G.sum(0)) # cf convergence sinkhorn w = ot.emd2([], [], M) # check loss=0 - assert np.allclose(w, 0) + np.testing.assert_allclose(w, 0) def test_emd2_multi(): @@ -66,7 +66,6 @@ def test_emd2_multi(): from ot.datasets import get_1D_gauss as gauss n = 1000 # nb bins - np.random.seed(0) # bin positions x = np.arange(n, dtype=np.float64) @@ -96,4 +95,4 @@ def test_emd2_multi(): emdn = ot.emd2(a, b, M) ot.toc('multi proc : {} s') - assert np.allclose(emd1, emdn) + np.testing.assert_allclose(emd1, emdn) -- cgit v1.2.3 From 109fc2a9243d2c0f9a911fa8c02079d2fc0277ab Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Wed, 26 Jul 2017 11:57:43 +0200 Subject: flake8 --- test/test_optim.py | 1 - test/test_ot.py | 1 - 2 files changed, 2 deletions(-) diff --git a/test/test_optim.py b/test/test_optim.py index 2840cad..05ca895 100644 --- a/test/test_optim.py +++ b/test/test_optim.py @@ -3,7 +3,6 @@ import numpy as np import ot - def test_conditional_gradient(): n_bins = 100 # nb bins diff --git a/test/test_ot.py b/test/test_ot.py index 7fe665f..531e6e0 100644 --- a/test/test_ot.py +++ b/test/test_ot.py @@ -3,7 +3,6 @@ import numpy as np import ot - def test_doctest(): import doctest -- cgit v1.2.3 From e0fa14ba146e6f92a3060b5f2f0a5c01bd18bdc4 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Wed, 26 Jul 2017 11:58:58 +0200 Subject: flake8 --- test/test_plot.py | 18 +++++++++--------- test/test_utils.py | 3 --- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/test/test_plot.py b/test/test_plot.py index 69789fa..d826988 100644 --- a/test/test_plot.py +++ b/test/test_plot.py @@ -9,17 +9,17 @@ def test_plot1D_mat(): import ot - n = 100 # nb bins + n_bins = 100 # nb bins # bin positions - x = np.arange(n, dtype=np.float64) + x = np.arange(n_bins, dtype=np.float64) # Gaussian distributions - a = ot.datasets.get_1D_gauss(n, m=20, s=5) # m= mean, s= std - b = ot.datasets.get_1D_gauss(n, m=60, s=10) + a = ot.datasets.get_1D_gauss(n_bins, m=20, s=5) # m= mean, s= std + b = ot.datasets.get_1D_gauss(n_bins, m=60, s=10) # loss matrix - M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1))) + M = ot.dist(x.reshape((n_bins, 1)), x.reshape((n_bins, 1))) M /= M.max() ot.plot.plot1D_mat(a, b, M, 'Cost matrix M') @@ -29,7 +29,7 @@ def test_plot2D_samples_mat(): import ot - n = 50 # nb samples + n_bins = 50 # nb samples mu_s = np.array([0, 0]) cov_s = np.array([[1, 0], [0, 1]]) @@ -37,9 +37,9 @@ def test_plot2D_samples_mat(): mu_t = np.array([4, 4]) cov_t = np.array([[1, -.8], [-.8, 1]]) - xs = ot.datasets.get_2D_samples_gauss(n, mu_s, cov_s) - xt = ot.datasets.get_2D_samples_gauss(n, mu_t, cov_t) + xs = ot.datasets.get_2D_samples_gauss(n_bins, mu_s, cov_s) + xt = ot.datasets.get_2D_samples_gauss(n_bins, mu_t, cov_t) - G = 1.0 * (np.random.rand(n, n) < 0.01) + G = 1.0 * (np.random.rand(n_bins, n_bins) < 0.01) ot.plot.plot2D_samples_mat(xs, xt, G, thr=1e-5) diff --git a/test/test_utils.py b/test/test_utils.py index 0883a8e..fe1b88d 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -3,9 +3,6 @@ import ot import numpy as np -# import pytest - - def test_parmap(): n = 100 -- cgit v1.2.3 From d101e088b72fa0be4648d57524946ebfc93bf34b Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Wed, 26 Jul 2017 11:59:25 +0200 Subject: nearly all review done --- test/test_utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_utils.py b/test/test_utils.py index fe1b88d..230d126 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -3,6 +3,7 @@ import ot import numpy as np + def test_parmap(): n = 100 -- cgit v1.2.3 From 77037cc2cb4b138d3d73d24b1ff4fc999cc64243 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Wed, 26 Jul 2017 12:00:05 +0200 Subject: gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 6edce05..42a9aad 100644 --- a/.gitignore +++ b/.gitignore @@ -100,3 +100,6 @@ ENV/ # Mac stuff .DS_Store + +# coverage output folder +cov_html/ -- cgit v1.2.3 From fac003de3d3a159bb8fb6228786479cdede2df4e Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Wed, 26 Jul 2017 12:07:03 +0200 Subject: author and license for tets files --- test/test_bregman.py | 5 +++++ test/test_da.py | 5 +++++ test/test_dr.py | 5 +++++ test/test_gpu.py | 5 +++++ test/test_optim.py | 5 +++++ test/test_ot.py | 5 +++++ test/test_plot.py | 4 ++++ test/test_utils.py | 5 +++++ 8 files changed, 39 insertions(+) diff --git a/test/test_bregman.py b/test/test_bregman.py index 1638ef6..4a800fd 100644 --- a/test/test_bregman.py +++ b/test/test_bregman.py @@ -1,3 +1,8 @@ +"""Tests for module bregman on OT with bregman projections """ + +# Author: Remi Flamary +# +# License: MIT License import numpy as np import ot diff --git a/test/test_da.py b/test/test_da.py index a38390f..dfba83f 100644 --- a/test/test_da.py +++ b/test/test_da.py @@ -1,3 +1,8 @@ +"""Tests for module da on Domain Adaptation """ + +# Author: Remi Flamary +# +# License: MIT License import numpy as np import ot diff --git a/test/test_dr.py b/test/test_dr.py index bdb920e..915012d 100644 --- a/test/test_dr.py +++ b/test/test_dr.py @@ -1,3 +1,8 @@ +"""Tests for module dr on Dimensionality Reduction """ + +# Author: Remi Flamary +# +# License: MIT License import numpy as np import ot diff --git a/test/test_gpu.py b/test/test_gpu.py index 98f59f7..615c2a7 100644 --- a/test/test_gpu.py +++ b/test/test_gpu.py @@ -1,3 +1,8 @@ +"""Tests for module gpu for gpu acceleration """ + +# Author: Remi Flamary +# +# License: MIT License import numpy as np import ot diff --git a/test/test_optim.py b/test/test_optim.py index 05ca895..69496a5 100644 --- a/test/test_optim.py +++ b/test/test_optim.py @@ -1,3 +1,8 @@ +"""Tests for module optim fro OT optimization """ + +# Author: Remi Flamary +# +# License: MIT License import numpy as np import ot diff --git a/test/test_ot.py b/test/test_ot.py index 531e6e0..acd8718 100644 --- a/test/test_ot.py +++ b/test/test_ot.py @@ -1,3 +1,8 @@ +"""Tests for main module ot """ + +# Author: Remi Flamary +# +# License: MIT License import numpy as np import ot diff --git a/test/test_plot.py b/test/test_plot.py index d826988..f7debee 100644 --- a/test/test_plot.py +++ b/test/test_plot.py @@ -1,4 +1,8 @@ +"""Tests for module plot for visualization """ +# Author: Remi Flamary +# +# License: MIT License import numpy as np import matplotlib diff --git a/test/test_utils.py b/test/test_utils.py index 230d126..9b140db 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -1,3 +1,8 @@ +"""Tests for module utils for timing and parallel computation """ + +# Author: Remi Flamary +# +# License: MIT License import ot -- cgit v1.2.3 From 00970175c0f8ba9a99b61a182b32e329f219d382 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Wed, 26 Jul 2017 12:15:40 +0200 Subject: add license and authors on all modules --- ot/__init__.py | 4 ++++ ot/bregman.py | 5 +++++ ot/da.py | 6 ++++++ ot/datasets.py | 4 ++++ ot/dr.py | 4 ++++ ot/gpu/__init__.py | 5 +++++ ot/gpu/bregman.py | 5 +++++ ot/gpu/da.py | 9 +++++++++ ot/lp/__init__.py | 4 ++++ ot/lp/emd_wrap.pyx | 9 ++++++--- ot/optim.py | 4 ++++ ot/plot.py | 3 +++ ot/utils.py | 5 +++++ 13 files changed, 64 insertions(+), 3 deletions(-) diff --git a/ot/__init__.py b/ot/__init__.py index a79a5ce..c2161e4 100644 --- a/ot/__init__.py +++ b/ot/__init__.py @@ -4,6 +4,10 @@ """ +# Author: Remi Flamary +# +# License: MIT License + # All submodules and packages from . import lp diff --git a/ot/bregman.py b/ot/bregman.py index fe10880..71a5548 100644 --- a/ot/bregman.py +++ b/ot/bregman.py @@ -3,6 +3,11 @@ Bregman projections for regularized OT """ +# Author: Remi Flamary +# Nicolas Courty +# +# License: MIT License + import numpy as np diff --git a/ot/da.py b/ot/da.py index 5039fbd..977d532 100644 --- a/ot/da.py +++ b/ot/da.py @@ -3,6 +3,12 @@ Domain adaptation with optimal transport """ +# Author: Remi Flamary +# Nicolas Courty +# Michael Perrot +# +# License: MIT License + import numpy as np from .bregman import sinkhorn from .lp import emd diff --git a/ot/datasets.py b/ot/datasets.py index 4371a23..e4fe118 100644 --- a/ot/datasets.py +++ b/ot/datasets.py @@ -2,6 +2,10 @@ Simple example datasets for OT """ +# Author: Remi Flamary +# +# License: MIT License + import numpy as np import scipy as sp diff --git a/ot/dr.py b/ot/dr.py index 77cbae2..d30ab30 100644 --- a/ot/dr.py +++ b/ot/dr.py @@ -3,6 +3,10 @@ Dimension reduction with optimal transport """ +# Author: Remi Flamary +# +# License: MIT License + from scipy import linalg import autograd.numpy as np from pymanopt.manifolds import Stiefel diff --git a/ot/gpu/__init__.py b/ot/gpu/__init__.py index 40b11c0..c8f9433 100644 --- a/ot/gpu/__init__.py +++ b/ot/gpu/__init__.py @@ -4,4 +4,9 @@ from . import bregman from . import da from .bregman import sinkhorn +# Author: Remi Flamary +# Leo Gautheron +# +# License: MIT License + __all__ = ["bregman", "da", "sinkhorn"] diff --git a/ot/gpu/bregman.py b/ot/gpu/bregman.py index 2302f80..86bfec1 100644 --- a/ot/gpu/bregman.py +++ b/ot/gpu/bregman.py @@ -3,6 +3,11 @@ Bregman projections for regularized OT with GPU """ +# Author: Remi Flamary +# Leo Gautheron +# +# License: MIT License + import numpy as np import cudamat diff --git a/ot/gpu/da.py b/ot/gpu/da.py index c66e755..7fb488d 100644 --- a/ot/gpu/da.py +++ b/ot/gpu/da.py @@ -3,6 +3,15 @@ Domain adaptation with optimal transport with GPU implementation """ +# Author: Remi Flamary +# Nicolas Courty +# Michael Perrot +# Leo Gautheron +# +# License: MIT License + + + import numpy as np from ..utils import unif from ..da import OTDA diff --git a/ot/lp/__init__.py b/ot/lp/__init__.py index db3da78..6e0bdb8 100644 --- a/ot/lp/__init__.py +++ b/ot/lp/__init__.py @@ -3,6 +3,10 @@ Solvers for the original linear program OT problem """ +# Author: Remi Flamary +# +# License: MIT License + import numpy as np # import compiled emd from .emd_wrap import emd_c, emd2_c diff --git a/ot/lp/emd_wrap.pyx b/ot/lp/emd_wrap.pyx index 46794ab..46c96c1 100644 --- a/ot/lp/emd_wrap.pyx +++ b/ot/lp/emd_wrap.pyx @@ -1,9 +1,12 @@ # -*- coding: utf-8 -*- """ -Created on Thu Sep 11 08:42:08 2014 - -@author: rflamary +Cython linker with C solver """ + +# Author: Remi Flamary +# +# License: MIT License + import numpy as np cimport numpy as np diff --git a/ot/optim.py b/ot/optim.py index adad95e..c59089c 100644 --- a/ot/optim.py +++ b/ot/optim.py @@ -3,6 +3,10 @@ Optimization algorithms for OT """ +# Author: Remi Flamary +# +# License: MIT License + import numpy as np from scipy.optimize.linesearch import scalar_search_armijo from .lp import emd diff --git a/ot/plot.py b/ot/plot.py index 61afc9f..784a372 100644 --- a/ot/plot.py +++ b/ot/plot.py @@ -2,6 +2,9 @@ Functions for plotting OT matrices """ +# Author: Remi Flamary +# +# License: MIT License import numpy as np import matplotlib.pylab as pl diff --git a/ot/utils.py b/ot/utils.py index 1dee932..2b2f8b3 100644 --- a/ot/utils.py +++ b/ot/utils.py @@ -2,6 +2,11 @@ """ Various function that can be usefull """ + +# Author: Remi Flamary +# +# License: MIT License + import multiprocessing from functools import reduce import time -- cgit v1.2.3 From 251af8eec2b39e74000242cbf5bff5e13910cfe8 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Wed, 26 Jul 2017 12:18:33 +0200 Subject: add author to all examples --- examples/plot_OTDA_2D.py | 4 ++++ examples/plot_OTDA_classes.py | 4 ++++ examples/plot_OTDA_color_images.py | 4 ++++ examples/plot_OTDA_mapping.py | 4 ++++ examples/plot_OTDA_mapping_color_images.py | 4 ++++ examples/plot_OT_1D.py | 5 ++++- examples/plot_OT_2D_samples.py | 5 ++++- examples/plot_OT_L1_vs_L2.py | 5 ++++- examples/plot_WDA.py | 5 ++++- examples/plot_barycenter_1D.py | 6 ++++-- examples/plot_compute_emd.py | 5 ++++- 11 files changed, 44 insertions(+), 7 deletions(-) diff --git a/examples/plot_OTDA_2D.py b/examples/plot_OTDA_2D.py index 1bda59c..f2108c6 100644 --- a/examples/plot_OTDA_2D.py +++ b/examples/plot_OTDA_2D.py @@ -6,6 +6,10 @@ OT for empirical distributions """ +# Author: Remi Flamary +# +# License: MIT License + import numpy as np import matplotlib.pylab as pl import ot diff --git a/examples/plot_OTDA_classes.py b/examples/plot_OTDA_classes.py index 4d3846a..53e4bae 100644 --- a/examples/plot_OTDA_classes.py +++ b/examples/plot_OTDA_classes.py @@ -6,6 +6,10 @@ OT for domain adaptation """ +# Author: Remi Flamary +# +# License: MIT License + import matplotlib.pylab as pl import ot diff --git a/examples/plot_OTDA_color_images.py b/examples/plot_OTDA_color_images.py index 75ac5b6..c5ff873 100644 --- a/examples/plot_OTDA_color_images.py +++ b/examples/plot_OTDA_color_images.py @@ -9,6 +9,10 @@ Regularized discrete optimal transport. SIAM Journal on Imaging Sciences, 7(3), 1853-1882. """ +# Author: Remi Flamary +# +# License: MIT License + import numpy as np from scipy import ndimage import matplotlib.pylab as pl diff --git a/examples/plot_OTDA_mapping.py b/examples/plot_OTDA_mapping.py index a5c2b21..a0d7f8b 100644 --- a/examples/plot_OTDA_mapping.py +++ b/examples/plot_OTDA_mapping.py @@ -9,6 +9,10 @@ OT mapping estimation for domain adaptation [8] Neural Information Processing Systems (NIPS), 2016. """ +# Author: Remi Flamary +# +# License: MIT License + import numpy as np import matplotlib.pylab as pl import ot diff --git a/examples/plot_OTDA_mapping_color_images.py b/examples/plot_OTDA_mapping_color_images.py index 9710461..8064b25 100644 --- a/examples/plot_OTDA_mapping_color_images.py +++ b/examples/plot_OTDA_mapping_color_images.py @@ -11,6 +11,10 @@ OT for domain adaptation with image color adaptation [6] with mapping estimation """ +# Author: Remi Flamary +# +# License: MIT License + import numpy as np from scipy import ndimage import matplotlib.pylab as pl diff --git a/examples/plot_OT_1D.py b/examples/plot_OT_1D.py index 2f3b924..0f3a26a 100644 --- a/examples/plot_OT_1D.py +++ b/examples/plot_OT_1D.py @@ -4,9 +4,12 @@ 1D optimal transport ==================== -@author: rflamary """ +# Author: Remi Flamary +# +# License: MIT License + import numpy as np import matplotlib.pylab as pl import ot diff --git a/examples/plot_OT_2D_samples.py b/examples/plot_OT_2D_samples.py index 75ed7db..023e645 100644 --- a/examples/plot_OT_2D_samples.py +++ b/examples/plot_OT_2D_samples.py @@ -4,9 +4,12 @@ 2D Optimal transport between empirical distributions ==================================================== -@author: rflamary """ +# Author: Remi Flamary +# +# License: MIT License + import numpy as np import matplotlib.pylab as pl import ot diff --git a/examples/plot_OT_L1_vs_L2.py b/examples/plot_OT_L1_vs_L2.py index 86d902b..dfc9462 100644 --- a/examples/plot_OT_L1_vs_L2.py +++ b/examples/plot_OT_L1_vs_L2.py @@ -8,9 +8,12 @@ Stole the figure idea from Fig. 1 and 2 in https://arxiv.org/pdf/1706.07650.pdf -@author: rflamary """ +# Author: Remi Flamary +# +# License: MIT License + import numpy as np import matplotlib.pylab as pl import ot diff --git a/examples/plot_WDA.py b/examples/plot_WDA.py index 9eb8693..42789f2 100644 --- a/examples/plot_WDA.py +++ b/examples/plot_WDA.py @@ -4,9 +4,12 @@ Wasserstein Discriminant Analysis ================================= -@author: rflamary """ +# Author: Remi Flamary +# +# License: MIT License + import numpy as np import matplotlib.pylab as pl diff --git a/examples/plot_barycenter_1D.py b/examples/plot_barycenter_1D.py index ab236e1..875f44c 100644 --- a/examples/plot_barycenter_1D.py +++ b/examples/plot_barycenter_1D.py @@ -4,10 +4,12 @@ 1D Wasserstein barycenter demo ============================== - -@author: rflamary """ +# Author: Remi Flamary +# +# License: MIT License + import numpy as np import matplotlib.pylab as pl import ot diff --git a/examples/plot_compute_emd.py b/examples/plot_compute_emd.py index 558facb..893eecf 100644 --- a/examples/plot_compute_emd.py +++ b/examples/plot_compute_emd.py @@ -4,9 +4,12 @@ 1D optimal transport ==================== -@author: rflamary """ +# Author: Remi Flamary +# +# License: MIT License + import numpy as np import matplotlib.pylab as pl import ot -- cgit v1.2.3 From 84aa3183491260b9c3dbb9f928499cc18e5341c1 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Wed, 26 Jul 2017 12:22:00 +0200 Subject: pep8 --- ot/__init__.py | 2 +- ot/bregman.py | 16 +++++++++------- ot/da.py | 4 ++-- ot/optim.py | 2 +- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/ot/__init__.py b/ot/__init__.py index c2161e4..6d4c4c6 100644 --- a/ot/__init__.py +++ b/ot/__init__.py @@ -28,6 +28,6 @@ from .utils import dist, unif, tic, toc, toq __version__ = "0.3.1" -__all__ = ["emd", "emd2", "sinkhorn","sinkhorn2", "utils", 'datasets', +__all__ = ["emd", "emd2", "sinkhorn", "sinkhorn2", "utils", 'datasets', 'bregman', 'lp', 'plot', 'tic', 'toc', 'toq', 'dist', 'unif', 'barycenter', 'sinkhorn_lpl1_mm', 'da', 'optim'] diff --git a/ot/bregman.py b/ot/bregman.py index 71a5548..929388e 100644 --- a/ot/bregman.py +++ b/ot/bregman.py @@ -108,7 +108,8 @@ def sinkhorn(a, b, M, reg, method='sinkhorn', numItermax=1000, stopThr=1e-9, ver stopThr=stopThr, verbose=verbose, log=log, **kwargs) elif method.lower() == 'sinkhorn_epsilon_scaling': def sink(): - return sinkhorn_epsilon_scaling(a, b, M, reg, numItermax=numItermax, + return sinkhorn_epsilon_scaling( + a, b, M, reg, numItermax=numItermax, stopThr=stopThr, verbose=verbose, log=log, **kwargs) else: print('Warning : unknown method using classic Sinkhorn Knopp') @@ -216,7 +217,8 @@ def sinkhorn2(a, b, M, reg, method='sinkhorn', numItermax=1000, stopThr=1e-9, ve stopThr=stopThr, verbose=verbose, log=log, **kwargs) elif method.lower() == 'sinkhorn_epsilon_scaling': def sink(): - return sinkhorn_epsilon_scaling(a, b, M, reg, numItermax=numItermax, + return sinkhorn_epsilon_scaling( + a, b, M, reg, numItermax=numItermax, stopThr=stopThr, verbose=verbose, log=log, **kwargs) else: print('Warning : unknown method using classic Sinkhorn Knopp') @@ -593,7 +595,7 @@ def sinkhorn_stabilized(a, b, M, reg, numItermax=1000, tau=1e3, stopThr=1e-9, wa cpt = cpt + 1 - #print('err=',err,' cpt=',cpt) + # print('err=',err,' cpt=',cpt) if log: log['logu'] = alpha / reg + np.log(u) log['logv'] = beta / reg + np.log(v) @@ -778,7 +780,7 @@ def sinkhorn_epsilon_scaling(a, b, M, reg, numItermax=100, epsilon0=1e4, numInne loop = False cpt = cpt + 1 - #print('err=',err,' cpt=',cpt) + # print('err=',err,' cpt=',cpt) if log: log['alpha'] = alpha log['beta'] = beta @@ -965,16 +967,16 @@ def unmix(a, D, M, M0, h0, reg, reg0, alpha, numItermax=1000, stopThr=1e-3, verb """ - #M = M/np.median(M) + # M = M/np.median(M) K = np.exp(-M / reg) - #M0 = M0/np.median(M0) + # M0 = M0/np.median(M0) K0 = np.exp(-M0 / reg0) old = h0 err = 1 cpt = 0 - #log = {'niter':0, 'all_err':[]} + # log = {'niter':0, 'all_err':[]} if log: log = {'err': []} diff --git a/ot/da.py b/ot/da.py index 977d532..4f9bce5 100644 --- a/ot/da.py +++ b/ot/da.py @@ -478,7 +478,7 @@ def joint_OT_mapping_kernel(xs, xt, mu=1, eta=0.001, kerneltype='gaussian', sigm Kp[:ns, :ns] = K # ls regu - #K0 = K1.T.dot(K1)+eta*I + # K0 = K1.T.dot(K1)+eta*I # Kreg=I # RKHS regul @@ -490,7 +490,7 @@ def joint_OT_mapping_kernel(xs, xt, mu=1, eta=0.001, kerneltype='gaussian', sigm I = np.eye(ns) # ls regul - #K0 = K1.T.dot(K1)+eta*I + # K0 = K1.T.dot(K1)+eta*I # Kreg=I # proper kernel ridge diff --git a/ot/optim.py b/ot/optim.py index c59089c..1d09adc 100644 --- a/ot/optim.py +++ b/ot/optim.py @@ -304,7 +304,7 @@ def gcg(a, b, M, reg1, reg2, f, df, G0=None, numItermax=10, numInnerItermax=200, Mi = M + reg2 * df(G) # solve linear program with Sinkhorn - #Gc = sinkhorn_stabilized(a,b, Mi, reg1, numItermax = numInnerItermax) + # Gc = sinkhorn_stabilized(a,b, Mi, reg1, numItermax = numInnerItermax) Gc = sinkhorn(a, b, Mi, reg1, numItermax=numInnerItermax) deltaG = Gc - G -- cgit v1.2.3 From 96f8b96cdd50be633d4f3c6f3255cb456e492e08 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Wed, 26 Jul 2017 12:33:14 +0200 Subject: valid flake8 --- ot/bregman.py | 4 ++-- ot/gpu/bregman.py | 2 +- ot/gpu/da.py | 3 +-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/ot/bregman.py b/ot/bregman.py index 929388e..d63c51d 100644 --- a/ot/bregman.py +++ b/ot/bregman.py @@ -110,7 +110,7 @@ def sinkhorn(a, b, M, reg, method='sinkhorn', numItermax=1000, stopThr=1e-9, ver def sink(): return sinkhorn_epsilon_scaling( a, b, M, reg, numItermax=numItermax, - stopThr=stopThr, verbose=verbose, log=log, **kwargs) + stopThr=stopThr, verbose=verbose, log=log, **kwargs) else: print('Warning : unknown method using classic Sinkhorn Knopp') @@ -219,7 +219,7 @@ def sinkhorn2(a, b, M, reg, method='sinkhorn', numItermax=1000, stopThr=1e-9, ve def sink(): return sinkhorn_epsilon_scaling( a, b, M, reg, numItermax=numItermax, - stopThr=stopThr, verbose=verbose, log=log, **kwargs) + stopThr=stopThr, verbose=verbose, log=log, **kwargs) else: print('Warning : unknown method using classic Sinkhorn Knopp') diff --git a/ot/gpu/bregman.py b/ot/gpu/bregman.py index 86bfec1..47939c4 100644 --- a/ot/gpu/bregman.py +++ b/ot/gpu/bregman.py @@ -4,7 +4,7 @@ Bregman projections for regularized OT with GPU """ # Author: Remi Flamary -# Leo Gautheron +# Leo Gautheron # # License: MIT License diff --git a/ot/gpu/da.py b/ot/gpu/da.py index 7fb488d..05c580f 100644 --- a/ot/gpu/da.py +++ b/ot/gpu/da.py @@ -6,12 +6,11 @@ Domain adaptation with optimal transport with GPU implementation # Author: Remi Flamary # Nicolas Courty # Michael Perrot -# Leo Gautheron +# Leo Gautheron # # License: MIT License - import numpy as np from ..utils import unif from ..da import OTDA -- cgit v1.2.3 From 838550ead9cc8a66d9b9c1212c5dda2457dc59a5 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Wed, 26 Jul 2017 15:12:44 +0200 Subject: last stuff --- test/test_utils.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/test_utils.py b/test/test_utils.py index 9b140db..1bd37cd 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -22,7 +22,7 @@ def test_parmap(): l2 = list(ot.utils.parmap(f, a)) - assert np.allclose(l1, l2) + np.testing.assert_allclose(l1, l2) def test_tic_toc(): @@ -35,10 +35,10 @@ def test_tic_toc(): t2 = ot.toq() # test timing - assert np.allclose(0.5, t, rtol=1e-2, atol=1e-2) + np.testing.assert_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) + np.testing.assert_allclose(t, t2, rtol=1e-2, atol=1e-2) def test_kernel(): @@ -50,7 +50,7 @@ def test_kernel(): K = ot.utils.kernel(x, x) # gaussian kernel has ones on the diagonal - assert np.allclose(np.diag(K), np.ones(n)) + np.testing.assert_allclose(np.diag(K), np.ones(n)) def test_unif(): @@ -59,7 +59,7 @@ def test_unif(): u = ot.unif(n) - assert np.allclose(1, np.sum(u)) + np.testing.assert_allclose(1, np.sum(u)) def test_dist(): @@ -77,8 +77,8 @@ def test_dist(): D3 = ot.dist(x) # dist shoul return squared euclidean - assert np.allclose(D, D2) - assert np.allclose(D, D3) + np.testing.assert_allclose(D, D2) + np.testing.assert_allclose(D, D3) def test_dist0(): @@ -87,7 +87,7 @@ def test_dist0(): M = ot.utils.dist0(n, method='lin_square') # dist0 default to linear sampling with quadratic loss - assert np.allclose(M[0, -1], (n - 1) * (n - 1)) + np.testing.assert_allclose(M[0, -1], (n - 1) * (n - 1)) def test_dots(): @@ -102,7 +102,7 @@ def test_dots(): X2 = A.dot(B.dot(C)) - assert np.allclose(X1, X2) + np.testing.assert_allclose(X1, X2) def test_clean_zeros(): -- cgit v1.2.3