Debian Codegraph @

test/gromov/test_lowrank.py

raw symbols SCIP
1 """Tests for gromov._lowrank.py"""
2
3 # Author: Laurène DAVID <[email protected]>
4 #
5 # License: MIT License
6
7 import ot
8 import numpy as np
9 import pytest
10
11
12 def test__flat_product_operator():
13 # test flat product operator
14 n, d = 100, 2
15 X = np.reshape(1.0 * np.arange(2 * n), (n, d))
16 A1, A2 = ot.lowrank.compute_lr_sqeuclidean_matrix(X, X, rescale_cost=False)
17
18 A1_ = ot.gromov._lowrank._flat_product_operator(A1)
19 A2_ = ot.gromov._lowrank._flat_product_operator(A2)
20 cost = ot.dist(X, X)
21
22 # test value
23 np.testing.assert_allclose(cost**2, np.dot(A1_, A2_.T), atol=1e-05)
24
25
26 def test_lowrank_gromov_wasserstein_samples():
27 # test low rank gromov wasserstein
28 n_samples = 20 # nb samples
29 mu_s = np.array([0, 0])
30 cov_s = np.array([[1, 0], [0, 1]])
31
32 X_s = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=1)
33 X_t = X_s[::-1].copy()
34
35 a = ot.unif(n_samples)
36 b = ot.unif(n_samples)
37
38 Q, R, g, log = ot.gromov.lowrank_gromov_wasserstein_samples(
39 X_s, X_t, a, b, reg=0.1, log=True, rescale_cost=False
40 )
41 P = log["lazy_plan"][:]
42
43 # check constraints for P
44 np.testing.assert_allclose(a, P.sum(1), atol=1e-04)
45 np.testing.assert_allclose(b, P.sum(0), atol=1e-04)
46
47 # check if lazy_plan is equal to the fully computed plan
48 P_true = np.dot(Q, np.dot(np.diag(1 / g), R.T))
49 np.testing.assert_allclose(P, P_true, atol=1e-05)
50
51 # check warn parameter when low rank GW algorithm doesn't converge
52 with pytest.warns(UserWarning):
53 ot.gromov.lowrank_gromov_wasserstein_samples(
54 X_s,
55 X_t,
56 a,
57 b,
58 reg=0.1,
59 stopThr=0,
60 numItermax=1,
61 warn=True,
62 warn_dykstra=False,
63 )
64
65 # check warn parameter when Dykstra algorithm doesn't converge
66 with pytest.warns(UserWarning):
67 ot.gromov.lowrank_gromov_wasserstein_samples(
68 X_s,
69 X_t,
70 a,
71 b,
72 reg=0.1,
73 stopThr_dykstra=0,
74 numItermax_dykstra=1,
75 warn=False,
76 warn_dykstra=True,
77 )
78
79
80 @pytest.mark.parametrize(("alpha, rank"), ((0.8, 2), (0.5, 3), (0.2, 6), (0.1, -1)))
81 def test_lowrank_gromov_wasserstein_samples_alpha_error(alpha, rank):
82 # Test warning for value of alpha and rank
83 n_samples = 20 # nb samples
84 mu_s = np.array([0, 0])
85 cov_s = np.array([[1, 0], [0, 1]])
86
87 X_s = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=1)
88 X_t = X_s[::-1].copy()
89
90 a = ot.unif(n_samples)
91 b = ot.unif(n_samples)
92
93 with pytest.raises(ValueError):
94 ot.gromov.lowrank_gromov_wasserstein_samples(
95 X_s, X_t, a, b, reg=0.1, rank=rank, alpha=alpha, warn=False
96 )
97
98
99 @pytest.mark.parametrize(("gamma_init"), ("rescale", "theory", "other"))
100 def test_lowrank_wasserstein_samples_gamma_init(gamma_init):
101 # Test lr sinkhorn with different init strategies
102 n_samples = 20 # nb samples
103 mu_s = np.array([0, 0])
104 cov_s = np.array([[1, 0], [0, 1]])
105
106 X_s = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=1)
107 X_t = X_s[::-1].copy()
108
109 a = ot.unif(n_samples)
110 b = ot.unif(n_samples)
111
112 if gamma_init not in ["rescale", "theory"]:
113 with pytest.raises(NotImplementedError):
114 ot.gromov.lowrank_gromov_wasserstein_samples(
115 X_s, X_t, a, b, reg=0.1, gamma_init=gamma_init, log=True
116 )
117
118 else:
119 Q, R, g, log = ot.gromov.lowrank_gromov_wasserstein_samples(
120 X_s, X_t, a, b, reg=0.1, gamma_init=gamma_init, log=True
121 )
122 P = log["lazy_plan"][:]
123
124 # check constraints for P
125 np.testing.assert_allclose(a, P.sum(1), atol=1e-04)
126 np.testing.assert_allclose(b, P.sum(0), atol=1e-04)
127
128
129 @pytest.skip_backend("tf")
130 def test_lowrank_gromov_wasserstein_samples_backends(nx):
131 # Test low rank sinkhorn for different backends
132 n_samples = 20 # nb samples
133 mu_s = np.array([0, 0])
134 cov_s = np.array([[1, 0], [0, 1]])
135
136 X_s = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=1)
137 X_t = X_s[::-1].copy()
138
139 a = ot.unif(n_samples)
140 b = ot.unif(n_samples)
141
142 ab, bb, X_sb, X_tb = nx.from_numpy(a, b, X_s, X_t)
143
144 Q, R, g, log = ot.gromov.lowrank_gromov_wasserstein_samples(
145 X_sb, X_tb, ab, bb, reg=0.1, log=True
146 )
147 lazy_plan = log["lazy_plan"]
148 P = lazy_plan[:]
149
150 np.testing.assert_allclose(ab, P.sum(1), atol=1e-04)
151 np.testing.assert_allclose(bb, P.sum(0), atol=1e-04)