-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_from_chain.py
More file actions
201 lines (174 loc) · 9.14 KB
/
plot_from_chain.py
File metadata and controls
201 lines (174 loc) · 9.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
"""
Copyright Eloise Birchall, ANU
eloise.birchall@anu.edu.au
Script to read in the chainfile.pkl files from the MCMC and plot some results"""
from __future__ import print_function, division
import scipy.ndimage as nd
import numpy as np
#import emcee
import matplotlib
#matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import corner
#import radmc3dPy as r3
#import astropy.io.fits as pyfits
import pdb
import os
#from keck_tools import *
import pickle
complete = True
#if the chain did not complete, use join_chain.py to make the chain_file.txt
plot_results = False
plot_corner = False
type = "sym"
im_num = 15.
temp=10000.
#the names of the parameters
names=['d_to_g ','gap_dep_1 ','gap_dep_2 ','r_dust ','r_in ','r_wall ',\
'inclination','pa ','star_x ','star_y ',\
'planet_x ','planet_y ','planet_r ']
latex_names = ["Dust to gas ratio","Inner disc depletion factor","Gap depletion factor",\
"Inner disc radius $(\au)$","Inner radius $(\au)$","Wall radius $(\au)$",\
"Inclination $(\degree)$","Position angle $(\degree)$","Star x $(\au)$",\
"Star y $(\au)$","Planet x $(\au)$","Planet y $(\au)$","Planet Radius $(\rsol)$"]
label=['d_to_g','gap_dep_1','gap_dep_2','r_dust','r_in','r_wall','inclination','pa','star_x','star_y',\
'planet_x','planet_y','planet_r']
#value=[d_to_g,gap_dep_1,gap_dep_2,r_dust,r_in,r_wall,inclination,pa,star_x,star_y,\
# planet_x,planet_y,planet_r]
if complete:
file = open('chainfile.pkl','r')
prob, chain = pickle.load(file)
file.close()
flatprob = np.reshape(prob, 26000)
flatchain = np.reshape(chain, (26000,13))
print("minimum ln prob =", np.max(flatprob))
print("this is a chi-squared of ", np.max(flatprob)/128./128./im_num*2*temp)
#10000 is the temperature, change this to the temperature that was actually used
print("the chain at this point is (with the first 6 ln) ", flatchain[np.argmax(flatprob)])
print("this means the first 6 parameters are actually ", np.exp(flatchain[np.argmax(flatprob)][0:6]))
param_number = np.arange(0,len(flatchain[1]))
flatchain = flatchain
#find the errors for each of the parameters and the appropriate value to quote
with open('uncert_table.txt', 'w') as f:
for i in param_number:
samples = flatchain
if i < 6:
samples[:,i] = np.exp(samples[:,i])
value = map(lambda v: (v[1], v[2]-v[1], v[1]-v[0]),
zip(*np.percentile(samples, [16, 50, 84],
axis=0)))
#print(names[i], ' has value and errors: ', value[i])
#print(' ')
#print('in latex format for a table these parameters are:')
val = value[i]
#print('in latex format for a table these parameters are:')
#print(names[i], ': ', '{0:6.1e}'.format(val[0]),'$^{+','{0:6.1e}'.format(val[1]),'}_{-','{0:6.1e}'.format(val[2]),'}$')
#f.write(str(names[i])+': '+'{0:6.1e}'.format(val[0])+'$^{+'+'{0:6.1e}'.format(val[1])+'}_{-'+'{0:6.1e}'.format(val[2])+'}$\n')
#print('in latex format for a table these parameters are:')
#print(names[i], ': ', '{0:6.1e}'.format(val[0]),'$^{+','{0:6.1e}'.format(val[1]),'}_{-','{0:6.1e}'.format(val[2]),'}$')
#f.write(str(names[i])+': '+'{0:6.1e}'.format(val[0])+'$^{+'+'{0:6.1e}'.format(val[1])+'}_{-'+'{0:6.1e}'.format(val[2])+'}$\n')
#print(names[i], ': ', '{0:.2g}'.format(val[0]),'$^{+','{0:.2g}'.format(val[1]),'}_{-','{0:.2g}'.format(val[2]),'}$')
#f.write(str(names[i])+': '+'{0:.2g}'.format(val[0])+'$^{+'+'{0:.2g}'.format(val[1])+'}_{-'+'{0:.2g}'.format(val[2])+'}$\n')
x = '{0:1.1e}'.format(val[0])
y = float(x.split('e')[1])
if val[0] < 1.:
v = '{0:1.1e}'.format(val[0])
print(names[i], ': ', v.split('e')[0],'$^{+','{0:.2g}'.format(val[1]),'}_{-','{0:.2g}'.format(val[2]),'}$')
f.write(str(names[i])+': '+v.split('e')[0]+'$^{+'+'{0:.2g}'.format(val[1]/(1*10**y))+'}_{-'+'{0:.2g}'.format(val[2]/(1*10**y))+r'}\times 10^'+'{0:1.0f}'.format(y)+'$\n')
elif val[0] > 100.:
v = '{0:.2g}'.format(val[0])
print(names[i], ': ', v.split('e')[0],'$^{+','{0:.2g}'.format(val[1]),'}_{-','{0:.2g}'.format(val[2]),'}$')
f.write(str(names[i])+': '+v.split('e')[0]+'$^{+'+'{0:.2g}'.format(val[1]/(1*10**y))+'}_{-'+'{0:.2g}'.format(val[2]/(1*10**y))+r'}\times 10^'+'{0:1.0f}'.format(y)+'$\n')
else:
print(names[i], ': ', '{0:.2g}'.format(val[0]),'$^{+','{0:.2g}'.format(val[1]),'}_{-','{0:.2g}'.format(val[2]),'}$')
f.write(str(names[i])+': '+'{0:.2g}'.format(val[0])+'$^{+'+'{0:.2g}'.format(val[1]/(1*10**y))+'}_{-'+'{0:.2g}'.format(val[2]/(1*10**y))+'}$\n')
'''
if val[0] < 1.:
#if i in [0,1,2,8,9]:
print(names[i], ': ', '{0:6.3e}'.format(val[0]),'$^{+','{0:6.3e}'.format(val[1]),'}_{-','{0:6.3e}'.format(val[2]),'}$')
f.write(str(names[i])+': '+'{0:6.3e}'.format(val[0])+'$^{+'+'{0:6.3e}'.format(val[1])+'}_{-'+'{0:6.3e}'.format(val[2])+'}$\n')
else:
print(names[i], ': ', '{0:6.3f}'.format(val[0]),'$^{+','{0:6.3f}'.format(val[1]),'}_{-','{0:6.3f}'.format(val[2]),'}$')
f.write(str(names[i])+': '+'{0:6.3f}'.format(val[0])+'$^{+'+'{0:6.3f}'.format(val[1])+'}_{-'+'{0:6.3f}'.format(val[2])+'}$\n')
'''
if plot_results:
os.makedirs("param_vs_param")
#Plot the MC threads
plt.plot(prob.T)
plt.title("MC Threads")
plt.xlabel( 'Iterations')
plt.ylabel('ln probability')
plt.savefig('MCMC_thread.eps')
plt.clf()
#Plot each of the parameters against each other
for i,k in zip(param_number,names):
for j,l in zip(param_number,names):
if i!=j:
if (i in range(6,13)) and (j in range(6,13)):
plt.plot((flatchain[:,i]), (flatchain[:,j]),'.')
elif i in range(6,13):
plt.plot((flatchain[:,i]), np.exp(flatchain[:,j]),'.')
elif j in range(6,13):
plt.plot(np.exp(flatchain[:,i]), (flatchain[:,j]),'.')
else:
plt.plot(np.exp(flatchain[:,i]), np.exp(flatchain[:,j]),'.')
#plt.plot(np.exp(flatchain[:,i]), np.exp(flatchain[:,j]),'.')
title = k + ' vs ' + l
im_name = "param_vs_param/"+k+"_vs_"+l+".eps"
plt.title(title)
plt.xlabel(k)
plt.ylabel(l)
plt.savefig(im_name)
plt.clf()
if plot_corner:
if type == "sym":
samples = samples[:,:8]
elif type == "asym":
samples = samples[:,:10]
else:
#planet case
samples = samples
fig = corner.corner(samples,labels=label)
fig.savefig("corner.eps")
else:
f = np.loadtxt('chain_file.txt')
flatprob = f[:,0]
flatchain = f[:,1:]
param_number = np.arange(0,len(flatchain[1]))
flatchain = flatchain
print("minimum ln prob =", np.max(flatprob))
print("this is a chi-squared of ", np.max(flatprob)/128./128./im_num*2*temp)
#10000 is the temperature, change this to the temperature that was actually used
print("the chain at this point is ", flatchain[np.argmax(flatprob)])
#find the errors for each of the parameters and the appropriate value to quote
for i in param_number:
samples = flatchain
if i < 6:
samples[:,i] = np.exp(samples[:,i])
value = map(lambda v: (v[1], v[2]-v[1], v[1]-v[0]),
zip(*np.percentile(samples, [16, 50, 84],
axis=0)))
print(names[i], ' has value and errors: ', value[i])
if plot_results:
os.makedirs("param_vs_param")
#Plot each of the parameters against each other
for i,k in zip(param_number,names):
for j,l in zip(param_number,names):
if i!=j:
if (i in range(6,13)) and (j in range(6,13)):
plt.plot((flatchain[:,i]), (flatchain[:,j]),'.')
elif i in range(6,13):
plt.plot((flatchain[:,i]), np.exp(flatchain[:,j]),'.')
elif j in range(6,13):
plt.plot(np.exp(flatchain[:,i]), (flatchain[:,j]),'.')
else:
plt.plot(np.exp(flatchain[:,i]), np.exp(flatchain[:,j]),'.')
#plt.plot(np.exp(flatchain[:,i]), np.exp(flatchain[:,j]),'.')
title = k + ' vs ' + l
im_name = "param_vs_param/"+k+"_vs_"+l+".eps"
plt.title(title)
plt.xlabel(k)
plt.ylabel(l)
plt.savefig(im_name)
plt.clf()