.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/barycenters/plot_generalized_free_support_barycenter.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_barycenters_plot_generalized_free_support_barycenter.py: ======================================= Generalized Wasserstein Barycenter Demo ======================================= This example illustrates the computation of Generalized Wasserstein Barycenter as proposed in [42]. [42] Delon, J., Gozlan, N., and Saint-Dizier, A.. Generalized Wasserstein barycenters between probability measures living on different subspaces. arXiv preprint arXiv:2105.09755, 2021. .. GENERATED FROM PYTHON SOURCE LINES 16-29 .. code-block:: Python # Author: Eloi Tanguy # # License: MIT License # sphinx_gallery_thumbnail_number = 2 import numpy as np import matplotlib.pyplot as plt import matplotlib.pylab as pl import ot import matplotlib.animation as animation .. GENERATED FROM PYTHON SOURCE LINES 30-32 Generate and plot data ---------------------- .. GENERATED FROM PYTHON SOURCE LINES 32-74 .. code-block:: Python # Input measures sub_sample_factor = 8 I1 = pl.imread('../../data/redcross.png').astype(np.float64)[::sub_sample_factor, ::sub_sample_factor, 2] I2 = pl.imread('../../data/tooth.png').astype(np.float64)[::-sub_sample_factor, ::sub_sample_factor, 2] I3 = pl.imread('../../data/heart.png').astype(np.float64)[::-sub_sample_factor, ::sub_sample_factor, 2] sz = I1.shape[0] UU, VV = np.meshgrid(np.arange(sz), np.arange(sz)) # Input measure locations in their respective 2D spaces X_list = [np.stack((UU[im == 0], VV[im == 0]), 1) * 1.0 for im in [I1, I2, I3]] # Input measure weights a_list = [ot.unif(x.shape[0]) for x in X_list] # Projections 3D -> 2D P1 = np.array([[1, 0, 0], [0, 1, 0]]) P2 = np.array([[0, 1, 0], [0, 0, 1]]) P3 = np.array([[1, 0, 0], [0, 0, 1]]) P_list = [P1, P2, P3] # Barycenter weights weights = np.array([1 / 3, 1 / 3, 1 / 3]) # Number of barycenter points to compute n_samples_bary = 150 # Send the input measures into 3D space for visualization X_visu = [Xi @ Pi for (Xi, Pi) in zip(X_list, P_list)] # Plot the input data fig = plt.figure(figsize=(3, 3)) axis = fig.add_subplot(1, 1, 1, projection="3d") for Xi in X_visu: axis.scatter(Xi[:, 0], Xi[:, 1], Xi[:, 2], marker='o', alpha=.6) axis.view_init(azim=45) axis.set_xticks([]) axis.set_yticks([]) axis.set_zticks([]) plt.show() .. image-sg:: /auto_examples/barycenters/images/sphx_glr_plot_generalized_free_support_barycenter_001.png :alt: plot generalized free support barycenter :srcset: /auto_examples/barycenters/images/sphx_glr_plot_generalized_free_support_barycenter_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 75-77 Barycenter computation and plot ------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 77-92 .. code-block:: Python Y = ot.lp.generalized_free_support_barycenter(X_list, a_list, P_list, n_samples_bary) fig = plt.figure(figsize=(3, 3)) axis = fig.add_subplot(1, 1, 1, projection="3d") for Xi in X_visu: axis.scatter(Xi[:, 0], Xi[:, 1], Xi[:, 2], marker='o', alpha=.6) axis.scatter(Y[:, 0], Y[:, 1], Y[:, 2], marker='o', alpha=.6) axis.view_init(azim=45) axis.set_xticks([]) axis.set_yticks([]) axis.set_zticks([]) plt.show() .. image-sg:: /auto_examples/barycenters/images/sphx_glr_plot_generalized_free_support_barycenter_002.png :alt: plot generalized free support barycenter :srcset: /auto_examples/barycenters/images/sphx_glr_plot_generalized_free_support_barycenter_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 93-95 Plotting projection matches --------------------------- .. GENERATED FROM PYTHON SOURCE LINES 95-128 .. code-block:: Python fig = plt.figure(figsize=(9, 3)) ax = fig.add_subplot(1, 3, 1, projection='3d') for Xi in X_visu: ax.scatter(Xi[:, 0], Xi[:, 1], Xi[:, 2], marker='o', alpha=.6) ax.scatter(Y[:, 0], Y[:, 1], Y[:, 2], marker='o', alpha=.6) ax.view_init(elev=0, azim=0) ax.set_xticks([]) ax.set_yticks([]) ax.set_zticks([]) ax = fig.add_subplot(1, 3, 2, projection='3d') for Xi in X_visu: ax.scatter(Xi[:, 0], Xi[:, 1], Xi[:, 2], marker='o', alpha=.6) ax.scatter(Y[:, 0], Y[:, 1], Y[:, 2], marker='o', alpha=.6) ax.view_init(elev=0, azim=90) ax.set_xticks([]) ax.set_yticks([]) ax.set_zticks([]) ax = fig.add_subplot(1, 3, 3, projection='3d') for Xi in X_visu: ax.scatter(Xi[:, 0], Xi[:, 1], Xi[:, 2], marker='o', alpha=.6) ax.scatter(Y[:, 0], Y[:, 1], Y[:, 2], marker='o', alpha=.6) ax.view_init(elev=90, azim=0) ax.set_xticks([]) ax.set_yticks([]) ax.set_zticks([]) plt.tight_layout() plt.show() .. image-sg:: /auto_examples/barycenters/images/sphx_glr_plot_generalized_free_support_barycenter_003.png :alt: plot generalized free support barycenter :srcset: /auto_examples/barycenters/images/sphx_glr_plot_generalized_free_support_barycenter_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 129-131 Rotation animation -------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 131-156 .. code-block:: Python fig = plt.figure(figsize=(7, 7)) ax = fig.add_subplot(1, 1, 1, projection="3d") def _init(): for Xi in X_visu: ax.scatter(Xi[:, 0], Xi[:, 1], Xi[:, 2], marker='o', alpha=.6) ax.scatter(Y[:, 0], Y[:, 1], Y[:, 2], marker='o', alpha=.6) ax.view_init(elev=0, azim=0) ax.set_xticks([]) ax.set_yticks([]) ax.set_zticks([]) return fig, def _update_plot(i): if i < 45: ax.view_init(elev=0, azim=4 * i) else: ax.view_init(elev=i - 45, azim=4 * i) return fig, ani = animation.FuncAnimation(fig, _update_plot, init_func=_init, frames=136, interval=50, blit=True, repeat_delay=2000) .. container:: sphx-glr-animation .. raw:: html
.. rst-class:: sphx-glr-timing **Total running time of the script:** (1 minutes 1.108 seconds) .. _sphx_glr_download_auto_examples_barycenters_plot_generalized_free_support_barycenter.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_generalized_free_support_barycenter.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_generalized_free_support_barycenter.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_