New matches (#69)

* Remove old file

* Add velocity plotting

* add smooth scale

* Fix bug

* Improve paths

* Edit plotting

* Add smoothed density

* Update boundaries

* Add basics

* Further docs

* Remove blank

* Better catalog broadcasting

* Update high res size

* Update plotting routines

* Update routine

* Update plotting

* Fix field saving name

* Add better colormap for environemnt
This commit is contained in:
Richard Stiskalek 2023-06-17 19:52:26 +01:00 committed by GitHub
parent 73687fd8cc
commit 35ccfb5c67
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 343 additions and 169 deletions

View file

@ -16,6 +16,7 @@
from os.path import join
from argparse import ArgumentParser
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy
import healpy
@ -209,8 +210,8 @@ def plot_hmf(pdf=False):
plt.close()
def load_field(kind, nsim, grid, MAS, in_rsp=False):
"""
def load_field(kind, nsim, grid, MAS, in_rsp=False, smooth_scale=None):
r"""
Load a single field.
Parameters
@ -225,13 +226,16 @@ def load_field(kind, nsim, grid, MAS, in_rsp=False):
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))
return numpy.load(paths.field(kind, MAS, grid, nsim, in_rsp=in_rsp,
smooth_scale=smooth_scale))
###############################################################################
@ -239,9 +243,9 @@ def load_field(kind, nsim, grid, MAS, in_rsp=False):
###############################################################################
def plot_projected_field(kind, nsim, grid, in_rsp, MAS="PCS",
def plot_projected_field(kind, nsim, grid, in_rsp, smooth_scale, MAS="PCS",
highres_only=True, slice_find=None, pdf=False):
"""
r"""
Plot the mean projected field, however can also plot a single slice.
Parameters
@ -254,6 +258,8 @@ def plot_projected_field(kind, nsim, grid, in_rsp, MAS="PCS",
Grid size.
in_rsp : bool
Whether to load the field in redshift space.
smooth_scale : float
Smoothing scale in :math:`\mathrm{Mpc} / h`.
MAS : str, optional
Mass assignment scheme.
highres_only : bool, optional
@ -273,11 +279,16 @@ def plot_projected_field(kind, nsim, grid, in_rsp, MAS="PCS",
box = csiborgtools.read.CSiBORGBox(nsnap, nsim, paths)
if kind == "overdensity":
field = load_field("density", nsim, grid, MAS=MAS, in_rsp=in_rsp)
field = load_field("density", nsim, grid, MAS=MAS, in_rsp=in_rsp,
smooth_scale=smooth_scale)
density_gen = csiborgtools.field.DensityField(box, MAS)
field = density_gen.overdensity_field(field) + 2
field = density_gen.overdensity_field(field) + 1
else:
field = load_field(kind, nsim, grid, MAS=MAS, in_rsp=in_rsp)
field = load_field(kind, nsim, grid, MAS=MAS, in_rsp=in_rsp,
smooth_scale=smooth_scale)
if kind == "velocity":
field = field[0, ...]
if highres_only:
csiborgtools.field.fill_outside(field, numpy.nan, rmax=155.5,
@ -286,10 +297,11 @@ def plot_projected_field(kind, nsim, grid, in_rsp, MAS="PCS",
end = round(field.shape[0] * 0.73)
field = field[start:end, start:end, start:end]
if kind != "environment":
cmap = "viridis"
if kind == "environment":
cmap = mpl.colors.ListedColormap(
['red', 'lightcoral', 'limegreen', 'khaki'])
else:
cmap = "brg"
cmap = "viridis"
labels = [r"$y-z$", r"$x-z$", r"$x-y$"]
with plt.style.context(plt_utils.mplstyle):
@ -309,12 +321,15 @@ def plot_projected_field(kind, nsim, grid, in_rsp, MAS="PCS",
else:
ax[i].imshow(img, vmin=vmin, vmax=vmax, cmap=cmap)
if not highres_only:
frad = 155.5 / 677.7
if not highres_only and 0.5 - frad < slice_find < 0.5 + frad:
theta = numpy.linspace(0, 2 * numpy.pi, 100)
rad = 155.5 / 677.7 * grid
z = (slice_find - 0.5) * grid
R = 155.5 / 677.7 * grid
rad = R * numpy.sqrt(1 - z**2 / R**2)
ax[i].plot(rad * numpy.cos(theta) + grid // 2,
rad * numpy.sin(theta) + grid // 2,
lw=plt.rcParams["lines.linewidth"], zorder=1,
lw=0.75 * plt.rcParams["lines.linewidth"], zorder=1,
c="red", ls="--")
ax[i].set_title(labels[i])
@ -343,17 +358,32 @@ def plot_projected_field(kind, nsim, grid, in_rsp, MAS="PCS",
(xticks * size / ncells - size / 2).astype(int))
ax[i].set_xlabel(r"$x_j ~ [\mathrm{Mpc} / h]$")
cbar_ax = fig.add_axes([1.0, 0.1, 0.025, 0.8])
cbar_ax = fig.add_axes([0.982, 0.155, 0.025, 0.75],
transform=ax[2].transAxes)
if slice_find is None:
clabel = "Mean projected field"
else:
clabel = "Sliced field"
fig.colorbar(im, cax=cbar_ax, label=clabel)
if kind == "environment":
bounds = [0, 1, 2, 3, 4]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
cbar = fig.colorbar(
mpl.cm.ScalarMappable(cmap=cmap, norm=norm), cax=cbar_ax,
ticks=[0.5, 1.5, 2.5, 3.5])
cbar.ax.set_yticklabels(["knot", "filament", "sheet", "void"],
rotation=90, va="center")
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(plt_utils.fout,
f"field_{kind}_{nsim}_rsp{in_rsp}.{ext}")
fout = join(
plt_utils.fout,
f"field_{kind}_{nsim}_rsp{in_rsp}_hres{highres_only}.{ext}")
if smooth_scale is not None and smooth_scale > 0:
smooth_scale = float(smooth_scale)
fout = fout.replace(f".{ext}", f"_smooth{smooth_scale}.{ext}")
print(f"Saving to `{fout}`.")
fig.savefig(fout, dpi=plt_utils.dpi, bbox_inches="tight")
plt.close()
@ -404,8 +434,8 @@ def get_sky_label(kind, volume_weight):
return label
def plot_sky_distribution(kind, nsim, grid, nside, MAS="PCS", plot_groups=True,
dmin=0, dmax=220, plot_halos=None,
def plot_sky_distribution(kind, nsim, grid, nside, smooth_scale, MAS="PCS",
plot_groups=True, dmin=0, dmax=220, plot_halos=None,
volume_weight=True, pdf=False):
r"""
Plot the sky distribution of a given field kind on the sky along with halos
@ -425,6 +455,8 @@ def plot_sky_distribution(kind, nsim, grid, nside, MAS="PCS", plot_groups=True,
Grid size.
nside : int
Healpix nside of the sky projection.
smooth_scale : float
Smoothing scale in :math:`\mathrm{Mpc} / h`.
MAS : str, optional
Mass assignment scheme.
plot_groups : bool, optional
@ -445,11 +477,13 @@ def plot_sky_distribution(kind, nsim, grid, nside, MAS="PCS", plot_groups=True,
box = csiborgtools.read.CSiBORGBox(nsnap, nsim, paths)
if kind == "overdensity":
field = load_field("density", nsim, grid, MAS=MAS, in_rsp=False)
field = load_field("density", nsim, grid, MAS=MAS, in_rsp=False,
smooth_scale=smooth_scale)
density_gen = csiborgtools.field.DensityField(box, MAS)
field = density_gen.overdensity_field(field) + 2
field = density_gen.overdensity_field(field) + 1
else:
field = load_field(kind, nsim, grid, MAS=MAS, in_rsp=False)
field = load_field(kind, nsim, grid, MAS=MAS, in_rsp=False,
smooth_scale=smooth_scale)
angpos = csiborgtools.field.nside2radec(nside)
dist = numpy.linspace(dmin, dmax, 500)
@ -519,7 +553,22 @@ if __name__ == "__main__":
if True:
kind = "environment"
grid = 256
smooth_scale = 0
# plot_projected_field("overdensity", 7444, grid, in_rsp=True,
# highres_only=False)
plot_projected_field(kind, 7444, grid, in_rsp=False,
slice_find=0.5, highres_only=False)
smooth_scale=smooth_scale, slice_find=0.5,
highres_only=False)
if False:
paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)
d = csiborgtools.read.read_h5(paths.particles(7444))["particles"]
plt.figure()
plt.hist(d[:100000, 4], bins="auto")
plt.tight_layout()
plt.savefig("../plots/velocity_distribution.png", dpi=450,
bbox_inches="tight")

View file

@ -648,7 +648,32 @@ def plot_significance(simname, runs, nsim, nobs, kind, kwargs, runs_to_mass):
plt.close()
def plot_significance_vs_mass(simname, runs, nsim, nobs, kind, kwargs):
def make_binlims(run, runs_to_mass):
"""
Make bin limits for the 1NN distance runs, corresponding to the first half
of the log-mass bin.
Parameters
----------
run : str
Run name.
runs_to_mass : dict
Dictionary mapping run names to total halo mass range.
Returns
-------
xmin, xmax : floats
"""
xmin, xmax = runs_to_mass[run]
xmax = xmin + (xmax - xmin) / 2
xmin, xmax = 10**xmin, 10**xmax
if run == "mass009":
xmax = numpy.infty
return xmin, xmax
def plot_significance_vs_mass(simname, runs, nsim, nobs, kind, kwargs,
runs_to_mass):
"""
Plot significance of the 1NN distance as a function of the total mass.
@ -667,6 +692,8 @@ def plot_significance_vs_mass(simname, runs, nsim, nobs, kind, kwargs):
(Kolmogorov-Smirnov p-value).
kwargs : dict
Nearest neighbour reader keyword arguments.
runs_to_mass : dict
Dictionary mapping run names to total halo mass range.
Returns
-------
@ -686,8 +713,12 @@ def plot_significance_vs_mass(simname, runs, nsim, nobs, kind, kwargs):
y = make_kl(simname, run, nsim, nobs, kwargs)
else:
y = numpy.log10(make_ks(simname, run, nsim, nobs, kwargs))
xs.append(x)
ys.append(y)
xmin, xmax = make_binlims(run, runs_to_mass)
mask = (x >= xmin) & (x < xmax)
xs.append(x[mask])
ys.append(y[mask])
xs = numpy.concatenate(xs)
ys = numpy.concatenate(ys)
@ -715,7 +746,7 @@ def plot_significance_vs_mass(simname, runs, nsim, nobs, kind, kwargs):
plt.close()
def plot_kl_vs_ks(simname, runs, nsim, nobs, kwargs):
def plot_kl_vs_ks(simname, runs, nsim, nobs, kwargs, runs_to_mass):
"""
Plot Kullback-Leibler divergence vs Kolmogorov-Smirnov statistic p-value.
@ -731,6 +762,8 @@ def plot_kl_vs_ks(simname, runs, nsim, nobs, kwargs):
Fiducial Quijote observer index. For CSiBORG must be set to `None`.
kwargs : dict
Nearest neighbour reader keyword arguments.
runs_to_mass : dict
Dictionary mapping run names to total halo mass range.
Returns
-------
@ -741,9 +774,16 @@ def plot_kl_vs_ks(simname, runs, nsim, nobs, kwargs):
xs, ys, cs = [], [], []
for run in runs:
cs.append(reader.read_single(simname, run, nsim, nobs)["mass"])
xs.append(make_kl(simname, run, nsim, nobs, kwargs))
ys.append(make_ks(simname, run, nsim, nobs, kwargs))
c = reader.read_single(simname, run, nsim, nobs)["mass"]
x = make_kl(simname, run, nsim, nobs, kwargs)
y = make_ks(simname, run, nsim, nobs, kwargs)
cmin, cmax = make_binlims(run, runs_to_mass)
mask = (c >= cmin) & (c < cmax)
xs.append(x[mask])
ys.append(y[mask])
cs.append(c[mask])
xs = numpy.concatenate(xs)
ys = numpy.log10(numpy.concatenate(ys))
cs = numpy.log10(numpy.concatenate(cs))
@ -768,7 +808,7 @@ def plot_kl_vs_ks(simname, runs, nsim, nobs, kwargs):
plt.close()
def plot_kl_vs_overlap(runs, nsim, kwargs):
def plot_kl_vs_overlap(runs, nsim, kwargs, runs_to_mass):
"""
Plot KL divergence vs overlap for CSiBORG.
@ -780,6 +820,8 @@ def plot_kl_vs_overlap(runs, nsim, kwargs):
Simulation index.
kwargs : dict
Nearest neighbour reader keyword arguments.
runs_to_mass : dict
Dictionary mapping run names to total halo mass range.
Returns
-------
@ -802,12 +844,15 @@ def plot_kl_vs_overlap(runs, nsim, kwargs):
prob_nomatch = prob_nomatch[mask]
mass = mass[mask]
kl = make_kl("csiborg", run, nsim, nobs=None, kwargs=kwargs)
xs.append(kl)
ys1.append(1 - numpy.mean(prob_nomatch, axis=1))
ys2.append(numpy.std(prob_nomatch, axis=1))
cs.append(numpy.log10(mass))
x = make_kl("csiborg", run, nsim, nobs=None, kwargs=kwargs)
y1 = 1 - numpy.mean(prob_nomatch, axis=1)
y2 = numpy.std(prob_nomatch, axis=1)
cmin, cmax = make_binlims(run, runs_to_mass)
mask = (mass >= cmin) & (mass < cmax)
xs.append(x[mask])
ys1.append(y1[mask])
ys2.append(y2[mask])
cs.append(numpy.log10(mass[mask]))
xs = numpy.concatenate(xs)
ys1 = numpy.concatenate(ys1)
@ -877,7 +922,7 @@ if __name__ == "__main__":
delete_disk_caches_for_function(func)
# Plot 1NN distance distributions.
if False:
if True:
for i in range(1, 10):
run = f"mass00{i}"
for pulled_cdf in [True, False]:
@ -886,12 +931,12 @@ if __name__ == "__main__":
plot_dist(run, "pdf", neighbour_kwargs, runs_to_mass)
# Plot 1NN CDF differences.
if False:
if True:
runs = [f"mass00{i}" for i in range(1, 10)]
for pulled_cdf in [True, False]:
plot_cdf_diff(runs, neighbour_kwargs, pulled_cdf=pulled_cdf,
runs_to_mass=runs_to_mass)
if False:
if True:
runs = [f"mass00{i}" for i in range(1, 9)]
for kind in ["kl", "ks"]:
plot_significance("csiborg", runs, 7444, nobs=None, kind=kind,
@ -902,12 +947,14 @@ if __name__ == "__main__":
runs = [f"mass00{i}" for i in range(1, 10)]
for kind in ["kl", "ks"]:
plot_significance_vs_mass("csiborg", runs, 7444, nobs=None,
kind=kind, kwargs=neighbour_kwargs)
kind=kind, kwargs=neighbour_kwargs,
runs_to_mass=runs_to_mass)
if False:
if True:
runs = [f"mass00{i}" for i in range(1, 10)]
plot_kl_vs_ks("csiborg", runs, 7444, None, kwargs=neighbour_kwargs)
plot_kl_vs_ks("csiborg", runs, 7444, None, kwargs=neighbour_kwargs,
runs_to_mass=runs_to_mass)
if False:
if True:
runs = [f"mass00{i}" for i in range(1, 10)]
plot_kl_vs_overlap(runs, 7444, neighbour_kwargs)
plot_kl_vs_overlap(runs, 7444, neighbour_kwargs, runs_to_mass)