|
| 1 | +""" |
| 2 | +Derivatives |
| 3 | +=========== |
| 4 | +This example shows how to use the suite of derivative operators, namely |
| 5 | +:py:class:`pylops_distributed.FirstDerivative`, |
| 6 | +:py:class:`pylops_distributed.SecondDerivative`, and |
| 7 | +:py:class:`pylops_distributed.Laplacian`. |
| 8 | +
|
| 9 | +The derivative operators can be applied along any dimension of a N-dimensional |
| 10 | +input. When the input is chuncked along any other direction(s) than the one |
| 11 | +the derivative is applied, the derivative is efficiently performed without |
| 12 | +neither communication between workers nor duplication of part of the input |
| 13 | +array. On the other hand when the input is chuncked along the direction where |
| 14 | +the derivative is applied, the chunks are partially overlapping such that no |
| 15 | +communication is required between the workers when applying the derivative. |
| 16 | +
|
| 17 | +In some applications, the user cannot avoid this second scenario to happen |
| 18 | +(e.g, when the derivative should be applied to all the dimensions of the |
| 19 | +dataset). Nevertheless our implementation makes this possible in a fully |
| 20 | +transparent way and with very little additional overhead. |
| 21 | +
|
| 22 | +""" |
| 23 | +import numpy as np |
| 24 | +import dask.array as da |
| 25 | +import matplotlib.pyplot as plt |
| 26 | + |
| 27 | +import pylops |
| 28 | +import pylops_distributed |
| 29 | + |
| 30 | +plt.close('all') |
| 31 | +np.random.seed(0) |
| 32 | + |
| 33 | +############################################################################### |
| 34 | +# Let's start by looking at a simple first-order centered derivative. We |
| 35 | +# chunck the vector in 3 chunks. |
| 36 | +nx = 100 |
| 37 | +nchunks = 3 |
| 38 | + |
| 39 | +x = np.zeros(nx) |
| 40 | +x[int(nx/2)] = 1 |
| 41 | +xd = da.from_array(x, chunks=nx // nchunks + 1) |
| 42 | +print('x:', xd) |
| 43 | + |
| 44 | +Dop = pylops.FirstDerivative(nx, dtype='float32') |
| 45 | +dDop = pylops_distributed.FirstDerivative(nx, compute=(True, True), |
| 46 | + dtype='float32') |
| 47 | + |
| 48 | +y = Dop * x |
| 49 | +xadj = Dop.H * y |
| 50 | + |
| 51 | +yd = Dop * xd |
| 52 | +xadjd = Dop.H * yd |
| 53 | + |
| 54 | + |
| 55 | +fig, axs = plt.subplots(3, 1, figsize=(13, 8)) |
| 56 | +axs[0].stem(np.arange(nx), x, linefmt='k', markerfmt='ko', |
| 57 | + use_line_collection=True) |
| 58 | +axs[0].set_title('Input', size=20, fontweight='bold') |
| 59 | +axs[1].stem(np.arange(nx), y, linefmt='--r', markerfmt='ro', |
| 60 | + label='Numpy', use_line_collection=True) |
| 61 | +axs[1].stem(np.arange(nx), yd, linefmt='--r', markerfmt='ro', |
| 62 | + label='Dask', use_line_collection=True) |
| 63 | +axs[1].set_title('Forward', size=20, fontweight='bold') |
| 64 | +axs[1].legend() |
| 65 | +axs[2].stem(np.arange(nx), xadj, linefmt='k', markerfmt='ko', |
| 66 | + label='Numpy', use_line_collection=True) |
| 67 | +axs[2].stem(np.arange(nx), xadjd, linefmt='--r', markerfmt='ro', |
| 68 | + label='Dask', use_line_collection=True) |
| 69 | +axs[2].set_title('Adjoint', size=20, fontweight='bold') |
| 70 | +axs[2].legend() |
| 71 | +plt.tight_layout() |
| 72 | + |
| 73 | +############################################################################### |
| 74 | +# As expected we obtain the same result, with the only difference that |
| 75 | +# in the second case we did not need to explicitly create a matrix, |
| 76 | +# saving memory and computational time. |
| 77 | +# |
| 78 | +# Let's move onto applying the same first derivative to a 2d array in |
| 79 | +# the first direction. Now we consider two cases, first when the data is |
| 80 | +# chunked along the first direction and second when its chunked along the |
| 81 | +# second direction. |
| 82 | +nx, ny = 11, 21 |
| 83 | +nchunks = 2 |
| 84 | + |
| 85 | +A = np.zeros((nx, ny)) |
| 86 | +A[nx//2, ny//2] = 1. |
| 87 | +A1d = da.from_array(A, chunks= (nx // nchunks + 1, ny)) |
| 88 | +A2d = da.from_array(A, chunks= (nx , ny // nchunks + 1)) |
| 89 | +print('A1d:', A1d) |
| 90 | +print('A2d:', A2d) |
| 91 | + |
| 92 | +Dop = pylops_distributed.FirstDerivative(nx * ny, dims=(nx, ny), |
| 93 | + compute=(True, True), |
| 94 | + dir=0, dtype='float64') |
| 95 | + |
| 96 | +B1d = np.reshape(Dop * A1d.flatten(), (nx, ny)) |
| 97 | +B2d = np.reshape(Dop * A2d.flatten(), (nx, ny)) |
| 98 | + |
| 99 | +fig, axs = plt.subplots(1, 3, figsize=(12, 3)) |
| 100 | +fig.suptitle('First Derivative in 1st direction', fontsize=12, |
| 101 | + fontweight='bold', y=0.95) |
| 102 | +im = axs[0].imshow(A, interpolation='nearest', cmap='rainbow') |
| 103 | +axs[0].axis('tight') |
| 104 | +axs[0].set_title('x') |
| 105 | +plt.colorbar(im, ax=axs[0]) |
| 106 | +im = axs[1].imshow(B1d, interpolation='nearest', cmap='rainbow') |
| 107 | +axs[1].axis('tight') |
| 108 | +axs[1].set_title('y (1st dir chunks)') |
| 109 | +plt.colorbar(im, ax=axs[1]) |
| 110 | +im = axs[2].imshow(B2d, interpolation='nearest', cmap='rainbow') |
| 111 | +axs[2].axis('tight') |
| 112 | +axs[2].set_title('y (2nd dir chunks)') |
| 113 | +plt.colorbar(im, ax=axs[2]) |
| 114 | +plt.tight_layout() |
| 115 | +plt.subplots_adjust(top=0.8) |
| 116 | + |
| 117 | +############################################################################### |
| 118 | +# We can now do the same for the second derivative |
| 119 | +A = np.zeros((nx, ny)) |
| 120 | +A[nx//2, ny//2] = 1. |
| 121 | +A1d = da.from_array(A, chunks= (nx // nchunks + 1, ny)) |
| 122 | +A2d = da.from_array(A, chunks= (nx , ny // nchunks + 1)) |
| 123 | +print('A1d:', A1d) |
| 124 | +print('A2d:', A2d) |
| 125 | + |
| 126 | +Dop = pylops_distributed.SecondDerivative(nx * ny, dims=(nx, ny), |
| 127 | + compute=(True, True), |
| 128 | + dir=0, dtype='float64') |
| 129 | + |
| 130 | +B1d = np.reshape(Dop * A1d.flatten(), (nx, ny)) |
| 131 | +B2d = np.reshape(Dop * A2d.flatten(), (nx, ny)) |
| 132 | + |
| 133 | +fig, axs = plt.subplots(1, 3, figsize=(12, 3)) |
| 134 | +fig.suptitle('Second Derivative in 1st direction', fontsize=12, |
| 135 | + fontweight='bold', y=0.95) |
| 136 | +im = axs[0].imshow(A, interpolation='nearest', cmap='rainbow') |
| 137 | +axs[0].axis('tight') |
| 138 | +axs[0].set_title('x') |
| 139 | +plt.colorbar(im, ax=axs[0]) |
| 140 | +im = axs[1].imshow(B1d, interpolation='nearest', cmap='rainbow') |
| 141 | +axs[1].axis('tight') |
| 142 | +axs[1].set_title('y (1st dir chunks)') |
| 143 | +plt.colorbar(im, ax=axs[1]) |
| 144 | +im = axs[2].imshow(B2d, interpolation='nearest', cmap='rainbow') |
| 145 | +axs[2].axis('tight') |
| 146 | +axs[2].set_title('y (2nd dir chunks)') |
| 147 | +plt.colorbar(im, ax=axs[2]) |
| 148 | +plt.tight_layout() |
| 149 | +plt.subplots_adjust(top=0.8) |
| 150 | + |
| 151 | + |
| 152 | +############################################################################### |
| 153 | +# We use the symmetrical Laplacian operator as well |
| 154 | +# as a asymmetrical version of it (by adding more weight to the |
| 155 | +# derivative along one direction) |
| 156 | + |
| 157 | +# symmetrical |
| 158 | +L2symop = pylops_distributed.Laplacian(dims=(nx, ny), weights=(1, 1), |
| 159 | + compute=(True, True), dtype='float64') |
| 160 | + |
| 161 | +# asymmetrical |
| 162 | +L2asymop = pylops_distributed.Laplacian(dims=(nx, ny), weights=(3, 1), |
| 163 | + compute=(True, True), dtype='float64') |
| 164 | + |
| 165 | +Bsym = np.reshape(L2symop * A1d.flatten(), (nx, ny)) |
| 166 | +Basym = np.reshape(L2asymop * A2d.flatten(), (nx, ny)) |
| 167 | + |
| 168 | +fig, axs = plt.subplots(1, 3, figsize=(10, 3)) |
| 169 | +fig.suptitle('Laplacian', fontsize=12, |
| 170 | + fontweight='bold', y=0.95) |
| 171 | +im = axs[0].imshow(A, interpolation='nearest', cmap='rainbow') |
| 172 | +axs[0].axis('tight') |
| 173 | +axs[0].set_title('x') |
| 174 | +plt.colorbar(im, ax=axs[0]) |
| 175 | +im = axs[1].imshow(Bsym, interpolation='nearest', cmap='rainbow') |
| 176 | +axs[1].axis('tight') |
| 177 | +axs[1].set_title('y sym') |
| 178 | +plt.colorbar(im, ax=axs[1]) |
| 179 | +im = axs[2].imshow(Basym, interpolation='nearest', cmap='rainbow') |
| 180 | +axs[2].axis('tight') |
| 181 | +axs[2].set_title('y asym') |
| 182 | +plt.colorbar(im, ax=axs[2]) |
| 183 | +plt.tight_layout() |
| 184 | +plt.subplots_adjust(top=0.8) |
0 commit comments