#Detrending Flux and Levenberg-Marquardt Fitting: #if SDE>6 ########################## #start values are correct values if True: #SDE >= 6: #polynomial detrending #fluxDetrend = polynomial(flux, order=5) #untrendy #fluxDetrend, ferr = untrendy.median(time, flux) #fluxDetrend = [x-1.0 for x in fluxDetrend] fitT = FitTransit() fitT.add_guess_star(rho=1.5) fitT.add_guess_planet(period=results[1], impact=0.0, T0=3.0, rprs=.2) #need a guess rprs fitT.add_data(time=time, flux=fluxDetrend) vary_star = [ 'rho' ] # not sure how to avoid free stellar parameters? ideally would not vary star at all vary_planet = (['period', 'rprs', 'impact', 'T0']) fitT.free_parameters(vary_star, vary_planet) fitT.do_fit()
def main(kic): client = kplr.API() star = client.star(kic) lcs = star.get_light_curves(short_cadence=False) time, flux, ferr, quality,quarter = [], [], [], [], [] for lc in lcs: with lc.open() as f: # The lightcurve data are in the first FITS HDU. hdu_data = f[1].data time = np.r_[time,hdu_data["time"]] #fluxval = hdu_data["sap_flux"] #fluxmed = np.median(fluxval) #flux = np.r_[flux,fluxval / fluxmed] #ferr = np.r_[ferr,hdu_data["sap_flux_err"] / fluxmed] flux = np.r_[flux,hdu_data["sap_flux"]] ferr = np.r_[ferr,hdu_data["sap_flux_err"]] quality = np.r_[quality,hdu_data["sap_quality"]] quarter = np.r_[quarter,f[0].header["QUARTER"] + np.zeros(len(hdu_data["time"]))] flux, ferr = byebyebaddata(flux,ferr,quality) time, flux, ferr, quarter, quality = i_hate_nans(time, flux, ferr, quarter, quality) flux, ferr = norm_by_quarter(flux, ferr,quarter) cflux = (flux / med_filt(time,flux,dt=1.0)) - 1.0 fitT = FitTransit() fitT.add_guess_star(rho=2.45) k0 = star.kois[0] fitT.add_guess_planet( period=k0.koi_period, impact=k0.koi_impact, T0=k0.koi_time0bk, rprs=k0.koi_ror) k1 = star.kois[1] fitT.add_guess_planet( period=k1.koi_period, impact=k1.koi_impact, T0=k1.koi_time0bk, rprs=k1.koi_ror) k2 = star.kois[2] fitT.add_guess_planet( period=k2.koi_period, impact=k2.koi_impact, T0=k2.koi_time0bk, rprs=k2.koi_ror) fitT.add_data(time=time,flux=cflux,ferr=ferr) freeparstar = ['rho','zpt'] freeparplanet = [ 'period','T0','impact','rprs'] fitT.free_parameters(freeparstar,freeparplanet) fitT.do_fit() return (time,cflux,ferr),fitT
plt.title("EPIC " + str(targetname) + " folded and binned LC") #pylab.show() ################################################################################### high = results[3] * results[4] low = high - results[3] fit = np.zeros(nbins) + high # H fit[results[5]:results[6] + 1] = low # L depth = high - low print('Guess rprs: ' + str(p(depth))) fitT = FitTransit() fitT.add_guess_star(rho=1.5) fitT.add_guess_planet(period=results[1], impact=0.0, T0=2425, rprs=p(depth)) fitT.add_data(time=time, flux=mergedfluxDetrend) vary_star = [] # free stellar parameters vary_planet = ([ 'period', # free planetary parameters 'rprs' ]) # free planet parameters are the same for every planet you model fitT.free_parameters(vary_star, vary_planet) fitT.do_fit() # run the fitting
rverr = rvdat[..., 2] return rvtime, rvval, rverr def get_lc(): time, flux, ferr = np.genfromtxt('../data/lc.dat', unpack=True) return time, flux, ferr if __name__ == '__main__': import ktransit from ktransit import FitTransit import matplotlib.pyplot as plt import numpy fitT = FitTransit() fitT.add_guess_star( rho=43.47, ld1=0.43, ld2=0.27, ld3=0.0, ld4=0.0, dil=0.0, zpt=-56.8E-6, veloffset= 15.6 # new keyword, the radial velocity zero-point offset in m/s ) fitT.add_guess_planet(T0=136.3837, period=6.246696, impact=0.865, rprs=0.019429,
def main(kic): client = kplr.API() star = client.star(kic) lcs = star.get_light_curves(short_cadence=False) time, flux, ferr, quality, quarter = [], [], [], [], [] for lc in lcs: with lc.open() as f: # The lightcurve data are in the first FITS HDU. hdu_data = f[1].data time = np.r_[time, hdu_data["time"]] #fluxval = hdu_data["sap_flux"] #fluxmed = np.median(fluxval) #flux = np.r_[flux,fluxval / fluxmed] #ferr = np.r_[ferr,hdu_data["sap_flux_err"] / fluxmed] flux = np.r_[flux, hdu_data["sap_flux"]] ferr = np.r_[ferr, hdu_data["sap_flux_err"]] quality = np.r_[quality, hdu_data["sap_quality"]] quarter = np.r_[quarter, f[0].header["QUARTER"] + np.zeros(len(hdu_data["time"]))] flux, ferr = byebyebaddata(flux, ferr, quality) time, flux, ferr, quarter, quality = i_hate_nans(time, flux, ferr, quarter, quality) flux, ferr = norm_by_quarter(flux, ferr, quarter) cflux = (flux / med_filt(time, flux, dt=1.0)) - 1.0 fitT = FitTransit() fitT.add_guess_star(rho=2.45) k0 = star.kois[0] fitT.add_guess_planet(period=k0.koi_period, impact=k0.koi_impact, T0=k0.koi_time0bk, rprs=k0.koi_ror) k1 = star.kois[1] fitT.add_guess_planet(period=k1.koi_period, impact=k1.koi_impact, T0=k1.koi_time0bk, rprs=k1.koi_ror) k2 = star.kois[2] fitT.add_guess_planet(period=k2.koi_period, impact=k2.koi_impact, T0=k2.koi_time0bk, rprs=k2.koi_ror) fitT.add_data(time=time, flux=cflux, ferr=ferr) freeparstar = ['rho', 'zpt'] freeparplanet = ['period', 'T0', 'impact', 'rprs'] fitT.free_parameters(freeparstar, freeparplanet) fitT.do_fit() return (time, cflux, ferr), fitT
def build_ktransit_model(ticid, lc, rprs=0.02, vary_transit=True): from ktransit import FitTransit fitT = FitTransit() model = BoxLeastSquares(lc.time, lc.flux) results = model.autopower(0.16, minimum_period=2., maximum_period=21.) period = results.period[np.argmax(results.power)] t0 = results.transit_time[np.argmax(results.power)] if rprs is None: depth = results.depth[np.argmax(results.power)] rprs = depth ** 2 fitT.add_guess_star(rho=0.022, zpt=0, ld1=0.6505,ld2=0.1041) #come up with better way to estimate this using AS fitT.add_guess_planet(T0=t0, period=period, impact=0.5, rprs=rprs) ferr = np.ones_like(lc.time) * 0.00001 fitT.add_data(time=lc.time,flux=lc.flux,ferr=ferr)#*1e-3) vary_star = ['zpt'] # free stellar parameters if vary_transit: vary_planet = (['period', 'impact', # free planetary parameters 'T0', #'esinw', 'ecosw', 'rprs']) #'impact', # free planet parameters are the same for every planet you model else: vary_planet = (['rprs']) fitT.free_parameters(vary_star, vary_planet) fitT.do_fit() # run the fitting return fitT
t = newtimes y = newfluxes print("finish lengths:", len(t), len(y)) #median filter and remove outliers y -= scipy.ndimage.filters.median_filter(y, size=150) #50 datapts ~ 1 day #2287: remove edge effect during midcampaign break edgemask = (t > 6.5) * (t < 22.5) t = t[-edgemask] y = y[-edgemask] from ktransit import FitTransit import ktransit fitT = FitTransit() #EPIC2287 fitT.add_guess_star(rho=0.0264, zpt=0, ld1=0.6505, ld2=0.1041) fitT.add_guess_planet(T0=7.5, period=9.17, impact=0.8, rprs=0.03, ecosw=0.0, esinw=0.0) #simult, sff fitT.add_data(time=t, flux=np.array(y)) vary_star = ['zpt'] #, 'ld1', 'ld2'] # free stellar parameters vary_planet = ([ 'period', # free planetary parameters
def fit_data(data, star=[1.4,0,0,0,0,0,1], planet=[365,0.0,0.0,.009155, 0.0, 0.0, 0.0], star_vary=[''], planet_vary=[''], auto=True, fold=False): """solves problems in assignment 2 star = stellar density, limb darkening parameters (4), dilution, zpt planet = T0, period, impact, rprs, ecosw, esinw, occ """ # Estimating basic input parameters, unless we give them in the call if auto: box = 5; sm_flux = msmooth(data[1],box) star[-1] = np.median(sm_flux) # Establishing photometric zero-point if fold: star[-1] += np.std(data[1]) ind = np.where(sm_flux < star[-1]-np.std(data[1]))[0] ind = ind[np.where(ind > box)] ind = ind[0] planet[0] = data[0][ind] # Estimating something close to a transit midpoint if fold: if star[0] > 15: planet[0] = 0.8 planet[3] = (data[1].max()-data[1].min())**0.5 # Estimating the depth # Performing the actual fit fitT = FitTransit() fitT.add_guess_star(rho=star[0], ld1=star[1], ld2=star[2],ld3=star[3],ld4=star[4], dil=star[5], zpt=star[6]) fitT.add_guess_planet(T0=planet[0], period=planet[1],impact=planet[2], rprs=planet[3],ecosw=planet[4],esinw=planet[5], occ=planet[6]) fitT.add_data(time=data[0],flux=data[1],ferr=data[2]) fitT.free_parameters(star_vary, planet_vary) fitT.do_fit() return fitT.__dict__['fitresultstellar'], \ fitT.__dict__['fitresultplanets'], \ fitT.transitmodel, star, planet
def transit_fit(t1, cfflux): addtime = 4833 # convert from trappist time to kepler time time = t1[(cfflux < 0.005) * (cfflux > -0.015)] + addtime # you need a time and a flux flux = cfflux[(cfflux < 0.005) * (cfflux > -0.015)] # there are no transits here :( ferr = np.ones_like(time) * 0.001 # uncertainty on the data fitT = FitTransit() fitT.add_guess_star(rho=50.0, ld1=0.43, ld2=0.14) # fixed because I'm sleepy for planet in [planetb, planetc, planetd, planete, planetf, planetg]: fitT.add_guess_planet(period=planet['period_days'][0], impact=planet['impact'][0], T0=planet['t0'][0], rprs=(planet['td_percent'][0] / 100)**0.5) fitT.add_data(time=time, flux=flux, ferr=ferr, itime=np.ones_like(time) * 0.0188) vary_star = ['rho', 'zpt'] # free stellar parameters vary_planet = ([ 'period', # free planetary parameters 'T0', 'impact', 'rprs' ]) # free planet parameters are the same for every planet you model fitT.free_parameters(vary_star, vary_planet) fitT.do_fit() # run the fitting return time, flux, fitT
def fitModel(time, flux, guessDict, freeParPlanet, ferr = 0): if not np.all(ferr): ferr = np.ones_like(flux)*1.E-5 freeParStar = ['rho'] # Make the fitting object according to guess dictionary fitT = FitTransit() fitT.add_guess_star(ld1 = 0, ld2 = 0) fitT.add_guess_planet(period = guessDict['period'], T0 = guessDict['T0']) fitT.add_data(time = time, flux = flux, ferr = ferr) fitT.free_parameters(freeParStar, freeParPlanet) fitT.do_fit() return fitT
show() ########################## #Detrending Flux and Levenberg-Marquardt Fitting: #if SDE>6 ########################## #start values are correct values if True: #if SDE >= 6 print('rprs guess ' + str(p(depth))) rprsguess = p(depth) fitT = FitTransit() fitT.add_guess_star(rho=rho) fitT.add_guess_planet(period=results[1], impact=impact, T0=T0, rprs=rprsguess) fitT.add_data(time=time, flux=mergedfluxDetrend) vary_star = ['rho'] # free stellar parameters vary_planet = ([ 'period', # free planetary parameters 'rprs' ]) # free planet parameters are the same for every planet you model fitT.free_parameters(vary_star, vary_planet) fitT.do_fit() # run the fitting
def fitt_bootstrap(time, flux, ferr, resid, fitp, fits): T0, period, impact = fitp['pnum0']['T0'], fitp['pnum0']['period'], fitp[ 'pnum0']['impact'] rprs, ecosw, esinw = fitp['pnum0']['rprs'], fitp['pnum0']['ecosw'], fitp[ 'pnum0']['esinw'] zpt, rho, ld1, ld2 = fits['zpt'], fits['rho'], fits['ld1'], fits['ld2'] fitT = FitTransit() fitT.add_guess_star(rho=rho, zpt=zpt, ld1=ld1, ld2=ld2) fitT.add_guess_planet(period=period, impact=impact, T0=T0, rprs=rprs, ecosw=ecosw, esinw=esinw) flux_resid = flux + choice(resid, size=len(flux)) fitT.add_data(time=time, flux=flux_resid, ferr=ferr) vary_star = ['rho', 'zpt', 'ld1', 'ld2'] # free stellar parameters vary_planet = ([ 'period', # free planetary parameters 'T0', 'impact', 'rprs', 'ecosw', 'esinw' ]) fitT.free_parameters(vary_star, vary_planet) fitT.do_fit() # run the fitting return fitT.fitresult
'ecosw', 'esinw' ]) fitT.free_parameters(vary_star, vary_planet) fitT.do_fit() # run the fitting return fitT.fitresult fn = '/Users/tom/gitcode/GP_model_Kepler_data/data/koi2133_np1_priorTrue_dil0.0GP.hdf5' f = h5py.File(fn) time = f['time'][:61000] flux = f['flux'][:61000] ferr = f['err'][:61000] fitT = FitTransit() fitT.add_guess_star(rho=43.47 / 1000., zpt=-5.68308831e-05, ld1=4.32157219e-01, ld2=2.69514828e-01) fitT.add_guess_planet(period=6.2467332, impact=0.09, T0=1.36383739e+02, rprs=0.019429, ecosw=0.0, esinw=0.0) fitT.add_data(time=time[:61000], flux=flux[:61000], ferr=ferr[:61000]) vary_star = ['rho', 'zpt', 'ld1', 'ld2'] # free stellar parameters vary_planet = ([ 'period', # free planetary parameters 'T0',
rvval = rvdat[...,1] - np.median(rvdat[...,1]) rverr = rvdat[...,2] return rvtime,rvval,rverr def get_lc(): time,flux,ferr = np.genfromtxt( '../data/lc.dat',unpack=True) return time,flux,ferr if __name__ == '__main__': import ktransit from ktransit import FitTransit import matplotlib.pyplot as plt import numpy fitT = FitTransit() fitT.add_guess_star( rho=43.47, ld1=0.43, ld2=0.27, ld3=0.0, ld4=0.0, dil=0.0, zpt=-56.8E-6, veloffset=15.6 # new keyword, the radial velocity zero-point offset in m/s ) fitT.add_guess_planet( T0=136.3837, period=6.246696, impact=0.865, rprs= 0.019429,
M.add_planet() # you can add as many planets as you like (up to 10) M.add_data( time=np.arange(0, 10, 0.0188), # timestamps to evaluate the model on itime=np.zeros_like(np.arange(0, 10, 0.0188)) + 0.0188) # integration time of each timestamp tmod = M.transitmodel # the out of transit data will be 0.0 unless you specify zpt plt.plot(M.time, tmod) #################### from ktransit import FitTransit # uncertainty on the data fitT = FitTransit() fitT.add_guess_star(rho=7.0) fitT.add_guess_planet(period=365.25, impact=0.0, T0=0.0, rprs=0.009155) fitT.add_data(time=M.time, flux=tmod) vary_star = ['rho', 'zpt'] # free stellar parameters vary_planet = ([ 'period', # free planetary parameters 'T0', 'impact', 'rprs' ]) # free planet parameters are the same for every planet you model fitT.free_parameters(vary_star, vary_planet) fitT.do_fit() # run the fitting