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.

[1]:
import lightkurve as lk
[2]:
search_result = lk.search_targetpixelfile('Pi Mensae', mission='TESS', sector=1)
[3]:
search_result
[3]:
SearchResult containing 2 data products.
#missionyearauthorexptimetarget_namedistance
sarcsec
0TESS Sector 012018SPOC1202611366790.0
1TESS Sector 012018TESS-SPOC18002611366790.0
[4]:
tpf = search_result.download(quality_bitmask='default')
/Users/chedges/repos/lightkurve/src/lightkurve/search.py:424: 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.
  warnings.warn(
[5]:
tpf
[5]:
TessTargetPixelFile(TICID: 261136679)

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

[6]:
tpf.mission
[6]:
'TESS'
[7]:
tpf.targetid  # TESS Input Catalog (TIC) Identifier
[7]:
261136679
[8]:
tpf.sector  # TESS Observation Sector
[8]:
1
[9]:
tpf.camera  # TESS Camera Number
[9]:
4
[10]:
tpf.ccd  # TESS CCD Number
[10]:
2

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.

[11]:
%matplotlib inline
tpf.plot(aperture_mask=tpf.pipeline_mask);
../../_images/tutorials_3-science-examples_exoplanets-recover-first-tess-candidate_15_0.png

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 astropy.io.fits object, for example

[12]:
tpf.hdu
[12]:
[<astropy.io.fits.hdu.image.PrimaryHDU object at 0x296c015b0>, <astropy.io.fits.hdu.table.BinTableHDU object at 0x296c3cac0>, <astropy.io.fits.hdu.image.ImageHDU object at 0x296c53d00>, <astropy.io.fits.hdu.table.BinTableHDU object at 0x296c53dc0>]

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

[13]:
tpf.time
[13]:
<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

[14]:
tpf.flux
[14]:
$[[[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

[15]:
tpf.flux[0]
[15]:
$[[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.

[16]:
lc = tpf.to_lightcurve()

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

[17]:
lc.errorbar();
../../_images/tutorials_3-science-examples_exoplanets-recover-first-tess-candidate_28_0.png

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

[18]:
aperture_mask = tpf.create_threshold_mask(threshold=10)

# Plot that aperture
tpf.plot(aperture_mask=aperture_mask);
../../_images/tutorials_3-science-examples_exoplanets-recover-first-tess-candidate_30_0.png
[19]:
lc = tpf.to_lightcurve(aperture_mask=aperture_mask)
[20]:
lc.errorbar();
../../_images/tutorials_3-science-examples_exoplanets-recover-first-tess-candidate_32_0.png

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.

[21]:
# Number of cadences in the full light curve
print(lc.time.shape)
(18279,)
[22]:
flat_lc = lc.flatten(window_length=1001)
flat_lc.errorbar();
../../_images/tutorials_3-science-examples_exoplanets-recover-first-tess-candidate_35_0.png

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.

[23]:
# 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.

[24]:
masked_lc = flat_lc[mask]
masked_lc.errorbar();
../../_images/tutorials_3-science-examples_exoplanets-recover-first-tess-candidate_39_0.png

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

[25]:
# First define the `matplotlib.pyplot.axes`
ax = flat_lc.errorbar()

# Pass that axis to the next plot
masked_lc.errorbar(ax=ax, label='masked');
../../_images/tutorials_3-science-examples_exoplanets-recover-first-tess-candidate_41_0.png

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().

[26]:
clipped_lc = masked_lc.remove_outliers(sigma=6)
clipped_lc.errorbar();
../../_images/tutorials_3-science-examples_exoplanets-recover-first-tess-candidate_43_0.png

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.

[27]:
clipped_lc.scatter(s=0.1);
../../_images/tutorials_3-science-examples_exoplanets-recover-first-tess-candidate_45_0.png

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

[28]:
ax = clipped_lc.scatter(s=0.1)
clipped_lc.errorbar(ax=ax, alpha=0.2);  # alpha determines the transparency
../../_images/tutorials_3-science-examples_exoplanets-recover-first-tess-candidate_47_0.png

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

[29]:
folded_lc = clipped_lc.fold(period=6.27, epoch_time=1325.504)
folded_lc.errorbar();
../../_images/tutorials_3-science-examples_exoplanets-recover-first-tess-candidate_49_0.png

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.

[30]:
import astropy.units as u
binned_lc = folded_lc.bin(time_bin_size=5*u.minute)
binned_lc.errorbar();
../../_images/tutorials_3-science-examples_exoplanets-recover-first-tess-candidate_51_0.png

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:

[31]:
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();
../../_images/tutorials_3-science-examples_exoplanets-recover-first-tess-candidate_53_0.png

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.

[32]:
# 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)
[33]:
ax = lc.errorbar(label='Default aperture')
custom_lc.errorbar(ax=ax, label='Custom aperture');
../../_images/tutorials_3-science-examples_exoplanets-recover-first-tess-candidate_56_0.png

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!