Velocity observer (#86)

* Continue if r200c not defined

* Remove smooth scale

* Remove smooth scale

* Edit Max Matching plot

* Add peculiar velocity

* Add Vobs calculation

* Edit docs

* Add Vobs plot

* Improve plotting

* Edit naming convention

* Make a note

* Add new cat options

* Update density field RSP calculation

* Update field 2 rsp

* Move functions and shorten documentation

* Improve transforms and comments

* Update docs

* Update imports

* Edit calculation

* Add docs

* Remove imports

* Add Quijote flags

* Edit documentation

* Shorten documentation

* Edit func calls

* Shorten

* Docs edits

* Edit docs

* Shorten docs

* Short docs edits

* Simplify docs a little bit

* Save plotting

* Update env
This commit is contained in:
Richard Stiskalek 2023-08-30 23:27:20 +01:00 committed by GitHub
parent 8e3127f4d9
commit ae92fd9b72
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 761 additions and 788 deletions

View file

@ -305,7 +305,7 @@ def plot_hmf_quijote_full(pdf=False):
plt.close()
def load_field(kind, nsim, grid, MAS, in_rsp=False, smooth_scale=None):
def load_field(kind, nsim, grid, MAS, in_rsp=False):
r"""
Load a single field.
@ -321,16 +321,13 @@ def load_field(kind, nsim, grid, MAS, in_rsp=False, smooth_scale=None):
Mass assignment scheme.
in_rsp : bool, optional
Whether to load the field in redshift space.
smooth_scale : float, optional
Smoothing scale in :math:`\mathrm{Mpc} / h`.
Returns
-------
field : n-dimensional array
"""
paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)
return numpy.load(paths.field(kind, MAS, grid, nsim, in_rsp=in_rsp,
smooth_scale=smooth_scale))
return numpy.load(paths.field(kind, MAS, grid, nsim, in_rsp=in_rsp))
###############################################################################
@ -377,16 +374,16 @@ def plot_projected_field(kind, nsim, grid, in_rsp, smooth_scale, MAS="PCS",
box = csiborgtools.read.CSiBORGBox(nsnap, nsim, paths)
if kind == "overdensity":
field = load_field("density", nsim, grid, MAS=MAS, in_rsp=in_rsp,
smooth_scale=smooth_scale)
field = load_field("density", nsim, grid, MAS=MAS, in_rsp=in_rsp)
density_gen = csiborgtools.field.DensityField(box, MAS)
field = density_gen.overdensity_field(field) + 1
field = density_gen.overdensity_field(field) + 2
field = numpy.log10(field)
elif kind == "borg_density":
field = File(paths.borg_mcmc(nsim), 'r')
field = field["scalars"]["BORG_final_density"][...]
else:
field = load_field(kind, nsim, grid, MAS=MAS, in_rsp=in_rsp,
smooth_scale=smooth_scale)
field = load_field(kind, nsim, grid, MAS=MAS, in_rsp=in_rsp)
if kind == "velocity":
field = field[vel_component, ...]
@ -423,6 +420,9 @@ def plot_projected_field(kind, nsim, grid, in_rsp, smooth_scale, MAS="PCS",
else:
ax[i].imshow(img, vmin=vmin, vmax=vmax, cmap=cmap)
k = img.shape[0] // 2
ax[i].scatter(k, k, marker="x", s=5, zorder=2, c="red")
frad = 155.5 / 677.7
R = 155.5 / 677.7 * grid
if slice_find is None:
@ -487,6 +487,7 @@ def plot_projected_field(kind, nsim, grid, in_rsp, smooth_scale, MAS="PCS",
else:
fig.colorbar(im, cax=cbar_ax, label=clabel)
fig.tight_layout(h_pad=0, w_pad=0)
for ext in ["png"] if pdf is False else ["png", "pdf"]:
fout = join(
@ -587,7 +588,7 @@ def plot_sky_distribution(field, nsim, grid, nside, smooth_scale=None,
nsnap = max(paths.get_snapshots(nsim, "csiborg"))
box = csiborgtools.read.CSiBORGBox(nsnap, nsim, paths)
if field== "overdensity":
if field == "overdensity":
field = load_field("density", nsim, grid, MAS=MAS, in_rsp=False,
smooth_scale=smooth_scale)
density_gen = csiborgtools.field.DensityField(box, MAS)
@ -652,7 +653,7 @@ if __name__ == "__main__":
if False:
plot_mass_vs_ncells(7444, pdf=False)
if True:
if False:
plot_hmf(pdf=False)
if False:
@ -665,16 +666,17 @@ if __name__ == "__main__":
plot_groups=False, dmin=45, dmax=60,
plot_halos=5e13, volume_weight=True)
if False:
kind = "overdensity"
grid = 256
if True:
kind = "potential"
grid = 512
smooth_scale = 0
# plot_projected_field("overdensity", 7444, grid, in_rsp=True,
# highres_only=False)
plot_projected_field(kind, 7444, grid, in_rsp=False,
smooth_scale=smooth_scale, slice_find=0.5,
MAS="PCS",
highres_only=True)
# for in_rsp in [True, False]:
for in_rsp in [True, False]:
plot_projected_field(kind, 7444, grid, in_rsp=in_rsp,
smooth_scale=smooth_scale, slice_find=0.5,
MAS="PCS", highres_only=True)
if False:
paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)

View file

@ -191,7 +191,7 @@ def get_mtot_vs_maxpairoverlap(nsim0, simname, mass_kind, min_logmass,
def get_max(y_):
if len(y_) == 0:
return 0
return numpy.max(y_)
return numpy.nanmax(y_)
reader = csiborgtools.read.NPairsOverlap(cat0, catxs, paths, min_logmass)
@ -218,7 +218,6 @@ def mtot_vs_maxpairoverlap(nsim0, simname, mass_kind, min_logmass, smoothed,
x, y, xbins = get_mtot_vs_maxpairoverlap(nsim0, simname, mass_kind,
min_logmass, smoothed, nbins)
plt.close("all")
with plt.style.context(plt_utils.mplstyle):
plt.figure()
plt.hexbin(x, y, mincnt=1, gridsize=50, bins="log")
@ -252,6 +251,87 @@ def mtot_vs_maxpairoverlap(nsim0, simname, mass_kind, min_logmass, smoothed,
plt.close()
# --------------------------------------------------------------------------- #
###############################################################################
# Total DM halo mass vs maximum pair overlap consistency #
###############################################################################
# --------------------------------------------------------------------------- #
@cache_to_disk(120)
def get_mtot_vs_maxpairoverlap_consistency(nsim0, simname, mass_kind,
min_logmass, smoothed):
paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)
nsimxs = csiborgtools.read.get_cross_sims(simname, nsim0, paths,
min_logmass, smoothed=smoothed)
cat0 = open_cat(nsim0, simname)
catxs = open_cats(nsimxs, simname)
reader = csiborgtools.read.NPairsOverlap(cat0, catxs, paths, min_logmass)
x = numpy.log10(cat0[mass_kind])
mask = x > min_logmass
x = x[mask]
nhalos = len(x)
y = numpy.full((len(catxs), nhalos), numpy.nan)
for i in trange(len(catxs), desc="Stacking catalogues"):
overlaps = reader[i].overlap(smoothed)
for j in range(nhalos):
# if len(overlaps[j]) > 0:
y[i, j] = numpy.sum(overlaps[j])
return x, y
def mtot_vs_maxpairoverlap_consistency(nsim0, simname, mass_kind, min_logmass,
smoothed, ext="png"):
left_edges = numpy.arange(min_logmass, 15, 0.1)
# delete_disk_caches_for_function("get_mtot_vs_maxpairoverlap_consistency")
x, y0 = get_mtot_vs_maxpairoverlap_consistency(nsim0, simname, mass_kind,
min_logmass, smoothed)
nsims, nhalos = y0.shape
x_2, y0_2 = get_mtot_vs_maxpairoverlap_consistency(
0, "quijote", "group_mass", min_logmass, smoothed)
nsims2, nhalos = y0_2.shape
with plt.style.context(plt_utils.mplstyle):
plt.figure()
yplot = numpy.full(len(left_edges), numpy.nan)
yplot_2 = numpy.full(len(left_edges), numpy.nan)
for ymin in [0.3]:
y = numpy.sum(y0 > ymin, axis=0) / nsims
y_2 = numpy.sum(y0_2 > ymin, axis=0) / nsims2
for i, left_edge in enumerate(left_edges):
mask = x > left_edge
yplot[i] = numpy.mean(y[mask]) #/ nsims
mask = x_2 > left_edge
yplot_2[i] = numpy.mean(y_2[mask]) #/ nsims
plt.plot(left_edges, yplot, label="CSiBORG")
plt.plot(left_edges, yplot_2, label="Quijote")
plt.legend()
# y2 = numpy.concatenate(y0)
# y2 = y2[y2 > 0]
# m = y0 > 0
# plt.hist(y0[m], bins=30, density=True, histtype="step")
# m = y0_2 > 0
# plt.hist(y0_2[m], bins=30, density=True, histtype="step")
# plt.yscale("log")
plt.tight_layout()
fout = join(
plt_utils.fout,
f"mass_vs_max_pair_overlap_consistency_{simname}_{nsim0}.{ext}")
print(f"Saving to `{fout}`.")
plt.savefig(fout, dpi=plt_utils.dpi, bbox_inches="tight")
plt.close()
# --------------------------------------------------------------------------- #
###############################################################################
# Total DM halo mass vs summed pair overlaps #
@ -876,8 +956,7 @@ def get_matching_max_vs_overlap(simname, nsim0, min_logmass, mult):
def matching_max_vs_overlap(simname, nsim0, min_logmass):
left_edges = numpy.arange(min_logmass, 15, 0.1)
delete_disk_caches_for_function("get_matching_max_vs_overlap")
nsims = 100 if simname == "csiborg" else 9
with plt.style.context("science"):
fig, axs = plt.subplots(ncols=2, figsize=(3.5 * 2, 2.625))
@ -891,34 +970,36 @@ def matching_max_vs_overlap(simname, nsim0, min_logmass):
success = x["success"]
nbins = len(left_edges)
y = numpy.full((nbins, 100), numpy.nan)
y2 = numpy.full((nbins, 100), numpy.nan)
y = numpy.full((nbins, nsims), numpy.nan)
y2 = numpy.full(nbins, numpy.nan)
y2err = numpy.full(nbins, numpy.nan)
for i in range(nbins):
m = mass0 > left_edges[i]
for j in range(100):
for j in range(nsims):
y[i, j] = numpy.sum(
max_overlap[m, j] == match_overlap[m, j])
y[i, j] /= numpy.sum(success[m, j])
y2[i, j] = numpy.sum(success[m, j]) / numpy.sum(m)
offset = numpy.random.normal(0, 0.01)
y2[i] = numpy.mean(numpy.sum(success[m, :], axis=1) / nsims)
y2err[i] = numpy.std(numpy.sum(success[m, :], axis=1) / nsims)
offset = numpy.random.normal(0, 0.015)
ysummary = numpy.percentile(y, [16, 50, 84], axis=1)
axs[0].errorbar(
left_edges + offset, ysummary[1],
yerr=[ysummary[1] - ysummary[0], ysummary[2] - ysummary[1]],
capsize=3, c=cols[n],
capsize=4, c=cols[n], ls="dashed",
label=r"$\leq {}~R_{{\rm 200c}}$".format(mult), errorevery=2)
ysummary = numpy.percentile(y2, [16, 50, 84], axis=1)
axs[1].errorbar(
left_edges + offset, ysummary[1], ls="--",
yerr=[ysummary[1] - ysummary[0], ysummary[2] - ysummary[1]],
capsize=3, c=cols[n], errorevery=2)
axs[1].errorbar(left_edges + offset, y2, yerr=y2err,
capsize=4, errorevery=2, c=cols[n], ls="dashed")
axs[0].legend(ncols=2, fontsize="small")
for i in range(2):
axs[i].set_xlabel(r"$\log M_{\rm tot, min} ~ [M_\odot / h]$")
axs[1].set_ylim(0)
axs[0].set_ylabel(r"$f_{\rm agreement}$")
axs[1].set_ylabel(r"$f_{\rm match}$")
@ -1048,7 +1129,7 @@ if __name__ == "__main__":
smoothed = True
nbins = 10
ext = "png"
plot_quijote = False
plot_quijote = True
min_maxoverlap = 0.
funcs = [
@ -1128,5 +1209,19 @@ if __name__ == "__main__":
mtot_vs_maxoverlap_property(0, "quijote", min_logmass, key,
min_maxoverlap, smoothed)
if False:
matching_max_vs_overlap("csiborg", 7444, min_logmass)
if plot_quijote:
matching_max_vs_overlap("quijote", 0, min_logmass)
if True:
matching_max_vs_overlap(7444, min_logmass)
mtot_vs_maxpairoverlap_consistency(
7444, "csiborg", "fof_totpartmass", min_logmass, smoothed,
ext="png")
# if plot_quijote:
# mtot_vs_maxpairoverlap_consistency(
# 0, "quijote", "group_mass", min_logmass, smoothed,
# ext="png")

View file

@ -0,0 +1,89 @@
# Copyright (C) 2023 Richard Stiskalek
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
from os.path import join
import matplotlib.pyplot as plt
import numpy
import scienceplots # noqa
from tqdm import tqdm
import csiborgtools
import plt_utils
def observer_peculiar_velocity(MAS, grid, ext="png"):
paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)
nsims = paths.get_ics("csiborg")
for n, nsim in enumerate(nsims):
fpath = paths.observer_peculiar_velocity(MAS, grid, nsim)
f = numpy.load(fpath)
if n == 0:
data = numpy.full((len(nsims), *f["observer_vp"].shape), numpy.nan)
smooth_scales = f["smooth_scales"]
data[n] = f["observer_vp"]
for n, smooth_scale in enumerate(tqdm(smooth_scales,
desc="Plotting smooth scales")):
with plt.style.context(plt_utils.mplstyle):
fig, axs = plt.subplots(ncols=3, figsize=(3.5 * 3, 2.625),
sharey=True)
fig.subplots_adjust(wspace=0)
for i, ax in enumerate(axs):
ax.hist(data[:, n, i], bins="auto", histtype="step")
mu, sigma = numpy.mean(data[:, n, i]), numpy.std(data[:, n, i])
ax.set_title(r"$V_{{\rm obs, i}} = {:.3f} \pm {:.3f} ~ \mathrm{{km}} / \mathrm{{s}}$".format(mu, sigma)) # noqa
axs[0].set_xlabel(r"$V_{\rm obs, x} ~ [\mathrm{km} / \mathrm{s}]$")
axs[1].set_xlabel(r"$V_{\rm obs, y} ~ [\mathrm{km} / \mathrm{s}]$")
axs[2].set_xlabel(r"$V_{\rm obs, z} ~ [\mathrm{km} / \mathrm{s}]$")
axs[0].set_ylabel(r"Counts")
fig.suptitle(r"$N_{{\rm grid}} = {}$, $\sigma_{{\rm smooth}} = {:.2f} ~ [\mathrm{{Mpc}} / h]$".format(grid, smooth_scale)) # noqa
fig.tight_layout(w_pad=0)
fout = join(plt_utils.fout,
f"observer_vp_{grid}_{smooth_scale}.{ext}")
fig.savefig(fout, dpi=plt_utils.dpi, bbox_inches="tight")
plt.close()
with plt.style.context(plt_utils.mplstyle):
fig, axs = plt.subplots(ncols=3, figsize=(3.5 * 3, 2.625))
for i, ax in enumerate(axs):
ymu = numpy.mean(data[..., i], axis=0)
ystd = numpy.std(data[..., i], axis=0)
ylow, yupp = ymu - ystd, ymu + ystd
ax.plot(smooth_scales, ymu, c="k")
ax.fill_between(smooth_scales, ylow, yupp, color="k", alpha=0.2)
ax.set_xlabel(r"$\sigma_{{\rm smooth}} ~ [\mathrm{{Mpc}} / h]$")
axs[0].set_ylabel(r"$V_{\rm obs, x} ~ [\mathrm{km} / \mathrm{s}]$")
axs[1].set_ylabel(r"$V_{\rm obs, y} ~ [\mathrm{km} / \mathrm{s}]$")
axs[2].set_ylabel(r"$V_{\rm obs, z} ~ [\mathrm{km} / \mathrm{s}]$")
fig.suptitle(r"$N_{{\rm grid}} = {}$".format(grid))
fig.tight_layout(w_pad=0)
fout = join(plt_utils.fout, f"observer_vp_summary_{grid}.{ext}")
fig.savefig(fout, dpi=plt_utils.dpi, bbox_inches="tight")
plt.close()
if __name__ == "__main__":
observer_peculiar_velocity("PCS", 512)