How to recover the first TESS planet candidate with Lightkurve?#

Data from the TESS mission are available from the data archive at MAST. This tutorial demonstrates how the Lightkurve Python package can be used to read in these data and create your own TESS light curves with different aperture masks.

Below is a quick tutorial on how to get started using Lightkurve and TESS data. We’ll use the nearby, bright target Pi Mensae (ID 261136679), around which the mission team recently discovered a short period planet candidate on a 6.27 day orbit. See the pre-print paper by Huang et al (2018) for more details.

TESS data is stored in a binary file format which is documented in the TESS Science Data Products Description Document. Lightkurve provides a TessTargetPixelFile class which allows you to interact with the data easily.

import lightkurve as lk
search_result = lk.search_targetpixelfile('Pi Mensae', mission='TESS', sector=1)
SearchResult containing 2 data products.
0TESS Sector 012018SPOC1202611366790.0
1TESS Sector 012018TESS-SPOC18002611366790.0
tpf ='default')
/Users/chedges/repos/lightkurve/src/lightkurve/ LightkurveWarning: Warning: 2 files available to download. Only the first file has been downloaded. Please use `download_all()` or specify additional criteria (e.g. quarter, campaign, or sector) to limit your search.
TessTargetPixelFile(TICID: 261136679)

TessTargetPixelFile’s have many helpful methods and attributes. For example, you can access basic meta data on the target easily:

tpf.targetid  # TESS Input Catalog (TIC) Identifier
tpf.sector  # TESS Observation Sector
[9]:  # TESS Camera Number
tpf.ccd  # TESS CCD Number

We might want to plot the data, we can do this with the plot() method. You can add the keyword aperture_mask to plot an aperture on top of the image. In this case we’ve used the pipeline_mask which is stored in the original .fits file, but you can use any aperture you like.

%matplotlib inline

If you want to access the original fits file that generated the data you can use the hdu attribute of the tpf. This will return an object, for example

[< object at 0x296c015b0>, < object at 0x296c3cac0>, < object at 0x296c53d00>, < object at 0x296c53dc0>]

You can access each extension and the data inside it in the same way you’d use If you want to access data held in the TPF, such as the time of the observations, you can do that easily by using

<Time object: scale='tdb' format='btjd' value=[1325.2969605  1325.29834936 1325.29973823 ... 1353.17428819 1353.17567704
 1353.1770659 ]>

This returns the time in units of days counted since Julian Day 2457000.

You can access the corresponding flux values using

$[[[146.7256,~110.36988,~56.275379,~\dots,~39.974297,~44.420666,~13.883464],~ [12.40297,~22.714397,~35.56646,~\dots,~34.970276,~25.019585,~16.769943],~ [0.44226855,~5.2752562,~13.086924,~\dots,~46.673801,~33.625275,~27.014521],~ \dots,~ [9.5051775,~9.7198277,~1.3479111,~\dots,~70.286613,~42.281826,~11.773497],~ [5.480844,~7.9346728,~0.029079039,~\dots,~28.776463,~18.01915,~17.183647],~ [14.138475,~13.451326,~-0.96531624,~\dots,~20.603649,~56.804371,~34.841518]],~ [[146.51241,~102.70561,~59.262955,~\dots,~42.885513,~41.133427,~13.1896],~ [16.164961,~24.440937,~35.480656,~\dots,~33.928349,~23.814917,~16.947351],~ [2.3862846,~6.945066,~11.343194,~\dots,~46.577034,~33.296593,~29.945692],~ \dots,~ [10.620657,~10.516301,~3.0659428,~\dots,~71.220596,~41.962044,~12.826224],~ [10.079242,~7.8618164,~0.92223197,~\dots,~26.886044,~21.453985,~17.361023],~ [17.991547,~14.24785,~-0.89504874,~\dots,~20.604334,~60.592606,~37.873325]],~ [[147.11113,~103.21112,~55.811832,~\dots,~40.107876,~42.042709,~13.124676],~ [12.790492,~24.672497,~35.172104,~\dots,~36.092194,~24.669579,~16.882807],~ [3.3153291,~5.3842049,~13.671597,~\dots,~45.781773,~34.207008,~28.059145],~ \dots,~ [9.0220547,~9.8164053,~1.1087129,~\dots,~71.519043,~41.883968,~11.880278],~ [10.229019,~8.0392551,~2.2663257,~\dots,~24.198313,~21.375395,~18.227297],~ [18.198311,~11.786104,~3.7386706,~\dots,~18.869574,~57.385891,~33.9618]],~ \dots,~ [[161.15787,~108.03804,~51.858047,~\dots,~43.581772,~49.806713,~14.164904],~ [18.993103,~26.591282,~40.043686,~\dots,~35.556953,~23.060726,~15.365005],~ [0.31335491,~4.2895284,~13.231918,~\dots,~44.335342,~30.73526,~23.809629],~ \dots,~ [11.251328,~11.421753,~1.3267132,~\dots,~71.324409,~44.457657,~12.979733],~ [8.0688171,~6.0919147,~1.6751376,~\dots,~28.238346,~19.737053,~13.095853],~ [16.164452,~9.866703,~0.60539889,~\dots,~19.105186,~52.871838,~34.6008]],~ [[160.29982,~106.82688,~56.284225,~\dots,~40.847317,~46.883755,~11.852956],~ [15.770498,~24.730665,~38.325031,~\dots,~34.854435,~24.194151,~18.465128],~ [-0.87357336,~6.0762038,~12.59219,~\dots,~45.660442,~32.911388,~26.929564],~ \dots,~ [11.781666,~12.340034,~3.3242891,~\dots,~71.608543,~44.662857,~12.400381],~ [6.859838,~6.1376705,~0.20367658,~\dots,~28.518646,~19.021349,~15.210475],~ [18.555952,~14.363894,~0.84527528,~\dots,~20.324306,~58.342983,~32.927376]],~ [[159.06865,~111.19041,~53.964203,~\dots,~45.936127,~54.400459,~13.901671],~ [19.670254,~27.231472,~40.060177,~\dots,~36.812996,~26.284605,~16.961464],~ [-0.7221055,~7.5333309,~11.48659,~\dots,~46.671589,~31.16301,~26.355782],~ \dots,~ [11.926732,~12.063825,~-0.36293313,~\dots,~73.793037,~42.813068,~10.06179],~ [7.8724666,~6.7322803,~2.5688095,~\dots,~30.497356,~17.394106,~14.637597],~ [19.632471,~13.151638,~0.61876702,~\dots,~21.240358,~55.385933,~35.322235]]] \; \mathrm{\frac{e^{-}}{s}}$

Flux is a numpy.ndarray with a shape of (TIME x PIXELS x PIXELS). If you want to access just the first frame you can use

$[[146.7256,~110.36988,~56.275379,~\dots,~39.974297,~44.420666,~13.883464],~ [12.40297,~22.714397,~35.56646,~\dots,~34.970276,~25.019585,~16.769943],~ [0.44226855,~5.2752562,~13.086924,~\dots,~46.673801,~33.625275,~27.014521],~ \dots,~ [9.5051775,~9.7198277,~1.3479111,~\dots,~70.286613,~42.281826,~11.773497],~ [5.480844,~7.9346728,~0.029079039,~\dots,~28.776463,~18.01915,~17.183647],~ [14.138475,~13.451326,~-0.96531624,~\dots,~20.603649,~56.804371,~34.841518]] \; \mathrm{\frac{e^{-}}{s}}$

These values are in units electrons per second.

Building Light Curves from TPFs#

We can use the to_lightcurve() method to turn this TPF into a light curve using Simple Aperture Photometry. This will put an aperture on the target, and sum up the flux in all the pixels inside the aperture.

The default for to_lightcurve() is to use the mask generated by the TESS pipeline.

lc = tpf.to_lightcurve()

Now we can use the plot function to take a look at the data.


This looks pretty good, but maybe we can improve things by creating a new aperture.

aperture_mask = tpf.create_threshold_mask(threshold=10)

# Plot that aperture
lc = tpf.to_lightcurve(aperture_mask=aperture_mask)

There’s a long term trend in this dataset, which we can remove with a simple smoothing filter. You can use the lc.flatten() method to apply and divide the Savitzky-Golay smoothing filter. Here we’ll use a window_length of 1001 cadences, which is roughly a 5% of the full length of the light curve.

# Number of cadences in the full light curve
flat_lc = lc.flatten(window_length=1001)

The light curve looks much flatter. Unfortunately there is a portion of the light curve that is very noisy, due to a jitter in the TESS spacecraft. We can remove this simply by masking the light curve. First we’ll select the times that had the jitter.

# Flag the times that are good quality
mask = (flat_lc.time.value < 1346) | (flat_lc.time.value > 1350)

Then we can just clip those times out.

masked_lc = flat_lc[mask]

We can use Lightkurve to plot these two light curves over each other to see the difference.

# First define the `matplotlib.pyplot.axes`
ax = flat_lc.errorbar()

# Pass that axis to the next plot
masked_lc.errorbar(ax=ax, label='masked');

This looks much better. Now we might want to clip out some outliers from the light curve. We can do that with a simple lightkurve function remove_outliers().

clipped_lc = masked_lc.remove_outliers(sigma=6)

It’s a little hard to see these data because of the plotting style. Let’s use a scatter plot instead. We can do this with the lc.scatter() method. This method works in the same way that matplotlib.pyplot.scatter works, and takes in the same keyword arguments.


We can also add errorbars using the lc.errorbar() method.

ax = clipped_lc.scatter(s=0.1)
clipped_lc.errorbar(ax=ax, alpha=0.2);  # alpha determines the transparency

Finally let’s use lightkurve to fold the data at the exoplanet orbital period and see if we can see the transit.

folded_lc = clipped_lc.fold(period=6.27, epoch_time=1325.504)

It looks like there’s something there, but it’s hard to see. Let’s bin the light curve to reduce the number of points, but also reduce the uncertainty of those points.

import astropy.units as u
binned_lc = folded_lc.bin(time_bin_size=5*u.minute)

And now we can see the transit of Pi Men c!

Note that you can actually do all these steps in just a few lines:

lc = tpf.to_lightcurve(aperture_mask=aperture_mask).flatten(window_length=1001)
lc = lc[(lc.time.value < 1346) | (lc.time.value > 1350)]
lc.remove_outliers(sigma=6).fold(period=6.27, epoch_time=1325.504).bin(time_bin_size=5*u.minute).errorbar();

Comparing two apertures#

In the above tutorial we used our own aperture instead of the pipeline aperture. Let’s compare the results from using these two different apertures.

# Use the default
lc = tpf.to_lightcurve(aperture_mask=tpf.pipeline_mask).flatten(window_length=1001)
lc = lc[(lc.time.value < 1346) | (lc.time.value > 1350)].remove_outliers(6).fold(period=6.27, epoch_time=1325.504).bin(5*u.minute)

# Use a custom aperture
custom_lc = tpf.to_lightcurve(aperture_mask=aperture_mask).flatten(window_length=1001)
custom_lc = custom_lc[(custom_lc.time.value < 1346) | (custom_lc.time.value > 1350)].remove_outliers(6).fold(period=6.27, epoch_time=1325.504).bin(5*u.minute)
ax = lc.errorbar(label='Default aperture')
custom_lc.errorbar(ax=ax, label='Custom aperture');

The importance of using different aperture masks is clearly visible in the figure above. Note however that the data archive at MAST also contains lightcurve products which have more advanced systematics removal methods applied. We will explore those in a future tutorial!