-
Notifications
You must be signed in to change notification settings - Fork 2
/
ofiltsky.py
398 lines (304 loc) · 10.1 KB
/
ofiltsky.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
"""Module to replace IRAF's ``ofilt`` sky fitting algorithm."""
from __future__ import absolute_import, division, print_function
# THIRD-PARTY
import numpy as np
from astropy.stats import sigma_clip
from astropy.stats.funcs import gaussian_sigma_to_fwhm
from scipy import signal
from scipy.stats import skew
__all__ = ['fitsky_ofilter']
def fitsky_ofilter(data, k1=3.0, binsize=0.1, losigma=3.0,
hisigma=3.0, maxiter=10, hwidth=None, smooth=False,
sigclip_sigma=None, sigclip_iters=10):
"""Procedure to fit the peak and width of the histogram using
repeated convolutions and a triangle function.
Parameters
----------
data : array-like (float)
Array of sky pixels.
k1 : float, optional
Extent of the histogram in sky sigma.
binsize : float, optional
The size of the histogram in sky sigma.
losigma, hisigma : float, optional
Upper and lower sigma rejection limits.
maxiter : int, optional
Maximum number of rejection cycles.
hwidth : float or `None`, optional
Width of histogram.
smooth : bool, optional
Smooth the histogram before fitting.
sigclip_sigma : float or `None`, optional
The number of standard deviations to use as the clipping limit
when sigma-clipping the data *before* constructing the histogram.
If `None`, no clipping is done.
sigclip_iters : int, optional
The number of iterations to use when sigma-clipping the data
*before* constructing the histogram. This is only used if
``sigclip_sigma`` is given.
Returns
-------
sky_mode : float
Computed sky value.
sky_sigma : float
Computed sigma of the sky pixels.
sky_skew : float
Skew of sky pixels.
Raises
------
ValueError
Invalid inputs or calculation failed.
"""
data = np.asarray(data).flatten()
# Sigma clipping
if sigclip_sigma is not None:
skypix = sigma_clip(data, sigma=sigclip_sigma, iters=sigclip_iters)
skypix = skypix.data[~skypix.mask]
else:
skypix = data
if skypix.size < 1:
raise ValueError('No sky pixels provided')
# Compute a first guess for the parameters.
sky_zero = skypix.mean()
dmin = skypix.min()
dmax = skypix.max()
sky_sigma = skypix.std()
sky_skew = skew(skypix)
sky_mean = max(dmin, min(np.median(skypix), dmax))
# Compute the width and bin size of histogram.
if hwidth is None or hwidth <= 0:
cut = min(sky_mean - dmin, dmax - sky_mean, k1 * sky_sigma)
hmin = sky_mean - cut
hmax = sky_mean + cut
dh = binsize * cut / k1
else:
hmin = sky_mean - k1 * hwidth
hmax = sky_mean + k1 * hwidth
dh = binsize * hwidth
# Compute the number of histogram bins and the resolution filter.
if dh > 0:
nbins = 2 * int((hmax - sky_mean) / dh)
dh = (hmax - hmin) / (nbins - 1)
else:
nbins = 1
dh = 0.0
# Test for a valid histogram.
if (nbins < 2 or k1 <= 0 or sky_sigma <= 0 or dh <= 0 or sky_sigma <= dh):
raise ValueError('Unable to construct histogram')
# Accumulate the histogram.
hgm = np.histogram(skypix, bins=nbins, range=(hmin, hmax))[0]
# Toss out bad data
skypix = skypix[(skypix >= hmin) & (skypix <= hmax)]
# Recalculate after initial rejection.
sky_mean = skypix.mean()
sky_sigma = skypix.std()
sky_skew = skew(skypix)
# Fit the peak of the histogram.
hist_lo = hmin + 0.5 * dh
hist_hi = hmax + 0.5 * dh
center = apmapr((hmin + hmax) * 0.5, hist_lo, hist_hi,
1.0, nbins)
if smooth:
nker = max(1, int(sky_sigma / dh))
ker = signal.boxcar(nker)
shgm = signal.convolve(hgm, ker)
hgm = signal.convolve(shgm, ker) # Smoothed twice
center, iter = aptopt(hgm, center, sky_sigma / dh, maxiter=maxiter)
if iter < 0:
raise ValueError('Histogram centering failed, no convergence')
sky_mode = apmapr(center, 1.0, nbins, hist_lo, hist_hi)
sky_mode = max(dmin, min(sky_mode, dmax))
# No need to continue, return results.
if sky_sigma <= dh or maxiter < 1:
return sky_mode, sky_sigma, sky_skew
dhh = (nbins - 1) / (hmax - hmin)
# Fit the histogram with pixel rejection.
for i in range(maxiter):
# Compute new histogram limits.
locut = sky_mode - losigma * sky_sigma
hicut = sky_mode + hisigma * sky_sigma
# Detect and reject the pixels.
badmask = (skypix < locut) | (skypix > hicut)
if not np.any(badmask):
break
# Remove them from histogram and data.
ibad_hist = ((skypix[badmask] - hmin) * dhh).astype(np.int)
hgm[ibad_hist] -= 1
skypix = skypix[~badmask]
if skypix.size <= 0:
raise ValueError(
'No good sky pixels left (niter={0})'.format(i + 1))
# Recompute the data limits.
sky_mean = skypix.mean()
sky_sigma = skypix.std()
sky_skew = skew(skypix)
if sky_sigma <= dh:
break
# Refit the sky.
if smooth:
nker = max(1, int(sky_sigma / dh))
ker = signal.boxcar(nker)
shgm = signal.convolve(hgm, ker)
hgm = signal.convolve(shgm, ker) # Smoothed twice
center, iter = aptopt(hgm, center, sky_sigma / dh, maxiter=maxiter)
if iter < 0:
raise ValueError('Histogram centering failed, no convergence')
sky_mode = apmapr(center, 1.0, nbins, hist_lo, hist_hi)
sky_mode = max(dmin, min(sky_mode, dmax))
if sky_sigma <= 0:
sky_sigma = 0.0
sky_skew = 0.0
return sky_mode, sky_sigma, sky_skew
def apmapr(a, a1, a2, b1, b2):
"""Vector linear transformation.
Map the range of pixel values ``a1, a2`` from ``a``
into the range ``b1, b2`` into ``b``.
It is assumed that ``a1 < a2`` and ``b1 < b2``.
Parameters
----------
a : float
The value to be mapped.
a1, a2 : float
The numbers specifying the input data range.
b1, b2 : float
The numbers specifying the output data range.
Returns
-------
b : float
Mapped value.
"""
scalar = (b2 - b1) / (a2 - a1)
return max(b1, min(b2, (a - a1) * scalar + b1))
def ap_tprofder(npix, center, sigma, ampl=1.0):
"""Estimate the approximating triangle function and its derivatives.
Parameters
----------
npix : int
Number of elements in output arrays.
center, sigma : float
Center and sigma of input Gaussian function.
ampl : float, optional
Amplitude.
Returns
-------
data : array-like
Output data.
der : array-like
Derivatives.
"""
data = np.zeros(npix)
der = np.zeros(npix)
x = (np.arange(npix) - center + 0.5) / (sigma * gaussian_sigma_to_fwhm)
xabs = np.abs(x)
mask = xabs <= 1
data[mask] = ampl * (1 - xabs[mask])
der[mask] = x[mask] * data[mask]
return data, der
def apply_sign(x, y):
"""Return the absolute value of ``x`` multiplied by
the sign (i.e., +1 or -1) of ``y``."""
if y < 0:
fac = -1.0
else:
fac = 1.0
return abs(x) * fac
def apqzero(x, y, qtol=0.125):
"""Return the root of a quadratic function defined by three points."""
if len(x) != 3 or len(y) != 3:
raise ValueError('This function only accepts 3 points')
# Compute the determinant.
x2 = x[1] - x[0]
x3 = x[2] - x[0]
y2 = y[1] - y[0]
y3 = y[2] - y[0]
det = x2 * x3 * (x2 - x3)
# Compute the shift in x.
if abs(det) > 0:
a = (x3 * y2 - x2 * y3) / det
b = -(x3 * x3 * y2 - x2 * x2 * y3) / det
c = a * y[0] / (b * b)
if abs(c) > qtol:
dx = (-b / (2.0 * a)) * (1.0 - np.sqrt(1.0 - 4.0 * c))
else:
dx = -(y[0] / b) * (1.0 + c)
elif abs(y3) > 0:
dx = -y[0] * x3 / y3
else:
dx = 0.0
return dx
def aptopt(data, center, sigma, maxiter=10, tol=0.001, max_search=3):
"""One-dimensional centering routine using repeated convolutions to
locate image center.
Parameters
----------
data : array-like
Initial data.
center : float
Initial guess at center.
sigma : float
Sigma of Gaussian.
maxiter : int, optional
Maximum number of iterations.
tol : float, optional
Gap tolerance for sigma.
max_search : int, optional
Max initial search steps.
Returns
-------
result : float
Calculated center.
niter : int
Number of iterations used.
"""
if sigma <= 0:
return np.nan, -1
# Initialize.
wgt = ap_tprofder(data.size, center, sigma)[1]
s = np.zeros(3)
s[0] = np.dot(wgt, data)
if s[0] == 0:
return center, 0
x = np.zeros(3)
x[0] = center
s[2] = s[0]
# Search for the correct interval.
i = 0
while (i < max_search) and (s[2] * s[0] >= 0):
s[2] = s[0]
x[2] = x[0]
x[0] = x[2] + apply_sign(sigma, s[2])
wgt = ap_tprofder(data.size, x[0], sigma)[1]
s[0] = np.dot(wgt, data)
if s[0] == 0:
return x[0], 0
i += 1
# Location not bracketed.
if s[2] * s[0] > 0:
return np.nan, -1
# Intialize the quadratic search.
delx = x[0] - x[2]
x[1] = x[2] - s[2] * delx / (s[0] - s[2])
wgt = ap_tprofder(data.size, x[1], sigma)[1]
s[1] = np.dot(wgt, data)
if s[1] == 0:
return x[1], 1
# Search quadratically.
for niter in range(1, maxiter):
# Check for completion.
if s[1] == 0 or np.any(abs(x[1:] - x[:-1]) <= tol):
break
# Compute new intermediate value.
newx = x[0] + apqzero(x, s)
wgt = ap_tprofder(data.size, newx, sigma)[1]
news = np.dot(wgt, data)
if s[0] * s[1] > 0:
s[0] = s[1]
x[0] = x[1]
s[1] = news
x[1] = newx
else:
s[2] = s[1]
x[2] = x[1]
s[1] = news
x[1] = newx
return x[1], niter + 1