| 1 |
"""Tests for main module ot""" |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
import warnings |
| 8 |
|
| 9 |
import numpy as np |
| 10 |
import pytest |
| 11 |
|
| 12 |
import ot |
| 13 |
from ot.datasets import make_1D_gauss as gauss |
| 14 |
from ot.backend import torch, tf |
| 15 |
|
| 16 |
|
| 17 |
def test_emd_dimension_and_mass_mismatch(): |
| 18 |
|
| 19 |
n_samples = 100 |
| 20 |
n_features = 2 |
| 21 |
rng = np.random.RandomState(0) |
| 22 |
|
| 23 |
x = rng.randn(n_samples, n_features) |
| 24 |
a = ot.utils.unif(n_samples + 1) |
| 25 |
|
| 26 |
M = ot.dist(x, x) |
| 27 |
|
| 28 |
np.testing.assert_raises(AssertionError, ot.emd, a, a, M) |
| 29 |
|
| 30 |
np.testing.assert_raises(AssertionError, ot.emd2, a, a, M) |
| 31 |
|
| 32 |
|
| 33 |
a = ot.utils.unif(n_samples) |
| 34 |
b = a.copy() |
| 35 |
a[0] = 100 |
| 36 |
np.testing.assert_raises(AssertionError, ot.emd, a, b, M) |
| 37 |
np.testing.assert_raises(AssertionError, ot.emd2, a, b, M) |
| 38 |
|
| 39 |
|
| 40 |
def test_emd_backends(nx): |
| 41 |
n_samples = 100 |
| 42 |
n_features = 2 |
| 43 |
rng = np.random.RandomState(0) |
| 44 |
|
| 45 |
x = rng.randn(n_samples, n_features) |
| 46 |
y = rng.randn(n_samples, n_features) |
| 47 |
a = ot.utils.unif(n_samples) |
| 48 |
|
| 49 |
M = ot.dist(x, y) |
| 50 |
|
| 51 |
G = ot.emd(a, a, M) |
| 52 |
|
| 53 |
ab, Mb = nx.from_numpy(a, M) |
| 54 |
|
| 55 |
Gb = ot.emd(ab, ab, Mb) |
| 56 |
|
| 57 |
np.allclose(G, nx.to_numpy(Gb)) |
| 58 |
|
| 59 |
|
| 60 |
def test_emd2_backends(nx): |
| 61 |
n_samples = 100 |
| 62 |
n_features = 2 |
| 63 |
rng = np.random.RandomState(0) |
| 64 |
|
| 65 |
x = rng.randn(n_samples, n_features) |
| 66 |
y = rng.randn(n_samples, n_features) |
| 67 |
a = ot.utils.unif(n_samples) |
| 68 |
|
| 69 |
M = ot.dist(x, y) |
| 70 |
|
| 71 |
val = ot.emd2(a, a, M) |
| 72 |
|
| 73 |
ab, Mb = nx.from_numpy(a, M) |
| 74 |
|
| 75 |
valb = ot.emd2(ab, ab, Mb) |
| 76 |
|
| 77 |
|
| 78 |
valb2 = ot.emd2([], [], Mb) |
| 79 |
|
| 80 |
np.allclose(val, nx.to_numpy(valb)) |
| 81 |
np.allclose(val, nx.to_numpy(valb2)) |
| 82 |
|
| 83 |
|
| 84 |
def test_emd_emd2_types_devices(nx): |
| 85 |
n_samples = 100 |
| 86 |
n_features = 2 |
| 87 |
rng = np.random.RandomState(0) |
| 88 |
|
| 89 |
x = rng.randn(n_samples, n_features) |
| 90 |
y = rng.randn(n_samples, n_features) |
| 91 |
a = ot.utils.unif(n_samples) |
| 92 |
|
| 93 |
M = ot.dist(x, y) |
| 94 |
|
| 95 |
for tp in nx.__type_list__: |
| 96 |
print(nx.dtype_device(tp)) |
| 97 |
|
| 98 |
ab, Mb = nx.from_numpy(a, M, type_as=tp) |
| 99 |
|
| 100 |
Gb = ot.emd(ab, ab, Mb) |
| 101 |
|
| 102 |
w = ot.emd2(ab, ab, Mb) |
| 103 |
|
| 104 |
nx.assert_same_dtype_device(Mb, Gb) |
| 105 |
nx.assert_same_dtype_device(Mb, w) |
| 106 |
|
| 107 |
|
| 108 |
@pytest.mark.skipif(not tf, reason="tf not installed") |
| 109 |
def test_emd_emd2_devices_tf(): |
| 110 |
nx = ot.backend.TensorflowBackend() |
| 111 |
|
| 112 |
n_samples = 100 |
| 113 |
n_features = 2 |
| 114 |
rng = np.random.RandomState(0) |
| 115 |
x = rng.randn(n_samples, n_features) |
| 116 |
y = rng.randn(n_samples, n_features) |
| 117 |
a = ot.utils.unif(n_samples) |
| 118 |
M = ot.dist(x, y) |
| 119 |
|
| 120 |
|
| 121 |
with tf.device("/CPU:0"): |
| 122 |
ab, Mb = nx.from_numpy(a, M) |
| 123 |
Gb = ot.emd(ab, ab, Mb) |
| 124 |
w = ot.emd2(ab, ab, Mb) |
| 125 |
nx.assert_same_dtype_device(Mb, Gb) |
| 126 |
nx.assert_same_dtype_device(Mb, w) |
| 127 |
|
| 128 |
if len(tf.config.list_physical_devices("GPU")) > 0: |
| 129 |
|
| 130 |
ab, Mb = nx.from_numpy(a, M) |
| 131 |
Gb = ot.emd(ab, ab, Mb) |
| 132 |
w = ot.emd2(ab, ab, Mb) |
| 133 |
nx.assert_same_dtype_device(Mb, Gb) |
| 134 |
nx.assert_same_dtype_device(Mb, w) |
| 135 |
assert nx.dtype_device(Gb)[1].startswith("GPU") |
| 136 |
|
| 137 |
|
| 138 |
def test_emd2_gradients(): |
| 139 |
n_samples = 100 |
| 140 |
n_features = 2 |
| 141 |
rng = np.random.RandomState(0) |
| 142 |
|
| 143 |
x = rng.randn(n_samples, n_features) |
| 144 |
y = rng.randn(n_samples, n_features) |
| 145 |
a = ot.utils.unif(n_samples) |
| 146 |
|
| 147 |
M = ot.dist(x, y) |
| 148 |
|
| 149 |
if torch: |
| 150 |
a1 = torch.tensor(a, requires_grad=True) |
| 151 |
b1 = torch.tensor(a, requires_grad=True) |
| 152 |
M1 = torch.tensor(M, requires_grad=True) |
| 153 |
|
| 154 |
val, log = ot.emd2(a1, b1, M1, log=True) |
| 155 |
|
| 156 |
val.backward() |
| 157 |
|
| 158 |
assert a1.shape == a1.grad.shape |
| 159 |
assert b1.shape == b1.grad.shape |
| 160 |
assert M1.shape == M1.grad.shape |
| 161 |
|
| 162 |
assert np.allclose( |
| 163 |
a1.grad.cpu().detach().numpy(), |
| 164 |
log["u"].cpu().detach().numpy() - log["u"].cpu().detach().numpy().mean(), |
| 165 |
) |
| 166 |
|
| 167 |
assert np.allclose( |
| 168 |
b1.grad.cpu().detach().numpy(), |
| 169 |
log["v"].cpu().detach().numpy() - log["v"].cpu().detach().numpy().mean(), |
| 170 |
) |
| 171 |
|
| 172 |
|
| 173 |
a2 = torch.tensor(a, requires_grad=True) |
| 174 |
b2 = torch.tensor(a, requires_grad=True) |
| 175 |
M2 = torch.tensor(M, requires_grad=True) |
| 176 |
|
| 177 |
val = 10.0 * ot.emd2(a2, b2, M2) |
| 178 |
|
| 179 |
val.backward() |
| 180 |
|
| 181 |
assert np.allclose( |
| 182 |
10.0 * a1.grad.cpu().detach().numpy(), a2.grad.cpu().detach().numpy() |
| 183 |
) |
| 184 |
assert np.allclose( |
| 185 |
10.0 * b1.grad.cpu().detach().numpy(), b2.grad.cpu().detach().numpy() |
| 186 |
) |
| 187 |
assert np.allclose( |
| 188 |
10.0 * M1.grad.cpu().detach().numpy(), M2.grad.cpu().detach().numpy() |
| 189 |
) |
| 190 |
|
| 191 |
|
| 192 |
def test_emd_emd2(): |
| 193 |
|
| 194 |
n = 100 |
| 195 |
rng = np.random.RandomState(0) |
| 196 |
|
| 197 |
x = rng.randn(n, 2) |
| 198 |
u = ot.utils.unif(n) |
| 199 |
|
| 200 |
M = ot.dist(x, x) |
| 201 |
|
| 202 |
G = ot.emd(u, u, M) |
| 203 |
|
| 204 |
|
| 205 |
np.testing.assert_allclose(G, np.eye(n) / n) |
| 206 |
|
| 207 |
np.testing.assert_allclose(u, G.sum(1)) |
| 208 |
np.testing.assert_allclose(u, G.sum(0)) |
| 209 |
|
| 210 |
w = ot.emd2(u, u, M) |
| 211 |
|
| 212 |
np.testing.assert_allclose(w, 0) |
| 213 |
|
| 214 |
|
| 215 |
def test_omp_emd2(): |
| 216 |
|
| 217 |
n = 100 |
| 218 |
rng = np.random.RandomState(0) |
| 219 |
|
| 220 |
x = rng.randn(n, 2) |
| 221 |
u = ot.utils.unif(n) |
| 222 |
|
| 223 |
M = ot.dist(x, x) |
| 224 |
|
| 225 |
w = ot.emd2(u, u, M) |
| 226 |
w2 = ot.emd2(u, u, M, numThreads=2) |
| 227 |
|
| 228 |
np.testing.assert_allclose(w, w2) |
| 229 |
|
| 230 |
|
| 231 |
def test_emd_empty(): |
| 232 |
|
| 233 |
n = 100 |
| 234 |
rng = np.random.RandomState(0) |
| 235 |
|
| 236 |
x = rng.randn(n, 2) |
| 237 |
u = ot.utils.unif(n) |
| 238 |
|
| 239 |
M = ot.dist(x, x) |
| 240 |
|
| 241 |
G = ot.emd([], [], M) |
| 242 |
|
| 243 |
|
| 244 |
np.testing.assert_allclose(G, np.eye(n) / n) |
| 245 |
|
| 246 |
np.testing.assert_allclose(u, G.sum(1)) |
| 247 |
np.testing.assert_allclose(u, G.sum(0)) |
| 248 |
|
| 249 |
w = ot.emd2([], [], M) |
| 250 |
|
| 251 |
np.testing.assert_allclose(w, 0) |
| 252 |
|
| 253 |
|
| 254 |
def test_emd2_multi(): |
| 255 |
n = 500 |
| 256 |
|
| 257 |
|
| 258 |
x = np.arange(n, dtype=np.float64) |
| 259 |
|
| 260 |
|
| 261 |
a = gauss(n, m=20, s=5) |
| 262 |
|
| 263 |
ls = np.arange(20, 500, 100) |
| 264 |
nb = len(ls) |
| 265 |
b = np.zeros((n, nb)) |
| 266 |
for i in range(nb): |
| 267 |
b[:, i] = gauss(n, m=ls[i], s=10) |
| 268 |
|
| 269 |
|
| 270 |
M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1))) |
| 271 |
|
| 272 |
|
| 273 |
print("Computing {} EMD ".format(nb)) |
| 274 |
|
| 275 |
|
| 276 |
ot.tic() |
| 277 |
emd1 = ot.emd2(a, b, M, 1) |
| 278 |
ot.toc("1 proc : {} s") |
| 279 |
|
| 280 |
|
| 281 |
ot.tic() |
| 282 |
emdn = ot.emd2(a, b, M) |
| 283 |
ot.toc("multi proc : {} s") |
| 284 |
|
| 285 |
np.testing.assert_allclose(emd1, emdn) |
| 286 |
|
| 287 |
|
| 288 |
ot.tic() |
| 289 |
emdn = ot.emd2(a, b, M, log=True, return_matrix=True) |
| 290 |
ot.toc("multi proc : {} s") |
| 291 |
|
| 292 |
for i in range(len(emdn)): |
| 293 |
emd = emdn[i] |
| 294 |
log = emd[1] |
| 295 |
cost = emd[0] |
| 296 |
check_duality_gap(a, b[:, i], M, log["G"], log["u"], log["v"], cost) |
| 297 |
emdn[i] = cost |
| 298 |
|
| 299 |
emdn = np.array(emdn) |
| 300 |
np.testing.assert_allclose(emd1, emdn) |
| 301 |
|
| 302 |
|
| 303 |
def test_lp_barycenter(): |
| 304 |
a1 = np.array([1.0, 0, 0])[:, None] |
| 305 |
a2 = np.array([0, 0, 1.0])[:, None] |
| 306 |
|
| 307 |
A = np.hstack((a1, a2)) |
| 308 |
M = np.array([[0, 1.0, 4.0], [1.0, 0, 1.0], [4.0, 1.0, 0]]) |
| 309 |
|
| 310 |
|
| 311 |
bary0 = np.array([0, 1.0, 0]) |
| 312 |
|
| 313 |
bary = ot.lp.barycenter(A, M, [0.5, 0.5]) |
| 314 |
|
| 315 |
np.testing.assert_allclose(bary, bary0, rtol=1e-5, atol=1e-7) |
| 316 |
np.testing.assert_allclose(bary.sum(), 1) |
| 317 |
|
| 318 |
|
| 319 |
def test_free_support_barycenter(): |
| 320 |
measures_locations = [ |
| 321 |
np.array([-1.0]).reshape((1, 1)), |
| 322 |
np.array([1.0]).reshape((1, 1)), |
| 323 |
] |
| 324 |
measures_weights = [np.array([1.0]), np.array([1.0])] |
| 325 |
|
| 326 |
X_init = np.array([-12.0]).reshape((1, 1)) |
| 327 |
|
| 328 |
|
| 329 |
bar_locations = np.array([0.0]).reshape((1, 1)) |
| 330 |
|
| 331 |
X = ot.lp.free_support_barycenter(measures_locations, measures_weights, X_init) |
| 332 |
|
| 333 |
np.testing.assert_allclose(X, bar_locations, rtol=1e-5, atol=1e-7) |
| 334 |
|
| 335 |
|
| 336 |
def test_free_support_barycenter_backends(nx): |
| 337 |
measures_locations = [ |
| 338 |
np.array([-1.0]).reshape((1, 1)), |
| 339 |
np.array([1.0]).reshape((1, 1)), |
| 340 |
] |
| 341 |
measures_weights = [np.array([1.0]), np.array([1.0])] |
| 342 |
X_init = np.array([-12.0]).reshape((1, 1)) |
| 343 |
|
| 344 |
X = ot.lp.free_support_barycenter(measures_locations, measures_weights, X_init) |
| 345 |
|
| 346 |
measures_locations2 = nx.from_numpy(*measures_locations) |
| 347 |
measures_weights2 = nx.from_numpy(*measures_weights) |
| 348 |
X_init2 = nx.from_numpy(X_init) |
| 349 |
|
| 350 |
X2 = ot.lp.free_support_barycenter(measures_locations2, measures_weights2, X_init2) |
| 351 |
|
| 352 |
np.testing.assert_allclose(X, nx.to_numpy(X2)) |
| 353 |
|
| 354 |
|
| 355 |
def test_generalised_free_support_barycenter(): |
| 356 |
X = [ |
| 357 |
np.array([-1.0, -1.0]).reshape((1, 2)), |
| 358 |
np.array([1.0, 1.0]).reshape((1, 2)), |
| 359 |
] |
| 360 |
a = [np.array([1.0]), np.array([1.0])] |
| 361 |
|
| 362 |
P = [np.eye(2), np.eye(2)] |
| 363 |
|
| 364 |
Y_init = np.array([-12.0, 7.0]).reshape((1, 2)) |
| 365 |
|
| 366 |
|
| 367 |
Y_true = np.array([0.0, 0.0]).reshape((1, 2)) |
| 368 |
|
| 369 |
|
| 370 |
Y = ot.lp.generalized_free_support_barycenter(X, a, P, 1) |
| 371 |
np.testing.assert_allclose(Y, Y_true, rtol=1e-5, atol=1e-7) |
| 372 |
|
| 373 |
|
| 374 |
Y, _ = ot.lp.generalized_free_support_barycenter( |
| 375 |
X, a, P, 1, Y_init=Y_init, b=np.array([1.0]), log=True |
| 376 |
) |
| 377 |
np.testing.assert_allclose(Y, Y_true, rtol=1e-5, atol=1e-7) |
| 378 |
|
| 379 |
|
| 380 |
def test_generalised_free_support_barycenter_backends(nx): |
| 381 |
X = [np.array([-1.0]).reshape((1, 1)), np.array([1.0]).reshape((1, 1))] |
| 382 |
a = [np.array([1.0]), np.array([1.0])] |
| 383 |
P = [np.array([1.0]).reshape((1, 1)), np.array([1.0]).reshape((1, 1))] |
| 384 |
Y_init = np.array([-12.0]).reshape((1, 1)) |
| 385 |
|
| 386 |
Y = ot.lp.generalized_free_support_barycenter(X, a, P, 1, Y_init=Y_init) |
| 387 |
|
| 388 |
X2 = nx.from_numpy(*X) |
| 389 |
a2 = nx.from_numpy(*a) |
| 390 |
P2 = nx.from_numpy(*P) |
| 391 |
Y_init2 = nx.from_numpy(Y_init) |
| 392 |
|
| 393 |
Y2 = ot.lp.generalized_free_support_barycenter(X2, a2, P2, 1, Y_init=Y_init2) |
| 394 |
|
| 395 |
np.testing.assert_allclose(Y, nx.to_numpy(Y2)) |
| 396 |
|
| 397 |
|
| 398 |
@pytest.mark.skipif(not ot.lp.cvx.cvxopt, reason="No cvxopt available") |
| 399 |
def test_lp_barycenter_cvxopt(): |
| 400 |
a1 = np.array([1.0, 0, 0])[:, None] |
| 401 |
a2 = np.array([0, 0, 1.0])[:, None] |
| 402 |
|
| 403 |
A = np.hstack((a1, a2)) |
| 404 |
M = np.array([[0, 1.0, 4.0], [1.0, 0, 1.0], [4.0, 1.0, 0]]) |
| 405 |
|
| 406 |
|
| 407 |
bary0 = np.array([0, 1.0, 0]) |
| 408 |
|
| 409 |
bary = ot.lp.barycenter(A, M, [0.5, 0.5], solver=None) |
| 410 |
|
| 411 |
np.testing.assert_allclose(bary, bary0, rtol=1e-5, atol=1e-7) |
| 412 |
np.testing.assert_allclose(bary.sum(), 1) |
| 413 |
|
| 414 |
|
| 415 |
def test_warnings(): |
| 416 |
n = 100 |
| 417 |
m = 100 |
| 418 |
|
| 419 |
mean1 = 30 |
| 420 |
mean2 = 50 |
| 421 |
|
| 422 |
|
| 423 |
x = np.arange(n, dtype=np.float64) |
| 424 |
y = np.arange(m, dtype=np.float64) |
| 425 |
|
| 426 |
|
| 427 |
a = gauss(n, m=mean1, s=5) |
| 428 |
|
| 429 |
b = gauss(m, m=mean2, s=10) |
| 430 |
|
| 431 |
|
| 432 |
M = ot.dist(x.reshape((-1, 1)), y.reshape((-1, 1))) ** (1.0 / 2) |
| 433 |
|
| 434 |
print("Computing {} EMD ".format(1)) |
| 435 |
with warnings.catch_warnings(record=True) as w: |
| 436 |
warnings.simplefilter("always") |
| 437 |
print("Computing {} EMD ".format(1)) |
| 438 |
ot.emd(a, b, M, numItermax=1) |
| 439 |
assert "numItermax" in str(w[-1].message) |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
def test_dual_variables(): |
| 444 |
n = 500 |
| 445 |
m = 600 |
| 446 |
|
| 447 |
mean1 = 300 |
| 448 |
mean2 = 400 |
| 449 |
|
| 450 |
|
| 451 |
x = np.arange(n, dtype=np.float64) |
| 452 |
y = np.arange(m, dtype=np.float64) |
| 453 |
|
| 454 |
|
| 455 |
a = gauss(n, m=mean1, s=5) |
| 456 |
|
| 457 |
b = gauss(m, m=mean2, s=10) |
| 458 |
|
| 459 |
|
| 460 |
M = ot.dist(x.reshape((-1, 1)), y.reshape((-1, 1))) ** (1.0 / 2) |
| 461 |
|
| 462 |
print("Computing {} EMD ".format(1)) |
| 463 |
|
| 464 |
|
| 465 |
ot.tic() |
| 466 |
G, log = ot.emd(a, b, M, log=True) |
| 467 |
ot.toc("1 proc : {} s") |
| 468 |
|
| 469 |
ot.tic() |
| 470 |
G2 = ot.emd(b, a, np.ascontiguousarray(M.T)) |
| 471 |
ot.toc("1 proc : {} s") |
| 472 |
|
| 473 |
cost1 = (G * M).sum() |
| 474 |
|
| 475 |
np.testing.assert_array_almost_equal(cost1, (M * G2.T).sum()) |
| 476 |
|
| 477 |
np.testing.assert_almost_equal(cost1, np.abs(mean1 - mean2)) |
| 478 |
|
| 479 |
|
| 480 |
np.testing.assert_almost_equal(cost1, log["cost"]) |
| 481 |
check_duality_gap(a, b, M, G, log["u"], log["v"], log["cost"]) |
| 482 |
|
| 483 |
constraint_violation = log["u"][:, None] + log["v"][None, :] - M |
| 484 |
|
| 485 |
assert constraint_violation.max() < 1e-8 |
| 486 |
|
| 487 |
|
| 488 |
def check_duality_gap(a, b, M, G, u, v, cost): |
| 489 |
cost_dual = np.vdot(a, u) + np.vdot(b, v) |
| 490 |
|
| 491 |
np.testing.assert_almost_equal(cost_dual, cost) |
| 492 |
|
| 493 |
[ind1, ind2] = np.nonzero(G) |
| 494 |
|
| 495 |
|
| 496 |
np.testing.assert_array_almost_equal( |
| 497 |
(M - u.reshape(-1, 1) - v.reshape(1, -1))[ind1, ind2], np.zeros(ind1.size) |
| 498 |
) |