forked from astropy/astropy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboundary_none.pyx
More file actions
230 lines (191 loc) · 7.95 KB
/
Copy pathboundary_none.pyx
File metadata and controls
230 lines (191 loc) · 7.95 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
from __future__ import division
import numpy as np
cimport numpy as np
DTYPE = np.float
ctypedef np.float_t DTYPE_t
cdef extern from "numpy/npy_math.h":
bint npy_isnan(double x)
cimport cython
@cython.boundscheck(False) # turn off bounds-checking for entire function
def convolve1d_boundary_none(np.ndarray[DTYPE_t, ndim=1] f,
np.ndarray[DTYPE_t, ndim=1] g):
if g.shape[0] % 2 != 1:
raise ValueError("Convolution kernel must have odd dimensions")
assert f.dtype == DTYPE and g.dtype == DTYPE
cdef int nx = f.shape[0]
cdef int nkx = g.shape[0]
cdef int wkx = nkx // 2
# The following need to be set to zeros rather than empty because the
# boundary does not get reset.
cdef np.ndarray[DTYPE_t, ndim=1] fixed = np.zeros([nx], dtype=DTYPE)
cdef np.ndarray[DTYPE_t, ndim=1] conv = np.zeros([nx], dtype=DTYPE)
cdef unsigned int i, ii
cdef int iimin, iimax
cdef DTYPE_t top, bot, ker, val
# Need a first pass to replace NaN values with value convolved from
# neighboring values
for i in range(wkx, nx - wkx):
if npy_isnan(f[i]):
top = 0.
bot = 0.
for ii in range(i - wkx, i + wkx + 1):
val = f[ii]
if not npy_isnan(val):
ker = g[<unsigned int>(wkx + ii - i)]
top += val * ker
bot += ker
if bot != 0.:
fixed[i] = top / bot
else:
fixed[i] = f[i]
else:
fixed[i] = f[i]
# Now run the proper convolution
for i in range(wkx, nx - wkx):
if not npy_isnan(fixed[i]):
top = 0.
bot = 0.
for ii in range(i - wkx, i + wkx + 1):
val = fixed[ii]
ker = g[<unsigned int>(wkx + ii - i)]
if not npy_isnan(val):
top += val * ker
bot += ker
if bot != 0:
conv[i] = top / bot
else:
conv[i] = fixed[i]
else:
conv[i] = fixed[i]
return conv
@cython.boundscheck(False) # turn off bounds-checking for entire function
def convolve2d_boundary_none(np.ndarray[DTYPE_t, ndim=2] f,
np.ndarray[DTYPE_t, ndim=2] g):
if g.shape[0] % 2 != 1 or g.shape[1] % 2 != 1:
raise ValueError("Convolution kernel must have odd dimensions")
assert f.dtype == DTYPE and g.dtype == DTYPE
cdef int nx = f.shape[0]
cdef int ny = f.shape[1]
cdef int nkx = g.shape[0]
cdef int nky = g.shape[1]
cdef int wkx = nkx // 2
cdef int wky = nky // 2
# The following need to be set to zeros rather than empty because the
# boundary does not get reset.
cdef np.ndarray[DTYPE_t, ndim=2] fixed = np.zeros([nx, ny], dtype=DTYPE)
cdef np.ndarray[DTYPE_t, ndim=2] conv = np.zeros([nx, ny], dtype=DTYPE)
cdef unsigned int i, j, ii, jj
cdef int iimin, iimax, jjmin, jjmax
cdef DTYPE_t top, bot, ker, val
# Need a first pass to replace NaN values with value convolved from
# neighboring values
for i in range(wkx, nx - wkx):
for j in range(wky, ny - wky):
if npy_isnan(f[i, j]):
top = 0.
bot = 0.
for ii in range(i - wkx, i + wkx + 1):
for jj in range(j - wky, j + wky + 1):
val = f[ii, jj]
if not npy_isnan(val):
ker = g[<unsigned int>(wkx + ii - i),
<unsigned int>(wky + jj - j)]
top += val * ker
bot += ker
if bot != 0.:
fixed[i, j] = top / bot
else:
fixed[i, j] = f[i, j]
else:
fixed[i, j] = f[i, j]
# Now run the proper convolution
for i in range(wkx, nx - wkx):
for j in range(wky, ny - wky):
if not npy_isnan(fixed[i, j]):
top = 0.
bot = 0.
for ii in range(i - wkx, i + wkx + 1):
for jj in range(j - wky, j + wky + 1):
val = fixed[ii, jj]
ker = g[<unsigned int>(wkx + ii - i),
<unsigned int>(wky + jj - j)]
if not npy_isnan(val):
top += val * ker
bot += ker
if bot != 0:
conv[i, j] = top / bot
else:
conv[i, j] = fixed[i, j]
else:
conv[i, j] = fixed[i, j]
return conv
@cython.boundscheck(False) # turn off bounds-checking for entire function
def convolve3d_boundary_none(np.ndarray[DTYPE_t, ndim=3] f,
np.ndarray[DTYPE_t, ndim=3] g):
if g.shape[0] % 2 != 1 or g.shape[1] % 2 != 1 or g.shape[2] % 2 != 1:
raise ValueError("Convolution kernel must have odd dimensions")
assert f.dtype == DTYPE and g.dtype == DTYPE
cdef int nx = f.shape[0]
cdef int ny = f.shape[1]
cdef int nz = f.shape[2]
cdef int nkx = g.shape[0]
cdef int nky = g.shape[1]
cdef int nkz = g.shape[2]
cdef int wkx = nkx // 2
cdef int wky = nky // 2
cdef int wkz = nkz // 2
# The following need to be set to zeros rather than empty because the
# boundary does not get reset.
cdef np.ndarray[DTYPE_t, ndim=3] fixed = np.zeros([nx, ny, nz], dtype=DTYPE)
cdef np.ndarray[DTYPE_t, ndim=3] conv = np.zeros([nx, ny, nz], dtype=DTYPE)
cdef unsigned int i, j, k, ii, jj, kk
cdef int iimin, iimax, jjmin, jjmax, kkmin, kkmax
cdef DTYPE_t top, bot, ker, val
# Need a first pass to replace NaN values with value convolved from
# neighboring values
for i in range(wkx, nx - wkx):
for j in range(wky, ny - wky):
for k in range(wkz, nz - wkz):
if npy_isnan(f[i, j, k]):
top = 0.
bot = 0.
for ii in range(i - wkx, i + wkx + 1):
for jj in range(j - wky, j + wky + 1):
for kk in range(k - wkz, k + wkz + 1):
val = f[ii, jj, kk]
if not npy_isnan(val):
ker = g[<unsigned int>(wkx + ii - i),
<unsigned int>(wky + jj - j),
<unsigned int>(wkz + kk - k)]
top += val * ker
bot += ker
if bot != 0.:
fixed[i, j, k] = top / bot
else:
fixed[i, j, k] = f[i, j, k]
else:
fixed[i, j, k] = f[i, j, k]
# Now run the proper convolution
for i in range(wkx, nx - wkx):
for j in range(wky, ny - wky):
for k in range(wkz, nz - wkz):
if not npy_isnan(fixed[i, j, k]):
top = 0.
bot = 0.
for ii in range(i - wkx, i + wkx + 1):
for jj in range(j - wky, j + wky + 1):
for kk in range(k - wkz, k + wkz + 1):
val = fixed[ii, jj, kk]
ker = g[<unsigned int>(wkx + ii - i),
<unsigned int>(wky + jj - j),
<unsigned int>(wkz + kk - k)]
if not npy_isnan(val):
top += val * ker
bot += ker
if bot != 0:
conv[i, j, k] = top / bot
else:
conv[i, j, k] = fixed[i, j, k]
else:
conv[i, j, k] = fixed[i, j, k]
return conv