Esempio n. 1
0
def recover_set(args):
    start, stop, N = args

    # load the data and compute initial sip
    fname = "data/ktwo201121245-c01_lpd-lc.fits"
    x, y, basis, _ = load_K2_data(fname)

    rotation = False
    fs = np.arange(10, 280, 1e-1) * 1e-6
    if rotation:
        fs = 1. / (np.linspace(1., 70., 1000) * 24 * 3600)
    s2n, amps2, w = SIP(x, y, basis, fs)

    # parallelisation parameters
    #     start = int(sys.argv[1])
    #     stop = int(sys.argv[2])
    #     N = int(sys.argv[3])

    # recover injections using SIP
    injection_fnames = range(N)  # names for file saves
    recovered, recovered_amps = recover_SIP(fname,
                                            injection_fnames,
                                            fs,
                                            amps2,
                                            start,
                                            stop,
                                            plot=False,
                                            rotation=rotation)
Esempio n. 2
0
def inj(fname, ifs, a_s, rotation=False):
    """
    inject sine wave into lc
    fname: "path/to/file/filename" - should be a K2 fits file
    ifs: an array of frequencies to inject
    a_s: an array of amplitudes to inject
    fs: the SIP freq array
    if rotation == True then save the output with a _r at the end
    returns the recovered frequencies and amplitudes
    """
    N = len(ifs)
    true_f, true_a = np.zeros_like(ifs), np.zeros_like(a_s)
    x, y, basis, _ = load_K2_data(fname)
    for i, f in enumerate(ifs):  # loop over frequencies
        print(i, "of", N)
        print("injection frequency = ", f)
        iy = y + inject(x, y, f, a_s[i])  # inject sinewave
        if rotation:
            np.savetxt("injections/{0}_r.txt".format(str(i).zfill(5)),
                       np.transpose((x, iy)))
        else:
            np.savetxt("injections/{0}.txt".format(str(i).zfill(5)),
                       np.transpose((x, iy)))
        true_f[i] = f  # save the truths and the ids
        true_a[i] = a_s[i]
    data = np.vstack((np.arange(N), true_f, true_a))
    if rotation:
        np.savetxt("truths_r.txt", data.T)
    else:
        np.savetxt("truths.txt", data.T)
Esempio n. 3
0
def find_spikes(fnames):
    for fname in fnames:
        eid = fname[15:24]
        print eid
        x, y, basis = load_K2_data(eid)
        fs = np.arange(40, 55, 1e-1) * 1e-6
        _, pg, _ = K2pgram(x, y, basis, fs)
        f = h5py.File("spikes/spike_%s.h5" % eid, "w")
        data = f.create_dataset("pgram", (len(fs), 2))
        data[:, 0] = fs
        data[:, 1] = pg
        f.close()
Esempio n. 4
0
def iterative_prewhiten(N):
    # prewhiten 10 times
    x, y, basis, _ = load_K2_data(fname)
    fs = np.arange(10, 300, 1e-1) * 1e-6
    s2n, amp2s, w = SIP(x, y, basis, fs)  # calculate SIP

    # find the top N peaks
    peak_fs, peaks_as = detect_all_peaks(fs, amp2s)
    peakN = np.sort(peaks_as)[-N:]
    peak_f = np.array([peak_fs[peaks_as == i][0] for i in peakN])

    # prewhiten
    for peak in peak_f[::-1]:  # highest peak to lowest
        y = prewhiten(x, y, peak, basis)
    return y
Esempio n. 5
0
def find_value(fnames):
    s2ns, eids = [], []
    for i, fname in enumerate(fnames):
        eid = fname[15:24]
        print eid, i, "of", len(fnames)
        x, y, basis = load_K2_data(eid)
        f = 47.2281
        # construct arrays
        AT = np.concatenate((basis, np.ones((3, len(y)))), axis=0)
        ATA = np.dot(AT, AT.T)
        _, s2n, _ = eval_freq(x, y, f, AT, ATA)
        s2ns.append(s2n)
        eids.append(eid)
#     f = h5py.File("s2ns.h5", "w")
#     data = f.create_dataset("s2n",  (len(s2ns), 2))
#     data[:, 0] = np.array(eids)
#     data[:, 1] = np.array(s2ns)
#     f.close()
    return s2ns
Esempio n. 6
0
def recover_SIP(template_id,
                inj_fnames,
                fs,
                oa2,
                start,
                stop,
                plot=False,
                subtract_baseline=True,
                rotation=False):
    """
    Find frequency and amplitude of the highest peak in the SIP
    fname: the name of the target used for injection
    inj_fnames: the names of the injected lc files
    fs: a grid of frequencies
    oamp2s: the original sip, before sine wave injection
    """
    recovered, recovered_amps = [], []  # array of freq of the highest peak
    _, _, basis, _ = load_K2_data(template_id)  # load original lc
    for i, fname in enumerate(inj_fnames[start:stop]):  # loop over injections
        print(i, "of", len(inj_fnames[start:stop]))
        if rotation:
            ix, iy = \
                np.genfromtxt("injections/{0}_r.txt".format(str(fname).zfill(5))).T
        else:
            ix, iy = \
                np.genfromtxt("injections/{0}.txt".format(str(fname).zfill(5))).T
        print("computing SIP")
        s2n, amps2, w = SIP(ix, iy, basis, fs)  # compute a sip
        if subtract_baseline:  # subtract the original sip

            astero_f = 2.131e-4  # this is a hack for normalising the sip.
            # specific to this target only!
            peaks_f, peaks_a = detect_all_peaks(fs, amps2)
            find_nearest_ind = lambda arr, val: np.abs(arr - val).argmin()
            astero_a2 = peaks_a[find_nearest_ind(peaks_f, astero_f)]
            opeaks_f, opeaks_a = detect_all_peaks(fs, oa2)
            astero_a1 = opeaks_a[find_nearest_ind(opeaks_f, astero_f)]
            ratio = astero_a1 / astero_a2
            amps2 = amps2 * ratio - oa2
        peak_f, peak_a = peak_detect(fs, amps2)  # find the highest peak

        print(peak_f)
        recovered.append(peak_f)
        recovered_amps.append(peak_a)
        if plot:
            plt.clf()
            plt.plot(fs, amps2)
            plt.axvline(peak_f, color="r")
            plt.savefig("{0}".format(str(fname).zfill(5)))

    # save the results
    rf, ra = np.array(recovered), np.array(recovered_amps)
    data = np.vstack((inj_fnames[start:stop], rf, ra))
    if rotation:
        np.savetxt(
            "recovered_{0}_{1}_r.txt".format(
                str(start).zfill(5),
                str(stop).zfill(5)), data.T)
    else:
        np.savetxt(
            "recovered_{0}_{1}.txt".format(
                str(start).zfill(5),
                str(stop).zfill(5)), data.T)
    return rf, ra
Esempio n. 7
0
        ids.append(data[0])
        rfs.append(data[1])
        ras.append(data[2])
    ids = np.array([i for j in ids for i in j])
    rfs = np.array([i for j in rfs for i in j])
    ras = np.array([i for j in ras for i in j])
    true_fs = true_fs[:len(rfs)]
    true_as = true_as[:len(rfs)]
    print(len(true_fs), "injected, ", len(rfs), "found", "\n")
    assert len(true_fs) == len(rfs)

    # find the successful recoveries
    # find the recovered fs and the recovered, true fs
    rec_f, rec_a, true_rec_f, true_rec_a = \
            success_list(rfs, ras, true_fs, true_as, 1e-6)

    resids = np.abs(rec_f - true_rec_f)
    rms = (np.mean((rec_f - true_rec_f)**2))**.5
    plt.clf()
    plt.hist(resids)
    plt.savefig("test")
    print("std = ", np.std(resids)*1e6, rms*1e6, "uHz")

    fname = "data/ktwo201121245-c01_lpd-lc.fits"
    _, _, _, med = load_K2_data(fname)
    true_rec_a = np.log10(true_rec_a * 1e6)  # convert to log ppm
    true_as = np.log10(true_as * 1e6)

    # make a histogram
    histo(true_rec_f, true_rec_a, true_fs, true_as, 20)
Esempio n. 8
0
import numpy as np
from recover import inj
import sys
from K2misc import load_K2_data

N = int(sys.argv[1])  # the number of injections
rotation = False

ifs = np.random.uniform(10e-6, 270e-6, N)  # injected freqs (astero)
if rotation:
    ifs = 1./(np.random.uniform(1, 30, N) * 24 * 3600)  # injected freqs (rot)

fname = "data/ktwo201121245-c01_lpd-lc.fits"
x, y, basis, med = load_K2_data(fname)
a_s = 10**np.random.uniform(-1, 2, N) * 1e-6 # convert to ppm

inj(fname, ifs, a_s, rotation=rotation)