-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvolve.py
More file actions
271 lines (193 loc) · 9.64 KB
/
convolve.py
File metadata and controls
271 lines (193 loc) · 9.64 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
'''Functions to perform 1D or 2D convolution with control on maximum
allowable missing data percentage in convolution window.
Main functions:
- convolve1D(): 1D convolution on nD array.
- convolve2D(): 2D convolution on 2D array.
- runMean1D(): 1D running mean using 1D convolution, on nD array.
- runMean2D(): 2D running mean using 2D convolution, on 2D array.
Calls a fortran module for the core convolution computation:
- conv1d.so: for 1D convolution and running mean.
- conv2d.so: for 2D convolution and running mean.
Author: guangzhi XU (xugzhi1987@gmail.com; guangzhi.xu@outlook.com)
Update time: 2017-11-08 16:07:36.
'''
import numpy
from conv1d import conv1d
from conv2d import conv2d
#----Get mask for missing data (masked or nan)----
def getMissingMask(slab):
'''Get a bindary denoting missing (masked or nan).
<slab>: nd array, possibly contains masked values or nans.
Return <mask>: nd bindary, 1s for missing, 0s otherwise.
'''
nan_mask=numpy.where(numpy.isnan(slab),1,0)
if not hasattr(slab,'mask'):
mask_mask=numpy.zeros(slab.shape)
else:
if slab.mask.size==1 and slab.mask==False:
mask_mask=numpy.zeros(slab.shape)
else:
mask_mask=numpy.where(slab.mask,1,0)
mask=numpy.where(mask_mask+nan_mask>0,1,0)
return mask
#----------Preprocess data for 1D convolution----------
def preProcess1D(slab,axis=0,verbose=True):
#--------------Get mask for missings--------------
slabmask=getMissingMask(slab)
slab=numpy.where(slabmask==0,slab,0)
#-----------------Reorder variable-----------------
shape=list(slab.shape)
n=shape[axis]
ngrid=slab.size/n
order_var=range(numpy.ndim(slab))
#----Put the specified axis to 1st in order------------
if axis!=0:
#----Switch order------
order_var[axis]=0
order_var[0]=axis
if verbose:
print '# <preProcess1D>: Re-order <var> to:',order_var
slab=numpy.transpose(slab,order_var)
slabmask=numpy.transpose(slabmask,order_var)
shape_reordered=slab.shape
#-------------------Tabulate var-------------------
slab=numpy.reshape(slab,(n,ngrid))
slabmask=numpy.reshape(slabmask,(n,ngrid))
return slab,slabmask,order_var,shape_reordered
#----------Postprocess data after 1D convolution----------
def postProcess1D(slab,slabmask,axis,order_var,shape_reordered,verbose=True):
#-------------------Reshape back-------------------
slab=numpy.reshape(slab,shape_reordered)
slabmask=numpy.reshape(slabmask,shape_reordered)
#-------------------Reorder back-------------------
if axis!=0:
slab=numpy.transpose(slab,order_var)
slabmask=numpy.transpose(slabmask,order_var)
if verbose:
print '# <postProcess1D>: Re-order <var> to:',order_var
slab=numpy.ma.masked_where(slabmask==1,slab)
return slab
#----------1D Convolution using Fortran ----------
def convolve1D(slab,kernel,axis=0,max_missing=0.5,verbose=True):
'''1D convolution using Fortran.
<slab>: nd array, with optional mask.
<kernel>: 1d array, convolution kernel.
<axis>: int, axis on <slab> to do convolution.
<max_missing>: real, max tolerable percentage of missings within any
convolution window.
E.g. if <max_missing> is 0.5, when over 50% of values
within a given element are missing, the center will be
set as missing (<res>=0, <resmask>=1). If only 40% is
missing, center value will be computed using the remaining
60% data in the element.
NOTE that out-of-bound grids are not counted as missings,
i.e. the number of valid values at edges drops as the kernel
approaches the edge.
Return <result>: nd array, <slab> convolved with <kernel> at axis <axis>.
Author: guangzhi XU (xugzhi1987@gmail.com; guangzhi.xu@outlook.com)
Update time: 2017-11-08 09:27:49.
'''
assert numpy.ndim(kernel)==1, "<kernel> needs to be 1D."
assert axis==int(axis) and axis>=0 and axis<=numpy.ndim(slab)-1,\
"<axis> needs to be an int within [0,%d]" %(numpy.ndim(slab)-1)
kernelflag=numpy.where(kernel==0,0,1)
slab,slabmask,order_var,shape_reordered=preProcess1D(slab,axis,verbose)
result,result_mask=conv1d.convolve1d(slab,slabmask,
kernel,kernelflag,max_missing)
result=postProcess1D(result,result_mask,axis,order_var,shape_reordered,verbose)
return result
#----------1D running meaning using Fortran ----------
def runMean1D(slab,kernel,axis=0,max_missing=0.5,verbose=True):
'''1D moving average with valid values control
<slab>: nd array, with optional mask, variable to do running mean.
<kernel>: 1d array, filtering kernel.
<axis>: int, axis on <slab> to do convolution.
<max_missing>: real, max tolerable percentage of missings within any
convolution window.
E.g. if <max_missing> is 0.5, when over 50% of values
within a given element are missing, the center will be
set as missing (<res>=0, <resmask>=1). If only 40% is
missing, center value will be computed using the remaining
60% data in the element.
NOTE that out-of-bound grids are not counted as missings,
i.e. the number of valid values at edges drops as the kernel
approaches the edge.
Return <result>: nd array, moving average done on <slab>, with same shape
as <slab>.
Author: guangzhi XU (xugzhi1987@gmail.com; guangzhi.xu@outlook.com)
Update time: 2017-11-08 14:06:15.
'''
assert numpy.ndim(kernel)==1, "<kernel> needs to be 1D."
assert axis==int(axis) and axis>=0 and axis<=numpy.ndim(slab)-1,\
"<axis> needs to be an int within [0,%d]" %(numpy.ndim(slab)-1)
#-----------------Flip the kernel-----------------
kernel=kernel[::-1]
kernelflag=numpy.where(kernel==0,0,1)
slab,slabmask,order_var,shape_reordered=preProcess1D(slab,axis,verbose)
result,result_mask=conv1d.runmean1d(slab,slabmask,
kernel,kernelflag,max_missing)
result=postProcess1D(result,result_mask,axis,order_var,shape_reordered,verbose)
return result
#----------2D Convolution using Fortran ----------
def convolve2D(slab,kernel,max_missing=0.5,verbose=True):
'''2D convolution using Fortran.
<slab>: 2d array, with optional mask.
<kernel>: 2d array, convolution kernel.
<max_missing>: real, max tolerable percentage of missings within any
convolution window.
E.g. if <max_missing> is 0.5, when over 50% of values
within a given element are missing, the center will be
set as missing (<res>=0, <resmask>=1). If only 40% is
missing, center value will be computed using the remaining
60% data in the element.
NOTE that out-of-bound grids are not counted as missings,
i.e. the number of valid values at edges drops as the kernel
approaches the edge.
Return <result>: 2d array, convolution of <slab> with <kernel>.
Author: guangzhi XU (xugzhi1987@gmail.com; guangzhi.xu@outlook.com)
Update time: 2017-01-16 10:59:55.
'''
assert numpy.ndim(slab)==2, "<slab> needs to be 2D."
assert numpy.ndim(kernel)==2, "<kernel> needs to be 2D."
kernelflag=numpy.where(kernel==0,0,1)
#--------------Get mask for missings--------------
slabmask=getMissingMask(slab)
# this is to set np.nan to a float, this won't affect the result as
# masked values are not used in convolution. Otherwise, nans will
# affect convolution in the same way as scipy.signal.convolve()
# and the result will contain nans.
slab=numpy.where(slabmask==1,0,slab)
result,result_mask=conv2d.convolve2d(slab,slabmask,kernel,
kernelflag,max_missing)
result=numpy.ma.masked_where(result_mask==1,result)
return result
#------------2D moving average wit valid values control------------
def runMean2D(slab,kernel,max_missing=0.5,verbose=True):
'''2D moving average with valid values control
<slab>: 2d array, with optional mask.
<kernel>: 2d array, convolution kernel.
<max_missing>: real, max tolerable percentage of missings within any
convolution window.
E.g. if <max_missing> is 0.5, when over 50% of values
within a given element are missing, the center will be
set as missing (<res>=0, <resmask>=1). If only 40% is
missing, center value will be computed using the remaining
60% data in the element.
NOTE that out-of-bound grids are not counted as missings,
i.e. the number of valid values at edges drops as the kernel
approaches the edge.
Return <result>: 2d moving average.
Author: guangzhi XU (xugzhi1987@gmail.com; guangzhi.xu@outlook.com)
Update time: 2016-11-10 17:28:00.
'''
assert numpy.ndim(slab)==2, "<slab> needs to be 2D."
assert numpy.ndim(kernel)==2, "<kernel> needs to be 2D."
#-----------------Flip the kernel-----------------
kernel=kernel[::-1,::-1]
kernelflag=numpy.where(kernel==0,0,1)
#--------------Get mask for missings--------------
slabmask=getMissingMask(slab)
slab=numpy.where(slabmask==1,0,slab)
result,result_mask=conv2d.runmean2d(slab,slabmask,kernel,kernelflag,max_missing)
result=numpy.ma.masked_where(result_mask==1,result)
return result