def calc_p_init(x, y, yerr, id, RESULTS_DIR, which_period="acf"): print("Calculating ACF") acf_period, acf, lags = simple_acf(x, y) err = acf_period * .1 print("acf period, err = ", acf_period, err) print("Calculating periodogram") ps = np.arange(.1, 100, .1) model = LombScargle().fit(x, y, yerr) pgram = model.periodogram(ps) plt.clf() plt.plot(ps, pgram) plt.savefig(os.path.join(RESULTS_DIR, "{0}_pgram".format(id))) print("saving figure ", os.path.join(RESULTS_DIR, "{0}_pgram".format(id))) peaks = np.array([ i for i in range(1, len(ps) - 1) if pgram[i - 1] < pgram[i] and pgram[i + 1] < pgram[i] ]) pgram_period = ps[pgram == max(pgram[peaks])][0] print("pgram period = ", pgram_period, "days") if which_period == "acf": p_init, perr = acf_period, err elif which_period == "pgram": p_init, perr = pgram_period, pgram_period * .1 else: print("which_period must equal 'acf' or 'pgram'") return p_init, perr
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 get_period_samples(id, x, y, nsamps=1000, ndays=200, pfunc=.1, plot=False, RESULTS_DIR="results"): """ Measure period with simple_acf using first ndays of data to reduce time. Generate samples from a Gaussian distribution centered on the period with a made up stdev. param x: (array) The time array. param y: (array) The flux array. param nsamps: (int) The number of samples. param ndays: (int) The number of days to use for the ACF period measurement. param pfunc: (float) The fractional uncertainty on the period to be used as the Gaussian stdev during the sampling. Returns rotation period (days) and period samples. """ print("Measuring period...") m = x < ndays period, acf, lags, rvar = sa.simple_acf(x[m], y[m]) if plot: plt.clf() plt.subplot(2, 1, 1) plt.plot(x[m], y[m], "k.") plt.subplot(2, 1, 2) plt.plot(lags, acf) plt.xlabel("Lags (days)") plt.ylabel("ACF") plt.savefig(os.path.join(RESULTS_DIR, "{}_acf".format(id))) period_samps = period*pfunc*np.random.randn(nsamps) + period return period, period_samps
def calc_p_init(self, clobber=False): """ Calculate the ACF and periodogram periods for initialisation. """ fname = os.path.join(self.RESULTS_DIR, "{0}_acf_pgram_results.txt".format(self.kid)) if not clobber and os.path.exists(fname): print("Previous ACF pgram result found") df = pd.read_csv(fname) m = df.N.values == self.kid acf_period = df.acf_period.values[m] err = df.acf_period_err.values[m] pgram_period = df.pgram_period.values[m] pgram_period_err = df.pgram_period_err.values[m] else: print("Calculating ACF") acf_period, acf, lags, rvar = sa.simple_acf(self.x, self.y) err = .1 * acf_period plt.clf() plt.plot(lags, acf) plt.axvline(acf_period, color="r") plt.xlabel("Lags (days)") plt.ylabel("ACF") plt.savefig( os.path.join(self.RESULTS_DIR, "{0}_acf".format(self.kid))) print("saving figure ", os.path.join(self.RESULTS_DIR, "{0}_acf".format(self.kid))) print("Calculating periodogram") ps = np.arange(.1, 100, .1) model = LombScargle().fit(self.x, self.y, self.yerr) pgram = model.periodogram(ps) plt.clf() plt.plot(ps, pgram) plt.savefig( os.path.join(self.RESULTS_DIR, "{0}_pgram".format(self.kid))) print("saving figure ", os.path.join(self.RESULTS_DIR, "{0}_pgram".format(self.kid))) peaks = np.array([ i for i in range(1, len(ps) - 1) if pgram[i - 1] < pgram[i] and pgram[i + 1] < pgram[i] ]) pgram_period = ps[pgram == max(pgram[peaks])][0] print("pgram period = ", pgram_period, "days") pgram_period_err = pgram_period * .1 df = pd.DataFrame({ "N": [self.kid], "acf_period": [acf_period], "acf_period_err": [err], "pgram_period": [pgram_period], "pgram_period_err": [pgram_period_err] }) df.to_csv(fname) return acf_period, err, pgram_period, pgram_period_err
def run_acf(c, epic, clobber=False, plot=True): """ Run the ACF on a light curve in the specified campaign. FOR PARALLEL RUNS. c (str): campaign, e.g. "c01". fn (str): fits file name for a target in campaign c. """ #period, acf_smooth, lags, rvar, peaks, dips, leftdips, rightdips, \ #bigpeaks = simple_acf(x, y) v = "2.0" filen = "hlsp_everest_k2_llc_{0}-c{1}_kepler_v{2}_lc.fits"\ .format(epic, c.zfill(2), v) file = "data/c{0}/{1}".format(c.zfill(2), filen) # Load time and flux if not os.path.exists(file): print(file, "file not found") return None try: x, y, yerr = process_data(file, c=c) except (IOError, ValueError): print("Bad file", file) return None # compute the acf period, acf_smooth, lags, rvar, peaks = simple_acf(x, y) # make a plot if plot: plt.clf() plt.subplot(2, 1, 1) plt.plot(x-x[0], y, "k.") plt.xlim(0, max(lags)) plt.xlabel("$\mathrm{Time~(days)}$") plt.ylabel("$\mathrm{Normalised~flux}$") plt.subplot(2, 1, 2) plt.plot(lags, acf_smooth, "k") plt.xlabel("$\mathrm{lags~(days)}$") plt.ylabel("$\mathrm{ACF}$") plt.axvline(period, color="m") plt.xlim(min(lags), max(lags)) plt.subplots_adjust(left=.16, bottom=.12, hspace=.4) plt.savefig("acfs/{}_acf".format(epic)) # Measure LS period star = ro.prot(kepid=epic, x=x, y=y, yerr=yerr) pgram_period = star.pgram_ps(filter_period=10, plot=True, cutoff=30, clobber=clobber) return epic, period
def my_acf(N): ids = np.arange(N) periods = [] for id in ids: print "\n", id, "of", N x, y = np.genfromtxt("simulations/%s.txt" % str(int(id)).zfill(4)).T period, acf, lags, flag = simple_acf(x, y) periods.append(period) print period np.savetxt("simulations/%s_myacf_result.txt" % str(int(id)).zfill(4), np.ones(5)*period) np.savetxt("simulations/myacf_results.txt", np.transpose((ids, np.array(periods))))
def acf_demo(x, y): yerr = np.ones_like(y) * 1e-5 # period, err, lags, acf = Kepler_ACF.corr_run(x, y, yerr, 25) period, acf, lags, rvar = sa.simple_acf(x, y) print("measured period = ", period) plt.clf() plt.plot(lags, acf, "k") plt.axvline(period, color="CornFlowerBlue") plt.axvline(20.8, color="LightCoral", ls="--") plt.xlabel("$\mathrm{Lags~(Days)}$") plt.ylabel("$\mathrm{Autocorrelation}$") plt.xlim(0, 200) # 450 plt.savefig(os.path.join(FIG_DIR, "demo_ACF.pdf"))
def calc_p_init(x, y, yerr, id, RESULTS_DIR, clobber=True): fname = os.path.join(RESULTS_DIR, "{0}_acf_pgram_results.txt".format(id)) if not clobber and os.path.exists(fname): print("Previous ACF pgram result found") df = pd.read_csv(fname) m = df.N.values == id acf_period = df.acf_period.values[m] err = df.acf_period_err.values[m] pgram_period = df.pgram_period.values[m] pgram_period_err = df.pgram_period_err.values[m] else: print("Calculating ACF") acf_period, acf, lags, rvar = sa.simple_acf(x, y) err = .1 * acf_period # acf_period, err, lags, acf = acf.corr_run(x, y, np.ones_like(y)*1e-5, # id, RESULTS_DIR) plt.clf() plt.plot(lags, acf) plt.axvline(acf_period, color="r") plt.xlabel("Lags (days)") plt.ylabel("ACF") plt.savefig(os.path.join(RESULTS_DIR, "{0}_acf".format(id))) print("saving figure ", os.path.join(RESULTS_DIR, "{0}_acf".format(id))) print("Calculating periodogram") ps = np.arange(.1, 100, .1) model = LombScargle().fit(x, y, yerr) pgram = model.periodogram(ps) plt.clf() plt.plot(ps, pgram) plt.savefig(os.path.join(RESULTS_DIR, "{0}_pgram".format(id))) print("saving figure ", os.path.join(RESULTS_DIR, "{0}_pgram".format(id))) assert 0 peaks = np.array([i for i in range(1, len(ps)-1) if pgram[i-1] < pgram[i] and pgram[i+1] < pgram[i]]) pgram_period = ps[pgram == max(pgram[peaks])][0] print("pgram period = ", pgram_period, "days") pgram_period_err = pgram_period * .1 df = pd.DataFrame({"N": [id], "acf_period": [acf_period], "acf_period_err": [err], "pgram_period": [pgram_period], "pgram_period_err": [pgram_period_err]}) df.to_csv(fname) return acf_period, err, pgram_period, pgram_period_err
def run_acf(c, fn, plot=False): """ Run the ACF on a light curve in the specified campaign. c (str): campaign, e.g. "c01". fn (str): fits file name for a target in campaign c. """ epic = fn[20:29] file = "data/c{0}/{1}".format(c, fn) if not os.path.exists(file): print(file, "file not found") return None try: x, y = process_data(file) # period, acf_smooth, lags, rvar, peaks, dips, leftdips, rightdips, \ # bigpeaks = simple_acf(x, y) except (IOError, ValueError): print("Bad file", file) return None # compute the acf period, acf_smooth, lags, rvar, peaks = simple_acf(x, y) # make a plot if plot: plt.clf() plt.subplot(2, 1, 1) plt.plot(x-x[0], y, "k.") plt.xlim(0, max(lags)) plt.xlabel("$\mathrm{Time~(days)}$") plt.ylabel("$\mathrm{Normalised~flux}$") plt.subplot(2, 1, 2) plt.plot(lags, acf_smooth, "k") plt.xlabel("$\mathrm{lags~(days)}$") plt.ylabel("$\mathrm{ACF}$") plt.axvline(period, color="m") plt.xlim(min(lags), max(lags)) plt.subplots_adjust(left=.16, bottom=.12, hspace=.4) plt.savefig("results/{}_acf".format(epic)) return epic, period
if __name__ == "__main__": ntests = 100 amy = False periods = np.exp(np.random.uniform(-1, 4.6, ntests)) p1, p2 = [np.zeros(ntests) for i in range(2)] for i, period in enumerate(periods): print(i, "of", ntests) x, y = gen_data(period) x_gaps, y_gaps = make_gaps(x, y, 1000) if amy: period1, err1, lags1, acf1 = ka.corr_run(x, y, np.ones_like(y)*1e-5, "test", ".") period2, err2, lags2, acf2 = ka.corr_run(x_gaps, y_gaps, np.ones_like(y_gaps)*1e-5, "test", ".") else: period1, acf, lags, rvar = sa.simple_acf(x, y) period2, acf, lags, rvar = sa.simple_acf(x_gaps, y_gaps) print(period1, period2) p1[i] = period1 p2[i] = period2 m = (p1 > 0) * (p2 > 0) plt.clf() plt.plot(np.log(periods[m]), np.log(p1[m]), "b.") plt.plot(np.log(periods[m]), np.log(p2[m]), "r.") plt.savefig("test_compare_simple")
def recover_injections(id, x, y, yerr, fn, burnin, run, interval, tol, npts=10, nwalkers=32, plot=True): """ Take x, y, yerr, calculate ACF period for initialisation and do MCMC. npts: number of points per period. """ p_init, acf_smooth, lags, _, _, _, _, _, _ = simple_acf(x, y) print("acf period = ", p_init) if p_init < .1: # prevent unphysical periods p_init = 10. # Format data plims = np.log([p_init - tol*p_init, p_init + tol*p_init]) print(p_init, np.exp(plims)) sub = int(p_init / float(npts) * 48) # 10 points per period ppd = 48. / sub ppp = ppd * p_init print("sub = ", sub, "points per day =", ppd, "points per period =", ppp) # subsample xsub, ysub, yerrsub = x[::sub], y[::sub], yerr[::sub] xb, yb, yerrb = x, y, yerr xb, yb, yerrb = x[:100], y[:100], yerr[:100] plt.clf() plt.plot(xb, yb, "k.") plt.savefig("gptest") theta_init = np.log([np.exp(-5), np.exp(7), np.exp(.6), np.exp(-16), p_init]) print("\n", "log(theta_init) = ", theta_init) print("theta_init = ", np.exp(theta_init), "\n") # set up MCMC ndim, nwalkers = len(theta_init), nwalkers p0 = [theta_init+1e-4*np.random.rand(ndim) for i in range(nwalkers)] args = (xb, yb, yerrb, plims) lp = lnprob # time the lhf call start = time.time() print("lnprob = ", lp(theta_init, xb, yb, yerrb, plims)) end = time.time() tm = end - start print("1 lhf call takes ", tm, "seconds") print("burn in will take", tm * nwalkers * burnin, "s") print("run will take", tm * nwalkers * run, "s") print("total = ", (tm * nwalkers * run + tm * nwalkers * burnin)/60, "mins") # run MCMC sampler = emcee.EnsembleSampler(nwalkers, ndim, lp, args=args) print("burning in...") start = time.time() p0, lp, state = sampler.run_mcmc(p0, burnin) sampler.reset() print("production run...") p0, lp, state = sampler.run_mcmc(p0, run) end = time.time() print("actual time = ", end - start) # save samples f = h5py.File("%s_samples.h5" % id, "w") data = f.create_dataset("samples", np.shape(sampler.chain)) data[:, :] = np.array(sampler.chain) f.close() # make various plots if plot: with h5py.File("%s_samples.h5" % id, "r") as f: samples = f["samples"][...] mcmc_result = make_plot(samples, xsub, ysub, yerrsub, id, fn, traces=True, tri=True, prediction=True)
nspot_min = 50 # minimum number of spots nspot_max = 500 # maximum number of spots incl_min = 0 # minimum inclination incl_max = np.pi/4. # maximum inclination amp_min = 1 # minimum amplitude (multiple of range of variability) amp_max = 100 # maximum amplitude (see above) pmin = .5 # minimum period (days) pmax = 90 # maximum period (days) tau_min = 5 # minimum spot lifetime (multiple of rotation period) tau_max = 20 # maximum spot lifetime (see above) # load k2 lc epic = "201131066" x, y = k2lc(epic) period, acf, lags, rvar, peaks, dips, leftdips, rightdips, bigpeaks \ = simple_acf(x, y) start = time.time() acf, lags, period, err, locheight = corr_run(x, y) end = time.time() print("time = ", end-start) xarr, yarr, true_params = generate_lcs(x, y, epic, N, nspot_min=nspot_min, nspot_max=nspot_max, incl_min=incl_min, incl_max=incl_max, amp_min=amp_min, amp_max=amp_max, pmin=pmin, pmax=pmax, tau_min=tau_min,
def calc_p_init(x, y, yerr, id, RESULTS_DIR="pgram_filtered_results_35", clobber=True): fname = os.path.join(RESULTS_DIR, "{0}_acf_pgram_results.txt".format(id)) if not clobber and os.path.exists(fname): print("Previous ACF pgram result found") df = pd.read_csv(fname) m = df.N.values == id acf_period = df.acf_period.values[m] err = df.acf_period_err.values[m] pgram_period = df.pgram_period.values[m] pgram_period_err = df.pgram_period_err.values[m] else: print("Calculating ACF") acf_period, acf, lags, rvar = sa.simple_acf(x, y) err = .1 * acf_period plt.clf() plt.plot(lags, acf) plt.axvline(acf_period, color="r") plt.xlabel("Lags (days)") plt.ylabel("ACF") plt.savefig(os.path.join(RESULTS_DIR, "{0}_acf".format(id))) print("saving figure ", os.path.join(RESULTS_DIR, "{0}_acf".format(id))) # Interpolate across gaps gap_days = 0.02043365 time = np.arange(x[0], x[-1], gap_days) lin_interp = np.interp(time, x, y) x, y = time, lin_interp yerr = np.ones(len(y)) * 1e-5 print("Calculating periodogram") ps = np.arange(.1, 100, .1) model = LombScargle().fit(x, y, yerr) pgram = model.periodogram(ps) plt.clf() plt.plot(ps, pgram) period = 35. # days fs = 1. / (x[1] - x[0]) lowcut = 1. / period # pgram = model.periodogram(ps) yfilt = butter_bandpass_filter(y, lowcut, fs, order=3, plot=False) # plt.clf() # plt.plot(x, y, label='Noisy signal') # plt.plot(x, yfilt, label='{0} day^(-1), {1} days'.format(lowcut, # period)) # plt.xlabel('time (seconds)') # plt.grid(True) # plt.axis('tight') # plt.legend(loc='upper left') # plt.savefig("butter_filtered") # normalise data and variance. med = np.median(y) y -= med var = np.std(y) print("var = ", var) y /= var print("Calculating periodogram") ps = np.arange(.1, 100, .1) model = LombScargle().fit(x, yfilt, yerr) pgram = model.periodogram(ps) plt.plot(ps, pgram) plt.savefig(os.path.join(RESULTS_DIR, "{0}_pgram".format(id))) print("saving figure ", os.path.join(RESULTS_DIR, "{0}_pgram".format(id))) peaks = np.array([ i for i in range(1, len(ps) - 1) if pgram[i - 1] < pgram[i] and pgram[i + 1] < pgram[i] ]) pgram_period = ps[pgram == max(pgram[peaks])][0] # Calculate the uncertainty. _freq = 1. / pgram_period pgram_freq_err = calc_pgram_uncertainty(x, y, _freq) print(1. / _freq, "period") print(_freq, "freq") print(pgram_freq_err, "pgram_freq_err") frac_err = pgram_freq_err / _freq print(frac_err, "frac_err") pgram_period_err = pgram_period * frac_err print(pgram_period_err, "pgram_period_err") print("pgram period = ", pgram_period, "+/-", pgram_period_err, "days") df = pd.DataFrame({ "N": [id], "acf_period": [acf_period], "acf_period_err": [err], "pgram_period": [pgram_period], "pgram_period_err": [pgram_period_err] }) df.to_csv(fname) return acf_period, err, pgram_period, pgram_period_err