Example #1
0
def quarter_acf(lc_files, quarters, savedir):
    # record kid and quarter and run ACF
    for i, lc_file in enumerate(lc_files):
        q = quarters.index(str(lc_file[48:61]))
        kid = lc_file[38:47]
        time, flux, flux_err = load_data(lc_file)
        corr_run(time, flux, flux_err, kid, q, savedir)
def fit(
    x,
    y,
    yerr,
    id,
    p_init,
    plims,
    burnin=500,
    run=1500,
    npts=48,
    cutoff=100,
    sine_kernel=False,
    acf=False,
    runMCMC=True,
    plot=False,
):
    """
    takes x, y, yerr and initial guesses and priors for period and does
    the full GP MCMC.
    Tuning parameters include cutoff (number of days), npts (number of points
    per bin).
    """

    # measure ACF period
    if acf:
        corr_run(x, y, yerr, id, "/Users/angusr/Python/GProtation/code")

    if sine_kernel:
        print "sine kernel"
        theta_init = [np.exp(-5), np.exp(7), np.exp(0.6), np.exp(-16), p_init]
        print theta_init
        from GProtation import MCMC, make_plot
    else:
        print "cosine kernel"
        theta_init = [1e-2, 1.0, 1e-2, p_init]
        print "theta_init = ", np.log(theta_init)
        from GProtation_cosine import MCMC, make_plot

    xb, yb, yerrb = bin_data(x, y, yerr, npts)  # bin data
    m = xb < cutoff  # truncate

    theta_init = np.log(theta_init)
    DIR = "cosine"
    if sine_kernel:
        DIR = "sine"

    print theta_init
    if runMCMC:
        sampler = MCMC(theta_init, xb[m], yb[m], yerrb[m], plims, burnin, run, id, DIR)

    # make various plots
    if plot:
        with h5py.File("%s/%s_samples.h5" % (DIR, str(int(id)).zfill(4)), "r") as f:
            samples = f["samples"][...]
        m = x < cutoff
        mcmc_result = make_plot(samples, x[m], y[m], yerr[m], id, DIR, traces=False, triangle=False, prediction=True)
Example #3
0
def all_acf(globfile, id_list, savedir):
    # loop over stars
    for i, kid in enumerate(id_list):
        # load each quarter
        lc_files = np.array(glob.glob('%s/kplr*%s*_llc.fits'%(globfile, int(kid))))
        # initialise the time and flux arrays
        time, flux, flux_err = load_data(lc_files[0])
        # loop over quarters
        for i in range(1, len(lc_files)):
            time = np.concatenate((time, load_data(lc_files[i])[0]))
            flux = np.concatenate((flux, load_data(lc_files[i])[1]))
            flux_err = np.concatenate((flux_err, load_data(lc_files[i])[2]))
        # run ACF, once per star
        corr_run(time, flux, flux_err, kid, 'all', savedir)
Example #4
0
def simulate(N):

    periods = np.random.uniform(50, 100, N)
    xs = np.arange(0, 1600, .02043365)
    xs = np.arange(0, 1600, .02043365)

    acf_ps, my_acf_ps = [], []
    for i, p in enumerate(periods):
        print "\n", i, "of", N, "\n"
#         ys = np.sin(xs * 2 * np.pi * (1./p)) + np.random.randn(len(xs))*.2
        ys = np.sin(xs * 2 * np.pi * (1./p))
#         period, acf, lags, flag = simple_acf(xs, ys)
        acfp, p_err = corr_run(xs, ys, 1e-5, i, "tests", saveplot=True)
        acf_ps.append(acfp[0])
#         my_acf_ps.append(period)

    plt.clf()
    xs = np.arange(0, 100, .1)
    plt.plot(periods, acf_ps, "r.")
#     plt.plot(periods, my_acf_ps, "b.")
    plt.plot(xs, xs, "k--")
    plt.xlim(0, 100)
    plt.ylim(0, 100)
    plt.show()
    plt.savefig("sine_test")

    acf_ps = np.array(acf_ps)
#     my_acf_ps = np.array(my_acf_ps)
#     results = np.vstack((periods, acf_ps, my_acf_ps))
    results = np.vstack((periods, acf_ps))
    np.savetxt("sine_test.txt", np.transpose((results)))
def periodograms(N, plot=False, savepgram=True):

    ids = np.arange(N)
    periods = []
    for id in ids:
        print "\n", id, "of", N

        # load simulated data
        x, y = np.genfromtxt("simulations/%s.txt" % str(int(id)).zfill(4)).T
        yerr = np.ones_like(y) * 1e-5

        # initialise with acf
        try:
            p_init = np.genfromtxt("simulations/%s_result.txt"
                                   % int((id)))
        except:
            corr_run(x, y, yerr, int(id), "simulations",
                     saveplot=False)
            p_init = np.genfromtxt("simulations/%s_result.txt" % int(id))
        print "acf period, err = ", p_init

        ps = np.linspace(p_init[0]*.1, p_init[0]*4, 1000)
        model = LombScargle().fit(x, y, yerr)
        pgram = model.periodogram(ps)

        # find peaks
        peaks = np.array([i for i in range(1, len(ps)-1) if pgram[i-1] <
                         pgram[i] and pgram[i+1] < pgram[i]])
        period = ps[pgram==max(pgram[peaks])][0]
        periods.append(period)
        print "pgram period = ", period

        if plot:
            plt.clf()
            plt.plot(ps, pgram)
            plt.axvline(period, color="r")
            plt.savefig("simulations/%s_pgram" % str(int(id)).zfill(4))

        if savepgram:
            np.savetxt("simulations/%s_pgram.txt" % str(int(id)).zfill(4),
                       np.transpose((ps, pgram)))

    np.savetxt("simulations/periodogram_results.txt",
               np.transpose((ids, periods)))
    return periods
def recover_injections(start, stop, N=1000, runMCMC=True, plot=False):
    """
    run MCMC on each star, initialising with the ACF period
    Next you could try running for longer, using more of the lightcurve,
    subsampling less, etc
    """
#     ids, true_ps, true_as = np.genfromtxt("simulations/true_periods.txt").T
    ids = range(N)

    if start == 0: id_list = ids[:stop]
    elif stop == N: id_list = ids[start:]
    else: id_list = ids[start:stop]
    for id in id_list:
        print "\n", "star id = ", id

        # load simulated data
        x, y = np.genfromtxt("simulations/%s.txt" % str(int(id)).zfill(4)).T
        yerr = np.ones_like(y) * 1e-5

#         print "true period = ", true_ps[int(id)]

        # initialise with acf
        try:
            p_init = np.genfromtxt("simulations/%s_result.txt" % id)
        except:
            corr_run(x, y, yerr, id, "simulations")
            p_init = np.genfromtxt("simulations/%s_result.txt" % id)
        print "acf period, err = ", p_init

        # run MCMC
        plims = [p_init[0]*.7, p_init[0]*1.5]
        npts = int(p_init[0] / 10. * 48)  # 10 points per period
        cutoff = 10 * p_init[0]
        fit(x, y, yerr, str(int(id)).zfill(4), p_init[0], np.log(plims),
                burnin=5000, run=10000, npts=npts, cutoff=cutoff,
                sine_kernel=True, acf=False, runMCMC=runMCMC, plot=plot)
Example #7
0
def fit(x,
        y,
        yerr,
        id,
        p_init,
        plims,
        DIR,
        burnin=500,
        run=1500,
        npts=48,
        cutoff=100,
        sine_kernel=False,
        acf=False,
        runMCMC=True,
        plot=False):
    """
    takes x, y, yerr and initial guesses and priors for period and does
    the full GP MCMC.
    Tuning parameters include cutoff (number of days), npts (number of points
    per bin).
    DIR is where to save output
    """

    # measure ACF period
    if acf:
        corr_run(x, y, yerr, id, "/Users/angusr/Python/GProtation/code")

    if sine_kernel:
        print("sine kernel")
        theta_init = [np.exp(-5), np.exp(7), np.exp(.6), np.exp(-16), p_init]
        print(theta_init)
        from GProtation import MCMC, make_plot
    else:
        print("cosine kernel")
        theta_init = [1e-2, 1., 1e-2, p_init]
        print("theta_init = ", np.log(theta_init))
        from GProtation_cosine import MCMC, make_plot

    xb, yb, yerrb = bin_data(x, y, yerr, npts)  # bin data
    m = xb < cutoff  # truncate

    theta_init = np.log(theta_init)

    print(theta_init)
    if runMCMC:
        sampler = MCMC(theta_init, xb[m], yb[m], yerrb[m], plims, burnin, run,
                       id, DIR)

    # make various plots
    if plot:
        with h5py.File("%s/%s_samples.h5" % (DIR, id), "r") as f:
            samples = f["samples"][...]
        m2 = x < cutoff
        mcmc_result = make_plot(samples,
                                xb[m],
                                yb[m],
                                yerrb[m],
                                x[m2],
                                y[m2],
                                yerr[m2],
                                str(int(id)).zfill(4),
                                DIR,
                                traces=True,
                                tri=True,
                                prediction=True)
Example #8
0
    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,
                                                 tau_max=tau_max)
    periods = true_params["periods"]
    amps = true_params["amps"]
Example #9
0
import numpy as np
import matplotlib.pyplot as plt
from Kepler_ACF import corr_run
import glob

DIR = "."  # edit me!
fnames = glob.glob("%s/*.dat" % DIR)

for i, fname in enumerate(fnames):
    id = fname.split("/")[-1].split("_")[0]  # edit me!
    x, y, _, _ = np.genfromtxt(fname, skip_header=1).T
    yerr = np.ones_like(y) * 1e-5  # FIXME
    corr_run(x, y, yerr, id, "acf")