-
Notifications
You must be signed in to change notification settings - Fork 0
/
tval.py
386 lines (294 loc) · 8.38 KB
/
tval.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
"""
Transit Validation
After the brute force period search yeilds candidate periods,
functions in this module will check for transit-like signature.
"""
import numpy as np
from numpy import ma
from scipy import optimize
import copy
import keptoy
import tfind
from numpy.polynomial import Legendre
dP = 0.5
depoch = 0.5
def val(t,fm,tres):
"""
Validate promising transits.
Parameters
----------
t : time
fm : flux
tres : a record array with the parameters of the putative transit.
- P
- epoch
- tdur
- df
Returns
-------
rval : a record array with following fields:
- P <- From best fit
- epoch
- tdur
- df
- s2n
"""
r2d = lambda r : dict(P=r['P'],epoch=r['epoch'],tdur=r['tdur'],df=r['df'])
dL = map(r2d,tres)
fcand = lambda d : fitcand(t,fm,d)
resL = map(fcand,dL)
# Cut out crazy fits.
resL = [r for r in resL if r['stbl'] ]
### Alias Lodgic ###
# Check the following periods for aliases.
resL = [r for r in resL if r['s2n'] > 5]
falias = lambda r : aliasW(t,fm,r)
resL = map(falias,resL)
dtype = [('P',float),('epoch',float),('tdur',float),('df',float),
('s2n',float)]
d2l = lambda d : tuple([ d[ k[0] ] for k in dtype ])
d2r = lambda d : np.array( d2l(d) ,dtype=dtype)
rval = map(d2r,resL)
rval = np.hstack(rval)
return rval
def getT(time,P,epoch,wd):
"""
Get Transits
time : Time series
P : Period
epoch: epoch
wd : How much data to return for each slice.
Returns
-------
Time series phase folded with everything but the transits masked out.
"""
tfold = time - epoch # Slide transits to 0, P, 2P
tfold = np.mod(tfold,P)
tfold = ma.masked_inside(tfold,wd/2,P-wd/2)
tfold = ma.masked_invalid(tfold)
return tfold
def trsh(P,tbase):
ftdurmi = 0.5
tdur = keptoy.a2tdur( keptoy.P2a(P) )
tdurmi = ftdurmi*tdur
dP = tdurmi * P / tbase
depoch = tdurmi
return dict(dP=dP,depoch=depoch)
def objMT(p,time,fdt):
"""
Multitransit Objective Function
With a prior on P and epoch
"""
fmod = keptoy.P05(p,time)
resid = (fmod - fdt)/1e-4
obj = (resid**2).sum()
return obj
def obj1T(p,t,f):
"""
Single Transit Objective Function
"""
model = keptoy.P051T(p,t)
resid = (model - f)/1e-4
obj = (resid**2).sum()
return obj
def id1T(t,fm,p,wd=2.,usemask=True):
"""
Grab the indecies and midpoint of a putative transit.
"""
tdur = p['tdur']
tdurcad = round(tdur/keptoy.lc)
wdcad = round(wd/keptoy.lc)
dM = tfind.mtd(t,fm.filled(),tdurcad)
dM.mask = fm.mask | ~tfind.isfilled(t,fm,tdurcad)
### Determine the indecies of the points to fit. ###
# Exclude regions where the convolution returned a nan.
ms = midTransId(t,p)
if usemask:
ms = [m for m in ms if ~dM.mask[m] ]
sLDT = [ getSlice(m,wdcad) for m in ms ]
x = np.arange(dM.size)
idL = [ x[s][~fm[s].mask] for s in sLDT ]
idL = [ id for id in idL if id.size > 10 ]
return ms,idL
def LDT(epoch,tdur,t,f,pad=0.2,deg=1):
"""
Local detrending
A simple function that subtracts a polynomial trend from the
lightcurve excluding a region around the transit.
pad : Extra number of days to notch out of the of the transit region.
"""
bcont = abs(t - epoch) > tdur/2 + pad
fcont = f[bcont]
tcont = t[bcont]
legtrend = Legendre.fit(tcont,fcont,deg,domain=[t.min(),t.max()])
trend = legtrend(t)
return trend
def LDTwrap(t,fm,p):
ms,idL = id1T(t,fm,p,wd=2.)
tdur = p['tdur']
dtype=[('trend',float),('fdt',float),('tdt',float) ]
resL = []
for m,id in zip(ms,idL):
trend = LDT( t[m],tdur, t[id] , fm[id].data)
fdt = fm[id]-trend
tdt = t[id]
res = np.array(zip(trend,fdt,tdt),dtype=dtype)
resL.append(res)
return resL
def fitcand(t,fm,p,full=False):
"""
Perform a non-linear fit to a putative transit.
Parameters
----------
t : time
fm : flux
p : trial parameter (dictionary)
full : Retrun tdt and fdt
Returns
-------
res : result dictionary.
"""
dtL = LDTwrap(t,fm,p)
dt = np.hstack(dtL)
fdt = dt['fdt']
tdt = dt['tdt']
p0 = np.array([p['P'],p['epoch'],p['df'],p['tdur']])
p1 = optimize.fmin_powell(objMT,p0,args=(tdt,fdt),disp=False)
dp = (p0[:2]-p1[:2])
if (abs(dp) > np.array([dP,depoch])).any():
stbl = False
elif (p1[0] < 0) | (p1[3] < 0):
stbl = False
else:
stbl = True
tfold = getT(tdt,p['P'],p['epoch'],p['tdur'])
fdt = ma.masked_array(fdt,mask=tfold.mask)
tdt = ma.masked_array(tdt,mask=tfold.mask)
s2n = s2n_fit(fdt,tdt,p1)
res = dict(P=p1[0],epoch=p1[1],df=p1[2],tdur=p1[3],s2n=s2n,stbl=stbl)
if full:
res['fdt'] = fdt
res['tdt'] = tdt
return res
def s2n_mean(fdt):
return -ma.mean(fdt)/ma.std(fdt)*np.sqrt(fdt.count())
def s2n_med(fdt):
sig = ma.median(fdt)
noise = ma.median(abs(fdt-sig))
return -sig/noise*np.sqrt(fdt.count())
def s2n_fit(fdt,tdt,p):
"""
Evaluate S/N taking the best fit depth as signal and the scatter
about the residuals as the noise.
"""
model = keptoy.P05(p,tdt)
sig = p[2]
resid = fdt-model
noise = ma.median(abs(resid))
s2n = sig/noise*np.sqrt(fdt.count() )
return s2n
thresh = 0.001
def window(fl,PcadG):
"""
Compute the window function.
The fraction of epochs that pass our criteria for transit.
"""
winL = []
for Pcad in PcadG:
flW = tfind.XWrap(fl,Pcad,fill_value=False)
win = (flW.sum(axis=0) >= 3).astype(float)
npass = np.where(win)[0].size
win = float(npass) / win.size
winL.append(win)
return winL
def midTransId(t,p):
"""
Mid Transit Index
Return the indecies of mid transit for input parameters.
Parameters
----------
t - timeseries
p - dictionary with 'P','epoch','tdur'
"""
P = p['P']
epoch = p['epoch']
epoch = np.mod(epoch,P)
tfold = np.mod(t,P)
ms = np.where(np.abs(tfold-epoch) < 0.5 * keptoy.lc)[0]
ms = list(ms)
return ms
def aliasW(t,fm,p0):
"""
Alias Wrap
"""
p = copy.deepcopy(p0)
X2,X2A = alias(t,fm,p)
if X2A < X2:
p['P'] = p['P']*0.5
return p
else:
return p
def alias(t,fm,p):
"""
Evaluate the Bayes Ratio between signal with P and 0.5*P
Parameters
----------
t : Time series
fm : Flux series
p : Parameter dictionary.
"""
pA = copy.deepcopy(p)
pA['P'] = 0.5 * pA['P']
resL = LDTwrap(t,fm,pA)
res = np.hstack(resL)
# Masked array corresponding to P = 2 P
tfold = getT(res['tdt'],pA['P'],pA['epoch'],pA['tdur'])
tT = ma.masked_array(res['tdt'],copy=True,mask=tfold.mask)
fT = ma.masked_array(res['fdt'],copy=True,mask=tfold.mask)
X2 = lambda par : ma.sum( (fT - keptoy.P05(pd2a(par),tT))**2 )
return X2(p),X2(pA)
def pd2a(d):
return np.array([d['P'],d['epoch'],d['df'],d['tdur']])
def pa2d(a):
return dict(P=a[0],epoch=a[1],df=a[2],tdur=a[3])
def getSlice(m,wdcad):
"""
Get slice
Parameters
----------
m : middle index (center of the slice).
wdcad : width of slice list (units of cadence).
"""
return slice( m-wdcad/2 , m+wdcad/2 )
def tdict(d,prefix=''):
"""
"""
outcol = ['P','epoch','df','tdur']
incol = [prefix+oc for oc in outcol]
outd = {}
for o,c in zip(outcol,incol):
try:
outd[o] = d[c]
except KeyError:
pass
return outd
def nT(t,mask,p):
"""
Simple helper function. Given the transit ephemeris, how many
transit do I expect in my data?
"""
trng = np.floor(
np.array([p['epoch'] - t[0],
t[-1] - p['epoch']]
) / p['P']).astype(int)
nt = np.arange(trng[0],trng[1]+1)
tbool = np.zeros(nt.size).astype(bool)
for i in range(nt.size):
it = nt[i]
tt = p['epoch'] + it*p['P']
# Find closest time.
dt = abs(t - tt)
imin = np.argmin(dt)
if (dt[imin] < 0.3) & ~mask[imin]:
tbool[i] = True
return tbool