2024-03-08 10:44:19 +00:00
|
|
|
# Copyright (C) 2024 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.
|
|
|
|
"""
|
|
|
|
Script to run the PV validation model on various catalogues and simulations.
|
2024-06-26 09:43:26 +00:00
|
|
|
The script is not MPI parallelised, instead it is best run on a GPU.
|
2024-03-08 10:44:19 +00:00
|
|
|
"""
|
2024-06-26 09:43:26 +00:00
|
|
|
from argparse import ArgumentParser, ArgumentTypeError
|
|
|
|
|
|
|
|
|
|
|
|
def none_or_int(value):
|
|
|
|
if value.lower() == "none":
|
|
|
|
return None
|
2024-09-16 10:12:32 +00:00
|
|
|
|
|
|
|
if "_" in value:
|
|
|
|
args = value.split("_")
|
|
|
|
if len(args) == 2:
|
|
|
|
k0, kf = args
|
|
|
|
dk = 1
|
|
|
|
elif len(args) == 3:
|
|
|
|
k0, kf, dk = args
|
|
|
|
else:
|
|
|
|
raise ArgumentTypeError(f"Invalid length of arguments: `{value}`.")
|
|
|
|
|
|
|
|
return [int(k) for k in range(int(k0), int(kf), int(dk))]
|
|
|
|
|
2024-06-26 09:43:26 +00:00
|
|
|
try:
|
|
|
|
return int(value)
|
|
|
|
except ValueError:
|
|
|
|
raise ArgumentTypeError(f"Invalid value: {value}. Must be an integer or 'none'.") # noqa
|
|
|
|
|
|
|
|
|
|
|
|
def parse_args():
|
|
|
|
parser = ArgumentParser()
|
|
|
|
parser.add_argument("--simname", type=str, required=True,
|
|
|
|
help="Simulation name.")
|
|
|
|
parser.add_argument("--catalogue", type=str, required=True,
|
2024-07-30 16:02:48 +00:00
|
|
|
help="PV catalogues.")
|
2024-09-11 06:45:42 +00:00
|
|
|
parser.add_argument("--ksmooth", type=int, default=0,
|
2024-06-26 09:43:26 +00:00
|
|
|
help="Smoothing index.")
|
|
|
|
parser.add_argument("--ksim", type=none_or_int, default=None,
|
|
|
|
help="IC iteration number. If 'None', all IC realizations are used.") # noqa
|
|
|
|
parser.add_argument("--ndevice", type=int, default=1,
|
|
|
|
help="Number of devices to request.")
|
|
|
|
parser.add_argument("--device", type=str, default="cpu",
|
|
|
|
help="Device to use.")
|
2024-07-30 16:02:48 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# Convert the catalogue to a list of catalogues
|
|
|
|
args.catalogue = args.catalogue.split(",")
|
|
|
|
|
|
|
|
return args
|
2024-06-26 09:43:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
ARGS = parse_args()
|
|
|
|
# This must be done before we import JAX etc.
|
|
|
|
from numpyro import set_host_device_count, set_platform # noqa
|
2024-07-01 10:48:50 +00:00
|
|
|
|
2024-06-26 09:43:26 +00:00
|
|
|
set_platform(ARGS.device) # noqa
|
|
|
|
set_host_device_count(ARGS.ndevice) # noqa
|
|
|
|
|
|
|
|
import sys # noqa
|
|
|
|
from os.path import join # noqa
|
|
|
|
|
2024-07-01 10:48:50 +00:00
|
|
|
import csiborgtools # noqa
|
2024-06-26 09:43:26 +00:00
|
|
|
import jax # noqa
|
2024-09-21 16:12:15 +00:00
|
|
|
import numpy as np # noqa
|
|
|
|
from csiborgtools import fprint # noqa
|
2024-06-26 09:43:26 +00:00
|
|
|
from h5py import File # noqa
|
2024-07-30 16:02:48 +00:00
|
|
|
from numpyro.infer import MCMC, NUTS, init_to_median # noqa
|
2024-06-26 09:43:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def print_variables(names, variables):
|
|
|
|
for name, variable in zip(names, variables):
|
|
|
|
print(f"{name:<20} {variable}", flush=True)
|
|
|
|
print(flush=True)
|
|
|
|
|
|
|
|
|
2024-09-21 16:12:15 +00:00
|
|
|
def get_models(ksim, get_model_kwargs, mag_selection, void_kwargs,
|
|
|
|
verbose=True):
|
2024-07-30 16:02:48 +00:00
|
|
|
"""Load the data and create the NumPyro models."""
|
2024-06-26 09:43:26 +00:00
|
|
|
paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)
|
2024-03-08 10:44:19 +00:00
|
|
|
folder = "/mnt/extraspace/rstiskalek/catalogs/"
|
2024-06-26 09:43:26 +00:00
|
|
|
|
|
|
|
nsims = paths.get_ics(ARGS.simname)
|
2024-09-16 10:12:32 +00:00
|
|
|
if ksim is None:
|
2024-06-26 09:43:26 +00:00
|
|
|
nsim_iterator = [i for i in range(len(nsims))]
|
|
|
|
else:
|
2024-09-16 10:12:32 +00:00
|
|
|
nsim_iterator = [ksim]
|
|
|
|
nsims = [nsims[ksim]]
|
2024-06-26 09:43:26 +00:00
|
|
|
|
|
|
|
if verbose:
|
|
|
|
print(f"{'Simulation:':<20} {ARGS.simname}")
|
|
|
|
print(f"{'Catalogue:':<20} {ARGS.catalogue}")
|
|
|
|
print(f"{'Num. realisations:':<20} {len(nsims)}")
|
|
|
|
print(flush=True)
|
|
|
|
|
2024-07-30 16:02:48 +00:00
|
|
|
# Get models
|
|
|
|
models = [None] * len(ARGS.catalogue)
|
|
|
|
for i, cat in enumerate(ARGS.catalogue):
|
|
|
|
if cat == "A2":
|
|
|
|
fpath = join(folder, "A2.h5")
|
|
|
|
elif cat in ["LOSS", "Foundation", "Pantheon+", "SFI_gals",
|
|
|
|
"2MTF", "SFI_groups", "SFI_gals_masked",
|
|
|
|
"Pantheon+_groups", "Pantheon+_groups_zSN",
|
|
|
|
"Pantheon+_zSN"]:
|
|
|
|
fpath = join(folder, "PV_compilation.hdf5")
|
2024-08-25 15:03:51 +00:00
|
|
|
elif "CF4_TFR" in cat:
|
|
|
|
fpath = join(folder, "PV/CF4/CF4_TF-distances.hdf5")
|
|
|
|
elif cat in ["CF4_GroupAll"]:
|
|
|
|
fpath = join(folder, "PV/CF4/CF4_GroupAll.hdf5")
|
2024-07-30 16:02:48 +00:00
|
|
|
else:
|
|
|
|
raise ValueError(f"Unsupported catalogue: `{ARGS.catalogue}`.")
|
|
|
|
|
|
|
|
loader = csiborgtools.flow.DataLoader(ARGS.simname, nsim_iterator,
|
|
|
|
cat, fpath, paths,
|
|
|
|
ksmooth=ARGS.ksmooth)
|
2024-08-26 22:36:00 +00:00
|
|
|
models[i] = csiborgtools.flow.get_model(
|
2024-09-21 16:12:15 +00:00
|
|
|
loader, mag_selection=mag_selection[i], void_kwargs=void_kwargs,
|
|
|
|
**get_model_kwargs)
|
2024-03-08 10:44:19 +00:00
|
|
|
|
2024-09-16 10:12:32 +00:00
|
|
|
fprint(f"num. radial steps is {len(loader.rdist)}")
|
2024-07-30 16:02:48 +00:00
|
|
|
return models
|
2024-03-08 10:44:19 +00:00
|
|
|
|
|
|
|
|
2024-09-21 16:12:15 +00:00
|
|
|
def select_void_h(kind):
|
|
|
|
hs = {"mb": 0.7615, "gauss": 0.7724, "exp": 0.7725}
|
|
|
|
try:
|
|
|
|
return hs[kind]
|
|
|
|
except KeyError:
|
|
|
|
raise ValueError(f"Unknown void kind: `{kind}`.")
|
|
|
|
|
|
|
|
|
2024-06-26 09:43:26 +00:00
|
|
|
def get_harmonic_evidence(samples, log_posterior, nchains_harmonic, epoch_num):
|
|
|
|
"""Compute evidence using the `harmonic` package."""
|
|
|
|
data, names = csiborgtools.dict_samples_to_array(samples)
|
|
|
|
data = data.reshape(nchains_harmonic, -1, len(names))
|
2024-07-30 16:02:48 +00:00
|
|
|
log_posterior = log_posterior.reshape(nchains_harmonic, -1)
|
2024-03-08 10:44:19 +00:00
|
|
|
|
2024-06-26 09:43:26 +00:00
|
|
|
return csiborgtools.harmonic_evidence(
|
|
|
|
data, log_posterior, return_flow_samples=False, epochs_num=epoch_num)
|
2024-03-08 10:44:19 +00:00
|
|
|
|
|
|
|
|
2024-09-11 06:45:42 +00:00
|
|
|
def run_model(model, nsteps, nburn, model_kwargs, out_folder,
|
|
|
|
calculate_harmonic, nchains_harmonic, epoch_num, kwargs_print,
|
|
|
|
fname_kwargs):
|
2024-06-26 09:43:26 +00:00
|
|
|
"""Run the NumPyro model and save output to a file."""
|
2024-09-11 06:45:42 +00:00
|
|
|
paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)
|
|
|
|
|
|
|
|
fname = paths.flow_validation(out_folder, ARGS.simname, ARGS.catalogue,
|
|
|
|
**fname_kwargs)
|
|
|
|
|
2024-03-26 14:11:04 +00:00
|
|
|
try:
|
2024-07-30 16:02:48 +00:00
|
|
|
ndata = sum(model.ndata for model in model_kwargs["models"])
|
2024-06-26 09:43:26 +00:00
|
|
|
except AttributeError as e:
|
2024-07-30 16:02:48 +00:00
|
|
|
raise AttributeError("The models must have an attribute `ndata` "
|
2024-06-26 09:43:26 +00:00
|
|
|
"indicating the number of data points.") from e
|
|
|
|
|
2024-09-16 10:12:32 +00:00
|
|
|
nuts_kernel = NUTS(model, init_strategy=init_to_median(num_samples=10000))
|
2024-06-26 09:43:26 +00:00
|
|
|
mcmc = MCMC(nuts_kernel, num_warmup=nburn, num_samples=nsteps)
|
|
|
|
rng_key = jax.random.PRNGKey(42)
|
|
|
|
|
|
|
|
mcmc.run(rng_key, extra_fields=("potential_energy",), **model_kwargs)
|
|
|
|
samples = mcmc.get_samples()
|
|
|
|
|
|
|
|
log_posterior = -mcmc.get_extra_fields()["potential_energy"]
|
2024-08-25 15:03:51 +00:00
|
|
|
BIC, AIC = csiborgtools.BIC_AIC(samples, log_posterior, ndata)
|
2024-06-26 09:43:26 +00:00
|
|
|
print(f"{'BIC':<20} {BIC}")
|
|
|
|
print(f"{'AIC':<20} {AIC}")
|
|
|
|
mcmc.print_summary()
|
|
|
|
|
2024-08-26 22:36:00 +00:00
|
|
|
if calculate_harmonic:
|
2024-06-26 09:43:26 +00:00
|
|
|
print("Calculating the evidence using `harmonic`.", flush=True)
|
2024-07-01 10:48:50 +00:00
|
|
|
neg_ln_evidence, neg_ln_evidence_err = get_harmonic_evidence(
|
2024-06-26 09:43:26 +00:00
|
|
|
samples, log_posterior, nchains_harmonic, epoch_num)
|
2024-08-26 22:36:00 +00:00
|
|
|
print(f"{'-ln(Z_h)':<20} {neg_ln_evidence}")
|
|
|
|
print(f"{'-ln(Z_h) error':<20} {neg_ln_evidence_err}")
|
2024-06-26 09:43:26 +00:00
|
|
|
else:
|
2024-07-01 10:48:50 +00:00
|
|
|
neg_ln_evidence = jax.numpy.nan
|
|
|
|
neg_ln_evidence_err = (jax.numpy.nan, jax.numpy.nan)
|
2024-06-26 09:43:26 +00:00
|
|
|
|
|
|
|
fname = join(out_folder, fname)
|
|
|
|
print(f"Saving results to `{fname}`.")
|
|
|
|
with File(fname, "w") as f:
|
|
|
|
# Write samples
|
|
|
|
grp = f.create_group("samples")
|
|
|
|
for key, value in samples.items():
|
|
|
|
grp.create_dataset(key, data=value)
|
|
|
|
|
|
|
|
# Write log likelihood and posterior
|
|
|
|
f.create_dataset("log_posterior", data=log_posterior)
|
|
|
|
|
|
|
|
# Write goodness of fit
|
|
|
|
grp = f.create_group("gof")
|
|
|
|
grp.create_dataset("BIC", data=BIC)
|
|
|
|
grp.create_dataset("AIC", data=AIC)
|
2024-08-26 22:36:00 +00:00
|
|
|
grp.create_dataset("neg_lnZ_harmonic", data=neg_ln_evidence)
|
|
|
|
grp.create_dataset("neg_lnZ_harmonic_err", data=neg_ln_evidence_err)
|
2024-06-26 09:43:26 +00:00
|
|
|
|
|
|
|
fname_summary = fname.replace(".hdf5", ".txt")
|
|
|
|
print(f"Saving summary to `{fname_summary}`.")
|
|
|
|
with open(fname_summary, 'w') as f:
|
|
|
|
original_stdout = sys.stdout
|
|
|
|
sys.stdout = f
|
|
|
|
|
|
|
|
print("User parameters:")
|
|
|
|
for kwargs in kwargs_print:
|
|
|
|
print_variables(kwargs.keys(), kwargs.values())
|
|
|
|
|
|
|
|
print("HMC summary:")
|
|
|
|
print(f"{'BIC':<20} {BIC}")
|
|
|
|
print(f"{'AIC':<20} {AIC}")
|
2024-07-01 10:48:50 +00:00
|
|
|
print(f"{'-ln(Z)':<20} {neg_ln_evidence}")
|
|
|
|
print(f"{'-ln(Z) error':<20} {neg_ln_evidence_err}")
|
2024-06-26 09:43:26 +00:00
|
|
|
mcmc.print_summary(exclude_deterministic=False)
|
|
|
|
sys.stdout = original_stdout
|
|
|
|
|
2024-03-08 10:44:19 +00:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Command line interface #
|
|
|
|
###############################################################################
|
|
|
|
|
2024-08-26 22:36:00 +00:00
|
|
|
def get_distmod_hyperparams(catalogue, sample_alpha, sample_mag_dipole):
|
2024-09-26 19:45:12 +00:00
|
|
|
alpha_min = -10 if "IndranilVoid" in ARGS.simname else -1.0
|
2024-09-11 06:45:42 +00:00
|
|
|
alpha_max = 10.0
|
2024-07-30 16:02:48 +00:00
|
|
|
|
|
|
|
if catalogue in ["LOSS", "Foundation", "Pantheon+", "Pantheon+_groups", "Pantheon+_zSN"]: # noqa
|
|
|
|
return {"e_mu_min": 0.001, "e_mu_max": 1.0,
|
|
|
|
"mag_cal_mean": -18.25, "mag_cal_std": 2.0,
|
|
|
|
"alpha_cal_mean": 0.148, "alpha_cal_std": 1.0,
|
|
|
|
"beta_cal_mean": 3.112, "beta_cal_std": 2.0,
|
|
|
|
"alpha_min": alpha_min, "alpha_max": alpha_max,
|
|
|
|
"sample_alpha": sample_alpha
|
|
|
|
}
|
2024-08-25 15:03:51 +00:00
|
|
|
elif catalogue in ["SFI_gals", "2MTF"] or "CF4_TFR" in catalogue:
|
2024-07-30 16:02:48 +00:00
|
|
|
return {"e_mu_min": 0.001, "e_mu_max": 1.0,
|
|
|
|
"a_mean": -21., "a_std": 5.0,
|
2024-08-25 15:03:51 +00:00
|
|
|
"b_mean": -5.95, "b_std": 4.0,
|
|
|
|
"c_mean": 0., "c_std": 20.0,
|
|
|
|
"a_dipole_mean": 0., "a_dipole_std": 1.0,
|
2024-08-26 22:36:00 +00:00
|
|
|
"sample_a_dipole": sample_mag_dipole,
|
2024-07-30 16:02:48 +00:00
|
|
|
"alpha_min": alpha_min, "alpha_max": alpha_max,
|
2024-08-25 15:03:51 +00:00
|
|
|
"sample_alpha": sample_alpha,
|
|
|
|
}
|
|
|
|
elif catalogue in ["CF4_GroupAll"]:
|
|
|
|
return {"e_mu_min": 0.001, "e_mu_max": 1.0,
|
|
|
|
"dmu_min": -3.0, "dmu_max": 3.0,
|
|
|
|
"dmu_dipole_mean": 0., "dmu_dipole_std": 1.0,
|
2024-08-26 22:36:00 +00:00
|
|
|
"sample_dmu_dipole": sample_mag_dipole,
|
2024-08-25 15:03:51 +00:00
|
|
|
"alpha_min": alpha_min, "alpha_max": alpha_max,
|
|
|
|
"sample_alpha": sample_alpha,
|
2024-07-30 16:02:48 +00:00
|
|
|
}
|
|
|
|
else:
|
|
|
|
raise ValueError(f"Unsupported catalogue: `{ARGS.catalogue}`.")
|
|
|
|
|
2024-03-08 10:44:19 +00:00
|
|
|
|
2024-09-11 06:45:42 +00:00
|
|
|
def get_toy_selection(catalogue):
|
|
|
|
"""Toy magnitude selection coefficients."""
|
2024-08-26 22:36:00 +00:00
|
|
|
if catalogue == "SFI_gals":
|
2024-09-11 06:45:42 +00:00
|
|
|
kind = "soft"
|
|
|
|
# m1, m2, a
|
|
|
|
coeffs = [11.467, 12.906, -0.231]
|
|
|
|
elif "CF4_TFR" in catalogue and "_i" in catalogue:
|
|
|
|
kind = "soft"
|
|
|
|
coeffs = [13.043, 14.423, -0.129]
|
|
|
|
elif "CF4_TFR" in catalogue and "w1" in catalogue:
|
|
|
|
kind = "soft"
|
|
|
|
coeffs = [11.731, 14.189, -0.118]
|
|
|
|
elif catalogue == "2MTF":
|
|
|
|
kind = "hard"
|
|
|
|
coeffs = 11.25
|
2024-08-26 22:36:00 +00:00
|
|
|
else:
|
2024-09-11 06:45:42 +00:00
|
|
|
fprint(f"found no selection coefficients for {catalogue}.")
|
|
|
|
return None
|
|
|
|
|
|
|
|
return {"kind": kind,
|
|
|
|
"coeffs": coeffs}
|
2024-08-26 22:36:00 +00:00
|
|
|
|
|
|
|
|
2024-03-08 10:44:19 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)
|
2024-06-26 09:43:26 +00:00
|
|
|
out_folder = "/mnt/extraspace/rstiskalek/csiborg_postprocessing/peculiar_velocity" # noqa
|
|
|
|
print(f"{'Num. devices:':<20} {jax.device_count()}")
|
|
|
|
print(f"{'Devices:':<20} {jax.devices()}")
|
|
|
|
|
|
|
|
###########################################################################
|
|
|
|
# Fixed user parameters #
|
|
|
|
###########################################################################
|
|
|
|
|
2024-09-11 06:45:42 +00:00
|
|
|
# `None` means default behaviour
|
|
|
|
nsteps = 10_000
|
|
|
|
nburn = 2_000
|
|
|
|
zcmb_min = None
|
2024-07-30 16:02:48 +00:00
|
|
|
zcmb_max = 0.05
|
2024-06-26 09:43:26 +00:00
|
|
|
nchains_harmonic = 10
|
2024-08-26 22:36:00 +00:00
|
|
|
num_epochs = 50
|
2024-09-25 13:30:06 +00:00
|
|
|
inference_method = "mike"
|
2024-09-11 06:45:42 +00:00
|
|
|
mag_selection = None
|
2024-10-02 17:07:52 +00:00
|
|
|
sample_alpha = False if (ARGS.simname == "no_field" or "IndranilVoid" in ARGS.simname) else True # noqa
|
2024-09-11 06:45:42 +00:00
|
|
|
sample_beta = None
|
2024-09-17 09:26:45 +00:00
|
|
|
no_Vext = None
|
2024-09-23 10:33:48 +00:00
|
|
|
sample_Vmag_vax = False
|
2024-08-25 15:03:51 +00:00
|
|
|
sample_Vmono = False
|
2024-08-26 22:36:00 +00:00
|
|
|
sample_mag_dipole = False
|
2024-09-16 10:12:32 +00:00
|
|
|
absolute_calibration = None
|
2024-09-11 06:45:42 +00:00
|
|
|
calculate_harmonic = False if inference_method == "bayes" else True
|
2024-09-16 10:12:32 +00:00
|
|
|
sample_h = True if absolute_calibration is not None else False
|
2024-09-11 06:45:42 +00:00
|
|
|
|
|
|
|
fname_kwargs = {"inference_method": inference_method,
|
|
|
|
"smooth": ARGS.ksmooth,
|
|
|
|
"nsim": ARGS.ksim,
|
|
|
|
"zcmb_min": zcmb_min,
|
|
|
|
"zcmb_max": zcmb_max,
|
|
|
|
"mag_selection": mag_selection,
|
|
|
|
"sample_alpha": sample_alpha,
|
|
|
|
"sample_beta": sample_beta,
|
2024-09-16 19:17:18 +00:00
|
|
|
"no_Vext": no_Vext,
|
2024-09-23 10:33:48 +00:00
|
|
|
"sample_Vmag_vax": sample_Vmag_vax,
|
2024-09-11 06:45:42 +00:00
|
|
|
"sample_Vmono": sample_Vmono,
|
|
|
|
"sample_mag_dipole": sample_mag_dipole,
|
2024-09-16 10:12:32 +00:00
|
|
|
"absolute_calibration": absolute_calibration,
|
2024-09-11 06:45:42 +00:00
|
|
|
}
|
2024-06-26 09:43:26 +00:00
|
|
|
|
2024-08-25 15:03:51 +00:00
|
|
|
main_params = {"nsteps": nsteps, "nburn": nburn,
|
|
|
|
"zcmb_min": zcmb_min,
|
|
|
|
"zcmb_max": zcmb_max,
|
2024-09-11 06:45:42 +00:00
|
|
|
"mag_selection": mag_selection,
|
2024-08-26 22:36:00 +00:00
|
|
|
"calculate_harmonic": calculate_harmonic,
|
2024-06-26 09:43:26 +00:00
|
|
|
"nchains_harmonic": nchains_harmonic,
|
2024-08-25 15:03:51 +00:00
|
|
|
"num_epochs": num_epochs,
|
2024-08-26 22:36:00 +00:00
|
|
|
"inference_method": inference_method,
|
|
|
|
"sample_mag_dipole": sample_mag_dipole,
|
2024-09-16 10:12:32 +00:00
|
|
|
"absolute_calibration": absolute_calibration,
|
|
|
|
"sample_h": sample_h,
|
2024-09-11 06:45:42 +00:00
|
|
|
}
|
2024-06-26 09:43:26 +00:00
|
|
|
print_variables(main_params.keys(), main_params.values())
|
|
|
|
|
2024-09-11 06:45:42 +00:00
|
|
|
if sample_beta is None:
|
|
|
|
sample_beta = ARGS.simname == "Carrick2015"
|
|
|
|
|
|
|
|
if mag_selection and inference_method != "bayes":
|
|
|
|
raise ValueError("Magnitude selection is only supported with `bayes` inference.") # noqa
|
|
|
|
|
2024-09-21 16:12:15 +00:00
|
|
|
if "IndranilVoid" in ARGS.simname:
|
|
|
|
if ARGS.ksim is not None:
|
|
|
|
raise ValueError(
|
|
|
|
"`IndranilVoid` does not have multiple realisations.")
|
|
|
|
|
2024-09-26 10:58:22 +00:00
|
|
|
profile = ARGS.simname.split("_")[-1]
|
|
|
|
h = select_void_h(profile)
|
2024-09-21 16:12:15 +00:00
|
|
|
rdist = np.arange(0, 165, 0.5)
|
2024-09-26 10:58:22 +00:00
|
|
|
void_kwargs = {"profile": profile, "h": h, "order": 1, "rdist": rdist}
|
2024-09-21 16:12:15 +00:00
|
|
|
else:
|
|
|
|
void_kwargs = None
|
|
|
|
h = 1.
|
2024-09-11 14:21:24 +00:00
|
|
|
|
2024-09-11 06:45:42 +00:00
|
|
|
if inference_method != "bayes":
|
|
|
|
mag_selection = [None] * len(ARGS.catalogue)
|
|
|
|
elif mag_selection is None or mag_selection:
|
|
|
|
mag_selection = [get_toy_selection(cat) for cat in ARGS.catalogue]
|
|
|
|
|
|
|
|
if nsteps % nchains_harmonic != 0:
|
|
|
|
raise ValueError(
|
|
|
|
"The number of steps must be divisible by the number of chains.")
|
|
|
|
|
|
|
|
calibration_hyperparams = {"Vext_min": -3000, "Vext_max": 3000,
|
2024-07-13 20:52:42 +00:00
|
|
|
"Vmono_min": -1000, "Vmono_max": 1000,
|
2024-09-11 06:45:42 +00:00
|
|
|
"beta_min": -10.0, "beta_max": 10.0,
|
2024-09-17 20:01:10 +00:00
|
|
|
"sigma_v_min": 1.0, "sigma_v_max": 5000 if "IndranilVoid_" in ARGS.simname else 750., # noqa
|
2024-09-16 10:12:32 +00:00
|
|
|
"h_min": 0.01, "h_max": 5.0,
|
2024-09-23 10:33:48 +00:00
|
|
|
"no_Vext": False if no_Vext is None else no_Vext, # noqa
|
|
|
|
"sample_Vmag_vax": sample_Vmag_vax,
|
2024-08-25 15:03:51 +00:00
|
|
|
"sample_Vmono": sample_Vmono,
|
|
|
|
"sample_beta": sample_beta,
|
2024-09-16 10:12:32 +00:00
|
|
|
"sample_h": sample_h,
|
2024-09-21 16:12:15 +00:00
|
|
|
"sample_rLG": "IndranilVoid" in ARGS.simname,
|
|
|
|
"rLG_min": 0.0, "rLG_max": 500 * h,
|
2024-06-26 09:43:26 +00:00
|
|
|
}
|
|
|
|
print_variables(
|
|
|
|
calibration_hyperparams.keys(), calibration_hyperparams.values())
|
|
|
|
|
2024-07-30 16:02:48 +00:00
|
|
|
distmod_hyperparams_per_catalogue = []
|
|
|
|
for cat in ARGS.catalogue:
|
2024-08-26 22:36:00 +00:00
|
|
|
x = get_distmod_hyperparams(cat, sample_alpha, sample_mag_dipole)
|
2024-07-30 16:02:48 +00:00
|
|
|
print(f"\n{cat} hyperparameters:")
|
|
|
|
print_variables(x.keys(), x.values())
|
|
|
|
distmod_hyperparams_per_catalogue.append(x)
|
2024-03-08 10:44:19 +00:00
|
|
|
|
2024-07-30 16:02:48 +00:00
|
|
|
kwargs_print = (main_params, calibration_hyperparams,
|
|
|
|
*distmod_hyperparams_per_catalogue)
|
2024-08-26 22:36:00 +00:00
|
|
|
|
2024-09-11 06:45:42 +00:00
|
|
|
###########################################################################
|
2024-08-26 22:36:00 +00:00
|
|
|
|
2024-09-16 10:12:32 +00:00
|
|
|
get_model_kwargs = {
|
|
|
|
"zcmb_min": zcmb_min,
|
|
|
|
"zcmb_max": zcmb_max,
|
|
|
|
"absolute_calibration": absolute_calibration,
|
|
|
|
"calibration_fpath": "/mnt/extraspace/rstiskalek/catalogs/PV/CF4/CF4_TF_calibration.hdf5", # noqa
|
2024-07-30 16:02:48 +00:00
|
|
|
}
|
|
|
|
|
2024-09-16 10:12:32 +00:00
|
|
|
# In case we want to run multiple simulations independently.
|
|
|
|
if not isinstance(ARGS.ksim, list):
|
|
|
|
ksim_iterator = [ARGS.ksim]
|
|
|
|
else:
|
|
|
|
ksim_iterator = ARGS.ksim
|
|
|
|
|
|
|
|
for i, ksim in enumerate(ksim_iterator):
|
|
|
|
if len(ksim_iterator) > 1:
|
|
|
|
print(f"{'Current simulation:':<20} {i + 1} ({ksim}) out of {len(ksim_iterator)}.") # noqa
|
|
|
|
|
|
|
|
fname_kwargs["nsim"] = ksim
|
2024-09-21 16:12:15 +00:00
|
|
|
models = get_models(ksim, get_model_kwargs, mag_selection, void_kwargs)
|
2024-09-16 10:12:32 +00:00
|
|
|
model_kwargs = {
|
|
|
|
"models": models,
|
|
|
|
"field_calibration_hyperparams": calibration_hyperparams,
|
|
|
|
"distmod_hyperparams_per_model": distmod_hyperparams_per_catalogue,
|
|
|
|
"inference_method": inference_method,
|
|
|
|
}
|
|
|
|
|
|
|
|
model = csiborgtools.flow.PV_validation_model
|
|
|
|
|
|
|
|
run_model(model, nsteps, nburn, model_kwargs, out_folder,
|
|
|
|
calculate_harmonic, nchains_harmonic, num_epochs,
|
|
|
|
kwargs_print, fname_kwargs)
|