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

Function test_emd_transport_class

test/test_da.py:488–586  ·  view source on GitHub ↗

test_sinkhorn_transport

(nx)

Source from the content-addressed store, hash-verified

486@pytest.skip_backend("jax")
487@pytest.skip_backend("tf")
488def test_emd_transport_class(nx):
489 """test_sinkhorn_transport"""
490
491 ns = 50
492 nt = 50
493
494 Xs, ys = make_data_classif("3gauss", ns)
495 Xt, yt = make_data_classif("3gauss2", nt)
496
497 Xs, ys, Xt, yt = nx.from_numpy(Xs, ys, Xt, yt)
498
499 otda = ot.da.EMDTransport()
500
501 # test its computed
502 otda.fit(Xs=Xs, Xt=Xt)
503 assert hasattr(otda, "cost_")
504 assert hasattr(otda, "coupling_")
505
506 # test dimensions of coupling
507 assert_equal(otda.cost_.shape, ((Xs.shape[0], Xt.shape[0])))
508 assert not np.any(np.isnan(nx.to_numpy(otda.cost_))), "cost is finite"
509 assert_equal(otda.coupling_.shape, ((Xs.shape[0], Xt.shape[0])))
510 assert np.all(np.isfinite(nx.to_numpy(otda.coupling_))), "coupling is finite"
511
512 # test margin constraints
513 mu_s = unif(ns)
514 mu_t = unif(nt)
515 assert_allclose(
516 nx.to_numpy(nx.sum(otda.coupling_, axis=0)), mu_t, rtol=1e-3, atol=1e-3
517 )
518 assert_allclose(
519 nx.to_numpy(nx.sum(otda.coupling_, axis=1)), mu_s, rtol=1e-3, atol=1e-3
520 )
521
522 # test transform
523 transp_Xs = otda.transform(Xs=Xs)
524 assert_equal(transp_Xs.shape, Xs.shape)
525
526 Xs_new = nx.from_numpy(make_data_classif("3gauss", ns + 1)[0])
527 transp_Xs_new = otda.transform(Xs_new)
528
529 # check that the oos method is working
530 assert_equal(transp_Xs_new.shape, Xs_new.shape)
531
532 # test inverse transform
533 transp_Xt = otda.inverse_transform(Xt=Xt)
534 assert_equal(transp_Xt.shape, Xt.shape)
535
536 # check label propagation
537 transp_yt = otda.transform_labels(ys)
538 assert_equal(transp_yt.shape[0], yt.shape[0])
539 assert_equal(transp_yt.shape[1], len(np.unique(ys)))
540
541 # check inverse label propagation
542 transp_ys = otda.inverse_transform_labels(yt)
543 assert_equal(transp_ys.shape[0], ys.shape[0])
544 assert_equal(transp_ys.shape[1], len(np.unique(yt)))
545

Callers

nothing calls this directly

Calls 15

fitMethod · 0.95
make_data_classifFunction · 0.90
unifFunction · 0.90
from_numpyMethod · 0.80
to_numpyMethod · 0.80
fit_transformMethod · 0.80
anyMethod · 0.45
isnanMethod · 0.45
isfiniteMethod · 0.45
sumMethod · 0.45
transformMethod · 0.45
inverse_transformMethod · 0.45

Tested by

no test coverage detected