def get_result(temp_dir, seed): save_file = temp_dir + "/final%d.npy" % seed if os.path.exists(save_file): res = np.load(save_file) if res.size == 0: return None else: return res else: interp = generate_and_return() z, t0, x0, x1, c, ston, lc = realise_light_curve(seed) if ston < 5 or ston > 10: np.save(save_file, np.array([])) return None try: chainf, parametersf, means, cov = get_gaussian_fit(z, t0, x0, x1, c, lc, seed, temp_dir, interp, type="iminuit") mcmc_file = temp_dir + "/mcmc%d.npy" % seed if os.path.exists(mcmc_file): chainf2 = np.load(mcmc_file) else: chainf2, _, _, _ = get_gaussian_fit(z, t0, x0, x1, c, lc, seed, temp_dir, interp, type="mcmc") np.save(mcmc_file, chainf2) chain, parameters = get_posterior(z, t0, lc, seed, temp_dir, interp) assert is_fit_good(t0, x0, x1, c, means, cov) assert not is_unconstrained(chain, parameters) except Exception as e: print(e) np.save(save_file, np.array([])) return None plot_results(chain, parameters, chainf, chainf2, parametersf, t0, x0, x1, c, temp_dir, seed, interp) muf = chainf[:, -1] muf2 = chainf2[:, -1] mu = chain[:, -1] mean2 = np.mean(chain[:, :-1], axis=0) cov2 = np.cov(chain[:, :-1].T) chain2 = np.random.multivariate_normal(mean2, cov2, size=int(1e6)) chain2, _ = add_mu_to_chain(interp, chain2, parameters[:-1]) mu2 = chain2[:, -1] mus = [np.mean(mu), np.mean(mu2), np.mean(muf), np.mean(muf2)] stds = [np.std(mu), np.std(mu2), np.std(muf), np.std(muf2)] res = np.array([seed, z, t0, x0, x1, c, ston] + mus + stds) np.save(save_file, res) return res
def random_obs(temp_dir, seed): np.random.seed(seed) interp = generate_and_return() x1 = np.random.normal() # colour = np.random.normal(scale=0.1) colour = 0 x0 = 1e-5 # t0 = np.random.uniform(low=1000, high=2000) t0 = 1000 z = np.random.uniform(low=0.1, high=1.0) # deltat = np.random.uniform(low=-20, high=0) # num_obs = np.random.randint(low=10, high=40) num_obs = 20 deltat = -35 filename = temp_dir + "/save_%d.npy" % seed if not os.path.exists(filename): ts = np.arange(t0 + deltat, (t0 + deltat) + 5 * num_obs, 5) times = np.array([[t, t + 0.05, t + 0.1, t + 0.2] for t in ts]).flatten() bands = [b for t in ts for b in ["desg", "desr", "desi", "desz"]] gains = np.ones(times.shape) skynoise = np.random.uniform(low=20, high=800) * np.ones(times.shape) zp = 30 * np.ones(times.shape) zpsys = ["ab"] * times.size obs = Table({"time": times, "band": bands, "gain": gains, "skynoise": skynoise, "zp": zp, "zpsys": zpsys}) model = sncosmo.Model(source="salt2") p = {"z": z, "t0": t0, "x0": x0, "x1": x1, "c": colour} model.set(z=z) print(seed, " Vals are ", p) lc = sncosmo.realize_lcs(obs, model, [p])[0] ston = (lc["flux"] / lc["fluxerr"]).max() model.set(t0=t0, x1=x1, c=colour, x0=x0) try: res, fitted_model = sncosmo.fit_lc( lc, model, ["t0", "x0", "x1", "c"], guess_amplitude=False, guess_t0=False ) except ValueError: return np.nan, np.nan, x1, colour, num_obs, ston, deltat, z, 0 fig = sncosmo.plot_lc(lc, model=fitted_model, errors=res.errors) fig.savefig(temp_dir + os.sep + "lc_%d.png" % seed, bbox_inches="tight", dpi=300) my_model = PerfectRedshift([lc], [z], t0, name="posterior%d" % seed) sampler = EnsembleSampler(temp_dir=temp_dir, num_burn=400, num_steps=1500) c = ChainConsumer() my_model.fit(sampler, chain_consumer=c) map = {"x0": "$x_0$", "x1": "$x_1$", "c": "$c$", "t0": "$t_0$"} parameters = [map[a] for a in res.vparam_names] mu1 = get_mu_from_chain(interped, c.chains[-1], c.parameters[-1]) c.parameteers[-1].append(r"$\mu$") c.chains[-1] = np.hstack((c.chains[-1], mu1[:, None])) chain2 = np.random.multivariate_normal(res.parameters[1:], res.covariance, size=int(1e5)) chain2 = np.hstack((chain2, get_mu_from_chain(interp, chain2, parameters)[:, None])) c.add_chain(chain2, parameters=parameters, name="Gaussian") figfilename = filename.replace(".npy", ".png") c.plot(filename=figfilename, truth={"$t_0$": t0, "$x_0$": x0, "$x_1$": x1, "$c$": colour}) means = [] stds = [] isgood = ( (np.abs(x1 - res.parameters[3]) < 4) & (np.abs(colour - res.parameters[4]) < 2) & (res.parameters[2] > 0.0) ) isgood *= 1.0 for i in range(len(c.chains)): a = c.chains[i][:, -1] means.append(a.mean()) stds.append(np.std(a)) diffmu = np.diff(means)[0] diffstd = np.diff(stds)[0] np.save(filename, np.array([diffmu, diffstd, ston, 1.0 * isgood])) else: vals = np.load(filename) diffmu = vals[0] diffstd = vals[1] ston = vals[2] isgood = vals[3] return diffmu, diffstd, x1, colour, num_obs, ston, deltat, z, isgood
def get_result(temp_dir, zps, seed, scatter, special=False, full=True): save_file = temp_dir + "/final%d.npy" % seed if os.path.exists(save_file): res = np.load(save_file) if res.size == 0: return None else: return res else: interp = generate_and_return() if full: cosmology = None else: cosmology = WMAP9 z, t0, x0, x1, c, ston, lc = realise_light_curve(temp_dir, zps, seed, scatter=scatter, cosmology=cosmology) if ston < 4: print("Failed ", z) np.save(save_file, np.array([])) return None try: chainf, parametersf, means, cov = get_gaussian_fit(z, t0, x0, x1, c, lc, seed, temp_dir, interp, type="iminuit") mcmc_file = temp_dir + "/mcmc%d.npy" % seed if os.path.exists(mcmc_file): chainf2 = np.load(mcmc_file) else: chainf2, _, _, _ = get_gaussian_fit(z, t0, x0, x1, c, lc, seed, temp_dir, interp, type="mcmc") np.save(mcmc_file, chainf2) if full: nestle_file = temp_dir + "/nestle%d.npy" % seed if os.path.exists(nestle_file): chainf3 = np.load(nestle_file) else: chainf3, _, _, _ = get_gaussian_fit(z, t0, x0, x1, c, lc, seed, temp_dir, interp, type="nestle") np.save(nestle_file, chainf3) chain, parameters = get_posterior(z, t0, lc, seed, temp_dir, interp, special) assert not is_unconstrained(chain, parameters) assert is_fit_good(t0, x0, x1, c, means, cov) except Exception as e: print(e) np.save(save_file, np.array([])) return None if special: try: plot_results(chain, parameters, chainf, chainf2, chainf3, parametersf, t0, x0, x1, c, temp_dir, seed, interp) except Exception as e: print(e) pass muf = chainf[:, -1] muf2 = chainf2[:, -1] if full: muf3 = chainf3[:, -1] mu = chain[:, -1] mean2 = np.mean(chain[:, :-1], axis=0) cov2 = np.cov(chain[:, :-1].T) chain2 = np.random.multivariate_normal(mean2, cov2, size=int(1e6)) chain2, _ = add_mu_to_chain(interp, chain2, parameters[:-1]) mu2 = chain2[:, -1] mus = [np.mean(mu), np.mean(mu2), np.mean(muf), np.mean(muf2), np.mean(muf3)] stds = [np.std(mu), np.std(mu2), np.std(muf), np.std(muf2), np.std(muf3)] res = np.array([seed, z, t0, x0, x1, c, ston] + mus + stds + [skew(mu)]) else: mus = [np.mean(muf), np.mean(muf2)] stds = [np.std(muf), np.std(muf2)] res = np.array([seed, z, t0, x0, x1, c, ston] + mus + stds) # print("Faster", res) np.save(save_file, res) return res
c.configure_bar(shade=True) c.plot(filename=surface, figsize=(7, 7)) if False: fig = sncosmo.plot_lc(lcs[0], model=fitted_model, errors=res.errors) fig.savefig(temp_dir + os.sep + "lc_simple.png", bbox_inches="tight", dpi=300) alpha = 0.14 beta = 3.15 c2 = ChainConsumer() means = [] stds = [] print("Add chains") for i in range(len(c.chains)): chain = c.chains[i] apparent_interp = generate_and_return() x0s = chain[:, c.parameters[i].index("$x_0$")] x1s = chain[:, c.parameters[i].index("$x_1$")] cs = chain[:, c.parameters[i].index("$c$")] a = apparent_interp(x1s, cs, grid=False) - (0.4 * np.log10(x0s / 1e-5)) a += alpha * x1s - beta * cs means.append(a.mean()) stds.append(np.std(a)) c2.add_chain(a, parameters=[r"$\mu+M$"], name=c.names[i]) print(means, stds, np.diff(means), np.diff(stds)) actual = apparent_interp(x1, colour, grid=False) - (0.4 * np.log10(x0 / 1e-5)) actual += alpha * x1 - beta * colour c2.plot(filename=mu_simple, figsize=(7, 4), truth=[actual], legend=True)