def periodogram(time, lightcurve): period_grid = np.exp(np.linspace(np.log(1), np.log(15), 50000)) bls = BoxLeastSquares(time, lightcurve) bls_power = bls.power(period_grid, 0.1, oversample=20) return bls, bls_power, period_grid
def bls_serial_pfind( times, mags, errs, magsarefluxes=False, startp=0.1, # search from 0.1 d to... endp=100.0, # ... 100.0 d -- don't search full timebase stepsize=5.0e-4, mintransitduration=0.01, # minimum transit length in phase maxtransitduration=0.4, # maximum transit length in phase ndurations=100, autofreq=True, # figure out f0, nf, and df automatically blsobjective='likelihood', blsmethod='fast', blsoversample=10, blsmintransits=3, blsfreqfactor=10.0, periodepsilon=0.1, nbestpeaks=5, sigclip=10.0, endp_timebase_check=True, verbose=True, raiseonfail=False): '''Runs the Box Least Squares Fitting Search for transit-shaped signals. Based on the version of BLS in Astropy 3.1: `astropy.stats.BoxLeastSquares`. If you don't have Astropy 3.1, this module will fail to import. Note that by default, this implementation of `bls_serial_pfind` doesn't use the `.autoperiod()` function from `BoxLeastSquares` but uses the same auto frequency-grid generation as the functions in `periodbase.kbls`. If you want to use Astropy's implementation, set the value of `autofreq` kwarg to 'astropy'. The dict returned from this function contains a `blsmodel` key, which is the generated model from Astropy's BLS. Use the `.compute_stats()` method to calculate the required stats like SNR, depth, duration, etc. Parameters ---------- times,mags,errs : np.array The magnitude/flux time-series to search for transits. magsarefluxes : bool If the input measurement values in `mags` and `errs` are in fluxes, set this to True. startp,endp : float The minimum and maximum periods to consider for the transit search. stepsize : float The step-size in frequency to use when constructing a frequency grid for the period search. mintransitduration,maxtransitduration : float The minimum and maximum transitdurations (in units of phase) to consider for the transit search. ndurations : int The number of transit durations to use in the period-search. autofreq : bool or str If this is True, the values of `stepsize` and `nphasebins` will be ignored, and these, along with a frequency-grid, will be determined based on the following relations:: nphasebins = int(ceil(2.0/mintransitduration)) if nphasebins > 3000: nphasebins = 3000 stepsize = 0.25*mintransitduration/(times.max()-times.min()) minfreq = 1.0/endp maxfreq = 1.0/startp nfreq = int(ceil((maxfreq - minfreq)/stepsize)) If this is False, you must set `startp`, `endp`, and `stepsize` as appropriate. If this is str == 'astropy', will use the `astropy.stats.BoxLeastSquares.autoperiod()` function to calculate the frequency grid instead of the kbls method. blsobjective : {'likelihood','snr'} Sets the type of objective to optimize in the `BoxLeastSquares.power()` function. blsmethod : {'fast','slow'} Sets the type of method to use in the `BoxLeastSquares.power()` function. blsoversample : {'likelihood','snr'} Sets the `oversample` kwarg for the `BoxLeastSquares.power()` function. blsmintransits : int Sets the `min_n_transits` kwarg for the `BoxLeastSquares.autoperiod()` function. blsfreqfactor : float Sets the `frequency_factor` kwarg for the `BoxLeastSquares.autperiod()` function. periodepsilon : float The fractional difference between successive values of 'best' periods when sorting by periodogram power to consider them as separate periods (as opposed to part of the same periodogram peak). This is used to avoid broad peaks in the periodogram and make sure the 'best' periods returned are all actually independent. nbestpeaks : int The number of 'best' peaks to return from the periodogram results, starting from the global maximum of the periodogram peak values. sigclip : float or int or sequence of two floats/ints or None If a single float or int, a symmetric sigma-clip will be performed using the number provided as the sigma-multiplier to cut out from the input time-series. If a list of two ints/floats is provided, the function will perform an 'asymmetric' sigma-clip. The first element in this list is the sigma value to use for fainter flux/mag values; the second element in this list is the sigma value to use for brighter flux/mag values. For example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma dimmings and greater than 3-sigma brightenings. Here the meaning of "dimming" and "brightening" is set by *physics* (not the magnitude system), which is why the `magsarefluxes` kwarg must be correctly set. If `sigclip` is None, no sigma-clipping will be performed, and the time-series (with non-finite elems removed) will be passed through to the output. endp_timebase_check : bool If True, will check if the ``endp`` value is larger than the time-base of the observations. If it is, will change the ``endp`` value such that it is half of the time-base. If False, will allow an ``endp`` larger than the time-base of the observations. verbose : bool If this is True, will indicate progress and details about the frequency grid used for the period search. raiseonfail : bool If True, raises an exception if something goes wrong. Otherwise, returns None. Returns ------- dict This function returns a dict, referred to as an `lspinfo` dict in other astrobase functions that operate on periodogram results. This is a standardized format across all astrobase period-finders, and is of the form below:: {'bestperiod': the best period value in the periodogram, 'bestlspval': the periodogram peak associated with the best period, 'nbestpeaks': the input value of nbestpeaks, 'nbestlspvals': nbestpeaks-size list of best period peak values, 'nbestperiods': nbestpeaks-size list of best periods, 'lspvals': the full array of periodogram powers, 'frequencies': the full array of frequencies considered, 'periods': the full array of periods considered, 'durations': the array of durations used to run BLS, 'blsresult': Astropy BLS result object (BoxLeastSquaresResult), 'blsmodel': Astropy BLS BoxLeastSquares object used for work, 'stepsize': the actual stepsize used, 'nfreq': the actual nfreq used, 'durations': the durations array used, 'mintransitduration': the input mintransitduration, 'maxtransitduration': the input maxtransitdurations, 'method':'bls' -> the name of the period-finder method, 'kwargs':{ dict of all of the input kwargs for record-keeping}} ''' # get rid of nans first and sigclip stimes, smags, serrs = sigclip_magseries(times, mags, errs, magsarefluxes=magsarefluxes, sigclip=sigclip) # make sure there are enough points to calculate a spectrum if len(stimes) > 9 and len(smags) > 9 and len(serrs) > 9: # if we're setting up everything automatically if isinstance(autofreq, bool) and autofreq: # use heuristic to figure out best timestep stepsize = 0.25 * mintransitduration / (stimes.max() - stimes.min()) # now figure out the frequencies to use minfreq = 1.0 / endp maxfreq = 1.0 / startp nfreq = int(npceil((maxfreq - minfreq) / stepsize)) # say what we're using if verbose: LOGINFO('min P: %s, max P: %s, nfreq: %s, ' 'minfreq: %s, maxfreq: %s' % (startp, endp, nfreq, minfreq, maxfreq)) LOGINFO('autofreq = True: using AUTOMATIC values for ' 'freq stepsize: %s, ndurations: %s, ' 'min transit duration: %s, max transit duration: %s' % (stepsize, ndurations, mintransitduration, maxtransitduration)) use_autoperiod = False elif isinstance(autofreq, bool) and not autofreq: minfreq = 1.0 / endp maxfreq = 1.0 / startp nfreq = int(npceil((maxfreq - minfreq) / stepsize)) # say what we're using if verbose: LOGINFO('min P: %s, max P: %s, nfreq: %s, ' 'minfreq: %s, maxfreq: %s' % (startp, endp, nfreq, minfreq, maxfreq)) LOGINFO('autofreq = False: using PROVIDED values for ' 'freq stepsize: %s, ndurations: %s, ' 'min transit duration: %s, max transit duration: %s' % (stepsize, ndurations, mintransitduration, maxtransitduration)) use_autoperiod = False elif isinstance(autofreq, str) and autofreq == 'astropy': use_autoperiod = True minfreq = 1.0 / endp maxfreq = 1.0 / startp else: LOGERROR("unknown autofreq kwarg encountered. can't continue...") return None # check the minimum frequency if ((minfreq < (1.0 / (stimes.max() - stimes.min()))) and endp_timebase_check): LOGWARNING('the requested max P = %.3f is larger than ' 'the time base of the observations = %.3f, ' ' will make minfreq = 2 x 1/timebase' % (endp, stimes.max() - stimes.min())) minfreq = 2.0 / (stimes.max() - stimes.min()) LOGWARNING('new minfreq: %s, maxfreq: %s' % (minfreq, maxfreq)) # run BLS try: # astropy's BLS requires durations in units of time durations = nplinspace(mintransitduration * startp, maxtransitduration * startp, ndurations) # set up the correct units for the BLS model if magsarefluxes: blsmodel = BoxLeastSquares(stimes * u.day, smags * u.dimensionless_unscaled, dy=serrs * u.dimensionless_unscaled) else: blsmodel = BoxLeastSquares(stimes * u.day, smags * u.mag, dy=serrs * u.mag) # use autoperiod if requested if use_autoperiod: periods = nparray( blsmodel.autoperiod(durations, minimum_period=startp, maximum_period=endp, minimum_n_transit=blsmintransits, frequency_factor=blsfreqfactor)) nfreq = periods.size if verbose: LOGINFO("autofreq = 'astropy', used .autoperiod() with " "minimum_n_transit = %s, freq_factor = %s " "to generate the frequency grid" % (blsmintransits, blsfreqfactor)) LOGINFO( 'stepsize = %.5f, nfreq = %s, minfreq = %.5f, ' 'maxfreq = %.5f, ndurations = %s' % (abs(1.0 / periods[1] - 1.0 / periods[0]), nfreq, 1.0 / periods.max(), 1.0 / periods.min(), durations.size)) # otherwise, use kbls method else: frequencies = minfreq + nparange(nfreq) * stepsize periods = 1.0 / frequencies if nfreq > 5.0e5: if verbose: LOGWARNING('more than 5.0e5 frequencies to go through; ' 'this will take a while. ' 'you might want to use the ' 'abls.bls_parallel_pfind function instead') # run the periodogram blsresult = blsmodel.power(periods * u.day, durations * u.day, objective=blsobjective, method=blsmethod, oversample=blsoversample) # get the peak values lsp = nparray(blsresult.power) # find the nbestpeaks for the periodogram: 1. sort the lsp array # by highest value first 2. go down the values until we find # five values that are separated by at least periodepsilon in # period # make sure to get only the finite peaks in the periodogram # this is needed because BLS may produce infs for some peaks finitepeakind = npisfinite(lsp) finlsp = lsp[finitepeakind] finperiods = periods[finitepeakind] # make sure that finlsp has finite values before we work on it try: bestperiodind = npargmax(finlsp) except ValueError: LOGERROR('no finite periodogram values ' 'for this mag series, skipping...') return { 'bestperiod': npnan, 'bestlspval': npnan, 'nbestpeaks': nbestpeaks, 'nbestinds': None, 'nbestlspvals': None, 'nbestperiods': None, 'lspvals': None, 'periods': None, 'durations': None, 'method': 'bls', 'blsresult': None, 'blsmodel': None, 'kwargs': { 'startp': startp, 'endp': endp, 'stepsize': stepsize, 'mintransitduration': mintransitduration, 'maxtransitduration': maxtransitduration, 'ndurations': ndurations, 'blsobjective': blsobjective, 'blsmethod': blsmethod, 'blsoversample': blsoversample, 'blsntransits': blsmintransits, 'blsfreqfactor': blsfreqfactor, 'autofreq': autofreq, 'periodepsilon': periodepsilon, 'nbestpeaks': nbestpeaks, 'sigclip': sigclip, 'magsarefluxes': magsarefluxes } } sortedlspind = npargsort(finlsp)[::-1] sortedlspperiods = finperiods[sortedlspind] sortedlspvals = finlsp[sortedlspind] # now get the nbestpeaks nbestperiods, nbestlspvals, nbestinds, peakcount = ([ finperiods[bestperiodind] ], [finlsp[bestperiodind]], [bestperiodind], 1) prevperiod = sortedlspperiods[0] # find the best nbestpeaks in the lsp and their periods for period, lspval, ind in zip(sortedlspperiods, sortedlspvals, sortedlspind): if peakcount == nbestpeaks: break perioddiff = abs(period - prevperiod) bestperiodsdiff = [abs(period - x) for x in nbestperiods] # print('prevperiod = %s, thisperiod = %s, ' # 'perioddiff = %s, peakcount = %s' % # (prevperiod, period, perioddiff, peakcount)) # this ensures that this period is different from the last # period and from all the other existing best periods by # periodepsilon to make sure we jump to an entire different # peak in the periodogram if (perioddiff > (periodepsilon * prevperiod) and all(x > (periodepsilon * period) for x in bestperiodsdiff)): nbestperiods.append(period) nbestlspvals.append(lspval) nbestinds.append(ind) peakcount = peakcount + 1 prevperiod = period # generate the return dict resultdict = { 'bestperiod': finperiods[bestperiodind], 'bestlspval': finlsp[bestperiodind], 'nbestpeaks': nbestpeaks, 'nbestinds': nbestinds, 'nbestlspvals': nbestlspvals, 'nbestperiods': nbestperiods, 'lspvals': lsp, 'frequencies': frequencies, 'periods': periods, 'durations': durations, 'blsresult': blsresult, 'blsmodel': blsmodel, 'stepsize': stepsize, 'nfreq': nfreq, 'mintransitduration': mintransitduration, 'maxtransitduration': maxtransitduration, 'method': 'bls', 'kwargs': { 'startp': startp, 'endp': endp, 'stepsize': stepsize, 'mintransitduration': mintransitduration, 'maxtransitduration': maxtransitduration, 'ndurations': ndurations, 'blsobjective': blsobjective, 'blsmethod': blsmethod, 'blsoversample': blsoversample, 'blsntransits': blsmintransits, 'blsfreqfactor': blsfreqfactor, 'autofreq': autofreq, 'periodepsilon': periodepsilon, 'nbestpeaks': nbestpeaks, 'sigclip': sigclip, 'magsarefluxes': magsarefluxes } } return resultdict except Exception as e: LOGEXCEPTION('BLS failed!') if raiseonfail: raise return { 'bestperiod': npnan, 'bestlspval': npnan, 'nbestinds': None, 'nbestpeaks': nbestpeaks, 'nbestlspvals': None, 'nbestperiods': None, 'lspvals': None, 'periods': None, 'durations': None, 'blsresult': None, 'blsmodel': None, 'stepsize': stepsize, 'nfreq': nfreq, 'mintransitduration': mintransitduration, 'maxtransitduration': maxtransitduration, 'method': 'bls', 'kwargs': { 'startp': startp, 'endp': endp, 'stepsize': stepsize, 'mintransitduration': mintransitduration, 'maxtransitduration': maxtransitduration, 'ndurations': ndurations, 'blsobjective': blsobjective, 'blsmethod': blsmethod, 'blsoversample': blsoversample, 'blsntransits': blsmintransits, 'blsfreqfactor': blsfreqfactor, 'autofreq': autofreq, 'periodepsilon': periodepsilon, 'nbestpeaks': nbestpeaks, 'sigclip': sigclip, 'magsarefluxes': magsarefluxes } } else: LOGERROR('no good detections for these times and mags, skipping...') return { 'bestperiod': npnan, 'bestlspval': npnan, 'nbestinds': None, 'nbestpeaks': nbestpeaks, 'nbestlspvals': None, 'nbestperiods': None, 'lspvals': None, 'periods': None, 'durations': None, 'blsresult': None, 'blsmodel': None, 'stepsize': stepsize, 'nfreq': None, 'nphasebins': None, 'mintransitduration': mintransitduration, 'maxtransitduration': maxtransitduration, 'method': 'bls', 'kwargs': { 'startp': startp, 'endp': endp, 'stepsize': stepsize, 'mintransitduration': mintransitduration, 'maxtransitduration': maxtransitduration, 'ndurations': ndurations, 'blsobjective': blsobjective, 'blsmethod': blsmethod, 'blsoversample': blsoversample, 'blsntransits': blsmintransits, 'blsfreqfactor': blsfreqfactor, 'autofreq': autofreq, 'periodepsilon': periodepsilon, 'nbestpeaks': nbestpeaks, 'sigclip': sigclip, 'magsarefluxes': magsarefluxes } }
def _parallel_bls_worker(task): ''' This wraps Astropy's BoxLeastSquares for use with bls_parallel_pfind below. `task` is a tuple:: task[0] = times task[1] = mags task[2] = errs task[3] = magsarefluxes task[4] = minfreq task[5] = nfreq task[6] = stepsize task[7] = ndurations task[8] = mintransitduration task[9] = maxtransitduration task[10] = blsobjective task[11] = blsmethod task[12] = blsoversample ''' try: times, mags, errs = task[:3] magsarefluxes = task[3] minfreq, nfreq, stepsize = task[4:7] ndurations, mintransitduration, maxtransitduration = task[7:10] blsobjective, blsmethod, blsoversample = task[10:] frequencies = minfreq + nparange(nfreq) * stepsize periods = 1.0 / frequencies # astropy's BLS requires durations in units of time durations = nplinspace(mintransitduration * periods.min(), maxtransitduration * periods.min(), ndurations) # set up the correct units for the BLS model if magsarefluxes: blsmodel = BoxLeastSquares(times * u.day, mags * u.dimensionless_unscaled, dy=errs * u.dimensionless_unscaled) else: blsmodel = BoxLeastSquares(times * u.day, mags * u.mag, dy=errs * u.mag) blsresult = blsmodel.power(periods * u.day, durations * u.day, objective=blsobjective, method=blsmethod, oversample=blsoversample) return { 'blsresult': blsresult, 'blsmodel': blsmodel, 'durations': durations, 'power': nparray(blsresult.power) } except Exception as e: LOGEXCEPTION('BLS for frequency chunk: (%.6f, %.6f) failed.' % (frequencies[0], frequencies[-1])) return { 'blsresult': None, 'blsmodel': None, 'durations': durations, 'power': nparray([npnan for x in range(nfreq)]), }
def from_lightcurve(lc, **kwargs): """Creates a Periodogram from a LightCurve using the Box Least Squares (BLS) method.""" time_unit = (kwargs.pop("time_unit", "day")) if time_unit not in dir(u): raise ValueError('{} is not a valid unit for time.'.format(time_unit)) try: from astropy.stats import BoxLeastSquares except ImportError: raise Exception("BLS requires AstroPy v3.1 or later") # BoxLeastSquares will not work if flux or flux_err contain NaNs lc = lc.remove_nans() if np.isfinite(lc.flux_err).all(): dy = lc.flux_err else: dy = None bls = BoxLeastSquares(lc.time, lc.flux, dy) duration = kwargs.pop("duration", 0.25) minimum_period = kwargs.pop("minimum_period", None) maximum_period = kwargs.pop("maximum_period", None) period = kwargs.pop("period", None) if minimum_period is None: if 'period' in kwargs: minimum_period = period.min() else: minimum_period = np.max([np.median(np.diff(lc.time)) * 4, np.max(duration) + np.median(np.diff(lc.time))]) if maximum_period is None: if 'period' in kwargs: maximum_period = period.max() else: maximum_period = (np.max(lc.time) - np.min(lc.time)) / 3. frequency_factor = kwargs.pop("frequency_factor", 10) df = frequency_factor * np.min(duration) / (np.max(lc.time) - np.min(lc.time))**2 npoints = int(((1/minimum_period) - (1/maximum_period))/df) # Too many points if npoints > 1e5: log.warning('`period` contains {} points.' 'Periodogram is likely to be large, and slow to evaluate. ' 'Consider setting `frequency_factor` to a higher value.' ''.format(np.round(npoints, 4))) # Way too many points if npoints > 1e7: raise ValueError('`period` contains {} points.' 'Periodogram is too large to evaluate. ' 'Consider setting `frequency_factor` to a higher value.' ''.format(np.round(npoints, 4))) period = kwargs.pop("period", bls.autoperiod(duration, minimum_period=minimum_period, maximum_period=maximum_period, frequency_factor=frequency_factor)) result = bls.power(period, duration, **kwargs) if not isinstance(result.period, u.quantity.Quantity): result.period = u.Quantity(result.period, time_unit) if not isinstance(result.power, u.quantity.Quantity): result.power = result.power * u.dimensionless_unscaled return BoxLeastSquaresPeriodogram(frequency=1. / result.period, power=result.power, default_view='period', label=lc.label, targetid=lc.targetid, transit_time=result.transit_time, duration=result.duration, depth=result.depth, bls_result=result, snr=result.depth_snr, bls_obj=bls, time=lc.time, flux=lc.flux, time_unit=time_unit)
def find_and_mask_transits(time, flux, flux_err, periods, durations, nplanets=1): """ Iteratively find and mask transits in the flattened light curve. Args: time (array): The time array. flux (array): The flux array. You'll get the best results if this is flattened. flux_err (array): The array of flux uncertainties. periods (array): The array of periods to search over for BLS. For example, periods = np.linspace(0.5, 20, 10) durations (array): The array of durations to search over for BLS. For example, durations = np.linspace(0.05, 0.2, 10) nplanets (Optional[int]): The number of planets you'd like to search for. This function will interatively find and remove nplanets. Default is 1. Returns: transit_masks (list): a list of masks that correspond to the in transit points of each light curve. To mask out transits do time[~transit_masks[index]], etc. """ cum_transit = np.ones(len(time), dtype=bool) _time, _flux, _flux_err = time * 1, flux * 1, flux_err * 1 t0s, durs, porbs = [np.zeros(nplanets) for i in range(3)] transit_masks = [] for i in range(nplanets): bls = BoxLeastSquares(t=_time, y=_flux, dy=_flux_err) bls.power(periods, durations) print("periods") periods = bls.autoperiod(durations, minimum_n_transit=3, frequency_factor=5.0) print("results") results = bls.autopower(durations, frequency_factor=5.0) # Find the period of the peak print("find_period") period = results.period[np.argmax(results.power)] print("extract") # Extract the parameters of the best-fit model index = np.argmax(results.power) porbs[i] = results.period[index] t0s[i] = results.transit_time[index] durs[i] = results.duration[index] # # Plot the periodogram # fig, ax = plt.subplots(1, 1, figsize=(10, 5)) # ax.plot(results.period, results.power, "k", lw=0.5) # ax.set_xlim(results.period.min(), results.period.max()) # ax.set_xlabel("period [days]") # ax.set_ylabel("log likelihood") # # Highlight the harmonics of the peak period # ax.axvline(period, alpha=0.4, lw=4) # for n in range(2, 10): # ax.axvline(n*period, alpha=0.4, lw=1, linestyle="dashed") # ax.axvline(period / n, alpha=0.4, lw=1, linestyle="dashed") # plt.show() # plt.plot(_time, _flux, ".") # plt.xlim(1355, 1360) print("mask") in_transit = bls.transit_mask(_time, porbs[i], 2 * durs[i], t0s[i]) transit_masks.append(in_transit) _time, _flux, _flux_err = _time[~in_transit], _flux[~in_transit], \ _flux_err[~in_transit] return transit_masks, t0s, durs, porbs
flux = 10.0**(mag / -2.5) / np.mean(10.0**(mag / -2.5)) fluxerror = flux * (10.0**(magerror / 2.5) - 1.0) #detrending lc = lk.LightCurve(time, flux, fluxerror) detrended_lc = lc.flatten(window_length=window_size).bin( binsize=bin_size).remove_outliers(sigma=Nsig) #doing BLS search: bls = BoxLeastSquares(detrended_lc.time, detrended_lc.flux, detrended_lc.flux_err) periods = np.arange(0.23, (max(detrended_lc.time) - min(detrended_lc.time)), 0.1) durations = np.arange((1.0 / 24.0), (5.0 / 24.0), 0.1) #1 hr to 5 hrs periodogram = bls.power(periods, durations, objective='snr') #phase folding with best BLS model index = np.argmax(periodogram.power) print(index) best_period = periodogram.period[index] print(best_period) best_t0 = periodogram.transit_time[index] print(best_t0) detrendedphasefoldedlc = [ detrended_lc.fold(period=best_period, t0=best_t0).phase, detrended_lc.fold(period=best_period, t0=best_t0).flux, detrended_lc.fold(period=best_period, t0=best_t0).flux_err ] phasefoldedlc = [ lc.fold(period=best_period, t0=best_t0).phase,
def tess_lightcurves(filename, TIC): import numpy as np from astropy.io import fits import matplotlib.pyplot as plt tpf_url = str(filename) with fits.open(tpf_url) as hdus: tpf = hdus[1].data tpf_hdr = hdus[1].header tpf_mask = hdus[2].data time = tpf["TIME"] flux = tpf["FLUX"] m = np.any(np.isfinite(flux), axis=(1, 2)) & (tpf["QUALITY"] == 0) time = np.ascontiguousarray(time[m] - np.min(time[m]), dtype=np.float64) flux = np.ascontiguousarray(flux[m], dtype=np.float64) mean_img = np.median(flux, axis=0) plt.imshow(mean_img.T, cmap="gray_r") plt.title("TESS image of Pi Men") plt.xticks([]) plt.yticks([]) from scipy.signal import savgol_filter # Sort the pixels by median brightness order = np.argsort(mean_img.flatten())[::-1] # A function to estimate the windowed scatter in a lightcurve def estimate_scatter_with_mask(mask): f = np.sum(flux[:, mask], axis=-1) smooth = savgol_filter(f, 1001, polyorder=5) return 1e6 * np.sqrt(np.median((f / smooth - 1)**2)) # Loop over pixels ordered by brightness and add them one-by-one # to the aperture masks, scatters = [], [] for i in range(10, 100): msk = np.zeros_like(tpf_mask, dtype=bool) msk[np.unravel_index(order[:i], mean_img.shape)] = True scatter = estimate_scatter_with_mask(msk) masks.append(msk) scatters.append(scatter) # Choose the aperture that minimizes the scatter pix_mask = masks[np.argmin(scatters)] # Plot the selected aperture plt.imshow(mean_img.T, cmap="gray_r") plt.imshow(pix_mask.T, cmap="Reds", alpha=0.3) plt.title("TIC" + str(TIC) + ": selected aperture ") plt.xticks([]) plt.yticks([]) # plt.savefig('/Users/Admin/Documents/Research_Lisa/TESS/plots/'+str(TIC)+'_aperture.pdf') plt.close() plt.figure(figsize=(10, 5)) sap_flux = np.sum(flux[:, pix_mask], axis=-1) sap_flux = (sap_flux / np.median(sap_flux) - 1) * 1e3 plt.plot(time, sap_flux, "k") plt.xlabel("time [days]") plt.ylabel("relative flux [ppt]") plt.title("TIC" + str(TIC) + ": raw light curve") plt.xlim(time.min(), time.max()) # plt.savefig('/Users/Admin/Documents/Research_Lisa/TESS/plots/'+str(TIC)+'_raw_lc.pdf') plt.close() # Build the first order PLD basis X_pld = np.reshape(flux[:, pix_mask], (len(flux), -1)) X_pld = X_pld / np.sum(flux[:, pix_mask], axis=-1)[:, None] # Build the second order PLD basis and run PCA to reduce the number of dimensions X2_pld = np.reshape(X_pld[:, None, :] * X_pld[:, :, None], (len(flux), -1)) U, _, _ = np.linalg.svd(X2_pld, full_matrices=False) X2_pld = U[:, :X_pld.shape[1]] # Construct the design matrix and fit for the PLD model X_pld = np.concatenate((np.ones((len(flux), 1)), X_pld, X2_pld), axis=-1) XTX = np.dot(X_pld.T, X_pld) w_pld = np.linalg.solve(XTX, np.dot(X_pld.T, sap_flux)) pld_flux = np.dot(X_pld, w_pld) # Plot the de-trended light curve plt.figure(figsize=(10, 5)) plt.plot(time, sap_flux - pld_flux, "k") plt.xlabel("time [days]") plt.ylabel("de-trended flux [ppt]") plt.title("TIC" + str(TIC) + ": initial de-trended light curve") plt.xlim(time.min(), time.max()) # plt.savefig('/Users/Admin/Documents/Research_Lisa/TESS/plots/'+str(TIC)+'_detrend_lc.pdf') plt.close() from astropy.stats import BoxLeastSquares period_grid = np.exp(np.linspace(np.log(1), np.log(15), 50000)) bls = BoxLeastSquares(time, sap_flux - pld_flux) bls_power = bls.power(period_grid, 0.1, oversample=20) # Save the highest peak as the planet candidate index = np.argmax(bls_power.power) bls_period = bls_power.period[index] bls_t0 = bls_power.transit_time[index] bls_depth = bls_power.depth[index] transit_mask = bls.transit_mask(time, bls_period, 0.2, bls_t0) fig, axes = plt.subplots(2, 1, figsize=(10, 10)) # Plot the periodogram ax = axes[0] ax.axvline(np.log10(bls_period), color="C1", lw=5, alpha=0.8) ax.plot(np.log10(bls_power.period), bls_power.power, "k") ax.annotate("period = {0:.4f} d".format(bls_period), (0, 1), xycoords="axes fraction", xytext=(5, -5), textcoords="offset points", va="top", ha="left", fontsize=12) ax.set_ylabel("bls power") ax.set_yticks([]) ax.set_xlim(np.log10(period_grid.min()), np.log10(period_grid.max())) ax.set_xlabel("log10(period)") # Plot the folded transit ax = axes[1] x_fold = (time - bls_t0 + 0.5 * bls_period) % bls_period - 0.5 * bls_period m = np.abs(x_fold) < 0.4 ax.plot(x_fold[m], sap_flux[m] - pld_flux[m], ".k") # Overplot the phase binned light curve bins = np.linspace(-0.41, 0.41, 32) denom, _ = np.histogram(x_fold, bins) num, _ = np.histogram(x_fold, bins, weights=sap_flux - pld_flux) denom[num == 0] = 1.0 ax.plot(0.5 * (bins[1:] + bins[:-1]), num / denom, color="C1") ax.set_xlim(-0.3, 0.3) ax.set_ylabel("de-trended flux [ppt]") ax.set_xlabel("time since transit") plt.title("TIC" + str(TIC)) plt.savefig('/Users/Admin/Documents/Research_Lisa/TESS/plots/' + str(TIC) + '_period_detrend_lc.pdf') plt.close() m = ~transit_mask XTX = np.dot(X_pld[m].T, X_pld[m]) w_pld = np.linalg.solve(XTX, np.dot(X_pld[m].T, sap_flux[m])) pld_flux = np.dot(X_pld, w_pld) x = np.ascontiguousarray(time, dtype=np.float64) y = np.ascontiguousarray(sap_flux - pld_flux, dtype=np.float64) plt.figure(figsize=(10, 5)) plt.plot(time, y, "k") plt.xlabel("time [days]") plt.ylabel("de-trended flux [ppt]") plt.title("TIC" + str(TIC) + ": final de-trended light curve") plt.xlim(time.min(), time.max()) # plt.savefig('/Users/Admin/Documents/Research_Lisa/TESS/plots/'+str(TIC)+'_detrend_lc_final.pdf') plt.close() plt.figure(figsize=(10, 5)) x_fold = (x - bls_t0 + 0.5 * bls_period) % bls_period - 0.5 * bls_period m = np.abs(x_fold) < 0.3 plt.plot(x_fold[m], pld_flux[m], ".k", ms=4) bins = np.linspace(-0.5, 0.5, 60) denom, _ = np.histogram(x_fold, bins) num, _ = np.histogram(x_fold, bins, weights=pld_flux) denom[num == 0] = 1.0 plt.plot(0.5 * (bins[1:] + bins[:-1]), num / denom, color="C1", lw=2) plt.xlim(-0.2, 0.2) plt.title("TIC" + str(TIC)) plt.xlabel("time since transit") plt.ylabel("PLD model flux") # plt.savefig('/Users/Admin/Documents/Research_Lisa/TESS/plots/'+str(TIC)+'_PLD_model.pdf') plt.close()