Debian Codegraph @

test/test_weak.py

raw symbols SCIP
1 """Tests for main module ot.weak"""
2
3 # Author: Remi Flamary <[email protected]>
4 #
5 # License: MIT License
6
7 import ot
8 import numpy as np
9
10
11 def test_weak_ot():
12 # test weak ot solver and identity stationary point
13 n = 50
14 rng = np.random.RandomState(0)
15
16 xs = rng.randn(n, 2)
17 xt = rng.randn(n, 2)
18 u = ot.utils.unif(n)
19
20 G, log = ot.weak_optimal_transport(xs, xt, u, u, log=True)
21
22 # check constraints
23 np.testing.assert_allclose(u, G.sum(1))
24 np.testing.assert_allclose(u, G.sum(0))
25
26 # chaeck that identity is recovered
27 G = ot.weak_optimal_transport(xs, xs, G0=np.eye(n) / n)
28
29 # check G is identity
30 np.testing.assert_allclose(G, np.eye(n) / n)
31
32 # check constraints
33 np.testing.assert_allclose(u, G.sum(1))
34 np.testing.assert_allclose(u, G.sum(0))
35
36
37 def test_weak_ot_bakends(nx):
38 # test weak ot solver for different backends
39 n = 50
40 rng = np.random.RandomState(0)
41
42 xs = rng.randn(n, 2)
43 xt = rng.randn(n, 2)
44 u = ot.utils.unif(n)
45
46 G = ot.weak_optimal_transport(xs, xt, u, u)
47
48 xs2, xt2, u2 = nx.from_numpy(xs, xt, u)
49
50 G2 = ot.weak_optimal_transport(xs2, xt2, u2, u2)
51
52 np.testing.assert_allclose(nx.to_numpy(G2), G)