forked from astropy/astropy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_convolve_kernels.py
More file actions
159 lines (123 loc) · 4.34 KB
/
test_convolve_kernels.py
File metadata and controls
159 lines (123 loc) · 4.34 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
# Licensed under a 3-clause BSD style license - see LICENSE.rst
import itertools
import numpy as np
import pytest
from numpy.testing import assert_allclose, assert_almost_equal
from astropy import units as u
from astropy.convolution.convolve import convolve, convolve_fft
from astropy.convolution.kernels import (
Box2DKernel,
Gaussian2DKernel,
Moffat2DKernel,
Tophat2DKernel,
)
SHAPES_ODD = [[15, 15], [31, 31]]
SHAPES_EVEN = [[8, 8], [16, 16], [32, 32]] # FIXME: not used ?!
NOSHAPE = [[None, None]]
WIDTHS = [2, 3, 4, 5]
KERNELS = []
for shape in SHAPES_ODD + NOSHAPE:
for width in WIDTHS:
KERNELS.append(
Gaussian2DKernel(
x_stddev=width,
x_size=shape[0],
y_size=shape[1],
mode="oversample",
factor=10,
)
)
KERNELS.append(
Box2DKernel(
width=width,
x_size=shape[0],
y_size=shape[1],
mode="oversample",
factor=10,
)
)
KERNELS.append(
Tophat2DKernel(
radius=width,
x_size=shape[0],
y_size=shape[1],
mode="oversample",
factor=10,
)
)
KERNELS.append(
Moffat2DKernel(
gamma=width,
alpha=2,
x_size=shape[0],
y_size=shape[1],
mode="oversample",
factor=10,
)
)
class Test2DConvolutions:
@pytest.mark.parametrize("kernel", KERNELS)
def test_centered_makekernel(self, kernel):
"""
Test smoothing of an image with a single positive pixel
"""
shape = kernel.array.shape
x = np.zeros(shape)
xslice = tuple(slice(sh // 2, sh // 2 + 1) for sh in shape)
x[xslice] = 1.0
c2 = convolve_fft(x, kernel, boundary="fill")
c1 = convolve(x, kernel, boundary="fill")
assert_almost_equal(c1, c2, decimal=12)
@pytest.mark.parametrize("kernel", KERNELS)
def test_random_makekernel(self, kernel):
"""
Test smoothing of an image made of random noise
"""
shape = kernel.array.shape
x = np.random.randn(*shape)
c2 = convolve_fft(x, kernel, boundary="fill")
c1 = convolve(x, kernel, boundary="fill")
# not clear why, but these differ by a couple ulps...
assert_almost_equal(c1, c2, decimal=12)
@pytest.mark.parametrize(
("shape", "width"), list(itertools.product(SHAPES_ODD, WIDTHS))
)
def test_uniform_smallkernel(self, shape, width):
"""
Test smoothing of an image with a single positive pixel
Uses a simple, small kernel
"""
if width % 2 == 0:
# convolve does not accept odd-shape kernels
return
kernel = np.ones([width, width])
x = np.zeros(shape)
xslice = tuple(slice(sh // 2, sh // 2 + 1) for sh in shape)
x[xslice] = 1.0
c2 = convolve_fft(x, kernel, boundary="fill")
c1 = convolve(x, kernel, boundary="fill")
assert_almost_equal(c1, c2, decimal=12)
@pytest.mark.parametrize(
("shape", "width"), list(itertools.product(SHAPES_ODD, [1, 3, 5]))
)
def test_smallkernel_Box2DKernel(self, shape, width):
"""
Test smoothing of an image with a single positive pixel
Compares a small uniform kernel to the Box2DKernel
"""
kernel1 = np.ones([width, width]) / float(width) ** 2
kernel2 = Box2DKernel(width, mode="oversample", factor=10)
x = np.zeros(shape)
xslice = tuple(slice(sh // 2, sh // 2 + 1) for sh in shape)
x[xslice] = 1.0
c2 = convolve_fft(x, kernel2, boundary="fill")
c1 = convolve_fft(x, kernel1, boundary="fill")
assert_almost_equal(c1, c2, decimal=12)
c2 = convolve(x, kernel2, boundary="fill")
c1 = convolve(x, kernel1, boundary="fill")
assert_almost_equal(c1, c2, decimal=12)
def test_gaussian_2d_kernel_quantity():
# Make sure that the angle can be a quantity
kernel1 = Gaussian2DKernel(x_stddev=2, y_stddev=4, theta=45 * u.deg)
kernel2 = Gaussian2DKernel(x_stddev=2, y_stddev=4, theta=np.pi / 4)
assert_allclose(kernel1.array, kernel2.array)