def run_my_fit(i): ruth2 = "/export/bbq2/angusr/GProtation/" lc_path = "/home/angusr/.kplr/data/lightcurves/" RESULTS_DIR = "/export/bbq2/angusr/GProtation/gprotation/kepler" # McQuillan stars s = pd.read_csv(os.path.join(ruth2, "code/data/Table_1_Periodic.txt")) kid = s.KIC.values[i] print(int(kid), i, "of", len(s.KIC.values)) star = client.star(int(kid)) lcs = star.get_light_curves(fetch=True, short_cadence=False, clobber=False) LC_DIR = os.path.join(lc_path, "{}".format(str(int(kid)).zfill(9))) x, y, yerr = kd.load_kepler_data(LC_DIR) emcee2 = False if emcee2: e2.gp_fit(x, y, yerr, "{}2".format(id), RESULTS_DIR) return fit = gp.fit(x, y, yerr, kid, RESULTS_DIR) fit.gp_fit(burnin=1000, nwalkers=16, nruns=5, full_run=1000, nsets=2, p_max=np.log(100))
def measure_prot(ids, DATA_DIR): """ Measure the rotation periods of a set of light curves. :param plot: (optional) boolean: create plot of the acf if True. """ # remove previous results file if os.path.exists("periods.txt"): os.remove("periods.txt") # perform acf on a set light curves acf_results = np.zeros((len(ids), 5)) # id, p, height, rvar, localph for i, id in enumerate(ids): print(i, "of", len(ids)) str_id = str(int(id)).zfill(9) fnames = [] for i in range(18): fnames.append(glob.glob(os.path.join(DATA_DIR, "DR25_Q{0}/kplr*{1}*llc.fits".format(i, str_id)))) x, y, yerr = load_kepler_data(fnames) period, acf, lags, rvar, height, localph, lppos, rppos = \ simple_acf(x, y) acf_results[i, :] = np.array([id, period, height, rvar, localph]) if localph < .1: # remove stars with low localph period = 0 # append results to file with open("kplr_periods.txt", "a") as f: f.write("{0} {1} \n".format(id, period))
def __init__(self, kepid=None, x=None, y=None, yerr=None, LC_DIR="/Users/ruthangus/.kplr/data/lightcurves"): """ params: ------ kepid: (int) The KIC id. x: (array) The time array. y: (array) The flux array. yerr: (array) The flux uncertainty array. """ self.kepid = kepid # If x, y and yerr are not provided, load them. if not np.array([x, y, yerr]).any(): lc_path = os.path.join(LC_DIR, str(kepid).zfill(9)) # If you don't have the light curves, download them. if not os.path.exists(lc_path): print("Downloading light curve...") star = client.star(kepid) star.get_light_curves(fetch=True, short_cadence=False) print("Loading light curve...") self.x, self.y, self.yerr = kd.load_kepler_data(lc_path) else: self.x, self.y, self.yerr = x, y, yerr
def get_lc(id, KPLR_DIR="/Users/ruthangus/.kplr/data/lightcurves"): """ Downloads the kplr light curve and loads x, y and yerr. """ kid = str(int(id)).zfill(9) path = os.path.join(KPLR_DIR, "{}".format(kid)) if not os.path.exists(path): client = kplr.API() star = client.star(kid) print("Downloading LC...") star.get_light_curves(fetch=True, short_cadence=False) x, y, yerr = kd.load_kepler_data(os.path.join(KPLR_DIR, "{}".format(kid))) else: x, y, yerr = kd.load_kepler_data(os.path.join(KPLR_DIR, "{}".format(kid))) x -= x[0] return x, y, yerr
def measure_LS_period(): data = pd.read_csv("../data/targets-small-sep.csv") for i, kepid in enumerate(data.kepid.values[10:50]): lc_dir = "/Users/ruthangus/.kplr/data/lightcurves/{}"\ .format(str(kepid).zfill(9)) x, y, yerr = kd.load_kepler_data(lc_dir) prot = ro.prot(kepid, x, y, yerr) prot.pgram_ps(plot=True)
def get_lc(id, KPLR_DIR): """ Downloads the kplr light curve and loads x, y and yerr. """ kid = str(int(id)).zfill(9) client = kplr.API() star = client.star(kid) star.get_light_curves(fetch=True, short_cadence=False) x, y, yerr = kd.load_kepler_data(os.path.join(KPLR_DIR, "{}".format(kid))) return x, y, yerr
def plot(kepid): path = "/Users/ruthangus/.kplr/data/lightcurves/" LC_DIR = os.path.join(path, "{}".format(str(int(kepid)).zfill(9))) print(LC_DIR) x, y, yerr = load_kepler_data(LC_DIR) plt.clf() plt.plot(x - x[0], y, "k.") plt.xlabel("$\mathrm{Time~(Days)}$") plt.ylabel("$\mathrm{Normalised~flux}$") plt.xlim(0, 20) plt.subplots_adjust(left=.18, bottom=.15) plt.savefig("{}_lc".format(kepid))
def pgram_prots(kepid_list, LC_DIR="/Users/ruthangus/.kplr/data/lightcurves", plot=False): pgram_period, err = [np.zeros(len(kepid_list)) for i in range(2)] for i, kepid in enumerate(kepid_list): print(kepid, i, "of", len(kepid_list)) try: x, y, yerr = load_kepler_data( os.path.join(LC_DIR, "{}".format(str(kepid).zfill(9)))) except IndexError: star = client.star(kepid) star.get_light_curves(fetch=True, short_cadence=False) pgram_period[i], err[i] = pgram_ps(x, y, yerr, kepid, plot=plot) return pgram_period, err
def get_period_from_kepid(kepid, LC_DIR="/Users/ruthangus/.kplr/data/lightcurves"): fname = "results/{}_pgram.csv".format(kepid) if os.path.exists(fname): print(kepid, "periodogram found") # Load the light curve x, y, yerr = load_kepler_data( os.path.join(LC_DIR, "{}".format(str(kepid).zfill(9)))) # Load the pgram & calculate period & err pgram_df = pd.read_csv(fname) pgram_period, pgram_period_err = \ get_period_from_pgram(pgram_df.periods.values, pgram_df.power.values, x, y, yerr) return pgram_period, pgram_period_err else: return 0, 0
def plot_lc(koi): """ Make demo plot of a light curve. """ # Load the data print(LC_DIR) x, y, yerr = kd.load_kepler_data(LC_DIR) x -= x[0] m = x < 500 x, y, yerr = x[m], y[m], yerr[m] # Load the posterior samples. df = pd.read_hdf(os.path.join(DATA_DIR, "KOI-{}.h5".format(int(koi))), key="samples") a = np.exp(MAP(df.ln_A.values)) l = np.exp(MAP(df.ln_l.values)) g = np.exp(MAP(df.ln_G.values)) s = np.exp(MAP(df.ln_sigma.values)) p = np.exp(MAP(df.ln_period.values)) print("ln(a) = ", np.log(a), "ln(l) = ", np.log(l), "ln(G) = ", np.log(g), "ln(s) = ", np.log(s), "ln(p) = ", np.log(p), "p = ", p) xs = np.linspace(min(x), max(x), 500) k = a * ExpSquaredKernel(l) \ * ExpSine2Kernel(g, p) + WhiteKernel(s) gp = george.GP(k) gp.compute(x, yerr) mu, cov = gp.predict(y, xs) plt.clf() plt.plot(x, y, "k.") plt.plot(xs, mu, color="CornFlowerBlue") plt.xlabel("$\mathrm{Time~(days)}$") plt.ylabel("$\mathrm{Normalised~flux}$") plt.subplots_adjust(left=.18) plt.savefig(os.path.join(FIG_DIR, "koi_lc_demo.pdf"))
plt.ylabel("$ACF$") plt.savefig("{}_acf".format(id)) if __name__ == "__main__": # Load the data # id = 6269070 # p_init = 20/6. id = 2437965 p_init = 1. LC_DIR = "/Users/ruthangus/.kplr/data/lightcurves/{}"\ .format(str(id).zfill(9)) x, y, yerr = load_kepler_data(LC_DIR) c = 20000 # Cut off after 1000 data points. x, y, yerr = x[:c], y[:c], yerr[:c] inds = np.argsort(x) x, y, yerr = x[inds], y[inds], yerr[inds] # Define the kernel kernel = CustomTerm(log_a=np.log(np.var(y)), log_b=-5, log_tau=5, log_P=np.log(p_init)) print(kernel.get_parameter_vector()) # Generate the covariance matrix. gp = celerite.GP(kernel, mean=np.mean(y)) gp.compute(x, yerr) # You always need to call compute once. # # Plot a draw from the prior.
def make_singles_and_doubles(ids, train_or_test, ndays, lc_dir, fdouble=.5, path=".", saveplot=False): """ params: ------- ids: list or array The KIC ids to create light curves from. train_or_test: str The directory in which to save the resulting light curves. """ ntrain = len(ids) ndouble = int((ntrain / 3) * 2) double_ids = ids[:ndouble] single_ids = ids[ndouble:] print(train_or_test, "set: ", ndouble / 2, "doubles, made with {} light" "curves and ".format(ndouble), len(single_ids), "singles \n") for j, i in enumerate(np.arange(0, len(double_ids), 2)): id0, id1, id2 = str(double_ids[i]).zfill(9), \ str(double_ids[i + 1]).zfill(9), str(single_ids[j]).zfill(9) print("double 1 id = ", id0, "double 2 id = ", id1, "single id = ", id2) # Download the light curves to add together fname = "{0}/{1}".format(lc_dir, id0) if not os.path.exists(fname): download_light_curve(double_ids[i]) fname = "{0}/{1}".format(lc_dir, id1) if not os.path.exists(fname): download_light_curve(double_ids[i + 1]) # Download the single star light curve. fname = "{0}/{1}".format(lc_dir, id2) if not os.path.exists(fname): download_light_curve(single_ids[j]) # Load the light curves into memory p0 = "{0}/{1}".format(lc_dir, id0) x0, y0, yerr0, cadence0 = load_kepler_data(p0) p1 = "{0}/{1}".format(lc_dir, id1) x1, y1, yerr1, cadence1 = load_kepler_data(p1) p2 = "{0}/{1}".format(lc_dir, id2) x2, y2, yerr2, cadence2 = load_kepler_data(p2) # Add the light curves together according to their cadences. x, y0, yerrdouble, x1, y1, yerr1, ydouble, \ xsingle, ysingle, yerrsingle, cadence = \ add_lcs_together(x0, y0, yerr0, cadence0, x1, y1, yerr1, cadence1, x2, y2, yerr2, cadence2) # Choose a random segment of the light curve that is ndays long and # make sure it's not empty! tmin, tmax = 0, max(x) - ndays for k in range(100): t = np.random.uniform(tmin, tmax) m = (t < x) * (x < t + ndays) if len(x[m]): break x, y0, yerrdouble, x1, y1, yerr1, ydouble, xsingle, ysingle, \ yerrsingle, cadence = x[m], y0[m], yerrdouble[m], x1[m], y1[m], \ yerr1[m], ydouble[m], xsingle[m], ysingle[m], yerrsingle[m], \ cadence[m] if saveplot: plt.figure(figsize=(16, 9)) plt.plot(x, y0, ".", label="star 1") plt.plot(x1, y1 + .01, ".", label="star 2") plt.plot(x, ydouble + .02, ".", label="double star") plt.plot(xsingle, ysingle + .03, "k.", label="single star") plt.legend() figname = "{0}/{1}/{2}_{3}_{4}_plot".format( path, train_or_test, id0, id1, id2) print("saving figure as ", figname) plt.savefig(figname) # Save the light curves. double_lc = pd.DataFrame( dict({ "time": x, "flux": ydouble, "flux_err": yerrdouble, "cadence": cadence })) fname = "{0}/{1}/{2}_{3}_lc.csv".format(path, train_or_test, id0, id1) print("saving double lc to ", fname) double_lc.to_csv(fname) single_lc = pd.DataFrame( dict({ "time": x, "flux": ysingle, "flux_err": yerrsingle, "cadence": cadence })) fname = "{0}/{1}/{2}_lc.csv".format(path, train_or_test, id2) print("saving single lc to ", fname, "\n") single_lc.to_csv(fname)
client = kplr.API() import kepler_data as kd # Download a light curve. kepid = int(sys.argv[1]) # 205117205 if kepid >= 200000000: sections, t, flux = get_light_curve(2, kepid) else: # include sections in load_kepler_data, one section for each quarter. kepstar = client.star(kepid) kepstar.get_light_curves(fetch=True, short_cadence=False) LC_DIR = "/Users/ruthangus/.kplr/data/lightcurves/{}".format( str(kepid).zfill(9)) sections, t, flux, flux_err = kd.load_kepler_data(LC_DIR) m = 2000 sections, t, flux = sections[:m], t[:m], flux[:m] flux *= 1e4 flux0 = np.array(flux) - np.mean(flux) # Mean subtract # Fit and remove straight line trend for the periodogram. A = np.vander(t - np.mean(flux), 4) w = np.linalg.solve(np.dot(A.T, A), np.dot(A.T, flux)) trend = np.dot(A, w) # First guess at the period min_period, max_period = 0.1, 30.0 # Compute periodogram.