Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/emilyhunt/masters_project. My Master's project code, using a mixture density network to infer photometric redshifts to galaxies with spectroscopic redshifts as training data.
Something went wrong. Try again.
masters_project plotter_pdfs.py
7.1 kB · 234 lines
Python
at master
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235"""PDF plots for the report"""
import numpy as npimport pandas as pdimport pickleimport matplotlib.pyplot as pltfrom scipy.stats import norm as scipy_normal
# Make TeX labels work on plots#plt.rc('font', **{'family': 'serif', 'serif': ['DejaVu Sans']})#plt.rc('text', usetex=True)
# Grab some datadata_train = pd.read_csv('./final_run_data/data_training.csv')data_valid_0 = pd.read_csv('./final_run_data/different_sn/data_validation_sn=1.0.csv')data_valid_4 = pd.read_csv('./final_run_data/different_sn/data_validation_sn=4.0.csv')
results_valid_0 = pd.read_csv('./final_run_data/different_sn/validation_results_sn=1.0.csv')results_valid_4 = pd.read_csv('./final_run_data/different_sn/validation_results_sn=4.0.csv')
with open('./final_run_data/different_sn/validation_mixtures_sn=1.0.pkl', 'rb') as f: mixtures_0 = pickle.load(f)
with open('./final_run_data/different_sn/validation_mixtures_sn=4.0.pkl', 'rb') as f: mixtures_4 = pickle.load(f)
# Make some pwetty PDF plotsfig = plt.figure(figsize=(3, 3))ax_1 = fig.add_subplot(2, 2, 1)ax_2 = fig.add_subplot(2, 2, 3, sharex=ax_1)ax_3 = fig.add_subplot(2, 2, 2)ax_4 = fig.add_subplot(2, 2, 4, sharex=ax_3)
ax_pdf_0 = ax_1ax_pdf_4 = ax_2
# Galaxy to plotgalaxy = 7 # 12, 39, 45, 18resolution = 200range = [1.7, 4.7]x_range = np.linspace(range[0], range[1], num=200)
# list of colourscolors = ['r', 'y', 'c', 'b', 'm']
# Get its deetsz_spec = data_valid_0.loc[galaxy, 'z_spec']z_ml_0 = results_valid_0['map']z_ml_4 = results_valid_4['map']z_upper_0 = results_valid_0['upper']z_upper_4 = results_valid_4['upper']z_lower_0 = results_valid_0['lower']z_lower_4 = results_valid_4['lower']
######################## SN0 plot galaxy 1
# Keep a record of the running totaltotal = np.zeros(resolution)
# Grab our deetsmeans = mixtures_0['means'][galaxy]std_deviations = mixtures_0['std_deviations'][galaxy]weights = mixtures_0['weights'][galaxy]
# Plot all the mixtures iterativelyi = 1for a_mean, a_std, a_weight, a_color in zip(means, std_deviations, weights, colors): this_pdf = a_weight * scipy_normal.pdf(x_range, loc=a_mean, scale=a_std) total = total + this_pdf ax_pdf_0.fill_between(x_range, this_pdf, color=a_color, alpha=0.3, label='Mixture {}'.format(i)) i += 1
y_size = total.max() * 1.1
ax_pdf_0.plot(x_range, total, '-', color='k', lw=1, alpha=1.0, label='Summed mixtures')ax_pdf_0.plot([z_spec, z_spec], [0, y_size], 'r-', alpha=1.0, lw=1, label='True value')
# Labels and stuffax_pdf_0.set_xlim(0, 7)ax_pdf_0.set_ylim(0, total.max() * 1.1)
ax_pdf_0.set_ylabel(r'$P(z)$')
######################## SN4 plot galaxy 1
# Keep a record of the running totaltotal = np.zeros(resolution)
# Grab our deetsmeans = mixtures_4['means'][galaxy]std_deviations = mixtures_4['std_deviations'][galaxy]weights = mixtures_4['weights'][galaxy]
# Plot all the mixtures iterativelyfor a_mean, a_std, a_weight, a_color in zip(means, std_deviations, weights, colors): this_pdf = a_weight * scipy_normal.pdf(x_range, loc=a_mean, scale=a_std) total = total + this_pdf ax_pdf_4.fill_between(x_range, this_pdf, color=a_color, alpha=0.3, label='Mixture {}'.format(i))
y_size = total.max() * 1.1
ax_pdf_4.plot(x_range, total, '-', color='k', lw=1, alpha=1.0, label='Summed total')ax_pdf_4.plot([z_spec, z_spec], [0, y_size], 'r-', alpha=1.0, lw=1)
# Labels and stuffax_pdf_4.set_xlim(range[0], range[1])ax_pdf_4.set_ylim(0, y_size)
###############################################################################
ax_pdf_0 = ax_3ax_pdf_4 = ax_4
# Galaxy to plotgalaxy = 12 # 7, 12, 26, 35resolution = 200range = [0, 3.1]x_range = np.linspace(range[0], range[1], num=200)
# list of colourscolors = ['r', 'y', 'c', 'b', 'm']
# Get its deetsz_spec = data_valid_0.loc[galaxy, 'z_spec']z_ml_0 = results_valid_0['map']z_ml_4 = results_valid_4['map']z_upper_0 = results_valid_0['upper']z_upper_4 = results_valid_4['upper']z_lower_0 = results_valid_0['lower']z_lower_4 = results_valid_4['lower']
######################## SN0 plot galaxy 2
# Keep a record of the running totaltotal = np.zeros(resolution)
# Grab our deetsmeans = mixtures_0['means'][galaxy]std_deviations = mixtures_0['std_deviations'][galaxy]weights = mixtures_0['weights'][galaxy]
# Plot all the mixtures iterativelyi = 1for a_mean, a_std, a_weight, a_color in zip(means, std_deviations, weights, colors): this_pdf = a_weight * scipy_normal.pdf(x_range, loc=a_mean, scale=a_std) total = total + this_pdf ax_pdf_0.fill_between(x_range, this_pdf, color=a_color, alpha=0.3, label='Mixture {}'.format(i)) i += 1
y_size = total.max() * 1.1
ax_pdf_0.plot(x_range, total, '-', color='k', lw=1, alpha=1.0, label='Summed mixtures')ax_pdf_0.plot([z_spec, z_spec], [0, y_size], 'r-', alpha=1.0, lw=1, label='True value')
# Labels and stuffax_pdf_0.set_xlim(range[0], range[1])ax_pdf_0.set_ylim(0, y_size)
######################## SN4 plot galaxy 2
# Keep a record of the running totaltotal = np.zeros(resolution)
# Grab our deetsmeans = mixtures_4['means'][galaxy]std_deviations = mixtures_4['std_deviations'][galaxy]weights = mixtures_4['weights'][galaxy]
# Plot all the mixtures iterativelyfor a_mean, a_std, a_weight, a_color in zip(means, std_deviations, weights, colors): this_pdf = a_weight * scipy_normal.pdf(x_range, loc=a_mean, scale=a_std) total = total + this_pdf ax_pdf_4.fill_between(x_range, this_pdf, color=a_color, alpha=0.3, label='Mixture {}'.format(i))
y_size = total.max() * 1.1
ax_pdf_4.plot(x_range, total, '-', color='k', lw=1, alpha=1.0, label='Summed total')ax_pdf_4.plot([z_spec, z_spec], [0, y_size], 'r-', alpha=1.0, lw=1)
# Labelsax_pdf_4.set_xlim(range[0], range[1])ax_pdf_4.set_ylim(0, y_size)
#######################
# Final deets and saving
ax_1.set_ylabel(r'$P(z)$')ax_2.set_ylabel(r'$P(z)$')ax_2.set_xlabel(r'$z$')ax_4.set_xlabel(r'$z$')
ax_1.set_title('Galaxy 7', fontsize=8)ax_3.set_title('Galaxy 12', fontsize=8)
# Label plotsax_3.text(-0.03, 0.8, r'$SNR \cdot 1.0$', ha='center', va='center', transform=ax_3.transAxes, fontsize=8, bbox=dict(boxstyle='round', ec=(0.0, 0.0, 0.0), fc=(1., 1.0, 1.0), ))
ax_4.text(-0.03, 0.8, r'$SNR \cdot 0.25$', ha='center', va='center', transform=ax_4.transAxes, fontsize=8, bbox=dict(boxstyle='round', ec=(0.0, 0.0, 0.0), fc=(1., 1.0, 1.0), ))
# Add a legend to plot 3#ax_3.legend(edgecolor='k', facecolor='w', fancybox=False, fontsize=8,# bbox_to_anchor=(0.0, 1.0), ncols=3)
# Fix possible spacing issues between left and right subplotsfig.subplots_adjust(hspace=0, wspace=0.05)
# Sort out the ticksax_2.set_xticks([2, 3, 4])ax_4.set_xticks([0, 1, 2, 3])ax_4.set_yticks([0, 1, 2])
ax_3.yaxis.tick_right()ax_4.yaxis.tick_right()plt.setp([a.get_xticklabels() for a in fig.axes[::2]], visible=False)
# fig.tight_layout()
fig.savefig('./final_plots/pres_pdfs.png', dpi=600, bbox_inches='tight')
fig.show()