POT icon indicating copy to clipboard operation
POT copied to clipboard

Certain combinations of 2D sample lengths raise error in emd calculation

Open ClusterDuck123 opened this issue 6 years ago • 6 comments

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

  1. Generate two 2D samples of different lengths.
  2. Calculate the cost matrix M2, i.e. the (squared) distances between each pair of the two samples.
  3. Define a, b to be uniform selections of these samples.
  4. Calculate their EMD via ot.emd(a,b,M2)
  5. 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

ClusterDuck123 avatar May 29 '20 22:05 ClusterDuck123

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

hichamjanati avatar May 30 '20 03:05 hichamjanati

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

ClusterDuck123 avatar Jun 02 '20 14:06 ClusterDuck123

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) ?

rflamary avatar Jun 02 '20 16:06 rflamary

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)

ClusterDuck123 avatar Jun 04 '20 00:06 ClusterDuck123

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 ..

hichamjanati avatar Jun 08 '20 14:06 hichamjanati

Closing this issue, still not reproducible and we now raise an error when the mass does not match.

rflamary avatar Aug 13 '25 14:08 rflamary