Esempio n. 1
0
def test_ln_prior():
    # First, test that using an out of bounds value breaks it
    mjd = np.linspace(0., 100., 50)
    sigma = np.random.uniform(0.05, 0.15, size=len(mjd))

    light_curve = SimulatedLightCurve(mjd=mjd, error=sigma, mag=15)
    light_curve.add_microlensing_event(t0=50., u0=0.3, tE=20)

    print "Good values:", ln_prior([15, 0.3, 50., 20.], light_curve.mag,
                                   light_curve.mjd)
    print "Test values:", ln_prior([15, 0.3, 50., 900.], light_curve.mag,
                                   light_curve.mjd)

    print "Bad m0:", ln_prior([25, 0.3, 50., 20.], light_curve.mag,
                              light_curve.mjd)

    print "Bad u0:", ln_prior([15, -0.3, 50., 20.], light_curve.mag,
                              light_curve.mjd)
    print "Bad u0:", ln_prior([15, 1.9, 50., 20.], light_curve.mag,
                              light_curve.mjd)

    print "Bad t0:", ln_prior([15, 0.3, 500., 20.], light_curve.mag,
                              light_curve.mjd)
    print "Bad t0:", ln_prior([15, 0.3, -50., 20.], light_curve.mag,
                              light_curve.mjd)

    print "Bad tE:", ln_prior([15, 0.3, 50., 0.2], light_curve.mag,
                              light_curve.mjd)
    print "Bad tE:", ln_prior([15, 0.3, 50., 2000.], light_curve.mag,
                              light_curve.mjd)
Esempio n. 2
0
def test_slice_peak2():
    ''' Test the slicing around peak '''
    light_curve = pdb.get_light_curve(100024, 11, 2693, clean=True) # bad data

    ml_chisq = 1E6
    ml_params = None
    for ii in range(10):
        params = pa.fit_microlensing_event(light_curve)
        new_chisq = params["result"].chisqr

        if new_chisq < ml_chisq:
            ml_chisq = new_chisq
            ml_params = params

    import matplotlib.pyplot as plt

    ax = plt.subplot(211)
    sliced_lc = light_curve.slice_mjd(ml_params["t0"].value-ml_params["tE"].value, ml_params["t0"].value+ml_params["tE"].value)
    print len(sliced_lc)
    sliced_lc.plot(ax)

    ax2 = plt.subplot(212)
    sliced_ml_params = pa.fit_microlensing_event(sliced_lc)
    new_sliced_light_curve = pa.fit_subtract_microlensing(sliced_lc, fit_data=sliced_ml_params)
    new_sliced_light_curve.plot(ax2)
    ax2.set_title(r"Med. Err: {0}, $\sigma$: {1}".format(np.median(sliced_lc.error), np.std(new_sliced_light_curve.mag)))

    plt.savefig("plots/test_slice_peak2_bad_data.png")

    # Now do with a simulated event
    from ptf.lightcurve import SimulatedLightCurve
    light_curve = SimulatedLightCurve(mjd=light_curve.mjd, mag=15, error=0.1)
    light_curve.add_microlensing_event(u0=0.1, t0=55600, tE=40)

    ml_chisq = 1E6
    ml_params = None
    for ii in range(10):
        params = pa.fit_microlensing_event(light_curve)
        new_chisq = params["result"].chisqr

        if new_chisq < ml_chisq:
            ml_chisq = new_chisq
            ml_params = params

    plt.clf()
    ax = plt.subplot(211)
    sliced_lc = light_curve.slice_mjd(ml_params["t0"].value-ml_params["tE"].value, ml_params["t0"].value+ml_params["tE"].value)
    print len(sliced_lc)
    sliced_lc.plot(ax)

    ax2 = plt.subplot(212)
    sliced_ml_params = pa.fit_microlensing_event(sliced_lc)
    new_sliced_light_curve = pa.fit_subtract_microlensing(sliced_lc, fit_data=sliced_ml_params)
    new_sliced_light_curve.plot(ax2)
    ax2.set_title(r"Med. Err: {0}, $\sigma$: {1}".format(np.median(sliced_lc.error), np.std(new_sliced_light_curve.mag)))

    plt.savefig("plots/test_slice_peak2_sim.png")
Esempio n. 3
0
def test_magnitude_model():
    mjd = np.arange(0., 100., 0.2)
    sigma = np.zeros_like(mjd) + 0.01

    u0 = 0.1
    t0 = 50.
    tE = 20
    mag = magnitude_model([15, u0, t0, tE], mjd)

    fig = plt.figure()
    ax = fig.add_subplot(111)

    ax.errorbar(mjd, mag, sigma)

    light_curve = SimulatedLightCurve(mjd=mjd, error=sigma, mag=15)
    light_curve.add_microlensing_event(t0=t0, u0=u0, tE=tE)
    light_curve.plot(ax)

    plt.show()
Esempio n. 4
0
def test_slice_peak():
    ''' Test the slicing around peak '''
    light_curve = pdb.get_light_curve(100024, 11, 2693, clean=True) # bad data

    ml_chisq = 1E6
    ml_params = None
    for ii in range(10):
        params = pa.fit_microlensing_event(light_curve)
        new_chisq = params["result"].chisqr

        if new_chisq < ml_chisq:
            ml_chisq = new_chisq
            ml_params = params

    import matplotlib.pyplot as plt

    for ii in [1,2,3]:
        ax = plt.subplot(3,1,ii)
        sliced_lc = light_curve.slice_mjd(ml_params["t0"].value-ii*ml_params["tE"].value, ml_params["t0"].value+ii*ml_params["tE"].value)
        sliced_lc.plot(ax)
    plt.savefig("plots/test_slice_peak_bad_data.png")

    # Now do with a simulated event
    from ptf.lightcurve import SimulatedLightCurve
    light_curve = SimulatedLightCurve(mjd=light_curve.mjd, mag=15, error=0.1)
    light_curve.add_microlensing_event(u0=0.1)

    ml_chisq = 1E6
    ml_params = None
    for ii in range(10):
        params = pa.fit_microlensing_event(light_curve)
        new_chisq = params["result"].chisqr

        if new_chisq < ml_chisq:
            ml_chisq = new_chisq
            ml_params = params

    plt.clf()
    for ii in [1,2,3]:
        ax = plt.subplot(3,1,ii)
        sliced_lc = light_curve.slice_mjd(ml_params["t0"].value-ii*ml_params["tE"].value, ml_params["t0"].value+ii*ml_params["tE"].value)
        sliced_lc.plot(ax)
    plt.savefig("plots/test_slice_peak_sim.png")
Esempio n. 5
0
def test_ln_likelihood():
    mjd = np.linspace(0., 100., 50)
    sigma = np.random.uniform(0.05, 0.15, size=len(mjd))

    light_curve = SimulatedLightCurve(mjd=mjd, error=sigma, mag=15)
    light_curve.add_microlensing_event(t0=50., u0=0.3, tE=20)

    likelihood1 = ln_likelihood(p=[15, 0.3, 50., 20.],
                                mag=light_curve.mag,
                                mjd=mjd,
                                sigma=sigma)
    likelihood2 = ln_likelihood(p=[15, 0.3, 1., 20.],
                                mag=light_curve.mag,
                                mjd=mjd,
                                sigma=sigma)
    likelihood3 = ln_likelihood(p=[15, 0.3, 50., 50.],
                                mag=light_curve.mag,
                                mjd=mjd,
                                sigma=sigma)

    print "Likelihood with model at correct peak: {}".format(likelihood1)
    print "Likelihood with model way off peak: {}".format(likelihood2)
    print "Likelihood with model at peak, wrong tE: {}".format(likelihood3)
Esempio n. 6
0
def make_light_curve_figure(light_curve,
                            sampler,
                            filename=None,
                            title="",
                            x_axis_labels=True,
                            y_axis_labels=True,
                            chains=True):
    chain = sampler.flatchain

    fig = plt.figure(figsize=(10, 12))
    fig.suptitle(title, fontsize=23)
    gs = gridspec.GridSpec(2, 1, height_ratios=[1, 2])
    ax = plt.subplot(gs[1])
    ax2 = plt.subplot(gs[0])

    # "best" parameters
    max_idx = np.ravel(sampler.lnprobability).argmax()
    best_m0, best_u0, best_t0, best_tE = chain[max_idx]

    mjd = np.arange(light_curve.mjd.min() - 1000.,
                    light_curve.mjd.max() + 1000., 0.2)

    if chains:
        first_t0 = None
        for ii in range(150):
            walker_idx = np.random.randint(sampler.k)
            chain = sampler.chain[walker_idx][-100:]
            probs = sampler.lnprobability[walker_idx][-100:]
            link_idx = np.random.randint(len(chain))

            prob = probs[link_idx]
            link = chain[link_idx]
            m0, u0, t0, tE = link

            if prob / probs.max() > 2:
                continue

            # More than 100% error
            if np.any(
                    np.fabs(link - np.mean(chain, axis=0)) /
                    np.mean(chain, axis=0) > 0.25) or tE < 8 or tE > 250.:
                continue

            if np.fabs(t0 - best_t0) > 20.:
                continue

            if first_t0 == None:
                first_t0 = t0
            s_light_curve = SimulatedLightCurve(mjd=mjd,
                                                error=np.zeros_like(mjd),
                                                mag=m0)
            s_light_curve.add_microlensing_event(t0=t0, u0=u0, tE=tE)
            ax.plot(s_light_curve.mjd - first_t0,
                    s_light_curve.mag,
                    linestyle="-",
                    color="#666666",
                    alpha=0.1)
            #ax_inset.plot(s_light_curve.mjd-first_t0, s_light_curve.mag, "r-", alpha=0.05)

    mean_m0, mean_u0, mean_t0, mean_tE = np.median(chain, axis=0)
    std_m0, std_u0, std_t0, std_tE = np.std(chain, axis=0)

    print("m0 = ", mean_m0, std_m0)
    print("u0 = ", mean_u0, std_u0)
    print("tE = ", mean_tE, std_tE)
    print("t0 = ", mean_t0, std_t0)

    light_curve.mjd -= mean_t0
    zoomed_light_curve = light_curve.slice_mjd(-3. * mean_tE, 3. * mean_tE)
    if len(zoomed_light_curve) == 0:
        mean_t0 = light_curve.mjd.min()
        mean_tE = 10.

        light_curve.mjd -= mean_t0
        zoomed_light_curve = light_curve.slice_mjd(-3. * mean_tE, 3. * mean_tE)

    zoomed_light_curve.plot(ax)
    light_curve.plot(ax2)
    ax.set_xlim(-3. * mean_tE, 3. * mean_tE)
    #ax.text(-2.5*mean_tE, np.min(s_light_curve.mag), r"$u_0=${u0:.3f}$\pm${std:.3f}".format(u0=u0, std=std_u0) + "\n" + r"$t_E=${tE:.1f}$\pm${std:.1f} days".format(tE=mean_tE, std=std_tE), fontsize=19)

    if chains:
        fig.text(
            0.15,
            0.48,
            r"$u_0=${u0:.3f}$\pm${std:.3f}".format(u0=mean_u0, std=std_u0) +
            "\n" + r"$t_E=${tE:.1f}$\pm${std:.1f} days".format(tE=mean_tE,
                                                               std=std_tE),
            fontsize=19)

    if y_axis_labels:
        ax.set_ylabel(r"$R$ (mag)", fontsize=24)
    if x_axis_labels:
        ax.set_xlabel(r"time-$t_0$ [days]", fontsize=24)

    # Now plot the "best fit" line in red
    if chains:
        s_light_curve = SimulatedLightCurve(mjd=mjd,
                                            error=np.zeros_like(mjd),
                                            mag=best_m0)
        s_light_curve.add_microlensing_event(t0=best_t0,
                                             u0=best_u0,
                                             tE=best_tE)
        ax.plot(s_light_curve.mjd - best_t0,
                s_light_curve.mag,
                "r-",
                alpha=0.6,
                linewidth=2)

    if y_axis_labels:
        ax2.set_ylabel(r"$R$ (mag)", fontsize=24)

    #light_curve.plot(ax_inset)
    #ax_inset.set_ylim(s_light_curve.mag.min()+abs(s_light_curve.mag.min()-m0)/5., s_light_curve.mag.min()-0.05)
    #ax_inset.set_xlim(-0.3*tE, 0.3*tE)
    #ax_inset.set_xticklabels([])
    #ax_inset.set_yticklabels([])

    fig.subplots_adjust(hspace=0.1, left=0.12)

    if filename == None:
        return fig, axes
    else:
        fig.savefig(os.path.join("plots/fit_events", filename))
Esempio n. 7
0
File: try_svm.py Progetto: adrn/PTF
all_data = np.zeros((len(sourceIDs), Nfeatures))

for ii, f in enumerate(features):
    if f == "sigma_mu":
        pdb_f1, pdb_f2 = pdb_index_name[f]
        all_data[:, ii] = goodSources[pdb_f1] / goodSources[pdb_f2]
    else:
        pdb_f = pdb_index_name[f]
        all_data[:, ii] = goodSources[pdb_f]

np.save("/home/aprice-whelan/tmp/all_data.npy", all_data)

random_sourceIDs = sourceIDs[np.random.randint(len(sourceIDs), size=Nsources)]

training_data = np.zeros((Nsources, Ntrials, Nfeatures))
for ii, sourceID in enumerate(random_sourceIDs):
    d = sourceData.readWhere("matchedSourceID == {0}".format(sourceID))
    mjd = d['mjd']
    mag = d['mag']
    err = d['magErr']

    for trial in range(Ntrials):
        lc = SimulatedLightCurve(mjd=mjd, mag=mag, error=err)
        lc.add_microlensing_event()
        stats = compute_variability_indices(lc, indices=features)
        training_data[ii, trial, :] = np.array([stats[x] for x in features])

np.save("/home/aprice-whelan/tmp/training_data.npy", training_data)

chip.close()
Esempio n. 8
0
def test_iscandidate(plot=False):
    ''' Use test light curves to test selection:
        - Periodic
        - Bad data
        - Various simulated events
        - Flat light curve
        - Transients (SN, Nova, etc.)
    '''

    np.random.seed(10)

    logger.setLevel(logging.DEBUG)
    from ptf.lightcurve import SimulatedLightCurve
    import ptf.db.mongodb as mongo

    db = mongo.PTFConnection()

    logger.info("---------------------------------------------------")
    logger.info(greenText("Periodic light curves"))
    logger.info("---------------------------------------------------")

    # Periodic light curves
    periodics = [(4588, 7, 13227), (4588, 2, 15432), (4588, 9, 17195), (2562, 10, 28317), (4721, 8, 11979), (4162, 2, 14360)]

    for field_id, ccd_id, source_id in periodics:
        periodic_light_curve = pdb.get_light_curve(field_id, ccd_id, source_id, clean=True)
        periodic_light_curve.indices = pa.compute_variability_indices(periodic_light_curve, indices=["eta", "delta_chi_squared", "j", "k", "sigma_mu"])
        assert pa.iscandidate(periodic_light_curve, lower_eta_cut=10**db.fields.find_one({"_id" : field_id}, {"selection_criteria" : 1})["selection_criteria"]["eta"]) in ["subcandidate" , False]
        if plot: plot_lc(periodic_light_curve)

    logger.info("---------------------------------------------------")
    logger.info(greenText("Bad light curves"))
    logger.info("---------------------------------------------------")

    # Bad data
    bads = [(3756, 0, 14281), (1983, 10, 1580)]

    for field_id, ccd_id, source_id in bads:
        bad_light_curve = pdb.get_light_curve(field_id, ccd_id, source_id, clean=True)
        bad_light_curve.indices = pa.compute_variability_indices(bad_light_curve, indices=["eta", "delta_chi_squared", "j", "k", "sigma_mu"])
        assert not pa.iscandidate(bad_light_curve, lower_eta_cut=10**db.fields.find_one({"_id" : field_id}, {"selection_criteria" : 1})["selection_criteria"]["eta"])
        if plot: plot_lc(bad_light_curve)

    logger.info("---------------------------------------------------")
    logger.info(greenText("Simulated light curves"))
    logger.info("---------------------------------------------------")

    # Simulated light curves
    for field_id,mjd in [(4721,periodic_light_curve.mjd)]:
        for err in [0.01, 0.05, 0.1]:
            logger.debug("field: {0}, err: {1}".format(field_id,err))
            light_curve = SimulatedLightCurve(mjd=mjd, mag=15, error=[err])
            light_curve.indices = pa.compute_variability_indices(light_curve, indices=["eta", "delta_chi_squared", "j", "k", "sigma_mu"])
            assert not pa.iscandidate(light_curve, lower_eta_cut=10**db.fields.find_one({"_id" : field_id}, {"selection_criteria" : 1})["selection_criteria"]["eta"])

            light_curve.add_microlensing_event(u0=np.random.uniform(0.2, 0.8), t0=light_curve.mjd[int(len(light_curve)/2)], tE=light_curve.baseline/8.)
            light_curve.indices = pa.compute_variability_indices(light_curve, indices=["eta", "delta_chi_squared", "j", "k", "sigma_mu"])
            if plot:
                plt.clf()
                light_curve.plot()
                plt.savefig("plots/tests/{0}_{1}.png".format(field_id,err))
            assert pa.iscandidate(light_curve, lower_eta_cut=10**db.fields.find_one({"_id" : field_id}, {"selection_criteria" : 1})["selection_criteria"]["eta"])

    logger.info("---------------------------------------------------")
    logger.info(greenText("Transient light curves"))
    logger.info("---------------------------------------------------")

    # Transients (SN, Novae)
    transients = [(4564, 0, 4703), (4914, 6, 9673), (100041, 1, 4855), (100082, 5, 7447), (4721, 8, 3208), (4445, 7, 11458),\
                  (100003, 6, 10741), (100001, 10, 5466), (4789, 6, 11457), (2263, 0, 3214), (4077, 8, 15293), (4330, 10, 6648), \
                  (4913, 7, 13436), (100090, 7, 2070), (4338, 2, 10330), (5171, 0, 885)]

    for field_id, ccd_id, source_id in transients:
        transient_light_curve = pdb.get_light_curve(field_id, ccd_id, source_id, clean=True)
        logger.debug(transient_light_curve)
        transient_light_curve.indices = pa.compute_variability_indices(transient_light_curve, indices=["eta", "delta_chi_squared", "j", "k", "sigma_mu"])
        assert pa.iscandidate(transient_light_curve, lower_eta_cut=10**db.fields.find_one({"_id" : field_id}, {"selection_criteria" : 1})["selection_criteria"]["eta"])
        if plot: plot_lc(transient_light_curve)