File: boundary_wrap.pyx

package info (click to toggle)
python-astropy 1.3-8~bpo8%2B2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 44,292 kB
  • sloc: ansic: 160,360; python: 137,322; sh: 11,493; lex: 7,638; yacc: 4,956; xml: 1,796; makefile: 474; cpp: 364
file content (267 lines) | stat: -rw-r--r-- 9,902 bytes parent folder | download | duplicates (2)
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
# Licensed under a 3-clause BSD style license - see LICENSE.rst
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" nogil:
    bint npy_isnan(double x)

cimport cython


@cython.boundscheck(False)  # turn off bounds-checking for entire function
def convolve1d_boundary_wrap(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
    cdef np.ndarray[DTYPE_t, ndim=1] fixed = np.empty([nx], dtype=DTYPE)
    cdef np.ndarray[DTYPE_t, ndim=1] conv = np.empty([nx], dtype=DTYPE)
    cdef unsigned int i, iii
    cdef int ii

    cdef int iimin, iimax

    cdef DTYPE_t top, bot, ker, val

    # release the GIL
    with nogil:
        # Need a first pass to replace NaN values with value convolved from
        # neighboring values
        for i in range(nx):
            if npy_isnan(f[i]):
                top = 0.
                bot = 0.
                iimin = i - wkx
                iimax = i + wkx + 1
                for ii in range(iimin, iimax):
                    iii = ii % nx
                    val = f[iii]
                    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(nx):
            if not npy_isnan(fixed[i]):
                top = 0.
                bot = 0.
                iimin = i - wkx
                iimax = i + wkx + 1
                for ii in range(iimin, iimax):
                    iii = ii % nx
                    val = fixed[iii]
                    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]
    # GIL acquired again here
    return conv


@cython.boundscheck(False)  # turn off bounds-checking for entire function
def convolve2d_boundary_wrap(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
    cdef np.ndarray[DTYPE_t, ndim=2] fixed = np.empty([nx, ny], dtype=DTYPE)
    cdef np.ndarray[DTYPE_t, ndim=2] conv = np.empty([nx, ny], dtype=DTYPE)
    cdef unsigned int i, j, iii, jjj
    cdef int ii, jj

    cdef int iimin, iimax, jjmin, jjmax

    cdef DTYPE_t top, bot, ker, val

    # release the GIL
    with nogil:
        # Need a first pass to replace NaN values with value convolved from
        # neighboring values
        for i in range(nx):
            for j in range(ny):
                if npy_isnan(f[i, j]):
                    top = 0.
                    bot = 0.
                    iimin = i - wkx
                    iimax = i + wkx + 1
                    jjmin = j - wky
                    jjmax = j + wky + 1
                    for ii in range(iimin, iimax):
                        for jj in range(jjmin, jjmax):
                            iii = ii % nx
                            jjj = jj % ny
                            val = f[iii, jjj]
                            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(nx):
            for j in range(ny):
                if not npy_isnan(fixed[i, j]):
                    top = 0.
                    bot = 0.
                    iimin = i - wkx
                    iimax = i + wkx + 1
                    jjmin = j - wky
                    jjmax = j + wky + 1
                    for ii in range(iimin, iimax):
                        for jj in range(jjmin, jjmax):
                            iii = ii % nx
                            jjj = jj % ny
                            val = fixed[iii, jjj]
                            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]
    # GIl acquired again here
    return conv


@cython.boundscheck(False)  # turn off bounds-checking for entire function
def convolve3d_boundary_wrap(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
    cdef np.ndarray[DTYPE_t, ndim=3] fixed = np.empty([nx, ny, nz], dtype=DTYPE)
    cdef np.ndarray[DTYPE_t, ndim=3] conv = np.empty([nx, ny, nz], dtype=DTYPE)
    cdef unsigned int i, j, k, iii, jjj, kkk
    cdef int ii, jj, kk

    cdef int iimin, iimax, jjmin, jjmax, kkmin, kkmax

    cdef DTYPE_t top, bot, ker, val

    # release the GIL
    with nogil:
        # Need a first pass to replace NaN values with value convolved from
        # neighboring values
        for i in range(nx):
            for j in range(ny):
                for k in range(nz):
                    if npy_isnan(f[i, j, k]):
                        top = 0.
                        bot = 0.
                        iimin = i - wkx
                        iimax = i + wkx + 1
                        jjmin = j - wky
                        jjmax = j + wky + 1
                        kkmin = k - wkz
                        kkmax = k + wkz + 1
                        for ii in range(iimin, iimax):
                            for jj in range(jjmin, jjmax):
                                for kk in range(kkmin, kkmax):
                                    iii = ii % nx
                                    jjj = jj % ny
                                    kkk = kk % nz
                                    val = f[iii, jjj, kkk]
                                    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(nx):
            for j in range(ny):
                for k in range(nz):
                    if not npy_isnan(fixed[i, j, k]):
                        top = 0.
                        bot = 0.
                        iimin = i - wkx
                        iimax = i + wkx + 1
                        jjmin = j - wky
                        jjmax = j + wky + 1
                        kkmin = k - wkz
                        kkmax = k + wkz + 1
                        for ii in range(iimin, iimax):
                            for jj in range(jjmin, jjmax):
                                for kk in range(kkmin, kkmax):
                                    iii = ii % nx
                                    jjj = jj % ny
                                    kkk = kk % nz
                                    val = fixed[iii, jjj, kkk]
                                    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]
    # GIL acquired again here
    return conv