def simulate_LSST(id, p, a, path, noise, tmin=3, tmax=30, dur=10, plot=False): """ Photometry with precision of 10 ppm (?). Uneven time sampling that ranges (uniformily) from 3 to 30 days (?). Lasting 10 years (?). id: id of the star. p: rotation period in seconds a: amplitude in ppm path: path to save files tmin, tmax: min and max intervals between observations in days. dur: duration in years. noise: noise level (ppm). Default is 10 ppm. """ print(id) id = str(int(id)).zfill(4) # # only make new simulation if one doesn't already exist # if os.path.exists("simulations/{0}".format(id)): # return # The time array # x = np.cumsum(np.random.uniform(tmin, tmax, 1000)) # x = x[x < dur * 365.25] # x = generate_visits() # x += -x[0] x, depth = np.genfromtxt("{0}_cadence.txt".format(path)).T # sin2incl = np.random.uniform(np.sin(0)**2, np.sin(np.pi/2)**2) # incl = np.arcsin(sin2incl**.5) # tau = np.exp(np.random.uniform(np.log(p), np.log(10*p))) # res0, res1 = mklc.mklc(x, incl=incl, tau=tau, p=p) res0, res1 = mklc.mklc(x, p=p) nspot, ff, amp_err = res0 time, area_tot, dF_tot, dF_tot0 = res1 noise_free_y = dF_tot0 / np.median(dF_tot0) - 1 y = dF_tot0 / np.median(dF_tot0) - 1 + noise*1e-6 * np.random.randn(len(x)) yerr = np.ones_like(y) * noise * 1e-6 data = np.vstack((x, y, yerr)) np.savetxt("{0}/{1}.txt".format(path, id), data.T) truths = np.array([p, a]) truths = np.vstack((np.array([p]), np.array([a]))) f = open("{0}/all_truths.txt".format(path), "a") np.savetxt(f, truths.T) f.close() if plot: print("plotting light curve") plt.clf() plt.errorbar(x/365.25, y, yerr=yerr, fmt="k.", capsize=0, ecolor=".5") plt.xlabel("$\mathrm{Time~(years)}$") plt.ylabel("$\mathrm{Normalised~Flux}$") plt.xlim(min(x/365.25), max(x/365.25)) plt.subplots_adjust(left=.2, bottom=.12) plt.savefig("{0}/{1}.pdf".format(path, id))
def simulate(id, pmin=0.5, pmax=100.0, amin=1e-3, amax=1e-1, nsim=100, gen_type="s", kepler=False, plot=False): """ pmin and pmax in days, amin and amax in ppm. """ periods = np.exp(np.random.uniform(np.log(pmin), np.log(pmax), nsim)) amps = np.exp(np.random.uniform(np.log(amin), np.log(amax), nsim)) np.savetxt("simulations/true_periods.txt", np.transpose((np.arange(nsim), periods, amps))) thetas = np.zeros((nsim, 5)) thetas[:, 0] = np.exp(np.random.uniform(-6, -4)) thetas[:, 1] = np.exp(np.random.uniform(4, 7)) thetas[:, 2] = np.exp(np.random.uniform(-1.5, 1.5)) thetas[:, 3] = np.exp(np.random.uniform(-11, -8)) thetas[:, 4] = periods # load test star data FIXME: I'm just using the time values # because this is not a quiet star # x, y, yerr = np.genfromtxt("%s_lc.txt" % id).T # time = x # else: time = np.arange(0, x[-1]-x[0], .02043365) time = np.arange(0, 1600, 0.02043365) std = 1e-5 yerr = np.ones_like(time) * std flux = np.zeros_like(time) + np.random.randn(len(time)) * std for i, p in enumerate(periods): print i, "of ", len(periods), "\n" print "amps = ", amps[i] if gen_type == "s": res0, res1 = mklc.mklc(time, p=p) nspot, ff, amp_err = res0 time, area_tot, dF_tot, dF_tot0 = res1 simflux = dF_tot0 / np.median(dF_tot0) - 1 elif gen_type == "GP": k = thetas[i, 0] * ExpSquaredKernel(thetas[i, 1]) * ExpSine2Kernel( thetas[i, 2], thetas[i, 4] ) + WhiteKernel(std) gp = george.gp() gp.compute(time, yerr) simflux = gp.sample() np.savetxt("simulations/%s_%s.txt" % (str(int(i)).zfill(4), gen_type), np.transpose((time, simflux))) if plot: plt.clf() plt.plot(time, simflux * amps[i] + flux, "k.") plt.savefig("simulations/%s%s" % (i, gen_type)) plt.title("p = %s, a = %s" % (p, amps[i])) assert 0
def inject_star_spots(raw_x, periods): amps = [] for i, p in enumerate(periods): # generate injection nspot = 200 incl = np.pi*5./12. amp = 1. tau = 30.5 res0, res1 = mklc(raw_x, nspot, incl, amp, tau, p) time = res1[0] flux = res1[2]/np.median(res1[2]) - 1 np.savetxt("injections/%s_lc.txt" % str(i).zfill(4), np.transpose((time, flux))) amps.append(res0[-1]) np.savetxt("injections/truth.txt", np.transpose((range(len(periods)), periods, np.array(amps))))
def simulate(EPIC, c=1, pmin=.5, pmax=100., amin=1e-3, amax=1e-1, nsim=1000, usereal=False): """ pmin and pmax in days, amin and amax in ppm. """ periods = np.exp(np.random.uniform(np.log(pmin), np.log(pmax), nsim)) amps = np.exp(np.random.uniform(np.log(amin), np.log(amax), nsim)) np.savetxt("true_periods.txt", np.transpose((np.arange(1000), periods, amps))) # load test star data fname = "hlsp_k2sff_k2_lightcurve_%s-c0%s_kepler_v1_llc.fits" % (EPIC, c) dname = "/export/bbq1/angusr/data/vanderburgC%s/%s" % (c, fname) data = fitsio.read(dname) time, raw, flux = [np.zeros(len(data)) for i in range(3)] for i in range(len(data)): time[i] = data[i][0] raw[i] = data[i][1] flux[i] = data[i][2] - 1 if usereal: # fit and remove straight line AT = np.vstack((time, np.ones_like(time))) ATA = np.dot(AT, AT.T) m, c = np.dot(np.linalg.inv(ATA), np.dot(AT, flux)) flux -= (m*time+c) std = np.std(flux) flux = np.zeros_like(time) + np.random.randn(len(time))*std for i, p in enumerate(periods): print i, "of ", len(periods), "\n" print "amps = ", amps[i] res0, res1 = mklc.mklc(time, p=p) nspot, ff, amp_err = res0 time, area_tot, dF_tot, dF_tot0 = res1 simflux = dF_tot0 / np.median(dF_tot0) - 1 np.savetxt("simulations/%s.txt" % i, np.transpose((time, simflux))) plt.clf() plt.plot(time, simflux*amps[i]+flux, "k.") plt.savefig("simulations/%s" % i) plt.title("p = %s, a = %s" % (p, amps[i]))
def generate_lcs(x, y, id, N, nspot_min=50, nspot_max=500, incl_min=0, incl_max=np.pi/4., amp_min=1, amp_max=100, pmin=.5, pmax=90, tau_min=5, tau_max=20): """ Generate N fake light curves based on the parameter limits. x (array): times of k2 lc. N (int): number of lcs. returns 2d array of light curves, the k2 light curve and a dictionary of true parameters. """ params = [nspot_min, nspot_max, incl_min, incl_max, amp_min*rvar, amp_max*rvar, pmin, pmax, tau_min, tau_max] nspots, incl, periods, amps, tau = injection_params(N, params) true_params = {'nspots': nspots, 'incl': incl, 'periods': periods, 'amps': amps, 'tau': tau} xarr = np.zeros((len(x), N)) yarr = np.zeros((len(x), N)) for i in range(N): print(i, "of", N) res0, res1 = mklc(x, nspot=nspots[i], incl=incl[i], tau=tau[i], p=periods[i]) med = np.median(res1[2, :]) ys = (res1[2, :] / med - 1) * amps[i] # median normalise and scale yarr[:, i] = ys + y xarr[:, i] = x if N == 1: return xarr.T[0], yarr.T[0], x, y, true_params # save the results np.savetxt("lcs.txt", yarr.T) np.savetxt("xs.txt", xarr.T) np.savetxt("truth.txt", np.vstack((nspots, incl, periods, amps, tau)).T) return xarr, yarr, true_params
def simulate_LSST(x, depth, filt, id, p, a, path, noise, tmin=3, tmax=30, dur=10, plot=False): """ Photometry with precision of 10 ppm (?). Uneven time sampling that ranges (uniformily) from 3 to 30 days (?). Lasting 10 years (?). id: id of the star. p: rotation period in seconds a: amplitude in ppm path: path to save files tmin, tmax: min and max intervals between observations in days. dur: duration in years. noise: noise level (ppm). Default is 10 ppm. """ print(id) id = str(int(id)).zfill(4) sin2incl = np.random.uniform(np.sin(0)**2, np.sin(np.pi/2)**2) incl = np.arcsin(sin2incl**.5) tau = np.exp(np.random.uniform(np.log(p), np.log(10*p))) res0, res1 = mklc.mklc(x, incl=incl, tau=tau, p=p) # res0, res1 = mklc.mklc(x, p=p) nspot, ff, amp_err = res0 time, area_tot, dF_tot, dF_tot0 = res1 noise_free_y = dF_tot0 / np.median(dF_tot0) - 1 y = dF_tot0 / np.median(dF_tot0) - 1 + noise*1e-6 * \ np.random.randn(len(x)) yerr = np.ones(len(y)) * noise * 1e-6 data = np.vstack((x, y, yerr)) np.savetxt("{0}/{1}.txt".format(path, id), data.T) # truths = np.array([p, a]) # truths = np.vstack((np.array([p]), np.array([a]))) # np.savetxt(f, truths.T) f = open("{0}/all_truths.txt".format(path), "a") f.write("{0} {1}\n".format(p, a)) f.close() if plot: u = filt == "u" g = filt == "g" r = filt == "r" i = filt == "i" z = filt == "z" y = filt == "y" print("plotting light curve") plt.clf() plt.errorbar(x[u]/365.25, y[u], yerr=yerr[u], fmt=".", capsize=0, ecolor=".5", color="b", label="u") plt.errorbar(x[g]/365.25, y[g], yerr=yerr[g], fmt=".", capsize=0, ecolor=".5", color="g", label="g") plt.errorbar(x[r]/365.25, y[r], yerr=yerr[r], fmt=".", capsize=0, ecolor=".5", color="r", label="r") plt.errorbar(x[i]/365.25, y[i], yerr=yerr[i], fmt=".", capsize=0, ecolor=".5", color="m", label="i") plt.errorbar(x[z]/365.25, y[z], yerr=yerr[z], fmt=".", capsize=0, ecolor=".5", color="y", label="z") plt.errorbar(x[y]/365.25, y[y], yerr=yerr[y], fmt=".", capsize=0, ecolor=".5", color="k", label="y") plt.xlabel("$\mathrm{Time~(years)}$") plt.ylabel("$\mathrm{Normalised~Flux}$") plt.xlim(min(x/365.25), max(x/365.25)) plt.subplots_adjust(left=.2, bottom=.12) plt.savefig(os.path.join(path, "{}".format(id)))