1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
"""Tests for module COOT on OT"""
# Author: Quang Huy Tran <quang-huy.tran@univ-ubs.fr>
#
# License: MIT License
import numpy as np
import ot
from ot.coot import co_optimal_transport as coot
from ot.coot import co_optimal_transport2 as coot2
import pytest
@pytest.mark.parametrize("verbose", [False, True, 1, 0])
def test_coot(nx, verbose):
n_samples = 60 # nb samples
mu_s = np.array([0, 0])
cov_s = np.array([[1, 0], [0, 1]])
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=4)
xt = xs[::-1].copy()
xs_nx = nx.from_numpy(xs)
xt_nx = nx.from_numpy(xt)
# test couplings
pi_sample, pi_feature = coot(X=xs, Y=xt, verbose=verbose)
pi_sample_nx, pi_feature_nx = coot(X=xs_nx, Y=xt_nx, verbose=verbose)
pi_sample_nx = nx.to_numpy(pi_sample_nx)
pi_feature_nx = nx.to_numpy(pi_feature_nx)
anti_id_sample = np.flipud(np.eye(n_samples, n_samples)) / n_samples
id_feature = np.eye(2, 2) / 2
np.testing.assert_allclose(pi_sample, anti_id_sample, atol=1e-04)
np.testing.assert_allclose(pi_sample_nx, anti_id_sample, atol=1e-04)
np.testing.assert_allclose(pi_feature, id_feature, atol=1e-04)
np.testing.assert_allclose(pi_feature_nx, id_feature, atol=1e-04)
# test marginal distributions
px_s, px_f = ot.unif(n_samples), ot.unif(2)
py_s, py_f = ot.unif(n_samples), ot.unif(2)
np.testing.assert_allclose(px_s, pi_sample_nx.sum(0), atol=1e-04)
np.testing.assert_allclose(py_s, pi_sample_nx.sum(1), atol=1e-04)
np.testing.assert_allclose(px_f, pi_feature_nx.sum(0), atol=1e-04)
np.testing.assert_allclose(py_f, pi_feature_nx.sum(1), atol=1e-04)
np.testing.assert_allclose(px_s, pi_sample.sum(0), atol=1e-04)
np.testing.assert_allclose(py_s, pi_sample.sum(1), atol=1e-04)
np.testing.assert_allclose(px_f, pi_feature.sum(0), atol=1e-04)
np.testing.assert_allclose(py_f, pi_feature.sum(1), atol=1e-04)
# test COOT distance
coot_np = coot2(X=xs, Y=xt, verbose=verbose)
coot_nx = nx.to_numpy(coot2(X=xs_nx, Y=xt_nx, verbose=verbose))
np.testing.assert_allclose(coot_np, 0, atol=1e-08)
np.testing.assert_allclose(coot_nx, 0, atol=1e-08)
def test_entropic_coot(nx):
n_samples = 60 # nb samples
mu_s = np.array([0, 0])
cov_s = np.array([[1, 0], [0, 1]])
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=4)
xt = xs[::-1].copy()
xs_nx = nx.from_numpy(xs)
xt_nx = nx.from_numpy(xt)
epsilon = (1, 1e-1)
nits_ot = 2000
# test couplings
pi_sample, pi_feature = coot(X=xs, Y=xt, epsilon=epsilon, nits_ot=nits_ot)
pi_sample_nx, pi_feature_nx = coot(
X=xs_nx, Y=xt_nx, epsilon=epsilon, nits_ot=nits_ot
)
pi_sample_nx = nx.to_numpy(pi_sample_nx)
pi_feature_nx = nx.to_numpy(pi_feature_nx)
np.testing.assert_allclose(pi_sample, pi_sample_nx, atol=1e-04)
np.testing.assert_allclose(pi_feature, pi_feature_nx, atol=1e-04)
# test marginal distributions
px_s, px_f = ot.unif(n_samples), ot.unif(2)
py_s, py_f = ot.unif(n_samples), ot.unif(2)
np.testing.assert_allclose(px_s, pi_sample_nx.sum(0), atol=1e-04)
np.testing.assert_allclose(py_s, pi_sample_nx.sum(1), atol=1e-04)
np.testing.assert_allclose(px_f, pi_feature_nx.sum(0), atol=1e-04)
np.testing.assert_allclose(py_f, pi_feature_nx.sum(1), atol=1e-04)
np.testing.assert_allclose(px_s, pi_sample.sum(0), atol=1e-04)
np.testing.assert_allclose(py_s, pi_sample.sum(1), atol=1e-04)
np.testing.assert_allclose(px_f, pi_feature.sum(0), atol=1e-04)
np.testing.assert_allclose(py_f, pi_feature.sum(1), atol=1e-04)
# test entropic COOT distance
coot_np = coot2(X=xs, Y=xt, epsilon=epsilon, nits_ot=nits_ot)
coot_nx = nx.to_numpy(coot2(X=xs_nx, Y=xt_nx, epsilon=epsilon, nits_ot=nits_ot))
np.testing.assert_allclose(coot_np, coot_nx, atol=1e-08)
def test_coot_with_linear_terms(nx):
n_samples = 60 # nb samples
mu_s = np.array([0, 0])
cov_s = np.array([[1, 0], [0, 1]])
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=4)
xt = xs[::-1].copy()
xs_nx = nx.from_numpy(xs)
xt_nx = nx.from_numpy(xt)
M_samp = np.ones((n_samples, n_samples))
np.fill_diagonal(np.fliplr(M_samp), 0)
M_feat = np.ones((2, 2))
np.fill_diagonal(M_feat, 0)
M_samp_nx, M_feat_nx = nx.from_numpy(M_samp), nx.from_numpy(M_feat)
alpha = (1, 2)
# test couplings
anti_id_sample = np.flipud(np.eye(n_samples, n_samples)) / n_samples
id_feature = np.eye(2, 2) / 2
pi_sample, pi_feature = coot(X=xs, Y=xt, alpha=alpha, M_samp=M_samp, M_feat=M_feat)
pi_sample_nx, pi_feature_nx = coot(
X=xs_nx, Y=xt_nx, alpha=alpha, M_samp=M_samp_nx, M_feat=M_feat_nx
)
pi_sample_nx = nx.to_numpy(pi_sample_nx)
pi_feature_nx = nx.to_numpy(pi_feature_nx)
np.testing.assert_allclose(pi_sample, anti_id_sample, atol=1e-04)
np.testing.assert_allclose(pi_sample_nx, anti_id_sample, atol=1e-04)
np.testing.assert_allclose(pi_feature, id_feature, atol=1e-04)
np.testing.assert_allclose(pi_feature_nx, id_feature, atol=1e-04)
# test marginal distributions
px_s, px_f = ot.unif(n_samples), ot.unif(2)
py_s, py_f = ot.unif(n_samples), ot.unif(2)
np.testing.assert_allclose(px_s, pi_sample_nx.sum(0), atol=1e-04)
np.testing.assert_allclose(py_s, pi_sample_nx.sum(1), atol=1e-04)
np.testing.assert_allclose(px_f, pi_feature_nx.sum(0), atol=1e-04)
np.testing.assert_allclose(py_f, pi_feature_nx.sum(1), atol=1e-04)
np.testing.assert_allclose(px_s, pi_sample.sum(0), atol=1e-04)
np.testing.assert_allclose(py_s, pi_sample.sum(1), atol=1e-04)
np.testing.assert_allclose(px_f, pi_feature.sum(0), atol=1e-04)
np.testing.assert_allclose(py_f, pi_feature.sum(1), atol=1e-04)
# test COOT distance
coot_np = coot2(X=xs, Y=xt, alpha=alpha, M_samp=M_samp, M_feat=M_feat)
coot_nx = nx.to_numpy(
coot2(X=xs_nx, Y=xt_nx, alpha=alpha, M_samp=M_samp_nx, M_feat=M_feat_nx)
)
np.testing.assert_allclose(coot_np, 0, atol=1e-08)
np.testing.assert_allclose(coot_nx, 0, atol=1e-08)
def test_coot_raise_value_error(nx):
n_samples = 80 # nb samples
mu_s = np.array([2, 4])
cov_s = np.array([[1, 0], [0, 1]])
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=43)
xt = xs[::-1].copy()
xs_nx = nx.from_numpy(xs)
xt_nx = nx.from_numpy(xt)
# raise value error of method sinkhorn
def coot_sh(method_sinkhorn):
return coot(X=xs, Y=xt, method_sinkhorn=method_sinkhorn)
def coot_sh_nx(method_sinkhorn):
return coot(X=xs_nx, Y=xt_nx, method_sinkhorn=method_sinkhorn)
np.testing.assert_raises(ValueError, coot_sh, "not_sinkhorn")
np.testing.assert_raises(ValueError, coot_sh_nx, "not_sinkhorn")
# raise value error for epsilon
def coot_eps(epsilon):
return coot(X=xs, Y=xt, epsilon=epsilon)
def coot_eps_nx(epsilon):
return coot(X=xs_nx, Y=xt_nx, epsilon=epsilon)
np.testing.assert_raises(ValueError, coot_eps, (1, 2, 3))
np.testing.assert_raises(ValueError, coot_eps_nx, [1, 2, 3, 4])
# raise value error for alpha
def coot_alpha(alpha):
return coot(X=xs, Y=xt, alpha=alpha)
def coot_alpha_nx(alpha):
return coot(X=xs_nx, Y=xt_nx, alpha=alpha)
np.testing.assert_raises(ValueError, coot_alpha, [1])
np.testing.assert_raises(ValueError, coot_alpha_nx, np.arange(4))
def test_coot_warmstart(nx):
n_samples = 80 # nb samples
mu_s = np.array([2, 3])
cov_s = np.array([[1, 0], [0, 1]])
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=125)
xt = xs[::-1].copy()
xs_nx = nx.from_numpy(xs)
xt_nx = nx.from_numpy(xt)
# initialize warmstart
rng = np.random.RandomState(42)
init_pi_sample = rng.rand(n_samples, n_samples)
init_pi_sample = init_pi_sample / np.sum(init_pi_sample)
init_pi_sample_nx = nx.from_numpy(init_pi_sample)
init_pi_feature = rng.rand(2, 2)
init_pi_feature /= init_pi_feature / np.sum(init_pi_feature)
init_pi_feature_nx = nx.from_numpy(init_pi_feature)
init_duals_sample = (rng.random(n_samples) * 2 - 1, rng.random(n_samples) * 2 - 1)
init_duals_sample_nx = (
nx.from_numpy(init_duals_sample[0]),
nx.from_numpy(init_duals_sample[1]),
)
init_duals_feature = (rng.random(2) * 2 - 1, rng.random(2) * 2 - 1)
init_duals_feature_nx = (
nx.from_numpy(init_duals_feature[0]),
nx.from_numpy(init_duals_feature[1]),
)
warmstart = {
"pi_sample": init_pi_sample,
"pi_feature": init_pi_feature,
"duals_sample": init_duals_sample,
"duals_feature": init_duals_feature,
}
warmstart_nx = {
"pi_sample": init_pi_sample_nx,
"pi_feature": init_pi_feature_nx,
"duals_sample": init_duals_sample_nx,
"duals_feature": init_duals_feature_nx,
}
# test couplings
pi_sample, pi_feature = coot(X=xs, Y=xt, warmstart=warmstart)
pi_sample_nx, pi_feature_nx = coot(X=xs_nx, Y=xt_nx, warmstart=warmstart_nx)
pi_sample_nx = nx.to_numpy(pi_sample_nx)
pi_feature_nx = nx.to_numpy(pi_feature_nx)
anti_id_sample = np.flipud(np.eye(n_samples, n_samples)) / n_samples
id_feature = np.eye(2, 2) / 2
np.testing.assert_allclose(pi_sample, anti_id_sample, atol=1e-04)
np.testing.assert_allclose(pi_sample_nx, anti_id_sample, atol=1e-04)
np.testing.assert_allclose(pi_feature, id_feature, atol=1e-04)
np.testing.assert_allclose(pi_feature_nx, id_feature, atol=1e-04)
# test marginal distributions
px_s, px_f = ot.unif(n_samples), ot.unif(2)
py_s, py_f = ot.unif(n_samples), ot.unif(2)
np.testing.assert_allclose(px_s, pi_sample_nx.sum(0), atol=1e-04)
np.testing.assert_allclose(py_s, pi_sample_nx.sum(1), atol=1e-04)
np.testing.assert_allclose(px_f, pi_feature_nx.sum(0), atol=1e-04)
np.testing.assert_allclose(py_f, pi_feature_nx.sum(1), atol=1e-04)
np.testing.assert_allclose(px_s, pi_sample.sum(0), atol=1e-04)
np.testing.assert_allclose(py_s, pi_sample.sum(1), atol=1e-04)
np.testing.assert_allclose(px_f, pi_feature.sum(0), atol=1e-04)
np.testing.assert_allclose(py_f, pi_feature.sum(1), atol=1e-04)
# test COOT distance
coot_np = coot2(X=xs, Y=xt, warmstart=warmstart)
coot_nx = nx.to_numpy(coot2(X=xs_nx, Y=xt_nx, warmstart=warmstart_nx))
np.testing.assert_allclose(coot_np, 0, atol=1e-08)
np.testing.assert_allclose(coot_nx, 0, atol=1e-08)
def test_coot_log(nx):
n_samples = 90 # nb samples
mu_s = np.array([-2, 0])
cov_s = np.array([[1, 0], [0, 1]])
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=43)
xt = xs[::-1].copy()
xs_nx = nx.from_numpy(xs)
xt_nx = nx.from_numpy(xt)
pi_sample, pi_feature, log = coot(X=xs, Y=xt, log=True)
pi_sample_nx, pi_feature_nx, log_nx = coot(X=xs_nx, Y=xt_nx, log=True)
duals_sample, duals_feature = log["duals_sample"], log["duals_feature"]
assert len(duals_sample) == 2
assert len(duals_feature) == 2
assert len(duals_sample[0]) == n_samples
assert len(duals_sample[1]) == n_samples
assert len(duals_feature[0]) == 2
assert len(duals_feature[1]) == 2
duals_sample_nx = log_nx["duals_sample"]
assert len(duals_sample_nx) == 2
assert len(duals_sample_nx[0]) == n_samples
assert len(duals_sample_nx[1]) == n_samples
duals_feature_nx = log_nx["duals_feature"]
assert len(duals_feature_nx) == 2
assert len(duals_feature_nx[0]) == 2
assert len(duals_feature_nx[1]) == 2
list_coot = log["distances"]
assert len(list_coot) >= 1
list_coot_nx = log_nx["distances"]
assert len(list_coot_nx) >= 1
# test with coot distance
coot_np, log = coot2(X=xs, Y=xt, log=True)
coot_nx, log_nx = coot2(X=xs_nx, Y=xt_nx, log=True)
duals_sample, duals_feature = log["duals_sample"], log["duals_feature"]
assert len(duals_sample) == 2
assert len(duals_feature) == 2
assert len(duals_sample[0]) == n_samples
assert len(duals_sample[1]) == n_samples
assert len(duals_feature[0]) == 2
assert len(duals_feature[1]) == 2
duals_sample_nx = log_nx["duals_sample"]
assert len(duals_sample_nx) == 2
assert len(duals_sample_nx[0]) == n_samples
assert len(duals_sample_nx[1]) == n_samples
duals_feature_nx = log_nx["duals_feature"]
assert len(duals_feature_nx) == 2
assert len(duals_feature_nx[0]) == 2
assert len(duals_feature_nx[1]) == 2
list_coot = log["distances"]
assert len(list_coot) >= 1
list_coot_nx = log_nx["distances"]
assert len(list_coot_nx) >= 1
|