| 1 |
"""Tests for module mapping""" |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
import numpy as np |
| 7 |
import ot |
| 8 |
import pytest |
| 9 |
from ot.backend import to_numpy |
| 10 |
|
| 11 |
|
| 12 |
try: |
| 13 |
import cvxpy |
| 14 |
|
| 15 |
nocvxpy = False |
| 16 |
except ImportError: |
| 17 |
nocvxpy = True |
| 18 |
|
| 19 |
|
| 20 |
@pytest.mark.skipif(nocvxpy, reason="No CVXPY available") |
| 21 |
def test_ssnb_qcqp_constants(): |
| 22 |
c1, c2, c3 = ot.mapping._ssnb_qcqp_constants(0.5, 1) |
| 23 |
np.testing.assert_almost_equal(c1, 1) |
| 24 |
np.testing.assert_almost_equal(c2, 0.5) |
| 25 |
np.testing.assert_almost_equal(c3, 1) |
| 26 |
|
| 27 |
|
| 28 |
@pytest.mark.skipif(nocvxpy, reason="No CVXPY available") |
| 29 |
def test_nearest_brenier_potential_fit(nx): |
| 30 |
X = nx.ones((2, 2)) |
| 31 |
phi, G, log = ot.mapping.nearest_brenier_potential_fit(X, X, its=3, log=True) |
| 32 |
np.testing.assert_almost_equal( |
| 33 |
to_numpy(G), to_numpy(X) |
| 34 |
) |
| 35 |
|
| 36 |
a = nx.ones(2) / 2 |
| 37 |
ot.mapping.nearest_brenier_potential_fit( |
| 38 |
X, X, X_classes=nx.ones(2), a=a, b=a, its=1, init_method="target" |
| 39 |
) |
| 40 |
|
| 41 |
|
| 42 |
@pytest.mark.skipif(nocvxpy, reason="No CVXPY available") |
| 43 |
def test_brenier_potential_predict_bounds(nx): |
| 44 |
X = nx.ones((2, 2)) |
| 45 |
phi, G = ot.mapping.nearest_brenier_potential_fit(X, X, its=3) |
| 46 |
phi_lu, G_lu, log = ot.mapping.nearest_brenier_potential_predict_bounds( |
| 47 |
X, phi, G, X, log=True |
| 48 |
) |
| 49 |
|
| 50 |
np.testing.assert_almost_equal(to_numpy(G_lu[0]), to_numpy(X)) |
| 51 |
np.testing.assert_almost_equal(to_numpy(G_lu[1]), to_numpy(X)) |
| 52 |
|
| 53 |
ot.mapping.nearest_brenier_potential_predict_bounds( |
| 54 |
X, phi, G, X, X_classes=nx.ones(2), Y_classes=nx.ones(2) |
| 55 |
) |
| 56 |
|
| 57 |
|
| 58 |
def test_joint_OT_mapping(): |
| 59 |
""" |
| 60 |
Complements the tests in test_da, for verbose, log and bias options |
| 61 |
""" |
| 62 |
xs = np.array([[0.1, 0.2], [-0.1, 0.3]]) |
| 63 |
ot.mapping.joint_OT_mapping_kernel(xs, xs, verbose=True) |
| 64 |
ot.mapping.joint_OT_mapping_linear(xs, xs, verbose=True) |
| 65 |
ot.mapping.joint_OT_mapping_kernel(xs, xs, log=True, bias=True) |
| 66 |
ot.mapping.joint_OT_mapping_linear(xs, xs, log=True, bias=True) |