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
Beispiel #2
0
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