def test_interpolation(): root_path = "Data/all" analysis = LikelihoodAnalysis() #Read in model names models = CFHTemu1.getModels()[:17] assert len(models) == 17 #Shuffle the models np.random.seed(1) np.random.shuffle(models) #Divide into training and testing training_models = models[:-1] testing_model = models[-1] #Read multipoles ell = np.load(os.path.join(root_path,"ell.npy")) #Load in the means of the power spectra of the 17 models, and populate the analysis instance for model in training_models: ens = Ensemble.fromfilelist([os.path.join(root_path,model._cosmo_id_string,"subfield1","sigma05","power_spectrum.npy")]) ens.load(from_old=True) analysis.add_model(parameters=model.squeeze(with_ns=True),feature=ens.mean()) #Add the multipoles to the analysis analysis.add_feature_label(ell) l = analysis.feature_label ens = Ensemble.fromfilelist([os.path.join(root_path,testing_model._cosmo_id_string,"subfield1","sigma05","power_spectrum.npy")]) ens.load(from_old=True) testing_Pl = ens.mean() #Load in also the observed power spectrum ens = Ensemble.fromfilelist([os.path.join(root_path,"observations","subfield1","sigma05","power_spectrum.npy")]) ens.load(from_old=True) observed_Pl = ens.mean() #Output the analysis stats np.savetxt("16_parameter_points.txt",analysis.parameter_set) for n in range(len(training_models)): plt.plot(l,l*(l+1)*analysis.training_set[n]/(2*np.pi)) plt.plot(l,l*(l+1)*observed_Pl/(2*np.pi),linestyle="--",label="Observation") plt.xlabel(r"$l$") plt.ylabel(r"$l(l+1)P_l/2\pi$") plt.yscale("log") plt.legend(loc="upper left") plt.savefig("16_power_spectra.png") plt.clf() #Train the interpolators analysis.train(use_parameters=range(3)) assert hasattr(analysis,"_interpolator") assert hasattr(analysis,"_num_bins") #Emulator portability test with pickle/unpickle analysis.save("analysis.p") emulator = LikelihoodAnalysis.load("analysis.p") #Predict the power spectrum at the remaining point predicted_Pl = emulator.predict(testing_model.squeeze()) #Plot it against the measured one fig,ax = plt.subplots(2,1,figsize=(16,8)) #Measured ax[0].plot(l,l*(l+1)*testing_Pl/(2*np.pi),label="measured") #Predicted ax[0].plot(l,l*(l+1)*predicted_Pl/(2*np.pi),label="interpolated") #Fractional difference ax[1].plot(l,(predicted_Pl - testing_Pl)/testing_Pl) ax[1].set_xlabel(r"$l$") ax[0].set_ylabel(r"$l(l+1)P_l/2\pi$") ax[1].set_ylabel(r"$P_l^I-P_l^M/P_l^M$") ax[0].set_yscale("log") ax[0].legend(loc="upper left") plt.savefig("power_interpolator_test.png") plt.clf() #Give it a shot with two points in parameter space to test vectorization two_parameter_points = np.array((training_models[0].squeeze(),testing_model.squeeze())) two_predicted_Pl = emulator.predict(two_parameter_points) fig,ax = plt.subplots(2,1,figsize=(16,8)) #Predicted ax[0].plot(l,l*(l+1)*two_predicted_Pl[0]/(2*np.pi),color="red",linestyle="--") ax[0].plot(l,l*(l+1)*two_predicted_Pl[1]/(2*np.pi),color="green",linestyle="--") #Measured ax[0].plot(l,l*(l+1)*emulator.training_set[0]/(2*np.pi),color="red",linestyle="-") ax[0].plot(l,l*(l+1)*testing_Pl/(2*np.pi),color="green",linestyle="-") #Fractional difference ax[1].plot(l,(two_predicted_Pl[0] - emulator.training_set[0])/emulator.training_set[0],color="red") ax[1].plot(l,(two_predicted_Pl[1] - testing_Pl)/testing_Pl,color="green") ax[1].set_xlabel(r"$l$") ax[0].set_ylabel(r"$l(l+1)P_l/2\pi$") ax[1].set_ylabel(r"$P_l^I-P_l^M/P_l^M$") ax[0].set_yscale("log") plt.savefig("power_interpolator_test_2.png") plt.clf() #Generate a fudge power spectrum covariance matrix covariance = np.diag(testing_Pl**2/(0.5 + l)) #Generate a fudge observation by wiggling the testing power spectrum observation = testing_Pl + np.random.uniform(low=-testing_Pl*0.1,high=testing_Pl*0.1) #Choose a bunch of points in parameter space points = emulator.parameter_set[:,:-1] #Compute the chi2 chi2_values_1 = emulator.chi2(points,observation,covariance) chi2_values_2 = emulator.chi2(points,observation,covariance,split_chunks=4) assert chi2_values_1.shape == chi2_values_2.shape #Compute the individual contributions chi2_contributions = emulator.chi2Contributions(points[0],observation,covariance) #Plot plt.imshow(chi2_contributions,interpolation="nearest") plt.colorbar() plt.xlabel(r"$j$") plt.ylabel(r"$i$") plt.savefig("chi2_contributions.png") plt.clf() return chi2_values_1,chi2_values_2
def main(): ################################################# ############Option parsing####################### ################################################# #Parse command line options parser = argparse.ArgumentParser() parser.add_argument("-f","--file",dest="options_file",action="store",type=str,help="analysis options file") parser.add_argument("-v","--verbose",dest="verbose",action="store_true",default=False,help="turn on verbosity") parser.add_argument("-vv","--verbose_plus",dest="verbose_plus",action="store_true",default=False,help="turn on additional verbosity") parser.add_argument("-m","--mask_scale",dest="mask_scale",action="store_true",default=False,help="scale peaks and power spectrum to unmasked area") parser.add_argument("-c","--cut_convergence",dest="cut_convergence",action="store",default=None,help="select convergence values in (min,max) to compute the likelihood. Safe for single descriptor only!!") parser.add_argument("-g","--group_subfields",dest="group_subfields",action="store_true",default=False,help="group feature realizations by taking the mean over subfields, this makes a big difference in the covariance matrix") parser.add_argument("-s","--save_points",dest="save_points",action="store",default=None,help="save points in parameter space to external npy file") parser.add_argument("-p","--prefix",dest="prefix",action="store",default="",help="give a prefix to the name of the pickled emulator") parser.add_argument("-d","--differentiate",dest="differentiate",action="store_true",default=False,help="differentiate the first minkowski functional to get the PDF") cmd_args = parser.parse_args() if cmd_args.options_file is None: parser.print_help() sys.exit(0) #Set verbosity level if cmd_args.verbose_plus: logging.basicConfig(level=DEBUG_PLUS) elif cmd_args.verbose: logging.basicConfig(level=logging.DEBUG) else: logging.basicConfig(level=logging.INFO) #Instantiate a FeatureLoader object that will take care of the memory loading feature_loader = FeatureLoader(cmd_args) #Get the names of all the simulated models available for the CFHT analysis, including smoothing scales and subfields all_simulated_models = CFHTemu1.getModels(root_path=feature_loader.options.get("simulations","root_path")) #Select subset of training models training_models = all_simulated_models #Create a LikelihoodAnalysis instance and load the training models into it analysis = LikelihoodAnalysis() ########################################################### ###############Feature loading############################# ########################################################### #Start loading the data logging.info("Loading features...") for feature_type in feature_loader.features_to_measure.keys(): logging.info("{0}, smoothing scales: {1} arcmin".format(feature_type,",".join([ str(s) for s in feature_loader.features_to_measure[feature_type] ]))) #Start start = time.time() #Load the simulated features for n,model in enumerate(training_models): logging.debug("Model {0}".format(n)) logging.debug(model) ensemble_all_subfields = feature_loader.load_features(model) #Add the feature to the LikelihoodAnalysis analysis.add_model(parameters=model.squeeze(),feature=ensemble_all_subfields.mean()) #Log timestamp now = time.time() logging.info("Simulated features loaded in {0:.1f}s".format(now-start)) last_timestamp = now ######################################################################################################## #####################Feature loading complete, can build the emulator now############################### ######################################################################################################## #Train the interpolators using the simulated features logging.info("Training interpolators...") analysis.train() #Log timestamp now = time.time() logging.info("Emulator trained in {0:.1f}s".format(now-last_timestamp)) last_timestamp = now #Pickle the emulator and save it to a .p file emulators_dir = os.path.join(feature_loader.options.get("analysis","save_path"),"emulators") if not os.path.isdir(emulators_dir): os.mkdir(emulators_dir) emulator_file = os.path.join(emulators_dir,"emulator{0}_{1}.p".format(cmd_args.prefix,output_string(feature_loader.feature_string))) logging.info("Pickling emulator and saving it to {0}".format(emulator_file)) analysis.save(emulator_file) #Log timestamp and finish end = time.time() logging.info("DONE!!") logging.info("Completed in {0:.1f}s".format(end-start))
def test_interpolation(): root_path = "Data/all" analysis = LikelihoodAnalysis() #Read in model names models = CFHTemu1.getModels()[:17] assert len(models) == 17 #Shuffle the models np.random.seed(1) np.random.shuffle(models) #Divide into training and testing training_models = models[:-1] testing_model = models[-1] #Read multipoles ell = np.load(os.path.join(root_path, "ell.npy")) #Load in the means of the power spectra of the 17 models, and populate the analysis instance for model in training_models: ens = Ensemble.fromfilelist([ os.path.join(root_path, model._cosmo_id_string, "subfield1", "sigma05", "power_spectrum.npy") ]) ens.load(from_old=True) analysis.add_model(parameters=model.squeeze(with_ns=True), feature=ens.mean()) #Add the multipoles to the analysis analysis.add_feature_label(ell) l = analysis.feature_label ens = Ensemble.fromfilelist([ os.path.join(root_path, testing_model._cosmo_id_string, "subfield1", "sigma05", "power_spectrum.npy") ]) ens.load(from_old=True) testing_Pl = ens.mean() #Load in also the observed power spectrum ens = Ensemble.fromfilelist([ os.path.join(root_path, "observations", "subfield1", "sigma05", "power_spectrum.npy") ]) ens.load(from_old=True) observed_Pl = ens.mean() #Output the analysis stats np.savetxt("16_parameter_points.txt", analysis.parameter_set) for n in range(len(training_models)): plt.plot(l, l * (l + 1) * analysis.training_set[n] / (2 * np.pi)) plt.plot(l, l * (l + 1) * observed_Pl / (2 * np.pi), linestyle="--", label="Observation") plt.xlabel(r"$l$") plt.ylabel(r"$l(l+1)P_l/2\pi$") plt.yscale("log") plt.legend(loc="upper left") plt.savefig("16_power_spectra.png") plt.clf() #Train the interpolators analysis.train(use_parameters=range(3)) assert hasattr(analysis, "_interpolator") assert hasattr(analysis, "_num_bins") #Emulator portability test with pickle/unpickle analysis.save("analysis.p") emulator = LikelihoodAnalysis.load("analysis.p") #Predict the power spectrum at the remaining point predicted_Pl = emulator.predict(testing_model.squeeze()) #Plot it against the measured one fig, ax = plt.subplots(2, 1, figsize=(16, 8)) #Measured ax[0].plot(l, l * (l + 1) * testing_Pl / (2 * np.pi), label="measured") #Predicted ax[0].plot(l, l * (l + 1) * predicted_Pl / (2 * np.pi), label="interpolated") #Fractional difference ax[1].plot(l, (predicted_Pl - testing_Pl) / testing_Pl) ax[1].set_xlabel(r"$l$") ax[0].set_ylabel(r"$l(l+1)P_l/2\pi$") ax[1].set_ylabel(r"$P_l^I-P_l^M/P_l^M$") ax[0].set_yscale("log") ax[0].legend(loc="upper left") plt.savefig("power_interpolator_test.png") plt.clf() #Give it a shot with two points in parameter space to test vectorization two_parameter_points = np.array( (training_models[0].squeeze(), testing_model.squeeze())) two_predicted_Pl = emulator.predict(two_parameter_points) fig, ax = plt.subplots(2, 1, figsize=(16, 8)) #Predicted ax[0].plot(l, l * (l + 1) * two_predicted_Pl[0] / (2 * np.pi), color="red", linestyle="--") ax[0].plot(l, l * (l + 1) * two_predicted_Pl[1] / (2 * np.pi), color="green", linestyle="--") #Measured ax[0].plot(l, l * (l + 1) * emulator.training_set[0] / (2 * np.pi), color="red", linestyle="-") ax[0].plot(l, l * (l + 1) * testing_Pl / (2 * np.pi), color="green", linestyle="-") #Fractional difference ax[1].plot(l, (two_predicted_Pl[0] - emulator.training_set[0]) / emulator.training_set[0], color="red") ax[1].plot(l, (two_predicted_Pl[1] - testing_Pl) / testing_Pl, color="green") ax[1].set_xlabel(r"$l$") ax[0].set_ylabel(r"$l(l+1)P_l/2\pi$") ax[1].set_ylabel(r"$P_l^I-P_l^M/P_l^M$") ax[0].set_yscale("log") plt.savefig("power_interpolator_test_2.png") plt.clf() #Generate a fudge power spectrum covariance matrix covariance = np.diag(testing_Pl**2 / (0.5 + l)) #Generate a fudge observation by wiggling the testing power spectrum observation = testing_Pl + np.random.uniform(low=-testing_Pl * 0.1, high=testing_Pl * 0.1) #Choose a bunch of points in parameter space points = emulator.parameter_set[:, :-1] #Compute the chi2 chi2_values_1 = emulator.chi2(points, observation, covariance) chi2_values_2 = emulator.chi2(points, observation, covariance, split_chunks=4) assert chi2_values_1.shape == chi2_values_2.shape #Compute the individual contributions chi2_contributions = emulator.chi2Contributions(points[0], observation, covariance) #Plot plt.imshow(chi2_contributions, interpolation="nearest") plt.colorbar() plt.xlabel(r"$j$") plt.ylabel(r"$i$") plt.savefig("chi2_contributions.png") plt.clf() return chi2_values_1, chi2_values_2