Skip to content

[MRG] Add assert for emd dimension mismatch #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ot/lp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ def emd(a, b, M, numItermax=100000, log=False, dense=True):
if len(b) == 0:
b = np.ones((M.shape[1],), dtype=np.float64) / M.shape[1]

assert (a.shape[0] == M.shape[0] and b.shape[0] == M.shape[1]), \
"Dimension mismatch, check dimensions of M with a and b"

if dense:
G, cost, u, v, result_code = emd_c(a, b, M, numItermax,dense)
else:
Expand Down Expand Up @@ -226,6 +229,9 @@ def emd2(a, b, M, processes=multiprocessing.cpu_count(),
if len(b) == 0:
b = np.ones((M.shape[1],), dtype=np.float64) / M.shape[1]

assert (a.shape[0] == M.shape[0] and b.shape[0] == M.shape[1]), \
"Dimension mismatch, check dimensions of M with a and b"

if log or return_matrix:
def f(b):
if dense:
Expand Down
16 changes: 16 additions & 0 deletions test/test_ot.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@
import pytest


def test_emd_dimension_mismatch():
# test emd and emd2 for dimension mismatch
n_samples = 100
n_features = 2
rng = np.random.RandomState(0)

x = rng.randn(n_samples, n_features)
a = ot.utils.unif(n_samples + 1)

M = ot.dist(x, x)

np.testing.assert_raises(AssertionError, ot.emd, a, a, M)

np.testing.assert_raises(AssertionError, ot.emd2, a, a, M)


def test_emd_emd2():
# test emd and emd2 for simple identity
n = 100
Expand Down