-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnoise.py
239 lines (200 loc) · 6.59 KB
/
noise.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
# &%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%
# Imports
# &%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats
from cube import *
import time
# &%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%
# The Noise Object
# &%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%
class Noise:
"""
...
"""
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# Contents
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
data = None
signal = None
backup = None
spec_axis = None
# Noise
scale = None
spec = None
map = None
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# Initialize and infrastructure
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
def __init__(
self,
data
):
"""
Construct a new mask object.
"""
self.data = data
self.spec_axis = data.spec_axis
def set_data(
self,
val=None
):
"""
Link the noise object to a data object.
"""
if val != None:
self.data = val
def set_signal_mask(
self,
val=None
):
"""
Link the noise object to a mask object.
"""
if val != None:
self.signal = val
def set_spectral_axis(
self,
val=None
):
"""
Set the spectral axis.
"""
if val != None:
self.spec_axis = val
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# Generate a noise estimate
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
def calc_1d(
self,
method="ROBUST",
timer=False,
verbose=False,
show=False,
showbins=100,
showsig=5.,
showlog=True):
"""
Calculate a single noise estimate for a data set.
"""
# .............................................................
# Time the operation if requested.
# .............................................................
if timer:
start=time.time()
# .............................................................
# Fit the 1-d noise distribution
# .............................................................
# Identify which values to fit - the data are valid and there
# is no signal associated with them.
use = self.data.valid
if self.signal != None:
use *= (self.signal.data == False)
if self.data.signal != None:
use *= (self.data.signal.data == False)
# Call the external noise fitter
self.scale = est_noise_1d(
self.data.data[use],
method=method)
# .............................................................
# Report and/or plot
# .............................................................
if verbose:
print "Fit 1d noise distribution value: ", self.scale
if show:
# Plot a histogram from -5 to +5 sigma
med = np.median(self.data.data[use])
low = med - showsig*self.scale
high = med + showsig*self.scale
bin_vals = np.arange(showbins)*(high-low)/(showbins-1.) + low
fig = plt.figure()
ax = fig.add_subplot(111)
n, bins, patches = ax.hist(
self.data.data[use],
bin_vals,
facecolor='blue',
log=showlog)
x_fid = np.arange(10.*showbins)*(high-low)/(showbins*10.) + low
y_fid = np.exp(-1.*(x_fid-med)**2/(2.*self.scale**2))* \
np.max(n)
ax.plot(x_fid,y_fid,'red',linewidth=4,label='fit RMS + median')
ax.set_xlabel('Data Value')
ax.set_ylabel('Counts')
ax.grid=True
plt.show()
# .............................................................
# Finish timing
# .............................................................
if timer:
stop=time.time()
print "Fitting the noise (1d) took ", stop-start
return
# &%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%
# Noise Routines
# &%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%
# These may be of general use and so are not part of the noise
# class. Instead they can be called piecemeal.
# ------------------------------------------------------------
# NOISE ESTIMATORS
# ------------------------------------------------------------
def est_noise_1d(
data,
method="ROBUST"):
"""
Calculate a single noise estimate for a vector.
"""
# Use a robust estimator (sigma clipping + M.A.D.)
if method == "ROBUST":
sig_thresh = \
sig_n_outliers(len(data),
n_out=1.0,
pos_only=True)
return sigma_rob(data,
iterations=1,
thresh=sig_thresh)
# Use the M.A.D. only.
if method == "MAD":
return mad(data)
# Default to the standard deviation
return numpy.std(data)
# ------------------------------------------------------------
# STASTICS HELPER PROCEDURES
# ------------------------------------------------------------
def mad(data, sigma=True):
"""
Return the median absolute deviation.
"""
med = np.median(data)
mad = np.median(np.abs(data - med))
if sigma==False:
return mad
else:
return mad*1.4826
def sigma_rob(data, iterations=1, thresh=3.0):
"""
Iterative m.a.d. based sigma with positive outlier rejection.
"""
noise = mad(data)
for i in range(iterations):
ind = (data <= thresh*noise).nonzero()
noise = mad(data[ind])
return noise
def sig_n_outliers(n_data, n_out=1.0, pos_only=True):
"""
Return the sigma needed to expect n (default 1) outliers given
n_data points.
"""
perc = float(n_out)/float(n_data)
if pos_only == False:
perc *= 2.0
return abs(scipy.stats.norm.ppf(perc))
# ------------------------------------------------------------
# Commentary
# ------------------------------------------------------------
# In theory the masked array class inside of numpy should expedite
# handling of blanked data (similarly the scipy.stats.nanmedian or
# nanstd functions). However, the masked array median operator seems
# to be either broken or infeasibly slow. This forces us into loops,
# which (shockingly) work out to be the fastest of the ways I have
# tried, but are still far from good.