-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
410 lines (333 loc) · 12.2 KB
/
utils.py
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
'''
Written by R. Jolivet 2017
License:
MPITS: Multi-Pixel InSAR Time Series
Copyright (C) 2018 <Romain Jolivet>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
# a bunch of useful routines
import numpy as np
import scipy.interpolate as sciint
import scipy.fftpack as fftpack
import scipy.ndimage.filters as scifilt
import scipy.signal as scisig
import tsinsar.mints as mints
def _diagonalConvolution(image, xm, ym, dx, dy,
covariance, inverse=False):
'''
Convolve an image of the size of the interferogram with a diagonal covariance.
Returns the convoluted function.
Args:
* image : 1d array containing the data
* xm : 1d array the size of image containing the x coordinates
* ym : 1d array the size of the
* covariance : Float diagonal value
* inverse : convolution by the inverse function (default is False)
'''
if inverse:
return image/covariance
else:
return image*covariance
def _detrend(intImage):
'''
Remove a ramp from the image.
'''
# Coordinates
x = np.arange(intImage.shape[1])
y = np.arange(intImage.shape[0])
x,y = np.meshgrid(x,y)
# Invert
G = np.vstack((x.flatten(), y.flatten(), np.ones((np.prod(x.shape),)))).T
m, n, p, res = np.linalg.lstsq(G, intImage.flatten())
# All done
flatImage = (intImage.flatten()-np.dot(G,m)).reshape(intImage.shape)
trend = intImage - flatImage
return flatImage, trend
def _expConvolution(image, xm, ym, dx, dy,
covariance, inverse=False, donotinterpolate=False):
'''
Convolve an image of the size of the interferogram with an exponential (or inverse exponential)
function.
The function is of the form:
f(x1,x2) = Sigma^2 exp(-||x1,x2||/Lambda)
where ||x1,x2|| is the distance between pixels x1 and x2.
Returns the convoluted function.
Args:
* image : 1d array containing the data
* xm : 1d array the size of image containing the x coordinates
* ym : 1d array the size of the
* covariance : (Lambda,Sigma)
- Lambda : Float, Correlation length
- Sigma : Float, Amplitude of the correlation function
* inverse : convolution by the inverse function (default is False)
* donotinterpolate: Bypasses the interpolation (image has to be a 2d array)
'''
# One simple case
if (image==0.).all():
return image
# Get Lambda and Sigma
Lambda, Sigma = covariance
# Get number of data
nPoints = float(len(ym))
# Interpolate
if donotinterpolate:
intImage = image
ymin = 0
xmin = 0
else:
intImage, xmin, ymin = _mintsInterp(image, xm, ym)
nTotal = float(np.prod(intImage.shape))
# Padding size
padlength = intImage.shape[0]/2, intImage.shape[1]/2
# Different case for inverse or direct
if not inverse:
# Zero Padding
intImage = np.pad(intImage, ((padlength[0], padlength[0]),
(padlength[1], padlength[1])),
mode='constant')
pad = np.zeros(intImage.shape)
pad[padlength[0]+ym-ymin, padlength[1]+xm-xmin] = 1.
intImage *= pad
else:
# Linear Padding
intImage = np.pad(intImage, ((padlength[0], padlength[0]),
(padlength[1], padlength[1])),
mode='reflect')
l,c = intImage.shape
hl = scisig.gaussian(l, padlength[0])
hc = scisig.gaussian(c, padlength[1])
intImage = hl[:,np.newaxis]*intImage*hc[np.newaxis,:]
# For debugging
#import matplotlib.pyplot as plt
#plt.figure()
#plt.imshow(intImage, interpolation='nearest')
#plt.colorbar()
#plt.show()
# For debugging
# Do the FFT
fm = fftpack.fft2(intImage)
u = fftpack.fftfreq(intImage.shape[1], d=dx)
v = fftpack.fftfreq(intImage.shape[0], d=dy)
u,v = np.meshgrid(u,v)
# Select the convolution function
if inverse:
H = _expInvF
else:
H = _expF
# Convolve with the function
dfm = H(u,v,Lambda,Sigma)*fm
dm = np.real(fftpack.ifft2(dfm))
# un-padding
dm = dm[padlength[0]:-padlength[0], padlength[1]:-padlength[1]]
# all done
if donotinterpolate:
return dm
else:
return dm[ym-ymin, xm-xmin]
def _nearestInterp(image, xm, ym):
'''
Computes the interpolation on the smallest rectangle inside the data
Args:
* image : 1d array of Data
* xm : 1d array of x-coordinates
* ym : 1d array of y-coordinates
'''
# Get the minimum area
xmin, xmax = np.min(xm), np.max(xm)+1
ymin, ymax = np.min(ym), np.max(ym)+1
x = np.arange(xmax-xmin)
y = np.arange(ymax-ymin)
x,y = np.meshgrid(x,y)
# Save what's not to be interpolated
intImage = np.zeros((ymax-ymin, xmax-xmin)); intImage[:,:] = np.nan
intImage[ym-ymin, xm-xmin] = image
ii = np.flatnonzero(np.isnan(intImage))
# Get x and y
x = x.flatten()[ii]
y = y.flatten()[ii]
# Create the interpolator
interpolator = sciint.NearestNDInterpolator(np.vstack((xm-xmin, ym-ymin)).T, image)
# Interpolate
intImage[y,x] = interpolator(x,y)
del interpolator, x, y
# Is there still holes?
if np.isnan(intImage).any():
_fillHoles(intImage)
# All done
return intImage, xmin, ymin
def _linearInterp(image, xm, ym):
'''
Computes the interpolation on the smallest rectangle inside the data
Args:
* image : 1d array of Data
* xm : 1d array of x-coordinates
* ym : 1d array of y-coordinates
'''
# Get the minimum area
xmin, xmax = np.min(xm), np.max(xm)+1
ymin, ymax = np.min(ym), np.max(ym)+1
x = np.arange(xmax-xmin)
y = np.arange(ymax-ymin)
x,y = np.meshgrid(x,y)
# Save what's not to be interpolated
intImage = np.zeros((ymax-ymin, xmax-xmin)); intImage[:,:] = np.nan
intImage[ym-ymin, xm-xmin] = image
ii = np.flatnonzero(np.isnan(intImage))
# Get x and y
x = x.flatten()[ii]
y = y.flatten()[ii]
# Create the interpolator
interpolator = sciint.LinearNDInterpolator(np.vstack((xm-xmin, ym-ymin)).T, image)
# Interpolate
intImage[y,x] = interpolator(x,y)
del interpolator, x, y
# Is there still holes?
if np.isnan(intImage).any():
_fillHoles(intImage)
# All done
return intImage, xmin, ymin
def _mintsInterp(image, xm, ym):
'''
Computes the interpolation on the smallest rectangle inside the data
using the interpolation from MInTS
Args:
* image : 1d array of Data
* xm : 1d array of x-coordinates
* ym : 1d array of y-coordinates
'''
# Get the minimum area
xmin, xmax = np.min(xm), np.max(xm)+1
ymin, ymax = np.min(ym), np.max(ym)+1
x = np.arange(xmax-xmin)
y = np.arange(ymax-ymin)
x,y = np.meshgrid(x,y)
# Save what's not to be interpolated
intImage = np.zeros((ymax-ymin, xmax-xmin)); intImage[:,:] = np.nan
intImage[ym-ymin, xm-xmin] = image
# Interpolate using inpaints
intImage = mints.inpaint(intImage)
# All done
return intImage, xmin, ymin
def _fillHoles(ifg):
'''
Fill the holes that are outside the Qhull area
Assumption: if the hole has not been filled through linear interpolation, then
it is a corner...
'''
# While loop
while np.isnan(ifg).any():
# Take the nans
lst, cst = np.where(np.isnan(ifg))
# Isolate the area
subimage, x0, x1, y0, y1 = _isolateNaNs(ifg, lst[0], cst[0])
# Do a small interpolation
x = range(x0, x1); y = range(y0, y1)
x,y = np.meshgrid(x,y)
# Get the number of non-NaN
count = np.flatnonzero(np.isfinite(subimage)).shape[0]
# if low number of points
if count<4000:
inter = sciint.Rbf(x[np.isfinite(subimage)],
y[np.isfinite(subimage)],
subimage[np.isfinite(subimage)])
else:
inter = sciint.NearestNDInterpolator(np.vstack((x[np.isfinite(subimage)],
y[np.isfinite(subimage)])).T,
subimage[np.isfinite(subimage)])
subimage[np.isnan(subimage)] = inter(x[np.isnan(subimage)],
y[np.isnan(subimage)])
# Put back subimage in image
ifg[y0:y1,x0:x1] = subimage
# Clean up
del inter, x, y, subimage
return
def _isolateNaNs(image, lst, cst):
'''
Returns a subimage that encompasses a region with NaNs starting from a pixel
'''
def update(image, xmin, xmax, ymin, ymax):
if xmin>0: xmin -= 1
if xmax<image.shape[1]-1: xmax += 1
if ymin>0: ymin -= 1
if ymax<image.shape[0]-1: ymax += 1
return xmin, xmax, ymin, ymax
def check(image, xmin, xmax, ymin, ymax):
# Xmin
if (np.isnan(image[ymin:ymax+1,xmin]).any()):
xLow = False
else:
xLow = True
if xmin==0: xLow = True
# Ymin
if (np.isnan(image[ymin,xmin:xmax+1]).any()):
yLow = False
else:
yLow = True
if ymin==0: yLow = True
# Ymax
if (np.isnan(image[ymax,xmin:xmax+1]).any()):
yUp = False
else:
yUp = True
if ymax==image.shape[0]-1: yUp = True
# Xmax
if (np.isnan(image[ymin:ymax+1,xmax]).any()):
xUp = False
else:
xUp = True
if xmax==image.shape[1]-1: xUp = True
return xLow*yLow*yUp*xUp
# Start
xmin, xmax, ymin, ymax = update(image, cst, cst, lst, lst)
# While loop
while not check(image, xmin, xmax, ymin, ymax):
xmin, xmax, ymin, ymax = update(image, xmin, xmax, ymin, ymax)
# All done
return image[ymin:ymax+1, xmin:xmax+1], xmin, xmax+1, ymin, ymax+1
# Covariance functions
def _expF(u, v, lam, sig):
return (sig*sig*lam*lam*2.0*np.pi)/((1 + (lam*u*2.0*np.pi)**2 + \
(lam*v*2.0*np.pi)**2)**(1.5))
def _expInvF(u, v, lam, sig):
return 1./_expF(u, v, lam, sig)
def _gaussF(u, v, lam, sig):
return sig**2/(2*np.pi) *np.exp(-lam*lam*(u*u+v*v)/2.)
def _gaussInvF(u, v, lam, sig):
return 1./_gaussF(u, v, lam, sig)
# List splitter
def _split_seq(seq, size):
newseq = []
splitsize = 1.0/size*len(seq)
for i in range(size):
newseq.append(seq[int(round(i*splitsize)):int(round((i+1)*splitsize))])
return newseq
def _matrixConvolution(m, x, y, dx, dy, Lambda, Sigma, inverse=False):
'''
Do the matrix form convolution. Testing mode only...
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Make sure you have a tiny image (less than 100x100)
Otherwise your computer will die in pain...
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
'''
X,XX = np.meshgrid(x.flatten()*dx, x.flatten()*dx)
Y,YY = np.meshgrid(y.flatten()*dy, y.flatten()*dy)
Cov = Sigma*Sigma*np.exp(-np.sqrt( (X-XX)**2 + (Y-YY)**2 )/Lambda)
if inverse:
Cov = np.linalg.inv(Cov)
mCm = np.dot(Cov, m.flatten()).reshape(m.shape)
if inverse:
mCm /= dx*dy
else:
mCm *= dx*dy
return mCm