forked from sep-developers/sep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.py
executable file
·206 lines (166 loc) · 5.9 KB
/
bench.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
#!/usr/bin/env python
"""Benchmarking SEP-PJW against equivalent photutils functions."""
from __future__ import print_function
import time
from os.path import join
import numpy as np
import sep_pjw as sep
# try to import photutils for comparison timing
try:
import photutils
HAVE_PHOTUTILS = True
except ImportError:
HAVE_PHOTUTILS = False
# Try to import any FITS reader
try:
from fitsio import read as getdata
HAVE_FITS = True
NEED_BYTESWAP = False
except ImportError:
try:
from astropy.io.fits import getdata
HAVE_FITS = True
except ImportError:
HAVE_FITS = False
CONDENSED = True
if HAVE_FITS:
rawdata = getdata(join("data", "image.fits")) # original is 256 x 256
data = np.tile(rawdata, (4, 4))
print("test image shape:", data.shape)
print("test image dtype:", data.dtype)
t0 = time.time()
bkg = sep.Background(data) # estimate background
t1 = time.time()
print("measure background: {0:6.2f} ms".format((t1 - t0) * 1.0e3))
t0 = time.time()
bkg.subfrom(data) # subtract it
t1 = time.time()
print("subtract background: {0:6.2f} ms".format((t1 - t0) * 1.0e3))
t0 = time.time()
backarr = bkg.back(dtype=np.float64) # background
t1 = time.time()
print("background array: {0:6.2f} ms".format((t1 - t0) * 1.0e3))
t0 = time.time()
rmsarr = bkg.rms()
t1 = time.time()
print("rms array: {0:6.2f} ms".format((t1 - t0) * 1.0e3))
t0 = time.time()
objects = sep.extract(data, 1.5 * bkg.globalrms)
t1 = time.time()
print(
"extract: {0:6.2f} ms [{1:d} objects]".format((t1 - t0) * 1.0e3, len(objects))
)
# --------------------------------------------------------------------------
# Background subtraction
print("")
if HAVE_PHOTUTILS:
print("sep version: ", sep.__version__)
print("photutils version:", photutils.__version__)
print("""
| test | sep | photutils | ratio |
|-------------------------|-----------------|-----------------|--------|""")
blankline = (
"| | | | |"
)
else:
print("sep version: ", sep.__version__)
print("""
| test | sep |
|-------------------------|-----------------|""")
blankline = "| | |"
nloop = 50
for ntile in [4]:
data = np.tile(rawdata, (ntile, ntile))
line = "| {0:4d}^2 image background |".format(data.shape[0])
t0 = time.time()
for _ in range(0, nloop):
bkg = sep.Background(data)
t1 = time.time()
t_sep = (t1 - t0) * 1.0e3 / nloop
line += " {0:7.2f} ms |".format(t_sep)
if HAVE_PHOTUTILS:
t0 = time.time()
for _ in range(0, nloop):
try:
bkg = photutils.background.Background(
data, (64, 64)
) # estimate background
except AttributeError:
bkg = photutils.background.Background2D(
data, (64, 64)
) # estimate background
t1 = time.time()
t_pu = (t1 - t0) * 1.0e3 / nloop
line += " {0:7.2f} ms | {1:6.2f} |".format(t_pu, t_pu / t_sep)
print(line)
# ------------------------------------------------------------------------------
# Circular aperture photometry benchmarks
if not CONDENSED:
print(blankline)
line = "| **aperture photometry** | |"
if HAVE_PHOTUTILS:
line += " | |"
print(line)
naper = 1000
data = np.ones((2000, 2000), dtype=np.float32)
x = np.random.uniform(200.0, 1800.0, naper)
y = np.random.uniform(200.0, 1800.0, naper)
if CONDENSED:
r_list = [5.0]
subpix_list = [(5, "subpixel", "subpix=5"), (0, "exact", "exact")]
else:
r_list = [3.0, 5.0, 10.0, 20.0]
subpix_list = [
(1, "center", "subpix=1"),
(5, "subpixel", "subpix=5"),
(0, "exact", "exact"),
]
for r in r_list:
for subpix, method, label in subpix_list:
line = "| circles r={0:2d} {1:8s} |".format(int(r), label)
t0 = time.time()
for _ in range(0, nloop):
flux, fluxerr, flag = sep.sum_circle(data, x, y, r, subpix=subpix)
t1 = time.time()
t_sep = (t1 - t0) * 1.0e6 / naper / nloop
line += " {0:7.2f} us/aper |".format(t_sep)
if HAVE_PHOTUTILS:
apertures = photutils.aperture.CircularAperture(np.column_stack((x, y)), r)
t0 = time.time()
for _ in range(0, nloop):
res = photutils.aperture.aperture_photometry(
data, apertures, method=method, subpixels=subpix
)
t1 = time.time()
t_pu = (t1 - t0) * 1.0e6 / naper / nloop
line += " {0:7.2f} us/aper | {1:6.2f} |".format(t_pu, t_pu / t_sep)
print(line)
if not CONDENSED:
print(blankline)
a = 1.0
b = 1.0
theta = np.pi / 4.0
for r in r_list:
for subpix, method, label in subpix_list:
line = "| ellipses r={0:2d} {1:8s} |".format(int(r), label)
t0 = time.time()
for _ in range(0, nloop):
flux, fluxerr, flag = sep.sum_ellipse(
data, x, y, a, b, theta, r, subpix=subpix
)
t1 = time.time()
t_sep = (t1 - t0) * 1.0e6 / naper / nloop
line += " {0:7.2f} us/aper |".format(t_sep)
if HAVE_PHOTUTILS:
apertures = photutils.aperture.EllipticalAperture(
np.column_stack((x, y)), a * r, b * r, theta
)
t0 = time.time()
for _ in range(0, nloop):
res = photutils.aperture.aperture_photometry(
data, apertures, method=method, subpixels=subpix
)
t1 = time.time()
t_pu = (t1 - t0) * 1.0e6 / naper / nloop
line += " {0:7.2f} us/aper | {1:6.2f} |".format(t_pu, t_pu / t_sep)
print(line)