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")
def ml_parameter_distributions(overwrite=False): """ Compute distributions of microlensing event parameter fits by doing my MCMC fit to all candidate, qso, not interesting, bad data, transient, and supernova tagged light curves. There are about ~4500 NEW: Hmm...maybe forget this, I keep breaking navtara """ ptf = mongo.PTFConnection() light_curve_collection = ptf.light_curves Nwalkers = 100 Nsamples = 1000 Nburn = 100 searched = [] max_parameters = [] for tag in ["candidate", "qso", "not interesting", "transient", "supernova", "bad data"]: for lc_document in list(light_curve_collection.find({"tags" : tag})): if str(lc_document["_id"]) in searched and not overwrite: continue light_curve = pdb.get_light_curve(lc_document["field_id"], lc_document["ccd_id"], lc_document["source_id"], clean=True) sampler = fit.fit_model_to_light_curve(light_curve, nwalkers=Nwalkers, nsamples=Nsamples, nburn_in=Nburn) max_idx = np.ravel(sampler.lnprobability).argmax() # Turn this on to dump plots for each light curve #fit.make_chain_distribution_figure(light_curve, sampler, filename="{0}_{1}_{2}_dists.png".format(lc_document["field_id"], lc_document["ccd_id"], lc_document["source_id"])) #fit.make_light_curve_figure(light_curve, sampler, filename="{0}_{1}_{2}_lc.png".format(lc_document["field_id"], lc_document["ccd_id"], lc_document["source_id"])) max_parameters.append(list(sampler.flatchain[max_idx])) searched.append(str(lc_document["_id"])) if len(searched) == 500: break if len(searched) == 500: break max_parameters = np.array(max_parameters) fig, axes = plt.subplots(2, 2, figsize=(14,14)) for ii, ax in enumerate(np.ravel(axes)): if ii == 3: bins = np.logspace(min(max_parameters[:,ii]), max(max_parameters[:,ii]), 25) ax.hist(max_parameters[:,ii], bins=bins, color="k", histtype="step") else: ax.hist(max_parameters[:,ii], color="k", histtype="step") ax.set_yscale("log") fig.savefig("plots/fit_events/all_parameters.png")
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")
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)
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")