mirror of
https://github.com/Richard-Sti/csiborgtools.git
synced 2024-10-19 05:45:09 +02:00
fb4abebeb6
* Add SN calibration model * Update imports * Scale Carrick field * Minor updates to flow validation * Update script * Update flow model * Add CSiBORG2 * Add CSiboRG2 params * Update imports * Add regular grid interpolator to LOS * Add nb * Remove old code * Update scripts * Minor updates * Minor updates * Add TF * Minor update * Update notebook * Update imports * Add scan and loss of numpyro * Add Pantheon * Update svript * Updaten b * Add model loader * Add jackknife * Add evidence * Update dr * Add BIC to the flow model * Update srcipt * Update nb * Update nb * Update scripts
2.7 MiB
2.7 MiB
In [1]:
import numpy as np
import matplotlib.pyplot as plt
from h5py import File
%matplotlib inline
from jax import numpy as jnp
import jax
from tqdm import tqdm
from scipy.optimize import fmin_powell
from numpyro.infer import MCMC, NUTS, util, init_to_median
import csiborgtools
import corner
%load_ext autoreload
%autoreload 2
paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)
LOS density & radial velocity plots¶
In [14]:
# fpath = "/mnt/extraspace/rstiskalek/catalogs/A2.h5"
fpath = "/mnt/extraspace/rstiskalek/catalogs/PV_compilation_Supranta2019.hdf5"
loader_carrick = csiborgtools.flow.DataLoader("Carrick2015", "LOSS", fpath, paths, ksmooth=0)
loader_csiborg = csiborgtools.flow.DataLoader("csiborg1", "LOSS", fpath, paths, ksmooth=0)
loader_csiborg2 = csiborgtools.flow.DataLoader("csiborg2_main", "LOSS", fpath, paths, ksmooth=0)
In [2]:
# ks = [115, 53, 77, 105, 26, 61, 86, 29, 80, 21]
ks = [19, 8, 15, 0, 16, 6, 48, 38, 26, 44]
# ks = [19]
# ks = np.random.choice(50, 10, replace=False)
# k = 6
for k in []:
fig, axs = plt.subplots(2, 1, figsize=(7, 7), sharex=True)
# Get rid of vertical spacing
fig.subplots_adjust(wspace=0)
# Plot CSiBORG
for i in range(loader_csiborg.los_density.shape[1]):
axs[0].plot(loader_csiborg.rdist, loader_csiborg.los_density[k, i, :], alpha=0.1, color="black")
axs[1].plot(loader_csiborg.rdist, loader_csiborg.los_radial_velocity[k, i, :], alpha=0.1, color="black")
# CSiBORG1
axs[0].plot(loader_csiborg.rdist, loader_csiborg.los_density[k, :, :].mean(axis=0), color="red", label="CSiBORG1")
axs[1].plot(loader_csiborg.rdist, loader_csiborg.los_radial_velocity[k, :, :].mean(axis=0), color="red")
# CSiBORG2
axs[0].plot(loader_csiborg2.rdist, loader_csiborg2.los_density[k, :, :].mean(axis=0), color="violet", label="CSiBORG2")
axs[1].plot(loader_csiborg2.rdist, loader_csiborg2.los_radial_velocity[k, :, :].mean(axis=0), color="violet")
# Plot Carrick+2015
axs[0].plot(loader_carrick.rdist, loader_carrick.los_density[k, 0, :], color="blue", label="Carrick+2015")
axs[1].plot(loader_carrick.rdist, loader_carrick.los_radial_velocity[k, 0, :] * 0.43, color="blue")
# for i in range(2):
# label = "SN"
# rdist = loader_csiborg.cat["r_hMpc"][k]
# axs[i].axvline(rdist, color="violet", linestyle="--",
# zorder=0, label=label)
axs[1].set_xlabel(r"$r ~ [\mathrm{Mpc} / h]$")
axs[0].set_ylabel(r"$\rho_{\rm LOS} / \langle \rho_{\rm matter} \rangle$")
axs[1].set_ylabel(r"$v_{\rm LOS} ~ [\mathrm{km/s}]$")
axs[0].set_yscale("log")
axs[0].legend(loc="upper right")
axs[0].set_xlim(0, 200)
fig.tight_layout(w_pad=0, h_pad=0)
fig.savefig(f"../plots/LOSS_los_{k}.png", dpi=500, bbox_inches="tight")
fig.show()
Test running a model¶
In [174]:
fpath_data = "/mnt/extraspace/rstiskalek/catalogs/PV_compilation_Supranta2019.hdf5"
# fpath_data = "/mnt/extraspace/rstiskalek/catalogs/A2.h5"
simname = "csiborg1"
catalogue = "2MTF"
loader = csiborgtools.flow.DataLoader(simname, catalogue, fpath_data, paths, ksmooth=0)
get_model_kwargs = {"zcmb_max": 0.06}
Maximizing the log-likelihood¶
In [137]:
samples, stats, fmin, logz, bic = csiborgtools.flow.optimize_model_with_jackknife(loader, 19, 5, True, get_model_kwargs=get_model_kwargs)
In [138]:
print("Log Z: ")
print(np.mean(logz), np.std(logz))
print("BIC: ")
print(np.mean(bic), np.std(bic))
print("beta: ")
print(*stats["beta"])
Running HMC¶
In [176]:
model = csiborgtools.flow.get_model(loader, k=30, **get_model_kwargs)
In [178]:
kernel = NUTS(model, init_strategy=init_to_median(num_samples=100))
mcmc = MCMC(kernel, num_warmup=250, num_samples=500)
rng_key = jax.random.PRNGKey(5)
In [179]:
mcmc.run(rng_key, sample_alpha=True)
In [180]:
mcmc.print_summary()
samples = mcmc.get_samples(group_by_chain=False)
In [181]:
plt.figure()
plt.hist(samples["beta"], bins="auto")
plt.xlabel(r"$\beta$")
plt.show()
In [182]:
Vmag = np.sqrt(samples["Vext_x"]**2 + samples["Vext_y"]**2 + samples["Vext_z"]**2)
V = np.vstack([samples["Vext_x"], samples["Vext_y"], samples["Vext_z"]]).T
V = csiborgtools.cartesian_to_radec(V)
l, b = csiborgtools.flow.radec_to_galactic(V[:, 1], V[:, 2])
In [183]:
print(f"|V| = {np.mean(Vmag)} +- {np.std(Vmag)}")
print(f"l = {np.mean(l)} +- {np.std(l)}")
print(f"b = {np.mean(b)} +- {np.std(b)}")
print(f"beta = {np.mean(samples['beta'])} +- {np.std(samples['beta'])}")
In [184]:
plt.figure()
plt.scatter(l, b)
plt.xlabel(r"$l$")
plt.ylabel(r"$b$")
plt.show()
In [185]:
plt.figure()
plt.hist(V[:, 0], bins="auto")
plt.show()
In [172]:
if "alpha" in samples:
data = np.vstack([samples["alpha"], samples["beta"], l, b, Vmag]).T
labels = [r"$\alpha$", r"$\beta$", r"$l$", r"$b$", r"$|\bf{V}_{\rm ext}|$"]
else:
data = np.vstack([samples["beta"], l, b, Vmag]).T
labels = [r"$\beta$", r"$l$", r"$b$", r"$|\bf{V}_{\rm ext}|$"]
# keys = samples.keys()
# data = np.vstack([samples[key] for key in keys]).T
# labels = list(keys)
In [173]:
fig = corner.corner(data, labels=labels, show_titles=True, title_fmt=".3f", title_kwargs={"fontsize": 12}, smooth=1)
fig.savefig(f"../plots/corner.png", dpi=300, bbox_inches="tight")
Vizualize the results¶
In [17]:
paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)
catalogue = "LOSS"
simname = "csiborg2_random"
nsims = paths.get_ics(simname)
Vx = []
Vy = []
Vz = []
beta = []
sigma_v = []
alpha = []
alpha_cal = []
beta_cal = []
mag_cal = []
e_mu_intrinsic = []
with File(f"/mnt/extraspace/rstiskalek/csiborg_postprocessing/peculiar_velocity/flow_samples_{catalogue}_{simname}_smooth_0.hdf5", 'r') as f:
for i, nsim in enumerate(tqdm(nsims)):
if i == 0:
print(f[f"sim_{nsim}"].keys())
Vx.append(f[f"sim_{nsim}/Vext_x"][:])
Vy.append(f[f"sim_{nsim}/Vext_y"][:])
Vz.append(f[f"sim_{nsim}/Vext_z"][:])
alpha.append(f[f"sim_{nsim}/alpha"][:])
beta.append(f[f"sim_{nsim}/beta"][:])
sigma_v.append(f[f"sim_{nsim}/sigma_v"][:])
alpha_cal.append(f[f"sim_{nsim}/alpha_cal"][:])
beta_cal.append(f[f"sim_{nsim}/beta_cal"][:])
mag_cal.append(f[f"sim_{nsim}/mag_cal"][:])
e_mu_intrinsic.append(f[f"sim_{nsim}/e_mu_intrinsic"][:])
Vx = np.hstack(Vx)
Vy = np.hstack(Vy)
Vz = np.hstack(Vz)
alpha = np.hstack(alpha)
beta = np.hstack(beta)
sigma_v = np.hstack(sigma_v)
alpha_cal = np.hstack(alpha_cal)
beta_cal = np.hstack(beta_cal)
mag_cal = np.hstack(mag_cal)
e_mu_intrinsic = np.hstack(e_mu_intrinsic)
Vmag = np.sqrt(Vx**2 + Vy**2 + Vz**2)
V = np.vstack([Vx, Vy, Vz]).T
V = csiborgtools.cartesian_to_radec(V)
l, b = csiborgtools.flow.radec_to_galactic(V[:, 1], V[:, 2])
data = np.vstack([alpha, beta, Vmag, l, b, sigma_v, alpha_cal, beta_cal, mag_cal, e_mu_intrinsic]).T
labels = [r"$\alpha$", r"$\beta$", r"$|\bf{V}_{\rm ext}|$", r"$l$", r"$b$", r"$\sigma_v$", r"$\alpha_{\rm cal}$", r"$\beta_{\rm cal}$", r"$M$", r"$\sigma_\mu$"]
In [18]:
fig = corner.corner(data, labels=labels, show_titles=True, title_fmt=".3f", title_kwargs={"fontsize": 12})
fig.suptitle(f"{catalogue}, {simname}")
fig.savefig(f"../plots/corner_{catalogue}_{simname}.png", dpi=300, bbox_inches="tight")
In [210]:
np.mean(l)
Out[210]:
In [299]:
0.906 * 0.43
Out[299]:
In [214]:
plt.figure()
plt.hist(V[:, 0], bins="auto", density=True)
plt.show()
In [219]:
print(beta.mean(), beta.std())
print(np.mean(V[:, 0]), np.std(V[:, 0]))
print(l.mean(), b.mean())
In [213]:
plt.figure()
plt.hexbin(l, b, gridsize=50, mincnt=1)
plt.xlabel(r"$l$")
plt.ylabel(r"$b$")
plt.show()
In [ ]:
In [130]:
plt.figure()
for i in range(len(x)):
plt.hist(x[i], bins="auto", density=True, histtype="step")
plt.show()
Test bias¶
In [437]:
from jax.lax import cond
In [488]:
def bias(rho_norm, b):
def bias_small(rho_norm, b):
return jnp.log((1 + jnp.exp((1 - b + b * rho_norm))) / (1 + jnp.exp((1 - b))))
def bias_large(rho_norm, b):
return 1 - b + b * rho_norm - jnp.log(1 + jnp.exp(1 - b))
return jnp.where(rho_norm < 3,
bias_small(rho_norm, b),
bias_large(rho_norm, b))
In [489]:
rho_norm = jnp.linspace(0, 100, 100)
b = 2
y = bias(rho_norm, b)
In [490]:
y
Out[490]:
In [485]:
bias_small(rho_norm, b)
Out[485]:
In [487]:
plt.figure()
plt.plot(rho_norm, y)
plt.plot(rho_norm, bias_small(rho_norm, b), ls="--")
plt.plot(rho_norm, bias_large(rho_norm, b), ls="dotted")
plt.show()
In [ ]:
In [462]:
bias(loader._los_density[0], 2.)
Out[462]:
In [ ]:
In [ ]:
In [436]:
b = 1.4
rho = np.linspace(0, 3, 100)
def bias(rho, b):
return np.log((1 + np.exp((1 - b + b * rho))) / (1 + np.exp((1 - b))))
def linear_bias(rho, b):
return (1 - b + b * rho)
plt.figure()
cols = plt.rcParams['axes.prop_cycle'].by_key()['color']
for i, b in enumerate([0.5, 1, 1.5, 3]):
plt.plot(rho, bias(rho, b), label=f"b = {b}", c=cols[i])
plt.plot(rho, 1 - b + b * rho - np.log(1 + np.exp(1 - b)), ls="--", c=cols[i])
# plt.plot(rho, linear_bias(rho, b), ls="--", c=cols[i])
plt.axline((1, 1), slope=1, color="red", linestyle="dotted")
plt.axhline(0, color="black", linestyle="--")
plt.xlabel(r"$\rho / \langle \rho \rangle$")
plt.ylabel(r"$1 + \delta_g$")
plt.legend()
plt.show()
In [ ]:
In [133]:
def get_field(kind):
folder = "/mnt/extraspace/rstiskalek/catalogs"
from os.path import join
if kind == "density":
fpath = join(folder, "twompp_density_carrick2015.npy")
return np.load(fpath).astype(np.float32)
elif kind == "velocity":
fpath = join(folder, "twompp_velocity_carrick2015.npy")
field = np.load(fpath).astype(np.float32)
# Because the Carrick+2015 data is in the following form:
# "The velocities are predicted peculiar velocities in the CMB
# frame in Galactic Cartesian coordinates, generated from the
# \(\delta_g^*\) field with \(\beta^* = 0.43\) and an external
# dipole \(V_\mathrm{ext} = [89,-131,17]\) (Carrick et al Table 3)
# has already been added.""
field[0] -= 89
field[1] -= -131
field[2] -= 17
field /= 0.43
return field
In [135]:
density = get_field("density")
velocity = get_field("velocity")
In [136]:
from h5py import File
In [ ]:
In [137]:
with File("/mnt/extraspace/rstiskalek/catalogs/Carrick2015_unscaled.hdf5", 'w') as f:
f.create_dataset("density", data=density)
f.create_dataset("velocity", data=velocity)
In [ ]: