-
Notifications
You must be signed in to change notification settings - Fork 157
/
lightcurve.py
1960 lines (1722 loc) · 81.5 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, and TessLightCurve."""
from __future__ import division, print_function
import copy
import os
import datetime
import logging
import warnings
import numpy as np
from scipy import signal
from scipy.interpolate import interp1d
from matplotlib import pyplot as plt
from copy import deepcopy
from astropy.stats import sigma_clip
from astropy.table import Table
from astropy.io import fits
from astropy.time import Time
from astropy import units as u
from . import PACKAGEDIR, MPLSTYLE
from .utils import (
running_mean, bkjd_to_astropy_time, btjd_to_astropy_time,
LightkurveWarning, validate_method
)
__all__ = ['LightCurve', 'KeplerLightCurve', 'TessLightCurve']
log = logging.getLogger(__name__)
class LightCurve(object):
"""Generic light curve object to hold time series photometry for one target.
Attributes
----------
time : array-like
Time values.
flux : array-like
Flux values for every time point.
flux_err : array-like
Uncertainty on each flux data point.
flux_unit : `~astropy.units.Unit` or str
Unit of the flux values. If a string is passed, it will be passed
on to `~astropy.units.Unit`.
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.
Examples
--------
>>> import lightkurve as lk
>>> lc = lk.LightCurve(time=[1, 2, 3, 4], flux=[0.97, 1.01, 1.03, 0.99])
>>> lc.time
array([1, 2, 3, 4])
>>> lc.flux
array([0.97, 1.01, 1.03, 0.99])
>>> lc.bin(binsize=2).flux
array([0.99, 1.01])
"""
def __init__(self, time=None, flux=None, flux_err=None, flux_unit=None,
time_format=None, time_scale=None, targetid=None, label=None,
meta=None):
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 = self._validate_time(time)
self.flux = self._validate_array(flux, name='flux')
self.flux_err = self._validate_array(flux_err, name='flux_err')
# If `time` or `flux` are astropy objects, we will retrieve
# `time_format`, `time_scale,` and `flux_unit` from them.
if isinstance(flux, u.Quantity):
flux_unit = flux.unit
if isinstance(time, Time):
time_format = time.format
time_scale = time.scale
self.flux_unit = flux_unit # @flux_unit.setter will validate this
self.time_format = time_format
self.time_scale = time_scale
self.targetid = targetid
self.label = label
if meta is None:
self.meta = {}
else:
self.meta = meta
@classmethod
def _validate_time(cls, time):
"""Ensure the `time` user input is valid."""
if isinstance(time, Time): # Support Astropy Time objects
time = time.value
time = np.asarray(time)
# Trigger warning if time=NaN are present
if np.isnan(time).any():
warnings.warn('LightCurve object contains NaN times', LightkurveWarning)
return time
def _validate_array(self, arr, name='array'):
"""Ensure the input flux/centroid/quality/etc arrays are valid and have
the exact same length as `self.time`."""
if arr is None: # arrays default to NaN arrays of length time
arr = np.nan * np.ones_like(self.time)
else:
arr = np.asarray(arr)
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 = self.copy()
copy_self.time = self.time[key]
copy_self.flux = self.flux[key]
copy_self.flux_err = self.flux_err[key]
return copy_self
def __len__(self):
return len(self.time)
def __add__(self, other):
newlc = self.copy()
if isinstance(other, LightCurve):
if len(self) != len(other):
raise ValueError("Cannot add LightCurve objects because "
"they do not have equal length ({} vs {})."
"".format(len(self), len(other)))
if np.any(self.time != other.time):
warnings.warn("Two LightCurve objects with inconsistent time "
"values are being added.")
newlc.flux = self.flux + other.flux
newlc.flux_err = np.hypot(self.flux_err, other.flux_err)
else:
newlc.flux = self.flux + other
return newlc
def __radd__(self, other):
return self.__add__(other)
def __sub__(self, other):
return self.__add__(-1 * other)
def __rsub__(self, other):
return (-1 * self).__add__(other)
def __mul__(self, other):
newlc = self.copy()
if isinstance(other, LightCurve):
if len(self) != len(other):
raise ValueError("Cannot multiply LightCurve objects because "
"they do not have equal length ({} vs {})."
"".format(len(self), len(other)))
if np.any(self.time != other.time):
warnings.warn("Two LightCurve objects with inconsistent time "
"values are being multiplied.")
newlc.flux = self.flux * other.flux
# Applying standard uncertainty propagation, cf.
# https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Example_formulae
newlc.flux_err = abs(newlc.flux) * np.hypot(self.flux_err / self.flux, other.flux_err / other.flux)
else:
newlc.flux = other * self.flux
newlc.flux_err = abs(other) * self.flux_err
return newlc
def __rmul__(self, other):
return self.__mul__(other)
def __truediv__(self, other):
return self.__mul__(1. / other)
def __rtruediv__(self, other):
newlc = self.copy()
if isinstance(other, LightCurve):
if len(self) != len(other):
raise ValueError("Cannot divide LightCurve objects because "
"they do not have equal length ({} vs {})."
"".format(len(self), len(other)))
if np.any(self.time != other.time):
warnings.warn("Two LightCurve objects with inconsistent time "
"values are being divided.")
newlc.flux = other.flux / self.flux
newlc.flux_err = abs(newlc.flux) * np.hypot(self.flux_err / self.flux, other.flux_err / other.flux)
else:
newlc.flux = other / self.flux
newlc.flux_err = abs((other * self.flux_err) / (self.flux**2))
return newlc
def __div__(self, other):
return self.__truediv__(other)
def __rdiv__(self, other):
return self.__rtruediv__(other)
@property
def flux_unit(self):
return self._flux_unit
@flux_unit.setter
def flux_unit(self, flux_unit):
# Validate user input for `flux_unit`
if flux_unit is None:
self._flux_unit = None
else:
try:
self._flux_unit = u.Unit(flux_unit)
except ValueError as e:
raise ValueError("invalid `flux_unit`: {}".format(e))
@property
def flux_quantity(self):
"""Returns the flux as an astropy.units.Quantity object."""
if isinstance(self.flux_unit, u.UnitBase):
return self.flux * self.flux_unit
else:
return self.flux * u.dimensionless_unscaled
@property
def astropy_time(self):
"""Returns the time values as an Astropy `~astropy.time.Time` object.
The Time object will be created based on the values of the light curve's
`time`, `time_format`, and `time_scale` attributes.
Examples
--------
The section below demonstrates working with time values using the TESS
light curve of Pi Mensae as an example, which we obtained as follows::
>>> import lightkurve as lk
>>> lc = lk.search_lightcurvefile("Pi Mensae", mission="TESS", sector=1).download().PDCSAP_FLUX
>>> lc
TessLightCurve(TICID: 261136679)
Every `LightCurve` object has a `time` attribute, which provides access
to the original array of time values given in the native format and
scale used by the data product from which the light curve was obtained::
>>> lc.time
array([1325.29698328, 1325.29837215, 1325.29976102, ..., 1353.17431099,
1353.17569985, 1353.17708871])
>>> lc.time_format
'btjd'
>>> lc.time_scale
'tdb'
To enable users to convert these time values to different formats or
scales, Lightkurve provides an easy way to access the time values
as an `AstroPy Time object <http://docs.astropy.org/en/stable/time/>`_::
>>> lc.astropy_time # doctest: +SKIP
<Time object: scale='tdb' format='jd' value=[2458325.29698328 2458325.29837215 2458325.29976102 ... 2458353.17431099
2458353.17569985 2458353.17708871]>
This is convenient because AstroPy Time objects provide a lot of useful
features. For example, we can now obtain the Julian Day or ISO values
that correspond to the raw time values::
>>> lc.astropy_time.iso # doctest: +SKIP
array(['2018-07-25 19:07:39.356', '2018-07-25 19:09:39.354',
'2018-07-25 19:11:39.352', ..., '2018-08-22 16:11:00.470',
'2018-08-22 16:13:00.467', '2018-08-22 16:15:00.464'], dtype='<U23')
>>> lc.astropy_time.jd # doctest: +SKIP
array([2458325.29698328, 2458325.29837215, 2458325.29976102, ...,
2458353.17431099, 2458353.17569985, 2458353.17708871])
Raises
------
ValueError
If the ``time_format`` attribute is not set or not one of the formats
allowed by AstroPy.
"""
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 show_properties(self):
"""Prints a description of all non-callable attributes.
Prints in order of type (ints, strings, lists, arrays, others).
"""
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'
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, inplace=False):
"""
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]
if inplace:
new_lc = self
else:
new_lc = self.copy()
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 copy(self):
"""Returns a copy of the LightCurve object.
This method uses the `copy.deepcopy` function to ensure that all
objects stored within the LightCurve are copied (e.g. time and flux).
Returns
-------
lc_copy : LightCurve
A new `LightCurve` object which is a copy of the original.
"""
return copy.deepcopy(self)
def flatten(self, window_length=101, polyorder=2, return_trend=False,
break_tolerance=5, niters=3, sigma=3, mask=None, **kwargs):
"""Removes the 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.
niters : int
Number of iterations to iteratively sigma clip and flatten. If more than one, will
perform the flatten several times, removing outliers each time.
sigma : int
Number of sigma above which to remove outliers from the flatten
mask : boolean array with length of self.time
Boolean array to mask data with before flattening. Flux values where
mask is True will not be used to flatten the data. An interpolated
result will be provided for these points. Use this mask to remove
data you want to preserve, e.g. transits.
**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 mask is None:
mask = np.ones(len(self.time), dtype=bool)
else:
# Deep copy ensures we don't change the original.
mask = deepcopy(~mask)
# No NaNs
mask &= np.isfinite(self.flux)
# No outliers
mask &= np.nan_to_num(np.abs(self.flux - np.nanmedian(self.flux))) <= (np.nanstd(self.flux) * sigma)
for iter in np.arange(0, niters):
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))
# Split the lightcurve into segments by finding large gaps in time
dt = self.time[mask][1:] - self.time[mask][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(self.time[mask]))
# Then, apply the savgol_filter to each segment separately
trend_signal = np.zeros(len(self.time[mask]))
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(self.flux[mask][l:h])
else:
# Scipy outputs a warning here that is not useful, will be fixed in version 1.2
with warnings.catch_warnings():
warnings.simplefilter('ignore', FutureWarning)
trend_signal[l:h] = signal.savgol_filter(x=self.flux[mask][l:h],
window_length=window_length,
polyorder=polyorder,
**kwargs)
# No outliers
mask1 = np.nan_to_num(np.abs(self.flux[mask] - trend_signal)) <\
(np.nanstd(self.flux[mask] - trend_signal) * sigma)
f = interp1d(self.time[mask][mask1], trend_signal[mask1], fill_value='extrapolate')
trend_signal = f(self.time)
mask[mask] &= mask1
flatten_lc = self.copy()
with warnings.catch_warnings():
# ignore invalid division warnings
warnings.simplefilter("ignore", RuntimeWarning)
flatten_lc.flux = flatten_lc.flux / trend_signal
flatten_lc.flux_err = flatten_lc.flux_err / trend_signal
if return_trend:
trend_lc = self.copy()
trend_lc.flux = trend_signal
return flatten_lc, trend_lc
return flatten_lc
def fold(self, period, t0=None, transit_midpoint=None):
"""Folds the lightcurve at a specified `period` and reference time `t0`.
This method returns a `~lightkurve.lightcurve.FoldedLightCurve` object
in which the time values range between -0.5 to +0.5 (i.e. the phase).
Data points which occur exactly at ``t0`` or an integer multiple of
``t0 + n*period`` will have phase value 0.0.
Examples
--------
The example below shows a light curve with a period dip which occurs near
time value 1001 and has a period of 5 days. Calling the `fold` method
will transform the light curve into a
`~lightkurve.lightcurve.FoldedLightCurve` object::
>>> import lightkurve as lk
>>> lc = lk.LightCurve(time=range(1001, 1012), flux=[0.5, 1.0, 1.0, 1.0, 1.0, 0.5, 1.0, 1.0, 1.0, 1.0, 0.5])
>>> folded_lc = lc.fold(period=5., t0=1006.)
>>> folded_lc # doctest: +SKIP
<lightkurve.lightcurve.FoldedLightCurve>
An object of type `~lightkurve.lightcurve.FoldedLightCurve` is useful
because it provides convenient access to the phase values and the
phase-folded fluxes::
>>> folded_lc.phase
array([-0.4, -0.4, -0.2, -0.2, 0. , 0. , 0. , 0.2, 0.2, 0.4, 0.4])
>>> folded_lc.flux
array([1. , 1. , 1. , 1. , 0.5, 0.5, 0.5, 1. , 1. , 1. , 1. ])
We can still access the original time values as well::
>>> folded_lc.time_original
array([1004, 1009, 1005, 1010, 1001, 1006, 1011, 1002, 1007, 1003, 1008])
A `~lightkurve.lightcurve.FoldedLightCurve` inherits all the features
of a standard `LightCurve` object. For example, we can very quickly
obtain a phase-folded plot using:
>>> folded_lc.plot() # doctest: +SKIP
Parameters
----------
period : float
The period upon which to fold, in the same units as this
LightCurve's ``time`` attribute.
t0 : float, optional
Time corresponding to zero phase, in the same units as this
LightCurve's ``time`` attribute. Defaults to 0 if not set.
transit_midpoint : float, optional
Deprecated. Use `t0` instead.
Returns
-------
folded_lightcurve : `~lightkurve.lightcurve.FoldedLightCurve`
A new light curve object in which the data are folded and sorted by
phase. The object contains an extra ``phase`` attribute.
"""
# Input validation. (Note: Quantities are simply ignored for now;
# we should consider adding extra validation here.)
if isinstance(period, u.quantity.Quantity):
period = period.value
if isinstance(t0, u.quantity.Quantity):
t0 = t0.value
# `transit_midpoint` is deprecated
if transit_midpoint is not None:
warnings.warn('`transit_midpoint` is deprecated, please use `t0` instead.',
LightkurveWarning)
if t0 is None:
t0 = transit_midpoint
if t0 is None:
t0 = 0.
if (t0 > 2450000):
if self.time_format == 'bkjd':
warnings.warn('`t0` appears to be given in JD, '
'however the light curve time uses BKJD '
'(i.e. JD - 2454833).', LightkurveWarning)
elif self.time_format == 'btjd':
warnings.warn('`t0` appears to be given in JD, '
'however the light curve time uses BTJD '
'(i.e. JD - 2457000).', LightkurveWarning)
phase = (t0 % period) / period
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(time=fold_time[sorted_args],
flux=self.flux[sorted_args],
flux_err=self.flux_err[sorted_args],
time_original=self.time[sorted_args],
targetid=self.targetid,
label=self.label,
meta=self.meta)
def normalize(self, unit='unscaled'):
"""Returns a normalized version of the light curve.
The normalized light curve is obtained by dividing the ``flux`` and
``flux_err`` object attributes by the by the median flux.
Optionally, the result will be multiplied by 1e2 (if `unit='percent'`),
1e3 (`unit='ppt'`), or 1e6 (`unit='ppm'`).
Parameters
----------
unit : 'unscaled', 'percent', 'ppt', 'ppm'
The desired relative units of the normalized light curve;
'ppt' means 'parts per thousand', 'ppm' means 'parts per million'.
Examples
--------
>>> import lightkurve as lk
>>> lc = lk.LightCurve(time=[1, 2, 3], flux=[25945.7, 25901.5, 25931.2], flux_err=[6.8, 4.6, 6.2])
>>> normalized_lc = lc.normalize()
>>> normalized_lc.flux
array([1.00055917, 0.99885466, 1. ])
>>> normalized_lc.flux_err
array([0.00026223, 0.00017739, 0.00023909])
Returns
-------
normalized_lightcurve : `LightCurve`
A new light curve object in which ``flux`` and ``flux_err`` have
been divided by the median flux.
Warns
-----
LightkurveWarning
If the median flux is negative or within half a standard deviation
from zero.
"""
validate_method(unit, ['unscaled', 'percent', 'ppt', 'ppm'])
median_flux = np.nanmedian(self.flux)
std_flux = np.nanstd(self.flux)
# If the median flux is within half a standard deviation from zero, the
# light curve is likely zero-centered and normalization makes no sense.
if (median_flux == 0) or (np.isfinite(std_flux) and (np.abs(median_flux) < 0.5*std_flux)):
warnings.warn("The light curve appears to be zero-centered "
"(median={:.2e} +/- {:.2e}); `normalize()` will divide "
"the light curve by a value close to zero, which is "
"probably not what you want."
"".format(median_flux, std_flux),
LightkurveWarning)
# If the median flux is negative, normalization will invert the light
# curve and makes no sense.
if median_flux < 0:
warnings.warn("The light curve has a negative median flux ({:.2e});"
" `normalize()` will therefore divide by a negative "
"number and invert the light curve, which is probably"
"not what you want".format(median_flux),
LightkurveWarning)
# Warn if the light curve is already in relative units.
if isinstance(self._flux_unit, u.UnitBase) and \
self._flux_unit.is_equivalent(u.dimensionless_unscaled):
warnings.warn("The light curve already appears to be in relative "
"units; `normalize()` will convert the light curve "
"into relative units for a second time, which is "
"probably not what you want.".format(self._flux_unit),
LightkurveWarning)
# Create a new light curve instance and normalize its values
lc = self.copy()
lc.flux = lc.flux / median_flux
lc.flux_err = lc.flux_err / median_flux
lc.flux_unit = u.dimensionless_unscaled
# Set the desired relative (dimensionless) units
if unit == 'unscaled':
lc.flux_unit = u.dimensionless_unscaled
elif unit == 'percent':
lc.flux_unit = u.percent
lc.flux *= 100
lc.flux_err *= 100
elif unit == 'ppt': # parts per thousand
# ppt is not included in astropy, so we define it here
lc.flux_unit = u.def_unit(['ppt', 'parts per thousand'], u.Unit(1e-3))
lc.flux *= 1000
lc.flux_err *= 1000
elif unit == 'ppm': # parts per million
lc.flux_unit = u.cds.ppm
lc.flux *= 1000000
lc.flux_err *= 1000000
return lc
def remove_nans(self):
"""Removes cadences where the flux is NaN.
Returns
-------
clean_lightcurve : `LightCurve`
A new light curve object from which NaNs fluxes have been removed.
"""
return self[~np.isnan(self.flux)] # This will return a sliced copy
def fill_gaps(self, method='gaussian_noise'):
"""Fill in gaps in time.
By default, the gaps will be filled with random white Gaussian noise
distributed according to
:math:`\mathcal{N} (\mu=\overline{\mathrm{flux}}, \sigma=\mathrm{CDPP})`.
No other methods are supported at this time.
Parameters
----------
method : string {'gaussian_noise'}
Method to use for gap filling. Fills with Gaussian noise by default.
Returns
-------
filled_lightcurve : `LightCurve`
A new light curve object in which all NaN values and gaps in time
have been filled.
"""
lc = self.copy().remove_nans()
nlc = lc.copy()
# Find missing time points
# Most precise method, taking into account time variation due to orbit
if hasattr(lc, 'cadenceno'):
dt = lc.time - np.median(np.diff(lc.time)) * lc.cadenceno
ncad = np.arange(lc.cadenceno[0], lc.cadenceno[-1] + 1, 1)
in_original = np.in1d(ncad, lc.cadenceno)
ncad = ncad[~in_original]
ndt = np.interp(ncad, lc.cadenceno, dt)
ncad = np.append(ncad, lc.cadenceno)
ndt = np.append(ndt, dt)
ncad, ndt = ncad[np.argsort(ncad)], ndt[np.argsort(ncad)]
ntime = ndt + np.median(np.diff(lc.time)) * ncad
nlc.cadenceno = ncad
else:
# Less precise method
dt = np.nanmedian(lc.time[1::] - lc.time[:-1:])
ntime = [lc.time[0]]
for t in lc.time[1::]:
prevtime = ntime[-1]
while (t - prevtime) > 1.2*dt:
ntime.append(prevtime + dt)
prevtime = ntime[-1]
ntime.append(t)
ntime = np.asarray(ntime, float)
in_original = np.in1d(ntime, lc.time)
# Fill in time points
nlc.time = ntime
f = np.zeros(len(ntime))
f[in_original] = np.copy(lc.flux)
fe = np.zeros(len(ntime))
fe[in_original] = np.copy(lc.flux_err)
fe[~in_original] = np.interp(ntime[~in_original], lc.time, lc.flux_err)
if method == 'gaussian_noise':
try:
std = lc.estimate_cdpp()*1e-6
except:
std = lc.flux.std()
f[~in_original] = np.random.normal(lc.flux.mean(), std, (~in_original).sum())
else:
raise NotImplementedError("No such method as {}".format(method))
nlc.flux = f
nlc.flux_err = fe
if hasattr(lc, 'quality'):
quality = np.zeros(len(ntime))
quality[in_original] = np.copy(lc.quality)
quality[~in_original] += 65536
nlc.quality = quality
if hasattr(lc, 'centroid_col'):
col = np.zeros(len(ntime)) * np.nan
col[in_original] = np.copy(lc.centroid_col)
nlc.centroid_col = col
if hasattr(lc, 'centroid_row'):
row = np.zeros(len(ntime)) * np.nan
row[in_original] = np.copy(lc.centroid_row)
nlc.centroid_row = row
return nlc
def remove_outliers(self, sigma=5., sigma_lower=None, sigma_upper=None,
return_mask=False, **kwargs):
"""Removes outlier data points using sigma-clipping.
This method returns a new `LightCurve` object from which data points
are removed if their flux values are greater or smaller than the median
flux by at least ``sigma`` times the standard deviation.
Sigma-clipping works by iterating over data points, each time rejecting
values that are discrepant by more than a specified number of standard
deviations from a center value. If the data contains invalid values
(NaNs or infs), they are automatically masked before performing the
sigma clipping.
.. note::
This function is a convenience wrapper around
`astropy.stats.sigma_clip()` and provides the same functionality.
Any extra arguments passed to this method will be passed on to
``sigma_clip``.
Parameters
----------
sigma : float
The number of standard deviations to use for both the lower and
upper clipping limit. These limits are overridden by
``sigma_lower`` and ``sigma_upper``, if input. Defaults to 5.
sigma_lower : float or None
The number of standard deviations to use as the lower bound for
the clipping limit. Can be set to float('inf') in order to avoid
clipping outliers below the median at all. If `None` then the
value of ``sigma`` is used. Defaults to `None`.
sigma_upper : float or None
The number of standard deviations to use as the upper bound for
the clipping limit. Can be set to float('inf') in order to avoid
clipping outliers above the median at all. If `None` then the
value of ``sigma`` is used. Defaults to `None`.
return_mask : bool
Whether or not to return a mask (i.e. a boolean array) indicating
which data points were removed. Entries marked as `True` in the
mask are considered outliers. This mask is not returned by default.
**kwargs : dict
Dictionary of arguments to be passed to `astropy.stats.sigma_clip`.
Returns
-------
clean_lc : `LightCurve`
A new light curve object from which outlier data points have been
removed.
outlier_mask : NumPy array, optional
Boolean array flagging which cadences were removed.
Only returned if `return_mask=True`.
Examples
--------
This example generates a new light curve in which all points
that are more than 1 standard deviation from the median are removed::
>>> lc = LightCurve(time=[1, 2, 3, 4, 5], flux=[1, 1000, 1, -1000, 1])
>>> lc_clean = lc.remove_outliers(sigma=1)
>>> lc_clean.time
array([1, 3, 5])
>>> lc_clean.flux
array([1, 1, 1])
Instead of specifying `sigma`, you may specify separate `sigma_lower`
and `sigma_upper` parameters to remove only outliers above or below
the median. For example::
>>> lc = LightCurve(time=[1, 2, 3, 4, 5], flux=[1, 1000, 1, -1000, 1])
>>> lc_clean = lc.remove_outliers(sigma_lower=float('inf'), sigma_upper=1)
>>> lc_clean.time
array([1, 3, 4, 5])
>>> lc_clean.flux
array([ 1, 1, -1000, 1])
Optionally, you may use the `return_mask` parameter to return a boolean
array which flags the outliers identified by the method. For example::
>>> lc_clean, mask = lc.remove_outliers(sigma=1, return_mask=True)
>>> mask
array([False, True, False, True, False])
"""
# First, we create the outlier mask using AstroPy's sigma_clip function
with warnings.catch_warnings(): # Ignore warnings due to NaNs or Infs
warnings.simplefilter("ignore")
outlier_mask = sigma_clip(data=self.flux,
sigma=sigma,
sigma_lower=sigma_lower,
sigma_upper=sigma_upper,
**kwargs).mask
# Second, we return the masked light curve and optionally the mask itself
if return_mask:
return self[~outlier_mask], outlier_mask
return self[~outlier_mask]
def bin(self, binsize=13, method='mean'):
"""Bins a lightcurve in blocks of size ``binsize``.
The value of the bins will contain the mean (``method='mean'``) or the
median (``method='median'``) of the original data. The default is mean.
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`
A new light curve which has been binned.
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 light curve 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.
"""
method = validate_method(method, supported_methods=['mean', 'median'])
methodf = np.__dict__['nan' + method]
n_bins = self.flux.size // binsize
binned_lc = self.copy()
indexes = np.array_split(np.arange(len(self.time)), n_bins)
binned_lc.time = np.array([methodf(self.time[a]) for a in indexes])
binned_lc.flux = np.array([methodf(self.flux[a]) for a in indexes])
if np.any(np.isfinite(self.flux_err)):
# root-mean-square error
binned_lc.flux_err = np.array(
[np.sqrt(np.nansum(self.flux_err[a]**2))
for a in indexes]
) / binsize
else:
# If the original light curve does not provide `flux_err`,
# then report the standard deviations of the fluxes in each bin.
binned_lc.flux_err = np.array([np.nanstd(self.flux[a]) for a in indexes])
if hasattr(binned_lc, 'quality'):
# Note: np.bitwise_or only works if there are no NaNs
binned_lc.quality = np.array(
[np.bitwise_or.reduce(a) if np.all(np.isfinite(a)) else np.nan
for a in np.array_split(self.quality, n_bins)])
if hasattr(binned_lc, 'cadenceno'):
binned_lc.cadenceno = np.array([np.nan] * n_bins)
if hasattr(binned_lc, 'centroid_col'):
# Note: nanmean/nanmedian yield a RuntimeWarning if a slice is all NaNs
binned_lc.centroid_col = np.array(
[methodf(a) if np.any(np.isfinite(a)) else np.nan
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) if np.any(np.isfinite(a)) else np.nan
for a in np.array_split(self.centroid_row, n_bins)])
return binned_lc
def estimate_cdpp(self, transit_duration=13, savgol_window=101,
savgol_polyorder=2, sigma=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` 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 : 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