forked from johanmazoyer/debrisdisk_mcmc_fit_and_plot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCMC_error_bar_spf_clean.py
619 lines (474 loc) · 21.6 KB
/
MCMC_error_bar_spf_clean.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
#pylint: disable=C0103
""" author: J MAZOYER
A code that tahke the chain return of the MCMCs and plot the SPFs for hr 4796
"""
import os
import csv
import numpy as np
import matplotlib.pyplot as plot
import yaml
from scipy import optimize
from emcee import backends
from disk_models import hg_1g, hg_2g, hg_3g, log_hg_2g, log_hg_3g
def measure_spf_errors(yaml_file_str, Number_rand_mcmc, Norm_90_inplot=1., save = False):
"""
take a set of scatt angles and a set of HG parameter and return
the log of a 2g HG SPF (usefull to fit from a set of points)
Args:
yaml_file_str: name of the yaml_ parameter file
Number_rand_mcmc: number of randomnly selected psf we use to
plot the error bars
Norm_90_inplot: the value at which you want to normalize the spf
in the plot ay 90 degree (to re-measure the error
bars properly)
Returns:
a dic that contains the 'best_spf', 'errorbar_sup',
'errorbar_sup', 'errorbar'
"""
with open(os.path.join('initialization_files', yaml_file_str + '.yaml'),
'r') as yaml_file:
params_mcmc_yaml = yaml.load(yaml_file, Loader=yaml.FullLoader)
dico_return = dict()
nwalkers = params_mcmc_yaml['NWALKERS']
n_dim_mcmc = params_mcmc_yaml['N_DIM_MCMC']
datadir = os.path.join(basedir, params_mcmc_yaml['BAND_DIR'])
mcmcresultdir = os.path.join(datadir, 'results_MCMC')
file_prefix = params_mcmc_yaml['FILE_PREFIX']
SPF_MODEL = params_mcmc_yaml['SPF_MODEL'] #Type of description for the SPF
name_h5 = file_prefix + "_backend_file_mcmc"
chain_name = os.path.join(mcmcresultdir, name_h5 + ".h5")
reader = backends.HDFBackend(chain_name)
#we only exctract the last itearations, assuming it converged
chain_flat = reader.get_chain(discard=0, flat=True)
burnin = np.clip(reader.iteration - 10*Number_rand_mcmc//nwalkers,0,None)
chain_flat = reader.get_chain(discard=burnin, flat=True)
if (SPF_MODEL == 'hg_1g'):
norm_chain = np.exp(chain_flat[:, 7])
g1_chain = chain_flat[:, 8]
bestmodel_Norm = np.percentile(norm_chain, 50)
bestmodel_g1 = np.percentile(g1_chain, 50)
Normalization = Norm_90_inplot
if save == True:
Normalization = bestmodel_Norm
best_hg_mcmc = hg_1g(scattered_angles, bestmodel_g1, Normalization)
elif SPF_MODEL == 'hg_2g':
norm_chain = np.exp(chain_flat[:, 7])
g1_chain = chain_flat[:, 8]
g2_chain = chain_flat[:, 9]
alph1_chain = chain_flat[:, 10]
bestmodel_Norm = np.percentile(norm_chain, 50)
bestmodel_g1 = np.percentile(g1_chain, 50)
bestmodel_g2 = np.percentile(g2_chain, 50)
bestmodel_alpha1 = np.percentile(alph1_chain, 50)
Normalization = Norm_90_inplot
if save == True:
Normalization = bestmodel_Norm
best_hg_mcmc = hg_2g(scattered_angles, bestmodel_g1, bestmodel_g2,
bestmodel_alpha1, Normalization)
elif SPF_MODEL == 'hg_3g':
# temporary, the 3g is not finish so we remove some of the
# chains that are obvisouly bad. When 3g is finally converged,
# we removed that
# incl_chain = np.degrees(np.arccos(chain_flat[:, 3]))
# where_incl_is_ok = np.where(incl_chain > 76)
# norm_chain = np.exp(chain_flat[where_incl_is_ok, 7]).flatten()
# g1_chain = chain_flat[where_incl_is_ok, 8].flatten()
# g2_chain = chain_flat[where_incl_is_ok, 9].flatten()
# alph1_chain = chain_flat[where_incl_is_ok, 10].flatten()
# g3_chain = chain_flat[where_incl_is_ok, 11].flatten()
# alph2_chain = chain_flat[where_incl_is_ok, 12].flatten()
# log_prob_samples_flat = reader.get_log_prob(discard=burnin,
# flat=True)
# log_prob_samples_flat = log_prob_samples_flat[where_incl_is_ok]
# wheremin = np.where(
# log_prob_samples_flat == np.max(log_prob_samples_flat))
# wheremin0 = np.array(wheremin).flatten()[0]
# bestmodel_g1 = g1_chain[wheremin0]
# bestmodel_g2 = g2_chain[wheremin0]
# bestmodel_g3 = g3_chain[wheremin0]
# bestmodel_alpha1 = alph1_chain[wheremin0]
# bestmodel_alpha2 = alph2_chain[wheremin0]
# bestmodel_Norm = norm_chain[wheremin0]
norm_chain = np.exp(chain_flat[:, 7])
g1_chain = chain_flat[:, 8]
g2_chain = chain_flat[:, 9]
alph1_chain = chain_flat[:, 10]
g3_chain = chain_flat[:, 11]
alph2_chain = chain_flat[:, 12]
bestmodel_Norm = np.percentile(norm_chain, 50)
bestmodel_g1 = np.percentile(g1_chain, 50)
bestmodel_g2 = np.percentile(g2_chain, 50)
bestmodel_alpha1 = np.percentile(alph1_chain, 50)
bestmodel_g3 = np.percentile(g3_chain, 50)
bestmodel_alpha2 = np.percentile(alph2_chain, 50)
Normalization = Norm_90_inplot
# we normalize the best model at 90 either by the value found
# by the MCMC if we want to save or by the value in the
# Norm_90_inplot if we want to plot
Normalization = Norm_90_inplot
if save == True:
Normalization = bestmodel_Norm
best_hg_mcmc = hg_3g(scattered_angles, bestmodel_g1, bestmodel_g2,
bestmodel_g3, bestmodel_alpha1, bestmodel_alpha2,
Normalization)
dico_return['best_spf'] = best_hg_mcmc
random_param_number = np.random.randint(1,
len(g1_chain) - 1,
Number_rand_mcmc)
if (SPF_MODEL == 'hg_1g') or (SPF_MODEL == 'hg_2g') or (
SPF_MODEL == 'hg_3g'):
g1_rand = g1_chain[random_param_number]
norm_rand = norm_chain[random_param_number]
if (SPF_MODEL == 'hg_2g') or (SPF_MODEL == 'hg_3g'):
g2_rand = g2_chain[random_param_number]
alph1_rand = alph1_chain[random_param_number]
if SPF_MODEL == 'hg_3g':
g3_rand = g3_chain[random_param_number]
alph2_rand = alph2_chain[random_param_number]
hg_mcmc_rand = np.zeros((len(best_hg_mcmc), len(random_param_number)))
errorbar_sup = scattered_angles * 0.
errorbar_inf = scattered_angles * 0.
errorbar = scattered_angles * 0.
for num_model in range(Number_rand_mcmc):
norm_here = norm_rand[num_model]
# we normalize the random SPF at 90 either by the value of
# the SPF by the MCMC if we want to save or around the
# Norm_90_inplot if we want to plot
Normalization = norm_here * Norm_90_inplot / bestmodel_Norm
if save == True:
Normalization = norm_here
if (SPF_MODEL == 'hg_1g'):
g1_here = g1_rand[num_model]
if (SPF_MODEL == 'hg_2g'):
g1_here = g1_rand[num_model]
g2_here = g2_rand[num_model]
alph1_here = alph1_rand[num_model]
hg_mcmc_rand[:, num_model] = hg_2g(
scattered_angles, g1_here, g2_here, alph1_here, Normalization)
if SPF_MODEL == 'hg_3g':
g3_here = g3_rand[num_model]
alph2_here = alph2_rand[num_model]
hg_mcmc_rand[:, num_model] = hg_3g(
scattered_angles, g1_here, g2_here, g3_here, alph1_here,
alph2_here, Normalization)
for anglei in range(len(scattered_angles)):
errorbar_sup[anglei] = np.max(hg_mcmc_rand[anglei, :])
errorbar_inf[anglei] = np.min(hg_mcmc_rand[anglei, :])
errorbar[anglei] = (np.max(hg_mcmc_rand[anglei, :]) -
np.min(hg_mcmc_rand[anglei, :])) / 2.
dico_return['errorbar_sup'] = errorbar_sup
dico_return['errorbar_inf'] = errorbar_inf
dico_return['errorbar'] = errorbar
if save == True:
savefortext = np.transpose([scattered_angles, best_hg_mcmc, errorbar_sup, errorbar_inf])
path_and_name_txt = os.path.join(folder_save_pdf, file_prefix+'_spf.txt')
np.savetxt( path_and_name_txt, savefortext, delimiter=',', fmt = '%10.2f') # save the array in a txt
return dico_return
if __name__ == '__main__':
basedir = os.environ["EXCHANGE_PATH"] # the base directory where is
# your data (using OS environnement variable allow to use same code on
# different computer without changing this).
# basedir = '/Users/jmazoyer/Dropbox/ExchangeFolder/data_python/Aurora/'
min_scat = 13.3
max_scat = 166.7
nb_random_models = 1000
folder_save_pdf = os.path.join(basedir, 'Spf_plots_produced')
scattered_angles = np.arange(np.round(max_scat - min_scat)) + np.round(
np.min(min_scat))
# ###########################################################################
# ### SPF injected real value
# ###########################################################################
g1_injected = 0.825
g2_injected = -0.201
alph1_injected = 0.298
injected_hg = hg_2g(scattered_angles,g1_injected,g2_injected,alph1_injected, 1.0)
# ###########################################################################
# ### SPHERE H2 exctracted by Milli et al. 2017
# ###########################################################################
# sphere spf extracted by Milli et al. 2017
angles_sphere_extractJulien = np.zeros(49)
spf_shpere_extractJulien = np.zeros(49)
errors_sphere_extractJulien = np.zeros(49)
i = 0
with open(os.path.join(basedir, 'SPHERE_Hdata', 'SPHERE_extraction_Milli.csv'),
'rt') as f:
readercsv = csv.reader(f)
for row in readercsv:
angles_sphere_extractJulien[i] = float(row[0])
spf_shpere_extractJulien[i] = float(row[1])
errors_sphere_extractJulien[i] = float(row[2])
i += 1
# fit a 3g function to sphere spf extracted by Milli et al. 2017
log_spf_sphere = np.log(spf_shpere_extractJulien)
log_errors_sphere = np.log(spf_shpere_extractJulien +
errors_sphere_extractJulien) - np.log(
spf_shpere_extractJulien)
initial_guess = [
0.99997858, 0.05095741, 0.04765255, 0.99946266, -0.03396517, 0.94720999
]
params = initial_guess
a = optimize.curve_fit(log_hg_3g,
angles_sphere_extractJulien,
log_spf_sphere,
initial_guess,
log_errors_sphere,
maxfev=10000,
bounds=([-1, -1., -1., -1., -1.,
-1.], [1, 1., 1., 1., 1., 4.]))
params_3g_fir_from_extraction = a[0]
hg3g_fitted_Milliextraction = hg_3g(
scattered_angles, params_3g_fir_from_extraction[0],
params_3g_fir_from_extraction[1], params_3g_fir_from_extraction[2],
params_3g_fir_from_extraction[3], params_3g_fir_from_extraction[4],
params_3g_fir_from_extraction[5])
# ##############################
# # GPI K1 extracted by Pauline from Pol
# ##############################
dir_exctract_Pauline = os.path.join(basedir, '150403_K1_Spec',
'Pauline_SPF_from_Pol')
data_exctract_Pauline = os.path.join(dir_exctract_Pauline,
'Pauline_SPF_ktot_NE_errs.txt')
text_array = np.loadtxt(data_exctract_Pauline)
angles_GPIPolK1_extractPauline = text_array[:, 0]
spf_GPIPolK1_extractPaulineNE = text_array[:, 1]
errors_GPIPolK1_extractPaulineNE = text_array[:, 2]
data_exctract_Pauline = os.path.join(dir_exctract_Pauline,
'Pauline_SPF_ktot_SW_errs.txt')
text_array = np.loadtxt(data_exctract_Pauline)
# angles_GPIPolK1_extractPaulineSW = text_array[:, 0] ## identical to NE
spf_GPIPolK1_extractPaulineSW = text_array[:, 1]
errors_GPIPolK1_extractPaulineSW = text_array[:, 2]
spf_GPIPolK1_extractPauline = 0.5 * (spf_GPIPolK1_extractPaulineSW +
spf_GPIPolK1_extractPaulineNE)
error_GPIPolK1_extractPauline = np.sqrt(errors_GPIPolK1_extractPaulineSW**2 +
errors_GPIPolK1_extractPaulineNE**2)
error_GPIPolK1_extractPauline = error_GPIPolK1_extractPauline / spf_GPIPolK1_extractPauline[
np.where(angles_GPIPolK1_extractPauline == 90)]
spf_GPIPolK1_extractPauline = spf_GPIPolK1_extractPauline / spf_GPIPolK1_extractPauline[
np.where(angles_GPIPolK1_extractPauline == 90)]
index = (np.arange((error_GPIPolK1_extractPauline.shape[0]) / 2)) * 2 + 1
angles_GPIPolK1_extractPauline = np.delete(angles_GPIPolK1_extractPauline,
index)
spf_GPIPolK1_extractPauline = np.delete(spf_GPIPolK1_extractPauline, index)
error_GPIPolK1_extractPauline = np.delete(error_GPIPolK1_extractPauline, index)
spf_sphere_h = measure_spf_errors('SPHERE_Hband_MCMC', nb_random_models)
spf_sphere_h_3g = measure_spf_errors('SPHERE_Hband_3g_MCMC', nb_random_models)
spf_gpi_not1at90 = measure_spf_errors('GPI_Hband_MCMC',
nb_random_models,
Norm_90_inplot=0.93)
spf_gpi_h_1at90 = measure_spf_errors('GPI_Hband_MCMC', nb_random_models)
spf_gpi_j = measure_spf_errors('GPI_Jband_MCMC', nb_random_models)
spf_gpi_k1 = measure_spf_errors('GPI_K1band_MCMC', nb_random_models)
spf_gpi_k2 = measure_spf_errors('GPI_K2band_MCMC', nb_random_models)
spf_gpi_h_fake = measure_spf_errors('GPI_Hband_fake_MCMC', nb_random_models)
spf_gpi_h_save = measure_spf_errors('GPI_Hband_MCMC', nb_random_models, save = True)
color0 = 'black'
color1 = '#3B73FF'
color2 = '#ED0052'
color3 = '#00AF64'
color4 = '#FFCF0B'
####################################################################################
## plot h only
####################################################################################
plot.figure()
name_pdf = 'compare_SPHERE_GPI_H.pdf'
plot.fill_between(scattered_angles,
spf_sphere_h['errorbar_sup'],
spf_sphere_h['errorbar_inf'],
facecolor=color0,
alpha=0.1)
plot.fill_between(scattered_angles,
spf_gpi_not1at90['errorbar_sup'],
spf_gpi_not1at90['errorbar_inf'],
facecolor=color1,
alpha=0.1)
plot.plot(scattered_angles,
spf_sphere_h['best_spf'],
linewidth=2,
color=color0,
label="SPHERE H2 (extraction MCMC, this work)")
plot.plot(scattered_angles,
spf_gpi_not1at90['best_spf'],
linewidth=2,
color=color1,
label="GPI H (extraction MCMC)")
plot.errorbar(angles_sphere_extractJulien,
1.38 * spf_shpere_extractJulien,
yerr=errors_sphere_extractJulien,
fmt='o',
label='SPHERE H2 (previous extraction by M17)',
ms=3,
capthick=1,
capsize=2,
elinewidth=1,
markeredgewidth=1,
color='grey')
# plot.errorbar(angles_GPIPolK1_extractPauline,
# spf_GPIPolK1_extractPauline,
# yerr=error_GPIPolK1_extractPauline,
# fmt='x',
# label='GPI Pol K1 (extraction P. Arriaga)',
# ms=3,
# capthick=1,
# capsize=2,
# elinewidth=1,
# markeredgewidth=1,
# color=color3)
plot.legend()
plot.yscale('log')
plot.ylim(bottom=0.3, top=30)
plot.xlim(left=0, right=180)
plot.xlabel('Scattering angles')
plot.ylabel('Normalized total intensity')
plot.tight_layout()
plot.savefig(os.path.join(folder_save_pdf, name_pdf))
plot.close()
####################################################################################
## plot all colors
####################################################################################
name_pdf = 'compare_GPI_color.pdf'
plot.figure()
plot.fill_between(scattered_angles,
spf_gpi_j['errorbar_sup'],
spf_gpi_j['errorbar_inf'],
facecolor=color4,
alpha=0.1)
plot.fill_between(scattered_angles,
spf_gpi_h_1at90['errorbar_sup'],
spf_gpi_h_1at90['errorbar_inf'],
facecolor=color1,
alpha=0.1)
plot.fill_between(scattered_angles,
spf_sphere_h['errorbar_sup'],
spf_sphere_h['errorbar_inf'],
facecolor=color0,
alpha=0.1)
plot.fill_between(scattered_angles,
spf_gpi_k1['errorbar_sup'],
spf_gpi_k1['errorbar_inf'],
facecolor=color2,
alpha=0.1)
plot.fill_between(scattered_angles,
spf_gpi_k2['errorbar_sup'],
spf_gpi_k2['errorbar_inf'],
facecolor=color3,
alpha=0.1)
plot.plot(scattered_angles,
spf_gpi_j['best_spf'],
linewidth=2,
color=color4,
label="GPI J (extraction MCMC)")
plot.plot(scattered_angles,
spf_gpi_h_1at90['best_spf'],
linewidth=2,
color=color1,
label="GPI H (extraction MCMC)")
plot.plot(scattered_angles,
spf_sphere_h['best_spf'],
linewidth=2,
color=color0,
label="SPHERE H2 (extraction MCMC)")
plot.plot(scattered_angles,
spf_gpi_k1['best_spf'],
linewidth=2,
color=color2,
label="GPI K1 (extraction MCMC)")
plot.plot(scattered_angles,
spf_gpi_k2['best_spf'],
linewidth=2,
color=color3,
label="GPI K2 (extraction MCMC)")
plot.legend()
plot.yscale('log')
plot.ylim(bottom=0.3, top=30)
plot.xlim(left=0, right=180)
plot.xlabel('Scattering angles')
plot.ylabel('Normalized total intensity')
plot.tight_layout()
plot.savefig(os.path.join(folder_save_pdf, name_pdf))
plot.close()
####################################################################################
## 3g plot
####################################################################################
name_pdf = 'compare_3g_SPF.pdf'
plot.figure()
plot.fill_between(scattered_angles,
spf_sphere_h['errorbar_sup'],
spf_sphere_h['errorbar_inf'],
facecolor=color0,
alpha=0.1)
plot.fill_between(scattered_angles,
spf_sphere_h_3g['errorbar_sup'],
spf_sphere_h_3g['errorbar_inf'],
facecolor=color2,
alpha=0.1)
plot.plot(scattered_angles,
spf_sphere_h['best_spf'],
linewidth=2,
color=color0,
label="2 HG SPF (MCMC best model)")
plot.plot(scattered_angles,
spf_sphere_h_3g['best_spf'],
linewidth=2,
color=color2,
linestyle = '-.',
label="3 HG SPF (MCMC best model)")
plot.plot(scattered_angles,
1.38 * hg3g_fitted_Milliextraction,
linewidth=2,
color=color3,
label="3 HG SPF fitted to M17 SPF (MCMC initial point)")
plot.errorbar(angles_sphere_extractJulien,
1.38 * spf_shpere_extractJulien,
yerr=errors_sphere_extractJulien,
fmt='o',
label='Previous extraction by M17',
ms=3,
capthick=1,
capsize=2,
elinewidth=1,
markeredgewidth=1,
color='grey')
handles, labels = plot.gca().get_legend_handles_labels()
order = [3, 2, 1, 0]
plot.legend([handles[idx] for idx in order], [labels[idx] for idx in order])
plot.yscale('log')
plot.ylim(bottom=0.3, top=30)
plot.xlim(left=0, right=180)
plot.xlabel('Scattering angles')
plot.ylabel('Normalized total intensity')
plot.tight_layout()
plot.savefig(os.path.join(folder_save_pdf, name_pdf))
plot.close()
####################################################################################
## injected spf plot
####################################################################################
name_pdf = 'Comparison_spf_injected_recovered.pdf'
plot.figure()
plot.fill_between(scattered_angles,
spf_gpi_h_fake['errorbar_sup'],
spf_gpi_h_fake['errorbar_inf'],
facecolor=color3,
alpha=0.1)
plot.plot(scattered_angles,
spf_gpi_h_fake['best_spf'],
linewidth=2,
color=color3,
label="SPF Recoreved After MCMC")
plot.plot(scattered_angles,
injected_hg,
linewidth=1.5 ,
linestyle = '-.',
color=color2,
label="SPF Injected into Empty Dataset")
handles, labels = plot.gca().get_legend_handles_labels()
order = [1, 0]
plot.legend([handles[idx] for idx in order], [labels[idx] for idx in order])
plot.yscale('log')
plot.ylim(bottom=0.3, top=30)
plot.xlim(left=0, right=180)
plot.xlabel('Scattering angles')
plot.ylabel('Normalized total intensity')
plot.tight_layout()
plot.savefig(os.path.join(folder_save_pdf, name_pdf))
plot.close()