def __init__(self,basedir=None,p1d_label=None,skewers_label=None, emulate_slope=True,emulate_running=False, emulate_growth=False,emulate_pressure=True, drop_tau_rescalings=False,drop_temp_rescalings=False, keep_every_other_rescaling=False, deg=4,kmax_Mpc=10.0,max_arxiv_size=None, undersample_z=1,verbose=False): """Setup emulator from base sim directory and label identifying skewer configuration (number, width)""" self.verbose=verbose # read all files with P1D measured in simulation suite self.arxiv=p1d_arxiv.ArxivP1D(basedir,p1d_label,skewers_label, drop_tau_rescalings=drop_tau_rescalings, drop_temp_rescalings=drop_temp_rescalings, keep_every_other_rescaling=keep_every_other_rescaling, max_arxiv_size=max_arxiv_size,undersample_z=undersample_z, verbose=verbose) # for each model in arxiv, fit smooth function to P1D self._fit_p1d_in_arxiv(deg,kmax_Mpc) # setup parameter space to be used in emulator self._setup_param_space(emulate_slope=emulate_slope, emulate_running=emulate_running, emulate_growth=emulate_growth, emulate_pressure=emulate_pressure) # for each order in polynomial, setup interpolation object self._setup_interp(deg)
def __init__(self, basedir=None, p1d_label=None, skewers_label=None, drop_tau_rescalings=False, drop_temp_rescalings=False, keep_every_other_rescaling=False, max_arxiv_size=None, undersample_z=1, verbose=False): """Setup emulator from base sim directory and label identifying skewer configuration (number, width)""" self.verbose = verbose # read all files with P1D measured in simulation suite self.arxiv = p1d_arxiv.ArxivP1D( basedir, p1d_label, skewers_label, drop_tau_rescalings=drop_tau_rescalings, drop_temp_rescalings=drop_temp_rescalings, keep_every_other_rescaling=keep_every_other_rescaling, max_arxiv_size=max_arxiv_size, undersample_z=undersample_z, verbose=verbose) # define metric to compute distances between models self.metric = self.set_distance_metric()
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
## Generate mock P1D measurement data = data_MPGADGET.P1D_MPGADGET(sim_label=test_sim_number, basedir=args.basedir, skewers_label=args.skewers_label, zmax=args.z_max, data_cov_factor=args.data_cov_factor, data_cov_label=args.data_year, pivot_scalar=args.pivot_scalar) zs = data.z ## Set up emulator training data archive = p1d_arxiv.ArxivP1D(basedir=args.basedir, drop_tau_rescalings=args.drop_tau_rescalings, z_max=args.z_max, drop_sim_number=test_sim_number, nearest_tau=args.nearest_tau, 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,
from scipy.optimize import curve_fit ''' Script to take a chosen simulation in the emulator archive and save a json dictionary of the format used by the data_MPGADGET class, including the best fit likelihood parameters. ''' sim_num = 0 ## Pick an emulator suite and a simulation number repo = os.environ['LYA_EMU_REPO'] basedir = repo + "/p1d_emulator/sim_suites/emulator_1024_21062019" skewers_label = 'Ns512_wM0.05' archive = p1d_arxiv.ArxivP1D(basedir=basedir, pick_sim_number=sim_num, drop_tau_rescalings=True, z_max=4, drop_temp_rescalings=True, skewers_label=skewers_label) sim_data = archive.data ## Save the P1D values for item in (sim_data): if item["z"] == 2.0 or item["z"] == 3.0 or item["z"] == 4.0: print("Sim z=", item["z"]) print("mF=", item["mF"]) print("T0=", item["T0"]) print("gamma=", item["gamma"]) print("kF_Mpc", item["kF_Mpc"]) print("Delta2_p=", item["Delta2_p"]) print("\n")
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
def __init__(self, basedir=None, p1d_label=None, skewers_label=None, max_arxiv_size=None, verbose=False, kmax_Mpc=10.0, paramList=None, train=False, drop_tau_rescalings=False, drop_temp_rescalings=False, keep_every_other_rescaling=False, undersample_z=1, emu_type="k_bin", z_max=5, z_list=None, passArxiv=None, set_noise_var=1e-3, asymmetric_kernel=False, checkHulls=False, set_hyperparams=None, paramLimits=None, rbf_only=False, emu_per_k=False, reduce_var_k=False, reduce_var_z=False, reduce_var_mf=False): self.kmax_Mpc = kmax_Mpc self.basedir = basedir self.emu_type = emu_type self.emu_noise = set_noise_var self.max_arxiv_size = max_arxiv_size self.drop_tau_rescalings = drop_tau_rescalings self.drop_temp_rescalings = drop_temp_rescalings self.keep_every_other_rescaling = keep_every_other_rescaling self.undersample_z = undersample_z self.verbose = verbose self.asymmetric_kernel = asymmetric_kernel self.z_max = z_max self.paramLimits = paramLimits self.crossval = False ## Flag to check whether or not a prediction is ## inside the training set self.rbf_only = rbf_only self.emu_per_k = emu_per_k self.reduce_var_k = reduce_var_k ## Emulate (1+k)P1D(k) self.reduce_var_z = reduce_var_z ## Emulate P1D(k)/(1+z)^3.8 self.reduce_var_mf = reduce_var_mf ## Emulate P1D(k)*<F>^2.5 # 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=self.max_arxiv_size, verbose=verbose, drop_tau_rescalings=drop_tau_rescalings, drop_temp_rescalings=drop_temp_rescalings, z_max=self.z_max, keep_every_other_rescaling=keep_every_other_rescaling, undersample_z=undersample_z) else: self.custom_arxiv = True if self.verbose: print( "Loading emulator using a specific arxiv, not the one set in basedir" ) self.arxiv = passArxiv ## Find max k bin self.k_bin = np.max( np.where(self.arxiv.data[0]["k_Mpc"] < self.kmax_Mpc)) + 1 self.training_k_bins = self.arxiv.data[0]["k_Mpc"][1:self.k_bin] ## If none, take all parameters if paramList == None: self.paramList = [ 'mF', 'sigT_Mpc', 'gamma', 'kF_Mpc', 'Delta2_p', 'n_p' ] else: self.paramList = paramList self._build_interp(self.arxiv, self.paramList) self.trained = False if train == True: self.train() self.checkHulls = checkHulls if self.checkHulls: self.hull = Delaunay(self.X_param_grid) else: self.hull = None self.emulators = None ## Flag that this is an individual emulator object
drop_sim_number=None # specify simulation suite and P1D mesurements basedir="/p1d_emulator/sim_suites/Australia20/" skewers_label='Ns500_wM0.05' p1d_label=None z_max=4.0 data=data_MPGADGET.P1D_MPGADGET(basedir=basedir, skewers_label=skewers_label, 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]]
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