Exemple #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
Exemple #2
0
def get_multiharmonic_periodogram(x, y, err, nh, hfac=3, ofac=10):
    model = LombScargle(Nterms=nh)
    model.fit(x, y, err)

    pers, p = model.periodogram_auto(nyquist_factor=hfac, oversampling=ofac)

    return np.power(pers, -1), p
Exemple #3
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
Exemple #4
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)))
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
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
Exemple #7
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
Exemple #8
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
Exemple #9
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)
Exemple #10
0
def gatspy_period(lc_g):

    time, mag, error = np.array(lc_g['MJD'].values), np.array(lc_g['MAG_KRON'].values), np.array(lc_g['MAGERR_KRON'].values)

    per_f = (np.max(time) - np.min(time))*10
    periods = np.linspace(.01, per_f, 10000)

    model = LombScargle(fit_offset=True).fit(time, mag, error)
    power = model.score(periods)

    best_per = periods[np.argmax(power)]

    return best_per
Exemple #11
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
Exemple #12
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
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)))
Exemple #14
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()
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 time_period():
    global t
    x = np.array(df['#time'].values.tolist())
    y = params1[0] * np.sin(params1[1] * x - params1[2]) + params1[3]
    model = LombScargle().fit(x, y)
    model.optimizer.period_range = (3, 4)
    t = model.best_period
    print(f'\n\nPeriod of revolution is: {t:.3f} years')
    return
Exemple #17
0
 def ls_period(self, time, magnitude, error, prange=(0.2, 1.4), **kwargs):
     if len(time) > 50:
         model = LombScargleFast().fit(time, magnitude, error)
         periods, power = model.periodogram_auto(nyquist_factor=100)
         model.optimizer.period_range = prange
     else:
         model = LombScargle().fit(time, magnitude, error)
         periods, power = model.periodogram_auto(nyquist_factor=100)
         model.optimizer.period_range = prange
     return type(model).__name__, model.best_period, periods, power
Exemple #18
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)
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))
Exemple #20
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)))
Exemple #21
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
def periodogram():
    x = np.array(df['#time'].values.tolist())
    y1 = params1[0] * np.sin(params1[1] * x - params1[2]) + params1[3]
    y2 = params2[0] * np.sin(params2[1] * x - params2[2]) + params2[3]
    model1 = LombScargle().fit(x, y1)
    model2 = LombScargle().fit(x, y2)
    periods1, power1 = model1.periodogram_auto()
    periods2, power2 = model2.periodogram_auto()
    plt.title("Periodogram")
    plt.plot(periods1, power1, color='red', label='Star 1')
    plt.scatter(periods2, power2, label='Star 2')
    plt.legend(loc="lower right")
    plt.ylabel("Lomb-Scargle Power")
    plt.xlabel("Period (Years)")
    plt.xlim(2, 5)
    plt.show()
    return
Exemple #23
0
    def run_gatspy(self):
        #this is the general simulation - ellc light curves and gatspy periodograms

        #for the multiband gatspy fit
        allObsDates = np.array([])
        allAppMagObs = np.array([])
        allAppMagObsErr = np.array([])
        allObsFilters = np.array([])
        minNobs = 1e10
        if (self.verbose):
            print("in run_gatspy")

        for i, filt in enumerate(self.filters):

            self.EB.LSS[filt] = -999.

            if (self.EB.obsDates[filt][0] is not None
                    and min(self.EB.appMagObs[filt]) > 0):

                #run gatspy for this filter
                drng = max(self.EB.obsDates[filt]) - min(
                    self.EB.obsDates[filt])
                minNobs = min(minNobs, len(self.EB.obsDates[filt]))
                #print("filter, nobs", filt, len(self.EB.obsDates[filt]))
                if (self.useFast and len(self.EB.obsDates[filt]) > 50):
                    model = LombScargleFast(fit_period=True,
                                            silence_warnings=True,
                                            optimizer_kwds={"quiet": True})
                else:
                    model = LombScargle(fit_period=True,
                                        optimizer_kwds={"quiet": True})
                pmin = self.gatspyPeriodMin
                if (self.EB.period < pmin):
                    pmin = 0.1 * self.EB.period
                model.optimizer.period_range = (pmin, drng)
                model.fit(self.EB.obsDates[filt], self.EB.appMagObs[filt],
                          self.EB.appMagObsErr[filt])
                self.EB.LSS[filt] = model.best_period
                self.EB.LSSmodel[filt] = model
                self.EB.maxDeltaMag = max(self.EB.deltaMag[filt],
                                          self.EB.maxDeltaMag)

                #to use for the multiband fit
                allObsDates = np.append(allObsDates, self.EB.obsDates[filt])
                allAppMagObs = np.append(allAppMagObs, self.EB.appMagObs[filt])
                allAppMagObsErr = np.append(allAppMagObsErr,
                                            self.EB.appMagObsErr[filt])
                allObsFilters = np.append(
                    allObsFilters, np.full(len(self.EB.obsDates[filt]), filt))

                if (self.verbose):
                    print('filter = ', filt)
                    print('obsDates = ', self.EB.obsDates[filt][0:10])
                    print('appMagObs = ', self.EB.appMagObs[filt][0:10])
                    print('delta_mag = ', self.EB.deltaMag[filt])
                    print('LSS = ', self.EB.LSS[filt])

        if (len(allObsDates) > 0 and self.doLSM):
            drng = max(allObsDates) - min(allObsDates)
            if (self.useFast and minNobs > 50):
                model = LombScargleMultibandFast(
                    fit_period=True, optimizer_kwds={"quiet": True})
            else:
                model = LombScargleMultiband(Nterms_band=self.n_band,
                                             Nterms_base=self.n_base,
                                             fit_period=True,
                                             optimizer_kwds={"quiet": True})
            pmin = self.gatspyPeriodMin
            if (self.EB.period < pmin):
                pmin = 0.1 * self.EB.period
            model.optimizer.period_range = (pmin, drng)
            model.fit(allObsDates, allAppMagObs, allAppMagObsErr,
                      allObsFilters)
            self.EB.LSM = model.best_period
            self.EB.LSMmodel = model
            if (self.verbose):
                print('LSM =', self.EB.LSM)
periods = np.linspace(0.2, 1.2, 5000)


for i in range(2):
    fig, ax = plt.subplots(2, 2, figsize=(10, 4), sharex='col')
    fig.subplots_adjust(left=0.07, right=0.95, wspace=0.1, bottom=0.15)

    if i == 0:
        ind = np.arange(Nobs) % 5
        y = mags[ind, np.arange(Nobs)]
        f = np.array(filts)[ind]
    else:
        arrs = np.broadcast_arrays(t, mags, dy, filts[:, None])
        t, y, dy, f = map(np.ravel, arrs)

    model1 = LombScargle()
    model1.fit(t, y, dy)
    yfit = model1.predict(tfit, period=period)

    plot_data(ax[0, 0], t, y, dy, f)
    ax[0, 0].plot(tfit / period, yfit, '-', color='gray', lw=4, alpha=0.5)
    ax[0, 1].plot(periods, model1.score(periods), color='gray')
    ax[0, 0].set_xlabel('')

    model2 = LombScargleMultiband(Nterms_base=1, Nterms_band=1)
    model2.fit(t, y, dy, f)
    yfits = model2.predict(tfit, filts=filts[:, None], period=period)

    plot_data(ax[1, 0], t, y, dy, f)
    for j in range(5):
        ax[1, 0].plot(tfit / period, yfits[j])
phasefit = np.linspace(0, 1, 1000)
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]:
Exemple #26
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")
fig = plt.figure(figsize=(10, 4))
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')
Exemple #28
0
for fignum, models in enumerate(([1, 2, 6], [10, 20, 30])):
    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)
# fnum = eval(sys.argv[1])

W = [50, 500, 5000]
scores = np.zeros((N_trials, len(W)))

for ii in range(N_trials):

    for jj in range(len(W)):
        # maximum frequency/min period
        fmax = W[jj]/t.max()
        
        # "data" array
        y = np.random.normal(0, 1.0, t.size)

        ls = LombScargle().fit(t, y, 1.0)
        ls.optimizer.quiet = True
        ls.optimizer.period_range = (1/fmax, t[-1])

        chisq_0 = np.var(y)
        scores[ii, jj] = ls.score(ls.best_period) * chisq_0 * 0.5 * (N_pts-1)

# fout = open("output{0}.dat".format(fnum), "w")

# for ii in range(N_trials):
#     fout.write("{0},{1}\n".format(scores[ii,0], scores[ii,1]))

# fout.close()


# data = data[:100000]
    fig.add_subplot(gs[1, 1])
]

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

# Here we construct some regularization.
Nterms = 20
sigma_r_inv = np.vstack([np.arange(Nterms + 1),
                         np.arange(Nterms + 1)]).T.ravel()[1:]**2
models = [0.5 * sigma_r_inv**2, None]

for i, reg in enumerate(models):
    model = LombScargle(Nterms=Nterms,
                        regularization=reg,
                        regularize_by_trace=False).fit(t, mag, dmag)

    if reg is None:
        label = "unregularized"
    else:
        label = "regularized"

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

    ax[1 + i].plot(periods,
                   model.periodogram(periods),
                   lw=1,
                   c=lines[0].get_color())
    for day_dtdates, day_jdates, day_bjdates, day_values, day_xrayvs in dateparts:
        
        # 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)
Exemple #32
0
fig = plt.figure(figsize=(10, 4))
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')
Exemple #33
0
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)
tfit = rrlyrae.period * phasefit

fig = plt.figure(figsize=(10, 4))
gs = plt.GridSpec(2, 2, left=0.07, right=0.95,
                  wspace=0.15, hspace=0.7,
                  bottom=0.15)
ax = [fig.add_subplot(gs[:, 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()
except ValueError:
    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]
Exemple #36
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
Exemple #37
0
    # fft long cadence data
    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)
Exemple #38
0
    ax.set_ylabel('magnitude')

# Plot the input data
fig, ax = plt.subplots()
plot_data(ax)
ax.set_title('Input Data')
plt.savefig('buildup_1.png')

# Plot the base model
fig, ax = plt.subplots()
plot_data(ax)

t_all = np.ravel(t * np.ones_like(mags))
mags_all = np.ravel(mags)
dy_all = np.ravel(dy * np.ones_like(mags))
basemodel = LombScargle(Nterms=2).fit(t_all, mags_all, dy_all)

period = rrlyrae.period
tfit = np.linspace(0, period, 1000)
base_fit = basemodel.predict(tfit, period=period)

ax.plot(tfit / period, base_fit, color='black', lw=5, alpha=0.5)
ax.set_title('2-term Base Model')

# Plot the band-by-band augmentation
multimodel = LombScargleMultiband(Nterms_base=2, Nterms_band=1)
t1, y1, dy1, f1 = map(np.ravel,
                      np.broadcast_arrays(t, mags, dy, filts[:, None]))
multimodel.fit(t1, y1, dy1, f1)

Exemple #39
0
mod_time=np.linspace(min(time),max(time),1000)

##############################
plt.errorbar(time,rv,yerr=erv,fmt='.')
plt.show()



####EXTRACT PERIOD ESTIMATE##############
N = 10000
periods=np.linspace(0.1,2*max(time),N)
fmin = 1. / periods.max()
fmax = 1. / periods.min()
df = (fmax - fmin) / N

model = LombScargle().fit(time, rv, erv)
power = model.score_frequency_grid(fmin, df, N)
freqs = fmin + df * np.arange(N)

periods=1./freqs
# plot the results
plt.plot(1. / freqs, power)
plt.show()

print('guess p: ', periods[power==max(power)])
print('guess sys v: ',np.mean(rv))
print('guess K: ',np.std(rv-np.mean(rv)))

#rvsys, K, w, ecc, T0, period
labels=['rvsys', 'K', 'w', 'ecc', 'T0', 'period','sig2']
initial=[0,40,90,0.01,15,6.25,20]
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
Exemple #41
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)
Exemple #42
0
                  right=0.95,
                  wspace=0.15,
                  hspace=0.7,
                  bottom=0.15)
ax = [
    fig.add_subplot(gs[:, 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()
Exemple #43
0
def find_cycle(feature,
               strain,
               mouse=None,
               bin_width=15,
               methods='LombScargleFast',
               disturb_t=False,
               gen_doc=False,
               plot=True,
               search_range_fit=None,
               nyquist_factor=3,
               n_cycle=10,
               search_range_find=(2, 26),
               sig=np.array([0.05])):
    """
    Use Lomb-Scargel method on different strain and mouse's data to find the
    best possible periods with highest p-values. The function can be used on
    specific strains and specific mouses, as well as just specific strains
    without specifying mouse number. We use the O(NlogN) fast implementation
    of Lomb-Scargle from the gatspy package, and also provide a way to
    visualize the result.

    Note that either plotting or calculating L-S power doesn't use the same
    method in finding best cycle. The former can use user-specified
    search_range, while the latter uses default two grid search_range.

    Parameters
    ----------
    feature: string in {"AS", "F", "M_AS", "M_IS", "W", "Distance"}
        "AS": Active state probalibity
        "F": Food consumed (g)
        "M_AS": Movement outside homebase
        "M_IS": Movement inside homebase
        "W": Water consumed (g)
        "Distance": Distance traveled
    strain: int
        nonnegative integer indicating the strain number
    mouse: int, default is None
        nonnegative integer indicating the mouse number
    bin_width: int, minute unit, default is 15 minutes
        number of minutes, the time interval for data aggregation
    methods: string in {"LombScargleFast", "LombScargle"}
        indicating the method used in determining periods and best cycle.
        If choose 'LombScargle', 'disturb_t' must be True.
    disturb_t: boolean, default is False
        If True, add uniformly distributed noise to the time sequence which
        are used to fit the Lomb Scargle model. This is to avoid the singular
        matrix error that could happen sometimes.
    plot: boolean, default is True
        If True, call the visualization function to plot the Lomb Scargle
        power versus periods plot. First use the data (either strain specific
        or strain-mouse specific) to fit the LS model, then use the
        search_range_fit as time sequence to predict the corresponding LS
        power, at last draw the plot out. There will also be stars and
        horizontal lines indicating the p-value of significance. Three stars
        will be p-value in [0,0.001], two stars will be p-value in
        [0.001,0.01], one star will be p-value in [0.01,0.05]. The horizontal
        line is the LS power that has p-value of 0.05.
    search_range_fit: list, numpy array or numpy arange, hours unit,
        default is None
        list of numbers as the time sequence to predict the corrsponding
        Lomb Scargle power. If plot is 'True', these will be drawn as the
        x-axis. Note that the number of search_range_fit points can not be
        too small, or the prediction smooth line will not be accurate.
        However the plot will always give the right periods and their LS
        power with 1,2 or 3 stars. This could be a sign to check whether
        search_range_fit is not enough to draw the correct plot.
        We recommend the default None, which is easy to use.
    nyquist_factor: int
        If search_range_fit is None, the algorithm will automatically
        choose the periods sequence.
        5 * nyquist_factor * length(time sequence) / 2 gives the number of
        power and periods used to make LS prediction and plot the graph.
    n_cycle: int, default is 10
        numbers of periods to be returned by function, which have the highest
        Lomb Scargle power and p-value.
    search_range_find: list, tuple or numpy array with length of 2, default is
                       (2,26), hours unit
        Range of periods to be searched for best cycle. Note that the minimum
        should be strictly larger than 0 to avoid 1/0 issues.
    sig: list or numpy array, default is [0.05].
        significance level to be used for plot horizontal line.
    gen_doc: boolean, default is False
        If true, return the parameters needed for visualize the LS power versus
        periods

    Returns
    -------
    cycle: numpy array of length 'n_cycle'
         The best periods with highest LS power and p-values.
    cycle_power: numpy array of length 'n_cycle'
         The corrsponding LS power of 'cycle'.
    cycle_pvalue: numpy array of length 'n_cycle'
         The corrsponding p-value of 'cycle'.
    periods: numpy array of the same length with 'power'
        use as time sequence in LS model to make predictions.Only return when
        gen_doc is True.
    power: numpy array of the same length with 'periods'
        the corresponding predicted power of periods. Only return when
        gen_doc is True.
    sig: list, tuple or numpy array, default is [0.05].
        significance level to be used for plot horizontal line.
        Only return when gen_doc is True.
    N: int
        the length of time sequence in the fit model. Only return when
        gen_doc is True.

    Examples
    -------
    >>> a,b,c = find_cycle(feature='F', strain = 0,mouse = 0, plot=False,)
    >>> print(a,b,c)
    >>> [ 23.98055016   4.81080233  12.00693952   6.01216335   8.0356203
         3.4316698    2.56303353   4.9294791   21.37925713   3.5697756 ]
        [ 0.11543449  0.05138839  0.03853218  0.02982237  0.02275952
        0.0147941  0.01151601  0.00998443  0.00845883  0.0082382 ]
        [  0.00000000e+00   3.29976046e-10   5.39367189e-07   8.10528027e-05
          4.71001953e-03   3.70178834e-01   9.52707020e-01   9.99372657e-01
         9.99999981e-01   9.99999998e-01]

    """
    if feature not in ALL_FEATURES:
        raise ValueError(
            'Input value must in {"AS", "F", "M_AS", "M_IS", "W", "Distance"}')
    if methods not in METHOD:
        raise ValueError(
            'Input value must in {"LombScargleFast","LombScargle"}')

    # get data
    if mouse is None:
        data_all = aggregate_data(feature=feature, bin_width=bin_width)
        n_mouse_in_strain = len(
            set(data_all.loc[data_all['strain'] == strain]['mouse']))
        data = [[] for i in range(n_mouse_in_strain)]
        t = [[] for i in range(n_mouse_in_strain)]
        for i in range(n_mouse_in_strain):
            data[i] = data_all.loc[(data_all['strain'] == strain)
                                   & (data_all['mouse'] == i)][feature]
            t[i] = np.array(
                np.arange(0,
                          len(data[i]) * bin_width / 60, bin_width / 60))

        data = [val for sublist in data for val in sublist]
        N = len(data)
        t = [val for sublist in t for val in sublist]
    else:
        if feature == 'Distance':
            data = aggregate_movement(strain=strain,
                                      mouse=mouse,
                                      bin_width=bin_width)
            N = len(data)
            t = np.arange(0, N * bin_width / 60, bin_width / 60)
        else:
            data = aggregate_interval(strain=strain,
                                      mouse=mouse,
                                      feature=feature,
                                      bin_width=bin_width)
            N = len(data)
            t = np.arange(0, N * bin_width / 60, bin_width / 60)

    y = data

    # fit model
    if disturb_t is True:
        t = t + np.random.uniform(-bin_width / 600, bin_width / 600, N)

    if methods == 'LombScargleFast':
        model = LombScargleFast(fit_period=False).fit(t=t, y=y)
    elif methods == 'LombScargle':
        model = LombScargle(fit_period=False).fit(t=t, y=y)

    # calculate periods' LS power
    if search_range_fit is None:
        periods, power = model.periodogram_auto(nyquist_factor=nyquist_factor)
    else:
        periods = search_range_fit
        power = model.periodogram(periods=search_range_fit)

    # find best cycle
    model.optimizer.period_range = search_range_find
    cycle, cycle_power = model.find_best_periods(return_scores=True,
                                                 n_periods=n_cycle)
    cycle_pvalue = 1 - (1 - np.exp(cycle_power / (-2) * (N - 1)))**(2 * N)

    # visualization
    if plot is True:
        lombscargle_visualize(periods=periods,
                              power=power,
                              sig=sig,
                              N=N,
                              cycle_power=cycle_power,
                              cycle_pvalue=cycle_pvalue,
                              cycle=cycle)

    if gen_doc is True:
        return periods, power, sig, N, cycle, cycle_power, cycle_pvalue

    return cycle, cycle_power, cycle_pvalue
Exemple #44
0
def LS(x, y, fs):
    ps = 1. / fs
    model = LombScargle().fit(x, y, np.ones_like(y) * 1e-5)
    return model.periodogram(ps)