def load(cls,fname): """Loads the prior from an input file. Parameters ---------- fname : :obj:`str` input filename Returns ------- prior : :obj:`prior` loaded prior object """ import h5py as h5 import numpy as np from ctypes import c_double from pyselfi.utils import PrintMessage PrintMessage(3, "Reading prior in data file '{}'...".format(fname)) with h5.File(fname,'r') as hf: # Load here any mandatory parameter to call the class constructor as follows, if needed some_hyperparameter=hf.attrs['/prior/some_hyperparameter'] some_array=np.array(hf.get('/prior/some_array'), dtype=c_double) # Call the class constructor prior=cls(kwargs) #kwargs can depend on some_hyperparameter, some_array, etc. # Load any other parameter, in particular the mandatory attributes (mean, covariance and inv_covariance), if they are not already defined at this point prior.mean = np.array(hf.get('prior/mean'), dtype=c_double) prior.covariance = np.array(hf.get('/prior/covariance'), dtype=c_double) prior.inv_covariance = np.array(hf.get('/prior/inv_covariance'), dtype=c_double) PrintMessage(3, "Reading prior in data file '{}' done.".format(fname)) return prior
def save(self,fname): """Saves the prior to an output file. Parameters ---------- fname : :obj:`str` output filename """ import h5py as h5 import numpy as np from ctypes import c_double from pyselfi.utils import PrintMessage, save_replace_dataset, save_replace_attr PrintMessage(3, "Writing prior in data file '{}'...".format(fname)) with h5.File(fname, 'r+') as hf: # Save interesting hyperparameters as follows, if needed save_replace_attr(hf, '/prior/some_hyperparameter', self.some_hyperparameter, dtype=c_double) # Save here any other interesting prior attribute as follows, if needed save_replace_dataset(hf, '/prior/some_array', data=self.some_array, maxshape=(None,), dtype=c_double) # Save mandatory attributes: mean, covariance and inv_covariance save_replace_dataset(hf, '/prior/mean', data=self.mean, maxshape=(None,), dtype=c_double) save_replace_dataset(hf, '/prior/covariance', data=self.covariance, maxshape=(None, None), dtype=c_double) save_replace_dataset(hf, '/prior/inv_covariance', data=self.inv_covariance, maxshape=(None, None), dtype=c_double) PrintMessage(3, "Writing prior in data file '{}' done.".format(fname))
def load(self, fname): """Loads the likelihood from an input file. Parameters ---------- fname : :obj:`str` input filename """ import numpy as np import h5py as h5 from ctypes import c_double from pyselfi.utils import PrintMessage PrintMessage(3, "Reading likelihood in data file '{}'...".format(fname)) with h5.File(fname, 'r') as hf: for d in range(self.S + 1): setattr( self, 'f_' + str(d), np.array(hf.get('/likelihood/f_' + str(d)), dtype=c_double).astype(c_double)) self.C_0 = np.array(hf.get('/likelihood/C_0'), dtype=c_double).astype(c_double) self.logdet2piC_0 = hf.attrs['/likelihood/logdet2piC_0'] self.inv_C_0 = np.array(hf.get('/likelihood/inv_C_0'), dtype=c_double).astype(c_double) self.grad_f = np.array(hf.get('/likelihood/grad_f'), dtype=c_double).astype(c_double) PrintMessage( 3, "Reading likelihood from in file '{}' done.".format(fname))
def save(self, fname): """Saves the prior to an output file. Parameters ---------- fname : :obj:`str` output filename """ import h5py as h5 import numpy as np from ctypes import c_double from pyselfi.utils import PrintMessage, save_replace_dataset, save_replace_attr PrintMessage(3, "Writing prior in data file '{}'...".format(fname)) with h5.File(fname, 'r+') as hf: save_replace_attr(hf, '/prior/theta_norm', self.theta_norm, dtype=c_double) save_replace_attr(hf, '/prior/k_corr', self.k_corr, dtype=c_double) save_replace_attr(hf, '/prior/alpha_cv', self.alpha_cv, dtype=c_double) save_replace_dataset(hf, '/prior/k_s', data=self.k_s, maxshape=(None, ), dtype=c_double) save_replace_dataset(hf, '/prior/mean', data=self.mean, maxshape=(None, ), dtype=c_double) save_replace_dataset(hf, '/prior/rbf', data=self.rbf, maxshape=(None, None), dtype=c_double) save_replace_dataset(hf, '/prior/cv', data=self.cv, maxshape=(None, None), dtype=c_double) save_replace_dataset(hf, '/prior/covariance', data=self.covariance, maxshape=(None, None), dtype=c_double) save_replace_dataset(hf, '/prior/inv_covariance', data=self.inv_covariance, maxshape=(None, None), dtype=c_double) PrintMessage(3, "Writing prior in data file '{}' done.".format(fname))
def save(self, fname): """Saves the likelihood to an output file. Parameters ---------- fname : :obj:`str` output filename """ import h5py as h5 from ctypes import c_double, c_double from pyselfi.utils import PrintMessage, save_replace_dataset, save_replace_attr PrintMessage(3, "Writing likelihood in data file '{}'...".format(fname)) with h5.File(fname, 'r+') as hf: for d in range(self.S + 1): save_replace_dataset(hf, '/likelihood/f_' + str(d), data=getattr(self, 'f_' + str(d)).astype(c_double), maxshape=(None, ), dtype=c_double) save_replace_dataset(hf, '/likelihood/C_0', data=self.C_0.astype(c_double), maxshape=(None, None), dtype=c_double) save_replace_attr(hf, '/likelihood/logdet2piC_0', self.logdet2piC_0, dtype=c_double) save_replace_dataset(hf, '/likelihood/inv_C_0', data=self.inv_C_0.astype(c_double), maxshape=(None, None), dtype=c_double) save_replace_dataset(hf, '/likelihood/grad_f', data=self.grad_f.astype(c_double), maxshape=(None, None), dtype=c_double) PrintMessage( 3, "Writing likelihood in data file '{}' done.".format(fname))
def make_data(self, cosmo, fname_powerspectrum, seedphases=None, seednoise=None, force=False): """Evaluates the GRF blackbox to make mock data, from input cosmological parameters. Parameters ---------- cosmo : dictionary cosmological parameters (and some infrastructure parameters) fname_powerspectrum : :obj:`str` name of input/output power spectrum file seedphases : int, optional, default=None value of the seed to generate the phases of the Gaussian random field seednoise : int, optional, default=None value of the seed to generate the noise realization, uses current state if set to None force : bool, optional, default=False force recomputation? Returns ------- Phi : array, double, dimension=P vector of summary statistics """ from pyselfi.utils import PrintMessage, INDENT, UNINDENT PrintMessage(3, "Making mock data...") INDENT() # Generate input initial power spectrum from cosmological parameters P_data = self._get_powerspectrum_from_cosmo(cosmo, fname_powerspectrum, force) # Call auxiliary blackbox method Phi = self._aux_blackbox(P_data, seedphases, seednoise) UNINDENT() PrintMessage(3, "Making mock data done.") return Phi
def evaluate(self, theta, seedphases=None, seednoise=None, i=0, N=0): """Evaluates the GRF blackbox from an input vector of power spectrum coefficients at the support wavenumbers. Parameters ---------- theta : array, double, dimension=S vector of power spectrum values at the support wavenumbers seedphases : int, optional, default=None value of the seed to generate the phases of the Gaussian random field seednoise : int, optional, default=None value of the seed to generate the noise realization, uses current state if set to None i : int, optional, default=0 current evaluation index of the blackbox N : int, optional, default=0 total number of evaluations of the blackbox Returns ------- Phi : array, double, dimension=P vector of summary statistics """ from pyselfi.utils import PrintMessage, PrintValue, INDENT, UNINDENT PrintMessage(3, "Evaluating blackbox ({}/{})...".format(i, N)) INDENT() PrintValue("seedphases", seedphases) PrintValue("seednoise", seednoise) # Interpolate P to get P_in P_in = self._get_powerspectrum_from_theta(theta) # Call auxiliary blackbox method Phi = self._aux_blackbox(P_in, seedphases, seednoise) UNINDENT() PrintMessage(3, "Evaluating blackbox ({}/{}) done.".format(i, N)) return Phi
def load(cls, fname): """Loads the prior from an input file. Parameters ---------- fname : :obj:`str` input filename Returns ------- prior : :obj:`prior` loaded prior object """ import h5py as h5 import numpy as np from ctypes import c_double from pyselfi.utils import PrintMessage PrintMessage(3, "Reading prior in data file '{}'...".format(fname)) with h5.File(fname, 'r') as hf: mean = np.array(hf.get('/prior/mean'), dtype=c_double) k_s = np.array(hf.get('/prior/k_s'), dtype=c_double) theta_norm = hf.attrs['/prior/theta_norm'] k_corr = hf.attrs['/prior/k_corr'] alpha_cv = hf.attrs['/prior/alpha_cv'] prior = cls(k_s, mean, theta_norm, k_corr, alpha_cv) prior.rbf = np.array(hf.get('/prior/rbf'), dtype=c_double) prior.cv = np.array(hf.get('/prior/cv'), dtype=c_double) prior.covariance = np.array(hf.get('/prior/covariance'), dtype=c_double) prior.inv_covariance = np.array(hf.get('/prior/inv_covariance'), dtype=c_double) PrintMessage(3, "Reading prior in data file '{}' done.".format(fname)) return prior
def evaluate(self, theta, d, i=0, N=0, force_powerspectrum=False, force_parfiles=False, force_sim=False, force_mock=False, remove_sbmy=True): """Evaluates the Simbelynë blackbox from an input vector of power spectrum coefficients at the support wavenumbers. Parameters ---------- theta : array, double, dimension=S vector of power spectrum values at the support wavenumbers d : int index giving the direction in parameter space: -1 for mock data, 0 for the expansion point, or from 1 to S i : int, optional, default=0 current evaluation index of the blackbox N : int, optional, default=0 total number of evaluations of the blackbox force_powerspectrum : bool, optional, default=False force recomputation of the power spectrum? force_parfiles : bool, optional, default=False overwrite if parameter files already exists? force_sim : bool, optional, default=False force recomputation if output density already exists? force_mock : bool, optional, default=False force recomputation if output mock summary statistics file already exists? remove_sbmy : bool, optional, default=True remove Simbelynë output files from disk? Returns ------- Phi : array, double, dimension=P vector of summary statistics """ from pyselfi.utils import PrintMessage, PrintValue, INDENT, UNINDENT PrintMessage(3, "Evaluating blackbox ({}/{})...".format(i, N)) INDENT() # Define filenames fname_powerspectrum = self.fsimdir + "/d" + str( d) + "/input_power_d" + str(d) + ".h5" fname_simparfile = self.fsimdir + "/d" + str(d) + "/sim_d" + str( d) + "_p" + str(i - 1) + ".sbmy" fname_mockparfile = self.fsimdir + "/d" + str(d) + "/mock_d" + str( d) + "_p" + str(i - 1) + ".sbmy" fname_RngStateLPT = self.fsimdir + "/RngStateLPT_p" + str(i - 1) + ".rng" fname_whitenoise = self.fsimdir + "/initial_density_white_p" + str( i - 1) + ".h5" fname_outputinitialdensity = self.fsimdir + "/initial_density_p" + str( i - 1) + ".h5" fname_outputrealspacedensity = self.fsimdir + "/d" + str( d) + "/output_realspace_density_d" + str(d) + "_p" + str(i - 1) + ".h5" fname_outputdensity = self.fsimdir + "/d" + str( d) + "/output_density_d" + str(d) + "_p" + str(i - 1) + ".h5" fname_RngStateMock = self.fsimdir + "/RngStateMocks_p" + str( i - 1) + ".rng" fname_outputmock = self.fsimdir + "/d" + str( d) + "/output_mock_d" + str(d) + "_p" + str(i - 1) + "_" fname_outputss = self.fsimdir + "/d" + str(d) + "/output_ss_d" + str( d) + "_p" + str(i - 1) + ".h5" fname_simlogs = self.fsimdir + "/d" + str(d) + "/logs_sim_d" + str( d) + "_p" + str(i - 1) + ".txt" fname_mocklogs = self.fsimdir + "/d" + str(d) + "/logs_mock_d" + str( d) + "_p" + str(i - 1) + ".txt" # Interpolate P to get P_in self._get_powerspectrum_from_theta(theta, fname_powerspectrum, force_powerspectrum) # Call auxiliary blackbox method Phi = self._aux_blackbox(d, fname_powerspectrum, fname_simparfile, fname_mockparfile, fname_RngStateLPT, fname_whitenoise, fname_outputinitialdensity, fname_outputrealspacedensity, fname_outputdensity, fname_RngStateMock, fname_outputmock, fname_outputss, fname_simlogs, fname_mocklogs, force_parfiles, force_sim, force_mock) # Clean Simbelynë outputs if remove_sbmy: self._clean_sbmy_outputs(fname_outputdensity, fname_outputmock, fname_outputss) UNINDENT() PrintMessage(3, "Evaluating blackbox ({}/{}) done.".format(i, N)) return Phi
def make_data(self, cosmo, i=0, force_powerspectrum=False, force_parfiles=False, force_sim=False, force_mock=False, force_cosmo=False): """Evaluates the Simbelynë blackbox to make mock data, from input cosmological parameters. Parameters ---------- cosmo : dictionary cosmological parameters (and some infrastructure parameters) i : int, optional, default=0 current evaluation index of the blackbox force_powerspectrum : bool, optional, default=False force recomputation of the power spectrum? force_parfiles : bool, optional, default=False overwrite if parameter files already exists? force_sim : bool, optional, default=False force recomputation if output density already exists? force_mock : bool, optional, default=False force recomputation if output mock summary statistics file already exists? Returns ------- Phi : array, double, dimension=P vector of summary statistics """ from pyselfi.utils import PrintMessage, INDENT, UNINDENT PrintMessage(3, "Making mock data...") INDENT() # Define filenames fname_cosmo = self.fsimdir + "/data/input_cosmo_" + str(i) + ".json" fname_powerspectrum = self.fsimdir + "/data/input_power_" + str( i) + ".h5" fname_simparfile = self.fsimdir + "/data/sim_" + str(i) + ".sbmy" fname_mockparfile = self.fsimdir + "/data/mock_" + str(i) + ".sbmy" fname_RngStateLPT = self.fsimdir + "/data/RngStateLPT_" + str( i) + ".rng" fname_whitenoise = self.fsimdir + "/data/initial_density_white_" + str( i) + ".h5" fname_outputinitialdensity = self.fsimdir + "/data/initial_density_" + str( i) + ".h5" fname_outputrealspacedensity = self.fsimdir + "/data/output_realspace_density_" + str( i) + ".h5" fname_outputdensity = self.fsimdir + "/data/output_density_" + str( i) + ".h5" fname_RngStateMock = self.fsimdir + "/data/RngStateMocks_" + str( i) + ".rng" fname_outputmock = self.fsimdir + "/data/output_mock_" + str(i) + "_" fname_outputss = self.fsimdir + "/data/output_ss_" + str(i) + ".h5" fname_simlogs = self.fsimdir + "/data/logs_sim_" + str(i) + ".txt" fname_mocklogs = self.fsimdir + "/data/logs_mock_" + str(i) + ".txt" # Save cosmological parameters self._save_cosmo(cosmo, fname_cosmo, force_cosmo) # Generate input initial power spectrum from cosmological parameters self._get_powerspectrum_from_cosmo(cosmo, fname_powerspectrum, force_powerspectrum) # Call auxiliary blackbox method Phi = self._aux_blackbox(-1, fname_powerspectrum, fname_simparfile, fname_mockparfile, fname_RngStateLPT, fname_whitenoise, fname_outputinitialdensity, fname_outputrealspacedensity, fname_outputdensity, fname_RngStateMock, fname_outputmock, fname_outputss, fname_simlogs, fname_mocklogs, force_parfiles, force_sim, force_mock) UNINDENT() PrintMessage(3, "Making mock data done.") return Phi