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 select_candidates(field, selection_criteria, num_fit_attempts=10): """ Select candidates from a field given the log10(selection criteria) from mongodb. The current selection scheme is to first select on eta, then to sanity check with delta chi-squared by making sure it's positive and >10. """ eta_cut = 10**selection_criteria light_curves = [] for ccd in field.ccds.values(): logger.info("Starting with CCD {}".format(ccd.id)) chip = ccd.read() ####### APW @ MDM #print("Total:", len(chip.sources.read(field="matchedSourceID"))) #cdtn = "(ngoodobs > 10) " #& ((ngoodobs/nobs) > 0.5)" #print("Condition:", len(chip.sources.readWhere(cdtn, field="matchedSourceID"))) #continue ######################## cdtn = ("(ngoodobs > {}) & (vonNeumannRatio > 0.0) & " "(vonNeumannRatio < {}) & ((ngoodobs/nobs) > 0.5)") cdtn = cdtn.format(min_number_of_good_observations, eta_cut) source_ids = chip.sources.readWhere(cdtn, field="matchedSourceID") logger.info("\tSelected {} pre-candidates from PDB"\ .format(len(source_ids))) for source_id in source_ids: # APW: TODO -- this is still the biggest time hog!!! It turns # out it's still faster than reading the whole thing into # memory, though! light_curve = ccd.light_curve(source_id, barebones=True, clean=True) # If light curve doesn't have enough clean observations, skip it if light_curve != None and \ len(light_curve) < min_number_of_good_observations: continue # Compute the variability indices for the cleaned light curve try: ind_names = ["eta", "delta_chi_squared", "j", "k", "sigma_mu"] indices = pa.compute_variability_indices(light_curve, indices=ind_names) except ValueError: logger.warning("Failed to compute variability indices for " "light curve! {0}".format(light_curve)) return False light_curve.indices = indices light_curve.tags = [] light_curve.features = {} if light_curve.sdss_type() == "galaxy": light_curve.tags.append("galaxy") continue # If the object is not a Galaxy or has no SDSS data, try to get # the SDSS colors to see if it passes the Richards et al. # QSO color cut. sdss_colors = light_curve.sdss_colors("psf") qso_status = richards_qso(sdss_colors) if sdss_colors != None and qso_status: light_curve.tags.append("qso") candidate_status = pa.iscandidate(light_curve, lower_eta_cut=eta_cut) if candidate_status == "candidate" and \ "qso" not in light_curve.tags: light_curve.tags.append("candidate") light_curves.append(light_curve) continue if candidate_status == "subcandidate" and \ light_curve.indices["eta"] < eta_cut and not qso_status: # Try to do period analysis with AOV try: peak_period = light_curve.features["aov_period"] peak_power = light_curve.features["aov_power"] except KeyError: try: fp = pa.findPeaks_aov(light_curve.mjd.copy(), light_curve.mag.copy(), light_curve.error.copy(), 3, 1., 2.*light_curve.baseline, 1., 0.1, 20) except ZeroDivisionError: continue peak_period = fp["peak_period"][0] peak_power = max(fp["peak_period"]) light_curve.features["aov_period"] = peak_period light_curve.features["aov_power"] = peak_power if (peak_period < 2.*light_curve.baseline): if peak_power > 25.: light_curve.tags.append("variable star") if "subcandidate" in light_curve.tags: light_curve.tags.pop(light_curve.tags\ .index("subcandidate")) if light_curve not in light_curves: light_curves.append(light_curve) ccd.close() return light_curves