Debian Codegraph @

test/gromov/test_quantized.py

raw symbols SCIP
1 """Tests for gromov._quantized.py"""
2
3 # Author: Cédric Vincent-Cuaz <[email protected]>
4 #
5 # License: MIT License
6
7 import numpy as np
8 import pytest
9
10 import ot
11
12 from ot.gromov._quantized import networkx_import, sklearn_import
13
14
15 def test_quantized_gw(nx):
16 n_samples = 30 # nb samples
17
18 rng = np.random.RandomState(0)
19 C1 = rng.uniform(low=0.0, high=10, size=(n_samples, n_samples))
20 C1 = (C1 + C1.T) / 2.0
21
22 C2 = rng.uniform(low=10.0, high=20.0, size=(n_samples, n_samples))
23 C2 = (C2 + C2.T) / 2.0
24
25 p = ot.unif(n_samples)
26 q = ot.unif(n_samples)
27
28 npart2 = 3
29
30 C1b, C2b, pb, qb = nx.from_numpy(C1, C2, p, q)
31
32 for npart1 in [1, n_samples + 1, 2]:
33 log_tests = [True, False, False, True, True, False]
34
35 pairs_part_rep = [("random", "random")]
36 if networkx_import:
37 pairs_part_rep += [("louvain", "random"), ("fluid", "pagerank")]
38 if sklearn_import:
39 pairs_part_rep += [("spectral", "random")]
40
41 count_mode = 0
42
43 for part_method, rep_method in pairs_part_rep:
44 log_ = log_tests[count_mode]
45 count_mode += 1
46
47 res = ot.gromov.quantized_fused_gromov_wasserstein(
48 C1,
49 C2,
50 npart1,
51 npart2,
52 p,
53 None,
54 C1,
55 None,
56 part_method=part_method,
57 rep_method=rep_method,
58 log=log_,
59 )
60
61 resb = ot.gromov.quantized_fused_gromov_wasserstein(
62 C1b,
63 C2b,
64 npart1,
65 npart2,
66 None,
67 qb,
68 None,
69 C2b,
70 part_method=part_method,
71 rep_method=rep_method,
72 log=log_,
73 )
74
75 if log_:
76 T_global, Ts_local, T, log = res
77 T_globalb, Ts_localb, Tb, logb = resb
78 else:
79 T_global, Ts_local, T = res
80 T_globalb, Ts_localb, Tb = resb
81
82 Tb = nx.to_numpy(Tb)
83 # check constraints
84 np.testing.assert_allclose(T, Tb, atol=1e-06)
85 np.testing.assert_allclose(
86 p, Tb.sum(1), atol=1e-06
87 ) # cf convergence gromov
88 np.testing.assert_allclose(
89 q, Tb.sum(0), atol=1e-06
90 ) # cf convergence gromov
91
92 if log_:
93 for key in log.keys():
94 # The inner test T_global[i, j] != 0. can lead to different
95 # computation of 1D OT computations between partition depending
96 # on the different float errors across backend
97 if key in logb.keys():
98 np.testing.assert_allclose(log[key], logb[key], atol=1e-06)
99
100
101 def test_quantized_fgw(nx):
102 n_samples = 30 # nb samples
103
104 rng = np.random.RandomState(0)
105 C1 = rng.uniform(low=0.0, high=10, size=(n_samples, n_samples))
106 C1 = (C1 + C1.T) / 2.0
107
108 F1 = rng.uniform(low=0.0, high=10, size=(n_samples, 1))
109
110 C2 = rng.uniform(low=10.0, high=20.0, size=(n_samples, n_samples))
111 C2 = (C2 + C2.T) / 2.0
112
113 F2 = rng.uniform(low=0.0, high=10, size=(n_samples, 1))
114
115 p = ot.unif(n_samples)
116 q = ot.unif(n_samples)
117
118 npart1 = 2
119 npart2 = 3
120
121 C1b, C2b, F1b, F2b, pb, qb = nx.from_numpy(C1, C2, F1, F2, p, q)
122
123 log_tests = [True, False, False, True, True, False]
124
125 pairs_part_rep = []
126 if networkx_import:
127 pairs_part_rep += [
128 ("louvain_fused", "pagerank"),
129 ("louvain", "pagerank_fused"),
130 ("fluid_fused", "pagerank_fused"),
131 ]
132 if sklearn_import:
133 pairs_part_rep += [("spectral_fused", "random")]
134
135 pairs_part_rep += [("random", "random")]
136 count_mode = 0
137
138 alpha = 0.5
139
140 for part_method, rep_method in pairs_part_rep:
141 log_ = log_tests[count_mode]
142 count_mode += 1
143
144 res = ot.gromov.quantized_fused_gromov_wasserstein(
145 C1,
146 C2,
147 npart1,
148 npart2,
149 p,
150 None,
151 C1,
152 None,
153 F1,
154 F2,
155 alpha,
156 part_method,
157 rep_method,
158 log_,
159 )
160
161 resb = ot.gromov.quantized_fused_gromov_wasserstein(
162 C1b,
163 C2b,
164 npart1,
165 npart2,
166 None,
167 qb,
168 None,
169 C2b,
170 F1b,
171 F2b,
172 alpha,
173 part_method,
174 rep_method,
175 log_,
176 )
177
178 if log_:
179 T_global, Ts_local, T, log = res
180 T_globalb, Ts_localb, Tb, logb = resb
181 else:
182 T_global, Ts_local, T = res
183 T_globalb, Ts_localb, Tb = resb
184
185 Tb = nx.to_numpy(Tb)
186 # check constraints
187 np.testing.assert_allclose(T, Tb, atol=1e-06)
188 np.testing.assert_allclose(p, Tb.sum(1), atol=1e-06) # cf convergence gromov
189 np.testing.assert_allclose(q, Tb.sum(0), atol=1e-06) # cf convergence gromov
190
191 if log_:
192 for key in log.keys():
193 # The inner test T_global[i, j] != 0. can lead to different
194 # computation of 1D OT computations between partition depending
195 # on the different float errors across backend
196 if key in logb.keys():
197 np.testing.assert_allclose(log[key], logb[key], atol=1e-06)
198
199 # complementary tests for utils functions
200 DF1b = ot.dist(F1b, F1b)
201 DF2b = ot.dist(F2b, F2b)
202 C1b_new = alpha * C1b + (1 - alpha) * DF1b
203 C2b_new = alpha * C2b + (1 - alpha) * DF2b
204
205 part1b = ot.gromov.get_graph_partition(
206 C1b_new, npart1, part_method=pairs_part_rep[-1][0], random_state=0
207 )
208 part2b = ot.gromov._quantized.get_graph_partition(
209 C2b_new, npart2, part_method=pairs_part_rep[-1][0], random_state=0
210 )
211
212 rep_indices1b = ot.gromov.get_graph_representants(
213 C1b, part1b, rep_method=pairs_part_rep[-1][1], random_state=0
214 )
215 rep_indices2b = ot.gromov.get_graph_representants(
216 C2b, part2b, rep_method=pairs_part_rep[-1][1], random_state=0
217 )
218
219 CR1b, list_R1b, list_p1b, FR1b = ot.gromov.format_partitioned_graph(
220 C1b, pb, part1b, rep_indices1b, F1b, DF1b, alpha
221 )
222 CR2b, list_R2b, list_p2b, FR2b = ot.gromov.format_partitioned_graph(
223 C2b, qb, part2b, rep_indices2b, F2b, DF2b, alpha
224 )
225
226 MRb = ot.dist(FR1b, FR2b)
227
228 T_globalb, Ts_localb, _ = ot.gromov.quantized_fused_gromov_wasserstein_partitioned(
229 CR1b, CR2b, list_R1b, list_R2b, list_p1b, list_p2b, MRb, alpha, build_OT=False
230 )
231
232 T_globalb = nx.to_numpy(T_globalb)
233 np.testing.assert_allclose(T_global, T_globalb, atol=1e-06)
234
235 for key in Ts_localb.keys():
236 T_localb = nx.to_numpy(Ts_localb[key])
237 np.testing.assert_allclose(Ts_local[key], T_localb, atol=1e-06)
238
239 # tests for edge cases of the graph partitioning
240 for method in ["unknown_method", "GW", "FGW"]:
241 with pytest.raises(ValueError):
242 ot.gromov.get_graph_partition(
243 C1b, npart1, part_method=method, random_state=0
244 )
245
246 with pytest.raises(ValueError):
247 ot.gromov.get_graph_partition(
248 C1b, npart1, part_method=method, alpha=0.5, F=None, random_state=0
249 )
250
251 # tests for edge cases of the representant selection
252 with pytest.raises(ValueError):
253 ot.gromov.get_graph_representants(
254 C1b, part1b, rep_method="unknown_method", random_state=0
255 )
256
257 # tests for edge cases of the format_partitioned_graph function
258 with pytest.raises(ValueError):
259 CR1b, list_R1b, list_p1b, FR1b = ot.gromov.format_partitioned_graph(
260 C1b, pb, part1b, rep_indices1b, F1b, None, alpha
261 )
262
263 # Tests in qFGW solvers
264 # for non admissible values of alpha
265 with pytest.raises(ValueError):
266 ot.gromov.quantized_fused_gromov_wasserstein_partitioned(
267 CR1b, CR2b, list_R1b, list_R2b, list_p1b, list_p2b, MRb, 0, build_OT=False
268 )
269
270 # for non-consistent feature information provided
271 with pytest.raises(ValueError):
272 ot.gromov.quantized_fused_gromov_wasserstein(
273 C1,
274 C2,
275 npart1,
276 npart2,
277 p,
278 q,
279 None,
280 None,
281 F1,
282 None,
283 0.5,
284 "spectral_fused",
285 "random",
286 log_,
287 )
288
289
290 @pytest.skip_backend("jax", reason="test very slow with jax backend")
291 def test_quantized_gw_samples(nx):
292 n_samples_1 = 15 # nb samples
293 n_samples_2 = 20 # nb samples
294
295 rng = np.random.RandomState(0)
296 X1 = rng.uniform(low=0.0, high=10, size=(n_samples_1, 2))
297 X2 = rng.uniform(low=0.0, high=10, size=(n_samples_2, 4))
298
299 p = ot.unif(n_samples_1)
300 q = ot.unif(n_samples_2)
301
302 npart1 = 2
303 npart2 = 3
304
305 X1b, X2b, pb, qb = nx.from_numpy(X1, X2, p, q)
306
307 log_tests = [True, False, True]
308 methods = ["random"]
309 if sklearn_import:
310 methods += ["kmeans"]
311
312 count_mode = 0
313 alpha = 1.0
314
315 for method in methods:
316 log_ = log_tests[count_mode]
317 count_mode += 1
318
319 res = ot.gromov.quantized_fused_gromov_wasserstein_samples(
320 X1, X2, npart1, npart2, p, None, None, None, alpha, method, log_
321 )
322
323 resb = ot.gromov.quantized_fused_gromov_wasserstein_samples(
324 X1b, X2b, npart1, npart2, None, qb, None, None, alpha, method, log_
325 )
326
327 if log_:
328 T_global, Ts_local, T, log = res
329 T_globalb, Ts_localb, Tb, logb = resb
330 else:
331 T_global, Ts_local, T = res
332 T_globalb, Ts_localb, Tb = resb
333
334 Tb = nx.to_numpy(Tb)
335 # check constraints
336 np.testing.assert_allclose(T, Tb, atol=1e-06)
337 np.testing.assert_allclose(p, Tb.sum(1), atol=1e-06) # cf convergence gromov
338 np.testing.assert_allclose(q, Tb.sum(0), atol=1e-06) # cf convergence gromov
339
340 if log_:
341 for key in log.keys():
342 # The inner test T_global[i, j] != 0. can lead to different
343 # computation of 1D OT computations between partition depending
344 # on the different float errors across backend
345 if key in logb.keys():
346 np.testing.assert_allclose(log[key], logb[key], atol=1e-06)
347
348 # tests for edge cases of the representant selection
349 with pytest.raises(ValueError):
350 ot.gromov.get_partition_and_representants_samples(
351 X1, npart1, method="unknown_method", random_state=0
352 )
353
354
355 @pytest.skip_backend("jax", reason="test very slow with jax backend")
356 def test_quantized_fgw_samples(nx):
357 n_samples_1 = 20 # nb samples
358 n_samples_2 = 30 # nb samples
359
360 rng = np.random.RandomState(0)
361 X1 = rng.uniform(low=0.0, high=10, size=(n_samples_1, 2))
362 X2 = rng.uniform(low=0.0, high=10, size=(n_samples_2, 4))
363
364 F1 = rng.uniform(low=0.0, high=10, size=(n_samples_1, 3))
365 F2 = rng.uniform(low=0.0, high=10, size=(n_samples_2, 3))
366
367 p = ot.unif(n_samples_1)
368 q = ot.unif(n_samples_2)
369
370 npart1 = 2
371 npart2 = 3
372
373 X1b, X2b, F1b, F2b, pb, qb = nx.from_numpy(X1, X2, F1, F2, p, q)
374
375 methods = []
376 if sklearn_import:
377 methods += ["kmeans", "kmeans_fused"]
378 methods += ["random"]
379
380 alpha = 0.5
381
382 for npart1 in [1, n_samples_1 + 1, 2]:
383 log_tests = [True, False, True]
384 count_mode = 0
385
386 for method in methods:
387 log_ = log_tests[count_mode]
388 count_mode += 1
389
390 res = ot.gromov.quantized_fused_gromov_wasserstein_samples(
391 X1, X2, npart1, npart2, p, None, F1, F2, alpha, method, log_
392 )
393
394 resb = ot.gromov.quantized_fused_gromov_wasserstein_samples(
395 X1b, X2b, npart1, npart2, None, qb, F1b, F2b, alpha, method, log_
396 )
397
398 if log_:
399 T_global, Ts_local, T, log = res
400 T_globalb, Ts_localb, Tb, logb = resb
401 else:
402 T_global, Ts_local, T = res
403 T_globalb, Ts_localb, Tb = resb
404
405 Tb = nx.to_numpy(Tb)
406 # check constraints
407 np.testing.assert_allclose(T, Tb, atol=1e-06)
408 np.testing.assert_allclose(
409 p, Tb.sum(1), atol=1e-06
410 ) # cf convergence gromov
411 np.testing.assert_allclose(
412 q, Tb.sum(0), atol=1e-06
413 ) # cf convergence gromov
414
415 if log_:
416 for key in log.keys():
417 # The inner test T_global[i, j] != 0. can lead to different
418 # computation of 1D OT computations between partition depending
419 # on the different float errors across backend
420 if key in logb.keys():
421 np.testing.assert_allclose(log[key], logb[key], atol=1e-06)
422
423 # complementary tests for utils functions
424 part1b, rep_indices1 = ot.gromov.get_partition_and_representants_samples(
425 X1b, npart1, method=method, random_state=0
426 )
427 part2b, rep_indices2 = ot.gromov.get_partition_and_representants_samples(
428 X2b, npart2, method=method, random_state=0
429 )
430
431 CR1b, list_R1b, list_p1b, FR1b = ot.gromov.format_partitioned_samples(
432 X1b, pb, part1b, rep_indices1, F1b, alpha
433 )
434 CR2b, list_R2b, list_p2b, FR2b = ot.gromov.format_partitioned_samples(
435 X2b, qb, part2b, rep_indices2, F2b, alpha
436 )
437
438 MRb = ot.dist(FR1b, FR2b)
439
440 T_globalb, Ts_localb, _ = ot.gromov.quantized_fused_gromov_wasserstein_partitioned(
441 CR1b, CR2b, list_R1b, list_R2b, list_p1b, list_p2b, MRb, alpha, build_OT=False
442 )
443
444 T_globalb = nx.to_numpy(T_globalb)
445 np.testing.assert_allclose(T_global, T_globalb, atol=1e-06)
446
447 for key in Ts_localb.keys():
448 T_localb = nx.to_numpy(Ts_localb[key])
449 np.testing.assert_allclose(Ts_local[key], T_localb, atol=1e-06)
450
451 # tests for edge cases of the format_partitioned_graph function
452 with pytest.raises(ValueError):
453 CR1b, list_R1b, list_p1b, FR1b = ot.gromov.format_partitioned_samples(
454 X1b, pb, part1b, rep_indices1, None, alpha
455 )
456
457 # for non-consistent feature information provided
458 with pytest.raises(ValueError):
459 ot.gromov.quantized_fused_gromov_wasserstein_samples(
460 X1, X2, npart1, npart2, p, None, None, F2, alpha, "fused_spectral", log_
461 )