max_k=0.30) sampler = DynestySampler(temp_dir=dir_name) fitter = Fitter(dir_name) fitter.add_model_and_dataset(models[0], data, name="Hinton2017") fitter.add_model_and_dataset(models[1], data, name="EH1998") fitter.set_sampler(sampler) fitter.set_num_walkers(10) fitter.fit(file) if fitter.should_plot(): from chainconsumer import ChainConsumer c = ChainConsumer() pks = {} for posterior, weight, chain, model, data, extra in fitter.load(): c.add_chain(chain, weights=weight, parameters=model.get_labels(), **extra) # params = dict([(p.name, v) for p, v in zip(model.get_active_params(), chain[posterior.argmax(), :])]) # params["om"] = 0.3121 # model.set_data(datas[0].get_data()) # key = f"{model.name}, alpha={params['alpha']:0.4f}" # pks[key] = model.get_model(datas[0].get_data(), params) c.configure(shade=True, bins=0.7) c.plotter.plot(filename=pfn + "_contour.png", truth={ "$\\Omega_m$": 0.3121,
pfn, dir_name, file = setup(__file__) model = TestModel() data = TestDataset() sampler = DynestySampler(temp_dir=dir_name, max_iter=None) fitter = Fitter(dir_name) fitter.add_model_and_dataset(model, data) fitter.set_sampler(sampler) fitter.set_num_walkers(1) fitter.fit(file) if fitter.should_plot(): res, = fitter.load() posterior, weight, chain, evidence, model, data, extra = res import matplotlib.pyplot as plt fig, ax = plt.subplots(nrows=2) ax[0].plot(weight) ax[1].plot(evidence) plt.show() from chainconsumer import ChainConsumer c = ChainConsumer() c.add_chain(chain, weights=weight, parameters=model.get_labels()) c.plotter.plot(filename=pfn + "_contour.png")
model.set_fix_params(["om", "sigma_nl"]) fitter.add_model_and_dataset(CorrBeutler2017(), d, name=f"Beutler 2017 {t}", linestyle=ls, color=cs[0]) fitter.add_model_and_dataset(model, d, name=f"Beutler 2017 Fixed $\\Sigma_{{nl}}$ {t}", linestyle=ls, color=cs[0]) fitter.add_model_and_dataset(CorrSeo2016(recon=r), d, name=f"Seo 2016 {t}", linestyle=ls, color=cs[1]) fitter.add_model_and_dataset(CorrDing2018(recon=r), d, name=f"Ding 2018 {t}", linestyle=ls, color=cs[2]) fitter.set_sampler(sampler) fitter.set_num_walkers(30) fitter.fit(file) if fitter.should_plot(): import logging logging.info("Creating plots") res = fitter.load() if True: from chainconsumer import ChainConsumer if True: # This is a dirty way of faking another column in the dataset so I can keep plots visually consistent. # Essentially resampling our weighted chain to be uniform weights. # Note this requires that xi_individual.py has been run first! ####################################### ind_path = "plots/xi_individual/xi_individual_alphameans.csv" n = 1000000 stds_dict = None if os.path.exists(ind_path): df = pd.read_csv(ind_path) cols = [c for c in df.columns if "std" in c] stds = df[cols] stds_dict = {}
data = PowerSpectrum_SDSS_DR12(isotropic=True, recon="iso") model = PowerBeutler2017(isotropic=data.isotropic, recon=data.recon, marg="full") sampler = DynestySampler(temp_dir=dir_name) fitter = Fitter(dir_name) fitter.add_model_and_dataset(model, data) fitter.set_sampler(sampler) fitter.set_num_walkers(1) fitter.fit(file) if fitter.should_plot(): from chainconsumer import ChainConsumer posterior, weight, chain, evidence, model, data, extra = fitter.load( )[0] print(extra["name"]) chi2, dof, bband, mods, smooths = plot_bestfit(posterior, chain, model, title=extra["name"], figname=pfn + "_bestfit.pdf") c = ChainConsumer() c.add_chain(chain, weights=weight, parameters=model.get_labels()) c.plotter.plot(filename=pfn + "_contour.pdf")
color=cs[1]) fitter.add_model_and_dataset(PowerBeutler2017(isotropic=False, poly_poles=[0, 2, 4]), d2, name=r"$P_{0}$+P_{2}+P_{4}$", color=cs[2]) fitter.set_sampler(sampler) fitter.set_num_walkers(10) fitter.fit(file) if fitter.should_plot(): import logging logging.info("Creating plots") res = fitter.load() from chainconsumer import ChainConsumer import copy c = ChainConsumer() for posterior, weight, chain, evidence, model, data, extra in res: chain_conv = copy.deepcopy(chain) chain_conv[:, 0], chain_conv[:, 2] = model.get_alphas( chain[:, 0], chain[:, 2]) parameters = model.get_labels() parameters[0] = r"$\alpha_{par}$" parameters[2] = r"$\alpha_{perp}$" c.add_chain(chain_conv, weights=weight, parameters=parameters,
fitter.set_num_walkers(5) fitter.set_num_concurrent(100) fitter.fit(file) # Everything below is nasty plotting code ########################################################### if fitter.should_plot(): import logging logging.info("Creating plots") from chainconsumer import ChainConsumer counter = 0 c = ChainConsumer() for i, (posterior, weight, chain, evidence, model, data, extra) in enumerate(fitter.load()): fitname = "_".join(extra["name"].split()[:5]) extra["name"] = extra["name"].replace("_", " ") print(fitname) color = plt.colors.rgb2hex(cmap(float(i / len(datasets)))) model.set_data(data) r_s = model.camb.get_data()["r_s"] df = pd.DataFrame(chain, columns=model.get_labels()) alpha = df["$\\alpha$"].to_numpy() epsilon = df["$\\epsilon$"].to_numpy() alpha_par, alpha_perp = model.get_alphas(alpha, epsilon) df["$\\alpha_\\parallel$"] = alpha_par