Skip to content

[MRG] Have Travis actually execute doctests #90

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 13 commits into from
Jul 2, 2019
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
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ before_script: # configure a headless display to test plot generation
# command to install dependencies
install:
- pip install -r requirements.txt
- pip install -U "numpy>=1.14" "scipy<1.3" # for numpy array formatting in doctests + scipy version: otherwise, pymanopt fails, cf <https://github.com/pymanopt/pymanopt/issues/77>
- pip install flake8 pytest "pytest-cov<2.6"
- pip install .
# command to run tests + check syntax style
script:
- python setup.py develop
- flake8 examples/ ot/ test/
- python -m pytest -v test/ --cov=ot
- python -m pytest -v test/ ot/ --doctest-modules --ignore ot/gpu/ --cov=ot
# - py.test ot test
103 changes: 50 additions & 53 deletions ot/bregman.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

def sinkhorn(a, b, M, reg, method='sinkhorn', numItermax=1000,
stopThr=1e-9, verbose=False, log=False, **kwargs):
u"""
r"""
Solve the entropic regularization optimal transport problem and return the OT matrix

The function solves the following optimization problem:
Expand Down Expand Up @@ -73,12 +73,12 @@ def sinkhorn(a, b, M, reg, method='sinkhorn', numItermax=1000,
--------

>>> import ot
>>> a=[.5,.5]
>>> b=[.5,.5]
>>> M=[[0.,1.],[1.,0.]]
>>> ot.sinkhorn(a,b,M,1)
array([[ 0.36552929, 0.13447071],
[ 0.13447071, 0.36552929]])
>>> a=[.5, .5]
>>> b=[.5, .5]
>>> M=[[0., 1.], [1., 0.]]
>>> ot.sinkhorn(a, b, M, 1)
array([[0.36552929, 0.13447071],
[0.13447071, 0.36552929]])


References
Expand Down Expand Up @@ -131,7 +131,7 @@ def sink():

def sinkhorn2(a, b, M, reg, method='sinkhorn', numItermax=1000,
stopThr=1e-9, verbose=False, log=False, **kwargs):
u"""
r"""
Solve the entropic regularization optimal transport problem and return the loss

The function solves the following optimization problem:
Expand Down Expand Up @@ -188,11 +188,11 @@ def sinkhorn2(a, b, M, reg, method='sinkhorn', numItermax=1000,
--------

>>> import ot
>>> a=[.5,.5]
>>> b=[.5,.5]
>>> M=[[0.,1.],[1.,0.]]
>>> ot.sinkhorn2(a,b,M,1)
array([ 0.26894142])
>>> a=[.5, .5]
>>> b=[.5, .5]
>>> M=[[0., 1.], [1., 0.]]
>>> ot.sinkhorn2(a, b, M, 1)
array([0.26894142])



Expand Down Expand Up @@ -248,7 +248,7 @@ def sink():

def sinkhorn_knopp(a, b, M, reg, numItermax=1000,
stopThr=1e-9, verbose=False, log=False, **kwargs):
"""
r"""
Solve the entropic regularization optimal transport problem and return the OT matrix

The function solves the following optimization problem:
Expand Down Expand Up @@ -302,12 +302,12 @@ def sinkhorn_knopp(a, b, M, reg, numItermax=1000,
--------

>>> import ot
>>> a=[.5,.5]
>>> b=[.5,.5]
>>> M=[[0.,1.],[1.,0.]]
>>> ot.sinkhorn(a,b,M,1)
array([[ 0.36552929, 0.13447071],
[ 0.13447071, 0.36552929]])
>>> a=[.5, .5]
>>> b=[.5, .5]
>>> M=[[0., 1.], [1., 0.]]
>>> ot.sinkhorn(a, b, M, 1)
array([[0.36552929, 0.13447071],
[0.13447071, 0.36552929]])


References
Expand Down Expand Up @@ -422,7 +422,7 @@ def sinkhorn_knopp(a, b, M, reg, numItermax=1000,


def greenkhorn(a, b, M, reg, numItermax=10000, stopThr=1e-9, verbose=False, log=False):
"""
r"""
Solve the entropic regularization optimal transport problem and return the OT matrix

The algorithm used is based on the paper
Expand Down Expand Up @@ -481,12 +481,12 @@ def greenkhorn(a, b, M, reg, numItermax=10000, stopThr=1e-9, verbose=False, log=
--------

>>> import ot
>>> a=[.5,.5]
>>> b=[.5,.5]
>>> M=[[0.,1.],[1.,0.]]
>>> ot.bregman.greenkhorn(a,b,M,1)
array([[ 0.36552929, 0.13447071],
[ 0.13447071, 0.36552929]])
>>> a=[.5, .5]
>>> b=[.5, .5]
>>> M=[[0., 1.], [1., 0.]]
>>> ot.bregman.greenkhorn(a, b, M, 1)
array([[0.36552929, 0.13447071],
[0.13447071, 0.36552929]])


References
Expand Down Expand Up @@ -576,7 +576,7 @@ def greenkhorn(a, b, M, reg, numItermax=10000, stopThr=1e-9, verbose=False, log=

def sinkhorn_stabilized(a, b, M, reg, numItermax=1000, tau=1e3, stopThr=1e-9,
warmstart=None, verbose=False, print_period=20, log=False, **kwargs):
"""
r"""
Solve the entropic regularization OT problem with log stabilization

The function solves the following optimization problem:
Expand Down Expand Up @@ -639,8 +639,8 @@ def sinkhorn_stabilized(a, b, M, reg, numItermax=1000, tau=1e3, stopThr=1e-9,
>>> b=[.5,.5]
>>> M=[[0.,1.],[1.,0.]]
>>> ot.bregman.sinkhorn_stabilized(a,b,M,1)
array([[ 0.36552929, 0.13447071],
[ 0.13447071, 0.36552929]])
array([[0.36552929, 0.13447071],
[0.13447071, 0.36552929]])


References
Expand Down Expand Up @@ -796,7 +796,7 @@ def get_Gamma(alpha, beta, u, v):

def sinkhorn_epsilon_scaling(a, b, M, reg, numItermax=100, epsilon0=1e4, numInnerItermax=100,
tau=1e3, stopThr=1e-9, warmstart=None, verbose=False, print_period=10, log=False, **kwargs):
"""
r"""
Solve the entropic regularization optimal transport problem with log
stabilization and epsilon scaling.

Expand Down Expand Up @@ -862,12 +862,12 @@ def sinkhorn_epsilon_scaling(a, b, M, reg, numItermax=100, epsilon0=1e4, numInne
--------

>>> import ot
>>> a=[.5,.5]
>>> b=[.5,.5]
>>> M=[[0.,1.],[1.,0.]]
>>> ot.bregman.sinkhorn_epsilon_scaling(a,b,M,1)
array([[ 0.36552929, 0.13447071],
[ 0.13447071, 0.36552929]])
>>> a=[.5, .5]
>>> b=[.5, .5]
>>> M=[[0., 1.], [1., 0.]]
>>> ot.bregman.sinkhorn_epsilon_scaling(a, b, M, 1)
array([[0.36552929, 0.13447071],
[0.13447071, 0.36552929]])


References
Expand Down Expand Up @@ -989,7 +989,7 @@ def projC(gamma, q):

def barycenter(A, M, reg, weights=None, numItermax=1000,
stopThr=1e-4, verbose=False, log=False):
"""Compute the entropic regularized wasserstein barycenter of distributions A
r"""Compute the entropic regularized wasserstein barycenter of distributions A

The function solves the following optimization problem:

Expand Down Expand Up @@ -1084,7 +1084,7 @@ def barycenter(A, M, reg, weights=None, numItermax=1000,


def convolutional_barycenter2d(A, reg, weights=None, numItermax=10000, stopThr=1e-9, stabThr=1e-30, verbose=False, log=False):
"""Compute the entropic regularized wasserstein barycenter of distributions A
r"""Compute the entropic regularized wasserstein barycenter of distributions A
where A is a collection of 2D images.

The function solves the following optimization problem:
Expand Down Expand Up @@ -1195,7 +1195,7 @@ def K(x):

def unmix(a, D, M, M0, h0, reg, reg0, alpha, numItermax=1000,
stopThr=1e-3, verbose=False, log=False):
"""
r"""
Compute the unmixing of an observation with a given dictionary using Wasserstein distance

The function solve the following optimization problem:
Expand Down Expand Up @@ -1302,7 +1302,7 @@ def unmix(a, D, M, M0, h0, reg, reg0, alpha, numItermax=1000,


def empirical_sinkhorn(X_s, X_t, reg, a=None, b=None, metric='sqeuclidean', numIterMax=10000, stopThr=1e-9, verbose=False, log=False, **kwargs):
'''
r'''
Solve the entropic regularization optimal transport problem and return the
OT matrix from empirical data

Expand Down Expand Up @@ -1360,10 +1360,9 @@ def empirical_sinkhorn(X_s, X_t, reg, a=None, b=None, metric='sqeuclidean', numI
>>> reg = 0.1
>>> X_s = np.reshape(np.arange(n_s), (n_s, 1))
>>> X_t = np.reshape(np.arange(0, n_t), (n_t, 1))
>>> emp_sinkhorn = empirical_sinkhorn(X_s, X_t, reg, verbose=False)
>>> print(emp_sinkhorn)
>>> [[4.99977301e-01 2.26989344e-05]
[2.26989344e-05 4.99977301e-01]]
>>> empirical_sinkhorn(X_s, X_t, reg, verbose=False) # doctest: +NORMALIZE_WHITESPACE
array([[4.99977301e-01, 2.26989344e-05],
[2.26989344e-05, 4.99977301e-01]])


References
Expand Down Expand Up @@ -1392,7 +1391,7 @@ def empirical_sinkhorn(X_s, X_t, reg, a=None, b=None, metric='sqeuclidean', numI


def empirical_sinkhorn2(X_s, X_t, reg, a=None, b=None, metric='sqeuclidean', numIterMax=10000, stopThr=1e-9, verbose=False, log=False, **kwargs):
'''
r'''
Solve the entropic regularization optimal transport problem from empirical
data and return the OT loss

Expand Down Expand Up @@ -1451,9 +1450,8 @@ def empirical_sinkhorn2(X_s, X_t, reg, a=None, b=None, metric='sqeuclidean', num
>>> reg = 0.1
>>> X_s = np.reshape(np.arange(n_s), (n_s, 1))
>>> X_t = np.reshape(np.arange(0, n_t), (n_t, 1))
>>> loss_sinkhorn = empirical_sinkhorn2(X_s, X_t, reg, verbose=False)
>>> print(loss_sinkhorn)
>>> [4.53978687e-05]
>>> empirical_sinkhorn2(X_s, X_t, reg, verbose=False)
array([4.53978687e-05])


References
Expand Down Expand Up @@ -1482,7 +1480,7 @@ def empirical_sinkhorn2(X_s, X_t, reg, a=None, b=None, metric='sqeuclidean', num


def empirical_sinkhorn_divergence(X_s, X_t, reg, a=None, b=None, metric='sqeuclidean', numIterMax=10000, stopThr=1e-9, verbose=False, log=False, **kwargs):
'''
r'''
Compute the sinkhorn divergence loss from empirical data

The function solves the following optimization problems and return the
Expand Down Expand Up @@ -1560,9 +1558,8 @@ def empirical_sinkhorn_divergence(X_s, X_t, reg, a=None, b=None, metric='sqeucli
>>> reg = 0.1
>>> X_s = np.reshape(np.arange(n_s), (n_s, 1))
>>> X_t = np.reshape(np.arange(0, n_t), (n_t, 1))
>>> emp_sinkhorn_div = empirical_sinkhorn_divergence(X_s, X_t, reg)
>>> print(emp_sinkhorn_div)
>>> [2.99977435]
>>> empirical_sinkhorn_divergence(X_s, X_t, reg) # doctest: +ELLIPSIS
array([1.499...])


References
Expand Down
11 changes: 0 additions & 11 deletions ot/gpu/bregman.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,6 @@ def sinkhorn_knopp(a, b, M, reg, numItermax=1000, stopThr=1e-9,
log : dict
log dictionary return only if log==True in parameters

Examples
--------

>>> import ot
>>> a=[.5,.5]
>>> b=[.5,.5]
>>> M=[[0.,1.],[1.,0.]]
>>> ot.sinkhorn(a,b,M,1)
array([[ 0.36552929, 0.13447071],
[ 0.13447071, 0.36552929]])


References
----------
Expand Down
22 changes: 11 additions & 11 deletions ot/lp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


def emd(a, b, M, numItermax=100000, log=False):
"""Solves the Earth Movers distance problem and returns the OT matrix
r"""Solves the Earth Movers distance problem and returns the OT matrix


.. math::
Expand Down Expand Up @@ -76,8 +76,8 @@ def emd(a, b, M, numItermax=100000, log=False):
>>> b=[.5,.5]
>>> M=[[0.,1.],[1.,0.]]
>>> ot.emd(a,b,M)
array([[ 0.5, 0. ],
[ 0. , 0.5]])
array([[0.5, 0. ],
[0. , 0.5]])

References
----------
Expand Down Expand Up @@ -117,7 +117,7 @@ def emd(a, b, M, numItermax=100000, log=False):

def emd2(a, b, M, processes=multiprocessing.cpu_count(),
numItermax=100000, log=False, return_matrix=False):
"""Solves the Earth Movers distance problem and returns the loss
r"""Solves the Earth Movers distance problem and returns the loss

.. math::
\gamma = arg\min_\gamma <\gamma,M>_F
Expand Down Expand Up @@ -315,7 +315,7 @@ def free_support_barycenter(measures_locations, measures_weights, X_init, b=None

def emd_1d(x_a, x_b, a=None, b=None, metric='sqeuclidean', p=1., dense=True,
log=False):
"""Solves the Earth Movers distance problem between 1d measures and returns
r"""Solves the Earth Movers distance problem between 1d measures and returns
the OT matrix


Expand Down Expand Up @@ -381,11 +381,11 @@ def emd_1d(x_a, x_b, a=None, b=None, metric='sqeuclidean', p=1., dense=True,
>>> x_a = [2., 0.]
>>> x_b = [0., 3.]
>>> ot.emd_1d(x_a, x_b, a, b)
array([[0. , 0.5],
[0.5, 0. ]])
array([[0. , 0.5],
[0.5, 0. ]])
>>> ot.emd_1d(x_a, x_b)
array([[0. , 0.5],
[0.5, 0. ]])
array([[0. , 0.5],
[0.5, 0. ]])

References
----------
Expand Down Expand Up @@ -435,7 +435,7 @@ def emd_1d(x_a, x_b, a=None, b=None, metric='sqeuclidean', p=1., dense=True,

def emd2_1d(x_a, x_b, a=None, b=None, metric='sqeuclidean', p=1., dense=True,
log=False):
"""Solves the Earth Movers distance problem between 1d measures and returns
r"""Solves the Earth Movers distance problem between 1d measures and returns
the loss


Expand Down Expand Up @@ -530,7 +530,7 @@ def emd2_1d(x_a, x_b, a=None, b=None, metric='sqeuclidean', p=1., dense=True,


def wasserstein_1d(x_a, x_b, a=None, b=None, p=1.):
"""Solves the p-Wasserstein distance problem between 1d measures and returns
r"""Solves the p-Wasserstein distance problem between 1d measures and returns
the distance


Expand Down
Loading