/
zylc.py
617 lines (525 loc) · 22.3 KB
/
zylc.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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# Last-modified: 06 Dec 2013 01:58:44
__all__ = ['LightCurve', 'get_data']
from lcio import readlc, readlc_3c, writelc
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from graphic import figure_handler
import numpy as np
from numpy.random import normal, multivariate_normal
from copy import copy, deepcopy
""" Load light curve files into a LightCurve object.
"""
class LightCurve(object):
def __init__(self, zylclist, names=None, set_subtractmean=True, qlist=None):
""" LightCurve object for encapsulating light curve data.
Parameters
----------
zylclist: list of lists/ndarrays
List of light curves.
names: list of strings, optional
Names of each individual light curve (default: None).
set_subtractmean: bool, optional
True if light curve means are subtracted (default: True).
qlist: list of q values, optional
Best-fit q values from javelin fitting, for accounting for the difference between sample means and
the truth means of light curves (default: None).
"""
if not isinstance(zylclist, list):
raise RuntimeError("zylclist has to be a list of lists or arrays")
else :
self.zylclist = []
for i in xrange(len(zylclist)) :
if isinstance(zylclist[i], np.ndarray) :
if zylclist[i].shape[-1] == 3 :
# if each element is an ndarray, convert to a list of 1d arrays
self.zylclist.append([zylclist[i][:,0], zylclist[i][:,1], zylclist[i][:,2]])
else :
raise RuntimeError("each single light curve array should have shape (#, 3)")
elif isinstance(zylclist[i], list) :
if len(zylclist[i]) == 3 :
self.zylclist.append([zylclist[i][0], zylclist[i][1], zylclist[i][2]])
else :
raise RuntimeError("each single light curve list should have 3 ndarrays")
# number of light curves
self.nlc = len(self.zylclist)
if names is None :
# simply use the sequences as their names (start from 0)
self.names = [str(i) for i in xrange(self.nlc)]
else :
if len(names) != self.nlc :
raise RuntimeError("names should match the dimension of zylclist")
else :
self.names = names
# issingle makes a difference in what you can do with the light curves
if(self.nlc == 1):
self.issingle = True
else:
self.issingle = False
# jlist/mlist/elist/ilist: list of j, m, e, i of each individual light curve
self.jlist, self.mlist, self.elist, self.ilist = self.sorteddatalist(self.zylclist)
# continuum properties, useful in determining continuum variability
self.cont_mean = np.mean(self.mlist[0])
self.cont_mean_err = np.mean(self.elist[0])
self.cont_std = np.std(self.mlist[0])
self.cont_cad_arr = self.jlist[0][1 :] - self.jlist[0][:-1]
self.cont_cad = np.median(self.cont_cad_arr)
self.cont_cad_min = np.min(self.cont_cad_arr)
self.cont_cad_max = np.max(self.cont_cad_arr)
if self.cont_mean_err != 0.0 :
# a rough estimate of the continuum variability signal-to-noise
# ratio
self.cont_SN = self.cont_std/self.cont_mean_err
else :
self.cont_SN = np.inf
# subtract the mean to get *blist*
# usually good for the code health, smaller means means less
# possibility of large round-off errors in the matrix computations.
# note tha it also modifies self.mlist.
if set_subtractmean:
self.blist = self.meansubtraction()
else:
self.blist = [0.0]*self.nlc
# number statistics: nptlist, npt
self.nptlist = np.array([a.size for a in self.jlist])
self.npt = sum(self.nptlist)
# combine all information into one vector, those are the primariy
# vectors we are gonna use in spear covariance.
self.jarr, self.marr, self.earr, self.iarr = self.combineddataarr()
# variance array
self.varr = self.earr*self.earr
# construct the linear response matrix
self.larr = np.zeros((self.npt, self.nlc))
for i in xrange(self.npt):
lcid = self.iarr[i] - 1
self.larr[i, lcid] = 1.0
self.larrTr = self.larr.T
# baseline of all the light curves
self.jstart = self.jarr[0]
self.jend = self.jarr[-1]
self.rj = self.jend - self.jstart
# q values for the *true* means of light curves
self.qlist = self.nlc*[0.0]
if qlist is None :
pass
else :
if len(qlist) != self.nlc :
raise RuntimeError("qlist should match the dimension of zylclist")
else :
self.update_qlist(qlist)
def __add__(self, other) :
_zylclist = self.zylclist + other.zylclist
_names = self.names + other.names
return(LightCurve(_zylclist, names=_names))
def shed_continuum(self) :
_zylclist = [self.zylclist[0],]
_names = [self.names[0],]
return(LightCurve(_zylclist, names=_names))
def split(self) :
""" split into individual LightCurves objects whenever the parent has multiple lightcurves.
"""
eggs = []
for i in xrange(self.nlc) :
_zylclist = [self.zylclist[i],]
_names = [self.names[i],]
eggs.append(LightCurve(_zylclist, names=_names))
return(eggs)
def spawn(self, errcov=0.0, names=None) :
""" generate one LightCurve for which the lightcurve values are the sum of the original ones and gaussian variates from gaussian errors.
"""
# _zylclist = list(self.zylclist) # copy the original list
_zylclist = deepcopy(self.zylclist) # copy the original list
for i in xrange(self.nlc) :
e = np.atleast_1d(_zylclist[i][2])
nwant = e.size
ediag = np.diag(e*e)
if errcov == 0.0 :
ecovmat = ediag
else :
temp1 = np.repeat(e, nwant).reshape(nwant,nwant)
temp2 = (temp1*temp1.T - ediag)*errcov
ecovmat = ediag + temp2
et = multivariate_normal(np.zeros_like(e), ecovmat)
_zylclist[i][1] = _zylclist[i][1] + et
if names is None :
names = ["-".join([r, "mock"]) for r in self.names]
return(LightCurve(_zylclist, names=names))
def shift_time(self, timeoffset) :
""" shift the time axies by `timeoffset`
"""
# fix jarr
self.jarr = self.jarr + timeoffset
self.jstart = self.jarr[0]
self.jend = self.jarr[-1]
# fix jlist and zylclist
for i in xrange(self.nlc) :
# fix jlist
self.jlist[i] = self.jlist[i] + timeoffset
# fix the original zylclist
self.zylclist[i][0] = np.atleast_1d(zylclist[i][0]) + timeoffset
def plot(self, set_pred=False, obs=None, marker="o", ms=4, ls='None', lw=1, figout=None, figext=None) :
""" Plot light curves.
Parameters
----------
set_pred: bool, optional
True if the current light curve data are simulated or predicted from
javelin, rather than real data (default: False).
obs: bool, optional
The observed light curve data to be overplotted on the current light
curves, usually used when set_pred is True (default: None).
marker: str, optional
Marker symbol (default: 'o').
ms : float, optional
Marker size (default: 4).
ls : str, optional
Line style (default: 'None').
lw: float, optional
Line width (default: 1).
figout: str, optional
Output figure name (default: None).
figext: str, optional
Output figure extension, ``png``, ``eps``, or ``pdf``. Set to None
for drawing without saving to files (default: None)
"""
fig = plt.figure(figsize=(8, 2*self.nlc))
height = 0.85/self.nlc
for i in xrange(self.nlc) :
ax = fig.add_axes([0.10, 0.1+i*height, 0.85, height])
mfc = cm.jet(i/(self.nlc-1.) if self.nlc > 1 else 0)
if set_pred :
ax.plot(self.jlist[i], self.mlist[i]+self.blist[i],
color=mfc, ls="-", lw=2,
label=self.names[i])
ax.fill_between(self.jlist[i],
y1=self.mlist[i]+self.blist[i]+self.elist[i],
y2=self.mlist[i]+self.blist[i]-self.elist[i],
color=mfc, alpha=0.5,
label=self.names[i])
if obs is not None :
ax.errorbar(obs.jlist[i], obs.mlist[i]+obs.blist[i],
yerr=obs.elist[i],
ecolor='k', marker=marker, ms=ms, mfc=mfc, mec='k', ls=ls, lw=lw,
label=" ".join([self.names[i], "observed"]))
else :
if np.sum(self.elist[i]) == 0.0 :
# no error, pure signal.
ax.plot(self.jlist[i], self.mlist[i]+self.blist[i],
marker=marker, ms=ms, mfc=mfc, mec='k', ls=ls, lw=lw,
label=self.names[i], color=mfc)
else :
ax.errorbar(self.jlist[i], self.mlist[i]+self.blist[i],
yerr=self.elist[i],
ecolor='k', marker=marker, ms=ms, mfc=mfc, mec='k', ls=ls, lw=lw,
label=self.names[i])
ax.set_xlim(self.jstart, self.jend)
ax.set_ylim(np.min(self.mlist[i])+self.blist[i]-np.min(self.elist[i]),
np.max(self.mlist[i])+self.blist[i]+np.max(self.elist[i]))
if i == 0 :
ax.set_xlabel(r"$t$")
else :
ax.set_xticklabels([])
ax.set_ylabel(r"$f$")
leg = ax.legend(loc='best', fancybox=True)
leg.get_frame().set_alpha(0.5)
return(figure_handler(fig=fig, figout=figout, figext=figext))
def plotdt(self, set_logdt=False, figout=None, figext=None, **histparams) :
""" Plot the time interval distribution.
set_logdt: bool, optional
True if Delta t is in log (default: False)
figout: str, optional
Output figure name (default: None).
figext: str, optional
Output figure extension, ``png``, ``eps``, or ``pdf``. Set to None
for drawing without saving to files (default: None)
histparams: kargs, optional
Parameters for ax.hist.
"""
_np = self.nptlist[0]
_ndt = _np*(_np-1)/2
dtarr = np.zeros(_ndt)
_k = 0
for i in xrange(_np-1) :
for j in xrange(i+1, _np) :
dtarr[_k] = self.jlist[0][j] - self.jlist[0][i]
_k += 1
if set_logdt :
dtarr = np.log10(dtarr)
fig = plt.figure(figsize=(8, 8))
ax = fig.add_axes([0.1, 0.1, 0.85, 0.85])
ax.hist(dtarr, **histparams)
if set_logdt :
ax.set_xlabel(r"$\log\;\Delta t$")
else :
ax.set_xlabel(r"$\Delta t$")
return(figure_handler(fig=fig, figout=figout, figext=figext))
def save(self, fname, set_overwrite=True):
""" Save current LightCurve into zylc file format.
Parameters
----------
fname : str
Output file name.
set_overwrite: bool, optional
True to overwrite existing files (default: True).
"""
try :
f=open(fname, "r")
if not set_overwrite :
raise RuntimeError("%s exists, exit"%fname)
else :
print("save light curves to %s"%fname)
writelc(self.zylclist, fname)
except IOError :
print("save light curves to %s"%fname)
writelc(self.zylclist, fname)
def save_continuum(self, fname, set_overwrite=True):
""" Save the continuum part of LightCurve into zylc file format.
Parameters
----------
fname : str
Output file name.
set_overwrite: bool, optional
True to overwrite existing files (default: True).
"""
try :
f=open(fname, "r")
if not set_overwrite :
raise RuntimeError("%s exists, exit"%fname)
else :
print("save continuum light curves to %s"%fname)
writelc([self.zylclist[0]], fname)
except IOError :
print("save continuum light curves to %s"%fname)
writelc([self.zylclist[0]], fname)
def save_lcarr(self, fname, set_overwrite=True, set_addmean=True, set_saveid=False) :
""" Save the data array into a 3-column file.
Parameters
----------
fname : str
Output file name.
set_overwrite: bool, optional
True to overwrite existing files (default: True).
set_saveid: bool, optional
True to save id array (default: False).
set_addmean: bool, optional
True to save original light curve values without no mean subtraction (default: True).
"""
try :
f=open(fname, "r")
if not set_overwrite :
raise RuntimeError("%s exists, exit"%fname)
except IOError :
print("%s does not exist")
print("save light curve data array to %s"%fname)
# TODO
_marr = np.empty(self.npt)
if set_addmean :
for i in xrange(self.nlc) :
sel = (self.iarr == i+1)
_marr[sel] = self.marr[sel] + self.blist[i]
else :
_marr = self.marr
if set_saveid :
np.savetxt(fname, np.vstack((self.jarr, _marr, self.earr, self.iarr)).T)
else :
np.savetxt(fname, np.vstack((self.jarr, _marr, self.earr)).T)
def update_qlist(self, qlist_new):
""" Update blist and mlist of the LightCurve object according to the
newly acquired qlist values.
Parameters
----------
qlist_new: list
Best-fit light curve mean modulation factors.
"""
for i in xrange(self.nlc):
# recover original data when qlist=0
self.blist[i] -= self.qlist[i]
self.mlist[i] += self.qlist[i]
# add q to blist
# subtract q from mlist
self.blist[i] += qlist_new[i]
self.mlist[i] -= qlist_new[i]
# redo combineddataarr
self.jarr, self.marr, self.earr, self.iarr = self.combineddataarr()
# variance array
self.varr = self.earr*self.earr
# update qlist
self.qlist = qlist_new
def meansubtraction(self):
""" Subtract the mean.
Returns
-------
blist: list
list of light curve means.
"""
blist = []
for i in xrange(self.nlc):
bar = np.mean(self.mlist[i])
blist.append(bar)
self.mlist[i] = self.mlist[i] - bar
return(blist)
def sorteddatalist(self, zylclist):
""" Sort the input lists by time epochs.
Parameters
----------
zylclist: list of lists/ndarrays
List of light curves.
Returns
-------
jlist:
List of time ndarrays.
mlist:
List of flux/mag ndarrays.
elist:
List of error ndarrays.
ilist:
List of index ndarrays, starting at 1 rather than 0.
"""
jlist = []
mlist = []
elist = []
ilist = []
for ilc, lclist in enumerate(zylclist):
if (len(lclist) == 3):
jsubarr, msubarr, esubarr = [np.array(l) for l in lclist]
nptlc = len(jsubarr)
# sort the date
p = jsubarr.argsort()
jlist.append(jsubarr[p])
mlist.append(msubarr[p])
elist.append(esubarr[p])
ilist.append(np.zeros(nptlc, dtype="int")+ilc+1)
else:
raise RuntimeError("each sub-zylclist has to be a list of lists or arrays")
return(jlist, mlist, elist, ilist)
def combineddataarr(self):
""" Combine lists into ndarrays.
Returns
-------
jarr:
Sorted time ndarray.
marr:
Sorted tFlux/mag ndarray.
earr:
Sorted tError ndarray.
iarr:
Sorted tindex ndarray.
"""
jarr = np.empty(self.npt)
marr = np.empty(self.npt)
earr = np.empty(self.npt)
iarr = np.empty(self.npt, dtype="int")
larr = np.zeros((self.nlc, self.npt))
start = 0
for i, nptlc in enumerate(self.nptlist):
jarr[start:start+nptlc] = self.jlist[i]
marr[start:start+nptlc] = self.mlist[i]
earr[start:start+nptlc] = self.elist[i]
# comply with the fortran version, where i starts from 1 rather than 0.
#iarr[start:start+nptlc] = i + 1
iarr[start:start+nptlc] = self.ilist[i]
start = start+nptlc
p = jarr.argsort()
return(jarr[p], marr[p], earr[p], iarr[p])
def get_data(lcfile, names=None, set_subtractmean=True, timeoffset=0.0, dat_type="flux", cont_frac=1.0):
""" Read light curve file(s) into a LightCurve object.
Parameters
----------
lcfile: string or list of strings
Input files, could be a single or multiple 3-column light curve files, or
a single zylc format light curve file.
names: list of strings
Names of each files in *lcfile* (default: None).
set_subtractmean: bool
Subtract mean in LightCurve if True (default: True).
timeoffset: float
The offset added to the time array in `lcfile`, so that t_final = t_orig + timeoffset
dat_type: str
The type of measurement data.
line_fraction: float
line fraction in the line band
Returns
-------
zydata: LightCurve object
Combined data in a LightCurve object
"""
if isinstance(lcfile, basestring):
nlc = 1
# lcfile should be a single file
try :
# either a 3-column single lc file
lclist = readlc_3c(lcfile)
except :
# or a zylc file
lclist = readlc(lcfile)
else :
# lcfile should be a list or tuple of 3-column files
try :
nlc = len(lcfile)
except :
raise RuntimeError("input is neither a list/tuple nor a string?")
lclist = []
for lcf in lcfile :
lc = readlc_3c(lcf)
# convert the mag data to flux, but the error needs @ZHW
if dat_type == "flux":
if lcf == lcfile[0]:
# set the maximum of continuum the reference value @ZHW
F0 = max(lc[0][1])
cont_lc = lc[0]
cont_mean = np.mean(cont_lc)
lc[0][1] = [lc[0][1][i] / F0 for i in range(len(lc[0][1]))]
lc[0][2] = [lc[0][2][i] / F0 for i in range(len(lc[0][2]))]
if lcf == lcfile[1]:
if cont_frac == 1:
cont_frac = np.mean(lc[0][1]) / cont_mean
lc[0][1] = [lc[0][1][i] - cont_frac * cont_lc[1][np.argmin(cont_lc[0] - lc[0][0][i])] for i in range(len(lc[0][1]))]
lc[0][2] = [(lc[0][2][i]**2 + (cont_frac * cont_lc[2][np.argmin(cont_lc[0] - lc[0][0][i])])**2)**0.5 for i in range(len(lc[0][2]))]
elif dat_type == "mag":
if lcf == lcfile[0]:
# set the maximum of continuum the reference value @ZHW
F0 = 10**(-0.4 * min(lc[0][1]))
# normalize the whole lightcurve, Only when different bands have the same zero-point! @ZHW
lc[0][1] = [10**(-0.4 * lc[0][1][i]) / F0 for i in range(len(lc[0][1]))]
# transform the mag error to (relative) flux error @ZHW
lc[0][2] = [10**(-0.4 * (lc[0][1][i] - min(lc[0][1]))) * np.log(10) * 0.4 * (lc[0][2][i]**2 + (lc[0][2][np.argmin(lc[0][1])])**2)**0.5 \
for i in range(len(lc[0][2]))]
if lcf == lcfile[0]:
cont_lc = lc[0]
cont_mean = np.mean(cont_lc)
if lcf == lcfile[1]:
if cont_frac == 1:
cont_frac = np.mean(lc[0][1]) / cont_mean
lc[0][1] = [lc[0][1][i] - cont_frac * cont_lc[1][np.argmin(np.array(cont_lc[0]) - lc[0][0][i])] for i in range(len(lc[0][1]))]
lc[0][2] = [(lc[0][2][i]**2 + (cont_frac * cont_lc[2][np.argmin(np.array(cont_lc[0]) - lc[0][0][i])])**2)**0.5 for i in range(len(lc[0][2]))]
'''
#subtract continuum from line band
if lcf == lcfile[1]:
lc[0][1] = [lc[0][1][i] * line_fraction for i in range(len(lc[0][1]))]
lc[0][2] = [lc[0][2][i] * line_fraction for i in range(len(lc[0][2]))]
lc[0][1] = [3631 * 10**(- 0.4 * lc[0][1][i]) for i in range(len(lc[0][1]))]
lc[0][2] = [0.4 * 3631 * 10**(- 0.4 * lc[0][1][i]) * np.log(10) * lc[0][2][i] for i in range(len(lc[0][2]))]
'''
lclist.append(lc[0])
for ilc in xrange(len(lclist)) :
lclist[ilc][0] = np.atleast_1d(lclist[ilc][0]) + timeoffset
zydata = LightCurve(lclist, names=names, set_subtractmean=set_subtractmean)
return(zydata)
if __name__ == "__main__":
zylclist= [
[
[2.0, 1.0, 5.0, 10.0],
[5.0, 5.5, 4.3, 5.6],
[0.1, 0.1, 0.1, 0.4]
],
[
[1.5],
[5.0],
[0.1]
],
[
[8.0, 9.0],
[3.0, 1.5],
[0.2, 0.1]
]
]
zylc = LightCurve(zylclist=zylclist)
print(zylc.cont_cad)