Skip to content

Commit 27032b6

Browse files
authored
Merge pull request #42 from rflamary/linear_mapping
Linear mapping + tests
2 parents 1ff3586 + 0496e2b commit 27032b6

File tree

15 files changed

+1510
-52
lines changed

15 files changed

+1510
-52
lines changed

Makefile

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22

3-
PYTHON=python
3+
PYTHON=python3
44

55
help :
66
@echo "The following make targets are available:"
@@ -41,21 +41,26 @@ pep8 :
4141
flake8 examples/ ot/ test/
4242

4343
test : FORCE pep8
44-
python -m py.test -v test/ --cov=ot --cov-report html:cov_html
44+
$(PYTHON) -m pytest -v test/ --cov=ot --cov-report html:cov_html
4545

4646
pytest : FORCE
47-
python -m py.test -v test/ --cov=ot
47+
$(PYTHON) -m py.test -v test/ --cov=ot
4848

4949
uploadpypi :
5050
#python setup.py register
51-
python setup.py sdist upload -r pypi
51+
$(PYTHON) setup.py sdist upload -r pypi
5252

5353
rdoc :
5454
pandoc --from=markdown --to=rst --output=docs/source/readme.rst README.md
5555

5656

5757
notebook :
5858
ipython notebook --matplotlib=inline --notebook-dir=notebooks/
59+
60+
autopep8 :
61+
autopep8 -ir test ot examples
5962

63+
aautopep8 :
64+
autopep8 -air test ot examples
6065

6166
FORCE :

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ This open source Python library provide several solvers for optimization problem
1313

1414
It provides the following solvers:
1515

16-
* OT solver for the linear program/ Earth Movers Distance [1].
16+
* OT Network Flow solver for the linear program/ Earth Movers Distance [1].
1717
* Entropic regularization OT solver with Sinkhorn Knopp Algorithm [2] and stabilized version [9][10] with optional GPU implementation (required cudamat).
1818
* Bregman projections for Wasserstein barycenter [3] and unmixing [4].
1919
* Optimal transport for domain adaptation with group lasso regularization [5]
2020
* Conditional gradient [6] and Generalized conditional gradient for regularized OT [7].
21-
* Joint OT matrix and mapping estimation [8].
21+
* Linear OT [14] and Joint OT matrix and mapping estimation [8].
2222
* Wasserstein Discriminant Analysis [11] (requires autograd + pymanopt).
23-
* Gromov-Wasserstein distances and barycenters [12]
23+
* Gromov-Wasserstein distances and barycenters ([13] and regularized [12])
2424

2525
Some demonstrations (both in Python and Jupyter Notebook format) are available in the examples folder.
2626

@@ -195,14 +195,18 @@ You can also post bug reports and feature requests in Github issues. Make sure t
195195

196196
[7] Rakotomamonjy, A., Flamary, R., & Courty, N. (2015). [Generalized conditional gradient: analysis of convergence and applications](https://arxiv.org/pdf/1510.06567.pdf). arXiv preprint arXiv:1510.06567.
197197

198-
[8] M. Perrot, N. Courty, R. Flamary, A. Habrard, [Mapping estimation for discrete optimal transport](http://remi.flamary.com/biblio/perrot2016mapping.pdf), Neural Information Processing Systems (NIPS), 2016.
198+
[8] M. Perrot, N. Courty, R. Flamary, A. Habrard (2016), [Mapping estimation for discrete optimal transport](http://remi.flamary.com/biblio/perrot2016mapping.pdf), Neural Information Processing Systems (NIPS).
199199

200200
[9] Schmitzer, B. (2016). [Stabilized Sparse Scaling Algorithms for Entropy Regularized Transport Problems](https://arxiv.org/pdf/1610.06519.pdf). arXiv preprint arXiv:1610.06519.
201201

202202
[10] Chizat, L., Peyré, G., Schmitzer, B., & Vialard, F. X. (2016). [Scaling algorithms for unbalanced transport problems](https://arxiv.org/pdf/1607.05816.pdf). arXiv preprint arXiv:1607.05816.
203203

204204
[11] Flamary, R., Cuturi, M., Courty, N., & Rakotomamonjy, A. (2016). [Wasserstein Discriminant Analysis](https://arxiv.org/pdf/1608.08063.pdf). arXiv preprint arXiv:1608.08063.
205205

206-
[12] Gabriel Peyré, Marco Cuturi, and Justin Solomon, [Gromov-Wasserstein averaging of kernel and distance matrices](http://proceedings.mlr.press/v48/peyre16.html) International Conference on Machine Learning (ICML). 2016.
206+
[12] Gabriel Peyré, Marco Cuturi, and Justin Solomon (2016), [Gromov-Wasserstein averaging of kernel and distance matrices](http://proceedings.mlr.press/v48/peyre16.html) International Conference on Machine Learning (ICML).
207207

208-
[13] Mémoli, Facundo. [Gromov–Wasserstein distances and the metric approach to object matching](https://media.adelaide.edu.au/acvt/Publications/2011/2011-Gromov%E2%80%93Wasserstein%20Distances%20and%20the%20Metric%20Approach%20to%20Object%20Matching.pdf). Foundations of computational mathematics 11.4 (2011): 417-487.
208+
[13] Mémoli, Facundo (2011). [Gromov–Wasserstein distances and the metric approach to object matching](https://media.adelaide.edu.au/acvt/Publications/2011/2011-Gromov%E2%80%93Wasserstein%20Distances%20and%20the%20Metric%20Approach%20to%20Object%20Matching.pdf). Foundations of computational mathematics 11.4 : 417-487.
209+
210+
[14] Knott, M. and Smith, C. S. (1984).[On the optimal mapping of distributions](https://link.springer.com/article/10.1007/BF00934745), Journal of Optimization Theory and Applications Vol 43.
211+
212+
[15] Peyré, G., & Cuturi, M. (2018). [Computational Optimal Transport](https://arxiv.org/pdf/1803.00567.pdf) .

docs/source/readme.rst

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
POT: Python Optimal Transport
22
=============================
33

4-
|PyPI version| |Build Status| |Documentation Status| |Anaconda Cloud|
5-
|License| |Anaconda downloads|
4+
|PyPI version| |Anaconda Cloud| |Build Status| |Documentation Status|
5+
|Anaconda downloads| |License|
66

77
This open source Python library provide several solvers for optimization
88
problems related to Optimal Transport for signal, image processing and
99
machine learning.
1010

1111
It provides the following solvers:
1212

13-
- OT solver for the linear program/ Earth Movers Distance [1].
13+
- OT Network Flow solver for the linear program/ Earth Movers Distance
14+
[1].
1415
- Entropic regularization OT solver with Sinkhorn Knopp Algorithm [2]
1516
and stabilized version [9][10] with optional GPU implementation
1617
(required cudamat).
@@ -19,10 +20,11 @@ It provides the following solvers:
1920
regularization [5]
2021
- Conditional gradient [6] and Generalized conditional gradient for
2122
regularized OT [7].
22-
- Joint OT matrix and mapping estimation [8].
23+
- Linear OT [14] and Joint OT matrix and mapping estimation [8].
2324
- Wasserstein Discriminant Analysis [11] (requires autograd +
2425
pymanopt).
25-
- Gromov-Wasserstein distances and barycenters [12]
26+
- Gromov-Wasserstein distances and barycenters ([13] and regularized
27+
[12])
2628

2729
Some demonstrations (both in Python and Jupyter Notebook format) are
2830
available in the examples folder.
@@ -281,10 +283,10 @@ conditional gradient: analysis of convergence and
281283
applications <https://arxiv.org/pdf/1510.06567.pdf>`__. arXiv preprint
282284
arXiv:1510.06567.
283285

284-
[8] M. Perrot, N. Courty, R. Flamary, A. Habrard, `Mapping estimation
285-
for discrete optimal
286+
[8] M. Perrot, N. Courty, R. Flamary, A. Habrard (2016), `Mapping
287+
estimation for discrete optimal
286288
transport <http://remi.flamary.com/biblio/perrot2016mapping.pdf>`__,
287-
Neural Information Processing Systems (NIPS), 2016.
289+
Neural Information Processing Systems (NIPS).
288290

289291
[9] Schmitzer, B. (2016). `Stabilized Sparse Scaling Algorithms for
290292
Entropy Regularized Transport
@@ -301,25 +303,32 @@ arXiv:1607.05816.
301303
Analysis <https://arxiv.org/pdf/1608.08063.pdf>`__. arXiv preprint
302304
arXiv:1608.08063.
303305

304-
[12] Gabriel Peyré, Marco Cuturi, and Justin Solomon,
306+
[12] Gabriel Peyré, Marco Cuturi, and Justin Solomon (2016),
305307
`Gromov-Wasserstein averaging of kernel and distance
306308
matrices <http://proceedings.mlr.press/v48/peyre16.html>`__
307-
International Conference on Machine Learning (ICML). 2016.
309+
International Conference on Machine Learning (ICML).
308310

309-
[13] Mémoli, Facundo. `Gromov–Wasserstein distances and the metric
310-
approach to object
311+
[13] Mémoli, Facundo (2011). `Gromov–Wasserstein distances and the
312+
metric approach to object
311313
matching <https://media.adelaide.edu.au/acvt/Publications/2011/2011-Gromov%E2%80%93Wasserstein%20Distances%20and%20the%20Metric%20Approach%20to%20Object%20Matching.pdf>`__.
312-
Foundations of computational mathematics 11.4 (2011): 417-487.
314+
Foundations of computational mathematics 11.4 : 417-487.
315+
316+
[14] Knott, M. and Smith, C. S. (1984).`On the optimal mapping of
317+
distributions <https://link.springer.com/article/10.1007/BF00934745>`__,
318+
Journal of Optimization Theory and Applications Vol 43.
319+
320+
[15] Peyré, G., & Cuturi, M. (2018). `Computational Optimal
321+
Transport <https://arxiv.org/pdf/1803.00567.pdf>`__ .
313322

314323
.. |PyPI version| image:: https://badge.fury.io/py/POT.svg
315324
:target: https://badge.fury.io/py/POT
325+
.. |Anaconda Cloud| image:: https://anaconda.org/conda-forge/pot/badges/version.svg
326+
:target: https://anaconda.org/conda-forge/pot
316327
.. |Build Status| image:: https://travis-ci.org/rflamary/POT.svg?branch=master
317328
:target: https://travis-ci.org/rflamary/POT
318329
.. |Documentation Status| image:: https://readthedocs.org/projects/pot/badge/?version=latest
319330
:target: http://pot.readthedocs.io/en/latest/?badge=latest
320-
.. |Anaconda Cloud| image:: https://anaconda.org/conda-forge/pot/badges/version.svg
331+
.. |Anaconda downloads| image:: https://anaconda.org/conda-forge/pot/badges/downloads.svg
321332
:target: https://anaconda.org/conda-forge/pot
322333
.. |License| image:: https://anaconda.org/conda-forge/pot/badges/license.svg
323334
:target: https://github.com/rflamary/POT/blob/master/LICENSE
324-
.. |Anaconda downloads| image:: https://anaconda.org/conda-forge/pot/badges/downloads.svg
325-
:target: https://anaconda.org/conda-forge/pot

examples/plot_otda_linear_mapping.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""
4+
Created on Tue Mar 20 14:31:15 2018
5+
6+
@author: rflamary
7+
"""
8+
9+
import numpy as np
10+
import pylab as pl
11+
import ot
12+
13+
##############################################################################
14+
# Generate data
15+
# -------------
16+
17+
n = 1000
18+
d = 2
19+
sigma = .1
20+
21+
# source samples
22+
angles = np.random.rand(n, 1) * 2 * np.pi
23+
xs = np.concatenate((np.sin(angles), np.cos(angles)),
24+
axis=1) + sigma * np.random.randn(n, 2)
25+
xs[:n // 2, 1] += 2
26+
27+
28+
# target samples
29+
anglet = np.random.rand(n, 1) * 2 * np.pi
30+
xt = np.concatenate((np.sin(anglet), np.cos(anglet)),
31+
axis=1) + sigma * np.random.randn(n, 2)
32+
xt[:n // 2, 1] += 2
33+
34+
35+
A = np.array([[1.5, .7], [.7, 1.5]])
36+
b = np.array([[4, 2]])
37+
xt = xt.dot(A) + b
38+
39+
##############################################################################
40+
# Plot data
41+
# ---------
42+
43+
pl.figure(1, (5, 5))
44+
pl.plot(xs[:, 0], xs[:, 1], '+')
45+
pl.plot(xt[:, 0], xt[:, 1], 'o')
46+
47+
48+
##############################################################################
49+
# Estimate linear mapping and transport
50+
# -------------------------------------
51+
52+
Ae, be = ot.da.OT_mapping_linear(xs, xt)
53+
54+
xst = xs.dot(Ae) + be
55+
56+
57+
##############################################################################
58+
# Plot transported samples
59+
# ------------------------
60+
61+
pl.figure(1, (5, 5))
62+
pl.clf()
63+
pl.plot(xs[:, 0], xs[:, 1], '+')
64+
pl.plot(xt[:, 0], xt[:, 1], 'o')
65+
pl.plot(xst[:, 0], xst[:, 1], '+')
66+
67+
pl.show()
68+
69+
##############################################################################
70+
# Load image data
71+
# ---------------
72+
73+
74+
def im2mat(I):
75+
"""Converts and image to matrix (one pixel per line)"""
76+
return I.reshape((I.shape[0] * I.shape[1], I.shape[2]))
77+
78+
79+
def mat2im(X, shape):
80+
"""Converts back a matrix to an image"""
81+
return X.reshape(shape)
82+
83+
84+
def minmax(I):
85+
return np.clip(I, 0, 1)
86+
87+
88+
# Loading images
89+
I1 = pl.imread('../data/ocean_day.jpg').astype(np.float64) / 256
90+
I2 = pl.imread('../data/ocean_sunset.jpg').astype(np.float64) / 256
91+
92+
93+
X1 = im2mat(I1)
94+
X2 = im2mat(I2)
95+
96+
##############################################################################
97+
# Estimate mapping and adapt
98+
# ----------------------------
99+
100+
mapping = ot.da.LinearTransport()
101+
102+
mapping.fit(Xs=X1, Xt=X2)
103+
104+
105+
xst = mapping.transform(Xs=X1)
106+
xts = mapping.inverse_transform(Xt=X2)
107+
108+
I1t = minmax(mat2im(xst, I1.shape))
109+
I2t = minmax(mat2im(xts, I2.shape))
110+
111+
# %%
112+
113+
114+
##############################################################################
115+
# Plot transformed images
116+
# -----------------------
117+
118+
pl.figure(2, figsize=(10, 7))
119+
120+
pl.subplot(2, 2, 1)
121+
pl.imshow(I1)
122+
pl.axis('off')
123+
pl.title('Im. 1')
124+
125+
pl.subplot(2, 2, 2)
126+
pl.imshow(I2)
127+
pl.axis('off')
128+
pl.title('Im. 2')
129+
130+
pl.subplot(2, 2, 3)
131+
pl.imshow(I1t)
132+
pl.axis('off')
133+
pl.title('Mapping Im. 1')
134+
135+
pl.subplot(2, 2, 4)
136+
pl.imshow(I2t)
137+
pl.axis('off')
138+
pl.title('Inverse mapping Im. 2')

0 commit comments

Comments
 (0)