mirror of
https://github.com/Richard-Sti/csiborgtools.git
synced 2025-01-03 04:34:16 +00:00
Update plotting routines
This commit is contained in:
parent
f82633f816
commit
371b4bd057
1 changed files with 92 additions and 29 deletions
|
@ -32,25 +32,43 @@ except ModuleNotFoundError:
|
||||||
|
|
||||||
|
|
||||||
@cache_to_disk(7)
|
@cache_to_disk(7)
|
||||||
def read_cdf(simname, run, kwargs):
|
def read_dist(simname, run, kind, kwargs):
|
||||||
"""Read the CDFs. Caches them to disk"""
|
|
||||||
paths = csiborgtools.read.Paths(**kwargs["paths_kind"])
|
paths = csiborgtools.read.Paths(**kwargs["paths_kind"])
|
||||||
reader = csiborgtools.read.NearestNeighbourReader(**kwargs, paths=paths)
|
reader = csiborgtools.read.NearestNeighbourReader(**kwargs, paths=paths)
|
||||||
return reader.build_cdf(simname, run, verbose=True)
|
return reader.build_dist(simname, run, kind, verbose=True)
|
||||||
|
|
||||||
|
|
||||||
def plot_cdf(run, kwargs):
|
@cache_to_disk(7)
|
||||||
|
def make_kl(simname, run, nsim, nobs, kwargs):
|
||||||
|
paths = csiborgtools.read.Paths(**kwargs["paths_kind"])
|
||||||
|
reader = csiborgtools.read.NearestNeighbourReader(**kwargs, paths=paths)
|
||||||
|
|
||||||
|
pdf = read_dist("quijote", run, "pdf", kwargs)
|
||||||
|
return reader.kl_divergence(simname, run, nsim, pdf, nobs=nobs)
|
||||||
|
|
||||||
|
|
||||||
|
@cache_to_disk(7)
|
||||||
|
def make_ks(simname, run, nsim, nobs, kwargs):
|
||||||
|
paths = csiborgtools.read.Paths(**kwargs["paths_kind"])
|
||||||
|
reader = csiborgtools.read.NearestNeighbourReader(**kwargs, paths=paths)
|
||||||
|
|
||||||
|
cdf = read_dist("quijote", run, "cdf", kwargs)
|
||||||
|
return reader.ks_significance(simname, run, nsim, cdf, nobs=nobs)
|
||||||
|
|
||||||
|
|
||||||
|
def plot_dist(run, kind, kwargs):
|
||||||
"""
|
"""
|
||||||
Plot the CDF of the nearest neighbour distance for Quijote and CSiBORG.
|
Plot the PDF/CDF of the nearest neighbour distance for Quijote and CSiBORG.
|
||||||
"""
|
"""
|
||||||
print("Plotting the CDFs.", flush=True)
|
assert kind in ["pdf", "cdf"]
|
||||||
|
print(f"Plotting the {kind}.", flush=True)
|
||||||
paths = csiborgtools.read.Paths(**kwargs["paths_kind"])
|
paths = csiborgtools.read.Paths(**kwargs["paths_kind"])
|
||||||
reader = csiborgtools.read.NearestNeighbourReader(**kwargs, paths=paths)
|
reader = csiborgtools.read.NearestNeighbourReader(**kwargs, paths=paths)
|
||||||
x = reader.bin_centres("neighbour")
|
x = reader.bin_centres("neighbour")
|
||||||
|
|
||||||
y_quijote = read_cdf("quijote", run, kwargs)
|
y_quijote = read_dist("quijote", run, kind, kwargs)
|
||||||
y_csiborg = read_cdf("csiborg", run, kwargs)
|
y_csiborg = read_dist("csiborg", run, kind, kwargs)
|
||||||
ncdf = y_quijote.shape[0]
|
ncdf = y_csiborg.shape[0]
|
||||||
|
|
||||||
with plt.style.context(utils.mplstyle):
|
with plt.style.context(utils.mplstyle):
|
||||||
plt.figure()
|
plt.figure()
|
||||||
|
@ -64,40 +82,83 @@ def plot_cdf(run, kwargs):
|
||||||
plt.plot(x, y_quijote[i], c="C0", label=label1)
|
plt.plot(x, y_quijote[i], c="C0", label=label1)
|
||||||
plt.plot(x, y_csiborg[i], c="C1", label=label2)
|
plt.plot(x, y_csiborg[i], c="C1", label=label2)
|
||||||
plt.xlim(0, 75)
|
plt.xlim(0, 75)
|
||||||
plt.ylim(0, 1)
|
|
||||||
plt.xlabel(r"$r_{1\mathrm{NN}}~[\mathrm{Mpc}]$")
|
plt.xlabel(r"$r_{1\mathrm{NN}}~[\mathrm{Mpc}]$")
|
||||||
plt.ylabel(r"$\mathrm{CDF}(r_{1\mathrm{NN}})$")
|
if kind == "pdf":
|
||||||
|
plt.ylabel(r"$p(r_{1\mathrm{NN}})$")
|
||||||
|
else:
|
||||||
|
plt.ylabel(r"$\mathrm{CDF}(r_{1\mathrm{NN}})$")
|
||||||
|
plt.ylim(0, 1)
|
||||||
plt.legend()
|
plt.legend()
|
||||||
|
|
||||||
plt.tight_layout()
|
plt.tight_layout()
|
||||||
for ext in ["png"]:
|
for ext in ["png"]:
|
||||||
fout = join(utils.fout, f"1nn_cdf_{run}.{ext}")
|
fout = join(utils.fout, f"1nn_{kind}_{run}.{ext}")
|
||||||
print(f"Saving to `{fout}`.")
|
print(f"Saving to `{fout}`.")
|
||||||
plt.savefig(fout, dpi=utils.dpi, bbox_inches="tight")
|
plt.savefig(fout, dpi=utils.dpi, bbox_inches="tight")
|
||||||
plt.close()
|
plt.close()
|
||||||
|
|
||||||
|
|
||||||
def plot_significance_hist(run, nsim, kwargs):
|
def plot_significance_hist(simname, run, nsim, nobs, kind, kwargs):
|
||||||
"""
|
"""Plot a histogram of the significance of the 1NN distance."""
|
||||||
Plot the histogram of the significance of the 1NN distance for CSiBORG.
|
assert kind in ["kl", "ks"]
|
||||||
"""
|
|
||||||
paths = csiborgtools.read.Paths(**kwargs["paths_kind"])
|
paths = csiborgtools.read.Paths(**kwargs["paths_kind"])
|
||||||
reader = csiborgtools.read.NearestNeighbourReader(**kwargs, paths=paths)
|
if kind == "kl":
|
||||||
|
x = make_kl(simname, run, nsim, nobs, kwargs)
|
||||||
cdf = read_cdf("quijote", run, kwargs)
|
else:
|
||||||
|
x = make_ks(simname, run, nsim, nobs, kwargs)
|
||||||
x = reader.calc_significance("csiborg", run, nsim, cdf)
|
x = numpy.log10(x)
|
||||||
x = x[numpy.isfinite(x)]
|
x = x[numpy.isfinite(x)]
|
||||||
|
|
||||||
with plt.style.context(utils.mplstyle):
|
with plt.style.context(utils.mplstyle):
|
||||||
plt.figure()
|
plt.figure()
|
||||||
plt.hist(x, bins="auto")
|
plt.hist(x, bins="auto")
|
||||||
|
|
||||||
plt.xlabel(r"$r_{1\mathrm{NN}}$ significance $\mathrm{[\sigma]}$")
|
if kind == "ks":
|
||||||
|
plt.xlabel(r"$\log p$-value of $r_{1\mathrm{NN}}$ distribution")
|
||||||
|
else:
|
||||||
|
plt.xlabel(r"$D_{\mathrm{KL}}$ of $r_{1\mathrm{NN}}$ distribution")
|
||||||
plt.ylabel(r"Counts")
|
plt.ylabel(r"Counts")
|
||||||
plt.tight_layout()
|
plt.tight_layout()
|
||||||
|
|
||||||
for ext in ["png"]:
|
for ext in ["png"]:
|
||||||
fout = join(utils.fout, f"sigma_{run}_{str(nsim).zfill(5)}.{ext}")
|
if simname == "quijote":
|
||||||
|
nsim = paths.quijote_fiducial_nsim(nsim, nobs)
|
||||||
|
fout = join(utils.fout, f"significance_{kind}_{simname}_{run}_{str(nsim).zfill(5)}.{ext}") # noqa
|
||||||
|
print(f"Saving to `{fout}`.")
|
||||||
|
plt.savefig(fout, dpi=utils.dpi, bbox_inches="tight")
|
||||||
|
plt.close()
|
||||||
|
|
||||||
|
|
||||||
|
def plot_significance_mass(simname, run, nsim, nobs, kind, kwargs):
|
||||||
|
"""
|
||||||
|
Plot significance of the 1NN distance as a function of the total mass.
|
||||||
|
"""
|
||||||
|
assert kind in ["kl", "ks"]
|
||||||
|
paths = csiborgtools.read.Paths(**kwargs["paths_kind"])
|
||||||
|
reader = csiborgtools.read.NearestNeighbourReader(**kwargs, paths=paths)
|
||||||
|
|
||||||
|
x = reader.read_single(simname, run, nsim, nobs)["mass"]
|
||||||
|
if kind == "kl":
|
||||||
|
y = make_kl(simname, run, nsim, nobs, kwargs)
|
||||||
|
else:
|
||||||
|
y = make_ks(simname, run, nsim, nobs, kwargs)
|
||||||
|
|
||||||
|
with plt.style.context(utils.mplstyle):
|
||||||
|
plt.figure()
|
||||||
|
plt.scatter(x, y)
|
||||||
|
|
||||||
|
plt.xscale("log")
|
||||||
|
plt.xlabel(r"$M_{\rm tot} / M_\odot$")
|
||||||
|
if kind == "ks":
|
||||||
|
plt.ylabel(r"$p$-value of $r_{1\mathrm{NN}}$ distribution")
|
||||||
|
plt.yscale("log")
|
||||||
|
else:
|
||||||
|
plt.ylabel(r"$D_{\mathrm{KL}}$ of $r_{1\mathrm{NN}}$ distribution")
|
||||||
|
|
||||||
|
plt.tight_layout()
|
||||||
|
for ext in ["png"]:
|
||||||
|
if simname == "quijote":
|
||||||
|
nsim = paths.quijote_fiducial_nsim(nsim, nobs)
|
||||||
|
fout = join(utils.fout, f"significance_vs_mass_{kind}_{simname}_{run}_{str(nsim).zfill(5)}.{ext}") # noqa
|
||||||
print(f"Saving to `{fout}`.")
|
print(f"Saving to `{fout}`.")
|
||||||
plt.savefig(fout, dpi=utils.dpi, bbox_inches="tight")
|
plt.savefig(fout, dpi=utils.dpi, bbox_inches="tight")
|
||||||
plt.close()
|
plt.close()
|
||||||
|
@ -114,13 +175,15 @@ if __name__ == "__main__":
|
||||||
"nbins_neighbour": 150,
|
"nbins_neighbour": 150,
|
||||||
"paths_kind": csiborgtools.paths_glamdring}
|
"paths_kind": csiborgtools.paths_glamdring}
|
||||||
|
|
||||||
cached_funcs = ["read_cdf"]
|
cached_funcs = ["read_dist", "make_kl", "make_ks"]
|
||||||
if args.clean:
|
if args.clean:
|
||||||
for func in cached_funcs:
|
for func in cached_funcs:
|
||||||
print(f"Cleaning cache for function {func}.")
|
print(f"Cleaning cache for function `{func}`.")
|
||||||
delete_disk_caches_for_function(func)
|
delete_disk_caches_for_function(func)
|
||||||
|
|
||||||
# paths = csiborgtools.read.Paths(**kwargs["paths_kind"])
|
paths = csiborgtools.read.Paths(**kwargs["paths_kind"])
|
||||||
# reader = csiborgtools.read.NearestNeighbourReader(**kwargs, paths=paths)
|
reader = csiborgtools.read.NearestNeighbourReader(**kwargs, paths=paths)
|
||||||
|
run = "mass003"
|
||||||
|
|
||||||
plot_significance_hist("mass003", 7444, kwargs)
|
plot_significance_mass("quijote", run, 0, nobs=0, kind="ks",
|
||||||
|
kwargs=kwargs)
|
||||||
|
|
Loading…
Reference in a new issue