def test_fit_microlensing_event(): true_params = {"u0" : 0.3, "t0" : 100., "tE" : 20., "m0" : 15.} light_curve = SimulatedLightCurve(mjd=np.linspace(0., 200., 300.), mag=15., error=[0.1]) light_curve.addMicrolensingEvent(**true_params) params = analyze.fit_microlensing_event(light_curve) plt.errorbar(light_curve.mjd, light_curve.mag, light_curve.error, color="k", marker=".") plt.plot(light_curve.mjd, analyze.microlensing_model(params, light_curve.mjd), "r-") plt.save()
def test_stetson_k(): import copy from ptf.simulation.simulatedlightcurve import SimulatedLightCurve mjd = np.linspace(0., 500., 150) sigmas = 10.**np.random.uniform(-2, -3, size=150) light_curve = SimulatedLightCurve(mjd, error=sigmas) # Make a flat light curve + microlensing event, compute J light_curve.addMicrolensingEvent(t0=250.) assert 0.7 < stetson_k(light_curve) < 0.9
def test_stetson_k(): import copy from ptf.simulation.simulatedlightcurve import SimulatedLightCurve mjd = np.linspace(0., 500., 150) sigmas = 10.**np.random.uniform(-2, -3, size=150) light_curve = SimulatedLightCurve(mjd, error=sigmas) # Make a flat light curve + microlensing event, compute J light_curve.addMicrolensingEvent(t0=250.) assert 0.7 < stetson_k(light_curve) < 0.9
def test_eta(): from ptf.simulation.simulatedlightcurve import SimulatedLightCurve mjd = np.linspace(0., 500., 150) sigmas = 10.**np.random.uniform(-2, -3, size=150) light_curve = SimulatedLightCurve(mjd, error=sigmas) print eta(light_curve) # Make a flat light curve + microlensing event, compute J light_curve.addMicrolensingEvent(t0=250.) print eta(light_curve)
def test_eta(): from ptf.simulation.simulatedlightcurve import SimulatedLightCurve mjd = np.linspace(0., 500., 150) sigmas = 10.**np.random.uniform(-2, -3, size=150) light_curve = SimulatedLightCurve(mjd, error=sigmas) print eta(light_curve) # Make a flat light curve + microlensing event, compute J light_curve.addMicrolensingEvent(t0=250.) print eta(light_curve)
def test_compute_variability_indices(): # Here we're really just seeing how long it takes to run... from ptf.simulation.simulatedlightcurve import SimulatedLightCurve mjd = np.linspace(0., 500., 200) sigmas = 10.**np.random.uniform(-2, -3, size=200) light_curve = SimulatedLightCurve(mjd, error=sigmas) light_curve.addMicrolensingEvent() import time a = time.time() for ii in range(100): idx = compute_variability_indices(light_curve) print time.time() - a
def test_compute_variability_indices(): # Here we're really just seeing how long it takes to run... from ptf.simulation.simulatedlightcurve import SimulatedLightCurve mjd = np.linspace(0., 500., 200) sigmas = 10.**np.random.uniform(-2, -3, size=200) light_curve = SimulatedLightCurve(mjd, error=sigmas) light_curve.addMicrolensingEvent() import time a = time.time() for ii in range(100): idx = compute_variability_indices(light_curve) print time.time() - a
def test_fit_microlensing_event(): true_params = {"u0": 0.3, "t0": 100., "tE": 20., "m0": 15.} light_curve = SimulatedLightCurve(mjd=np.linspace(0., 200., 300.), mag=15., error=[0.1]) light_curve.addMicrolensingEvent(**true_params) params = analyze.fit_microlensing_event(light_curve) plt.errorbar(light_curve.mjd, light_curve.mag, light_curve.error, color="k", marker=".") plt.plot(light_curve.mjd, analyze.microlensing_model(params, light_curve.mjd), "r-") plt.save()
def select_light_curves(field, N=1000): """ Fetch a sample of light curves from the photometric database from the specified Field. Parameters ---------- field : Field Represents a PTF Field N : int The number of light curves to fetch from each CCD """ if not isinstance(field, pdb.Field): raise ValueError("field parameter must be a Field object") light_curves = [] for ccdid, ccd in field.ccds.items(): logging.debug("CCD {}".format(ccdid)) chip = ccd.read() # Read in all of the source IDs for this chip source_ids = chip.sources.col("matchedSourceID") # Shuffle them about / randomize the order np.random.shuffle(source_ids) count = 0 for sid in source_ids: # Get a LightCurve object for this source id on this ccd lc = field.ccds[0].light_curve(sid, clean=True) # If the light curve has more than 25 observations, include it # HACK alert if len(lc.mjd) > 25: light_curve = SimulatedLightCurve(lc.mjd, mag=lc.mag, error=lc.error) light_curves.append(light_curve) count += 1 if count >= N: break ccd.close() return light_curves
def example_light_curves(field, u0s=[], limiting_mags=[]): """ Given a field, a list of microlensing event impact parameters, and a list of limiting magnitudes, generate a grid of sample light curves with simulated microlensing events. """ num_u0_bins = len(u0s) num_mag_bins = len(limiting_mags) - 1 fig, axes = plt.subplots(num_u0_bins, num_mag_bins, figsize=(30, 30)) file_base = "sample_light_curves_field{:06d}_u0_{}_m{}".format( field.id, "-".join(map(str, u0s)), "-".join(map( str, limiting_mags))) + ".{ext}" plot_filename = os.path.join("plots", "detectionefficiency", file_base.format(ext="png")) all_ylims = [] for ii, limiting_mag1 in enumerate(limiting_mags[:-1]): limiting_mag2 = limiting_mags[ii + 1] # Hack to shrink the range of magnitudes to select from diff = limiting_mag2 - limiting_mag1 limiting_mag1 += diff / 2. limiting_mag2 -= diff / 4. ylims = [] light_curve = None for jj, u0 in enumerate(u0s): logger.debug("limiting_mags = {:.2f},{:.2f}, u0 = {:.2f}".format( limiting_mag1, limiting_mag2, u0)) # Get the axis object for these indices in the grid ax = axes[ii, jj] if light_curve == None: # Get a random CCD ccd_key = field.ccds.keys()[np.random.randint(len(field.ccds))] ccd = field.ccds[ccd_key] # Get the chip object for this CCD chip = ccd.read() # Read information from the 'sources' table read_wheres = [ "(ngoodobs > 100)", "(stetsonJ < 100)", "(stetsonJ > 0)", "(vonNeumannRatio > 1.0)", "(referenceMag < {:.3f})".format(limiting_mag2) ] sources = chip.sources.readWhere(" & ".join(read_wheres)) source_ids = sources["matchedSourceID"] counter = 0 while True: idx = np.random.randint(len(source_ids)) source_id = source_ids[idx] light_curve = ccd.light_curve(source_id, clean=True, barebones=True) counter += 1 if len(light_curve.mjd) > 100: break elif counter >= len(source_ids): logger.error("Light curve not found!") sys.exit(0) sim_light_curve = SimulatedLightCurve(light_curve.mjd, light_curve.mag, light_curve.error) #for tE in [1.0, 10., 100.]: for tE in [20.]: sim_light_curve.reset() sim_light_curve.addMicrolensingEvent(t0=np.mean( sim_light_curve.mjd), u0=u0, tE=tE) sim_light_curve.plot(ax=ax) ylims.append(ax.get_ylim()) if ii == 0: ax.set_title("$u_0$={:.2f}".format(u0), size=36, y=1.1) if jj == (num_mag_bins - 1): ax.set_ylabel("R={:.2f}".format( float(sources[idx]["referenceMag"])), size=36, rotation="horizontal") ax.yaxis.set_label_position("right") plt.setp(ax.get_xticklabels(), visible=False) plt.setp(ax.get_yticklabels(), visible=False) if jj == 0: plt.setp(ax.get_yticklabels(), visible=True, size=24) ax.set_ylabel(r"$R$", size=38) if ii == (num_u0_bins - 1): ax.set_xlabel(r"Time", size=34) #plt.setp(ax.get_xticklabels(), visible=True, size=24) all_ylims.append(ylims) for ii, row in enumerate(all_ylims): bottoms = [x[0] for x in row] tops = [y[1] for y in row] for jj in range(len(u0s)): axes[ii, jj].set_ylim(max(bottoms), min(tops)) fig.subplots_adjust(hspace=0.1, wspace=0.1, right=0.88) fig.savefig(plot_filename)
def example_light_curves(field, u0s=[], limiting_mags=[]): """ Given a field, a list of microlensing event impact parameters, and a list of limiting magnitudes, generate a grid of sample light curves with simulated microlensing events. """ num_u0_bins = len(u0s) num_mag_bins = len(limiting_mags)-1 fig, axes = plt.subplots(num_u0_bins, num_mag_bins, figsize=(30,30)) file_base = "sample_light_curves_field{:06d}_u0_{}_m{}".format(field.id, "-".join(map(str,u0s)), "-".join(map(str,limiting_mags))) + ".{ext}" plot_filename = os.path.join("plots", "detectionefficiency", file_base.format(ext="png")) all_ylims = [] for ii, limiting_mag1 in enumerate(limiting_mags[:-1]): limiting_mag2 = limiting_mags[ii+1] # Hack to shrink the range of magnitudes to select from diff = limiting_mag2 - limiting_mag1 limiting_mag1 += diff/2. limiting_mag2 -= diff/4. ylims = [] light_curve = None for jj, u0 in enumerate(u0s): logger.debug("limiting_mags = {:.2f},{:.2f}, u0 = {:.2f}".format(limiting_mag1,limiting_mag2,u0)) # Get the axis object for these indices in the grid ax = axes[ii, jj] if light_curve == None: # Get a random CCD ccd_key = field.ccds.keys()[np.random.randint(len(field.ccds))] ccd = field.ccds[ccd_key] # Get the chip object for this CCD chip = ccd.read() # Read information from the 'sources' table read_wheres = ["(ngoodobs > 100)", "(stetsonJ < 100)", "(stetsonJ > 0)", "(vonNeumannRatio > 1.0)", "(referenceMag < {:.3f})".format(limiting_mag2)] sources = chip.sources.readWhere(" & ".join(read_wheres)) source_ids = sources["matchedSourceID"] counter = 0 while True: idx = np.random.randint(len(source_ids)) source_id = source_ids[idx] light_curve = ccd.light_curve(source_id, clean=True, barebones=True) counter += 1 if len(light_curve.mjd) > 100: break elif counter >= len(source_ids): logger.error("Light curve not found!") sys.exit(0) sim_light_curve = SimulatedLightCurve(light_curve.mjd, light_curve.mag, light_curve.error) #for tE in [1.0, 10., 100.]: for tE in [20.]: sim_light_curve.reset() sim_light_curve.addMicrolensingEvent(t0=np.mean(sim_light_curve.mjd), u0=u0, tE=tE) sim_light_curve.plot(ax=ax) ylims.append(ax.get_ylim()) if ii == 0: ax.set_title("$u_0$={:.2f}".format(u0), size=36, y=1.1) if jj == (num_mag_bins-1): ax.set_ylabel("R={:.2f}".format(float(sources[idx]["referenceMag"])), size=36, rotation="horizontal") ax.yaxis.set_label_position("right") plt.setp(ax.get_xticklabels(), visible=False) plt.setp(ax.get_yticklabels(), visible=False) if jj == 0: plt.setp(ax.get_yticklabels(), visible=True, size=24) ax.set_ylabel(r"$R$", size=38) if ii == (num_u0_bins-1): ax.set_xlabel(r"Time", size=34) #plt.setp(ax.get_xticklabels(), visible=True, size=24) all_ylims.append(ylims) for ii,row in enumerate(all_ylims): bottoms = [x[0] for x in row] tops = [y[1] for y in row] for jj in range(len(u0s)): axes[ii,jj].set_ylim(max(bottoms), min(tops)) fig.subplots_adjust(hspace=0.1, wspace=0.1, right=0.88) fig.savefig(plot_filename)