示例#1
0
    def __init__(self,
                 basedir=None,
                 p1d_label=None,
                 skewers_label=None,
                 verbose=False,
                 kmax_Mpc=10.0,
                 paramList=None,
                 train=False,
                 max_arxiv_size=None,
                 drop_tau_rescalings=False,
                 drop_temp_rescalings=False,
                 keep_every_other_rescaling=False,
                 emu_type="k_bin",
                 set_noise_var=1e-3,
                 N_mf=10):

        # setup central mean flux and edges
        self._setup_mean_flux_bins(N_mf)
        if verbose:
            for i in range(self.N_mf):
                print(i, self.cen_mf[i], self.min_mf[i], '<mf<',
                      self.max_mf[i])

        # load full arxiv
        self.arxiv = p1d_arxiv.ArxivP1D(
            basedir=basedir,
            p1d_label=p1d_label,
            skewers_label=skewers_label,
            verbose=verbose,
            max_arxiv_size=max_arxiv_size,
            drop_tau_rescalings=drop_tau_rescalings,
            drop_temp_rescalings=drop_temp_rescalings,
            keep_every_other_rescaling=keep_every_other_rescaling)

        self.emulators = []
        for i in range(self.N_mf):
            # select entries within mean flux range
            mf_arxiv = self.arxiv.sub_arxiv_mf(min_mf=self.min_mf[i],
                                               max_mf=self.max_mf[i])

            if verbose:
                print('build emulator %d/%d, <F>=%.3f' %
                      (i, N_mf, self.cen_mf[i]))

            # create GP emulator using only entries in mean flux range
            mf_emu = gp_emulator.GPEmulator(verbose=verbose,
                                            kmax_Mpc=kmax_Mpc,
                                            paramList=paramList,
                                            train=train,
                                            emu_type=emu_type,
                                            set_noise_var=set_noise_var,
                                            passArxiv=mf_arxiv)

            self.emulators.append(mf_emu)

        self.verbose = verbose
示例#2
0
                             drop_temp_rescalings=args.drop_temp_rescalings,
                             skewers_label=skewers_label,
                             undersample_cube=args.undersample_cube,
                             undersample_z=args.undersample_z)

## Set up an emulator
if args.z_emulator == False:
    emu = gp_emulator.GPEmulator(
        args.basedir,
        p1d_label,
        skewers_label,
        z_max=args.z_max,
        passArxiv=archive,
        verbose=False,
        paramList=paramList,
        train=True,
        emu_type=args.emu_type,
        checkHulls=False,
        kmax_Mpc=kmax_Mpc,
        asymmetric_kernel=args.asym_kernel,
        rbf_only=args.asym_kernel,
        drop_tau_rescalings=args.drop_tau_rescalings,
        drop_temp_rescalings=args.drop_temp_rescalings,
        set_noise_var=args.emu_noise_var)
else:
    emu = z_emulator.ZEmulator(args.basedir,
                               p1d_label,
                               skewers_label,
                               z_max=args.z_max,
                               verbose=False,
                               paramList=paramList,
                                sim_label=test_sim_label,
                                zmax=z_max)

# Set up emulator training set
z_max=4
arxiv=p1d_arxiv.ArxivP1D(basedir=basedir,drop_sim_number=drop_sim_number,
                            drop_tau_rescalings=True,z_max=z_max,
                            drop_temp_rescalings=True,skewers_label=skewers_label)

## Build emulator
paramList=['mF', 'sigT_Mpc', 'gamma', 'kF_Mpc', 'Delta2_p', 'n_p']
# specify k range
kmax_Mpc=8
emu=gp_emulator.GPEmulator(basedir,p1d_label,skewers_label,z_max=z_max,
                                verbose=False,paramList=paramList,train=True,
                                asymmetric_kernel=True,rbf_only=True,
                                emu_type="k_bin",passArxiv=arxiv,
                                kmax_Mpc=kmax_Mpc)

## Set up likelihood object
free_param_names=["As","ns"]
free_param_limits=[[1.1e-09, 3.19e-09], [0.89, 1.05]]

prior=None ## None for uniform prior, otherwise this value sets the width of the Gaussian within the unit prior volume
like=likelihood.Likelihood(data=data,emulator=emu,
                            free_param_names=free_param_names,
                            free_param_limits=free_param_limits,
                            prior_Gauss_rms=prior)

## Set up sampler
sampler = emcee_sampler.EmceeSampler(like=like,rootdir=rootdir,
示例#4
0
    def __init__(self,
                 basedir=None,
                 p1d_label=None,
                 skewers_label=None,
                 verbose=False,
                 kmax_Mpc=10.0,
                 paramList=None,
                 train=False,
                 max_arxiv_size=None,
                 z_max=5,
                 passArxiv=None,
                 drop_tau_rescalings=False,
                 drop_temp_rescalings=False,
                 keep_every_other_rescaling=False,
                 checkHulls=False,
                 emu_type="k_bin",
                 set_noise_var=1e-10,
                 N_mf=10,
                 z_list=None,
                 paramLimits=None):

        # read all files with P1D measured in simulation suite
        if passArxiv == None:
            self.custom_arxiv = False
            self.arxiv = p1d_arxiv.ArxivP1D(
                basedir,
                p1d_label,
                skewers_label,
                max_arxiv_size=max_arxiv_size,
                verbose=verbose,
                drop_tau_rescalings=drop_tau_rescalings,
                drop_temp_rescalings=drop_temp_rescalings,
                z_max=z_max,
                keep_every_other_rescaling=keep_every_other_rescaling)
        else:
            self.custom_arxiv = True
            print(
                "Loading emulator using a specific arxiv, not the one set in basedir"
            )
            self.arxiv = passArxiv

        self._split_arxiv_up(z_list)
        self.emulators = []
        self.paramList = paramList
        self.kmax_Mpc = kmax_Mpc
        self.emu_type = emu_type
        self.paramLimits = paramLimits

        for arxiv in self.arxiv_list:
            emu = gp_emulator.GPEmulator(verbose=verbose,
                                         kmax_Mpc=kmax_Mpc,
                                         paramList=paramList,
                                         train=train,
                                         emu_type=emu_type,
                                         set_noise_var=set_noise_var,
                                         passArxiv=arxiv,
                                         checkHulls=checkHulls,
                                         paramLimits=self.paramLimits)
            self.emulators.append(emu)

        self.training_k_bins = self.emulators[0].training_k_bins
示例#5
0
    def read_chain_from_file(self, chain_number, rootdir, subfolder):
        """Read chain from file, and check parameters"""

        if rootdir:
            chain_location = rootdir
        else:
            assert ('LYA_EMU_REPO' in os.environ), 'export LYA_EMU_REPO'
            chain_location = os.environ['LYA_EMU_REPO'] + "/lya_sampler/chains/"
        if subfolder:
            self.save_directory = chain_location + "/" + subfolder + "/chain_" + str(
                chain_number)
        else:
            self.save_directory = chain_location + "/chain_" + str(
                chain_number)

        if os.path.isfile(self.save_directory + "/backend.h5"):
            self.backend = emcee.backends.HDFBackend(self.save_directory +
                                                     "/backend.h5")
        else:
            self.backend = None
            print(
                "No backend found - will be able to plot chains but not run sampler"
            )

        with open(self.save_directory + "/config.json") as json_file:
            config = json.load(json_file)

        if self.verbose: print("Building arxiv")
        try:
            kp = config["kp_Mpc"]
        except:
            kp = None
        ## Set up the arxiv
        archive = p1d_arxiv.ArxivP1D(
            basedir=config["basedir"],
            drop_tau_rescalings=config["drop_tau_rescalings"],
            drop_temp_rescalings=config["drop_temp_rescalings"],
            nearest_tau=config["nearest_tau"],
            z_max=config["z_max"],
            drop_sim_number=config["data_sim_number"],
            p1d_label=config["p1d_label"],
            skewers_label=config["skewers_label"],
            undersample_cube=config["undersample_cube"],
            kp_Mpc=kp)

        if self.verbose: print("Setting up emulator")
        try:
            reduce_var = config["reduce_var"]
        except:
            reduce_var = False
        ## Set up the emulators
        if config["z_emulator"]:
            emulator = z_emulator.ZEmulator(paramList=config["paramList"],
                                            train=True,
                                            emu_type=config["emu_type"],
                                            kmax_Mpc=config["kmax_Mpc"],
                                            reduce_var_mf=reduce_var,
                                            passArxiv=archive,
                                            verbose=self.verbose)
        else:
            emulator = gp_emulator.GPEmulator(
                paramList=config["paramList"],
                train=True,
                emu_type=config["emu_type"],
                kmax_Mpc=config["kmax_Mpc"],
                asymmetric_kernel=config["asym_kernel"],
                rbf_only=config["asym_kernel"],
                reduce_var_mf=reduce_var,
                passArxiv=archive,
                verbose=self.verbose)

        ## Try/excepts are for backwards compatibility
        ## as old config files don't have these entries
        try:
            data_cov = config["data_cov_factor"]
        except:
            data_cov = 1.
        try:
            data_year = config["data_year"]
        except:
            # if datacov_label wasn't recorded, it was PD2013
            data_year = "PD2013"

        ## Old chains won't have pivot_scalar saved
        if "pivot_scalar" in config.keys():
            pivot_scalar = config["pivot_scalar"]
        else:
            pivot_scalar = 0.05

        ## Set up mock data
        data = data_MPGADGET.P1D_MPGADGET(
            sim_label=config["data_sim_number"],
            basedir=config["basedir"],
            skewers_label=config["skewers_label"],
            z_list=np.asarray(config["z_list"]),
            data_cov_factor=data_cov,
            data_cov_label=data_year,
            pivot_scalar=pivot_scalar)

        if self.verbose: print("Setting up likelihood")
        ## Set up likelihood
        free_param_names = []
        for item in config["free_params"]:
            free_param_names.append(item[0])

        ## Not all saved chains will have this flag
        try:
            free_param_limits = config["free_param_limits"]
        except:
            free_param_limits = None

        self.like = likelihood.Likelihood(
            data=data,
            emulator=emulator,
            free_param_names=free_param_names,
            free_param_limits=free_param_limits,
            verbose=False,
            prior_Gauss_rms=config["prior_Gauss_rms"],
            emu_cov_factor=config["emu_cov_factor"],
            pivot_scalar=pivot_scalar)

        if self.verbose: print("Load sampler data")
        ## Load chains
        self.chain_from_file = {}
        self.chain_from_file["chain"] = np.asarray(config["flatchain"])
        self.chain_from_file["lnprob"] = np.asarray(config["lnprob"])

        if self.verbose:
            print("Chain shape is ", np.shape(self.chain_from_file["chain"]))

        self.ndim = len(self.like.free_params)
        self.nwalkers = config["nwalkers"]
        self.burnin_nsteps = config["burn_in"]
        self.autocorr = np.asarray(config["autocorr"])

        return