Example #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)
Example #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")
Example #3
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)
Example #4
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()
Example #5
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()
Example #6
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")
Example #7
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)
Example #8
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)
Example #9
0
File: try_svm.py Project: 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()
Example #10
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))
Example #11
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))
Example #12
0
File: try_svm.py Project: 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()
Example #13
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)
Example #14
0
def fit_candidates(Nburn, Nsamples, Nwalkers, seed=None):
    final_candidates = ["PTFS1206i", "PTFS1216bt", "PTFS1217cv"]

    fig1 = plt.figure(figsize=(22,8))
    fontsize = 16
    gs = gridspec.GridSpec(2, 3, height_ratios=[1,2])

    u0s,t0s,m0s,tEs = [],[],[],[]
    du0s,dt0s,dm0s,dtEs = [],[],[],[]

    # Make 3-panel figure with candidates
    for ii,PTFID in enumerate(final_candidates):
        if ii == 2:
            Nsamples = 1000

        chains = True
        filename = "data/candidates/lc_{0}.dat".format(PTFID)
        figtext = ptfid_to_longname[PTFID] #candidate["long_ptf_name"]
        light_curve = lightcurve_from_filename(filename)

        if ii == 0:
            y_axis_labels = True
        else:
            y_axis_labels = False

        if ii == 1:
            x_axis_labels = True
        else:
            x_axis_labels = False

        logger.info("Fitting {0}".format(PTFID))
        sampler = fit.fit_model_to_light_curve(light_curve, nwalkers=Nwalkers, nsamples=Nsamples, nburn_in=Nburn, seed=seed)
        chain = sampler.flatchain
        print "Mean acceptance fraction: {0:.3f}".format(np.mean(sampler.acceptance_fraction))

        ax = plt.subplot(gs[1,ii])
        ax2 = plt.subplot(gs[0,ii])

        # "best" parameters
        flatchain = np.vstack(sampler.chain)
        best_m0, best_u0, best_t0, best_tE = np.median(flatchain, axis=0)

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

        first_t0 = None
        for jj in range(100):
            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="#ababab", alpha=0.05, marker=None)
            #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)

        m0s.append(mean_m0)
        dm0s.append(std_m0)
        t0s.append(mean_t0+54832.) # HJD to MJD
        dt0s.append(std_t0)
        tEs.append(mean_tE)
        dtEs.append(std_tE)
        u0s.append(mean_u0)
        du0s.append(std_u0)
        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)

        ylim, xlim = ax.get_ylim(), ax.get_xlim()
        dx, dy = (xlim[1]-xlim[0]), (ylim[0]-ylim[1])
        ax.text(xlim[0]+dx/20., ylim[1]+dy/8.,
                 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=fontsize)

        ylim, xlim = ax2.get_ylim(), ax2.get_xlim()
        dx, dy = (xlim[1]-xlim[0]), (ylim[0]-ylim[1])
        if ii == 2:
            ax2.text((xlim[0]+xlim[1])/2.-30,ylim[1]+dy/8.,figtext,fontsize=fontsize)
        else:
            ax2.text(xlim[0]+dx/20., ylim[1]+dy/8., figtext,fontsize=fontsize)

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

        # Now plot the "best fit" line in red
        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, "k-", alpha=0.6, linewidth=3)

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

    import astropy.table as at
    from astropy.io import ascii
    tbl = at.Table([at.Column(data=u0s, name="u0"),
                    at.Column(data=du0s, name="e_u0"),
                    at.Column(data=tEs, name="tE"),
                    at.Column(data=dtEs, name="e_tE"),
                    at.Column(data=t0s, name="t0"),
                    at.Column(data=dt0s, name="e_t0"),
                    at.Column(data=m0s, name="m0"),
                    at.Column(data=dm0s, name="e_m0")])

    ascii.write(tbl, output=os.path.join("plots/fit_events/metadata.tex"), Writer=ascii.Latex)

    fig1.subplots_adjust(hspace=0.15, wspace=0.15, left=0.06, right=0.95, top=0.98, bottom=0.09)
    fig1.savefig(os.path.join("plots/fit_events", "fig10.eps"))
Example #15
0
def microlensing_event_sim():
    """ Create the multi-panel figure with simulated microlensing events for a single
        'typical' PTF light curve.
    """

    #field = pdb.Field(100062, "R")
    #ccd = field.ccds[1]
    #chip = ccd.read()
    #sources = chip.sources.readWhere("(ngoodobs > 300) & (vonNeumannRatio > 1.235)")
    #light_curve = ccd.light_curve(sources["matchedSourceID"][np.random.randint(0, len(sources))], clean=True)
    #print sources["matchedSourceID"]
    light_curve = pdb.get_light_curve(100062, 1, 13268, clean=True)

    num = 4
    fig, axes = plt.subplots(num,1, sharex=True, figsize=(11,15))

    sim_light_curve = SimulatedLightCurve(mjd=light_curve.mjd, mag=light_curve.mag, error=light_curve.error)

    t0 = sim_light_curve.mjd[int(len(sim_light_curve.mjd)/2)]

    kwarg_list = [None, {"u0" : 1.0, "t0" : t0, "tE" : 20},
                        {"u0" : 0.5, "t0" : t0, "tE" : 20},
                        {"u0" : 0.01, "t0" : t0, "tE" : 20}]

    args_list = [(16.66, "a)"), (16.4, "b)"), (16.0, "c)"), (12, "d)")]
    args_list2 = [16.68, 16.5, 16.2, 13]

    for ii in range(num):
        axes[ii].xaxis.set_visible(False)

        if ii != 0:
            #sim_light_curve.reset()
            sim_light_curve = SimulatedLightCurve(mjd=light_curve.mjd, mag=light_curve.mag, error=light_curve.error)
            sim_light_curve.add_microlensing_event(**kwarg_list[ii])

        sim_light_curve.plot(axes[ii], marker="o", ms=3, alpha=0.75)

        axes[ii].axhline(14.3, color='r', linestyle="--")

        if kwarg_list[ii] == None:
            u0_str = ""
        else:
            u0 = kwarg_list[ii]["u0"]
            u0_str = r"$u_0={:.2f}$".format(u0)
        #axes[ii].set_ylabel(u0_str, rotation="horizontal")

        #for tick in axes[ii].yaxis.get_major_ticks():
        #    tick.label.set_fontsize(tick_font_size)

        if ii == 0:
            [tick.set_visible(False) for jj,tick in enumerate(axes[ii].get_yticklabels()) if jj % 2 != 0]

        if ii % 2 != 0:
            axes[ii].yaxis.tick_right()
        else:
            axes[ii].yaxis.set_label_position("right")

        if ii == 0:
            axes[ii].set_ylabel(r"$R$", rotation="horizontal", fontsize=26)
            axes[ii].yaxis.set_label_position("left")

        axes[ii].text(56100, *args_list[ii], fontsize=24)
        axes[ii].text(56100, args_list2[ii], u0_str, fontsize=24)

    #fig.suptitle("PTF light curve with simulated microlensing events", fontsize=24)

    for ax in fig.axes:
        for ticklabel in ax.get_yticklabels():
            ticklabel.set_fontsize(18)

    fig.subplots_adjust(hspace=0.0, left=0.1, right=0.9)
    fig.savefig(os.path.join(pg.plots_path, "paper_figures", "simulated_events.pdf"), bbox_inches="tight", facecolor="white")