| 1 |
"""Tests for module 1d Wasserstein solver""" |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
import numpy as np |
| 9 |
import pytest |
| 10 |
|
| 11 |
import ot |
| 12 |
from ot.backend import tf |
| 13 |
from ot.lp import wasserstein_1d |
| 14 |
|
| 15 |
from scipy.stats import wasserstein_distance |
| 16 |
|
| 17 |
|
| 18 |
def test_emd_1d_emd2_1d_with_weights(): |
| 19 |
|
| 20 |
n = 20 |
| 21 |
m = 30 |
| 22 |
rng = np.random.RandomState(0) |
| 23 |
u = rng.randn(n, 1) |
| 24 |
v = rng.randn(m, 1) |
| 25 |
|
| 26 |
w_u = rng.uniform(0.0, 1.0, n) |
| 27 |
w_u = w_u / w_u.sum() |
| 28 |
|
| 29 |
w_v = rng.uniform(0.0, 1.0, m) |
| 30 |
w_v = w_v / w_v.sum() |
| 31 |
|
| 32 |
M = ot.dist(u, v, metric="sqeuclidean") |
| 33 |
|
| 34 |
G, log = ot.emd(w_u, w_v, M, log=True) |
| 35 |
wass = log["cost"] |
| 36 |
G_1d, log = ot.emd_1d(u, v, w_u, w_v, metric="sqeuclidean", log=True) |
| 37 |
wass1d = log["cost"] |
| 38 |
wass1d_emd2 = ot.emd2_1d(u, v, w_u, w_v, metric="sqeuclidean", log=False) |
| 39 |
wass1d_euc = ot.emd2_1d(u, v, w_u, w_v, metric="euclidean", log=False) |
| 40 |
|
| 41 |
|
| 42 |
np.testing.assert_allclose(wass, wass1d) |
| 43 |
np.testing.assert_allclose(wass, wass1d_emd2) |
| 44 |
|
| 45 |
|
| 46 |
wass_sp = wasserstein_distance(u.reshape((-1,)), v.reshape((-1,)), w_u, w_v) |
| 47 |
np.testing.assert_allclose(wass_sp, wass1d_euc) |
| 48 |
|
| 49 |
|
| 50 |
np.testing.assert_allclose(w_u, G.sum(1)) |
| 51 |
np.testing.assert_allclose(w_v, G.sum(0)) |
| 52 |
|
| 53 |
|
| 54 |
np.testing.assert_raises(ValueError, ot.emd_1d, u, v, w_u, w_v, metric="cosine") |
| 55 |
np.testing.assert_raises(ValueError, ot.emd2_1d, u, v, w_u, w_v, metric="cosine") |
| 56 |
|
| 57 |
|
| 58 |
def test_wasserstein_1d(nx): |
| 59 |
rng = np.random.RandomState(0) |
| 60 |
|
| 61 |
n = 100 |
| 62 |
x = np.linspace(0, 5, n) |
| 63 |
rho_u = np.abs(rng.randn(n)) |
| 64 |
rho_u /= rho_u.sum() |
| 65 |
rho_v = np.abs(rng.randn(n)) |
| 66 |
rho_v /= rho_v.sum() |
| 67 |
|
| 68 |
xb, rho_ub, rho_vb = nx.from_numpy(x, rho_u, rho_v) |
| 69 |
|
| 70 |
|
| 71 |
np.testing.assert_almost_equal( |
| 72 |
wasserstein_1d(xb, xb, rho_ub, rho_vb, p=1), |
| 73 |
wasserstein_distance(x, x, rho_u, rho_v), |
| 74 |
) |
| 75 |
|
| 76 |
|
| 77 |
np.testing.assert_almost_equal(wasserstein_1d(xb, xb + 1, p=2), 1.0) |
| 78 |
|
| 79 |
|
| 80 |
X = np.stack((np.linspace(0, 5, n), np.linspace(0, 5, n) * 10), -1) |
| 81 |
Xb = nx.from_numpy(X) |
| 82 |
res = wasserstein_1d(Xb, Xb, rho_ub, rho_vb, p=2) |
| 83 |
np.testing.assert_almost_equal(100 * res[0], res[1], decimal=4) |
| 84 |
|
| 85 |
|
| 86 |
def test_wasserstein_1d_type_devices(nx): |
| 87 |
rng = np.random.RandomState(0) |
| 88 |
|
| 89 |
n = 10 |
| 90 |
x = np.linspace(0, 5, n) |
| 91 |
rho_u = np.abs(rng.randn(n)) |
| 92 |
rho_u /= rho_u.sum() |
| 93 |
rho_v = np.abs(rng.randn(n)) |
| 94 |
rho_v /= rho_v.sum() |
| 95 |
|
| 96 |
for tp in nx.__type_list__: |
| 97 |
print(nx.dtype_device(tp)) |
| 98 |
|
| 99 |
xb, rho_ub, rho_vb = nx.from_numpy(x, rho_u, rho_v, type_as=tp) |
| 100 |
|
| 101 |
res = wasserstein_1d(xb, xb, rho_ub, rho_vb, p=1) |
| 102 |
|
| 103 |
nx.assert_same_dtype_device(xb, res) |
| 104 |
|
| 105 |
|
| 106 |
@pytest.mark.skipif(not tf, reason="tf not installed") |
| 107 |
def test_wasserstein_1d_device_tf(): |
| 108 |
nx = ot.backend.TensorflowBackend() |
| 109 |
rng = np.random.RandomState(0) |
| 110 |
n = 10 |
| 111 |
x = np.linspace(0, 5, n) |
| 112 |
rho_u = np.abs(rng.randn(n)) |
| 113 |
rho_u /= rho_u.sum() |
| 114 |
rho_v = np.abs(rng.randn(n)) |
| 115 |
rho_v /= rho_v.sum() |
| 116 |
|
| 117 |
|
| 118 |
with tf.device("/CPU:0"): |
| 119 |
xb, rho_ub, rho_vb = nx.from_numpy(x, rho_u, rho_v) |
| 120 |
res = wasserstein_1d(xb, xb, rho_ub, rho_vb, p=1) |
| 121 |
nx.assert_same_dtype_device(xb, res) |
| 122 |
|
| 123 |
if len(tf.config.list_physical_devices("GPU")) > 0: |
| 124 |
|
| 125 |
xb, rho_ub, rho_vb = nx.from_numpy(x, rho_u, rho_v) |
| 126 |
res = wasserstein_1d(xb, xb, rho_ub, rho_vb, p=1) |
| 127 |
nx.assert_same_dtype_device(xb, res) |
| 128 |
assert nx.dtype_device(res)[1].startswith("GPU") |
| 129 |
|
| 130 |
|
| 131 |
def test_emd_1d_emd2_1d(): |
| 132 |
|
| 133 |
n = 20 |
| 134 |
m = 30 |
| 135 |
rng = np.random.RandomState(0) |
| 136 |
u = rng.randn(n, 1) |
| 137 |
v = rng.randn(m, 1) |
| 138 |
|
| 139 |
M = ot.dist(u, v, metric="sqeuclidean") |
| 140 |
|
| 141 |
G, log = ot.emd([], [], M, log=True) |
| 142 |
wass = log["cost"] |
| 143 |
G_1d, log = ot.emd_1d(u, v, [], [], metric="sqeuclidean", log=True) |
| 144 |
wass1d = log["cost"] |
| 145 |
wass1d_emd2 = ot.emd2_1d(u, v, [], [], metric="sqeuclidean", log=False) |
| 146 |
wass1d_euc = ot.emd2_1d(u, v, [], [], metric="euclidean", log=False) |
| 147 |
|
| 148 |
|
| 149 |
np.testing.assert_allclose(wass, wass1d) |
| 150 |
np.testing.assert_allclose(wass, wass1d_emd2) |
| 151 |
|
| 152 |
|
| 153 |
wass_sp = wasserstein_distance(u.reshape((-1,)), v.reshape((-1,))) |
| 154 |
np.testing.assert_allclose(wass_sp, wass1d_euc) |
| 155 |
|
| 156 |
|
| 157 |
np.testing.assert_allclose(np.ones((n,)) / n, G.sum(1)) |
| 158 |
np.testing.assert_allclose(np.ones((m,)) / m, G.sum(0)) |
| 159 |
|
| 160 |
|
| 161 |
np.testing.assert_allclose(G, G_1d, atol=1e-15) |
| 162 |
|
| 163 |
|
| 164 |
u = rng.randn(n, 2) |
| 165 |
v = rng.randn(m, 2) |
| 166 |
with pytest.raises(AssertionError): |
| 167 |
ot.emd_1d(u, v, [], []) |
| 168 |
|
| 169 |
|
| 170 |
def test_emd1d_type_devices(nx): |
| 171 |
rng = np.random.RandomState(0) |
| 172 |
|
| 173 |
n = 10 |
| 174 |
x = np.linspace(0, 5, n) |
| 175 |
rho_u = np.abs(rng.randn(n)) |
| 176 |
rho_u /= rho_u.sum() |
| 177 |
rho_v = np.abs(rng.randn(n)) |
| 178 |
rho_v /= rho_v.sum() |
| 179 |
|
| 180 |
for tp in nx.__type_list__: |
| 181 |
print(nx.dtype_device(tp)) |
| 182 |
|
| 183 |
xb, rho_ub, rho_vb = nx.from_numpy(x, rho_u, rho_v, type_as=tp) |
| 184 |
|
| 185 |
emd = ot.emd_1d(xb, xb, rho_ub, rho_vb) |
| 186 |
emd2 = ot.emd2_1d(xb, xb, rho_ub, rho_vb) |
| 187 |
|
| 188 |
nx.assert_same_dtype_device(xb, emd) |
| 189 |
nx.assert_same_dtype_device(xb, emd2) |
| 190 |
|
| 191 |
|
| 192 |
@pytest.mark.skipif(not tf, reason="tf not installed") |
| 193 |
def test_emd1d_device_tf(): |
| 194 |
nx = ot.backend.TensorflowBackend() |
| 195 |
rng = np.random.RandomState(0) |
| 196 |
n = 10 |
| 197 |
x = np.linspace(0, 5, n) |
| 198 |
rho_u = np.abs(rng.randn(n)) |
| 199 |
rho_u /= rho_u.sum() |
| 200 |
rho_v = np.abs(rng.randn(n)) |
| 201 |
rho_v /= rho_v.sum() |
| 202 |
|
| 203 |
|
| 204 |
with tf.device("/CPU:0"): |
| 205 |
xb, rho_ub, rho_vb = nx.from_numpy(x, rho_u, rho_v) |
| 206 |
emd = ot.emd_1d(xb, xb, rho_ub, rho_vb) |
| 207 |
emd2 = ot.emd2_1d(xb, xb, rho_ub, rho_vb) |
| 208 |
nx.assert_same_dtype_device(xb, emd) |
| 209 |
nx.assert_same_dtype_device(xb, emd2) |
| 210 |
|
| 211 |
if len(tf.config.list_physical_devices("GPU")) > 0: |
| 212 |
|
| 213 |
xb, rho_ub, rho_vb = nx.from_numpy(x, rho_u, rho_v) |
| 214 |
emd = ot.emd_1d(xb, xb, rho_ub, rho_vb) |
| 215 |
emd2 = ot.emd2_1d(xb, xb, rho_ub, rho_vb) |
| 216 |
nx.assert_same_dtype_device(xb, emd) |
| 217 |
nx.assert_same_dtype_device(xb, emd2) |
| 218 |
assert nx.dtype_device(emd)[1].startswith("GPU") |
| 219 |
|
| 220 |
|
| 221 |
def test_wasserstein_1d_circle(): |
| 222 |
|
| 223 |
n = 20 |
| 224 |
m = 30 |
| 225 |
rng = np.random.RandomState(0) |
| 226 |
u = rng.rand( |
| 227 |
n, |
| 228 |
) |
| 229 |
v = rng.rand( |
| 230 |
m, |
| 231 |
) |
| 232 |
|
| 233 |
w_u = rng.uniform(0.0, 1.0, n) |
| 234 |
w_u = w_u / w_u.sum() |
| 235 |
|
| 236 |
w_v = rng.uniform(0.0, 1.0, m) |
| 237 |
w_v = w_v / w_v.sum() |
| 238 |
|
| 239 |
M1 = np.minimum(np.abs(u[:, None] - v[None]), 1 - np.abs(u[:, None] - v[None])) |
| 240 |
|
| 241 |
wass1 = ot.emd2(w_u, w_v, M1) |
| 242 |
|
| 243 |
wass1_bsc = ot.binary_search_circle(u, v, w_u, w_v, p=1) |
| 244 |
w1_circle = ot.wasserstein_circle(u, v, w_u, w_v, p=1) |
| 245 |
|
| 246 |
M2 = M1**2 |
| 247 |
wass2 = ot.emd2(w_u, w_v, M2) |
| 248 |
wass2_bsc = ot.binary_search_circle(u, v, w_u, w_v, p=2) |
| 249 |
w2_circle = ot.wasserstein_circle(u, v, w_u, w_v, p=2) |
| 250 |
|
| 251 |
|
| 252 |
np.testing.assert_allclose(wass1, wass1_bsc) |
| 253 |
np.testing.assert_allclose(wass1, w1_circle, rtol=1e-2) |
| 254 |
np.testing.assert_allclose(wass2, wass2_bsc) |
| 255 |
np.testing.assert_allclose(wass2, w2_circle) |
| 256 |
|
| 257 |
|
| 258 |
@pytest.skip_backend("tf") |
| 259 |
def test_wasserstein1d_circle_devices(nx): |
| 260 |
rng = np.random.RandomState(0) |
| 261 |
|
| 262 |
n = 10 |
| 263 |
x = np.linspace(0, 1, n) |
| 264 |
rho_u = np.abs(rng.randn(n)) |
| 265 |
rho_u /= rho_u.sum() |
| 266 |
rho_v = np.abs(rng.randn(n)) |
| 267 |
rho_v /= rho_v.sum() |
| 268 |
|
| 269 |
for tp in nx.__type_list__: |
| 270 |
print(nx.dtype_device(tp)) |
| 271 |
|
| 272 |
xb, rho_ub, rho_vb = nx.from_numpy(x, rho_u, rho_v, type_as=tp) |
| 273 |
|
| 274 |
w1 = ot.wasserstein_circle(xb, xb, rho_ub, rho_vb, p=1) |
| 275 |
w2_bsc = ot.wasserstein_circle(xb, xb, rho_ub, rho_vb, p=2) |
| 276 |
|
| 277 |
nx.assert_same_dtype_device(xb, w1) |
| 278 |
nx.assert_same_dtype_device(xb, w2_bsc) |
| 279 |
|
| 280 |
|
| 281 |
def test_wasserstein_1d_unif_circle(): |
| 282 |
|
| 283 |
n = 20 |
| 284 |
m = 1000 |
| 285 |
|
| 286 |
rng = np.random.RandomState(0) |
| 287 |
u = rng.rand( |
| 288 |
n, |
| 289 |
) |
| 290 |
v = rng.rand( |
| 291 |
m, |
| 292 |
) |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
w_u = ot.utils.unif(n) |
| 298 |
w_v = ot.utils.unif(m) |
| 299 |
|
| 300 |
M1 = np.minimum(np.abs(u[:, None] - v[None]), 1 - np.abs(u[:, None] - v[None])) |
| 301 |
wass2 = ot.emd2(w_u, w_v, M1**2) |
| 302 |
|
| 303 |
wass2_circle = ot.wasserstein_circle(u, v, w_u, w_v, p=2, eps=1e-15) |
| 304 |
wass2_unif_circle = ot.semidiscrete_wasserstein2_unif_circle(u, w_u) |
| 305 |
|
| 306 |
|
| 307 |
np.testing.assert_allclose(wass2, wass2_unif_circle, atol=1e-2) |
| 308 |
np.testing.assert_allclose(wass2_circle, wass2_unif_circle, atol=1e-2) |
| 309 |
|
| 310 |
|
| 311 |
def test_wasserstein1d_unif_circle_devices(nx): |
| 312 |
rng = np.random.RandomState(0) |
| 313 |
|
| 314 |
n = 10 |
| 315 |
x = np.linspace(0, 1, n) |
| 316 |
rho_u = np.abs(rng.randn(n)) |
| 317 |
rho_u /= rho_u.sum() |
| 318 |
|
| 319 |
for tp in nx.__type_list__: |
| 320 |
print(nx.dtype_device(tp)) |
| 321 |
|
| 322 |
xb, rho_ub = nx.from_numpy(x, rho_u, type_as=tp) |
| 323 |
|
| 324 |
w2 = ot.semidiscrete_wasserstein2_unif_circle(xb, rho_ub) |
| 325 |
|
| 326 |
nx.assert_same_dtype_device(xb, w2) |
| 327 |
|
| 328 |
|
| 329 |
def test_binary_search_circle_log(): |
| 330 |
n = 20 |
| 331 |
m = 30 |
| 332 |
rng = np.random.RandomState(0) |
| 333 |
u = rng.rand( |
| 334 |
n, |
| 335 |
) |
| 336 |
v = rng.rand( |
| 337 |
m, |
| 338 |
) |
| 339 |
|
| 340 |
wass2_bsc, log = ot.binary_search_circle(u, v, p=2, log=True) |
| 341 |
optimal_thetas = log["optimal_theta"] |
| 342 |
|
| 343 |
assert optimal_thetas.shape[0] == 1 |
| 344 |
|
| 345 |
|
| 346 |
def test_wasserstein_circle_bad_shape(): |
| 347 |
n = 20 |
| 348 |
m = 30 |
| 349 |
rng = np.random.RandomState(0) |
| 350 |
u = rng.rand(n, 2) |
| 351 |
v = rng.rand(m, 1) |
| 352 |
|
| 353 |
with pytest.raises(ValueError): |
| 354 |
_ = ot.wasserstein_circle(u, v, p=2) |
| 355 |
|
| 356 |
with pytest.raises(ValueError): |
| 357 |
_ = ot.wasserstein_circle(u, v, p=1) |