forked from mmaus96/Lens_Modeling_Auto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
initial_fits_long.py
executable file
·352 lines (255 loc) · 13.5 KB
/
initial_fits_long.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
from Lens_Modeling_Auto.auto_modeling_functions import prepareFit
from Lens_Modeling_Auto.auto_modeling_functions import runFit
from lenstronomy.Workflow.fitting_sequence import FittingSequence
from Lens_Modeling_Auto.auto_modeling_functions import find_components
from Lens_Modeling_Auto.auto_modeling_functions import mask_for_sat
from Lens_Modeling_Auto.auto_modeling_functions import mask_for_lens_gal
####################### Initial Params #######################
lens_light_sersic_fixed = {}
lens_light_sersic_init = {'R_sersic': 1.0, 'n_sersic': 2.,'e1': 0., 'e2': 0., 'center_x': 0., 'center_y': 0}
lens_light_sersic_sigma = {'R_sersic': 0.1, 'n_sersic': 1.0,'e1': 0.5, 'e2': 0.5, 'center_x': 0.01, 'center_y': 0.01}
lens_light_sersic_lower = {'R_sersic': 0.001, 'n_sersic': 1.0,'e1': -0.5, 'e2': -0.5, 'center_x': -1.5, 'center_y': -1.5}
lens_light_sersic_upper = {'R_sersic': 5., 'n_sersic': 10.,'e1': 0.5, 'e2': 0.5, 'center_x': 1.5, 'center_y': 1.5}
source_sersic_fixed = {}
source_sersic_init = {'R_sersic': 1.0, 'n_sersic': 1.,'e1': 0., 'e2': 0., 'center_x': 0., 'center_y': 0}
source_sersic_sigma = {'R_sersic': 0.1, 'n_sersic': 0.5,'e1': 0.5, 'e2': 0.5, 'center_x': 0.01, 'center_y': 0.01}
source_sersic_lower = {'R_sersic': 0.001, 'n_sersic': 0.1,'e1': -0.5, 'e2': -0.5, 'center_x': -1.5, 'center_y': -1.5}
source_sersic_upper = {'R_sersic': 5., 'n_sersic': 10.,'e1': 0.5, 'e2': 0.5, 'center_x': 1.5, 'center_y': 1.5}
lens_sie_fixed = {}
lens_sie_init = {'theta_E': 1.5, 'e1': 0., 'e2': 0., 'center_x': 0., 'center_y': 0.}
lens_sie_sigma = {'theta_E': .3, 'e1': 0.5, 'e2': 0.5, 'center_x': 0.1, 'center_y': 0.1}
lens_sie_lower = {'theta_E': 0.1, 'e1': -1, 'e2': -1, 'center_x': -1.5, 'center_y': -1.5}
lens_sie_upper = {'theta_E': 5.0, 'e1': 1, 'e2': 1, 'center_x': 1.5, 'center_y': 1.5}
lens_shear_fixed = {'ra_0': 0, 'dec_0': 0}
lens_shear_init = {'ra_0': 0, 'dec_0': 0,'gamma1': 0., 'gamma2': 0.0}
lens_shear_sigma = {'gamma1': 0.1, 'gamma2': 0.1}
lens_shear_lower = {'gamma1': -0.5, 'gamma2': -0.5}
lens_shear_upper = {'gamma1': 0.5, 'gamma2': 0.5}
if includeShear == True:
lens_initial_params = deepcopy([[lens_sie_init,lens_shear_init],
[lens_sie_sigma,lens_shear_sigma],
[lens_sie_fixed,lens_shear_fixed],
[lens_sie_lower,lens_shear_lower],
[lens_sie_upper,lens_shear_upper]])
else:
lens_initial_params = deepcopy([[lens_sie_init],
[lens_sie_sigma],
[lens_sie_fixed],
[lens_sie_lower],
[lens_sie_upper]])
lens_light_initial_params = deepcopy([[lens_light_sersic_init],
[lens_light_sersic_sigma],
[lens_light_sersic_fixed],
[lens_light_sersic_lower],
[lens_light_sersic_upper]])
source_initial_params = deepcopy([[source_sersic_init],
[source_sersic_sigma],
[source_sersic_fixed],
[source_sersic_lower],
[source_sersic_upper]])
########################################## Model Lens Light ##########################################
print('I will first model lens light with a SERSIC_ELLIPSE profile')
print('------------------------------------------------------------------------------')
#Model Lists
lens_model_list = []
source_model_list = []
lens_light_model_list = ['SERSIC_ELLIPSE']
gal_mask_list = []
mask_list = []
for data in kwargs_data:
gal_mask_list.append(mask_for_lens_gal(data['image_data'],deltaPix))
if use_mask:
mask_list.append(mask_for_sat(data['image_data'],deltaPix))
else: mask_list = None
#prepare fitting kwargs
kwargs_likelihood, kwargs_model, kwargs_data_joint, multi_band_list,kwargs_constraints = prepareFit(kwargs_data, kwargs_psf,
lens_model_list, source_model_list,
lens_light_model_list,
image_mask_list = gal_mask_list)
###prepare kwarg_params
lens_light_params = [[],[],[],[],[]]
for j,f in enumerate(lens_light_params):
for i in range(len(kwargs_data)):
f.extend(deepcopy(lens_light_initial_params[j]))
source_params = [[],[],[],[],[]]
lens_params = [[],[],[],[],[]]
kwargs_params = {'lens_model': deepcopy(lens_params),
'source_model': deepcopy(source_params),
'lens_light_model': deepcopy(lens_light_params)}
kwargs_fixed = {'kwargs_lens': deepcopy(lens_params[2]),
'kwargs_source': deepcopy(source_params[2]),
'kwargs_lens_light': deepcopy(lens_light_params[2])}
print('The lens, source, and lens light modeling parameters are')
print('lens model: ', kwargs_params['lens_model'])
print('\n')
print('source model: ', kwargs_params['source_model'])
print('\n')
print('lens light model: ', kwargs_params['lens_light_model'])
print('\n')
print('-------------------------------------------------------------------')
print('\n')
print('I will now begin the PSO:')
# fitting_kwargs_list = [['PSO', {'sigma_scale': 1., 'n_particles': 50, 'n_iterations': 100,'threadCount': 1}]]
chain_list, kwargs_result = runFit(fitting_kwargs_list, kwargs_params,
kwargs_likelihood, kwargs_model,
kwargs_data_joint, kwargs_constraints = kwargs_constraints)
print('\n')
print('##########################################################################')
print('\n')
########################################## Model Lens and Source ##########################################
print('I will now include the source and lens profiles')
print('\n')
print('-------------------------------------------------------------------')
print('\n')
#Model Lists
if includeShear == True:
lens_model_list = ['SIE','SHEAR']
else:
lens_model_list = ['SIE']
source_model_list = ['SERSIC_ELLIPSE']
lens_light_model_list = ['SERSIC_ELLIPSE']
#prepare fitting kwargs
kwargs_likelihood, kwargs_model, kwargs_data_joint, multi_band_list,kwargs_constraints = prepareFit(kwargs_data, kwargs_psf,
lens_model_list, source_model_list,
lens_light_model_list,
image_mask_list = mask_list)
#prepare kwarg_params
for l,x in enumerate(source_params):
for i in range(len(kwargs_data)):
x.extend(deepcopy(source_initial_params[l]))
lens_light_params[0] = deepcopy(kwargs_result['kwargs_lens_light'])
lens_params = deepcopy(lens_initial_params)
# lens_initial_params = deepcopy(lens_params)
kwargs_params = {'lens_model': deepcopy(lens_params),
'source_model': deepcopy(source_params),
'lens_light_model': deepcopy(lens_light_params)}
kwargs_fixed = {'kwargs_lens': deepcopy(lens_params[2]),
'kwargs_source': deepcopy(source_params[2]),
'kwargs_lens_light': deepcopy(lens_light_params[2])}
print('The lens, source, and lens light modeling parameters are')
print('lens model: ', kwargs_params['lens_model'])
print('\n')
print('source model: ', kwargs_params['source_model'])
print('\n')
print('lens light model: ', kwargs_params['lens_light_model'])
print('\n')
print('-------------------------------------------------------------------')
print('\n')
print('I will now begin the PSO:')
chain_list, kwargs_result = runFit(fitting_kwargs_list, kwargs_params,
kwargs_likelihood, kwargs_model,
kwargs_data_joint, kwargs_constraints = kwargs_constraints)
print('\n')
print('##########################################################################')
print('\n')
lens_light_params[0] = deepcopy(kwargs_result['kwargs_lens_light'])
source_params[0] = deepcopy(kwargs_result['kwargs_source'])
lens_params[0] = deepcopy(kwargs_result['kwargs_lens'])
########################################## Model Lens Light Again ##########################################
print('I will now model lens light again')
print('------------------------------------------------------------------------------')
#Model Lists
lens_model_list = []
source_model_list = []
lens_light_model_list = ['SERSIC_ELLIPSE']
kwargs_params = {'lens_model': [[],[],[],[],[]],
'source_model': [[],[],[],[],[]],
'lens_light_model': deepcopy(lens_light_params)}
kwargs_likelihood, kwargs_model, kwargs_data_joint, multi_band_list,kwargs_constraints = prepareFit(kwargs_data, kwargs_psf,
lens_model_list, source_model_list,
lens_light_model_list,
image_mask_list = gal_mask_list)
print('The lens, source, and lens light modeling parameters are')
print('lens model: ', kwargs_params['lens_model'])
print('\n')
print('source model: ', kwargs_params['source_model'])
print('\n')
print('lens light model: ', kwargs_params['lens_light_model'])
print('\n')
print('-------------------------------------------------------------------')
print('\n')
print('I will now begin the PSO:')
chain_list, kwargs_result = runFit(fitting_kwargs_list, kwargs_params,
kwargs_likelihood, kwargs_model,
kwargs_data_joint, kwargs_constraints = kwargs_constraints)
lens_light_params[0] = deepcopy(kwargs_result['kwargs_lens_light'])
########################################## Model Lens and Source Again ##########################################
print('I will now include the source and lens profiles')
print('\n')
print('-------------------------------------------------------------------')
print('\n')
#Model Lists
if includeShear == True:
lens_model_list = ['SIE','SHEAR']
else:
lens_model_list = ['SIE']
source_model_list = ['SERSIC_ELLIPSE']
lens_light_model_list = ['SERSIC_ELLIPSE']
kwargs_params = {'lens_model': deepcopy(lens_params),
'source_model': deepcopy(source_params),
'lens_light_model': deepcopy(lens_light_params)}
#prepare fitting kwargs
kwargs_likelihood, kwargs_model, kwargs_data_joint, multi_band_list,kwargs_constraints = prepareFit(kwargs_data, kwargs_psf,
lens_model_list, source_model_list,
lens_light_model_list,
image_mask_list = mask_list)
print('The lens, source, and lens light modeling parameters are')
print('lens model: ', kwargs_params['lens_model'])
print('\n')
print('source model: ', kwargs_params['source_model'])
print('\n')
print('lens light model: ', kwargs_params['lens_light_model'])
print('\n')
print('-------------------------------------------------------------------')
print('\n')
print('I will now begin the PSO:')
chain_list, kwargs_result = runFit(fitting_kwargs_list, kwargs_params,
kwargs_likelihood, kwargs_model,
kwargs_data_joint, kwargs_constraints = kwargs_constraints)
kwargs_fixed = {'kwargs_lens': deepcopy(lens_params[2]),
'kwargs_source': deepcopy(source_params[2]),
'kwargs_lens_light': deepcopy(lens_light_params[2])}
multi_source_model_list = []
multi_lens_light_model_list = []
for i in range(len(kwargs_data)):
multi_source_model_list.extend(deepcopy(source_model_list))
multi_lens_light_model_list.extend(deepcopy(lens_light_model_list))
################## Write Initial Params to text file ##################
file = open(results_path+"/initial_params.txt","a")#append mode
file.write("Model lists: \n")
file.write("lens model: " + str(lens_model_list) + " \n")
file.write("source model: " + str(multi_source_model_list) + " \n")
file.write("lens light model: "+ str(multi_lens_light_model_list) + " \n")
file.write("\n")
file.write("\n")
file.write("kwargs_lens (init,sigma,fixed,lower,upper): \n")
for l in lens_initial_params:
file.write(str(l)+ " \n")
file.write("\n")
file.write("kwargs_source (init,sigma,fixed,lower,upper): \n")
for s in source_initial_params:
file.write(str(s) + " \n")
file.write("\n")
file.write("kwargs_lens_light (init,sigma,fixed,lower,upper): \n")
for l in lens_light_initial_params:
file.writelines(str(l)+ " \n")
file.write("\n")
file.write("\n")
file.write("kwargs_model: \n")
print(kwargs_model, file = file)
file.write("\n")
file.write("\n")
file.write("\n")
file.write("kwargs_constraints: \n")
print(kwargs_constraints, file = file)
file.write("\n")
file.write("\n")
file.write("\n")
file.write("kwargs_likelihood: \n")
print(kwargs_likelihood, file = file)
file.write("\n")
file.write("\n")
file.write('\n')
file.write('\n'+'Custom_LogL_Priors: ' + str([[0,'q',0.8,0.1]]) + ' \n')
file.write('\n')
file.close()