/
bplot.py
327 lines (222 loc) · 7.77 KB
/
bplot.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
from matplotlib.pylab import plt
import numpy as np
from numpy import *
import keptoy
import pbls
import blsw
import find_blocks
import bb
from scipy.ndimage import median_filter
from scipy.stats import ks_2samp
from peakdetect import peakdet
def p2d(p,farr,ph):
"""
Shows the power for the BLS 2D spectrum
"""
# first index is plotted as y,this should be the other way around
p = np.swapaxes(p,0,1)
f = plt.gcf()
f.clf()
# create two axes, one for the image, the other for the profile.
r1d = [0.1,0.75,0.8,0.2]
r2d = [0.1,0.1,0.8,0.65]
ax1d = plt.axes(r1d)
ax2d = plt.axes(r2d,sharex=ax1d)
power = np.max(p,axis=0)
ax1d.plot(farr,power)
ax1d.set_ylabel('Power')
ax2d.pcolorfast(farr,ph,p)
ax2d.set_xlabel('frequency days^-1')
ax2d.set_ylabel('phase of trans / 2 pi')
plt.show()
def phasemov():
parr=np.linspace(0,2*pi,30)
for i in range(len(parr)):
f,t = keptoy.lightcurve(s2n=1000,P=10.,phase=parr[i])
nf,fmin,df,nb,qmi,qma,n = pbls.blsinit(t,f,nf=1000)
p,farr,ph = blsw.blswph(t,f,nf,fmin,df,nb,qmi,qma,n)
p2d(p,farr,ph)
f = plt.gcf()
f.text(0.8,0.7, "EEBLS Phase %.2f" % (parr[i]) ,ha="center",
bbox=dict(boxstyle="round", fc="w", ec="k"))
f.savefig('frames/ph%02d.png' % i)
plt.show()
def blocks(t,f,last,val):
"""
Plot lines for the Bayesian Blocks Algorithm
"""
fig = plt.gcf()
fig.clf()
ax = fig.add_subplot(111)
ax.plot(t,f,'o',ms=1,alpha=0.5)
cp = unique(last)
n = len(t)
idxlo = cp # index of left side of region
idxhi = append(cp[1:],n)-1 # index of right side of region
ax.hlines(val[idxhi],t[idxlo],t[idxhi],'red',lw=5)
ax.set_xlabel('Time (days)')
ax.set_ylabel('Flux (normalized)')
plt.show()
def ncp(s2n):
"""
Explore the output of BB as we change the prior on the number of
change points
"""
nncp = logspace(0.5,1.5,9)
f,t = keptoy.lightcurve(s2n=s2n)
sig = zeros(len(t)) + std(f)
for i in range( len(nncp) ):
last,val = find_blocks.pt( t,f,sig,ncp=nncp[i] )
blocks(t,f,last,val)
ax = plt.gca()
ax.set_title('s2n - %.1e, cpts prior - %.2e' % (s2n,nncp[i]) )
fig = plt.gcf()
fig.savefig('frames/s2n-%.1e_%02d.png' % (s2n,i) )
plt.show()
def phasemov():
ph=linspace(0,2*pi,30)
for i in range(len(ph)):
f,t = keptoy.lightcurve(s2n=1000,P=10.,phase=ph[i])
nf,fmin,df,nb,qmi,qma,n = pbls.blsinit(t,f,nf=1000)
p = blswph(t,f,nf,fmin,df,nb,qmi,qma,n)
f = plt.gcf()
f.clf()
ax = f.add_subplot(111)
ax.imshow(p,aspect='auto')
ax.set_xlabel('bin number of start of transit')
ax.set_ylabel('frequency')
ax.set_title('2D BLS spectrum - phase %.2f' % ph[i])
f.savefig('frames/ph%02d.png' % i)
plt.show()
def blsbb():
"""
Simulate a time series. Compare two bls spectra.
1. Normal BLS
2. Blocks returned by BB
Also plot the timeseries.
"""
s2n = logspace(0.5,2,8)
for i in range( len(s2n) ):
f,t = keptoy.lightcurve(s2n = s2n[i],tbase=600)
sig = zeros(len(t)) + std(f)
last,val = find_blocks.pt(t,f,sig,ncp=5)
fb = bb.get_blocks(f,last,val)
o = pbls.blswrap(t,f,blsfunc=blsw.blsw,nf=1000)
ob = pbls.blswrap(t,fb,blsfunc=blsw.blsw,nf=1000)
fig = plt.gcf()
fig.clf()
ax = fig.add_subplot(211)
ax.plot(t,f+1,'o',ms=1,alpha=0.5)
cp = unique(last)
n = len(t)
idxlo = cp # index of left side of region
idxhi = append(cp[1:],n)-1 # index of right side of region
ax.hlines(val[idxhi],t[idxlo],t[idxhi],'red',lw=5)
ax.set_ylabel('Flux (normalized)')
ax.set_xlabel('Flux (normalized)')
ax = fig.add_subplot(212)
ax.plot(o['farr'],o['p'] ,label = 'BLS')
ax.plot(ob['farr'],ob['p'] ,label = 'BLS + BB')
ax.set_xlabel('Frequency days^-1')
ax.set_ylabel('SR')
ax.legend()
fig.text(0.9,0.9, "S/N - %.1e" % (s2n[i]) ,
ha="center",fontsize=36,
bbox=dict(boxstyle="round", fc="w", ec="k"))
fig.savefig('frames/blsbb%02d.png' % i)
plt.show()
def bbfold(P):
# Trial Data:
tbase = 90.
f,t = keptoy.lightcurve(tbase=tbase,s2n=5,P=10.1)
o = pbls.blswrap(t,f,blsfunc=blsw.blsw)
farr = linspace(min(o['farr']),max(o['farr']),100)
sig = zeros(len(f)) + std(f)
# for fq in farr:
tfold = mod(t,P) # Fold according to trial period.
sidx = argsort(tfold)
tfold = tfold[sidx]
ffold = f[sidx]
last,val = find_blocks.pt(tfold,ffold,sig,ncp=8)
fig = plt.gcf()
fig.clf()
ax = fig.add_subplot(111)
ax.plot(tfold,ffold,'o',ms=1,alpha=0.5)
cp = unique(last)
n = len(t)
idxlo = cp # index of left side of region
idxhi = append(cp[1:],n)-1 # index of right side of region
ax.hlines(val[idxhi],tfold[idxlo],tfold[idxhi],'red',lw=5)
ax.set_ylabel('Flux (normalized)')
plt.show()
def perfind():
"""
Test how good the KS test is at descriminating a real planet from
a none.
"""
P = 200.
tbase = 1000.
nf = 500
s2n = logspace( log10(3),log10(15),5 )
print "S/N sig-noise noise-noise"
for s in s2n:
# Generate a lightcurve.
f,t = keptoy.lightcurve(P=P,tbase=tbase,s2n=s)
o = blsw.blswrap(t,f,nf=nf,fmax=1/50.)
# Generate a null lightcurve
fn = std(f)*random.randn(len(f))
on = blsw.blswrap(t,fn,nf=nf,fmax=1/50.)
# Generate another null lightcurve
fn2 = std(f)*random.randn(len(f))
on2 = blsw.blswrap(t,fn2,nf=nf,fmax=1/50.)
o['p'] -= median_filter(o['p'],size=200)
on['p'] -= median_filter(on['p'],size=200)
on2['p'] -= median_filter(on2['p'],size=200)
print "%.2f %.2e %.2e %.2f" % \
(s,(ks_2samp(o['p'],on['p']))[1],(ks_2samp(o['p'],on2['p']))[1],
o['parr'][argmax(o['p'])])
fig = plt.gcf()
fig.clf()
ax = fig.add_subplot(111)
ax.plot(o['parr'],o['p'],label='Signal')
ax.plot(o['parr'],on['p'],label='Noise')
ax.plot(o['parr'],on2['p'],label='Noise2')
ax.legend()
plt.show()
def perfind2():
P = 200.
tbase = 1000.
nf = 5000
s2n = logspace( log10(3),log10(15),5 )
print "S/N sig-noise noise-noise"
for s in s2n:
# Generate a lightcurve.
f,t = keptoy.lightcurve(P=P,tbase=tbase,s2n=s)
o = blsw.blswrap(t,f,nf=nf,fmax=1/50.)
# Subtract off the trend
o['p'] -= median_filter(o['p'],size=200)
# Find the highest peak.
mxid = argmax(o['p'])
mxpk = o['p'][mxid]
# Find all the peaks
mxt,mnt = peakdet(o['p'],delta=1e-3*mxpk,x=o['parr'])
mxt = array(mxt)
# Restrict attention to the highest 100 but leaving off top 10
t1id = where( (mxt[::,1] > sort( mxt[::,1] )[-100]) &
(mxt[::,1] < sort( mxt[::,1] )[-10]) )
fig = plt.gcf()
fig.clf()
ax = fig.add_subplot(111)
ax.plot(o['parr'],o['p'],label='Signal')
ax.scatter( mxt[t1id,0],mxt[t1id,1],label='Tier 1' )
# tpyical values of the highest 100 peaks.
hair = median(mxt[t1id,1])
left = min(o['parr'])
right = max(o['parr'])
ax.hlines(hair,left,right)
if mxpk > 3*hair:
print "Peroid is %.2f" % (o['parr'][mxid])
else:
print "No signal"
ax.legend()
plt.show()