Debian Codegraph @

test/test_coot.py

raw symbols SCIP
1 """Tests for module COOT on OT"""
2
3 # Author: Quang Huy Tran <[email protected]>
4 #
5 # License: MIT License
6
7 import numpy as np
8 import ot
9 from ot.coot import co_optimal_transport as coot
10 from ot.coot import co_optimal_transport2 as coot2
11 import pytest
12
13
14 @pytest.mark.parametrize("verbose", [False, True, 1, 0])
15 def test_coot(nx, verbose):
16 n_samples = 60 # nb samples
17
18 mu_s = np.array([0, 0])
19 cov_s = np.array([[1, 0], [0, 1]])
20
21 xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=4)
22 xt = xs[::-1].copy()
23 xs_nx = nx.from_numpy(xs)
24 xt_nx = nx.from_numpy(xt)
25
26 # test couplings
27 pi_sample, pi_feature = coot(X=xs, Y=xt, verbose=verbose)
28 pi_sample_nx, pi_feature_nx = coot(X=xs_nx, Y=xt_nx, verbose=verbose)
29 pi_sample_nx = nx.to_numpy(pi_sample_nx)
30 pi_feature_nx = nx.to_numpy(pi_feature_nx)
31
32 anti_id_sample = np.flipud(np.eye(n_samples, n_samples)) / n_samples
33 id_feature = np.eye(2, 2) / 2
34
35 np.testing.assert_allclose(pi_sample, anti_id_sample, atol=1e-04)
36 np.testing.assert_allclose(pi_sample_nx, anti_id_sample, atol=1e-04)
37 np.testing.assert_allclose(pi_feature, id_feature, atol=1e-04)
38 np.testing.assert_allclose(pi_feature_nx, id_feature, atol=1e-04)
39
40 # test marginal distributions
41 px_s, px_f = ot.unif(n_samples), ot.unif(2)
42 py_s, py_f = ot.unif(n_samples), ot.unif(2)
43
44 np.testing.assert_allclose(px_s, pi_sample_nx.sum(0), atol=1e-04)
45 np.testing.assert_allclose(py_s, pi_sample_nx.sum(1), atol=1e-04)
46 np.testing.assert_allclose(px_f, pi_feature_nx.sum(0), atol=1e-04)
47 np.testing.assert_allclose(py_f, pi_feature_nx.sum(1), atol=1e-04)
48
49 np.testing.assert_allclose(px_s, pi_sample.sum(0), atol=1e-04)
50 np.testing.assert_allclose(py_s, pi_sample.sum(1), atol=1e-04)
51 np.testing.assert_allclose(px_f, pi_feature.sum(0), atol=1e-04)
52 np.testing.assert_allclose(py_f, pi_feature.sum(1), atol=1e-04)
53
54 # test COOT distance
55
56 coot_np = coot2(X=xs, Y=xt, verbose=verbose)
57 coot_nx = nx.to_numpy(coot2(X=xs_nx, Y=xt_nx, verbose=verbose))
58 np.testing.assert_allclose(coot_np, 0, atol=1e-08)
59 np.testing.assert_allclose(coot_nx, 0, atol=1e-08)
60
61
62 def test_entropic_coot(nx):
63 n_samples = 60 # nb samples
64
65 mu_s = np.array([0, 0])
66 cov_s = np.array([[1, 0], [0, 1]])
67
68 xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=4)
69 xt = xs[::-1].copy()
70 xs_nx = nx.from_numpy(xs)
71 xt_nx = nx.from_numpy(xt)
72
73 epsilon = (1, 1e-1)
74 nits_ot = 2000
75
76 # test couplings
77 pi_sample, pi_feature = coot(X=xs, Y=xt, epsilon=epsilon, nits_ot=nits_ot)
78 pi_sample_nx, pi_feature_nx = coot(
79 X=xs_nx, Y=xt_nx, epsilon=epsilon, nits_ot=nits_ot
80 )
81 pi_sample_nx = nx.to_numpy(pi_sample_nx)
82 pi_feature_nx = nx.to_numpy(pi_feature_nx)
83
84 np.testing.assert_allclose(pi_sample, pi_sample_nx, atol=1e-04)
85 np.testing.assert_allclose(pi_feature, pi_feature_nx, atol=1e-04)
86
87 # test marginal distributions
88 px_s, px_f = ot.unif(n_samples), ot.unif(2)
89 py_s, py_f = ot.unif(n_samples), ot.unif(2)
90
91 np.testing.assert_allclose(px_s, pi_sample_nx.sum(0), atol=1e-04)
92 np.testing.assert_allclose(py_s, pi_sample_nx.sum(1), atol=1e-04)
93 np.testing.assert_allclose(px_f, pi_feature_nx.sum(0), atol=1e-04)
94 np.testing.assert_allclose(py_f, pi_feature_nx.sum(1), atol=1e-04)
95
96 np.testing.assert_allclose(px_s, pi_sample.sum(0), atol=1e-04)
97 np.testing.assert_allclose(py_s, pi_sample.sum(1), atol=1e-04)
98 np.testing.assert_allclose(px_f, pi_feature.sum(0), atol=1e-04)
99 np.testing.assert_allclose(py_f, pi_feature.sum(1), atol=1e-04)
100
101 # test entropic COOT distance
102
103 coot_np = coot2(X=xs, Y=xt, epsilon=epsilon, nits_ot=nits_ot)
104 coot_nx = nx.to_numpy(coot2(X=xs_nx, Y=xt_nx, epsilon=epsilon, nits_ot=nits_ot))
105
106 np.testing.assert_allclose(coot_np, coot_nx, atol=1e-08)
107
108
109 def test_coot_with_linear_terms(nx):
110 n_samples = 60 # nb samples
111
112 mu_s = np.array([0, 0])
113 cov_s = np.array([[1, 0], [0, 1]])
114
115 xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=4)
116 xt = xs[::-1].copy()
117 xs_nx = nx.from_numpy(xs)
118 xt_nx = nx.from_numpy(xt)
119
120 M_samp = np.ones((n_samples, n_samples))
121 np.fill_diagonal(np.fliplr(M_samp), 0)
122 M_feat = np.ones((2, 2))
123 np.fill_diagonal(M_feat, 0)
124 M_samp_nx, M_feat_nx = nx.from_numpy(M_samp), nx.from_numpy(M_feat)
125
126 alpha = (1, 2)
127
128 # test couplings
129 anti_id_sample = np.flipud(np.eye(n_samples, n_samples)) / n_samples
130 id_feature = np.eye(2, 2) / 2
131
132 pi_sample, pi_feature = coot(X=xs, Y=xt, alpha=alpha, M_samp=M_samp, M_feat=M_feat)
133 pi_sample_nx, pi_feature_nx = coot(
134 X=xs_nx, Y=xt_nx, alpha=alpha, M_samp=M_samp_nx, M_feat=M_feat_nx
135 )
136 pi_sample_nx = nx.to_numpy(pi_sample_nx)
137 pi_feature_nx = nx.to_numpy(pi_feature_nx)
138
139 np.testing.assert_allclose(pi_sample, anti_id_sample, atol=1e-04)
140 np.testing.assert_allclose(pi_sample_nx, anti_id_sample, atol=1e-04)
141 np.testing.assert_allclose(pi_feature, id_feature, atol=1e-04)
142 np.testing.assert_allclose(pi_feature_nx, id_feature, atol=1e-04)
143
144 # test marginal distributions
145 px_s, px_f = ot.unif(n_samples), ot.unif(2)
146 py_s, py_f = ot.unif(n_samples), ot.unif(2)
147
148 np.testing.assert_allclose(px_s, pi_sample_nx.sum(0), atol=1e-04)
149 np.testing.assert_allclose(py_s, pi_sample_nx.sum(1), atol=1e-04)
150 np.testing.assert_allclose(px_f, pi_feature_nx.sum(0), atol=1e-04)
151 np.testing.assert_allclose(py_f, pi_feature_nx.sum(1), atol=1e-04)
152
153 np.testing.assert_allclose(px_s, pi_sample.sum(0), atol=1e-04)
154 np.testing.assert_allclose(py_s, pi_sample.sum(1), atol=1e-04)
155 np.testing.assert_allclose(px_f, pi_feature.sum(0), atol=1e-04)
156 np.testing.assert_allclose(py_f, pi_feature.sum(1), atol=1e-04)
157
158 # test COOT distance
159
160 coot_np = coot2(X=xs, Y=xt, alpha=alpha, M_samp=M_samp, M_feat=M_feat)
161 coot_nx = nx.to_numpy(
162 coot2(X=xs_nx, Y=xt_nx, alpha=alpha, M_samp=M_samp_nx, M_feat=M_feat_nx)
163 )
164 np.testing.assert_allclose(coot_np, 0, atol=1e-08)
165 np.testing.assert_allclose(coot_nx, 0, atol=1e-08)
166
167
168 def test_coot_raise_value_error(nx):
169 n_samples = 80 # nb samples
170
171 mu_s = np.array([2, 4])
172 cov_s = np.array([[1, 0], [0, 1]])
173
174 xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=43)
175 xt = xs[::-1].copy()
176 xs_nx = nx.from_numpy(xs)
177 xt_nx = nx.from_numpy(xt)
178
179 # raise value error of method sinkhorn
180 def coot_sh(method_sinkhorn):
181 return coot(X=xs, Y=xt, method_sinkhorn=method_sinkhorn)
182
183 def coot_sh_nx(method_sinkhorn):
184 return coot(X=xs_nx, Y=xt_nx, method_sinkhorn=method_sinkhorn)
185
186 np.testing.assert_raises(ValueError, coot_sh, "not_sinkhorn")
187 np.testing.assert_raises(ValueError, coot_sh_nx, "not_sinkhorn")
188
189 # raise value error for epsilon
190 def coot_eps(epsilon):
191 return coot(X=xs, Y=xt, epsilon=epsilon)
192
193 def coot_eps_nx(epsilon):
194 return coot(X=xs_nx, Y=xt_nx, epsilon=epsilon)
195
196 np.testing.assert_raises(ValueError, coot_eps, (1, 2, 3))
197 np.testing.assert_raises(ValueError, coot_eps_nx, [1, 2, 3, 4])
198
199 # raise value error for alpha
200 def coot_alpha(alpha):
201 return coot(X=xs, Y=xt, alpha=alpha)
202
203 def coot_alpha_nx(alpha):
204 return coot(X=xs_nx, Y=xt_nx, alpha=alpha)
205
206 np.testing.assert_raises(ValueError, coot_alpha, [1])
207 np.testing.assert_raises(ValueError, coot_alpha_nx, np.arange(4))
208
209
210 def test_coot_warmstart(nx):
211 n_samples = 80 # nb samples
212
213 mu_s = np.array([2, 3])
214 cov_s = np.array([[1, 0], [0, 1]])
215
216 xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=125)
217 xt = xs[::-1].copy()
218 xs_nx = nx.from_numpy(xs)
219 xt_nx = nx.from_numpy(xt)
220
221 # initialize warmstart
222 rng = np.random.RandomState(42)
223 init_pi_sample = rng.rand(n_samples, n_samples)
224 init_pi_sample = init_pi_sample / np.sum(init_pi_sample)
225 init_pi_sample_nx = nx.from_numpy(init_pi_sample)
226
227 init_pi_feature = rng.rand(2, 2)
228 init_pi_feature /= init_pi_feature / np.sum(init_pi_feature)
229 init_pi_feature_nx = nx.from_numpy(init_pi_feature)
230
231 init_duals_sample = (rng.random(n_samples) * 2 - 1, rng.random(n_samples) * 2 - 1)
232 init_duals_sample_nx = (
233 nx.from_numpy(init_duals_sample[0]),
234 nx.from_numpy(init_duals_sample[1]),
235 )
236
237 init_duals_feature = (rng.random(2) * 2 - 1, rng.random(2) * 2 - 1)
238 init_duals_feature_nx = (
239 nx.from_numpy(init_duals_feature[0]),
240 nx.from_numpy(init_duals_feature[1]),
241 )
242
243 warmstart = {
244 "pi_sample": init_pi_sample,
245 "pi_feature": init_pi_feature,
246 "duals_sample": init_duals_sample,
247 "duals_feature": init_duals_feature,
248 }
249
250 warmstart_nx = {
251 "pi_sample": init_pi_sample_nx,
252 "pi_feature": init_pi_feature_nx,
253 "duals_sample": init_duals_sample_nx,
254 "duals_feature": init_duals_feature_nx,
255 }
256
257 # test couplings
258 pi_sample, pi_feature = coot(X=xs, Y=xt, warmstart=warmstart)
259 pi_sample_nx, pi_feature_nx = coot(X=xs_nx, Y=xt_nx, warmstart=warmstart_nx)
260 pi_sample_nx = nx.to_numpy(pi_sample_nx)
261 pi_feature_nx = nx.to_numpy(pi_feature_nx)
262
263 anti_id_sample = np.flipud(np.eye(n_samples, n_samples)) / n_samples
264 id_feature = np.eye(2, 2) / 2
265
266 np.testing.assert_allclose(pi_sample, anti_id_sample, atol=1e-04)
267 np.testing.assert_allclose(pi_sample_nx, anti_id_sample, atol=1e-04)
268 np.testing.assert_allclose(pi_feature, id_feature, atol=1e-04)
269 np.testing.assert_allclose(pi_feature_nx, id_feature, atol=1e-04)
270
271 # test marginal distributions
272 px_s, px_f = ot.unif(n_samples), ot.unif(2)
273 py_s, py_f = ot.unif(n_samples), ot.unif(2)
274
275 np.testing.assert_allclose(px_s, pi_sample_nx.sum(0), atol=1e-04)
276 np.testing.assert_allclose(py_s, pi_sample_nx.sum(1), atol=1e-04)
277 np.testing.assert_allclose(px_f, pi_feature_nx.sum(0), atol=1e-04)
278 np.testing.assert_allclose(py_f, pi_feature_nx.sum(1), atol=1e-04)
279
280 np.testing.assert_allclose(px_s, pi_sample.sum(0), atol=1e-04)
281 np.testing.assert_allclose(py_s, pi_sample.sum(1), atol=1e-04)
282 np.testing.assert_allclose(px_f, pi_feature.sum(0), atol=1e-04)
283 np.testing.assert_allclose(py_f, pi_feature.sum(1), atol=1e-04)
284
285 # test COOT distance
286 coot_np = coot2(X=xs, Y=xt, warmstart=warmstart)
287 coot_nx = nx.to_numpy(coot2(X=xs_nx, Y=xt_nx, warmstart=warmstart_nx))
288 np.testing.assert_allclose(coot_np, 0, atol=1e-08)
289 np.testing.assert_allclose(coot_nx, 0, atol=1e-08)
290
291
292 def test_coot_log(nx):
293 n_samples = 90 # nb samples
294
295 mu_s = np.array([-2, 0])
296 cov_s = np.array([[1, 0], [0, 1]])
297
298 xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=43)
299 xt = xs[::-1].copy()
300 xs_nx = nx.from_numpy(xs)
301 xt_nx = nx.from_numpy(xt)
302
303 pi_sample, pi_feature, log = coot(X=xs, Y=xt, log=True)
304 pi_sample_nx, pi_feature_nx, log_nx = coot(X=xs_nx, Y=xt_nx, log=True)
305
306 duals_sample, duals_feature = log["duals_sample"], log["duals_feature"]
307 assert len(duals_sample) == 2
308 assert len(duals_feature) == 2
309 assert len(duals_sample[0]) == n_samples
310 assert len(duals_sample[1]) == n_samples
311 assert len(duals_feature[0]) == 2
312 assert len(duals_feature[1]) == 2
313
314 duals_sample_nx = log_nx["duals_sample"]
315 assert len(duals_sample_nx) == 2
316 assert len(duals_sample_nx[0]) == n_samples
317 assert len(duals_sample_nx[1]) == n_samples
318
319 duals_feature_nx = log_nx["duals_feature"]
320 assert len(duals_feature_nx) == 2
321 assert len(duals_feature_nx[0]) == 2
322 assert len(duals_feature_nx[1]) == 2
323
324 list_coot = log["distances"]
325 assert len(list_coot) >= 1
326
327 list_coot_nx = log_nx["distances"]
328 assert len(list_coot_nx) >= 1
329
330 # test with coot distance
331 coot_np, log = coot2(X=xs, Y=xt, log=True)
332 coot_nx, log_nx = coot2(X=xs_nx, Y=xt_nx, log=True)
333
334 duals_sample, duals_feature = log["duals_sample"], log["duals_feature"]
335 assert len(duals_sample) == 2
336 assert len(duals_feature) == 2
337 assert len(duals_sample[0]) == n_samples
338 assert len(duals_sample[1]) == n_samples
339 assert len(duals_feature[0]) == 2
340 assert len(duals_feature[1]) == 2
341
342 duals_sample_nx = log_nx["duals_sample"]
343 assert len(duals_sample_nx) == 2
344 assert len(duals_sample_nx[0]) == n_samples
345 assert len(duals_sample_nx[1]) == n_samples
346
347 duals_feature_nx = log_nx["duals_feature"]
348 assert len(duals_feature_nx) == 2
349 assert len(duals_feature_nx[0]) == 2
350 assert len(duals_feature_nx[1]) == 2
351
352 list_coot = log["distances"]
353 assert len(list_coot) >= 1
354
355 list_coot_nx = log_nx["distances"]
356 assert len(list_coot_nx) >= 1