def postprocess(): # Postprocess DNest4 output dn4.postprocess(plot=False) # Load the data f = open("data.yaml") data = yaml.load(f, Loader=yaml.SafeLoader) f.close() # Plot the forecast posterior_sample = dn4.my_loadtxt("posterior_sample.txt") amount = posterior_sample[:, -1] amount = np.sort(amount) n = len(amount) indices = [int(0.1 * n), int(0.5 * n), int(0.9 * n)] future_tips = amount quantiles = future_tips[indices] print("") print( "Assuming things keep rolling along more or less as they have been, I") print("predict, with 90% probability, that you'll receive between") print(np.round(quantiles[0], 2), "and", np.round(quantiles[2], 2), "LBC", end=" ") print("over the next month.") # plt.hist(future_tips, 500, density=True) # plt.xlabel("Future tips over next month (LBC)") # plt.ylabel("Probability Density") # plt.show() return quantiles
def display(): """ Function to load and plot the output of a run. """ f = open("config.yaml") config = yaml.load(f, Loader=yaml.SafeLoader) f.close() # Load the data file data = dn4.my_loadtxt(config["data_file"]) x = data[:, 0] y = data[:, 1] # Load posterior samples etc posterior_sample = dn4.my_loadtxt("posterior_sample.txt") temp = dn4.load_column_names("posterior_sample.txt") indices = temp["indices"] # Prepare some arrays wide_integral = np.zeros(posterior_sample.shape[0]) wide_center_of_mass = np.zeros(posterior_sample.shape[0]) wide_width = np.zeros(posterior_sample.shape[0]) wide_skewness = np.zeros(posterior_sample.shape[0]) wide_nongaussianity = np.zeros(posterior_sample.shape[0]) spikes_integral = np.zeros(posterior_sample.shape[0]) max_num_spikes = int(posterior_sample[0, indices["max_num_peaks1"]]) # For calculating posterior means of functions bg_tot = np.zeros(data.shape[0]) wide_component_tot = np.zeros(data.shape[0]) the_spikes_tot = np.zeros(data.shape[0]) model_tot = np.zeros(data.shape[0]) # A data frame to store some results macroquantities = pd.DataFrame() plt.clf() for i in range(0, posterior_sample.shape[0]): # Extract the background start = indices["bg[0]"] end = start + data.shape[0] bg = posterior_sample[i, start:end] bg_tot += bg # Extract the wide component start = indices["wide[0]"] end = start + data.shape[0] wide_component = posterior_sample[i, start:end] wide_component_tot += wide_component # Extract the spikes component start = indices["narrow[0]"] end = start + data.shape[0] the_spikes = posterior_sample[i, start:end] the_spikes_tot += the_spikes # Extract the model curve start = indices["model_curve[0]"] end = start + data.shape[0] model = posterior_sample[i, start:end] model_tot += model # Compute some integrals wide_integral[i] = np.sum(wide_component) if wide_integral[i] != 0.0: f = wide_component / wide_integral[i] # Normalised wide_center_of_mass[i] = np.sum(x * f) wide_width[i] = np.sqrt(np.sum( (x - wide_center_of_mass[i])**2 * f)) wide_skewness[i] = np.sum(f *\ ((x - wide_center_of_mass[i]) / wide_width[i])**3) # Best fitting gaussian to the wide component gaussian = np.exp(-0.5*(x - wide_center_of_mass[i])**2 \ / wide_width[i]**2) gaussian /= gaussian.sum() # Nongaussianity based on KL divergence wide_nongaussianity[i] = np.sum(f * np.log(f / gaussian + 1E-300)) spikes_integral[i] = np.sum(the_spikes) else: wide_center_of_mass[i] = np.NaN wide_width[i] = np.NaN wide_skewness[i] = np.NaN wide_nongaussianity[i] = np.NaN # Plot the model. 50 posterior samples and the # posterior mean for each part. if i < 50: plt.plot(data[:, 0], model, "g", linewidth=1, alpha=0.1) plt.plot(data[:, 0], wide_component, "b", linewidth=1, alpha=0.1) plt.plot(data[:, 0], the_spikes, "r", linewidth=1, alpha=0.1) plt.plot(data[:, 0], bg, "y", linewidth=1, alpha=0.1) plt.ylim(0) # Plot the posterior means plt.plot(data[:, 0], model_tot / posterior_sample.shape[0], "g-", linewidth=3, alpha=1) plt.plot(data[:, 0], wide_component_tot / posterior_sample.shape[0], "b--", linewidth=3, alpha=1) plt.plot(data[:, 0], the_spikes_tot / posterior_sample.shape[0], "r--", linewidth=3, alpha=1) plt.plot(data[:, 0], bg_tot / posterior_sample.shape[0], "y--", linewidth=3, alpha=1) # Plot the data plt.plot(data[:, 0], data[:, 1], "ko", markersize=3, alpha=0.5) plt.xlabel("$2\\theta$ (degrees)", fontsize=14) plt.ylabel("Intensity", fontsize=14) plt.show() # Plot the standardised residuals of the posterior mean curve. # Use the posterior mean of sigma0 and sigma1 for the standardisation. # This is not ideal - really there is a posterior distribution over # residuals. curve = model_tot / posterior_sample.shape[0] sd = np.sqrt(posterior_sample[:,indices["sigma0"]].mean()**2 \ + posterior_sample[:,indices["sigma1"]].mean()*curve) resid = (curve - data[:, 1]) / sd plt.plot(data[:, 0], resid, "-") plt.xlabel("$2\\theta$ (degrees)") plt.ylabel("Standardised residuals (of posterior mean model curve)") plt.show() # Number of narrow components binwidth = 0.8 plt.hist(posterior_sample[:, 11], bins=np.arange(0, max_num_spikes) - 0.5 * binwidth, width=binwidth, color=[0.3, 0.3, 0.3]) plt.xlabel("Number of narrow components") plt.ylabel("Number of posterior samples") # A data frame to store some results macroquantities["num_narrow_peaks"] = posterior_sample[:, 11] plt.show() wide_fraction = wide_integral / (spikes_integral + wide_integral) macroquantities["amorph/(amorph + crystal)"] = wide_fraction # Remove any nans before plotting wide_fraction = wide_fraction[~np.isnan(wide_fraction)] plt.hist(wide_fraction, 100, color=[0.3, 0.3, 0.3]) plt.xlim([0, 1]) plt.xlabel("(amorph)/(amorph + crystal)") print("(amorph)/(amorph + crystal) = {a} +- {b}".format(a=wide_fraction.mean(),\ b=wide_fraction.std())) plt.show() macroquantities["wide_center_of_mass"] = wide_center_of_mass wide_center_of_mass = wide_center_of_mass[~np.isnan(wide_center_of_mass)] plt.hist(wide_center_of_mass, 100, color=[0.3, 0.3, 0.3]) plt.xlabel("Center of mass of wide component") print("Center of mass = {a} +- {b}".format(a=wide_center_of_mass.mean(), b=wide_center_of_mass.std())) plt.show() macroquantities["wide_halfwidth"] = wide_width wide_width = wide_width[~np.isnan(wide_width)] plt.hist(wide_width, 100, color=[0.3, 0.3, 0.3]) plt.xlabel("Half-width of wide component") print("Half-width = {a} +- {b}".format(a=wide_width.mean(), b=wide_width.std())) plt.show() macroquantities["wide_skewness"] = wide_skewness wide_skewness = wide_skewness[~np.isnan(wide_skewness)] plt.hist(wide_skewness, 100, color=[0.3, 0.3, 0.3]) plt.xlabel("Skewness of wide component") print("Skewness = {a} +- {b}".format(a=wide_skewness.mean(), b=wide_skewness.std())) plt.show() macroquantities["wide_nongaussianity"] = wide_nongaussianity wide_nongaussianity = wide_nongaussianity[~np.isnan(wide_nongaussianity)] plt.hist(wide_nongaussianity, 100, color=[0.3, 0.3, 0.3]) plt.xlabel("Nongaussianity of wide component") plt.xlim(0.0) print("Nongaussianity = {a} +- {b}".format(a=wide_nongaussianity.mean(), b=wide_nongaussianity.std())) plt.show() print("Saving macroquantities.csv") macroquantities.index = range(1, posterior_sample.shape[0] + 1) macroquantities.to_csv("macroquantities.csv", index_label="Sample number")
from pylab import * import os import dnest4.classic as dn4 data = loadtxt('fake_data.txt') posterior_sample = atleast_2d(dn4.my_loadtxt('posterior_sample.txt')) saveFrames = False # For making movies if saveFrames: os.system('rm Frames/*.png') ion() for i in range(0, posterior_sample.shape[0]): hold(False) plot(data[:,0], data[:,1], 'k.', alpha=0.2) hold(True) plot(data[:,0], posterior_sample[i, 0:data.shape[0]], 'g') xlabel('Time', fontsize=16) ylabel('y', fontsize=16) draw() if saveFrames: savefig('Frames/' + '%0.4d'%(i+1) + '.png', bbox_inches='tight') print('Frames/' + '%0.4d'%(i+1) + '.png') ioff() show()
import numpy as np import matplotlib.pyplot as plt import os import dnest4.classic as dn4 data = dn4.my_loadtxt("easy_data.txt") posterior_sample = dn4.my_loadtxt("posterior_sample.txt") indices = dn4.load_column_names("posterior_sample.txt")["indices"] print("WARNING! This will delete\ movie.mkv and the Frames/ directory, if these exist.") ch = input("Continue? y/n: ") if ch != "y" and ch != "Y": exit() os.system("rm -rf Frames/ movie.mkv") os.mkdir("Frames") for i in range(0, posterior_sample.shape[0]): line = posterior_sample[i, :].copy() line = line[indices["model_prediction[0]"]:] plt.clf() plt.subplot(3, 1, 1) plt.hold(False) plt.errorbar(data[:,0], data[:,1], yerr=data[:,2], fmt="ko") plt.hold(True) plt.plot(data[:,0], line[0:data.shape[0]], "g",\ linewidth=2) plt.gca().invert_yaxis()
setup["data_files"]["sigmas_file"] # Load files (DNest4 output and data files) f = open(a) metadata = yaml.load(f, Loader=yaml.SafeLoader) f.close() num_images = metadata["num_images"] ni = metadata["ni"] nj = metadata["nj"] max_num_stars = setup["assumptions"]["max_num_stars"] num_pixels = ni * nj * num_images # Convert back to list metadata = [num_images, ni, nj, metadata["x_min"], metadata["x_max"],\ metadata["y_min"], metadata["y_max"]] posterior_sample = dn4.my_loadtxt('posterior_sample.txt', single_precision=True) indices = dn4.load_column_names("posterior_sample.txt")["indices"] data = np.reshape(np.loadtxt(b), (num_images, ni, nj)) sig = np.reshape(np.loadtxt(c), (num_images, ni, nj)) stars = posterior_sample[:, (num_pixels + 3 + 2 * num_images):(num_pixels + 3 + 2 * num_images + max_num_stars * (2 + num_images))] stars_x = stars[:, 0:max_num_stars] stars_y = stars[:, max_num_stars:2 * max_num_stars] stars_f = stars[:, 2 * max_num_stars:2 * max_num_stars + num_images * max_num_stars] # Plot posterior for number of stars # Histogram-like.
# The four control points xx = np.array([x.min(), 10.0, 40.0, x.max()]) yy = b * np.exp(n) # Plot the background component shape plt.plot(xx, yy, "ko-", alpha=0.2) plt.xlabel("$x$") plt.ylabel("$y$") plt.title("Background component") plt.savefig("figures/background.pdf", bbox_inches="tight") print("figures/background.pdf done") plt.clf() # Load some prior samples sample = dn4.my_loadtxt("figures/sample.txt") indices = dn4.load_column_names("figures/sample.txt")["indices"] x = dn4.my_loadtxt("../src/easy_data.txt")[:,0] start = indices["wide[0]"] end = indices["wide[1000]"] + 1 for i in range(0, 8): k = rng.randint(sample.shape[0]) y = sample[k, start:end] y /= y.max()# + 0.00001 y *= np.exp(0.1*rng.randn()) plt.plot(x, y, "-", alpha=0.4) plt.xlabel("$x$") plt.ylabel("$y$")
import dnest4.classic as dn4 import matplotlib.pyplot as plt dn4.postprocess() # Load the data and posterior samples data = dn4.my_loadtxt("data/example_data.txt").reshape((75, 80, 100)) posterior_sample = dn4.my_loadtxt("posterior_sample.txt") indices = dn4.load_column_names("sample.txt")["indices"] # Residual of first posterior sample (summed over frequency dimension) image_start = indices["model_image[0][0][0]"] model = posterior_sample[0, image_start:].reshape((75, 80, 100)) plt.imshow(data.sum(axis=2) - model.sum(axis=2), cmap="coolwarm") plt.title("Residuals") plt.show()
import matplotlib.pyplot as plt import numpy as np import dnest4.classic as dn4 plt.rcParams["font.family"] = "serif" plt.rcParams["font.size"] = 16 plt.rc("text", usetex=True) posterior_sample = np.atleast_2d(dn4.my_loadtxt("posterior_sample.txt")) plt.plot(posterior_sample[:,0], np.exp(posterior_sample[:,1]),\ "ko", alpha=0.1, markersize=5, label="Posterior samples") plt.plot(0, 1, "r*", markersize=20, label="Truth", alpha=0.5) plt.xlabel("$\\mu$") plt.ylabel("$\\sigma$") plt.legend(loc="upper left", numpoints=1) plt.title("ABC Results") plt.axis([-1.5, 1.5, 0.5, 1.5]) plt.savefig("abc_results.pdf", bbox_inches="tight") plt.show()
import dnest4.classic as dn4 import matplotlib.pyplot as plt import numpy as np import os sample = dn4.my_loadtxt("sample.txt") num_atoms = sample.shape[1] // 3 x = sample[:, 0:num_atoms] y = sample[:, num_atoms:2*num_atoms] os.system("rm figures/*.png") for i in range(0, sample.shape[0]): plt.clf() plt.plot(x[i, :], y[i, :], ".") plt.axis("square") plt.axis([0.0, 50.0, 0.0, 50.0]) plt.savefig("figures/{i}.png".format(i=i)) print(i+1)
""" img = np.zeros(x.shape) for i in range(blobs.shape[0]): # Unpack blob parameters xc, yc, M, w = blobs[i, :] rsq = (x - xc)**2 + (y - yc)**2 inside = rsq <= w**2 img[inside] += 2.0*M/(np.pi*w**2)*(1.0 - rsq[inside]/w**2) return img # Load the posterior samples print("Loading posterior samples...", end="", flush=True) posterior_sample = dn4.my_loadtxt("posterior_sample.txt") indices = dn4.load_column_names("posterior_sample.txt")["indices"] print("done.") # Create a grid based on the properties of the run import yaml f = open("run_files.yaml") metadata_filename = yaml.load(f, Loader=yaml.SafeLoader)["metadata_file"] f.close() f = open(metadata_filename) the_dict = yaml.load(f, Loader=yaml.SafeLoader)["dimensions"] x_min, x_max, y_min, y_max = the_dict["x_min"], the_dict["x_max"],\ the_dict["y_min"], the_dict["y_max"] nj, ni = the_dict["nj"], the_dict["ni"] f.close()
from pylab import * import dnest4.classic as dn4 rc("font", size=14, family="serif", serif="Computer Sans") rc("text", usetex=True) # Piecewise linear stretch def stretch(x): y = x.copy() y = (y - y.min()) / (y.max() - y.min()) y[y > 0.1] = 0.1 + 0.05 * (y[y > 0.1] - 0.1) return y data = dn4.my_loadtxt('Data/test_image.txt') img = stretch(data) imshow(img, cmap='gray', interpolation='nearest') xlabel('$x$') ylabel('$y$') gca().set_xticks([-0.5, 99.5, 199.5]) gca().set_yticks([-0.5, 99.5, 199.5]) gca().set_xticklabels(['-1', '0', '1']) gca().set_yticklabels(['1', '0', '-1']) title('GalaxyField Data') savefig('galaxyfield_data.pdf', bbox_inches='tight') show() posterior_sample = dn4.my_loadtxt('posterior_sample.txt')
import dnest4.classic as dn4 import numpy as np # Load samples sample = dn4.my_loadtxt("sample.txt") sample_info = dn4.my_loadtxt("sample_info.txt") # Log likelihood logL = sample_info[:, 1] # Best particle which = np.nonzero(logL == logL.max())[0] best = sample[which, :] # Get header f = open("sample.txt") header = f.readline() f.close() np.savetxt("best_fit.txt", best, header=header[1:]) print("Saved to best_fit.txt")
from pylab import * rc("font", size=14, family="serif", serif="Computer Sans") rc("text", usetex=True) import dnest4.classic as dn4 posterior_sample = dn4.my_loadtxt('posterior_sample.txt') data = loadtxt('fake_data.txt') width = 0.5 bins = np.arange(0.0, 100.0) - 0.5 * width hist(posterior_sample[:, data.shape[0] + 7], bins=bins, width=width, color="k", alpha=0.2) xlabel('Number of Sinusoids $N$', fontsize=14) ylabel('Number of posterior samples', fontsize=14) xlim([-0.5, 100.5]) savefig('N_result.pdf', bbox_inches='tight') show() #sample_info = loadtxt('Metropolis/sample_info.txt') #plot(sample_info[:,1], linewidth=1) #xlabel('Iteration / 20000') #ylabel('Log Likelihood') #savefig('trace_logl.pdf', bbox_inches='tight') #show()
import numpy as np import matplotlib.pyplot as plt import os import dnest4.classic as dn4 data = dn4.my_loadtxt("easy_data.txt") posterior_sample = dn4.my_loadtxt("posterior_sample.txt") indices = dn4.load_column_names("posterior_sample.txt")["indices"] print("WARNING! This will delete\ movie.mkv and the Frames/ directory, if these exist.") ch = input("Continue? y/n: ") if ch != "y" and ch != "Y": exit() os.system("rm -rf Frames/ movie.mkv") os.mkdir("Frames") for i in range(0, posterior_sample.shape[0]): line = posterior_sample[i, :].copy() line = line[indices["model_prediction[0]"]:] plt.clf() plt.subplot(3, 1, 1) plt.hold(False) plt.errorbar(data[:, 0], data[:, 1], yerr=data[:, 2], fmt="ko") plt.hold(True) plt.plot(data[:,0], line[0:data.shape[0]], "g",\ linewidth=2) plt.gca().invert_yaxis()
import numpy as np import dnest4.classic as dn4 import matplotlib.pyplot as plt # Load both sample.txt and posterior_sample.txt sample = dn4.my_loadtxt("sample.txt", single_precision=True) posterior_sample = dn4.my_loadtxt("posterior_sample.txt", single_precision=True) metadata = np.loadtxt('Data/1000_metadata.txt') indices = dn4.load_column_names("sample.txt")["indices"] num_images = int(metadata[0]) ni = int(metadata[1]) nj = int(metadata[2]) max_num_stars = 2000 num_pixels = ni * nj * num_images kernel = np.zeros((ni, nj)) ii = np.arange(0, ni) jj = np.arange(0, nj) [jj, ii] = np.meshgrid(ii, jj) rsq = (ii - ni / 2)**2 + (jj - nj / 2)**2 f = 1.0 / (1.0 + rsq / 10.0**2) f /= f.sum() f = np.fft.fftshift(f) _f = np.fft.fft2(f) def convolve(img): _img = np.fft.fft2(img) _result = _img * _f
import numpy as np import matplotlib.pyplot as plt import dnest4.classic as dn4 dn4.postprocess_abc() plt.rc("font", size=18, family="serif", serif="Computer Sans") plt.rc("text", usetex=True) posterior_sample = np.atleast_2d(dn4.my_loadtxt("posterior_sample.txt")) plt.plot(posterior_sample[:,0], np.exp(posterior_sample[:,1]),\ "ko", alpha=0.1, markersize=5, label="Posterior samples") plt.hold(True) plt.plot(0, 1, "r*", markersize=20, label="Truth", alpha=0.5) plt.xlabel("$\\mu$") plt.ylabel("$\\sigma$") plt.legend(loc="upper left", numpoints=1) plt.title("ABC Results") plt.axis([-1.5, 1.5, 0.5, 1.5]) plt.savefig("abc_results.pdf", bbox_inches="tight") plt.show()
import dnest4.classic as dn4 dn4.postprocess() import matplotlib.pyplot as plt posterior_sample = dn4.my_loadtxt("posterior_sample.txt") # Plot some curves through the data x_data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0] y_data = [1.0, 3.8, 3.7, 3.8, 2.9, 2.4, 2.0, 1.3, 0.4, -0.2] plt.plot(x_data, y_data, "o") for i in range(0, 10): plt.plot(x_data, posterior_sample[i, 2:], color="g", alpha=0.2) plt.show()
from pylab import * rc("font", size=14, family="serif", serif="Computer Sans") rc("text", usetex=True) import dnest4.classic as dn4 posterior_sample = dn4.my_loadtxt('posterior_sample.txt') data = loadtxt('fake_data.txt') width = 0.5 bins = np.arange(0.0, 100.0) - 0.5*width hist(posterior_sample[:,data.shape[0] + 7], bins=bins, width=width, color="k", alpha=0.2) xlabel('Number of Sinusoids $N$', fontsize=14) ylabel('Number of posterior samples', fontsize=14) xlim([-0.5, 100.5]) savefig('N_result.pdf', bbox_inches='tight') show() #sample_info = loadtxt('Metropolis/sample_info.txt') #plot(sample_info[:,1], linewidth=1) #xlabel('Iteration / 20000') #ylabel('Log Likelihood') #savefig('trace_logl.pdf', bbox_inches='tight') #show()
from pylab import * import os import dnest4.classic as dn4 data = loadtxt('fake_data.txt') posterior_sample = atleast_2d(dn4.my_loadtxt('posterior_sample.txt')) saveFrames = False # For making movies if saveFrames: os.system('rm Frames/*.png') ion() for i in range(0, posterior_sample.shape[0]): hold(False) plot(data[:, 0], data[:, 1], 'k.', alpha=0.2) hold(True) plot(data[:, 0], posterior_sample[i, 0:data.shape[0]], 'g') xlabel('Time', fontsize=16) ylabel('y', fontsize=16) draw() if saveFrames: savefig('Frames/' + '%0.4d' % (i + 1) + '.png', bbox_inches='tight') print('Frames/' + '%0.4d' % (i + 1) + '.png') ioff() show()
from pylab import * import dnest4.classic as dn4 rc("font", size=14, family="serif", serif="Computer Sans") rc("text", usetex=True) # Piecewise linear stretch def stretch(x): y = x.copy() y = (y - y.min())/(y.max() - y.min()) y[y > 0.1] = 0.1 + 0.05*(y[y > 0.1] - 0.1) return y data = dn4.my_loadtxt('Data/test_image.txt') img = stretch(data) imshow(img, cmap='gray', interpolation='nearest') xlabel('$x$') ylabel('$y$') gca().set_xticks([-0.5, 99.5, 199.5]) gca().set_yticks([-0.5, 99.5, 199.5]) gca().set_xticklabels(['-1', '0', '1']) gca().set_yticklabels(['1', '0', '-1']) title('GalaxyField Data') savefig('galaxyfield_data.pdf', bbox_inches='tight') show() posterior_sample = dn4.my_loadtxt('posterior_sample.txt') hist(posterior_sample[:,40006], 50)