MCPcopy Create free account
hub / github.com/PythonOT/POT / test_mapping_transport_class

Function test_mapping_transport_class

test/test_da.py:593–641  ·  view source on GitHub ↗

test_mapping_transport

(nx, kernel, bias)

Source from the content-addressed store, hash-verified

591@pytest.mark.parametrize("kernel", ["linear", "gaussian"])
592@pytest.mark.parametrize("bias", ["unbiased", "biased"])
593def 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")

Callers

nothing calls this directly

Calls 7

fitMethod · 0.95
transformMethod · 0.95
make_data_classifFunction · 0.90
unifFunction · 0.90
from_numpyMethod · 0.80
to_numpyMethod · 0.80
sumMethod · 0.45

Tested by

no test coverage detected