diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/test_bregman.py | 10 | ||||
-rw-r--r-- | test/test_da.py | 52 | ||||
-rw-r--r-- | test/test_dr.py | 4 | ||||
-rw-r--r-- | test/test_gromov.py | 16 | ||||
-rw-r--r-- | test/test_optim.py | 8 | ||||
-rw-r--r-- | test/test_ot.py | 2 | ||||
-rw-r--r-- | test/test_plot.py | 8 | ||||
-rw-r--r-- | test/test_smooth.py | 79 |
8 files changed, 129 insertions, 50 deletions
diff --git a/test/test_bregman.py b/test/test_bregman.py index 4a800fd..c8e9179 100644 --- a/test/test_bregman.py +++ b/test/test_bregman.py @@ -83,8 +83,8 @@ def test_bary(): n_bins = 100 # nb bins # Gaussian distributions - 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) + a1 = ot.datasets.make_1D_gauss(n_bins, m=30, s=10) # m= mean, s= std + a2 = ot.datasets.make_1D_gauss(n_bins, m=40, s=10) # creating matrix A containing all distributions A = np.vstack((a1, a2)).T @@ -110,10 +110,10 @@ def test_unmix(): n_bins = 50 # nb bins # Gaussian distributions - 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) + a1 = ot.datasets.make_1D_gauss(n_bins, m=20, s=10) # m= mean, s= std + a2 = ot.datasets.make_1D_gauss(n_bins, m=40, s=10) - a = ot.datasets.get_1D_gauss(n_bins, m=30, s=10) + a = ot.datasets.make_1D_gauss(n_bins, m=30, s=10) # creating matrix A containing all distributions D = np.vstack((a1, a2)).T diff --git a/test/test_da.py b/test/test_da.py index 3022721..97e23da 100644 --- a/test/test_da.py +++ b/test/test_da.py @@ -8,7 +8,7 @@ import numpy as np from numpy.testing.utils import assert_allclose, assert_equal import ot -from ot.datasets import get_data_classif +from ot.datasets import make_data_classif from ot.utils import unif @@ -19,8 +19,8 @@ def test_sinkhorn_lpl1_transport_class(): ns = 150 nt = 200 - Xs, ys = get_data_classif('3gauss', ns) - Xt, yt = get_data_classif('3gauss2', nt) + Xs, ys = make_data_classif('3gauss', ns) + Xt, yt = make_data_classif('3gauss2', nt) otda = ot.da.SinkhornLpl1Transport() @@ -45,7 +45,7 @@ def test_sinkhorn_lpl1_transport_class(): transp_Xs = otda.transform(Xs=Xs) assert_equal(transp_Xs.shape, Xs.shape) - Xs_new, _ = get_data_classif('3gauss', ns + 1) + Xs_new, _ = make_data_classif('3gauss', ns + 1) transp_Xs_new = otda.transform(Xs_new) # check that the oos method is working @@ -55,7 +55,7 @@ def test_sinkhorn_lpl1_transport_class(): transp_Xt = otda.inverse_transform(Xt=Xt) assert_equal(transp_Xt.shape, Xt.shape) - Xt_new, _ = get_data_classif('3gauss2', nt + 1) + Xt_new, _ = make_data_classif('3gauss2', nt + 1) transp_Xt_new = otda.inverse_transform(Xt=Xt_new) # check that the oos method is working @@ -92,8 +92,8 @@ def test_sinkhorn_l1l2_transport_class(): ns = 150 nt = 200 - Xs, ys = get_data_classif('3gauss', ns) - Xt, yt = get_data_classif('3gauss2', nt) + Xs, ys = make_data_classif('3gauss', ns) + Xt, yt = make_data_classif('3gauss2', nt) otda = ot.da.SinkhornL1l2Transport() @@ -119,7 +119,7 @@ def test_sinkhorn_l1l2_transport_class(): transp_Xs = otda.transform(Xs=Xs) assert_equal(transp_Xs.shape, Xs.shape) - Xs_new, _ = get_data_classif('3gauss', ns + 1) + Xs_new, _ = make_data_classif('3gauss', ns + 1) transp_Xs_new = otda.transform(Xs_new) # check that the oos method is working @@ -129,7 +129,7 @@ def test_sinkhorn_l1l2_transport_class(): transp_Xt = otda.inverse_transform(Xt=Xt) assert_equal(transp_Xt.shape, Xt.shape) - Xt_new, _ = get_data_classif('3gauss2', nt + 1) + Xt_new, _ = make_data_classif('3gauss2', nt + 1) transp_Xt_new = otda.inverse_transform(Xt=Xt_new) # check that the oos method is working @@ -173,8 +173,8 @@ def test_sinkhorn_transport_class(): ns = 150 nt = 200 - Xs, ys = get_data_classif('3gauss', ns) - Xt, yt = get_data_classif('3gauss2', nt) + Xs, ys = make_data_classif('3gauss', ns) + Xt, yt = make_data_classif('3gauss2', nt) otda = ot.da.SinkhornTransport() @@ -200,7 +200,7 @@ def test_sinkhorn_transport_class(): transp_Xs = otda.transform(Xs=Xs) assert_equal(transp_Xs.shape, Xs.shape) - Xs_new, _ = get_data_classif('3gauss', ns + 1) + Xs_new, _ = make_data_classif('3gauss', ns + 1) transp_Xs_new = otda.transform(Xs_new) # check that the oos method is working @@ -210,7 +210,7 @@ def test_sinkhorn_transport_class(): transp_Xt = otda.inverse_transform(Xt=Xt) assert_equal(transp_Xt.shape, Xt.shape) - Xt_new, _ = get_data_classif('3gauss2', nt + 1) + Xt_new, _ = make_data_classif('3gauss2', nt + 1) transp_Xt_new = otda.inverse_transform(Xt=Xt_new) # check that the oos method is working @@ -252,8 +252,8 @@ def test_emd_transport_class(): ns = 150 nt = 200 - Xs, ys = get_data_classif('3gauss', ns) - Xt, yt = get_data_classif('3gauss2', nt) + Xs, ys = make_data_classif('3gauss', ns) + Xt, yt = make_data_classif('3gauss2', nt) otda = ot.da.EMDTransport() @@ -278,7 +278,7 @@ def test_emd_transport_class(): transp_Xs = otda.transform(Xs=Xs) assert_equal(transp_Xs.shape, Xs.shape) - Xs_new, _ = get_data_classif('3gauss', ns + 1) + Xs_new, _ = make_data_classif('3gauss', ns + 1) transp_Xs_new = otda.transform(Xs_new) # check that the oos method is working @@ -288,7 +288,7 @@ def test_emd_transport_class(): transp_Xt = otda.inverse_transform(Xt=Xt) assert_equal(transp_Xt.shape, Xt.shape) - Xt_new, _ = get_data_classif('3gauss2', nt + 1) + Xt_new, _ = make_data_classif('3gauss2', nt + 1) transp_Xt_new = otda.inverse_transform(Xt=Xt_new) # check that the oos method is working @@ -329,9 +329,9 @@ def test_mapping_transport_class(): ns = 60 nt = 120 - Xs, ys = get_data_classif('3gauss', ns) - Xt, yt = get_data_classif('3gauss2', nt) - Xs_new, _ = get_data_classif('3gauss', ns + 1) + Xs, ys = make_data_classif('3gauss', ns) + Xt, yt = make_data_classif('3gauss2', nt) + Xs_new, _ = make_data_classif('3gauss', ns + 1) ########################################################################## # kernel == linear mapping tests @@ -449,8 +449,8 @@ def test_linear_mapping(): ns = 150 nt = 200 - Xs, ys = get_data_classif('3gauss', ns) - Xt, yt = get_data_classif('3gauss2', nt) + Xs, ys = make_data_classif('3gauss', ns) + Xt, yt = make_data_classif('3gauss2', nt) A, b = ot.da.OT_mapping_linear(Xs, Xt) @@ -467,8 +467,8 @@ def test_linear_mapping_class(): ns = 150 nt = 200 - Xs, ys = get_data_classif('3gauss', ns) - Xt, yt = get_data_classif('3gauss2', nt) + Xs, ys = make_data_classif('3gauss', ns) + Xt, yt = make_data_classif('3gauss2', nt) otmap = ot.da.LinearTransport() @@ -491,8 +491,8 @@ def test_otda(): n_samples = 150 # nb samples np.random.seed(0) - xs, ys = ot.datasets.get_data_classif('3gauss', n_samples) - xt, yt = ot.datasets.get_data_classif('3gauss2', n_samples) + xs, ys = ot.datasets.make_data_classif('3gauss', n_samples) + xt, yt = ot.datasets.make_data_classif('3gauss2', n_samples) a, b = ot.unif(n_samples), ot.unif(n_samples) diff --git a/test/test_dr.py b/test/test_dr.py index 915012d..c5df287 100644 --- a/test/test_dr.py +++ b/test/test_dr.py @@ -22,7 +22,7 @@ def test_fda(): np.random.seed(0) # generate gaussian dataset - xs, ys = ot.datasets.get_data_classif('gaussrot', n_samples) + xs, ys = ot.datasets.make_data_classif('gaussrot', n_samples) n_features_noise = 8 @@ -44,7 +44,7 @@ def test_wda(): np.random.seed(0) # generate gaussian dataset - xs, ys = ot.datasets.get_data_classif('gaussrot', n_samples) + xs, ys = ot.datasets.make_data_classif('gaussrot', n_samples) n_features_noise = 8 diff --git a/test/test_gromov.py b/test/test_gromov.py index bb23469..fb86274 100644 --- a/test/test_gromov.py +++ b/test/test_gromov.py @@ -15,7 +15,7 @@ def test_gromov(): mu_s = np.array([0, 0])
cov_s = np.array([[1, 0], [0, 1]])
- xs = ot.datasets.get_2D_samples_gauss(n_samples, mu_s, cov_s)
+ xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s)
xt = xs[::-1].copy()
@@ -55,7 +55,7 @@ def test_entropic_gromov(): mu_s = np.array([0, 0])
cov_s = np.array([[1, 0], [0, 1]])
- xs = ot.datasets.get_2D_samples_gauss(n_samples, mu_s, cov_s)
+ xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s)
xt = xs[::-1].copy()
@@ -96,8 +96,8 @@ def test_gromov_barycenter(): ns = 50
nt = 60
- Xs, ys = ot.datasets.get_data_classif('3gauss', ns)
- Xt, yt = ot.datasets.get_data_classif('3gauss2', nt)
+ Xs, ys = ot.datasets.make_data_classif('3gauss', ns)
+ Xt, yt = ot.datasets.make_data_classif('3gauss2', nt)
C1 = ot.dist(Xs)
C2 = ot.dist(Xt)
@@ -123,8 +123,8 @@ def test_gromov_entropic_barycenter(): ns = 50
nt = 60
- Xs, ys = ot.datasets.get_data_classif('3gauss', ns)
- Xt, yt = ot.datasets.get_data_classif('3gauss2', nt)
+ Xs, ys = ot.datasets.make_data_classif('3gauss', ns)
+ Xt, yt = ot.datasets.make_data_classif('3gauss2', nt)
C1 = ot.dist(Xs)
C2 = ot.dist(Xt)
@@ -133,13 +133,13 @@ def test_gromov_entropic_barycenter(): Cb = ot.gromov.entropic_gromov_barycenters(n_samples, [C1, C2],
[ot.unif(ns), ot.unif(nt)
], ot.unif(n_samples), [.5, .5],
- 'square_loss', 1e-3,
+ 'square_loss', 2e-3,
max_iter=100, tol=1e-3)
np.testing.assert_allclose(Cb.shape, (n_samples, n_samples))
Cb2 = ot.gromov.entropic_gromov_barycenters(n_samples, [C1, C2],
[ot.unif(ns), ot.unif(nt)
], ot.unif(n_samples), [.5, .5],
- 'kl_loss', 1e-3,
+ 'kl_loss', 2e-3,
max_iter=100, tol=1e-3)
np.testing.assert_allclose(Cb2.shape, (n_samples, n_samples))
diff --git a/test/test_optim.py b/test/test_optim.py index 69496a5..dfefe59 100644 --- a/test/test_optim.py +++ b/test/test_optim.py @@ -16,8 +16,8 @@ def test_conditional_gradient(): x = np.arange(n_bins, dtype=np.float64) # Gaussian distributions - 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) + a = ot.datasets.make_1D_gauss(n_bins, m=20, s=5) # m= mean, s= std + b = ot.datasets.make_1D_gauss(n_bins, m=60, s=10) # loss matrix M = ot.dist(x.reshape((n_bins, 1)), x.reshape((n_bins, 1))) @@ -45,8 +45,8 @@ def test_generalized_conditional_gradient(): x = np.arange(n_bins, dtype=np.float64) # Gaussian distributions - 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) + a = ot.datasets.make_1D_gauss(n_bins, m=20, s=5) # m= mean, s= std + b = ot.datasets.make_1D_gauss(n_bins, m=60, s=10) # loss matrix M = ot.dist(x.reshape((n_bins, 1)), x.reshape((n_bins, 1))) diff --git a/test/test_ot.py b/test/test_ot.py index cc25bf4..399e549 100644 --- a/test/test_ot.py +++ b/test/test_ot.py @@ -9,7 +9,7 @@ import warnings import numpy as np import ot -from ot.datasets import get_1D_gauss as gauss +from ot.datasets import make_1D_gauss as gauss import pytest diff --git a/test/test_plot.py b/test/test_plot.py index a50ed14..f77d879 100644 --- a/test/test_plot.py +++ b/test/test_plot.py @@ -20,8 +20,8 @@ def test_plot1D_mat(): x = np.arange(n_bins, dtype=np.float64) # Gaussian distributions - 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) + a = ot.datasets.make_1D_gauss(n_bins, m=20, s=5) # m= mean, s= std + b = ot.datasets.make_1D_gauss(n_bins, m=60, s=10) # loss matrix M = ot.dist(x.reshape((n_bins, 1)), x.reshape((n_bins, 1))) @@ -43,8 +43,8 @@ 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_bins, mu_s, cov_s) - xt = ot.datasets.get_2D_samples_gauss(n_bins, mu_t, cov_t) + xs = ot.datasets.make_2D_samples_gauss(n_bins, mu_s, cov_s) + xt = ot.datasets.make_2D_samples_gauss(n_bins, mu_t, cov_t) G = 1.0 * (np.random.rand(n_bins, n_bins) < 0.01) diff --git a/test/test_smooth.py b/test/test_smooth.py new file mode 100644 index 0000000..2afa4f8 --- /dev/null +++ b/test/test_smooth.py @@ -0,0 +1,79 @@ +"""Tests for ot.smooth model """ + +# Author: Remi Flamary <remi.flamary@unice.fr> +# +# License: MIT License + +import numpy as np +import ot +import pytest + + +def test_smooth_ot_dual(): + + # get data + n = 100 + rng = np.random.RandomState(0) + + x = rng.randn(n, 2) + u = ot.utils.unif(n) + + M = ot.dist(x, x) + + with pytest.raises(NotImplementedError): + Gl2, log = ot.smooth.smooth_ot_dual(u, u, M, 1, reg_type='none') + + Gl2, log = ot.smooth.smooth_ot_dual(u, u, M, 1, reg_type='l2', log=True, stopThr=1e-10) + + # check constratints + np.testing.assert_allclose( + u, Gl2.sum(1), atol=1e-05) # cf convergence sinkhorn + np.testing.assert_allclose( + u, Gl2.sum(0), atol=1e-05) # cf convergence sinkhorn + + # kl regyularisation + G = ot.smooth.smooth_ot_dual(u, u, M, 1, reg_type='kl', stopThr=1e-10) + + # check constratints + 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 + + G2 = ot.sinkhorn(u, u, M, 1, stopThr=1e-10) + np.testing.assert_allclose(G, G2, atol=1e-05) + + +def test_smooth_ot_semi_dual(): + + # get data + n = 100 + rng = np.random.RandomState(0) + + x = rng.randn(n, 2) + u = ot.utils.unif(n) + + M = ot.dist(x, x) + + with pytest.raises(NotImplementedError): + Gl2, log = ot.smooth.smooth_ot_semi_dual(u, u, M, 1, reg_type='none') + + Gl2, log = ot.smooth.smooth_ot_semi_dual(u, u, M, 1, reg_type='l2', log=True, stopThr=1e-10) + + # check constratints + np.testing.assert_allclose( + u, Gl2.sum(1), atol=1e-05) # cf convergence sinkhorn + np.testing.assert_allclose( + u, Gl2.sum(0), atol=1e-05) # cf convergence sinkhorn + + # kl regyularisation + G = ot.smooth.smooth_ot_semi_dual(u, u, M, 1, reg_type='kl', stopThr=1e-10) + + # check constratints + 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 + + G2 = ot.sinkhorn(u, u, M, 1, stopThr=1e-10) + np.testing.assert_allclose(G, G2, atol=1e-05) |