def to_file(self, outdir, label): """ Write the prior distribution to file. Parameters ---------- outdir: str output directory name label: str Output file naming scheme """ check_directory_exists_and_if_not_mkdir(outdir) prior_file = os.path.join(outdir, "{}.prior".format(label)) logger.debug("Writing priors to {}".format(prior_file)) joint_dists = [] with open(prior_file, "w") as outfile: for key in self.keys(): if JointPrior in self[key].__class__.__mro__: distname = '_'.join(self[key].dist.names) + '_{}'.format(self[key].dist.distname) if distname not in joint_dists: joint_dists.append(distname) outfile.write( "{} = {}\n".format(distname, self[key].dist)) diststr = repr(self[key].dist) priorstr = repr(self[key]) outfile.write( "{} = {}\n".format(key, priorstr.replace(diststr, distname))) else: outfile.write( "{} = {}\n".format(key, self[key]))
def to_json(self, outdir, label): check_directory_exists_and_if_not_mkdir(outdir) prior_file = os.path.join(outdir, "{}_prior.json".format(label)) logger.debug("Writing priors to {}".format(prior_file)) with open(prior_file, "w") as outfile: json.dump(self._get_json_dict(), outfile, cls=BilbyJsonEncoder, indent=2)
def _parse_and_load(): args, unknown_args = parse_args(get_command_line_arguments(), create_parser()) logger.info(f"Generating plots for results file {args.result}") result = CBCResult.from_json(args.result) if "data_dump" in result.meta_data and os.path.exists( result.meta_data["data_dump"]): data_dump = DataDump.from_pickle(result.meta_data["data_dump"]) else: data_dump = None if hasattr(args, "webdir"): outdir = os.path.join(args.webdir, "bilby") elif hasattr(args, "outdir"): outdir = args.outdir else: outdir = result.outdir logger.info(f"Plots will be made in {outdir}") check_directory_exists_and_if_not_mkdir(outdir) result.outdir = outdir return args, result, data_dump
#!/usr/bin/env python """ An example of how to use bilby to perform parameter estimation for hyper params """ import matplotlib.pyplot as plt import numpy as np from bilby.core.likelihood import GaussianLikelihood from bilby.core.prior import Uniform from bilby.core.result import make_pp_plot from bilby.core.sampler import run_sampler from bilby.core.utils import check_directory_exists_and_if_not_mkdir from bilby.hyper.likelihood import HyperparameterLikelihood outdir = "outdir" check_directory_exists_and_if_not_mkdir(outdir) # Define a model to fit to each data set def model(x, c0, c1): return c0 + c1 * x N = 10 x = np.linspace(0, 10, N) sigma = 1 Nevents = 4 labels = ["a", "b", "c", "d"] true_mu_c0 = 5 true_sigma_c0 = 1