test_mapping_transport
(nx, kernel, bias)
| 591 | @pytest.mark.parametrize("kernel", ["linear", "gaussian"]) |
| 592 | @pytest.mark.parametrize("bias", ["unbiased", "biased"]) |
| 593 | def test_mapping_transport_class(nx, kernel, bias): |
| 594 | """test_mapping_transport""" |
| 595 | |
| 596 | ns = 20 |
| 597 | nt = 30 |
| 598 | |
| 599 | Xs, ys = make_data_classif("3gauss", ns) |
| 600 | Xt, yt = make_data_classif("3gauss2", nt) |
| 601 | Xs_new, _ = make_data_classif("3gauss", ns + 1) |
| 602 | |
| 603 | Xs, Xt, Xs_new = nx.from_numpy(Xs, Xt, Xs_new) |
| 604 | |
| 605 | # Mapping tests |
| 606 | bias = bias == "biased" |
| 607 | otda = ot.da.MappingTransport(kernel=kernel, bias=bias) |
| 608 | otda.fit(Xs=Xs, Xt=Xt) |
| 609 | assert hasattr(otda, "coupling_") |
| 610 | assert hasattr(otda, "mapping_") |
| 611 | assert hasattr(otda, "log_") |
| 612 | |
| 613 | assert_equal(otda.coupling_.shape, ((Xs.shape[0], Xt.shape[0]))) |
| 614 | S = Xs.shape[0] if kernel == "gaussian" else Xs.shape[1] # if linear |
| 615 | if bias: |
| 616 | S += 1 |
| 617 | assert_equal(otda.mapping_.shape, ((S, Xt.shape[1]))) |
| 618 | |
| 619 | # test margin constraints |
| 620 | mu_s = unif(ns) |
| 621 | mu_t = unif(nt) |
| 622 | assert_allclose( |
| 623 | nx.to_numpy(nx.sum(otda.coupling_, axis=0)), mu_t, rtol=1e-3, atol=1e-3 |
| 624 | ) |
| 625 | assert_allclose( |
| 626 | nx.to_numpy(nx.sum(otda.coupling_, axis=1)), mu_s, rtol=1e-3, atol=1e-3 |
| 627 | ) |
| 628 | |
| 629 | # test transform |
| 630 | transp_Xs = otda.transform(Xs=Xs) |
| 631 | assert_equal(transp_Xs.shape, Xs.shape) |
| 632 | |
| 633 | transp_Xs_new = otda.transform(Xs_new) |
| 634 | |
| 635 | # check that the oos method is working |
| 636 | assert_equal(transp_Xs_new.shape, Xs_new.shape) |
| 637 | |
| 638 | # check everything runs well with log=True |
| 639 | otda = ot.da.MappingTransport(kernel=kernel, bias=bias, log=True) |
| 640 | otda.fit(Xs=Xs, Xt=Xt) |
| 641 | assert len(otda.log_.keys()) != 0 |
| 642 | |
| 643 | |
| 644 | @pytest.skip_backend("jax") |
nothing calls this directly
no test coverage detected