-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysky.py
496 lines (396 loc) · 12.8 KB
/
mysky.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
"""
Gaussian fit to histogram of distribution and related
functions.
These functions were ported from existing IDL packages.
They are used by ACS/WFC reference files generation and
statistics packages.
:Authors: Pey Lian Lim (Python), others (IDL)
:Organization: Space Telescope Science Institute
:History:
* 2011/08/01 PLL changed documentation to Sphinx format.
* 2010/03/04 PLL modified documentations to comply with Epydoc convention.
* 2009/11/24 PLL converted these functions from IDL to Python.
"""
# External modules
import numpy, pylab, scipy
from scipy import optimize
def robust_sigma(in_y, zero=0):
"""
Calculate a resistant estimate of the dispersion of
a distribution. For an uncontaminated distribution,
this is identical to the standard deviation.
Use the median absolute deviation as the initial
estimate, then weight points using Tukey Biweight.
See, for example, Understanding Robust and
Exploratory Data Analysis, by Hoaglin, Mosteller
and Tukey, John Wiley and Sons, 1983.
.. note:: ROBUST_SIGMA routine from IDL ASTROLIB.
:History:
* H Freudenreich, STX, 8/90
* Replace MED call with MEDIAN(/EVEN), W. Landsman, December 2001
* Converted to Python by P. L. Lim, 11/2009
Examples
--------
>>> result = robust_sigma(in_y, zero=1)
Parameters
----------
in_y: array_like
Vector of quantity for which the dispersion is
to be calculated
zero: int
If set, the dispersion is calculated w.r.t. 0.0
rather than the central value of the vector. If
Y is a vector of residuals, this should be set.
Returns
-------
out_val: float
Dispersion value. If failed, returns -1.
"""
# Flatten array
y = in_y.reshape(in_y.size, )
eps = 1.0E-20
c1 = 0.6745
c2 = 0.80
c3 = 6.0
c4 = 5.0
c_err = -1.0
min_points = 3
if zero:
y0 = 0.0
else:
y0 = numpy.median(y)
dy = y - y0
del_y = abs( dy )
# First, the median absolute deviation MAD about the median:
mad = numpy.median( del_y ) / c1
# If the MAD=0, try the MEAN absolute deviation:
if mad < eps:
mad = numpy.mean( del_y ) / c2
if mad < eps:
return 0.0
# Now the biweighted value:
u = dy / (c3 * mad)
uu = u*u
q = numpy.where(uu <= 1.0)
count = len(q[0])
if count < min_points:
print 'ROBUST_SIGMA: This distribution is TOO WEIRD! Returning', c_err
return c_err
numerator = numpy.sum( (y[q]-y0)**2.0 * (1.0-uu[q])**4.0 )
n = y.size
den1 = numpy.sum( (1.0-uu[q]) * (1.0-c4*uu[q]) )
siggma = n * numerator / ( den1 * (den1 - 1.0) )
if siggma > 0:
out_val = numpy.sqrt( siggma )
else:
out_val = 0.0
return out_val
def meanclip(indata, clipsig=3.0, maxiter=5, converge_num=0.02, verbose=0):
"""
Computes an iteratively sigma-clipped mean on a
data set. Clipping is done about median, but mean
is returned.
.. note:: MYMEANCLIP routine from ACS library.
:History:
* 21/10/1998 Written by RSH, RITSS
* 20/01/1999 Added SUBS, fixed misplaced paren on float call, improved doc. RSH
* 24/11/2009 Converted to Python. PLL.
Examples
--------
>>> mean, sigma = meanclip(indata)
Parameters
----------
indata: array_like
Input data.
clipsig: float
Number of sigma at which to clip.
maxiter: int
Ceiling on number of clipping iterations.
converge_num: float
If the proportion of rejected pixels is less than
this fraction, the iterations stop.
verbose: {0, 1}
Print messages to screen?
Returns
-------
mean: float
N-sigma clipped mean.
sigma: float
Standard deviation of remaining pixels.
"""
# Flatten array
skpix = indata.reshape( indata.size, )
ct = indata.size
iter = 0; c1 = 1.0 ; c2 = 0.0
while (c1 >= c2) and (iter < maxiter):
lastct = ct
medval = numpy.median(skpix)
sig = numpy.float64( numpy.std(skpix) ) # Bug - Need to recast
wsm = numpy.where( abs(skpix-medval) < clipsig*sig )
ct = len(wsm[0])
if ct > 0:
skpix = skpix[wsm]
c1 = abs(ct - lastct)
c2 = converge_num * lastct
iter += 1
# End of while loop
mean = numpy.mean( skpix )
sigma = robust_sigma( skpix )
if verbose:
prf = 'MEANCLIP:'
print '%s %.1f-sigma clipped mean' % (prf, clipsig)
print '%s Mean computed in %i iterations' % (prf, iter)
print '%s Mean = %.6f, sigma = %.6f' % (prf, mean, sigma)
return mean, sigma
def mytotal(inarray, axis, type='meanclip'):
"""
Collapse 2-D array in one dimension.
.. note:: MYTOTAL routine from ACS library.
:History:
* Obtained from M. Sirianni.
* Modified and converted to Python by P. L. Lim in 2009.
Examples
--------
>>> collapsed_array = mytotal(inarray, 1, type='median')
Parameters
----------
inarray: array_like
Input 2-D array.
axis: {1, 2}
Axis to collapse.
* 1: Return values along Y.
* 2: Return values along X.
type: {'median', 'meanclip', 'stdev'}
Algorithm to use.
Returns
-------
out_arr: array_like
1-D array collapsed along desired axis with desired
algorithm.
"""
func_name = 'MYTOTAL'
out_arr = 0.0
# Check inarray
if inarray.ndim != 2:
print '%s: Input array must be 2D' % func_name
return out_arr
# Check axis
if axis == 1:
n_out = inarray.shape[0]
elif axis == 2:
n_out = inarray.shape[1]
else:
print func_name, ': Axis not supported -', axis
return out_arr
# Initialize output array
out_arr = numpy.zeros(n_out)
out_rng = range(0, n_out)
# Check type
if type == 'meanclip':
for i in out_rng:
if axis == 1:
im_i = inarray[i,:]
else:
im_i = inarray[:,i]
mmean, msigma = meanclip(im_i, maxiter=10, converge_num=0.001)
out_arr[i] = mmean
elif type == 'stdev':
for i in out_rng:
if axis == 1:
im_i = inarray[i,:]
else:
im_i = inarray[:,i]
mmean, msigma = meanclip(im_i, maxiter=10, converge_num=0.001)
out_arr[i] = msigma
elif type == 'median':
for i in out_rng:
if axis == 1:
im_i = inarray[i,:]
else:
im_i = inarray[:,i]
out_arr[i] = numpy.median(im_i)
else:
print func_name, ': Type not supported -', type
out_arr = 0.0
return out_arr
def gaussian(height, center_x, width_x):
"""
Returns a gaussian function with the given parameters.
This is used for least square fitting optimization.
.. note:: This is used by `msky`.
Parameters
----------
height: float
Peak amplitude.
center_x: float
Peak location.
width_x: float
Sigma of gaussian curve.
Returns
-------
x: lambda function
Function used for optimization.
"""
return lambda x: height * numpy.exp(-(center_x-x)**2/(2.0*width_x**2))
def msky(inarray, do_plot=0, verbose=0, ptitle='', func=0):
"""
Find modal sky on an array.
First step is determination of median value and sigma.
Histogram of the data and fit parabola to the
logaritmic histogram. The coefficient of the parabola
are used to get mode and sigma of the sky on the
assumption that it is well fitted by a gaussian or
2nd-degree polynomial.
.. note:: MYSKY5 routine from ACS library.
:History:
* 11/25/2009 Created by PLL based on IDL routine by MS.
* 01/29/2010 PLL fixed broken where statement for histogram.
Parameters
----------
inarray: array_like
Input data.
do_plot: {0, 1}
Do plot?
verbose: {0, 1}
Print info to screen?
ptitle: string
Title of plot. Only used if `do_plot`=1.
func: {0, 1}
Function for fitting:
* 0: 2nd degree polynomial
* 1: Gaussian
Returns
-------
mmean: float
Mode of fitted function.
sigma: float
Sigma of fitted function.
"""
# Constants
func_name = 'MSKY'
nsig = 8.0
c1 = 2.5 # 2.8
c2 = 0.8 # 1.3
# Min/max of input array
arr_min = numpy.min(inarray)
arr_max = numpy.max(inarray)
# Get sigma
mmean, sigma = meanclip(inarray, clipsig=5.0, maxiter=10, verbose=verbose)
if sigma <= 0:
print ' '
print '%s: Weird distribution' % func_name
print '%-6s: %.6f' % ('MEAN', mmean)
print '%-6s: %.6f' % ('STDDEV', sigma)
print '%-6s: %.6f' % ('MIN', arr_min)
print '%-6s: %.6f' % ('MAX', arr_max)
return mmean, sigma
# Print info
if verbose:
print ' '
print '%s input array info' % func_name
print '%-6s: %.6f' % ('MIN', arr_min)
print '%-6s: %.6f' % ('MAX', arr_max)
# Flatten input array
arr_1d = inarray.reshape( inarray.size, )
# Define min and max for the histogram
x = nsig * sigma
mmean = numpy.median(arr_1d)
minhist = mmean - x
maxhist = mmean + x
ufi = inarray[ numpy.where((inarray > minhist) & (inarray < maxhist)) ]
# Calculate 25% and 75% percentile to get the interquartile range
# IRQ = pc75-pc25
# zenman, A. J. 1991.
sixd = numpy.argsort( ufi )
ndata = ufi.size
pc25 = ufi[ sixd[0.25*ndata] ]
pc75 = ufi[ sixd[0.75*ndata] ]
irq = pc75 - pc25
step = 2.0 * irq * ndata**(-1.0/3.0)
# Calculate number of bins to use
nbin = round(2*x / step - 1)
# Histogram
# http://www.scipy.org/Tentative_NumPy_Tutorial
yhist, hbin = numpy.histogram(arr_1d, range=(minhist,maxhist), bins=nbin)
xhist = 0.5 * ( hbin[1:] + hbin[:-1] )
# Define xmin and xmax for the 2-0rder fit
x1 = mmean - c1*sigma
x2 = mmean + c2*sigma
# Select the points beween x1 and x2 for the fit
w = numpy.where((xhist > x1) & (xhist < x2) & (yhist > 0))
count = len(w[0])
xwg = xhist[w]
nywg = yhist[w]
if count < 2:
print ' '
print '%s: Singular matrix' % func_name
print '%-6s: %.6f' % ('X[W]', xwg)
print '%-6s: %.6f' % ('NY[W]', nywg)
print '%-6s: %.6f' % ('MEDIAN', mmean)
return mmean, sigma
# Change to log scale
yhist = numpy.log10(yhist)
iyh = numpy.where( ~numpy.isinf(yhist) )
xhist = xhist[iyh]
yhist = yhist[iyh]
nywg = numpy.log10(nywg)
# Calculate the fit coefficients
ymax = nywg.max()
# Gaussian
# http://www.scipy.org/Cookbook/FittingData
if func == 1:
if verbose: print '%s: Fitting gaussian' % func_name
# Initial guess
ysum = numpy.sum(nywg)
mmean = numpy.sum(xwg * nywg) / ysum
sigma = numpy.sqrt(numpy.abs(numpy.sum((xwg-mmean)**2*nywg)/ ysum))
params = (ymax, mmean, sigma)
# Error function to minimize
errorfunction = lambda p: scipy.ravel(gaussian(*p)(xhist) - yhist)
# Linear least square fitting
a_opt, a_success = optimize.leastsq(errorfunction, params)
ymax = a_opt[0]
mmean = a_opt[1]
sigma = a_opt[2]
# Fit to entire range
yall = ymax * numpy.exp(-(xhist-mmean)**2/(2.0*sigma**2))
# 2nd degree polynomial
else:
if verbose: print '%s: Fitting 2nd deg polynomial' % func_name
# Polynomial
a_opt = numpy.polyfit(xwg, nywg, 2)
mmean = -0.5*a_opt[1]/a_opt[0]
sigma = numpy.sqrt(-0.5 / ( a_opt[0]*numpy.log(10) ) )
# Fit to entire range
yall = a_opt[0]*xhist**2 + a_opt[1]*xhist + a_opt[2]
# Print results
if verbose:
print ' '
print '%s: Results' % func_name
print '%-6s: %.6f' % ('MODE', mmean)
print '%-6s: %.6f' % ('SIGMA', sigma)
print ' '
plot_y1 = 0
plot_y2 = ymax + 0.1
# Plot results
if do_plot:
pylab.clf()
# Data points
pylab.plot(xhist, yhist, 'ko')
# Mark fitted region
pylab.plot(xwg, nywg, 'bo', hold=1)
# Draw fitted function
pylab.plot(xhist, yall, 'r--', hold=1)
pylab.axvline(x=mmean, color='r')
# Plot xmin xmax for the fit
pylab.axvline(x=x1, ymin=0, ymax=0.1, color='b')
pylab.axvline(x=x2, ymin=0, ymax=0.1, color='b')
# Axis limits and labels
pylab.axis([minhist, maxhist, plot_y1, plot_y2])
pylab.xlabel('Pix value')
pylab.ylabel('Log num of pix')
pylab.title(ptitle)
#pylab.show()
# Pause for visual examination of plot
x_user = raw_input('ENTER ANY CHAR TO CONTINUE: ')
return mmean, sigma