/
lightcurve.py
1321 lines (1163 loc) · 51.9 KB
/
lightcurve.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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Defines LightCurve, KeplerLightCurve, TessLightCurve, etc."""
from __future__ import division, print_function
import copy
from tqdm import tqdm
import os
import datetime
import logging
import pandas as pd
import warnings
import oktopus
import numpy as np
from scipy import signal
from scipy.optimize import minimize
from matplotlib import pyplot as plt
from astropy.stats import sigma_clip
from astropy.table import Table
from astropy.io import fits
from astropy.time import Time
from . import PACKAGEDIR, MPLSTYLE
from .utils import running_mean, bkjd_to_astropy_time, btjd_to_astropy_time
__all__ = ['LightCurve', 'KeplerLightCurve', 'TessLightCurve',
'FoldedLightCurve']
log = logging.getLogger(__name__)
class LightCurve(object):
"""
Implements a simple class for a generic light curve.
Attributes
----------
time : array-like
Time measurements
flux : array-like
Data flux for every time point
flux_err : array-like
Uncertainty on each flux data point
time_format : str
String specifying how an instant of time is represented,
e.g. 'bkjd' or 'jd'.
time_scale : str
String which specifies how the time is measured,
e.g. tdb', 'tt', 'ut1', or 'utc'.
targetid : str
Identifier of the target.
label : str
Human-friendly object label, e.g. "KIC 123456789"
meta : dict
Free-form metadata associated with the LightCurve.
"""
def __init__(self, time=None, flux=None, flux_err=None, time_format=None,
time_scale=None, targetid=None, label=None, meta={}):
if time is None and flux is None:
raise ValueError('either time or flux must be given')
if time is None:
self.time = np.arange(len(flux))
else:
self.time = np.asarray(time)
self.flux = self._validate_array(flux, name='flux')
self.flux_err = self._validate_array(flux_err, name='flux_err')
self.time_format = time_format
self.time_scale = time_scale
self.targetid = targetid
self.label = label
self.meta = meta
def _validate_array(self, arr, name='array'):
"""Ensure the input arrays have the same length as `self.time`."""
if arr is not None:
arr = np.asarray(arr)
else:
arr = np.nan * np.ones_like(self.time)
if not (len(self.time) == len(arr)):
raise ValueError("Input arrays have different lengths."
" len(time)={}, len({})={}"
.format(len(self.time), name, len(arr)))
return arr
def __getitem__(self, key):
copy_self = copy.copy(self)
copy_self.time = self.time[key]
copy_self.flux = self.flux[key]
copy_self.flux_err = self.flux_err[key]
return copy_self
def __add__(self, other):
copy_self = copy.copy(self)
copy_self.flux = copy_self.flux + other
return copy_self
def __radd__(self, other):
return self.__add__(other)
def __sub__(self, other):
return self.__add__(-other)
def __rsub__(self, other):
copy_self = copy.copy(self)
copy_self.flux = other - copy_self.flux
return copy_self
def __mul__(self, other):
copy_self = copy.copy(self)
copy_self.flux = other * copy_self.flux
copy_self.flux_err = abs(other) * copy_self.flux_err
return copy_self
def __rmul__(self, other):
return self.__mul__(other)
def __truediv__(self, other):
return self.__mul__(1./other)
def __rtruediv__(self, other):
copy_self = copy.copy(self)
copy_self.flux = other / copy_self.flux
return copy_self
def __div__(self, other):
return self.__truediv__(other)
def __rdiv__(self, other):
return self.__rtruediv__(other)
@property
def astropy_time(self):
"""Returns an `astropy.time.Time` object.
The Time object will be created using the values in `self.time`
and the `self.time_format` and `self.time_scale` attributes.
For Kepler data products, the times are Barycentric.
Raises
------
ValueError
If `self.time_format` is not set or not one of the formats
allowed by AstroPy.
"""
from astropy.time import Time
if self.time_format is None:
raise ValueError("To retrieve a `Time` object the `time_format` "
"attribute must be set on the LightCurve object, "
"e.g. `lightcurve.time_format = 'jd'`.")
# AstroPy does not support BKJD, so we call a function to convert to JD.
# In the future, we should think about making an AstroPy-compatible
# `TimeFormat` class for BKJD.
if self.time_format == 'bkjd':
return bkjd_to_astropy_time(self.time)
elif self.time_format == 'btjd': # TESS
return btjd_to_astropy_time(self.time)
return Time(self.time, format=self.time_format, scale=self.time_scale)
def properties(self):
'''Print out a description of each of the non-callable attributes of a
LightCurve object.
Prints in order of type (ints, strings, lists, arrays and others)
Prints in alphabetical order.'''
attrs = {}
for attr in dir(self):
if not attr.startswith('_'):
res = getattr(self, attr)
if callable(res):
continue
if attr == 'hdu':
attrs[attr] = {'res': res, 'type': 'list'}
for idx, r in enumerate(res):
if idx == 0:
attrs[attr]['print'] = '{}'.format(r.header['EXTNAME'])
else:
attrs[attr]['print'] = '{}, {}'.format(
attrs[attr]['print'], '{}'.format(r.header['EXTNAME']))
continue
else:
attrs[attr] = {'res': res}
if isinstance(res, int):
attrs[attr]['print'] = '{}'.format(res)
attrs[attr]['type'] = 'int'
elif isinstance(res, np.ndarray):
attrs[attr]['print'] = 'array {}'.format(res.shape)
attrs[attr]['type'] = 'array'
elif isinstance(res, list):
attrs[attr]['print'] = 'list length {}'.format(len(res))
attrs[attr]['type'] = 'list'
elif isinstance(res, str):
if res == '':
attrs[attr]['print'] = '{}'.format('None')
else:
attrs[attr]['print'] = '{}'.format(res)
attrs[attr]['type'] = 'str'
elif attr == 'wcs':
attrs[attr]['print'] = 'astropy.wcs.wcs.WCS'.format(attr)
attrs[attr]['type'] = 'other'
else:
attrs[attr]['print'] = '{}'.format(type(res))
attrs[attr]['type'] = 'other'
output = Table(names=['Attribute', 'Description'], dtype=[object, object])
idx = 0
types = ['int', 'str', 'list', 'array', 'other']
for typ in types:
for attr, dic in attrs.items():
if dic['type'] == typ:
output.add_row([attr, dic['print']])
idx += 1
output.pprint(max_lines=-1, max_width=-1)
def append(self, others):
"""
Append LightCurve objects.
Parameters
----------
others : LightCurve object or list of LightCurve objects
Light curves to be appended to the current one.
Returns
-------
new_lc : LightCurve object
Concatenated light curve.
"""
if not hasattr(others, '__iter__'):
others = [others]
new_lc = copy.copy(self)
for i in range(len(others)):
new_lc.time = np.append(new_lc.time, others[i].time)
new_lc.flux = np.append(new_lc.flux, others[i].flux)
new_lc.flux_err = np.append(new_lc.flux_err, others[i].flux_err)
if hasattr(new_lc, 'cadenceno'):
new_lc.cadenceno = np.append(new_lc.cadenceno, others[i].cadenceno) # KJM
if hasattr(new_lc, 'quality'):
new_lc.quality = np.append(new_lc.quality, others[i].quality)
if hasattr(new_lc, 'centroid_col'):
new_lc.centroid_col = np.append(new_lc.centroid_col, others[i].centroid_col)
if hasattr(new_lc, 'centroid_row'):
new_lc.centroid_row = np.append(new_lc.centroid_row, others[i].centroid_row)
return new_lc
def flatten(self, window_length=101, polyorder=2, return_trend=False,
break_tolerance=5, **kwargs):
"""
Removes low frequency trend using scipy's Savitzky-Golay filter.
This method wraps `scipy.signal.savgol_filter`.
Parameters
----------
window_length : int
The length of the filter window (i.e. the number of coefficients).
``window_length`` must be a positive odd integer.
polyorder : int
The order of the polynomial used to fit the samples. ``polyorder``
must be less than window_length.
return_trend : bool
If `True`, the method will return a tuple of two elements
(flattened_lc, trend_lc) where trend_lc is the removed trend.
break_tolerance : int
If there are large gaps in time, flatten will split the flux into
several sub-lightcurves and apply `savgol_filter` to each
individually. A gap is defined as a period in time larger than
`break_tolerance` times the median gap. To disable this feature,
set `break_tolerance` to None.
**kwargs : dict
Dictionary of arguments to be passed to `scipy.signal.savgol_filter`.
Returns
-------
flatten_lc : LightCurve object
Flattened lightcurve.
If `return_trend` is `True`, the method will also return:
trend_lc : LightCurve object
Trend in the lightcurve data
"""
if break_tolerance is None:
break_tolerance = np.nan
if polyorder >= window_length:
polyorder = window_length - 1
log.warning("polyorder must be smaller than window_length, "
"using polyorder={}.".format(polyorder))
lc_clean = self.remove_nans()
# Split the lightcurve into segments by finding large gaps in time
dt = lc_clean.time[1:] - lc_clean.time[0:-1]
with warnings.catch_warnings(): # Ignore warnings due to NaNs
warnings.simplefilter("ignore", RuntimeWarning)
cut = np.where(dt > break_tolerance * np.nanmedian(dt))[0] + 1
low = np.append([0], cut)
high = np.append(cut, len(lc_clean.time))
# Then, apply the savgol_filter to each segment separately
trend_signal = np.zeros(len(lc_clean.time))
for l, h in zip(low, high):
# Reduce `window_length` and `polyorder` for short segments;
# this prevents `savgol_filter` from raising an exception
# If the segment is too short, just take the median
if np.any([window_length > (h - l), (h - l) < break_tolerance]):
trend_signal[l:h] = np.nanmedian(lc_clean.flux[l:h])
else:
trend_signal[l:h] = signal.savgol_filter(x=lc_clean.flux[l:h],
window_length=window_length,
polyorder=polyorder,
**kwargs)
trend_signal = np.interp(self.time, lc_clean.time, trend_signal)
flatten_lc = copy.deepcopy(self)
flatten_lc.flux = flatten_lc.flux / trend_signal
flatten_lc.flux_err = flatten_lc.flux_err / trend_signal
if return_trend:
trend_lc = copy.deepcopy(self)
trend_lc.flux = trend_signal
return flatten_lc, trend_lc
return flatten_lc
def fold(self, period, phase=0.):
"""Folds the lightcurve at a specified ``period`` and ``phase``.
This method returns a new ``LightCurve`` object in which the time
values range between -0.5 to +0.5. Data points which occur exactly
at ``phase`` or an integer multiple of `phase + n*period` have time
value 0.0.
Parameters
----------
period : float
The period upon which to fold.
phase : float, optional
Time reference point.
Returns
-------
folded_lightcurve : LightCurve object
A new ``LightCurve`` in which the data are folded and sorted by
phase.
"""
fold_time = (((self.time - phase * period) / period) % 1)
# fold time domain from -.5 to .5
fold_time[fold_time > 0.5] -= 1
sorted_args = np.argsort(fold_time)
return FoldedLightCurve(fold_time[sorted_args],
self.flux[sorted_args],
flux_err=self.flux_err[sorted_args])
def normalize(self):
"""Returns a normalized version of the lightcurve.
The normalized lightcurve is obtained by dividing `flux` and `flux_err`
by the median flux.
Returns
-------
normalized_lightcurve : LightCurve object
A new ``LightCurve`` in which `flux` and `flux_err` are divided
by the median.
"""
lc = copy.copy(self)
lc.flux_err = lc.flux_err / np.nanmedian(lc.flux)
lc.flux = lc.flux / np.nanmedian(lc.flux)
return lc
def remove_nans(self):
"""Removes cadences where the flux is NaN.
Returns
-------
clean_lightcurve : LightCurve object
A new ``LightCurve`` from which NaNs fluxes have been removed.
"""
return self[~np.isnan(self.flux)] # This will return a sliced copy
def fill_gaps(lc, method='nearest'):
"""Fill in gaps in time with linear interpolation.
Parameters
----------
method : string {None, 'backfill'/'bfill', 'pad'/'ffill', 'nearest'}
Method to use for gap filling. 'nearest' by default.
Returns
-------
nlc : LightCurve object
A new ``LightCurve`` in which NaNs values and gaps in time have been
filled.
"""
clc = copy.deepcopy(lc.remove_nans())
nlc = copy.deepcopy(lc)
# Average gap between cadences
dt = np.nanmedian(clc.time[1::] - clc.time[:-1:])
# Iterate over flux and flux_err
for idx, y in enumerate([clc.flux, clc.flux_err]):
# We need to ensure pandas gets the correct byteorder
# Background info: https://github.com/astropy/astropy/issues/1156
if y.dtype.byteorder == '>':
y = y.byteswap().newbyteorder()
ts = pd.Series(y, index=clc.time)
newindex = [clc.time[0]]
for t in clc.time[1::]:
prevtime = newindex[-1]
while (t - prevtime) > 1.2*dt:
newindex.append(prevtime + dt)
prevtime = newindex[-1]
newindex.append(t)
ts = ts.reindex(newindex, method=method)
if idx == 0:
nlc.flux = np.asarray(ts)
elif idx == 1:
nlc.flux_err = np.asarray(ts)
nlc.time = np.asarray(ts.index)
return nlc
def remove_outliers(self, sigma=5., return_mask=False, **kwargs):
"""Removes outlier flux values using sigma-clipping.
This method returns a new LightCurve object from which flux values
are removed if they are separated from the mean flux by `sigma` times
the standard deviation.
Parameters
----------
sigma : float
The number of standard deviations to use for clipping outliers.
Defaults to 5.
return_mask : bool
Whether or not to return the mask indicating which data points
were removed. Entries marked as `True` are considered outliers.
**kwargs : dict
Dictionary of arguments to be passed to `astropy.stats.sigma_clip`.
Returns
-------
clean_lightcurve : LightCurve object
A new ``LightCurve`` in which outliers have been removed.
"""
with warnings.catch_warnings(): # Ignore warnings due to NaNs or Infs
warnings.simplefilter("ignore")
outlier_mask = sigma_clip(data=self.flux, sigma=sigma, **kwargs).mask
if return_mask:
return self[~outlier_mask], outlier_mask
return self[~outlier_mask]
def bin(self, binsize=13, method='mean'):
"""Bins a lightcurve using a function defined by `method`
on blocks of samples of size `binsize`.
Parameters
----------
binsize : int
Number of cadences to include in every bin.
method: str, one of 'mean' or 'median'
The summary statistic to return for each bin. Default: 'mean'.
Returns
-------
binned_lc : LightCurve object
Binned lightcurve.
Notes
-----
- If the ratio between the lightcurve length and the binsize is not
a whole number, then the remainder of the data points will be
ignored.
- If the original lightcurve contains flux uncertainties (flux_err),
the binned lightcurve will report the root-mean-square error.
If no uncertainties are included, the binned curve will return the
standard deviation of the data.
- If the original lightcurve contains a quality attribute, then the
bitwise OR of the quality flags will be returned per bin.
"""
available_methods = ['mean', 'median']
if method not in available_methods:
raise ValueError("method must be one of: {}".format(available_methods))
methodf = np.__dict__['nan' + method]
n_bins = self.flux.size // binsize
binned_lc = copy.copy(self)
binned_lc.time = np.array([methodf(a) for a in np.array_split(self.time, n_bins)])
binned_lc.flux = np.array([methodf(a) for a in np.array_split(self.flux, n_bins)])
if np.any(np.isfinite(self.flux_err)):
# root-mean-square error
binned_lc.flux_err = np.array(
[np.sqrt(np.nansum(a**2))
for a in np.array_split(self.flux_err, n_bins)]
) / binsize
else:
# compute the standard deviation from the data
binned_lc.flux_err = np.array([np.nanstd(a)
for a in np.array_split(self.flux, n_bins)])
if hasattr(binned_lc, 'quality'):
binned_lc.quality = np.array(
[np.bitwise_or.reduce(a) for a in np.array_split(self.quality, n_bins)])
if hasattr(binned_lc, 'centroid_col'):
binned_lc.centroid_col = np.array(
[methodf(a) for a in np.array_split(self.centroid_col, n_bins)])
if hasattr(binned_lc, 'centroid_row'):
binned_lc.centroid_row = np.array(
[methodf(a) for a in np.array_split(self.centroid_row, n_bins)])
return binned_lc
def cdpp(self, transit_duration=13, savgol_window=101, savgol_polyorder=2,
sigma_clip=5.):
"""Estimate the CDPP noise metric using the Savitzky-Golay (SG) method.
A common estimate of the noise in a lightcurve is the scatter that
remains after all long term trends have been removed. This is the idea
behind the Combined Differential Photometric Precision (CDPP) metric.
The official Kepler Pipeline computes this metric using a wavelet-based
algorithm to calculate the signal-to-noise of the specific waveform of
transits of various durations. In this implementation, we use the
simpler "sgCDPP proxy algorithm" discussed by Gilliland et al
(2011ApJS..197....6G) and Van Cleve et al (2016PASP..128g5002V).
The steps of this algorithm are:
1. Remove low frequency signals using a Savitzky-Golay filter with
window length `savgol_window` and polynomial order `savgol_polyorder`.
2. Remove outliers by rejecting data points which are separated from
the mean by `sigma_clip` times the standard deviation.
3. Compute the standard deviation of a running mean with
a configurable window length equal to `transit_duration`.
We use a running mean (as opposed to block averaging) to strongly
attenuate the signal above 1/transit_duration whilst retaining
the original frequency sampling. Block averaging would set the Nyquist
limit to 1/transit_duration.
Parameters
----------
transit_duration : int, optional
The transit duration in units of number of cadences. This is the
length of the window used to compute the running mean. The default
is 13, which corresponds to a 6.5 hour transit in data sampled at
30-min cadence.
savgol_window : int, optional
Width of Savitsky-Golay filter in cadences (odd number).
Default value 101 (2.0 days in Kepler Long Cadence mode).
savgol_polyorder : int, optional
Polynomial order of the Savitsky-Golay filter.
The recommended value is 2.
sigma_clip : float, optional
The number of standard deviations to use for clipping outliers.
The default is 5.
Returns
-------
cdpp : float
Savitzky-Golay CDPP noise metric in units parts-per-million (ppm).
Notes
-----
This implementation is adapted from the Matlab version used by
Jeff van Cleve but lacks the normalization factor used there:
svn+ssh://murzim/repo/so/trunk/Develop/jvc/common/compute_SG_noise.m
"""
if not isinstance(transit_duration, int):
raise ValueError("transit_duration must be an integer in units "
"number of cadences, got {}.".format(transit_duration))
detrended_lc = self.flatten(window_length=savgol_window,
polyorder=savgol_polyorder)
cleaned_lc = detrended_lc.remove_outliers(sigma=sigma_clip)
mean = running_mean(data=cleaned_lc.flux, window_size=transit_duration)
cdpp_ppm = np.std(mean) * 1e6
return cdpp_ppm
def _create_plot(self, method='plot', ax=None, normalize=True,
xlabel=None, ylabel=None, title='', style='lightkurve',
show_colorbar=True, colorbar_label='',
**kwargs):
"""Implements `plot()`, `scatter()`, and `errorbar()` to avoid code duplication.
Returns
-------
ax : matplotlib.axes._subplots.AxesSubplot
The matplotlib axes object.
"""
# Configure the default style
if style is None or style == 'lightkurve':
style = MPLSTYLE
# Default xlabel
if xlabel is None:
if self.time_format == 'bkjd':
xlabel = 'Time - 2454833 [BKJD days]'
elif self.time_format == 'btjd':
xlabel = 'Time - 2457000 [BTJD days]'
elif self.time_format == 'jd':
xlabel = 'Time [JD]'
else:
xlabel = 'Time'
# Default ylabel
if ylabel is None:
if normalize:
ylabel = 'Normalized Flux'
else:
ylabel = 'Flux [e$^-$s$^{-1}$]'
# Default legend label
if ('label' not in kwargs):
kwargs['label'] = self.label
# Normalize the data if requested
if normalize:
lc_normed = self.normalize()
flux, flux_err = lc_normed.flux, lc_normed.flux_err
else:
flux, flux_err = self.flux, self.flux_err
# Make the plot
with plt.style.context(style):
if ax is None:
fig, ax = plt.subplots(1)
if method == 'scatter':
sc = ax.scatter(self.time, flux, **kwargs)
# Colorbars should only be plotted if the user specifies, and there is
# a color specified that is not a string (e.g. 'C1') and is iterable.
if show_colorbar and ('c' in kwargs) and \
(not isinstance(kwargs['c'], str)) and hasattr(kwargs['c'], '__iter__'):
cbar = plt.colorbar(sc, ax=ax)
cbar.set_label(colorbar_label)
cbar.ax.yaxis.set_tick_params(tick1On=False, tick2On=False)
cbar.ax.minorticks_off()
elif method == 'errorbar':
ax.errorbar(x=self.time, y=flux, yerr=flux_err, **kwargs)
else:
ax.plot(self.time, flux, **kwargs)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
# Show the legend if labels were set
legend_labels = ax.get_legend_handles_labels()
if (np.sum([len(a) for a in legend_labels]) != 0):
ax.legend()
return ax
def plot(self, **kwargs):
"""Plot the light curve using matplotlib's `plot` method.
Parameters
----------
ax : matplotlib.axes._subplots.AxesSubplot
A matplotlib axes object to plot into. If no axes is provided,
a new one will be generated.
normalize : bool
Normalize the lightcurve before plotting?
xlabel : str
Plot x axis label
ylabel : str
Plot y axis label
title : str
Plot set_title
style : str
Path or URL to a matplotlib style file, or name of one of
matplotlib's built-in stylesheets (e.g. 'ggplot').
Lightkurve's custom stylesheet is used by default.
kwargs : dict
Dictionary of arguments to be passed to `matplotlib.pyplot.plot`.
Returns
-------
ax : matplotlib.axes._subplots.AxesSubplot
The matplotlib axes object.
"""
return self._create_plot(method='plot', **kwargs)
def scatter(self, colorbar_label='', show_colorbar=True, **kwargs):
"""Plots the light curve using matplotlib's `scatter` method.
Parameters
----------
ax : matplotlib.axes._subplots.AxesSubplot
A matplotlib axes object to plot into. If no axes is provided,
a new one will be generated.
normalize : bool
Normalize the lightcurve before plotting?
xlabel : str
Plot x axis label
ylabel : str
Plot y axis label
title : str
Plot set_title
style : str
Path or URL to a matplotlib style file, or name of one of
matplotlib's built-in stylesheets (e.g. 'ggplot').
Lightkurve's custom stylesheet is used by default.
colorbar_label : str
Label to show next to the colorbar (if `c` is given).
show_colorbar : boolean
Show the colorbar if colors are given using the `c` argument?
kwargs : dict
Dictionary of arguments to be passed to `matplotlib.pyplot.scatter`.
Returns
-------
ax : matplotlib.axes._subplots.AxesSubplot
The matplotlib axes object.
"""
return self._create_plot(method='scatter', colorbar_label=colorbar_label,
show_colorbar=show_colorbar, **kwargs)
def errorbar(self, linestyle='', **kwargs):
"""Plots the light curve using matplotlib's `errorbar` method.
Parameters
----------
ax : matplotlib.axes._subplots.AxesSubplot
A matplotlib axes object to plot into. If no axes is provided,
a new one will be generated.
normalize : bool
Normalize the lightcurve before plotting?
xlabel : str
Plot x axis label
ylabel : str
Plot y axis label
title : str
Plot set_title
style : str
Path or URL to a matplotlib style file, or name of one of
matplotlib's built-in stylesheets (e.g. 'ggplot').
Lightkurve's custom stylesheet is used by default.
linestyle : str
Connect the error bars using a line?
kwargs : dict
Dictionary of arguments to be passed to `matplotlib.pyplot.scatter`.
Returns
-------
ax : matplotlib.axes._subplots.AxesSubplot
The matplotlib axes object.
"""
if 'ls' not in kwargs:
kwargs['linestyle'] = linestyle
return self._create_plot(method='errorbar', **kwargs)
def to_table(self):
"""Export the LightCurve as an AstroPy Table.
Returns
-------
table : `astropy.table.Table` object
An AstroPy Table with columns 'time', 'flux', and 'flux_err'.
"""
return Table(data=(self.time, self.flux, self.flux_err),
names=('time', 'flux', 'flux_err'),
meta=self.meta)
def to_pandas(self, columns=['time', 'flux', 'flux_err']):
"""Export the LightCurve as a Pandas DataFrame.
Parameters
----------
columns : list of str
List of columns to include in the DataFrame. The names must match
attributes of the `LightCurve` object (e.g. `time`, `flux`).
Returns
-------
dataframe : `pandas.DataFrame` object
A dataframe indexed by `time` and containing the columns `flux`
and `flux_err`.
"""
try:
import pandas as pd
# lightkurve does not require pandas, so check for import success.
except ImportError:
raise ImportError("You need to install pandas to use the "
"LightCurve.to_pandas() method.")
data = {}
for col in columns:
if hasattr(self, col):
data[col] = vars(self)[col]
df = pd.DataFrame(data=data, index=self.time, columns=columns)
df.index.name = 'time'
return df
def to_csv(self, path_or_buf=None, **kwargs):
"""Writes the LightCurve to a csv file.
Parameters
----------
path_or_buf : string or file handle, default None
File path or object, if None is provided the result is returned as
a string.
**kwargs : dict
Dictionary of arguments to be passed to `pandas.DataFrame.to_csv()`.
Returns
-------
csv : str or None
Returns a csv-formatted string if `path_or_buf=None`,
returns None otherwise.
"""
return self.to_pandas().to_csv(path_or_buf=path_or_buf, **kwargs)
def periodogram(self, frequencies=None):
"""Returns a `Periodogram`.
Parameters
----------
frequencies : array-like
The regular grid of frequencies to use. The frequencies must be
in units microhertz. Alternatively, an AstroPy Quantity object can
be passed with any unit of type '1/time'.
Returns
-------
Periodogram : `Periodogram` object
Returns a Periodogram object extracted from the lightcurve.
"""
from . import Periodogram
return Periodogram.from_lightcurve(lc=self, frequencies=frequencies)
def to_fits(self, path=None, overwrite=False, **extra_data):
"""Writes the KeplerLightCurve to a fits file.
Parameters
----------
path : string, default None
File path, if None returns an astropy.io.fits object.
overwrite : bool
Whether or not to overwrite the file
extra_data : dict
Extra keywords or columns to include in the FITS file.
Arguments of type str, int, float, or bool will be stored as
keywords in the primary header.
Arguments of type np.array or list will be stored as columns
in the first extension.
Returns
-------
hdu : astropy.io.fits
Returns an astropy.io.fits object if path is None
"""
typedir = {int: 'J', str: 'A', float: 'D', bool: 'L',
np.int32: 'J', np.int32: 'K', np.float32: 'E', np.float64: 'D'}
def _header_template(extension):
"""Returns a template `fits.Header` object for a given extension."""
template_fn = os.path.join(PACKAGEDIR, "data",
"lc-ext{}-header.txt".format(extension))
return fits.Header.fromtextfile(template_fn)
def _make_primary_hdu(extra_data={}):
"""Returns the primary extension (#0)."""
hdu = fits.PrimaryHDU()
# Copy the default keywords from a template file from the MAST archive
tmpl = _header_template(0)
for kw in tmpl:
hdu.header[kw] = (tmpl[kw], tmpl.comments[kw])
# Override the defaults where necessary
from . import __version__
default = default = {'ORIGIN': "Unofficial data product",
'DATE': datetime.datetime.now().strftime("%Y-%m-%d"),
'CREATOR': "lightkurve",
'PROCVER': str(__version__)}
for kw in default:
hdu.header['{}'.format(kw).upper()] = default[kw]
if default[kw] is None:
log.warning('Value for {} is None.'.format(kw))
if ('quarter' in dir(self)) and (self.quarter is not None):
hdu.header['QUARTER'] = self.quarter
elif ('campaign' in dir(self)) and self.campaign is not None:
hdu.header['CAMPAIGN'] = self.campaign
else:
log.warning('Cannot find Campaign or Quarter number.')
for kw in extra_data:
if isinstance(extra_data[kw], (str, float, int, bool, type(None))):
hdu.header['{}'.format(kw).upper()] = extra_data[kw]
if extra_data[kw] is None:
log.warning('Value for {} is None.'.format(kw))
return hdu
def _make_lightcurve_extension(extra_data={}):
"""Create the 'LIGHTCURVE' extension (i.e. extension #1)."""
# Turn the data arrays into fits columns and initialize the HDU
cols = []
if ~np.asarray(['TIME' in k.upper() for k in extra_data.keys()]).any():
cols.append(fits.Column(name='TIME', format='D', unit=self.time_format,
array=self.time))
if ~np.asarray(['FLUX' in k.upper() for k in extra_data.keys()]).any():
cols.append(fits.Column(name='FLUX', format='E',
unit='counts', array=self.flux))
if 'flux_err' in dir(self):
if ~np.asarray(['FLUX_ERR' in k.upper() for k in extra_data.keys()]).any():
cols.append(fits.Column(name='FLUX_ERR', format='E',
unit='counts', array=self.flux_err))
if 'cadenceno' in dir(self):
if ~np.asarray(['CADENCENO' in k.upper() for k in extra_data.keys()]).any():
cols.append(fits.Column(name='CADENCENO', format='J',
array=self.cadenceno))
for kw in extra_data:
if isinstance(extra_data[kw], (np.ndarray, list)):
cols.append(fits.Column(name='{}'.format(kw).upper(),
format=typedir[type(extra_data[kw][0])],
array=extra_data[kw]))
coldefs = fits.ColDefs(cols)
hdu = fits.BinTableHDU.from_columns(coldefs)
hdu.header['EXTNAME'] = 'LIGHTCURVE'
return hdu
def _hdulist(**extra_data):
"""Returns an astropy.io.fits.HDUList object."""
return fits.HDUList([_make_primary_hdu(extra_data=extra_data),
_make_lightcurve_extension(extra_data=extra_data)])
def _header_template(extension):
"""Returns a template `fits.Header` object for a given extension."""
template_fn = os.path.join(PACKAGEDIR, "data",
"lc-ext{}-header.txt".format(extension))
return fits.Header.fromtextfile(template_fn)
hdu = _hdulist(**extra_data)
if path is not None:
hdu.writeto(path, overwrite=overwrite, checksum=True)
return hdu
class FoldedLightCurve(LightCurve):
"""Defines a folded lightcurve with different plotting defaults."""
def __init__(self, *args, **kwargs):
super(FoldedLightCurve, self).__init__(*args, **kwargs)
@property
def phase(self):
return self.time
def plot(self, **kwargs):
"""Plot the folded light curve usng matplotlib's `plot` method.
See `LightCurve.plot` for details on the accepted arguments.
Parameters
----------
kwargs : dict
Dictionary of arguments to be passed to `LightCurve.plot`.
Returns
-------
ax : matplotlib.axes._subplots.AxesSubplot
The matplotlib axes object.
"""
ax = super(FoldedLightCurve, self).plot(**kwargs)
if 'xlabel' not in kwargs:
ax.set_xlabel("Phase")
return ax
def scatter(self, **kwargs):
"""Plot the folded light curve usng matplotlib's `scatter` method.
See `LightCurve.scatter` for details on the accepted arguments.
Parameters
----------
kwargs : dict
Dictionary of arguments to be passed to `LightCurve.scatter`.
Returns
-------
ax : matplotlib.axes._subplots.AxesSubplot
The matplotlib axes object.
"""
ax = super(FoldedLightCurve, self).scatter(**kwargs)
if 'xlabel' not in kwargs:
ax.set_xlabel("Phase")
return ax
class KeplerLightCurve(LightCurve):
"""Defines a light curve class for NASA's Kepler and K2 missions.
Attributes
----------
time : array-like
Time measurements
flux : array-like
Data flux for every time point
flux_err : array-like
Uncertainty on each flux data point
time_format : str
String specifying how an instant of time is represented,
e.g. 'bkjd' or 'jd'.
time_scale : str
String which specifies how the time is measured,
e.g. tdb', 'tt', 'ut1', or 'utc'.
centroid_col : array-like
Centroid column coordinates as a function of time
centroid_row : array-like
Centroid row coordinates as a function of time
quality : array-like