-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_tde_fits.py
198 lines (176 loc) · 8.3 KB
/
plot_tde_fits.py
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
import bagpipes as pipes
import numpy as np
import matplotlib.pyplot as plt
import sys
from pipes_utils import *
# from matplotlib import rcParams
# rcParams.update({'figure.autolayout': True})
redshifts = [0.0206, 0.0484, 0.06, 0.026211, 0.022, 0.0512, 0.01513, 0.018]
datafiles = ["ASASSN14li","ASASSN15oi","AT2018fyk","AT2019ahk","AT2019azh","AT2019dsg", "AT2019qiz", "iPTF16fnl"]
exponential1 = {}
exponential1["age"] = (7.5, 12.5) # Gyr
exponential1["tau"] = (0.5, 2.0) # Gyr
exponential1["massformed"] = (5.0, 12.5) # log_10(M*/M_solar)
exponential1["metallicity"] = (0.0, 2.5) # Z/Z_oldsolar
exponential2 = {}
exponential2["age"] = (0.0, 3.5) # Gyr, lifetime of F type stars
exponential2["tau"] = (0.1, 2.0) # Gyr
exponential2["massformed"] = (0.0, 12.5) # log_10(M*/M_solar)
exponential2["metallicity"] = (0.0, 2.5) # Z/Z_oldsolar
dblplaw = {}
dblplaw["tau"] = (0.0, 4.5) # Do not let the peak occur beyond ~6.5 Gyr
dblplaw["alpha"] = (5.0, 10.0) # Formation must begin and end reasonably fast
dblplaw["beta"] = (5.0, 10.0) # Formation must begin and end reasonably fast
dblplaw["massformed"] = (5.0, 12.5)
dblplaw["metallicity"] = (0.0, 2.5)
delayed = {} # Delayed Tau model t*e^-(t/tau)
delayed["age"] = (7.5, 12.5) # Time since SF began: Gyr
delayed["tau"] = (0.1, 2.0) # Timescale of decrease: Gyr
delayed["massformed"] = (5.0, 12.5)
delayed["metallicity"] = (0.0, 2.5)
lognormal = {}
lognormal["tmax"] = (1.0, 6.0 )
lognormal["fwhm"] = (0.1, 3.0)
lognormal["massformed"] = (5.0, 12.5)
lognormal["metallicity"] = (0.0, 2.5)
dust = {}
dust["type"] = "Calzetti"
dust["Av"] = (0.0, 2.0)
nebular = {}
nebular["logU"] = (-4.0,-2.0)
chi_squ_vals = {}
for redshift, filename in zip(redshifts, datafiles):
# Get the galaxy object and a priori model compenents dictionary.
galaxy = pipes.galaxy(filename, load_xshooter, photometry_exists=False)
# Calculate redshift constraints.
z_low, z_high = redshift - 0.001, redshift + 0.001
try:
f = open("pipes/posterior/exponential_burst_r4/"+filename+".h5")
f.close()
fit_instructions = {
"redshift" : (z_low, z_high), # z varies tight_layout around z_obs.
"t_bc" : (0.013, 0.021), # Constraints from Murray 2011.
"veldisp" : (50.0, 450.0), # Constrained by Faber-Jackson. TODO: Lookup Minkowski 1962!
"exponential1" : exponential1, # Add the exp SFH component.
"exponential2" : exponential2, # Add the burst component.
"dust" : dust,
"nebular" : nebular
}
# Do a fit with both an old exponential component and recent burst.
fit = pipes.fit(galaxy, fit_instructions, run="exponential_burst_r4")
fit.fit(verbose=False)
# Create a dictionary for storying posterior sample distribution widths.
chi_squ_vals["exponential_burst_r4"] = chi_squared(galaxy, fit)
# Select the fit with lowest chi-ssquared value and plot it.
plt.tight_layout()
fig = fit.plot_sfh_posterior(save=True, show=False)
except IOError:
print("Run not exponential_burst_r4 not accessible for "+filename)
try:
f = open("pipes/posterior/dblplaw_burst_r4/"+filename+".h5")
f.close()
fit_instructions = {
"redshift" : (z_low, z_high), # z varies tight_layout around z_obs.
"t_bc" : (0.013, 0.021), # Constraints from Murray 2011.
"veldisp" : (50.0, 450.0), # Constrained by Faber-Jackson. TODO: Lookup Minkowski 1962!
"dblplaw" : dblplaw, # Add the dblplaw SFH component.
"exponential2" : exponential2, # Add the burst component.
"dust" : dust,
"nebular" : nebular
}
# Do a fit with both an old delayed component and recent burst.
fit = pipes.fit(galaxy, fit_instructions, run="dblplaw_burst_r4")
fit.fit(verbose=False)
# Create a dictionary for storying posterior sample distribution widths.
chi_squ_vals["dblplaw_burst_r4"] = chi_squared(galaxy, fit)
# Select the fit with lowest chi-squared value and plot it.
plt.tight_layout()
fig = fit.plot_sfh_posterior(save=True, show=False)
except IOError:
print("Run not dblplaw_burst_r4 not accessible for "+filename)
try:
f = open("pipes/posterior/delayed_burst_r4/"+filename+".h5")
f.close()
fit_instructions = {
"redshift" : (z_low, z_high), # z varies tight_layout around z_obs.
"t_bc" : (0.013, 0.021), # Constraints from Murray 2011.
"veldisp" : (50.0, 450.0), # Constrained by Faber-Jackson. TODO: Lookup Minkowski 1962!
"delayed" : delayed, # Add the delayed SFH component.
"exponential2" : exponential2, # Add the burst component.
"dust" : dust,
"nebular" : nebular
}
# Do a fit with both an old delayed component and recent burst.
fit = pipes.fit(galaxy, fit_instructions, run="delayed_burst_r4")
fit.fit(verbose=False)
# Create a dictionary for storying posterior sample distribution widths.
chi_squ_vals["delayed_burst_r4"] = chi_squared(galaxy, fit)
# Select the fit with lowest chi-squared value and plot it.
plt.tight_layout()
fig = fit.plot_sfh_posterior(save=True, show=False)
except IOError:
print("Run not delayed_burst_r4 not accessible for "+filename)
try:
f = open("pipes/posterior/lognormal_burst_r4/"+filename+".h5")
f.close()
fit_instructions = {
"redshift" : (z_low, z_high), # z varies tight_layout around z_obs.
"t_bc" : (0.013, 0.021), # Constraints from Murray 2011.
"veldisp" : (50.0, 450.0), # Constrained by Faber-Jackson. TODO: Lookup Minkowski 1962!
"lognormal" : lognormal, # Add the lognormal SFH component.
"exponential2" : exponential2, # Add the burst component.
"dust" : dust,
"nebular" : nebular
}
# Do a fit with both an old delayed component and recent burst.
fit = pipes.fit(galaxy, fit_instructions, run="lognormal_burst_r4")
fit.fit(verbose=False)
# Create a dictionary for storying posterior sample distribution widths.
chi_squ_vals["lognormal_burst_r4"] = chi_squared(galaxy, fit)
# Select the fit with lowest chi-squared value and plot it.
plt.tight_layout()
fig = fit.plot_sfh_posterior(save=True, show=False)
except IOError:
print("Run not lognormal_burst_r4 not accessible for "+filename)
# # Reload all the saved fits.
# fit = pipes.fit(galaxy, fit_instructions, run="exponential_burst_r4")
# fit.fit(verbose=False)
# chi_squ_vals = {"exponential_burst_r4" : chi_squared(galaxy, fit)}
#
# fit = pipes.fit(galaxy, fit_instructions, run="dblplaw_burst_r4")
# fit.fit(verbose=True)
# chi_squ_vals["dblplaw_burst_r4"] = chi_squared(galaxy, fit)
#
# fit = pipes.fit(galaxy, fit_instructions, run="delayed_burst_r4")
# fit.fit(verbose=False)
# chi_squ_vals["delayed_burst_r4"] = chi_squared(galaxy, fit)
#
# fit = pipes.fit(galaxy, fit_instructions, run="lognormal_burst_r4")
# fit.fit(verbose=False)
# chi_squ_vals["lognormal_burst_r4"] = chi_squared(galaxy, fit)
# Get the functional form with the lowest chi-squared value.
best_func = min(chi_squ_vals, key=lambda k: chi_squ_vals[k])
# Set the fit instructions dictionay to contain the correct priors.
# try:
# fit_instructions.pop("")
# except KeyError:
# pass
# try:
# fit_instructions.pop("")
# except KeyError:
# pass
# try:
# fit_instructions.pop("")
# except KeyError:
# pass
# try:
# fit_instructions.pop("")
# except KeyError:
# pass
# Select the fit with lowest chi-squared value and plot it.
fit = pipes.fit(galaxy, fit_instructions, run=best_func)
plt.tight_layout()
fig = fit.plot_sfh_posterior(save=False, show=True)
print ('parameter median 16th percentile 84th percentile')
for key in fit.posterior.samples.keys():
print(key+": ", np.median(fit.posterior.samples[key]), np.percentile(fit.posterior.samples[key], 16), np.percentile(fit.posterior.samples[key], 84))