Skip to content
Snippets Groups Projects
Commit 15981225 authored by frcojimenez's avatar frcojimenez
Browse files

added error options

parent c11167ed
No related branches found
No related tags found
No related merge requests found
Source diff could not be displayed: it is too large. Options to address this: view the blob.
#!/usr/bin/env python #!/usr/bin/env python
# coding: utf-8 # coding: utf-8
# In[3]: # In[48]:
#Import relevant modules, import data and all that #Import relevant modules, import data and all that
...@@ -49,7 +49,7 @@ except SystemExit: ...@@ -49,7 +49,7 @@ except SystemExit:
pass pass
# In[4]: # In[49]:
# path # path
...@@ -64,9 +64,10 @@ simulation_number = np.int(simulation_number) ...@@ -64,9 +64,10 @@ simulation_number = np.int(simulation_number)
output_folder = parser.get('output-folder','output-folder') output_folder = parser.get('output-folder','output-folder')
overwrite = parser.get('setup','overwrite') overwrite = parser.get('setup','overwrite')
downfactor = np.int(parser.get('setup','plot_down_factor')) downfactor = np.int(parser.get('setup','plot_down_factor'))
sampler = parser.get('setup','sampler')
# In[5]: # In[50]:
if not os.path.exists(output_folder): if not os.path.exists(output_folder):
...@@ -74,7 +75,7 @@ if not os.path.exists(output_folder): ...@@ -74,7 +75,7 @@ if not os.path.exists(output_folder):
print("Directory " , output_folder , " Created ") print("Directory " , output_folder , " Created ")
# In[6]: # In[51]:
# time config # time config
...@@ -89,7 +90,7 @@ t_align=parser.get('time-setup','t_align') ...@@ -89,7 +90,7 @@ t_align=parser.get('time-setup','t_align')
t_align = np.float(t_align) t_align = np.float(t_align)
# In[7]: # In[52]:
# n-tones & nlive # n-tones & nlive
...@@ -101,7 +102,7 @@ npoints=parser.get('n-live-points','npoints') ...@@ -101,7 +102,7 @@ npoints=parser.get('n-live-points','npoints')
npoints = np.int(npoints) npoints = np.int(npoints)
# In[8]: # In[81]:
# model # model
...@@ -120,16 +121,16 @@ print('tshift:',tshift) ...@@ -120,16 +121,16 @@ print('tshift:',tshift)
print('error:', error_str) print('error:', error_str)
# In[10]: # In[83]:
output_folder_1=output_folder+'/'+model+'-nmax'+str(nmax) output_folder_1=output_folder+'/'+model+'-nmax'+str(nmax)+'_'+str(error_str)
if not os.path.exists(output_folder_1): if not os.path.exists(output_folder_1):
os.mkdir(output_folder_1) os.mkdir(output_folder_1)
print("Directory " , output_folder_1 , " Created ") print("Directory " , output_folder_1 , " Created ")
# In[11]: # In[84]:
corner_plot=output_folder_1+'/Dynesty_'+str(simulation_number)+'_'+model+'_nmax='+str(nmax)+'_tshift='+str(tshift)+'_'+str(npoints)+'corner_plot.png' corner_plot=output_folder_1+'/Dynesty_'+str(simulation_number)+'_'+model+'_nmax='+str(nmax)+'_tshift='+str(tshift)+'_'+str(npoints)+'corner_plot.png'
...@@ -137,14 +138,14 @@ diagnosis_plot=output_folder_1+'/Dynesty_diagnosis'+str(simulation_number)+'_'+m ...@@ -137,14 +138,14 @@ diagnosis_plot=output_folder_1+'/Dynesty_diagnosis'+str(simulation_number)+'_'+m
fit_plot=output_folder_1+'/Fit_results_'+str(simulation_number)+'tshift_'+str(tshift)+'_'+model+'_nmax_'+str(nmax)+'.png' fit_plot=output_folder_1+'/Fit_results_'+str(simulation_number)+'tshift_'+str(tshift)+'_'+model+'_nmax_'+str(nmax)+'.png'
# In[12]: # In[85]:
sumary_data = output_folder_1+'/summary'+str(simulation_number)+'_'+model+'_nmax_'+str(nmax)+'.csv' sumary_data = output_folder_1+'/summary'+str(simulation_number)+'_'+model+'_nmax_'+str(nmax)+'.csv'
best_data=output_folder_1+'/best_values_'+str(simulation_number)+'_'+model+'_nmax_'+str(nmax)+'.csv' best_data=output_folder_1+'/best_values_'+str(simulation_number)+'_'+model+'_nmax_'+str(nmax)+'.csv'
# In[66]: # In[86]:
# loading priors # loading priors
...@@ -193,7 +194,7 @@ if model == 'w-tau-fixed': ...@@ -193,7 +194,7 @@ if model == 'w-tau-fixed':
prior_dim = len(priors_min) prior_dim = len(priors_min)
# In[67]: # In[87]:
vary_fund = True vary_fund = True
...@@ -241,7 +242,7 @@ def tauRD_to_t_Phys(tau,M): ...@@ -241,7 +242,7 @@ def tauRD_to_t_Phys(tau,M):
return ((M*MS*G)/c**3)*tau return ((M*MS*G)/c**3)*tau
# In[68]: # In[88]:
#This loads the 22 mode data #This loads the 22 mode data
...@@ -273,7 +274,7 @@ tmax5=FindTmaximum(gw5_sxs_bbh_0305) ...@@ -273,7 +274,7 @@ tmax5=FindTmaximum(gw5_sxs_bbh_0305)
times5 = times5 - tmax5 times5 = times5 - tmax5
# In[69]: # In[89]:
#Select the data from 0 onwards #Select the data from 0 onwards
...@@ -285,7 +286,7 @@ timesrd=gw_sxs_bbh_0305[position:-1][:,0][:-1]-tmax ...@@ -285,7 +286,7 @@ timesrd=gw_sxs_bbh_0305[position:-1][:,0][:-1]-tmax
timesrd5=gw5_sxs_bbh_0305[position5:-1][:,0][:-1]-tmax5 timesrd5=gw5_sxs_bbh_0305[position5:-1][:,0][:-1]-tmax5
# In[70]: # In[90]:
#Test plot real part (data was picked in the last cell). Aligning in time #Test plot real part (data was picked in the last cell). Aligning in time
...@@ -297,7 +298,7 @@ plt.plot(timesrd5, np.sqrt(gw_sxs_bbh_0305rd5[:,1]**2+gw_sxs_bbh_0305rd5[:,2]**2 ...@@ -297,7 +298,7 @@ plt.plot(timesrd5, np.sqrt(gw_sxs_bbh_0305rd5[:,1]**2+gw_sxs_bbh_0305rd5[:,2]**2
plt.legend() plt.legend()
# In[71]: # In[91]:
#Test plot im part (data was picked in the last cell). Aligning in time #Test plot im part (data was picked in the last cell). Aligning in time
...@@ -309,7 +310,7 @@ plt.plot(timesrd5, np.sqrt(gw_sxs_bbh_0305rd5[:,1]**2+gw_sxs_bbh_0305rd5[:,2]**2 ...@@ -309,7 +310,7 @@ plt.plot(timesrd5, np.sqrt(gw_sxs_bbh_0305rd5[:,1]**2+gw_sxs_bbh_0305rd5[:,2]**2
plt.legend() plt.legend()
# In[72]: # In[92]:
# Depending on nmax, you load nmax number of freqs. and damping times from the qnm package # Depending on nmax, you load nmax number of freqs. and damping times from the qnm package
...@@ -318,7 +319,7 @@ w = (np.real(omegas))/mf ...@@ -318,7 +319,7 @@ w = (np.real(omegas))/mf
tau=-1/(np.imag(omegas))*mf tau=-1/(np.imag(omegas))*mf
# In[73]: # In[93]:
gwnew_re = interpolate.interp1d(timesrd, gw_sxs_bbh_0305rd[:,1], kind = 'cubic') gwnew_re = interpolate.interp1d(timesrd, gw_sxs_bbh_0305rd[:,1], kind = 'cubic')
...@@ -328,7 +329,7 @@ gwnew_re5 = interpolate.interp1d(timesrd5, gw_sxs_bbh_0305rd5[:,1], kind = 'cubi ...@@ -328,7 +329,7 @@ gwnew_re5 = interpolate.interp1d(timesrd5, gw_sxs_bbh_0305rd5[:,1], kind = 'cubi
gwnew_im5 = interpolate.interp1d(timesrd5, gw_sxs_bbh_0305rd5[:,2], kind = 'cubic') gwnew_im5 = interpolate.interp1d(timesrd5, gw_sxs_bbh_0305rd5[:,2], kind = 'cubic')
# In[74]: # In[94]:
if timesrd5[-1]>= timesrd[-1]: if timesrd5[-1]>= timesrd[-1]:
...@@ -345,7 +346,7 @@ gwdatanew = gwdatanew_re - 1j*gwdatanew_im ...@@ -345,7 +346,7 @@ gwdatanew = gwdatanew_re - 1j*gwdatanew_im
gwdatanew5 = gwdatanew_re5- 1j*gwdatanew_im5 gwdatanew5 = gwdatanew_re5- 1j*gwdatanew_im5
# In[75]: # In[95]:
mismatch=1-EasyMatchT(timesrd_final,gwdatanew,gwdatanew5,0,0+90) mismatch=1-EasyMatchT(timesrd_final,gwdatanew,gwdatanew5,0,0+90)
...@@ -353,7 +354,7 @@ error=np.sqrt(2*mismatch) ...@@ -353,7 +354,7 @@ error=np.sqrt(2*mismatch)
print(mismatch) print(mismatch)
# In[76]: # In[96]:
# Phase alignement # Phase alignement
...@@ -365,7 +366,7 @@ plt.plot(timesrd_final, phas, "r", alpha=0.3, lw=3, label=r'$phase$') ...@@ -365,7 +366,7 @@ plt.plot(timesrd_final, phas, "r", alpha=0.3, lw=3, label=r'$phase$')
plt.plot(timesrd_final, phas5, "blue", alpha=0.3, lw=3, label=r'$phase$') plt.plot(timesrd_final, phas5, "blue", alpha=0.3, lw=3, label=r'$phase$')
# In[77]: # In[97]:
position = np.argmax(timesrd_final >= (t_align)) position = np.argmax(timesrd_final >= (t_align))
...@@ -384,7 +385,7 @@ plt.plot(timesrd_final, phas, "r", alpha=0.3, lw=3, label=r'$phase$') ...@@ -384,7 +385,7 @@ plt.plot(timesrd_final, phas, "r", alpha=0.3, lw=3, label=r'$phase$')
plt.plot(timesrd_final, phas5, "blue", alpha=0.3, lw=3, label=r'$phase$') plt.plot(timesrd_final, phas5, "blue", alpha=0.3, lw=3, label=r'$phase$')
# In[78]: # In[98]:
mismatch=1-EasyMatchT(timesrd_final,gwdatanew,gwdatanew5,0,+90) mismatch=1-EasyMatchT(timesrd_final,gwdatanew,gwdatanew5,0,+90)
...@@ -395,10 +396,11 @@ else : ...@@ -395,10 +396,11 @@ else :
error = 1 error = 1
# In[79]: # In[100]:
#Test the new interpolated data #Test the new interpolated data
if error_str:
plt.figure(figsize = (12, 8)) plt.figure(figsize = (12, 8))
plt.plot(timesrd_final, gwdatanew.real, "r", alpha=0.3, lw=2, label='Lev6') plt.plot(timesrd_final, gwdatanew.real, "r", alpha=0.3, lw=2, label='Lev6')
plt.plot(timesrd_final, gwdatanew5.real, "b", alpha=0.3, lw=2, label='Lev5') plt.plot(timesrd_final, gwdatanew5.real, "b", alpha=0.3, lw=2, label='Lev5')
...@@ -406,10 +408,11 @@ plt.plot(timesrd_final, error.real, "b", alpha=0.3, lw=2, label='error') ...@@ -406,10 +408,11 @@ plt.plot(timesrd_final, error.real, "b", alpha=0.3, lw=2, label='error')
plt.legend() plt.legend()
# In[80]: # In[101]:
#Test the error data #Test the error data
if error_str:
plt.figure(figsize = (12, 8)) plt.figure(figsize = (12, 8))
plt.plot(timesrd_final, error.real, "b", alpha=0.3, lw=2, label='error real') plt.plot(timesrd_final, error.real, "b", alpha=0.3, lw=2, label='error real')
plt.plot(timesrd_final, error.imag, "r", alpha=0.3, lw=2, label='error imag') plt.plot(timesrd_final, error.imag, "r", alpha=0.3, lw=2, label='error imag')
...@@ -417,7 +420,7 @@ plt.plot(timesrd_final, np.sqrt(error.imag**2+error.real**2), "r", alpha=0.3, lw ...@@ -417,7 +420,7 @@ plt.plot(timesrd_final, np.sqrt(error.imag**2+error.real**2), "r", alpha=0.3, lw
plt.legend() plt.legend()
# In[81]: # In[103]:
#Take the piece of waveform you want #Take the piece of waveform you want
...@@ -426,10 +429,13 @@ position_end = np.argmax(timesrd_final >= tend) ...@@ -426,10 +429,13 @@ position_end = np.argmax(timesrd_final >= tend)
timesrd_final_tsh = timesrd_final[position_in:position_end] timesrd_final_tsh = timesrd_final[position_in:position_end]
gwdatanew_re_tsh = gwdatanew_re[position_in:position_end] gwdatanew_re_tsh = gwdatanew_re[position_in:position_end]
gwdatanew_im_tsh = gwdatanew_im[position_in:position_end] gwdatanew_im_tsh = gwdatanew_im[position_in:position_end]
if error_str:
error_tsh=error[position_in:position_end] error_tsh=error[position_in:position_end]
else:
error_tsh=1
# In[82]: # In[104]:
#Fitting #Fitting
...@@ -504,13 +510,13 @@ def log_probability(theta): ...@@ -504,13 +510,13 @@ def log_probability(theta):
return lp + log_likelihood(theta) return lp + log_likelihood(theta)
# In[83]: # In[105]:
dict = {'w-tau': model_dv_tau , 'w-q': model_dv_q, 'w-tau-fixed': model_dv} dict = {'w-tau': model_dv_tau , 'w-q': model_dv_q, 'w-tau-fixed': model_dv}
# In[84]: # In[106]:
#I need to provid an initial guess for 4*(nmax+1) the parameters #I need to provid an initial guess for 4*(nmax+1) the parameters
...@@ -524,14 +530,14 @@ vars_ml=soln.x ...@@ -524,14 +530,14 @@ vars_ml=soln.x
print(vars_ml) print(vars_ml)
# In[85]: # In[107]:
f2=dynesty.NestedSampler(log_likelihood, prior_transform, prior_dim, nlive=npoints,sample='rwalk') f2=dynesty.NestedSampler(log_likelihood, prior_transform, prior_dim, nlive=npoints,sample=sampler)
f2.run_nested() f2.run_nested()
# In[87]: # In[108]:
wstr = r'$\omega_' wstr = r'$\omega_'
...@@ -563,7 +569,7 @@ if model=='w-tau-fixed': ...@@ -563,7 +569,7 @@ if model=='w-tau-fixed':
labels = np.concatenate((amp_lab,pha_lab)) labels = np.concatenate((amp_lab,pha_lab))
# In[88]: # In[109]:
if model=='w-tau-fixed': if model=='w-tau-fixed':
...@@ -586,7 +592,7 @@ else: ...@@ -586,7 +592,7 @@ else:
npamps[i] = np.quantile(amps_aux, 0.5) npamps[i] = np.quantile(amps_aux, 0.5)
# In[89]: # In[110]:
res = f2.results res = f2.results
...@@ -595,20 +601,20 @@ res.summary() ...@@ -595,20 +601,20 @@ res.summary()
samps=f2.results.samples samps=f2.results.samples
# In[90]: # In[111]:
evidence = res.logz[-1] evidence = res.logz[-1]
evidence_error = res.logzerr[-1] evidence_error = res.logzerr[-1]
# In[91]: # In[112]:
summary_titles=['n','id','t_shift','dlogz','dlogz_err'] summary_titles=['n','id','t_shift','dlogz','dlogz_err']
# In[92]: # In[113]:
if os.path.exists(sumary_data): if os.path.exists(sumary_data):
...@@ -624,14 +630,14 @@ with open(sumary_data, 'a') as file: ...@@ -624,14 +630,14 @@ with open(sumary_data, 'a') as file:
writer.writerow(outvalues[0]) writer.writerow(outvalues[0])
# In[93]: # In[114]:
samps=f2.results.samples samps=f2.results.samples
samps_tr=np.transpose(samps) samps_tr=np.transpose(samps)
# In[94]: # In[115]:
sigma_vars_m = np.empty(prior_dim) sigma_vars_m = np.empty(prior_dim)
...@@ -644,18 +650,18 @@ for i in range(prior_dim): ...@@ -644,18 +650,18 @@ for i in range(prior_dim):
sigma_vars_p[i] = np.quantile(amps_aux, 0.9) sigma_vars_p[i] = np.quantile(amps_aux, 0.9)
# In[95]: # In[116]:
sigma_vars_all = [sigma_vars,sigma_vars_m,sigma_vars_p] sigma_vars_all = [sigma_vars,sigma_vars_m,sigma_vars_p]
sigma_vars_all=np.stack([sigma_vars,sigma_vars_m,sigma_vars_p], axis=0) sigma_vars_all=np.stack([sigma_vars,sigma_vars_m,sigma_vars_p], axis=0)
# In[96]: # In[117]:
key =['max val','lower bound','higher bound'] key =['max val','lower bound','higher bound']
dfslist = [pd.DataFrame(np.concatenate(([tshift],sigma_vars_all[1])).reshape((-1,prior_dim+1)), columns=np.concatenate((['tshift'],labels)), index = [key[i]]) for i in range(3)] dfslist = [pd.DataFrame(np.concatenate(([tshift],sigma_vars_all[i])).reshape((-1,prior_dim+1)), columns=np.concatenate((['tshift'],labels)), index = [key[i]]) for i in range(3)]
df2 = pd.concat(dfslist) df2 = pd.concat(dfslist)
if os.path.exists(best_data): if os.path.exists(best_data):
df2.to_csv(best_data, mode='a', header=False,index = True) df2.to_csv(best_data, mode='a', header=False,index = True)
...@@ -663,7 +669,7 @@ else: ...@@ -663,7 +669,7 @@ else:
df2.to_csv(best_data,index = True) df2.to_csv(best_data,index = True)
# In[97]: # In[118]:
if model == 'w-q': if model == 'w-q':
...@@ -676,7 +682,7 @@ elif model == 'w-tau-fixed': ...@@ -676,7 +682,7 @@ elif model == 'w-tau-fixed':
truths = npamps truths = npamps
# In[98]: # In[119]:
fg, ax = dyplot.cornerplot(res, color='blue', fg, ax = dyplot.cornerplot(res, color='blue',
...@@ -688,13 +694,13 @@ fg, ax = dyplot.cornerplot(res, color='blue', ...@@ -688,13 +694,13 @@ fg, ax = dyplot.cornerplot(res, color='blue',
) )
# In[99]: # In[121]:
fg.savefig(corner_plot, format = 'png', bbox_inches = 'tight') fg.savefig(corner_plot, format = 'png', bbox_inches = 'tight')
# In[100]: # In[122]:
from dynesty import plotting as dyplot from dynesty import plotting as dyplot
...@@ -704,13 +710,13 @@ fig, axes = dyplot.runplot(res, lnz_truth=lnz_truth) ...@@ -704,13 +710,13 @@ fig, axes = dyplot.runplot(res, lnz_truth=lnz_truth)
fig.tight_layout() fig.tight_layout()
# In[44]: # In[123]:
fig.savefig(diagnosis_plot, format = 'png', dpi = 384, bbox_inches = 'tight') fig.savefig(diagnosis_plot, format = 'png', dpi = 384, bbox_inches = 'tight')
# In[101]: # In[124]:
figband = plt.figure(figsize = (12, 9)) figband = plt.figure(figsize = (12, 9))
...@@ -720,7 +726,7 @@ onesig_bounds = np.array([np.percentile(samps[:, i], [16, 84]) for i in range(le ...@@ -720,7 +726,7 @@ onesig_bounds = np.array([np.percentile(samps[:, i], [16, 84]) for i in range(le
samples_1sigma = filter(lambda sample: np.all(onesig_bounds[0] <= sample) and np.all(sample <= onesig_bounds[1]), samps) samples_1sigma = filter(lambda sample: np.all(onesig_bounds[0] <= sample) and np.all(sample <= onesig_bounds[1]), samps)
samples_1sigma_down = list(samples_1sigma)[::downfactor] samples_1sigma_down = list(samples_1sigma)[::downfactor]
for sample in samples_1sigma_down: for sample in samples_1sigma_down:
plt.plot(timesrd_final_tsh, dict[model](sample).real, "r-", alpha=0.01, lw=3) plt.plot(timesrd_final_tsh, dict[model](sample).real, "r-", alpha=0.04, lw=3)
plt.title(r'Comparison of the MC fit data and the $1-\sigma$ error band') plt.title(r'Comparison of the MC fit data and the $1-\sigma$ error band')
plt.legend() plt.legend()
plt.xlabel('t') plt.xlabel('t')
...@@ -728,7 +734,7 @@ plt.ylabel('h') ...@@ -728,7 +734,7 @@ plt.ylabel('h')
plt.show() plt.show()
# In[102]: # In[125]:
figband.savefig(fit_plot) figband.savefig(fit_plot)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment