-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.py
233 lines (183 loc) · 7.78 KB
/
tests.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
import numpy as np
import time
import matplotlib.pyplot as plt
import scipy.signal
from scipy.linalg import inv
import savitzky_golay_werrors
###################################################################################################
def main():
testSine()
#testSineEqualWeights()
#testSine_wsimplecov()
testSine_wcov()
#testSpeed()
return
###################################################################################################
def testSine_wcov():
scatter_av = 0.1
scatter_sigma = 0.05
window = 15
order = 4
xx = np.arange(0.0, 10.0, 0.01)
y_true = np.sin(xx)
x = np.arange(0.0, 10.0, 0.2)
y = np.sin(x)
np.random.seed(250)
q_err = (np.abs(np.random.normal(scatter_av, scatter_sigma,
(len(x)))))
cov = np.diag(np.ones(q_err.size))
# A block diagonal covariance matrix where the correlation coefficient
# falls as a function of offset from the diagonal
for i in range(q_err.size):
for offset in range(1, q_err.size):
if i+offset >= q_err.size:
continue
cov[i, i+offset] = 0.3/offset**2
cov[i+offset, i] = 0.3/offset**2
for i in range(q_err.size):
for j in range(i, q_err.size):
cov[i, j] = cov[i, j] * q_err[i] * q_err[j]
cov[j, i] = cov[i, j]
y = np.random.multivariate_normal(y, cov)
sg = scipy.signal.savgol_filter(y, window, order, deriv = 0)
sg_err = savitzky_golay_werrors.savgol_filter_werror(y, window, order, cov=cov, deriv=None)
fig = plt.figure()
ax = fig.add_subplot(2, 1, 1)
ax.set_ylim([-2, 2])
import palettable
ax.set_color_cycle(palettable.colorbrewer.qualitative.Dark2_8.mpl_colors)
ax.errorbar(x, y, yerr = q_err, fmt = '.', marker = 'o', ms = 3.0,
label = 'Noisy data with covariant errors')
ax.plot(x, sg_err, '-', label = 'This work')
ax.plot(x, sg, '-', label = 'Traditional SG')
ax.plot(xx, y_true, '-', label = 'Noiseless')
ax.legend(loc = 3, frameon=0, ncol=2, fontsize=13)
ax.set_ylabel("y(x)")
ax.set_xticklabels([])
# Let us compute chisquare from the truth
# Chi-squared values compared to the underlying truth
print ("Chi-squared values compared to truth")
chisq_trad_truth = (np.dot(np.dot((sg-np.sin(x)).T, inv(cov)), (sg-np.sin(x))))
print ("Traditional: %.2f " % chisq_trad_truth )
chisq_new_truth = (np.dot(np.dot((sg_err-np.sin(x)).T, inv(cov)), (sg_err-np.sin(x))))
print ("This work: %.2f " % chisq_new_truth )
print ("Chi-squared values compared to data")
chisq_trad_data = (np.dot(np.dot((sg-y).T, inv(cov)), (sg-y)) )
print ("Traditional: %.2f " % chisq_trad_data)
chisq_new_data = (np.dot(np.dot((sg_err-y).T, inv(cov)), (sg_err-y)))
print ("This work: %.2f " % chisq_new_data)
ax = fig.add_subplot(2, 1, 2)
ax.set_color_cycle(palettable.colorbrewer.qualitative.Dark2_8.mpl_colors)
ax.errorbar(x, (y-np.sin(x))/q_err, q_err/q_err, fmt = '.', marker = 'o', ms = 3.0)
ax.plot(x, (sg_err-np.sin(x))/q_err, '-', label = r'This work $\chi^2=%.2f$' % chisq_new_truth)
ax.plot(x, (sg-np.sin(x))/q_err, '-', label= "Traditional SG $\chi^2=%.2f$" % chisq_trad_truth)
ax.axhline(0.0, color='grey')
ax.set_xlabel("x")
ax.set_ylabel(r"[y-sin(x)]/$\sigma_y$")
ax.legend(loc = 3, frameon=0, ncol=2, fontsize=13)
ax.set_ylim([-3, 3])
plt.tight_layout()
plt.savefig('Test_Sine_wcov.pdf')
return
###################################################################################################
def testSine():
scatter_av = 0.1
scatter_sigma = 0.05
window = 15
order = 4
xx = np.arange(0.0, 10.0, 0.01)
y_true = np.sin(xx)
x = np.arange(0.0, 10.0, 0.2)
y = np.sin(x)
np.random.seed(250)
q_err = np.abs(np.random.normal(scatter_av, scatter_sigma, (len(x))))
for i in range(len(x)):
y[i] += np.random.normal(0.0, q_err[i])
sg = scipy.signal.savgol_filter(y, window, order, deriv = 0)
sg_err = savitzky_golay_werrors.savgol_filter_werror(y, window, order, q_err, deriv=None)
fig = plt.figure()
ax = fig.add_subplot(2, 1, 1)
ax.set_ylim([-2, 2])
import palettable
ax.set_color_cycle(palettable.colorbrewer.qualitative.Dark2_8.mpl_colors)
ax.errorbar(x, y, yerr = q_err, fmt = '.', marker = 'o', ms = 3.0,
label = 'Noisy data, independent errors')
ax.plot(x, sg_err, '-', label = 'This work')
ax.plot(x, sg, '-', label = 'Traditional SG')
ax.plot(xx, y_true, '-', label = 'Noiseless')
ax.legend(loc = 3, frameon=0, ncol=2, fontsize=13)
ax.set_ylabel("y(x)")
ax.set_xticklabels([])
cov = np.diag(q_err**2.0)
print ("Chi-squared values compared to data")
chisq_trad_data = (np.dot(np.dot((sg-y).T, inv(cov)), (sg-y)) )
print ("Traditional: %.2f " % chisq_trad_data)
chisq_new_data = (np.dot(np.dot((sg_err-y).T, inv(cov)), (sg_err-y)))
print ("This work: %.2f " % chisq_new_data)
print ("Chi-squared values compared to truth")
chisq_trad_truth = (np.dot(np.dot((sg-np.sin(x)).T, inv(cov)), (sg-np.sin(x))) )
print ("Traditional: %.2f " % chisq_trad_truth)
chisq_new_truth = (np.dot(np.dot((sg_err-np.sin(x)).T, inv(cov)), (sg_err-np.sin(x))))
print ("This work: %.2f " % chisq_new_truth)
ax = fig.add_subplot(2, 1, 2)
ax.set_color_cycle(palettable.colorbrewer.qualitative.Dark2_8.mpl_colors)
ax.errorbar(x, (y-np.sin(x))/q_err, q_err/q_err, fmt = '.', marker = 'o', ms = 3.0)
ax.plot(x, (sg_err-np.sin(x))/q_err, '-', label = 'This work $\chi^2=%.2f$' % chisq_new_truth)
ax.plot(x, (sg-np.sin(x))/q_err, '-', label = 'Traditional SG $\chi^2=%.2f$' % chisq_trad_truth)
ax.axhline(0.0, color='grey')
ax.set_xlabel("x")
ax.set_ylabel("(y-sin(x))/$\sigma_y$")
ax.set_ylim([-3, 3])
ax.legend(loc = 3, frameon=0, ncol=2, fontsize=13)
plt.tight_layout()
plt.savefig('Test_Sine.pdf')
return
###################################################################################################
def testSineEqualWeights():
scatter = 0.1
window = 15
order = 4
x = np.arange(0.0, 10.0, 0.2)
xx = np.arange(0.0, 10.0, 0.01)
y_true = np.sin(xx)
y = np.sin(x)
np.random.seed(152)
s = np.random.normal(0.0, scatter, (len(x)))
y = y + s
q_err = np.ones((len(x)), np.float) * scatter
sg = scipy.signal.savgol_filter(y, window, order, deriv = 0)
sg_err = savitzky_golay_werrors.savgol_filter_werror(y, window, order, q_err, deriv=None)
plt.figure()
plt.errorbar(x, y, yerr = q_err, fmt = '.', marker = 'o', ms = 3.0, label = 'data')
plt.plot(xx, y_true, ':', label = 'True')
plt.plot(x, sg, '-', label = 'SG')
plt.plot(x, sg_err, '--', label = 'SG new')
ax.legend(loc = 3, frameon=0, ncol=2, fontsize=13)
plt.savefig('Test_Sine_EqualWeights.pdf')
return
###################################################################################################
def testSpeed():
scatter = 0.1
window = 15
order = 4
N = 1000
x = np.arange(0.0, 10.0, 0.2)
y = np.sin(x)
np.random.seed(152)
s = np.random.normal(0.0, scatter, (len(x)))
y = y + s
q_err = np.ones((len(x)), np.float) * scatter
t1 = time.clock()
for dummy in range(N):
_ = scipy.signal.savgol_filter(y, window, order, deriv = 0)
print('Numpy: %.2f s' % (time.clock() - t1))
t1 = time.clock()
for dummy in range(N):
_ = savitzky_golay_werrors.savgol_filter_werror(y, window, order, q_err, deriv=None)
print('New: %.2f s' % (time.clock() - t1))
return
###################################################################################################
# Trigger
###################################################################################################
if __name__ == "__main__":
main()