forked from PyLops/pylops
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplot_stacking.py
More file actions
executable file
·367 lines (319 loc) · 11.8 KB
/
plot_stacking.py
File metadata and controls
executable file
·367 lines (319 loc) · 11.8 KB
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
"""
Operators concatenation
=======================
This example shows how to use 'stacking' operators such as
:py:class:`pylops.VStack`, :py:class:`pylops.HStack`,
:py:class:`pylops.Block`, :py:class:`pylops.BlockDiag`,
and :py:class:`pylops.Kronecker`.
These operators allow for different combinations of multiple linear operators
in a single operator. Such functionalities are used within PyLops as the basis
for the creation of complex operators as well as in the definition of various
types of optimization problems with regularization or preconditioning.
Some of this operators naturally lend to embarassingly parallel computations.
Within PyLops we leverage the
`Multiprocessing <https://docs.python.org/3/library/multiprocessing.html>`_
module to run multiple processes at the same time evaluating a subset of the
operators involved in one of the stacking operations.
"""
import matplotlib.pyplot as plt
import numpy as np
import pylops
plt.close("all")
###############################################################################
# Let's start by defining two second derivatives :py:class:`pylops.SecondDerivative`
# that we will be using in this example.
D2hop = pylops.SecondDerivative(dims=(11, 21), axis=1, dtype="float32")
D2vop = pylops.SecondDerivative(dims=(11, 21), axis=0, dtype="float32")
###############################################################################
# Chaining of operators represents the simplest concatenation that
# can be performed between two or more linear operators.
# This can be easily achieved using the ``*`` symbol
#
# .. math::
# \mathbf{D_{cat}}= \mathbf{D_v} \mathbf{D_h}
Nv, Nh = 11, 21
X = np.zeros((Nv, Nh))
X[int(Nv / 2), int(Nh / 2)] = 1
D2op = D2vop * D2hop
Y = D2op * X
fig, axs = plt.subplots(1, 2, figsize=(10, 3))
fig.suptitle("Chain", fontsize=14, fontweight="bold", y=0.95)
im = axs[0].imshow(X, interpolation="nearest")
axs[0].axis("tight")
axs[0].set_title(r"$x$")
plt.colorbar(im, ax=axs[0])
im = axs[1].imshow(Y, interpolation="nearest")
axs[1].axis("tight")
axs[1].set_title(r"$y=(D_x+D_y) x$")
plt.colorbar(im, ax=axs[1])
plt.tight_layout()
plt.subplots_adjust(top=0.8)
###############################################################################
# We now want to *vertically stack* three operators
#
# .. math::
# \mathbf{D_{Vstack}} =
# \begin{bmatrix}
# \mathbf{D_v} \\
# \mathbf{D_h}
# \end{bmatrix}, \qquad
# \mathbf{y} =
# \begin{bmatrix}
# \mathbf{D_v}\mathbf{x} \\
# \mathbf{D_h}\mathbf{x}
# \end{bmatrix}
Nv, Nh = 11, 21
X = np.zeros((Nv, Nh))
X[int(Nv / 2), int(Nh / 2)] = 1
Dstack = pylops.VStack([D2vop, D2hop])
Y = np.reshape(Dstack * X.ravel(), (Nv * 2, Nh))
fig, axs = plt.subplots(1, 2, figsize=(10, 3))
fig.suptitle("Vertical stacking", fontsize=14, fontweight="bold", y=0.95)
im = axs[0].imshow(X, interpolation="nearest")
axs[0].axis("tight")
axs[0].set_title(r"$x$")
plt.colorbar(im, ax=axs[0])
im = axs[1].imshow(Y, interpolation="nearest")
axs[1].axis("tight")
axs[1].set_title(r"$y$")
plt.colorbar(im, ax=axs[1])
plt.tight_layout()
plt.subplots_adjust(top=0.8)
###############################################################################
# If one now wants to have one or more null operators in the stack, it is
# reccomended to use the :py:class:`pylops.Zero` operator (instead of, for example,
# a :py:class:`pylops.MatrixMult` filled with a zero matrix); under the hood, a
# :py:class:`pylops.Zero` operator will be simply by-passed both in the forward and
# adjoint steps.
#
# .. math::
# \mathbf{D_{Vstack}} =
# \begin{bmatrix}
# \mathbf{D_v} \\
# \mathbf{0} \\
# \mathbf{D_h}
# \end{bmatrix}, \qquad
# \mathbf{y} =
# \begin{bmatrix}
# \mathbf{D_v}\mathbf{x} \\
# \mathbf{0} \\
# \mathbf{D_h}\mathbf{x}
# \end{bmatrix}
#
# Note that this feature will become particularly handy when defining
# :py:class:`pylops.Block` operators with large zero blocks.
# With MatrixMult operator filled with zeros
Zop = pylops.MatrixMult(np.zeros((Nv * Nh, Nv * Nh)))
Dstack = pylops.VStack([D2vop, Zop, D2hop])
Y = np.reshape(Dstack * X.ravel(), (Nv * 3, Nh))
# With Zero operator
Zop = pylops.Zero(Nv * Nh)
D1stack = pylops.VStack([D2vop, Zop, D2hop])
Y1 = np.reshape(D1stack * X.ravel(), (Nv * 3, Nh))
print("Y == Y1:", np.allclose(Y, Y1))
###############################################################################
# Similarly we can now *horizontally stack* three operators
#
# .. math::
# \mathbf{D_{Hstack}} =
# \begin{bmatrix}
# \mathbf{D_v} & 0.5*\mathbf{D_v} & -1*\mathbf{D_h}
# \end{bmatrix}, \qquad
# \mathbf{y} =
# \mathbf{D_v}\mathbf{x}_1 + 0.5*\mathbf{D_v}\mathbf{x}_2 -
# \mathbf{D_h}\mathbf{x}_3
Nv, Nh = 11, 21
X = np.zeros((Nv * 3, Nh))
X[int(Nv / 2), int(Nh / 2)] = 1
X[int(Nv / 2) + Nv, int(Nh / 2)] = 1
X[int(Nv / 2) + 2 * Nv, int(Nh / 2)] = 1
Hstackop = pylops.HStack([D2vop, 0.5 * D2vop, -1 * D2hop])
Y = np.reshape(Hstackop * X.ravel(), (Nv, Nh))
fig, axs = plt.subplots(1, 2, figsize=(10, 3))
fig.suptitle("Horizontal stacking", fontsize=14, fontweight="bold", y=0.95)
im = axs[0].imshow(X, interpolation="nearest")
axs[0].axis("tight")
axs[0].set_title(r"$x$")
plt.colorbar(im, ax=axs[0])
im = axs[1].imshow(Y, interpolation="nearest")
axs[1].axis("tight")
axs[1].set_title(r"$y$")
plt.colorbar(im, ax=axs[1])
plt.tight_layout()
plt.subplots_adjust(top=0.8)
###############################################################################
# We can even stack them both *horizontally* and *vertically* such that we
# create a *block* operator
#
# .. math::
# \mathbf{D_{Block}} =
# \begin{bmatrix}
# \mathbf{D_v} & 0.5*\mathbf{D_v} & -1*\mathbf{D_h} \\
# \mathbf{D_h} & 2*\mathbf{D_h} & \mathbf{D_v} \\
# \end{bmatrix}, \qquad
# \mathbf{y} =
# \begin{bmatrix}
# \mathbf{D_v} \mathbf{x_1} + 0.5*\mathbf{D_v} \mathbf{x_2} -
# \mathbf{D_h} \mathbf{x_3} \\
# \mathbf{D_h} \mathbf{x_1} + 2*\mathbf{D_h} \mathbf{x_2} +
# \mathbf{D_v} \mathbf{x_3}
# \end{bmatrix}
Bop = pylops.Block([[D2vop, 0.5 * D2vop, -1 * D2hop], [D2hop, 2 * D2hop, D2vop]])
Y = np.reshape(Bop * X.ravel(), (2 * Nv, Nh))
fig, axs = plt.subplots(1, 2, figsize=(10, 3))
fig.suptitle("Block", fontsize=14, fontweight="bold", y=0.95)
im = axs[0].imshow(X, interpolation="nearest")
axs[0].axis("tight")
axs[0].set_title(r"$x$")
plt.colorbar(im, ax=axs[0])
im = axs[1].imshow(Y, interpolation="nearest")
axs[1].axis("tight")
axs[1].set_title(r"$y$")
plt.colorbar(im, ax=axs[1])
plt.tight_layout()
plt.subplots_adjust(top=0.8)
###############################################################################
# And here with some Zero blocks
#
# .. math::
# \mathbf{D_{Block}} =
# \begin{bmatrix}
# \mathbf{D_v} & \mathbf{0} & -1*\mathbf{D_h} \\
# \mathbf{D_h} & 2*\mathbf{D_h} & \mathbf{0} \\
# \end{bmatrix}, \qquad
# \mathbf{y} =
# \begin{bmatrix}
# \mathbf{D_v} \mathbf{x_1} - \mathbf{D_h} \mathbf{x_3} \\
# \mathbf{D_h} \mathbf{x_1} + 2*\mathbf{D_h} \mathbf{x_2}
# \end{bmatrix}
# With MatrixMult operator filled with zeros
Zop = pylops.MatrixMult(np.zeros(D2vop.shape))
Bop = pylops.Block([[D2vop, Zop, -1 * D2hop], [D2hop, 2 * D2hop, Zop]])
Y = np.reshape(Bop * X.ravel(), (2 * Nv, Nh))
# With Zero operator
Zop = pylops.Zero(D2vop.shape[0])
B1op = pylops.Block([[D2vop, Zop, -1 * D2hop], [D2hop, 2 * D2hop, Zop]])
Y1 = np.reshape(B1op * X.ravel(), (2 * Nv, Nh))
print("Y == Y1:", np.allclose(Y, Y1))
###############################################################################
# Finally we can use the *block-diagonal operator* to apply three operators
# to three different subset of the model and data
#
# .. math::
# \mathbf{D_{BDiag}} =
# \begin{bmatrix}
# \mathbf{D_v} & \mathbf{0} & \mathbf{0} \\
# \mathbf{0} & 0.5*\mathbf{D_v} & \mathbf{0} \\
# \mathbf{0} & \mathbf{0} & -\mathbf{D_h}
# \end{bmatrix}, \qquad
# \mathbf{y} =
# \begin{bmatrix}
# \mathbf{D_v} \mathbf{x_1} \\
# 0.5*\mathbf{D_v} \mathbf{x_2} \\
# -\mathbf{D_h} \mathbf{x_3}
# \end{bmatrix}
BD = pylops.BlockDiag([D2vop, 0.5 * D2vop, -1 * D2hop])
Y = np.reshape(BD * X.ravel(), (3 * Nv, Nh))
fig, axs = plt.subplots(1, 2, figsize=(10, 3))
fig.suptitle("Block-diagonal", fontsize=14, fontweight="bold", y=0.95)
im = axs[0].imshow(X, interpolation="nearest")
axs[0].axis("tight")
axs[0].set_title(r"$x$")
plt.colorbar(im, ax=axs[0])
im = axs[1].imshow(Y, interpolation="nearest")
axs[1].axis("tight")
axs[1].set_title(r"$y$")
plt.colorbar(im, ax=axs[1])
plt.tight_layout()
plt.subplots_adjust(top=0.8)
###############################################################################
# If we consider now the case of having a large number of operators inside a
# blockdiagonal structure, it may be convenient to span multiple processes
# handling subset of operators at the same time. This can be easily achieved
# by simply defining the number of processes we want to use via ``nproc``.
X = np.zeros((Nv * 10, Nh))
for iv in range(10):
X[int(Nv / 2) + iv * Nv, int(Nh / 2)] = 1
BD = pylops.BlockDiag([D2vop] * 10, nproc=2)
print("BD Operator multiprocessing pool", BD.pool)
Y = np.reshape(BD * X.ravel(), (10 * Nv, Nh))
BD.pool.close()
fig, axs = plt.subplots(1, 2, figsize=(10, 3))
fig.suptitle("Block-diagonal", fontsize=14, fontweight="bold", y=0.95)
im = axs[0].imshow(X, interpolation="nearest")
axs[0].axis("tight")
axs[0].set_title(r"$x$")
plt.colorbar(im, ax=axs[0])
im = axs[1].imshow(Y, interpolation="nearest")
axs[1].axis("tight")
axs[1].set_title(r"$y$")
plt.colorbar(im, ax=axs[1])
plt.tight_layout()
plt.subplots_adjust(top=0.8)
###############################################################################
# Finally we use the *Kronecker operator* and replicate this example on
# `wiki <https://en.wikipedia.org/wiki/Kronecker_product>`_.
#
# .. math::
# \begin{bmatrix}
# 1 & 2 \\
# 3 & 4 \\
# \end{bmatrix} \otimes
# \begin{bmatrix}
# 0 & 5 \\
# 6 & 7 \\
# \end{bmatrix} =
# \begin{bmatrix}
# 0 & 5 & 0 & 10 \\
# 6 & 7 & 12 & 14 \\
# 0 & 15 & 0 & 20 \\
# 18 & 21 & 24 & 28 \\
# \end{bmatrix}
A = np.array([[1, 2], [3, 4]])
B = np.array([[0, 5], [6, 7]])
AB = np.kron(A, B)
n1, m1 = A.shape
n2, m2 = B.shape
Aop = pylops.MatrixMult(A)
Bop = pylops.MatrixMult(B)
ABop = pylops.Kronecker(Aop, Bop)
x = np.ones(m1 * m2)
y = AB.dot(x)
yop = ABop * x
xinv = ABop / yop
print(f"AB = \n {AB}")
print(f"x = {x}")
print(f"y = {y}")
print(f"yop = {yop}")
print(f"xinv = {xinv}")
###############################################################################
# We can also use :py:class:`pylops.Kronecker` to do something more
# interesting. Any operator can in fact be applied on a single direction of a
# multi-dimensional input array if combined with an :py:class:`pylops.Identity`
# operator via Kronecker product. We apply here the
# :py:class:`pylops.FirstDerivative` to the second dimension of the model.
#
# Note that for those operators whose implementation allows their application
# to a single axis via the ``axis`` parameter, using the Kronecker product
# would lead to slower performance. Nevertheless, the Kronecker product allows
# any other operator to be applied to a single dimension.
Nv, Nh = 11, 21
Iop = pylops.Identity(Nv, dtype="float32")
D2hop = pylops.FirstDerivative(Nh, dtype="float32")
X = np.zeros((Nv, Nh))
X[Nv // 2, Nh // 2] = 1
D2hop = pylops.Kronecker(Iop, D2hop)
Y = D2hop * X.ravel()
Y = Y.reshape(Nv, Nh)
fig, axs = plt.subplots(1, 2, figsize=(10, 3))
fig.suptitle("Kronecker", fontsize=14, fontweight="bold", y=0.95)
im = axs[0].imshow(X, interpolation="nearest")
axs[0].axis("tight")
axs[0].set_title(r"$x$")
plt.colorbar(im, ax=axs[0])
im = axs[1].imshow(Y, interpolation="nearest")
axs[1].axis("tight")
axs[1].set_title(r"$y$")
plt.colorbar(im, ax=axs[1])
plt.tight_layout()
plt.subplots_adjust(top=0.8)