| 1 |
"""Tests for module partial""" |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
import numpy as np |
| 9 |
import scipy as sp |
| 10 |
import ot |
| 11 |
from ot.backend import to_numpy, torch |
| 12 |
import pytest |
| 13 |
|
| 14 |
|
| 15 |
def test_raise_errors(): |
| 16 |
n_samples = 20 |
| 17 |
n_noise = 20 |
| 18 |
|
| 19 |
mu = np.array([0, 0]) |
| 20 |
cov = np.array([[1, 0], [0, 2]]) |
| 21 |
|
| 22 |
rng = np.random.RandomState(42) |
| 23 |
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu, cov, random_state=rng) |
| 24 |
xs = np.append(xs, (rng.rand(n_noise, 2) + 1) * 4).reshape((-1, 2)) |
| 25 |
xt = ot.datasets.make_2D_samples_gauss(n_samples, mu, cov, random_state=rng) |
| 26 |
xt = np.append(xt, (rng.rand(n_noise, 2) + 1) * -3).reshape((-1, 2)) |
| 27 |
|
| 28 |
M = ot.dist(xs, xt) |
| 29 |
|
| 30 |
p = ot.unif(n_samples + n_noise) |
| 31 |
q = ot.unif(n_samples + n_noise) |
| 32 |
|
| 33 |
with pytest.raises(ValueError): |
| 34 |
ot.partial.partial_wasserstein_lagrange(p + 1, q, M, 1, log=True) |
| 35 |
|
| 36 |
with pytest.raises(ValueError): |
| 37 |
ot.partial.partial_wasserstein(p, q, M, m=2, log=True) |
| 38 |
|
| 39 |
with pytest.raises(ValueError): |
| 40 |
ot.partial.partial_wasserstein(p, q, M, m=-1, log=True) |
| 41 |
|
| 42 |
with pytest.raises(ValueError): |
| 43 |
ot.partial.entropic_partial_wasserstein(p, q, M, reg=1, m=2, log=True) |
| 44 |
|
| 45 |
with pytest.raises(ValueError): |
| 46 |
ot.partial.entropic_partial_wasserstein(p, q, M, reg=1, m=-1, log=True) |
| 47 |
|
| 48 |
with pytest.raises(ValueError): |
| 49 |
ot.partial.partial_gromov_wasserstein(M, M, p, q, m=2, log=True) |
| 50 |
|
| 51 |
with pytest.raises(ValueError): |
| 52 |
ot.partial.partial_gromov_wasserstein(M, M, p, q, m=-1, log=True) |
| 53 |
|
| 54 |
with pytest.raises(ValueError): |
| 55 |
ot.partial.entropic_partial_gromov_wasserstein(M, M, p, q, reg=1, m=2, log=True) |
| 56 |
|
| 57 |
with pytest.raises(ValueError): |
| 58 |
ot.partial.entropic_partial_gromov_wasserstein( |
| 59 |
M, M, p, q, reg=1, m=-1, log=True |
| 60 |
) |
| 61 |
|
| 62 |
|
| 63 |
def test_partial_wasserstein_lagrange(): |
| 64 |
n_samples = 20 |
| 65 |
n_noise = 20 |
| 66 |
|
| 67 |
mu = np.array([0, 0]) |
| 68 |
cov = np.array([[1, 0], [0, 2]]) |
| 69 |
|
| 70 |
rng = np.random.RandomState(42) |
| 71 |
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu, cov, random_state=rng) |
| 72 |
xs = np.append(xs, (rng.rand(n_noise, 2) + 1) * 4).reshape((-1, 2)) |
| 73 |
xt = ot.datasets.make_2D_samples_gauss(n_samples, mu, cov, random_state=rng) |
| 74 |
xt = np.append(xt, (rng.rand(n_noise, 2) + 1) * -3).reshape((-1, 2)) |
| 75 |
|
| 76 |
M = ot.dist(xs, xt) |
| 77 |
|
| 78 |
p = ot.unif(n_samples + n_noise) |
| 79 |
q = ot.unif(n_samples + n_noise) |
| 80 |
|
| 81 |
w0, log0 = ot.partial.partial_wasserstein_lagrange(p, q, M, 1, log=True) |
| 82 |
|
| 83 |
w0, log0 = ot.partial.partial_wasserstein_lagrange(p, q, M, 100, log=True) |
| 84 |
|
| 85 |
|
| 86 |
def test_partial_wasserstein(nx): |
| 87 |
n_samples = 20 |
| 88 |
n_noise = 20 |
| 89 |
|
| 90 |
mu = np.array([0, 0]) |
| 91 |
cov = np.array([[1, 0], [0, 2]]) |
| 92 |
|
| 93 |
rng = np.random.RandomState(42) |
| 94 |
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu, cov, random_state=rng) |
| 95 |
xs = np.append(xs, (rng.rand(n_noise, 2) + 1) * 4).reshape((-1, 2)) |
| 96 |
xt = ot.datasets.make_2D_samples_gauss(n_samples, mu, cov, random_state=rng) |
| 97 |
xt = np.append(xt, (rng.rand(n_noise, 2) + 1) * -3).reshape((-1, 2)) |
| 98 |
|
| 99 |
M = ot.dist(xs, xt) |
| 100 |
|
| 101 |
p = ot.unif(n_samples + n_noise) |
| 102 |
q = ot.unif(n_samples + n_noise) |
| 103 |
|
| 104 |
m = 0.5 |
| 105 |
|
| 106 |
p, q, M = nx.from_numpy(p, q, M) |
| 107 |
|
| 108 |
w0, log0 = ot.partial.partial_wasserstein(p, q, M, m=m, log=True) |
| 109 |
w, log = ot.partial.entropic_partial_wasserstein( |
| 110 |
p, q, M, reg=1, m=m, log=True, verbose=True |
| 111 |
) |
| 112 |
|
| 113 |
|
| 114 |
np.testing.assert_equal(to_numpy(nx.sum(w0, axis=1) - p) <= 1e-5, [True] * len(p)) |
| 115 |
np.testing.assert_equal(to_numpy(nx.sum(w0, axis=0) - q) <= 1e-5, [True] * len(q)) |
| 116 |
np.testing.assert_equal(to_numpy(nx.sum(w0, axis=1) - p) <= 1e-5, [True] * len(p)) |
| 117 |
np.testing.assert_equal(to_numpy(nx.sum(w0, axis=0) - q) <= 1e-5, [True] * len(q)) |
| 118 |
|
| 119 |
|
| 120 |
np.testing.assert_allclose(np.sum(to_numpy(w0)), m, atol=1e-04) |
| 121 |
np.testing.assert_allclose(np.sum(to_numpy(w)), m, atol=1e-04) |
| 122 |
|
| 123 |
w0, log0 = ot.partial.partial_wasserstein2(p, q, M, m=m, log=True) |
| 124 |
w0_val = ot.partial.partial_wasserstein2(p, q, M, m=m, log=False) |
| 125 |
|
| 126 |
G = log0["T"] |
| 127 |
|
| 128 |
np.testing.assert_allclose(w0, w0_val, atol=1e-1, rtol=1e-1) |
| 129 |
|
| 130 |
|
| 131 |
np.testing.assert_equal(to_numpy(nx.sum(G, axis=1) - p) <= 1e-5, [True] * len(p)) |
| 132 |
np.testing.assert_equal(to_numpy(nx.sum(G, axis=0) - q) <= 1e-5, [True] * len(q)) |
| 133 |
np.testing.assert_allclose(np.sum(to_numpy(G)), m, atol=1e-04) |
| 134 |
|
| 135 |
empty_array = nx.zeros(0, type_as=M) |
| 136 |
w = ot.partial.partial_wasserstein(empty_array, empty_array, M=M, m=None) |
| 137 |
|
| 138 |
|
| 139 |
np.testing.assert_equal(to_numpy(nx.sum(w, axis=1) - p) <= 1e-5, [True] * len(p)) |
| 140 |
np.testing.assert_equal(to_numpy(nx.sum(w, axis=0) - q) <= 1e-5, [True] * len(q)) |
| 141 |
np.testing.assert_equal(to_numpy(nx.sum(w, axis=1) - p) <= 1e-5, [True] * len(p)) |
| 142 |
np.testing.assert_equal(to_numpy(nx.sum(w, axis=0) - q) <= 1e-5, [True] * len(q)) |
| 143 |
|
| 144 |
|
| 145 |
np.testing.assert_allclose(np.sum(to_numpy(w)), 1, atol=1e-04) |
| 146 |
|
| 147 |
w0 = ot.partial.entropic_partial_wasserstein( |
| 148 |
empty_array, empty_array, M=M, reg=10, m=None |
| 149 |
) |
| 150 |
|
| 151 |
|
| 152 |
np.testing.assert_equal(to_numpy(nx.sum(w0, axis=1) - p) <= 1e-5, [True] * len(p)) |
| 153 |
np.testing.assert_equal(to_numpy(nx.sum(w0, axis=0) - q) <= 1e-5, [True] * len(q)) |
| 154 |
np.testing.assert_equal(to_numpy(nx.sum(w0, axis=1) - p) <= 1e-5, [True] * len(p)) |
| 155 |
np.testing.assert_equal(to_numpy(nx.sum(w0, axis=0) - q) <= 1e-5, [True] * len(q)) |
| 156 |
|
| 157 |
|
| 158 |
np.testing.assert_allclose(np.sum(to_numpy(w0)), 1, atol=1e-04) |
| 159 |
|
| 160 |
|
| 161 |
def test_partial_wasserstein2_gradient(): |
| 162 |
if torch: |
| 163 |
n_samples = 40 |
| 164 |
|
| 165 |
mu = np.array([0, 0]) |
| 166 |
cov = np.array([[1, 0], [0, 2]]) |
| 167 |
|
| 168 |
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu, cov) |
| 169 |
xt = ot.datasets.make_2D_samples_gauss(n_samples, mu, cov) |
| 170 |
|
| 171 |
M = torch.tensor(ot.dist(xs, xt), requires_grad=True, dtype=torch.float64) |
| 172 |
|
| 173 |
p = torch.tensor(ot.unif(n_samples), dtype=torch.float64) |
| 174 |
q = torch.tensor(ot.unif(n_samples), dtype=torch.float64) |
| 175 |
|
| 176 |
m = 0.5 |
| 177 |
|
| 178 |
w, log = ot.partial.partial_wasserstein2(p, q, M, m=m, log=True) |
| 179 |
|
| 180 |
w.backward() |
| 181 |
|
| 182 |
assert M.grad is not None |
| 183 |
assert M.grad.shape == M.shape |
| 184 |
|
| 185 |
|
| 186 |
def test_entropic_partial_wasserstein_gradient(): |
| 187 |
if torch: |
| 188 |
n_samples = 40 |
| 189 |
|
| 190 |
mu = np.array([0, 0]) |
| 191 |
cov = np.array([[1, 0], [0, 2]]) |
| 192 |
|
| 193 |
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu, cov) |
| 194 |
xt = ot.datasets.make_2D_samples_gauss(n_samples, mu, cov) |
| 195 |
|
| 196 |
M = torch.tensor(ot.dist(xs, xt), requires_grad=True, dtype=torch.float64) |
| 197 |
|
| 198 |
p = torch.tensor(ot.unif(n_samples), requires_grad=True, dtype=torch.float64) |
| 199 |
q = torch.tensor(ot.unif(n_samples), requires_grad=True, dtype=torch.float64) |
| 200 |
|
| 201 |
m = 0.5 |
| 202 |
reg = 1 |
| 203 |
|
| 204 |
_, log = ot.partial.entropic_partial_wasserstein( |
| 205 |
p, q, M, m=m, reg=reg, log=True |
| 206 |
) |
| 207 |
|
| 208 |
log["partial_w_dist"].backward() |
| 209 |
|
| 210 |
assert M.grad is not None |
| 211 |
assert p.grad is not None |
| 212 |
assert q.grad is not None |
| 213 |
assert M.grad.shape == M.shape |
| 214 |
assert p.grad.shape == p.shape |
| 215 |
assert q.grad.shape == q.shape |
| 216 |
|
| 217 |
|
| 218 |
def test_partial_gromov_wasserstein(): |
| 219 |
rng = np.random.RandomState(42) |
| 220 |
n_samples = 20 |
| 221 |
n_noise = 10 |
| 222 |
|
| 223 |
p = ot.unif(n_samples + n_noise) |
| 224 |
q = ot.unif(n_samples + n_noise) |
| 225 |
|
| 226 |
mu_s = np.array([0, 0]) |
| 227 |
cov_s = np.array([[1, 0], [0, 1]]) |
| 228 |
|
| 229 |
mu_t = np.array([0, 0, 0]) |
| 230 |
cov_t = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) |
| 231 |
|
| 232 |
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=rng) |
| 233 |
xs = np.concatenate((xs, ((rng.rand(n_noise, 2) + 1) * 4)), axis=0) |
| 234 |
P = sp.linalg.sqrtm(cov_t) |
| 235 |
xt = rng.randn(n_samples, 3).dot(P) + mu_t |
| 236 |
xt = np.concatenate((xt, ((rng.rand(n_noise, 3) + 1) * 10)), axis=0) |
| 237 |
xt2 = xs[::-1].copy() |
| 238 |
|
| 239 |
C1 = ot.dist(xs, xs) |
| 240 |
C2 = ot.dist(xt, xt) |
| 241 |
C3 = ot.dist(xt2, xt2) |
| 242 |
|
| 243 |
m = 2 / 3 |
| 244 |
res0, log0 = ot.partial.partial_gromov_wasserstein( |
| 245 |
C1, C3, p, q, m=m, log=True, verbose=True |
| 246 |
) |
| 247 |
np.testing.assert_allclose(res0, 0, atol=1e-1, rtol=1e-1) |
| 248 |
|
| 249 |
C1 = sp.spatial.distance.cdist(xs, xs) |
| 250 |
C2 = sp.spatial.distance.cdist(xt, xt) |
| 251 |
|
| 252 |
m = 1 |
| 253 |
res0, log0 = ot.partial.partial_gromov_wasserstein(C1, C2, p, q, m=m, log=True) |
| 254 |
G = ot.gromov.gromov_wasserstein(C1, C2, p, q, "square_loss") |
| 255 |
np.testing.assert_allclose(G, res0, atol=1e-04) |
| 256 |
|
| 257 |
res, log = ot.partial.entropic_partial_gromov_wasserstein( |
| 258 |
C1, C2, p, q, 10, m=m, log=True |
| 259 |
) |
| 260 |
G = ot.gromov.entropic_gromov_wasserstein(C1, C2, p, q, "square_loss", epsilon=10) |
| 261 |
np.testing.assert_allclose(G, res, atol=1e-02) |
| 262 |
|
| 263 |
w0, log0 = ot.partial.partial_gromov_wasserstein2(C1, C2, p, q, m=m, log=True) |
| 264 |
w0_val = ot.partial.partial_gromov_wasserstein2(C1, C2, p, q, m=m, log=False) |
| 265 |
G = log0["T"] |
| 266 |
np.testing.assert_allclose(w0, w0_val, atol=1e-1, rtol=1e-1) |
| 267 |
|
| 268 |
m = 2 / 3 |
| 269 |
res0, log0 = ot.partial.partial_gromov_wasserstein(C1, C2, p, q, m=m, log=True) |
| 270 |
res, log = ot.partial.entropic_partial_gromov_wasserstein( |
| 271 |
C1, C2, p, q, 100, m=m, log=True |
| 272 |
) |
| 273 |
|
| 274 |
|
| 275 |
np.testing.assert_equal( |
| 276 |
res0.sum(1) <= p, [True] * len(p) |
| 277 |
) |
| 278 |
np.testing.assert_equal( |
| 279 |
res0.sum(0) <= q, [True] * len(q) |
| 280 |
) |
| 281 |
np.testing.assert_allclose(np.sum(res0), m, atol=1e-04) |
| 282 |
|
| 283 |
np.testing.assert_equal( |
| 284 |
res.sum(1) <= p, [True] * len(p) |
| 285 |
) |
| 286 |
np.testing.assert_equal( |
| 287 |
res.sum(0) <= q, [True] * len(q) |
| 288 |
) |
| 289 |
np.testing.assert_allclose(np.sum(res), m, atol=1e-04) |