Certain combinations of 2D sample lengths raise error in emd calculation
Describe the bug
I want to calculate the EMD of two 2D sample distribution of different lengths. The same code either works fine or randomly raises a UserWarning warning. Certain combinations of different sample lengths are more likely to cause this error.
To Reproduce
- Generate two 2D samples of different lengths.
- Calculate the cost matrix
M2, i.e. the (squared) distances between each pair of the two samples. - Define
a, bto be uniform selections of these samples. - Calculate their EMD via
ot.emd(a,b,M2) - Repeat steps 1.-4. until the output results in the zero-matrix.
:warning: Warning Message :warning:
/Users/markusyoussef/Desktop/git/supplements/venv/graco/lib/python3.7/site-packages/ot/lp/__init__.py:276: UserWarning: Problem infeasible. Check that a and b are in the simplex
result_code_string = check_result(result_code)
Code sample
from scipy.spatial.distance import cdist
import ot
import numpy as np
for i in range(100):
xs = np.random.uniform(size=[5,2])
xt = np.random.uniform(size=[960,2])
M = cdist(xs,xt)
M2 = M**2
a = np.ones(len(xs))/len(xs)
b = np.ones(len(xt))/len(xt)
F = ot.emd(a,b, M2)
assert np.isclose(np.sum(F), 1)
Expected behavior
Nothing, the assertion should pass in all 100 cases.
Environment
- OS: MacOS
- Python version: 3.7.4
- How was POT installed:
pip
Output of the following code snippet:
import platform; print(platform.platform())
import sys; print("Python", sys.version)
import numpy; print("NumPy", numpy.__version__)
import scipy; print("SciPy", scipy.__version__)
import ot; print("POT", ot.__version__)
is
Darwin-19.4.0-x86_64-i386-64bit
Python 3.7.4 (default, Oct 12 2019, 18:55:28)
[Clang 11.0.0 (clang-1100.0.33.8)]
NumPy 1.18.2
SciPy 1.4.1
POT 0.7.0
I can reproduce this bug, it's odd as it passes with POT 0.6.0. It must have been some change since then that causes this
I downgraded POT now to 0.6.0, it still doesn't work for me.
Darwin-19.5.0-x86_64-i386-64bit
Python 3.7.4 (default, Oct 12 2019, 18:55:28)
[Clang 11.0.0 (clang-1100.0.33.8)]
NumPy 1.18.2
SciPy 1.4.1
POT 0.6.0
Hello, I cannot reproduce the bug on linux so this is aan issue specific to MacOS.
@hichamjanati can you look into it (after neurips deadline of course) ?
It looks like the bug is only occurring when one sample is considerably longer than the other. I have a temporary solution for now where I just double the length of the shorter sample until it passes the assertion. The following code works for me:
from scipy.spatial.distance import cdist
import ot
import numpy as np
for i in range(100):
xs = np.random.uniform(size=[5,2])
xt = np.random.uniform(size=[960,2])
M = cdist(xs,xt)
M2 = M**2
for j in range(5):
a = np.ones(len(xs))/len(xs)
b = np.ones(len(xt))/len(xt)
F = ot.emd(a,b, M2)
if np.isclose(np.sum(F), 1):
break
else:
xs = np.vstack([xs,xs])
M = np.vstack([M ,M ])
M2 = np.vstack([M2,M2])
assert np.isclose(np.sum(F), 1)
When the assertion does not pass, it also raises a warning prior to that: check that a and b are in the simplex. I believe we have encountered this before where the problem was inputs in 32 bits vs checks in 64 bits, but it seems that everything is in float64 here ..
Closing this issue, still not reproducible and we now raise an error when the mass does not match.