Debian Codegraph @

examples/barycenters/plot_generalized_free_support_barycenter.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 """
3 =======================================
4 Generalized Wasserstein Barycenter Demo
5 =======================================
6
7 This example illustrates the computation of Generalized Wasserstein Barycenter
8 as proposed in [42].
9
10
11 [42] Delon, J., Gozlan, N., and Saint-Dizier, A..
12 Generalized Wasserstein barycenters between probability measures living on different subspaces.
13 arXiv preprint arXiv:2105.09755, 2021.
14
15 """
16
17 # Author: Eloi Tanguy <[email protected]>
18 #
19 # License: MIT License
20
21 # sphinx_gallery_thumbnail_number = 2
22
23 import numpy as np
24 import matplotlib.pyplot as plt
25 import matplotlib.pylab as pl
26 import ot
27 import matplotlib.animation as animation
28
29 ########################
30 # Generate and plot data
31 # ----------------------
32
33 # Input measures
34 sub_sample_factor = 8
35 I1 = pl.imread("../../data/redcross.png").astype(np.float64)[
36 ::sub_sample_factor, ::sub_sample_factor, 2
37 ]
38 I2 = pl.imread("../../data/tooth.png").astype(np.float64)[
39 ::-sub_sample_factor, ::sub_sample_factor, 2
40 ]
41 I3 = pl.imread("../../data/heart.png").astype(np.float64)[
42 ::-sub_sample_factor, ::sub_sample_factor, 2
43 ]
44
45 sz = I1.shape[0]
46 UU, VV = np.meshgrid(np.arange(sz), np.arange(sz))
47
48 # Input measure locations in their respective 2D spaces
49 X_list = [np.stack((UU[im == 0], VV[im == 0]), 1) * 1.0 for im in [I1, I2, I3]]
50
51 # Input measure weights
52 a_list = [ot.unif(x.shape[0]) for x in X_list]
53
54 # Projections 3D -> 2D
55 P1 = np.array([[1, 0, 0], [0, 1, 0]])
56 P2 = np.array([[0, 1, 0], [0, 0, 1]])
57 P3 = np.array([[1, 0, 0], [0, 0, 1]])
58 P_list = [P1, P2, P3]
59
60 # Barycenter weights
61 weights = np.array([1 / 3, 1 / 3, 1 / 3])
62
63 # Number of barycenter points to compute
64 n_samples_bary = 150
65
66 # Send the input measures into 3D space for visualization
67 X_visu = [Xi @ Pi for (Xi, Pi) in zip(X_list, P_list)]
68
69 # Plot the input data
70 fig = plt.figure(figsize=(3, 3))
71 axis = fig.add_subplot(1, 1, 1, projection="3d")
72 for Xi in X_visu:
73 axis.scatter(Xi[:, 0], Xi[:, 1], Xi[:, 2], marker="o", alpha=0.6)
74 axis.view_init(azim=45)
75 axis.set_xticks([])
76 axis.set_yticks([])
77 axis.set_zticks([])
78 plt.show()
79
80 #################################
81 # Barycenter computation and plot
82 # -------------------------------
83
84 Y = ot.lp.generalized_free_support_barycenter(X_list, a_list, P_list, n_samples_bary)
85 fig = plt.figure(figsize=(3, 3))
86
87 axis = fig.add_subplot(1, 1, 1, projection="3d")
88 for Xi in X_visu:
89 axis.scatter(Xi[:, 0], Xi[:, 1], Xi[:, 2], marker="o", alpha=0.6)
90 axis.scatter(Y[:, 0], Y[:, 1], Y[:, 2], marker="o", alpha=0.6)
91 axis.view_init(azim=45)
92 axis.set_xticks([])
93 axis.set_yticks([])
94 axis.set_zticks([])
95 plt.show()
96
97
98 #############################
99 # Plotting projection matches
100 # ---------------------------
101
102 fig = plt.figure(figsize=(9, 3))
103
104 ax = fig.add_subplot(1, 3, 1, projection="3d")
105 for Xi in X_visu:
106 ax.scatter(Xi[:, 0], Xi[:, 1], Xi[:, 2], marker="o", alpha=0.6)
107 ax.scatter(Y[:, 0], Y[:, 1], Y[:, 2], marker="o", alpha=0.6)
108 ax.view_init(elev=0, azim=0)
109 ax.set_xticks([])
110 ax.set_yticks([])
111 ax.set_zticks([])
112
113 ax = fig.add_subplot(1, 3, 2, projection="3d")
114 for Xi in X_visu:
115 ax.scatter(Xi[:, 0], Xi[:, 1], Xi[:, 2], marker="o", alpha=0.6)
116 ax.scatter(Y[:, 0], Y[:, 1], Y[:, 2], marker="o", alpha=0.6)
117 ax.view_init(elev=0, azim=90)
118 ax.set_xticks([])
119 ax.set_yticks([])
120 ax.set_zticks([])
121
122 ax = fig.add_subplot(1, 3, 3, projection="3d")
123 for Xi in X_visu:
124 ax.scatter(Xi[:, 0], Xi[:, 1], Xi[:, 2], marker="o", alpha=0.6)
125 ax.scatter(Y[:, 0], Y[:, 1], Y[:, 2], marker="o", alpha=0.6)
126 ax.view_init(elev=90, azim=0)
127 ax.set_xticks([])
128 ax.set_yticks([])
129 ax.set_zticks([])
130
131 plt.tight_layout()
132 plt.show()
133
134 ##############################################
135 # Rotation animation
136 # --------------------------------------------
137
138 fig = plt.figure(figsize=(7, 7))
139 ax = fig.add_subplot(1, 1, 1, projection="3d")
140
141
142 def _init():
143 for Xi in X_visu:
144 ax.scatter(Xi[:, 0], Xi[:, 1], Xi[:, 2], marker="o", alpha=0.6)
145 ax.scatter(Y[:, 0], Y[:, 1], Y[:, 2], marker="o", alpha=0.6)
146 ax.view_init(elev=0, azim=0)
147 ax.set_xticks([])
148 ax.set_yticks([])
149 ax.set_zticks([])
150 return (fig,)
151
152
153 def _update_plot(i):
154 if i < 45:
155 ax.view_init(elev=0, azim=4 * i)
156 else:
157 ax.view_init(elev=i - 45, azim=4 * i)
158 return (fig,)
159
160
161 ani = animation.FuncAnimation(
162 fig,
163 _update_plot,
164 init_func=_init,
165 frames=136,
166 interval=50,
167 blit=True,
168 repeat_delay=2000,
169 )