Check that all functions run without error.
(metric)
| 80 | |
| 81 | @pytest.mark.parametrize("metric", ["sqeuclidean", "euclidean", "minkowski", "kl"]) |
| 82 | def test_metrics(metric): |
| 83 | """Check that all functions run without error.""" |
| 84 | |
| 85 | batchsize = 2 |
| 86 | n = 4 |
| 87 | d = 2 |
| 88 | rng = np.random.RandomState(0) |
| 89 | X = rng.rand(batchsize, n, d) |
| 90 | if metric == "kl": |
| 91 | X = np.abs(X) + 1e-6 |
| 92 | X = X / np.sum(X, axis=-1, keepdims=True) |
| 93 | M = dist_batch(X, X, metric=metric) |
| 94 | is_positive = M >= 0 |
| 95 | np.testing.assert_equal(is_positive.all(), True) |
| 96 | |
| 97 | # Solve batch |
| 98 | res = solve_batch(M, reg=0.1, max_iter=10, tol=1e-5) |
| 99 | |
| 100 | # Solve sample batch |
| 101 | res = solve_sample_batch(X, X, reg=0.1, max_iter=10, tol=1e-5, metric=metric) |
| 102 | |
| 103 | # Compute loss |
| 104 | loss = res.value_linear # loss given by solver |
| 105 | loss2 = loss_linear_batch(M, res.plan) # recompute loss from plan |
| 106 | loss3 = loss_linear_samples_batch( |
| 107 | X, X, res.plan, metric=metric |
| 108 | ) # recompute loss from plan and samples |
| 109 | np.testing.assert_allclose(loss, loss2, atol=1e-5) |
| 110 | np.testing.assert_allclose(loss, loss3, atol=1e-5) |
| 111 | |
| 112 | |
| 113 | @pytest.mark.skipif(not torch, reason="torch not installed") |
nothing calls this directly
no test coverage detected