Ejemplo n.º 1
0
def periodograms(id, x, y, yerr, path, plot=False, savepgram=False):
    """
    takes id of the star, returns an array of period measurements and saves the
    results.
    id: star id.
    x, y, yerr: time, flux and error arrays.
    path: path where you want to save the output.
    """
    ps = np.linspace(2, 100, 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]])
    if len(peaks):
        period = ps[pgram==max(pgram[peaks])][0]
    else: period = 0

    if plot:
        plt.clf()
        plt.plot(ps, pgram)
        plt.axvline(period, color="r")
        plt.savefig("{0}/{1}_pgram".format(path, str(int(id)).zfill(4)))

    if savepgram:
        np.savetxt("{0}/{1}_pgram.txt".format(path, str(int(id)).zfill(4)),
                   np.transpose((ps, pgram)))

    np.savetxt("{0}/{1}_pgram_result.txt".format(path, str(int(id)).zfill(4)),
               np.ones(2).T*period)
    return period
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
def pgram(N, years, fname):
    ps = np.linspace(2, 100, 1000)  # the period array (in days)

    print("Computing periodograms")
    # Now compute LS pgrams for a set of LSST light curves & save highest peak
    ids = np.arange(N)
    periods = np.zeros_like(ids)
    for i, id in enumerate(ids):
        sid = str(int(id)).zfill(4)
        x, y, yerr = np.genfromtxt("simulations/{0}/{1}.txt".format(
            fname, sid)).T
        m = x < years * 365.25
        xt, yt, yerrt = x[m], y[m], yerr[m][m]
        model = LombScargle().fit(xt, yt, yerrt)  # compute pgram
        pgram = model.periodogram(ps)

        # find peaks
        peaks = np.array([
            j for j in range(1,
                             len(ps) - 1)
            if pgram[j - 1] < pgram[j] and pgram[j + 1] < pgram[j]
        ])
        if len(peaks):
            period = ps[pgram == max(pgram[peaks])][0]
        else:
            period = 0

        periods[i] = period
        np.savetxt(
            "results/{0}/{1}_{2}yr_result.txt".format(fname, sid, years),
            [period])
    np.savetxt("{0}_{1}yr_results.txt".format(fname, years), periods.T)
    return periods
Ejemplo n.º 4
0
def gen_recover_period(prange, ntimes, npers, randtimes = 0.0, rantnormp = 0.0, snrnoise = 0.0, normpropnoise = 0.0, samples = 100):
    """Generate some periodic data and then try to recover the maximum period under various conditions.

    prange = period to analyse (kicking off with a single period) with lower bound and upper bound for search
    ntimes number of times to generate signal for
    npers number of periods (possibly fractional) to scan for
    randtimes whether we vary the sample times 0.0 none, 1 probably max
    rantnormp variation in times proortion between uniform (0.0) and gaussian (1.0)
    snr signal to noise ration of noise to add 0 for None
    normpropnoise proportion of noise uniform (0.0) or gaussian (1.0)
    samples is the number of samples for the periodogram

    Return fractional error"""

    lbound, period, ubound = prange

    timelist, amps = siggen.siggen(period, ntimes=ntimes, npers=npers, randv=randtimes, unorm=rantnormp, randphase=True)

    if snrnoise > 0.0:
        amps = noise.noise(amps, snrnoise, normpropnoise)

    # OK now lets do our stuff

    model = LombScargle().fit(timelist, amps, 0.001)
    periods = np.linspace(lbound, ubound, samples)
    pgram = model.periodogram(periods)

    # Find maximum

    maxes = argmaxmin.maxmaxes(periods, pgram)
    if len(maxes) == 0:
        return  1.0

    maxp = periods[maxes[0]]
    return abs(maxp - period) / period
Ejemplo n.º 5
0
def pgram(N, years, fname):
    ps = np.linspace(2, 100, 1000)  # the period array (in days)

    print("Computing periodograms")
    # Now compute LS pgrams for a set of LSST light curves & save highest peak
    ids = np.arange(N)
    periods = np.zeros_like(ids)
    for i, id in enumerate(ids):
        sid = str(int(id)).zfill(4)
        x, y, yerr = np.genfromtxt("simulations/{0}/{1}.txt".format(fname,
                                   sid)).T
        m = x < years * 365.25
        xt, yt, yerrt = x[m], y[m], yerr[m][m]
        model = LombScargle().fit(xt, yt, yerrt)  # compute pgram
        pgram = model.periodogram(ps)

        # find peaks
        peaks = np.array([j for  j in range(1, len(ps)-1) if pgram[j-1]
                          < pgram[j] and pgram[j+1] < pgram[j]])
        if len(peaks):
            period = ps[pgram == max(pgram[peaks])][0]
        else:
            period = 0

        periods[i] = period

    data = np.vstack((ids, periods))
    f = open("results/{0}_{1}yr_results.txt".format(fname, years), "a")
    np.savetxt(f, data.T)
    f.close()
    return periods
Ejemplo n.º 6
0
def vbg(campaign):
    fnames = glob.glob("data/c%s/*lc.fits" % campaign)
    for fname in fnames:
        eid = fname[12:21]
        print eid
        if campaign == 1:
            fname = "/Users/angusr/data/K2/c1lcsr4"
            # load data
            x, y, _ = np.genfromtxt("%s/ep%s.csv" % (fname, str(int(eid))),
                                    delimiter=",").T
        elif campaign == 0:
            fname = "/Users/angusr/data/K2/c0corcutlcs"
            # load data
            x, y, _ = np.genfromtxt("%s/ep%scorcut.csv" %
                                    (fname, str(int(eid))),
                                    delimiter=",",
                                    skip_header=1).T

        y /= np.median(y)
        y -= 1
        x *= 24 * 3600  # convert to seconds
        # load basis
        with h5py.File("data/c1.h5", "r") as f:
            basis = f["basis"][:150]
        fs = np.arange(1, 300, 4e-2) * 1e-6
        ps = 1. / fs
        model = LombScargle().fit(x, y, np.ones_like(y) * 1e-5)
        s2n = model.periodogram(ps)
        # save pgram
        plt.clf()
        plt.plot(fs, s2n, "k")
        plt.savefig("astero/%svbg_pgram" % eid)
        np.savetxt("astero/%svbg_pgram.txt" % eid, np.transpose((fs, s2n)))
Ejemplo n.º 7
0
    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
Ejemplo n.º 8
0
def plot_vbg(fname, eid):
    # plot andrew's lc
    x_vbg, y_vbg, _ = np.genfromtxt("data/ep%s.csv" % eid, delimiter=",").T
    x_vbg *= 24 * 3600
    y_vbg = y_vbg / np.median(y_vbg) - 1
    model = LombScargle().fit(x_vbg, y_vbg, np.ones_like(y_vbg) * 1e-5)
    period = 1. / fs
    pgram = model.periodogram(period)
    plt.clf()
    plt.plot(fs, pgram, "k")
    plt.xlabel("$\mathrm{Frequency~(}\mu \mathrm{Hz)}$")
    plt.ylabel("$\mathrm{Power}$")
    plt.savefig("astero/vbg_%spgram" % eid)
Ejemplo n.º 9
0
def pgram(t, y, dy, fname):
#     t *= 24*3600  # convert to seconds
#     fs = np.arange(50, 1000, .1)*1e-6  # Hz
#     period = 1. / fs
#     plt.plot(fs, power)
    model = LombScargle().fit(t, y, dy)
    period = np.linspace(.25/24, 5./24, 1000)
    fs = 1./period
    power = model.periodogram(period)
    plt.clf()
    plt.plot(period*24., power)
    print "%s_pgram" % fname
    plt.savefig("%s_pgram" % fname)
    print period[power==max(power)]*24
Ejemplo n.º 10
0
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
Ejemplo n.º 11
0
def bases_FFT(eid, nbases):
    x, y, basis = read_data(eid, nbases)
    fs = np.linspace(1e-6, .7, 1000)
    ps = 1. / fs
    plt.clf()
    cols = np.linspace(.1, .99, len(basis))
    freqs = []
    for i in range(len(basis)):
        model = LombScargle().fit(x, basis[i], np.ones_like(x) * 1e-5)
        pgram = model.periodogram(ps)
        plt.plot(fs, pgram, color="%s" % cols[i])
        freqs.append(fs[pgram == max(pgram)][0])
    plt.savefig("all_bases")
    freqs = np.array(freqs)
    np.savetxt("elc_freqs.txt", np.transpose((np.arange(nbases), freqs)))
Ejemplo n.º 12
0
def find_spikes_vbg(fnames):
    for i, fname in enumerate(fnames):
        eid = fname[15:24]
        print eid, i, "of", len(fnames)
        x, y, _ = np.genfromtxt("../data/c1/ep%s.csv" % eid, delimiter=",").T
        x *= 24 * 3600
        y /= np.median(y)
        fs = np.arange(40, 55, 1e-1) * 1e-6
        ps = 1. / fs
        model = LombScargle().fit(x, y, np.ones_like(y) * 1e-5)
        pg = model.periodogram(ps)
        f = h5py.File("spikes/spike_%s_vbg.h5" % eid, "w")
        data = f.create_dataset("pgram", (len(fs), 2))
        data[:, 0] = fs
        data[:, 1] = pg
        f.close()
Ejemplo n.º 13
0
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
Ejemplo n.º 14
0
def trialfor(p1, p2):
    """Try L-S routine for periods p1 and p2, other parameters given by globals"""
    
    global trialfreqs, trialperiods, usegatspy, obstimes, amp1, amp2, usegatspy, phases, maxnum, errbar, errs
    global resultvec, rounding
    global TWOPI

    rs = set()
    for p in phases:
        sig = amp1 * np.sin(obstimes * TWOPI / p1) + amp2 * np.sin(p + obstimes * TWOPI / p2)
        if usegatspy:
            model = LombScargle().fit(obstimes, sig, errnar)
            pgram = model.periodogram(trialperiods)
        else:
            pgram = lsas(obstimes, sig, errs, trialfreqs)
        maxima = argmaxmin.maxmaxes(trialperiods, pgram)
        if len(maxima) > maxnum:
            maxima = maxima[0:maxnum]
        rs |= set(np.round(trialperiods[maxima], rounding))
    rs = list(rs)
    rs.sort()
    resultvec.append((p1, p2, rs))
Ejemplo n.º 15
0
def plot_raw(fname, eid):
    # read the data
    data = fitsio.read("../data/c1/ktwo%s-c01_lpd-lc.fits" % epid)
    aps = fitsio.read("../data/c1/ktwo%s-c01_lpd-lc.fits" % epid, 2)
    y = data["flux"][:, np.argmin(aps["cdpp6"])]
    x = data["time"]
    q = data["quality"]
    l = np.isfinite(y) * np.isfinite(x) * (q == 0)
    y, x = y[l], x[l]
    y /= np.median(y)
    y -= 1
    x *= 24 * 3600
    model = LombScargle().fit(x, y, np.ones_like(y) * 1e-5)
    fs = np.linspace(1, 280, 1000) * 1e-6
    period = 1. / fs
    pgram = model.periodogram(period)
    plt.clf()
    plt.plot(fs * 1e6, pgram, "k")
    plt.xlabel("$\\nu\mathrm{(}\mu \mathrm{Hz)}$")
    plt.ylabel("$\mathrm{Power}$")
    plt.title("$\mathrm{EPIC~%s}$" % eid)
    plt.savefig("documents/raw_%s" % eid)
Ejemplo n.º 16
0
def find_modes(fname, eid, nbasis=150, campaign=1, raw=False):
    data = fitsio.read(fname)
    aps = fitsio.read(fname, 2)
    y = data["flux"][:, np.argmin(aps["cdpp6"])]
    x = data["time"]
    q = data["quality"]
    l = np.isfinite(y) * np.isfinite(x) * (q==0)
    y, x = y[l], x[l]
    y /= np.median(y)
    y -= 1
    x *= 24*3600  # convert to seconds

    # plot raw data
    if raw == True:
        plt.clf()
        model = LombScargle().fit(x, y, np.ones_like(y)*1e-5)
        period = 1. / fs
        raw_pgram = model.periodogram(period)
        plt.plot(fs, raw_pgram, "k")
        plt.savefig("astero/raw_%spgram" % eid)

    # load basis
    with h5py.File("data/c%s.h5" % campaign, "r") as f:
        basis = f["basis"][:150, l]

    fs = np.arange(10, 300, 4e-2) * 1e-6
    amps2, s2n, w = K2pgram(x, y, basis, fs)

    # plot our pgram
    plt.clf()
    fs *= 1e6
    plt.plot(fs, s2n, "k")
    plt.xlabel("$\mathrm{Frequency~(}\mu \mathrm{Hz)}$")
    plt.ylabel("$\mathrm{Power}$")
    plt.savefig("astero/%sastero_pgram" % eid)

    # save pgram
    np.savetxt("astero/%sastero_pgram.txt" % eid, np.transpose((fs, s2n)))
Ejemplo n.º 17
0
def periodograms(id, x, y, yerr, path, plot=False, savepgram=False):
    """
    takes id of the star, returns an array of period measurements and saves the
    results.
    id: star id.
    x, y, yerr: time, flux and error arrays.
    path: path where you want to save the output.
    """
    ps = np.linspace(2, 100, 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]
    ])
    if len(peaks):
        period = ps[pgram == max(pgram[peaks])][0]
    else:
        period = 0

    if plot:
        plt.clf()
        plt.plot(ps, pgram)
        plt.axvline(period, color="r")
        plt.savefig("{0}/{1}_pgram".format(path, str(int(id)).zfill(4)))

    if savepgram:
        np.savetxt("{0}/{1}_pgram.txt".format(path,
                                              str(int(id)).zfill(4)),
                   np.transpose((ps, pgram)))

    np.savetxt("{0}/{1}_pgram_result.txt".format(path,
                                                 str(int(id)).zfill(4)),
               np.ones(2).T * period)
    return period
Ejemplo n.º 18
0
    fig.add_subplot(gs[0, 1])
]

# Plot the data
ax[0].errorbar(t, mag, dmag, fmt='o', color='#333333')
ax[1].errorbar(phase, mag, dmag, fmt='.', color='#888888')

# Fit and plot the model
model = LombScargle().fit(t, mag, dmag)
model.optimizer.period_range = (0.2, 1.2)

phase = (t / model.best_period) % 1
phasefit = np.linspace(0, 1, 1000)
tfit = model.best_period * phasefit

lines = ax[2].plot(periods, model.periodogram(periods), lw=1)
ax[1].plot(phasefit, model.predict(tfit), c=lines[0].get_color())
ax[1].invert_yaxis()

ax[0].set_xlabel('date of observation (MJD)')
ax[0].set_ylabel('magnitude')
ax[0].set_title('Input Data')
ax[0].invert_yaxis()

ax[1].set_title('Folded Data (P={0:.3f} days)'.format(rrlyrae.period))
ax[1].set_xlabel('phase')
ax[1].yaxis.set_major_locator(plt.MultipleLocator(0.2))

ax[2].set_title('Periodogram')
ax[2].set_xlabel('period (days)')
ax[2].set_ylabel('power')
Ejemplo n.º 19
0
    plt.subplot(3, 1, 2)
    print ("calculating  fft for long cadence data")
    pgram = nufft.nufft3(stops, y, ws)
    plt.plot((fs-fs0)*1e6, pgram, "k", alpha=.7)
    if len(truths):
        for truth in truths:
            plt.axvline(truth*1e-6, color="r", linestyle="-.")
    plt.ylabel("$\mathrm{FFT}$")
    plt.xlabel("$\mathrm{Frequency-%s~(uHz)}$" % (fs0*1e6))

    # LombScargle long cadence data
    plt.subplot(3, 1, 3)
    print ("calculating LombScargle for long cadence data")
    periods = 1./fs
    model = LombScargle().fit(x, y, yerr)
    power = model.periodogram(periods)
    plt.plot((fs-fs0)*1e6, power, "k", alpha=.7)
    if len(truths):
        for truth in truths:
            plt.axvline(truth*1e-6, color="r", linestyle="-.")
    plt.ylabel("$\mathrm{LS}$")
    plt.xlabel("$\mathrm{Frequency-%s~(uHz)}$" % (fs0*1e6))
    plt.subplots_adjust(hspace=.4)

#     # pwnnyquist long cadence data
#     plt.clf()
#     plt.subplot(3, 1, 1)
#     print ("calculating superpgram for long cadence data")
#     x, y, yerr, ivar = load_data(kid, kepler_lc_dir, sc=False)
#     starts, stops, centres = real_footprint(x)
#     amp2s = spg.superpgram(starts, stops, y, ivar, ws)
Ejemplo n.º 20
0
      fig.add_subplot(gs[1, 1]),
      fig.add_subplot(gs[0, 1])]

# Plot the data
ax[0].errorbar(t, mag, dmag, fmt='o', color='#333333')
ax[1].errorbar(phase, mag, dmag, fmt='.', color='#888888')

# Fit and plot the model
model = LombScargle().fit(t, mag, dmag)
model.optimizer.period_range = (0.2, 1.2)

phase = (t / model.best_period) % 1
phasefit = np.linspace(0, 1, 1000)
tfit = model.best_period * phasefit
        
lines = ax[2].plot(periods, model.periodogram(periods), lw=1)
ax[1].plot(phasefit, model.predict(tfit),
           c=lines[0].get_color())
ax[1].invert_yaxis()

ax[0].set_xlabel('date of observation (MJD)')
ax[0].set_ylabel('magnitude')
ax[0].set_title('Input Data')
ax[0].invert_yaxis()

ax[1].set_title('Folded Data (P={0:.3f} days)'.format(rrlyrae.period))
ax[1].set_xlabel('phase')
ax[1].yaxis.set_major_locator(plt.MultipleLocator(0.2))

ax[2].set_title('Periodogram')
ax[2].set_xlabel('period (days)')
Ejemplo n.º 21
0
    print "Conversion error on", integ
    sys.exit(12)
except IndexError:
    print "Invalid data time column max =", arr.shape[0] 
    sys.exit(13)

results = np.zeros_like(periods)

for n in xrange(0,iters):
	sums = np.zeros_like(timings)
	if gaussp is not None:
		sums += nr.normal(loc=gmean, scale=gstd, size=len(timings))
	if uniformp is not None:
		sums += nr.uniform(low=ulow, high=uhigh, size=len(timings))
	model = LombScargle().fit(timings, sums, err)
	pgram = model.periodogram(periods)
	if maxes > 0:
		maxima = argmaxmin.maxmaxes(periods, pgram)
		if len(maxima) > maxes: maxima = maxima[0:maxes]
		results[maxima] += pgram[maxima]
	else:
		results += pgram
	print "Completed iteration", n+1

res = np.array([periods, results])

try:
    np.savetxt(outspec, res.transpose())
except IOError as e:
    print "Could not save output file", outspec, "error was", e.args[1]
    sys.exit(14)
Ejemplo n.º 22
0
gs = plt.GridSpec(2, 2, left=0.07, right=0.95, wspace=0.15, bottom=0.15)
ax = [
    fig.add_subplot(gs[:, 0]),
    fig.add_subplot(gs[0, 1]),
    fig.add_subplot(gs[1, 1])
]

ax[0].errorbar(phase[mask], mag[mask], dmag[mask], fmt='o', color='#666666')
ax[0].errorbar(phase[~mask], mag[~mask], dmag[~mask], fmt='o', color='#CCCCCC')
ax[0].invert_yaxis()

for fit_offset in [False, True]:
    i = int(fit_offset)
    model = LombScargle(fit_offset=fit_offset).fit(t[mask], mag[mask],
                                                   dmag[mask])
    P = model.periodogram(periods)
    if fit_offset:
        label = 'floating mean'
    else:
        label = 'standard'
    lines = ax[0].plot(phasefit,
                       model.predict(tfit, period=rrlyrae.period),
                       label=label)
    ax[1 + i].plot(periods, P, lw=1, c=lines[0].get_color())
    ax[1 + i].set_title('{0} Periodogram'.format(label.title()))
    ax[1 + i].set_ylabel('power')
    ax[1 + i].set_ylim(0, 1)
    ax[1 + i].set_xlim(0.2, 1.4)

ax[0].legend(loc='upper left')
ax[0].set_xlabel('phase')
Ejemplo n.º 23
0
def raw_and_vbg():

    plotpar = {
        'axes.labelsize': 12,
        'text.fontsize': 18,
        'legend.fontsize': 18,
        'xtick.labelsize': 14,
        'ytick.labelsize': 14,
        'text.usetex': True
    }
    plt.rcParams.update(plotpar)

    # eid = "201545182"
    eid = "201183188"
    fname = "../data/c1/ktwo%s-c01_lpd-lc.fits" % eid

    # load raw data
    data = fitsio.read(fname)
    aps = fitsio.read(fname, 2)
    y = data["flux"][:, np.argmin(aps["cdpp6"])]
    x = data["time"]
    q = data["quality"]
    l = np.isfinite(y) * np.isfinite(x) * (q == 0)
    y, x = y[l], x[l]
    MAD = np.median(y - np.median(y))
    y /= np.median(y)
    y -= 1
    x *= 24 * 3600  # convert to seconds

    fs = np.arange(.1, 300, 4e-2) * 1e-6  # astero

    # plot raw data
    fig = plt.figure()
    ax = fig.add_subplot(211)
    ax1 = fig.add_subplot(311)
    model = LombScargle().fit(x, y, np.ones_like(y) * 1e-5)
    period = 1. / fs

    start = time.time()
    raw_pgram = model.periodogram(period)
    end = time.time()
    print("LS time = ", end - start)

    ax1.plot(fs[::3] * 1e6, raw_pgram[::3], "k", label="$\mathrm{Raw}$")
    ax.set_title("$\mathrm{EPIC~%s}$" % eid)
    ax1.set_xlim(10, 280)
    ax1.set_ylim(0, .015)
    plt.ylabel("$\mathrm{Power}$")
    #     plt.legend()
    #     leg1 = Rectangle((0, 0), 0, 0, alpha=0.0)
    #     plt.legend([leg1], "$\mathrm{Raw}$", handlelength=0)
    plt.text(230, .012, "$\mathrm{Raw}$")
    ticks = ax1.get_yticks()
    ax1.set_yticks(ticks[1:-1])
    ax.set_yticklabels(ax.get_yticklabels(), visible=False)
    ax.set_xticklabels(ax.get_xticklabels(), visible=False)

    ax.spines['top'].set_color('none')
    ax.spines['bottom'].set_color('none')
    ax.spines['left'].set_color('none')
    ax.spines['right'].set_color('none')
    ax.tick_params(labelcolor='w',
                   top='off',
                   bottom='off',
                   left='off',
                   right='off')

    # load andrew's lcs
    ax2 = fig.add_subplot(312)
    x, y = np.genfromtxt("../data/c1/ep%s.csv" % eid, skip_header=1).T
    x *= 24 * 3600

    model = LombScargle().fit(x, y, np.ones_like(y) * 1e-5)
    ps = 1. / fs
    pgram = model.periodogram(ps)
    ax2.plot(fs[::3] * 1e6, pgram[::3], "k", label="$\mathrm{Detrended}$")
    plt.text(200, .010, "$\mathrm{VJ14~Detrended}$")
    #     leg1 = Rectangle((0, 0), 0, 0, alpha=0.0)
    #     plt.legend([leg1], "$\mathrm{VJ14~Detrended}$", handlelength=0)
    ax2.set_xlim(10, 280)
    ax2.set_ylim(0, .015)
    plt.ylabel("$\mathrm{Power}$")
    ticks = ax2.get_yticks()
    ax2.set_yticks(ticks[1:-1])
    #     fig.text(0.04, 0.5, "$\mathrm{Power}$", ha="center", va="top",
    #              rotation="vertical")

    # load sip
    fs, s2n = np.genfromtxt("../astero/%sastero_pgram.txt" % str(int(eid))).T
    ax3 = fig.add_subplot(313)
    if MAD == 0.:
        MAD = 1.
    plt.plot(fs[::3], s2n[::3] * 10e4 / MAD**2, "k", label="$\mathrm{SIP}$")
    #     leg1 = Rectangle((0, 0), 0, 0, alpha=0.0)
    #     plt.legend([leg1], "$\mathrm{SIP}$", handlelength=0)
    plt.text(230, 2.2, "$\mathrm{SIP}$")
    ax3.set_xlim(10, 280)
    plt.ylabel(
        "$\mathrm{Relative~(S/N)}^2\mathrm{~(} \\times 10^{4}\mathrm{)}$")
    plt.xlabel("$\\nu\mathrm{~(}\mu\mathrm{Hz)}$")
    fig.subplots_adjust(hspace=0, bottom=.1)
    #     ticks = ax3.get_yticks()
    #     ax3.set_yticks(ticks[1:-1])
    print("saving as ../documents/rawvbg_%s.pdf" % eid)
    print("saving as poster_rawvbg_%s.pdf" % eid)
    plt.savefig("../documents/rawvbg_%s.pdf" % eid)
    plt.savefig("poster_rawvbg_%s" % eid, transparent=True)

    # compute Fourier transform
    sp = np.fft.fft(y)
    freq = np.fft.fftfreq(x.shape[-1])
    fft = sp.real**2 * np.imag**2
    plt.clf()
    plt.plot(freq, fft)
    plt.savefig("fft")
Ejemplo n.º 24
0
        
        # Scale back to zero doesn't alter L-S result
        
        day_bjdates -= day_bjdates[0] 

        sel = day_xrayvs <= xraylevel
        bjd = day_bjdates[sel]
        dv = day_values[sel]
        #dv -= dv.mean()                     # Need this for ss.lombscargle to work
        
        if plotit:
            plt.figure()
            plt.plot(bjd, dv)
        
        model = LombScargle().fit(bjd, dv, errorlev)
        spectrum = model.periodogram(idrange)
        if abss: spectrum = np.abs(spectrum)        
        np.savetxt(outfile + "_day_%d.ls" % fnum, np.transpose(np.array([idrange, spectrum])))
        fnum += 1

bjdates -= bjdates[0]
sel = xrayvs < xraylevel
bjd = bjdates[sel]
dv = values[sel]
#dv -= dv.mean()

if plotit:
    plt.figure()
    plt.plot(bjd, dv)
model = LombScargle().fit(bjd, dv, errorlev)
spectrum = model.periodogram(allrange)
Ejemplo n.º 25
0
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
Ejemplo n.º 26
0
def p_child_plot(x, y, eid):

    plotpar = {'axes.labelsize': 15,
               'text.fontsize': 15,
               'legend.fontsize': 15,
               'xtick.labelsize': 12,
               'ytick.labelsize': 12,
               'text.usetex': True}
    plt.rcParams.update(plotpar)

    y = y/np.median(y) - 1

    # SUBTRACT LINEAR TREND!
    plv = np.polyfit(x, y, 1)
    print(plv)
    poly = plv[1] + x * plv[0]
    y -= poly

    # compute acf
    dt = 0.02043359821692  # time resolution of K2 data (from header)

    # for timing: acf takes 6 milliseconds with 3357 data points
    # N = 1000
    # start = time.time()
    # for i in range(N):
    #     acf = emcee.autocorr.function(y)
    # end = time.time()
    # print("acf time = ", (end - start)/N * 1e3, "milliseconds")

    acf = emcee.autocorr.function(y)
    lags = np.arange(len(acf)) * dt

    # smooth acf
    acf = sps.savgol_filter(acf, 15, 1)

    # compute LS periodogram
    model = LombScargle().fit(x, y, np.ones_like(y)*1e-5)
    fmax = max(lags)/100.
    fs = np.linspace(1e-6, fmax, 1000)
    ps = 1./fs
#     ps = np.linspace(.1, 2, 1000)
#     fs = 1./ps
    pgram = model.periodogram(ps)
    np.savetxt("lspgram_%s.txt" % eid, np.transpose((ps, pgram)))

    plt.clf()
    plt.subplot(3, 1, 1)
    l = x < 2016
    plt.plot(x[l], y[l], "k")
    plt.plot(x[~l], y[~l], "k")
    plt.xlabel("$\mathrm{BJD-2454833~(days)}$")
    plt.ylabel("$\mathrm{Normalized~Flux}$")
    plt.xlim(min(x), max(x))
    plt.title("$\mathrm{EPIC~%s}$" % eid)

#     plt.ylim(-.02, .02)

    plt.subplot(3, 1, 2)
    plt.plot(lags, acf, "k")
    plt.ylabel("$\mathrm{Autocorrelation}$")
    plt.xlabel("$\mathrm{Time~(days)}$")
    acfx, acfy = acf_peak_detect(lags, acf)
    plt.axvline(acfx, color=".5", linestyle="--", label="$P_{rot}=%.2f$"
                % acfx)
    plt.legend()
    plt.xlim(min(lags), max(lags))

    plt.subplot(3, 1, 3)
#     plt.plot(fs, pgram, "k")
#     plt.xlim(min(fs), fmax)
    plt.plot(1./fs, pgram, "k")
#     plt.xlim(0, 2)
    plt.xlim(min(1./fs), max(lags))
    plt.xlabel("$\mathrm{Period~(days)}$")
#     plt.xlabel("$\mathrm{Frequency~(days}^{-1}\mathrm{)}$")
    plt.ylabel("$\mathrm{Power}$")
    l = fs > 1./100
    mx, my = max_peak_detect(fs[l], pgram[l])
    px = 1./mx
#     plt.axvline(mx, color=".5", linestyle="--", label="$P_{rot}=%.2f$" % px)
    plt.axvline(px, color=".5", linestyle="--", label="$P_{max}=%.2f$" % px)
#     plt.ylim(0, .6)
    plt.legend(loc="best")
    plt.subplots_adjust(hspace=.4)
#     plt.savefig("vbg_%s" % eid)
#     plt.savefig("../documents/rotation_poster_child.pdf")
    print("../documents/rotation%s.pdf" % eid)
    plt.savefig("../documents/rotation%s.pdf" % eid)
    return acfx, px
tfit = rrlyrae.period * phasefit

fig = plt.figure(figsize=(10, 4))
gs = plt.GridSpec(3, 2, left=0.07, right=0.95, bottom=0.15, wspace=0.15, hspace=0.3)

ax = [fig.add_subplot(gs[:, 0]), fig.add_subplot(gs[0, 1]), fig.add_subplot(gs[1, 1]), fig.add_subplot(gs[2, 1])]

# Plot the data
ax[0].errorbar(phase, mag, dmag, fmt="o", color="#AAAAAA")

# Plot the fits
models = [1, 2, 6]

for i, Nterms in enumerate(models):
    model = LombScargle(Nterms=Nterms).fit(t, mag, dmag)
    P = model.periodogram(periods)

    label = "{0} terms".format(Nterms)
    if Nterms == 1:
        label = label[:-1]

    lines = ax[0].plot(phasefit, model.predict(tfit, period=rrlyrae.period), label=label)
    ax[1 + i].plot(periods, model.periodogram(periods), c=lines[0].get_color(), lw=1)
    ax[1 + i].set_title("{0}-term Periodogram".format(Nterms))
    ax[1 + i].set_xlim(0.2, 1.4)
    ax[1 + i].set_ylim(0, 1)

ax[2].set_ylabel("power")
ax[3].set_xlabel("period (days)")
for i in [1, 2]:
    ax[i].xaxis.set_major_formatter(plt.NullFormatter())
Ejemplo n.º 28
0
    fig = plt.figure(figsize=(10, 4))
    gs = plt.GridSpec(3, 2, left=0.07, right=0.95, bottom=0.15,
                      wspace=0.15, hspace=0.3)

    ax = [fig.add_subplot(gs[:, 0]),
          fig.add_subplot(gs[0, 1]),
          fig.add_subplot(gs[1, 1]),
          fig.add_subplot(gs[2, 1])]

    # Plot the data
    ax[0].errorbar(phase, mag, dmag, fmt='o', color='#AAAAAA')

    # Plot the fits
    for i, Nterms in enumerate(models):
        model = LombScargle(Nterms=Nterms).fit(t, mag, dmag)
        P = model.periodogram(periods)

        label = "{0} terms".format(Nterms)
        if Nterms == 1:
            label = label[:-1]

        alpha = 1.0 if (Nterms < 20) else 0.5

        lines = ax[0].plot(phasefit, model.predict(tfit, period=rrlyrae.period),
                           label=label, alpha=alpha)

        ax[1 + i].plot(periods, model.periodogram(periods),
                       color=lines[0].get_color(), lw=1)
        ax[1 + i].set_title("{0}-term Periodogram".format(Nterms))
        ax[1 + i].set_xlim(0.2, 1.4)
        ax[1 + i].set_ylim(0, 1)
Ejemplo n.º 29
0
def grid_over_amps(basis,
                   flux,
                   raw_x,
                   raw_y,
                   truth,
                   fs,
                   amps,
                   true_a,
                   flag,
                   n,
                   plot=False,
                   raw=False,
                   random_amps=True):

    # find the threshold level
    _, initial_pgram, _ = SIP(raw_x, raw_y, basis, fs)
    mx, threshold = peak_detect(fs, initial_pgram)

    K2P, rawP, K2a, rawa = [], [], [], []
    alla, allp = [], []
    all_results = []
    for i, a in enumerate(amps):
        if random_amps:
            if flag == "r":
                a = 10**(np.random.uniform(np.log10(1e-5), np.log10(1e-3)))
            elif flag == "a":
                a = 10**(np.random.uniform(np.log10(1e-5), np.log10(1e-3)))

        tf = 1. / truth
        print("period = ", truth)

        # add lcs together
        #         plt.clf()
        #         plt.plot(flux * a, "k.")
        noise = np.random.randn(len(flux)) * 50 * 13**.5 * 1e-6
        #         print 50*13**.5*1e-6, a
        fx = flux * a + noise
        #         plt.plot(fx, "r.")
        #         plt.savefig("Test")
        #         assert 0
        y = fx + raw_y
        SN = np.var(fx) / np.var(raw_y)

        if flag == "r":
            threshold = .1
        elif flag == "a":
            threshold = .1

#         # Calculate time
#         start = time.time()
#         amp2s, s2n, w = SIP(raw_x, y, basis, fs[:1000])
#         end = time.time()
#         print("SIP time = ", (end-start), "s")
#         print("for", len(y), "data points and", len(fs), "freqs")

# calculate SIP
        amp2s, s2n, w = SIP(raw_x, y, basis, fs)
        pgram = s2n
        best_f, best_pgram = peak_detect(fs, pgram)  # find peaks
        print("recovered period", 1. / best_f)
        s = 0  # success indicator
        alla.append(a)
        allp.append(truth)
        all_results.append(best_f)

        print(tf - threshold * tf, best_f, tf + threshold * tf)
        if tf - threshold * tf < best_f and best_f < tf + threshold * tf:
            K2P.append(truth)
            K2a.append(a)
            print("success!", "\n")
            s = 1

        # calculate periodogram of raw light curve
        y = np.array([_y.astype("float64") for _y in y])
        raw_x = np.array([_raw_x.astype("float64") for _raw_x in raw_x])

        # Calculate time
        start = time.time()
        model = LombScargle().fit(raw_x, y, np.ones_like(y) * 1e-5)
        end = time.time()
        print("LS time = ", (end - start) * 1e3, "ms")
        print("for", len(y), "data points and", len(fs), "freqs")
        assert 0
        #         # Calculate time
        #         start = time.time()
        #         model = LombScargle().fit(raw_x, y, np.ones_like(y)*1e-5)
        #         end = time.time()
        #         print("SIP time = ", (end-start)*1e3, "ms")
        #         print("for", len(y), "data points and", len(fs), "freqs")
        #         assert 0

        model = LombScargle().fit(raw_x, y, np.ones_like(y) * 1e-5)
        period = 1. / fs
        pg = model.periodogram(period)
        best_f2, best_pg2 = peak_detect(fs, pg)

        if tf - threshold * tf < best_f2 and best_f2 < tf + threshold * tf:
            rawP.append(truth)
            rawa.append(a)

        if plot:
            plt.clf()
            plt.subplot(2, 1, 1)
            plt.plot(raw_x, y, "k.")
            plt.plot(raw_x, fx, color="g")
            plt.title("$\mathrm{Amp = %s, P = %.3f}$" % (a, (1. / tf)))
            plt.subplot(2, 1, 2)
            plt.axvline(best_f, color="r", linestyle="-")
            plt.axvline(tf, color="k", linestyle="--")
            print("best f = ", best_f)
            print("true f = ", tf)
            print(tf - threshold * tf, tf + threshold * tf)
            c = "b"
            if s == 1:
                c = "m"
            plt.plot(fs, pgram, color=c, label="$\mathrm{SIP$}")
            plt.savefig("../injections/sine/%s_%s_result_%s" %
                        (str(n).zfill(2), str(i).zfill(2), flag))
            # n is the period index, i is the amplitude index
            print("%s_%s_result_%s" % (str(n).zfill(2), str(i).zfill(2), flag))
#             raw_input('enter')
    return np.array(K2a), np.array(K2P), np.array(rawa), np.array(rawP), \
            np.array(alla), np.array(allp), np.array(all_results)
Ejemplo n.º 30
0
def LS(x, y, fs):
    ps = 1. / fs
    model = LombScargle().fit(x, y, np.ones_like(y) * 1e-5)
    return model.periodogram(ps)