| 1 |
"""Tests for gromov._utils.py""" |
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
import itertools |
| 8 |
import numpy as np |
| 9 |
import pytest |
| 10 |
|
| 11 |
import ot |
| 12 |
from ot.gromov._utils import networkx_import, sklearn_import |
| 13 |
|
| 14 |
|
| 15 |
def test_update_barycenter(nx): |
| 16 |
ns = 5 |
| 17 |
nt = 10 |
| 18 |
|
| 19 |
Xs, ys = ot.datasets.make_data_classif("3gauss", ns, random_state=42) |
| 20 |
Xt, yt = ot.datasets.make_data_classif("3gauss2", nt, random_state=42) |
| 21 |
|
| 22 |
rng = np.random.RandomState(42) |
| 23 |
ys = rng.randn(Xs.shape[0], 2) |
| 24 |
yt = rng.randn(Xt.shape[0], 2) |
| 25 |
|
| 26 |
C1 = ot.dist(Xs) |
| 27 |
C2 = ot.dist(Xt) |
| 28 |
C1 /= C1.max() |
| 29 |
C2 /= C2.max() |
| 30 |
|
| 31 |
p1, p2 = ot.unif(ns), ot.unif(nt) |
| 32 |
n_samples = 3 |
| 33 |
|
| 34 |
ysb, ytb, C1b, C2b, p1b, p2b = nx.from_numpy(ys, yt, C1, C2, p1, p2) |
| 35 |
|
| 36 |
lambdas = [0.5, 0.5] |
| 37 |
Csb = [C1b, C2b] |
| 38 |
Ysb = [ysb, ytb] |
| 39 |
|
| 40 |
Tb = [nx.ones((m, n_samples), type_as=C1b) / (m * n_samples) for m in [ns, nt]] |
| 41 |
pb = nx.concatenate([nx.sum(elem, 0)[None, :] for elem in Tb], axis=0) |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
Cb = ot.gromov.update_barycenter_structure( |
| 46 |
[elem.T for elem in Tb], Csb, lambdas, pb, target=False |
| 47 |
) |
| 48 |
Xb = ot.gromov.update_barycenter_feature( |
| 49 |
[elem.T for elem in Tb], Ysb, lambdas, pb, target=False |
| 50 |
) |
| 51 |
|
| 52 |
Cbt = ot.gromov.update_barycenter_structure( |
| 53 |
Tb, Csb, lambdas, None, target=True, check_zeros=False |
| 54 |
) |
| 55 |
Xbt = ot.gromov.update_barycenter_feature( |
| 56 |
Tb, Ysb, lambdas, None, target=True, check_zeros=False |
| 57 |
) |
| 58 |
|
| 59 |
np.testing.assert_allclose(Cb, Cbt) |
| 60 |
np.testing.assert_allclose(Xb, Xbt) |
| 61 |
|
| 62 |
|
| 63 |
with pytest.raises(ValueError): |
| 64 |
Cbt = ot.gromov.update_barycenter_structure( |
| 65 |
Tb, Csb, lambdas, None, loss_fun="unknown", target=True |
| 66 |
) |
| 67 |
with pytest.raises(ValueError): |
| 68 |
Xbt = ot.gromov.update_barycenter_feature( |
| 69 |
Tb, Ysb, lambdas, None, loss_fun="unknown", target=True |
| 70 |
) |
| 71 |
|
| 72 |
|
| 73 |
def test_semirelaxed_init_plan(nx): |
| 74 |
ns = 5 |
| 75 |
nt = 10 |
| 76 |
|
| 77 |
Xs, ys = ot.datasets.make_data_classif("3gauss", ns, random_state=42) |
| 78 |
Xt, yt = ot.datasets.make_data_classif("3gauss2", nt, random_state=42) |
| 79 |
|
| 80 |
rng = np.random.RandomState(42) |
| 81 |
ys = rng.randn(Xs.shape[0], 2) |
| 82 |
yt = rng.randn(Xt.shape[0], 2) |
| 83 |
|
| 84 |
C1 = ot.dist(Xs) |
| 85 |
C2 = ot.dist(Xt) |
| 86 |
C1 /= C1.max() |
| 87 |
C2 /= C2.max() |
| 88 |
|
| 89 |
p1, p2 = ot.unif(ns), ot.unif(nt) |
| 90 |
|
| 91 |
ysb, ytb, C1b, C2b, p1b, p2b = nx.from_numpy(ys, yt, C1, C2, p1, p2) |
| 92 |
|
| 93 |
|
| 94 |
with pytest.raises(ValueError): |
| 95 |
_ = ot.gromov.semirelaxed_init_plan(C1b, C2b, p1b, method="unknown") |
| 96 |
|
| 97 |
if sklearn_import: |
| 98 |
|
| 99 |
for method in ["kmeans", "spectral"]: |
| 100 |
T = ot.gromov.semirelaxed_init_plan(C1b, C2b, p1b, method=method) |
| 101 |
Tb = ot.gromov.semirelaxed_init_plan(C1b, C2b, p1b, method=method) |
| 102 |
np.testing.assert_allclose(T, Tb) |
| 103 |
|
| 104 |
|
| 105 |
T = ot.gromov.semirelaxed_init_plan(C1b, C1b, p1b, method=method) |
| 106 |
Tb = ot.gromov.semirelaxed_init_plan(C1b, C1b, p1b, method=method) |
| 107 |
np.testing.assert_allclose(T, Tb) |
| 108 |
|
| 109 |
if networkx_import: |
| 110 |
|
| 111 |
T = ot.gromov.semirelaxed_init_plan(C1b, C2b, p1b, method="fluid") |
| 112 |
Tb = ot.gromov.semirelaxed_init_plan(C1b, C2b, p1b, method="fluid") |
| 113 |
np.testing.assert_allclose(T, Tb) |
| 114 |
|
| 115 |
|
| 116 |
T = ot.gromov.semirelaxed_init_plan(C1b, C1b, p1b, method="fluid") |
| 117 |
Tb = ot.gromov.semirelaxed_init_plan(C1b, C1b, p1b, method="fluid") |
| 118 |
np.testing.assert_allclose(T, Tb) |
| 119 |
|
| 120 |
|
| 121 |
@pytest.mark.parametrize("divergence", ["kl", "l2"]) |
| 122 |
def test_div_between_product(nx, divergence): |
| 123 |
ns = 5 |
| 124 |
nt = 10 |
| 125 |
|
| 126 |
ps, pt = ot.unif(ns), ot.unif(nt) |
| 127 |
ps, pt = nx.from_numpy(ps), nx.from_numpy(pt) |
| 128 |
ps1, pt1 = 2 * ps, 2 * pt |
| 129 |
|
| 130 |
res_nx = ot.gromov.div_between_product(ps, pt, ps1, pt1, divergence, nx=nx) |
| 131 |
res = ot.gromov.div_between_product(ps, pt, ps1, pt1, divergence, nx=None) |
| 132 |
|
| 133 |
np.testing.assert_allclose(res_nx, res, atol=1e-06) |
| 134 |
|
| 135 |
|
| 136 |
@pytest.mark.parametrize( |
| 137 |
"divergence, mass", itertools.product(["kl", "l2"], [True, False]) |
| 138 |
) |
| 139 |
def test_div_to_product(nx, divergence, mass): |
| 140 |
ns = 5 |
| 141 |
nt = 10 |
| 142 |
|
| 143 |
a, b = ot.unif(ns), ot.unif(nt) |
| 144 |
a, b = nx.from_numpy(a), nx.from_numpy(b) |
| 145 |
|
| 146 |
pi = 2 * a[:, None] * b[None, :] |
| 147 |
pi1, pi2 = nx.sum(pi, 1), nx.sum(pi, 0) |
| 148 |
|
| 149 |
res = ot.gromov.div_to_product( |
| 150 |
pi, a, b, pi1=None, pi2=None, divergence=divergence, mass=mass, nx=None |
| 151 |
) |
| 152 |
res1 = ot.gromov.div_to_product( |
| 153 |
pi, a, b, pi1=None, pi2=None, divergence=divergence, mass=mass, nx=nx |
| 154 |
) |
| 155 |
res2 = ot.gromov.div_to_product( |
| 156 |
pi, a, b, pi1=pi1, pi2=pi2, divergence=divergence, mass=mass, nx=None |
| 157 |
) |
| 158 |
res3 = ot.gromov.div_to_product( |
| 159 |
pi, a, b, pi1=pi1, pi2=pi2, divergence=divergence, mass=mass, nx=nx |
| 160 |
) |
| 161 |
|
| 162 |
np.testing.assert_allclose(res1, res, atol=1e-06) |
| 163 |
np.testing.assert_allclose(res2, res, atol=1e-06) |
| 164 |
np.testing.assert_allclose(res3, res, atol=1e-06) |