summaryrefslogtreecommitdiff
path: root/docs/source/auto_examples/plot_partial_wass_and_gromov.rst
blob: 2d51210c5671156f23ebbe2746fbbe72acf4bbce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_auto_examples_plot_partial_wass_and_gromov.py>`     to download the full example code
    .. rst-class:: sphx-glr-example-title

    .. _sphx_glr_auto_examples_plot_partial_wass_and_gromov.py:


==================================================
Partial Wasserstein and Gromov-Wasserstein example
==================================================

This example is designed to show how to use the Partial (Gromov-)Wassertsein
distance computation in POT.


.. code-block:: default


    # Author: Laetitia Chapel <laetitia.chapel@irisa.fr>
    # License: MIT License

    # necessary for 3d plot even if not used
    from mpl_toolkits.mplot3d import Axes3D  # noqa
    import scipy as sp
    import numpy as np
    import matplotlib.pylab as pl
    import ot









Sample two 2D Gaussian distributions and plot them
--------------------------------------------------

For demonstration purpose, we sample two Gaussian distributions in 2-d
spaces and add some random noise.


.. code-block:: default



    n_samples = 20  # nb samples (gaussian)
    n_noise = 20  # nb of samples (noise)

    mu = np.array([0, 0])
    cov = np.array([[1, 0], [0, 2]])

    xs = ot.datasets.make_2D_samples_gauss(n_samples, mu, cov)
    xs = np.append(xs, (np.random.rand(n_noise, 2) + 1) * 4).reshape((-1, 2))
    xt = ot.datasets.make_2D_samples_gauss(n_samples, mu, cov)
    xt = np.append(xt, (np.random.rand(n_noise, 2) + 1) * -3).reshape((-1, 2))

    M = sp.spatial.distance.cdist(xs, xt)

    fig = pl.figure()
    ax1 = fig.add_subplot(131)
    ax1.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')
    ax2 = fig.add_subplot(132)
    ax2.scatter(xt[:, 0], xt[:, 1], color='r')
    ax3 = fig.add_subplot(133)
    ax3.imshow(M)
    pl.show()




.. image:: /auto_examples/images/sphx_glr_plot_partial_wass_and_gromov_001.png
    :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    /home/rflamary/PYTHON/POT/examples/plot_partial_wass_and_gromov.py:51: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
      pl.show()




Compute partial Wasserstein plans and distance
----------------------------------------------


.. code-block:: default


    p = ot.unif(n_samples + n_noise)
    q = ot.unif(n_samples + n_noise)

    w0, log0 = ot.partial.partial_wasserstein(p, q, M, m=0.5, log=True)
    w, log = ot.partial.entropic_partial_wasserstein(p, q, M, reg=0.1, m=0.5,
                                                     log=True)

    print('Partial Wasserstein distance (m = 0.5): ' + str(log0['partial_w_dist']))
    print('Entropic partial Wasserstein distance (m = 0.5): ' +
          str(log['partial_w_dist']))

    pl.figure(1, (10, 5))
    pl.subplot(1, 2, 1)
    pl.imshow(w0, cmap='jet')
    pl.title('Partial Wasserstein')
    pl.subplot(1, 2, 2)
    pl.imshow(w, cmap='jet')
    pl.title('Entropic partial Wasserstein')
    pl.show()





.. image:: /auto_examples/images/sphx_glr_plot_partial_wass_and_gromov_002.png
    :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    Partial Wasserstein distance (m = 0.5): 0.507323938973194
    Entropic partial Wasserstein distance (m = 0.5): 0.5205305886057896
    /home/rflamary/PYTHON/POT/examples/plot_partial_wass_and_gromov.py:76: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
      pl.show()




Sample one 2D and 3D Gaussian distributions and plot them
---------------------------------------------------------

The Gromov-Wasserstein distance allows to compute distances with samples that
do not belong to the same metric space. For demonstration purpose, we sample
two Gaussian distributions in 2- and 3-dimensional spaces.


.. code-block:: default


    n_samples = 20  # nb samples
    n_noise = 10  # nb of samples (noise)

    p = ot.unif(n_samples + n_noise)
    q = ot.unif(n_samples + n_noise)

    mu_s = np.array([0, 0])
    cov_s = np.array([[1, 0], [0, 1]])

    mu_t = np.array([0, 0, 0])
    cov_t = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])


    xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s)
    xs = np.concatenate((xs, ((np.random.rand(n_noise, 2) + 1) * 4)), axis=0)
    P = sp.linalg.sqrtm(cov_t)
    xt = np.random.randn(n_samples, 3).dot(P) + mu_t
    xt = np.concatenate((xt, ((np.random.rand(n_noise, 3) + 1) * 10)), axis=0)

    fig = pl.figure()
    ax1 = fig.add_subplot(121)
    ax1.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')
    ax2 = fig.add_subplot(122, projection='3d')
    ax2.scatter(xt[:, 0], xt[:, 1], xt[:, 2], color='r')
    pl.show()





.. image:: /auto_examples/images/sphx_glr_plot_partial_wass_and_gromov_003.png
    :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    /home/rflamary/PYTHON/POT/examples/plot_partial_wass_and_gromov.py:112: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
      pl.show()




Compute partial Gromov-Wasserstein plans and distance
-----------------------------------------------------


.. code-block:: default


    C1 = sp.spatial.distance.cdist(xs, xs)
    C2 = sp.spatial.distance.cdist(xt, xt)

    # transport 100% of the mass
    print('-----m = 1')
    m = 1
    res0, log0 = ot.partial.partial_gromov_wasserstein(C1, C2, p, q, m=m, log=True)
    res, log = ot.partial.entropic_partial_gromov_wasserstein(C1, C2, p, q, 10,
                                                              m=m, log=True)

    print('Wasserstein distance (m = 1): ' + str(log0['partial_gw_dist']))
    print('Entropic Wasserstein distance (m = 1): ' + str(log['partial_gw_dist']))

    pl.figure(1, (10, 5))
    pl.title("mass to be transported m = 1")
    pl.subplot(1, 2, 1)
    pl.imshow(res0, cmap='jet')
    pl.title('Wasserstein')
    pl.subplot(1, 2, 2)
    pl.imshow(res, cmap='jet')
    pl.title('Entropic Wasserstein')
    pl.show()

    # transport 2/3 of the mass
    print('-----m = 2/3')
    m = 2 / 3
    res0, log0 = ot.partial.partial_gromov_wasserstein(C1, C2, p, q, m=m, log=True)
    res, log = ot.partial.entropic_partial_gromov_wasserstein(C1, C2, p, q, 10,
                                                              m=m, log=True)

    print('Partial Wasserstein distance (m = 2/3): ' +
          str(log0['partial_gw_dist']))
    print('Entropic partial Wasserstein distance (m = 2/3): ' +
          str(log['partial_gw_dist']))

    pl.figure(1, (10, 5))
    pl.title("mass to be transported m = 2/3")
    pl.subplot(1, 2, 1)
    pl.imshow(res0, cmap='jet')
    pl.title('Partial Wasserstein')
    pl.subplot(1, 2, 2)
    pl.imshow(res, cmap='jet')
    pl.title('Entropic partial Wasserstein')
    pl.show()



.. image:: /auto_examples/images/sphx_glr_plot_partial_wass_and_gromov_004.png
    :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    -----m = 1
    Wasserstein distance (m = 1): 63.65368600872179
    Entropic Wasserstein distance (m = 1): 65.23659085946916
    /home/rflamary/PYTHON/POT/examples/plot_partial_wass_and_gromov.py:141: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
      pl.show()
    -----m = 2/3
    Partial Wasserstein distance (m = 2/3): 0.23235485397666825
    Entropic partial Wasserstein distance (m = 2/3): 1.4645434781619244
    /home/rflamary/PYTHON/POT/examples/plot_partial_wass_and_gromov.py:157: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance.  In a future version, a new instance will always be created and returned.  Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
      pl.subplot(1, 2, 1)
    /home/rflamary/PYTHON/POT/examples/plot_partial_wass_and_gromov.py:160: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance.  In a future version, a new instance will always be created and returned.  Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
      pl.subplot(1, 2, 2)
    /home/rflamary/PYTHON/POT/examples/plot_partial_wass_and_gromov.py:163: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
      pl.show()





.. rst-class:: sphx-glr-timing

   **Total running time of the script:** ( 0 minutes  1.543 seconds)


.. _sphx_glr_download_auto_examples_plot_partial_wass_and_gromov.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



  .. container:: sphx-glr-download sphx-glr-download-python

     :download:`Download Python source code: plot_partial_wass_and_gromov.py <plot_partial_wass_and_gromov.py>`



  .. container:: sphx-glr-download sphx-glr-download-jupyter

     :download:`Download Jupyter notebook: plot_partial_wass_and_gromov.ipynb <plot_partial_wass_and_gromov.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_