-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver_bothwd_he_thread_plummer.py
764 lines (631 loc) · 25.9 KB
/
driver_bothwd_he_thread_plummer.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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
from astropy.io import ascii
from astropy import units
import numpy
from matplotlib import pyplot
#matplotlib.use('Qt5Agg')
import corner
import emcee
import pylab as plt
import numpy as np
import numpy.ma as ma
from numpy import sum
#import seaborn
import multiprocessing as multi
#Nthreads = multi.cpu_count() - 2
Nthreads = 6
from scipy.optimize import curve_fit
from scipy.interpolate import RegularGridInterpolator
from scipy.interpolate import RectBivariateSpline
from scipy.interpolate import griddata
from scipy.optimize import leastsq
import sys
Rsun = units.Rsun.to(units.cm) #6.957e10 #cm
Msun = units.Msun.to(units.g) #1.9891e+33 #g
parsec = units.pc.to(units.cm) #3.086e18 #cm
Grav = 6.6743e-8 #cgs
#these are various contols for the MCMC
Nemcee = 1000
Nthin = 1
Nburn = 200
Nwalkers = 500
bndsprior = True
#I also set bndsprior = True in MCMCfit.py.
print("Nemcee = ", Nemcee)
print("Nwalkers = ", Nwalkers)
print("Nthin = ", Nthin)
print("Nburn = ", Nburn)
if (Nthreads > Nwalkers):
print(" WARNING: you set Nthreads > Nwalkers. This is probably not wise. I will set Nthreads = Nwalkers")
Nthreads = Nwalkers
print("Nthreads = ", Nthreads)
#Eclipsing binary distance (Meibom+2009)
#dobs = 1770.0 #pc
#dobserr = 75.0
#Gaia DR2 distance from Christian Knigge:
#dobs = 1942.0 #pc
#dobserr = 30.0
#Gaia distance from Phill Cargile:
dobs = 1955.0 #pc
pradius = 11.0 #pc - scale radius for the Plummer sphere
#dobserr = 15.0 #presumed tidal radius
#dobserr = 130.0 #error in the data fits
# this function interpolates in the WD grid of log g and Teff, then normalizes
# based on the radius and distance
#this function fits BS1 with a He-core grid and BS2 with a CO-core grid
#He-core mass/radius relationship comes from Althaus et al: http://evolgroup.fcaglp.unlp.edu.ar/TRACKS/tracks_heliumcore.html
#CO-core mass/radius relationship comes from Bergeron: http://www.astro.umontreal.ca/~bergeron/CoolingModels/
distsave = []
def wdparams(wave, ps, ifunc):
modg, modT = ps
wdmod = np.array([modg,modT])
#dist = np.random.normal(dobs, dobserr)
#findM = griddata((temp, logg), Mo, (modT, modg), method='linear', fill_value=1e8)
#modrad = np.sqrt( (Grav * findM * Msun ) / 10**modg )
#Sample Plummer sphere to randomly assign distance:
phi = np.random.uniform(0, 2*np.pi)
theta = np.arccos(np.random.uniform(-1,1))
r = pradius / np.sqrt(np.random.uniform(0,1)**(-2.0/3.0) - 1)
x = (r * np.sin(theta) * np.cos(phi)) + dobs
y = r * np.sin(theta) * np.sin(phi)
z = r * np.cos(theta)
dist = np.sqrt (x**2.0 + y**2.0 + z**2.0)
realdist = dist * parsec
np.append(distsave, realdist)
if (ifunc==interpfunc1):
logmodT = np.log10(modT)
#findM_He = interpMo_He(np.array([modg,modT]))[0]
#if (findM_He>=0.44):
#findR_He = interpRo(np.array([modg,modT]))[0]
#elif (findM_He<0.44):
findR_He = interpRo_He(np.array([modg,logmodT]))[0]
modrad = findR_He * Rsun
norm = (modrad**2 / realdist**2)
if (findR_He==0.):
print("WE HAVE A PROBLEM")
elif (ifunc==interpfunc2):
findM = interpMo(np.array([modg,modT]))[0]
findR = interpRo(np.array([modg,modT]))[0]
modrad = findR * Rsun
norm = (modrad**2 / realdist**2)
#if (findM>=0.49):
# norm = (modrad**2 / realdist**2)
#elif (findM<0.49):
# norm = 0
#prevent CO fits below the CO mass limit
#logmodT = np.log10(modT)
#findM_He = interpMo_He(np.array([modg,modT]))[0]
#if (findM_He>=0.44):
#findR_He = interpRo(np.array([modg,modT]))[0]
#elif (findM_He<0.44):
#findR_He = interpRo_He(np.array([modg,logmodT]))[0]
#modrad = findR_He * Rsun
#if (findR_He==0.):
# print "WE HAVE A PROBLEM"
fitflux = ifunc(wdmod)[0] * np.pi * norm
return fitflux
#NOTE: interpfunc1 and interpfunc2 are defined below
#this is our new function that will do the interpolation on each WD separately, but using the same distance
def wdparams2(waves, ps):
modg1, modT1, modg2, modT2 = ps
wave1, wave2 = waves
fitflux1 = wdparams(wave1, (modg1, modT1), interpfunc1)
fitflux2 = wdparams(wave2, (modg2, modT2), interpfunc2)
return fitflux1, fitflux2
def findM(ps):
modg, modT = ps
mass = griddata((temp, logg), Mo, (modT, modg), method='linear', fill_value=1e20)
return mass
def findM_He(ps):
modg, modT = ps
modT = np.log10(modT)
mass_He = griddata((t_He, logg_He), Mo_He, (modT, modg), method='linear', fill_value=1e20)
return mass_He
#we're not really fitting radius, radius is defined by log g and temp, we're fitting for distance...
#read in table of CO-core WD values:
temp, logg, Mo, Ro = np.loadtxt('../Natalie/wdtable.txt', unpack=True)
#griddata can do this all in fewer lines, but it takes SO MUCH longer
#findM = griddata((temp,logg), Mo, (15000,7.5), method='linear')
#let's make some tables...
glist = [7.0, 7.5, 8.0, 8.5, 9.0]
tlist = temp[0:60]
#we'll only be looking up either M or R at a time.
tableMo = np.zeros([len(glist), len(tlist)])
tableRo = np.zeros([len(glist), len(tlist)])
for i in range(0, len(glist)):
for j in range(0, len(tlist)):
tableMo[i][j] = Mo[i*(len(tlist))+j]
tableRo[i][j] = Ro[i*(len(tlist))+j]
interpMo = RegularGridInterpolator((glist,tlist), tableMo, bounds_error=False, fill_value=None)
interpRo = RegularGridInterpolator((glist,tlist), tableRo, bounds_error=False, fill_value=None)
#read in table of He-core WD values:
t_He, logg_He, Mo_He, Ro_He, age = np.loadtxt('../Natalie/mygrid.txt', unpack=True)
#t_He, logg_He, Mo_He, Ro_He = np.loadtxt('mygrid.txt', unpack=True)
#temp_He = 10.0**(t_He)
temp_He = t_He
tlist_He = temp_He[0:300]
glist_He = logg_He[0::300]
tableMo_He = np.zeros([len(glist_He), len(tlist_He)])
tableRo_He = np.zeros([len(glist_He), len(tlist_He)])
for i in range(0, len(glist_He)):
for j in range(0, len(tlist_He)):
tableMo_He[i][j] = Mo_He[i*(len(tlist_He))+j]
tableRo_He[i][j] = Ro_He[i*(len(tlist_He))+j]
interpMo_He = RegularGridInterpolator((glist_He,tlist_He), tableMo_He, bounds_error=False, fill_value=None)
interpRo_He = RegularGridInterpolator((glist_He,tlist_He), tableRo_He, bounds_error=False, fill_value=None)
#read in the BSS data and ...
#CREATE WAVELENGTH MASK
#edges of masked regions adjusted by hand in TestMasks.ipynb to
#block out the geocoronal lines.
#w1, f1, err1 = np.loadtxt('lcb201010_x1dsum.txt.bin30.unred', unpack=True)
w1, f1, stdev1, err1 = np.loadtxt('../Natalie/lcb201010_x1dsum.txt.bin30.unred.newerrors', unpack=True)
add_disp = 9.1e-18
#adding intrinsic dispersion
toterr1 = np.sqrt(err1**2.0 + add_disp**2.0)
w1_1 = ma.masked_less(w1, 1141)
w1_2 = ma.masked_inside(w1_1, 1178., 1250.)
w1_3 = ma.masked_inside(w1_2, 1292., 1318.)
w1_4 = ma.masked_inside(w1_3, 1348., 1367.)
w1_5 = ma.masked_inside(w1_4, 1332., 1340.)
dataw1 = ma.masked_greater(w1_5, 1725.)
dataw1c = 1.0*dataw1.compressed()
#w2, f2, err2 = np.loadtxt('lcb202010_x1dsum.txt.bin30.unred', unpack=True)
w2, f2, stdev2, err2 = np.loadtxt('../Natalie/lcb202010_x1dsum.txt.bin30.unred.newerrors', unpack=True)
#adding intrinsic dispersion
toterr2 = np.sqrt(err2**2.0 + add_disp**2.0)
w2_1 = ma.masked_less(w2, 1142)
w2_2 = ma.masked_inside(w2_1, 1182., 1250.)
w2_3 = ma.masked_inside(w2_2, 1290., 1318.)
w2_4 = ma.masked_inside(w2_3, 1348., 1368.)
w2_5 = ma.masked_inside(w2_4, 1330., 1340.)
#dataw2 = ma.masked_greater(w2_5, 1533.)
dataw2 = ma.masked_greater(w2_5, 1600.)
dataw2c = 1.0*dataw2.compressed()
#We're not going to fit the BSS. Worried that a bad line list in the UV would
#mess everything up. So the mask cuts off at the red end to remove
#contribution from the BSS. BSS2 is brighter, so the cutoff is bluer.
#transfer mask to flux and error
dataf1 = ma.masked_array(f1, mask=dataw1.mask)
dataerr1 = ma.masked_array(toterr1, mask=dataw1.mask)
dataf1c = 1.0*dataf1.compressed()
dataerr1c = 1.0*dataerr1.compressed()
dataf2 = ma.masked_array(f2, mask=dataw2.mask)
dataerr2 = ma.masked_array(toterr2, mask=dataw2.mask)
dataf2c = 1.0*dataf2.compressed()
dataerr2c = 1.0*dataerr2.compressed()
wave1 = dataw1c
ydata1 = dataf1c
sigma1 = dataerr1c
wave2 = dataw2c
ydata2 = dataf2c
sigma2 = dataerr2c
#set up data dict
data = dict()
data['x1'] = wave1
data['y1'] = ydata1
data['y1e'] = sigma1
data['x2'] = wave2
data['y2'] = ydata2
data['y2e'] = sigma2
#send the data
vars1 = data['x1'] #x-axis
vals1 = data['y1'] #y-axis
evals1 = data['y1e'] #error on the y axis
vars2 = data['x2'] #x-axis
vals2 = data['y2'] #y-axis
evals2 = data['y2e'] #error on the y axis
'''
#find the distance constraint
distcm = dobs #* parsec
#f is 1sig
#distcmerr = dobserr*1.0 #* parsec
#setting arbitrarily large distance error to allow the walkers to explore the posterior space
distcmerr = dobserr*3.0 #* parsec
distmin = (distcm - distcmerr)
distmax = (distcm + distcmerr)
print("This is the minimum distance:")
print(distmin)
print("This is the maximum distance:")
print(distmax)
'''
#define the function
func = wdparams2
#make guesses at parameters :
#NOTE: these are now used as Gaussian priors
#log g1, T1, logg2, T2, dist
params = (7.4, 15600., 7.85, 17500.)
eparams = (0.1, 1000., 0.1, 1000.)
#distance fit in units of pc
#give the parameter names for plotting (can use latex symbols)
params_name = [r'log$_{10}$(g$_{5379}$)',r'Temp$_{5379}$(K)',r'log$_{10}$(g$_{4540}$)',r'Temp$_{4540}$(K)']
#provide bounds on the parameters used to draw the initial guesses and walkers and possibly to limit the priors
bnds = ( (6.0, 7.74), (12000., 35000.), (7.74, 9.0), (12000., 35000.))
#make lists of g and T values so we can read in the models.
glist = ["600","625","650","675","700","725","750","775","800","825",
"850","875","900"]
garray100 = np.array((glist), dtype=np.float)
garray = [g/100.0 for g in garray100]
Tlist = ["11000","12000","13000","14000","16000","18000","20000",
"22000","24000","26000","28000","30000","35000"]
Tarray = np.array((Tlist), dtype=np.float)
modinfo = np.array((glist, Tlist))
modflux1 = np.zeros((len(glist), len(Tlist), len(dataf1c)))
modflux2 = np.zeros((len(glist), len(Tlist), len(dataf2c)))
#read in the models for BS1
for i in range(0, len(glist)):
for j in range(0, len(Tlist)):
file = "../Natalie/da" + str(Tlist[j]) +"_" + \
str(glist[i]) +".dk.rebin1.lsf.bin30"
modw1, modf1, moderr1 = np.loadtxt(file, unpack=True)
dummy = ma.masked_array(modf1, mask=dataw1.mask)
modflux1[i][j] = 1.0e-8*dummy.compressed()
#the model fluxes are per cm. the above
#line changes this to per A.
interpfunc1 = RegularGridInterpolator((garray,Tarray), modflux1,
bounds_error=False, fill_value=None)
#read in the models for BS2
for i in range(0, len(glist)):
for j in range(0, len(Tlist)):
file = "../Natalie/da" + str(Tlist[j]) +"_" + \
str(glist[i]) +".dk.rebin2.lsf.bin30"
modw2, modf2, moderr2 = np.loadtxt(file, unpack=True)
dummy = ma.masked_array(modf2, mask=dataw2.mask)
modflux2[i][j] = 1.0e-8*dummy.compressed()
#the model fluxes are per cm. the above
#line changes this to per A.
interpfunc2 = RegularGridInterpolator((garray,Tarray), modflux2,
bounds_error=False, fill_value=None)
#dummy figure to see what the initial guess looks like
yval_mi1, yval_mi2 = func((vars1, vars2), params)
f2 = plt.figure(figsize=(5,7))
ax1 = plt.subplot(211)
ax1.set_xlabel('wavelength')
ax1.set_ylabel('flux_1')
ax1.errorbar(vars1, vals1, yerr=evals1, fmt='r.')
#ax1.set_xscale("log", nonposx='clip')
ax1.set_yscale("log", nonposy='clip')
ax1.plot(vars1,yval_mi1,'b')
ax2 = plt.subplot(212)
ax2.set_xlabel('wavelength')
ax2.set_ylabel('flux_2')
ax2.errorbar(vars2, vals2, yerr=evals2, fmt='r.')
#ax2.set_xscale("log", nonposx='clip')
ax2.set_yscale("log", nonposy='clip')
ax2.plot(vars2, yval_mi2,'b')
plt.subplots_adjust(hspace = 0.4, top = 0.95, bottom = 0.07, left = 0.15, right = 0.95)
f2.savefig('initial_guess_fit_setdist_plummer.png')
####################################################################################################
#to enable threading in emcee, we need the MCMCfit.py methods to be here, instead of having this all in a class
#it's less elegant, but this works. (And apparently this is because emcee must "pickle" the data and can't do that inside a class)
#parameters for fit
#emcee parameters
doMCMC = True
pr_lo = 16.
pr_mi = 50.
pr_hi = 84.
#for plots
doplots = True
f1name = "walkers_setdist_plummer.png"
f2name = "fit_MCMC_setdist_plummer.png"
f3name = "chains_setdist_plummer.png"
f4name = "posterior_setdist_plummer.png"
#f5name = "fit_leastsq_wd2.png"
#output
results = []
print_posterior = True
posterior_file = "posterior_setdist_plummer.txt"
#flatten a tuple of tuples into a list
#also works for lists
def flatten(tupoftup):
return [element for foo in tupoftup for element in foo]
#normal chi^2
def chi2(ps, args):
x,obs,sigma,func = args
ob = flatten(obs)
si = flatten(sigma)
fu = flatten(func(x,ps))
OC = [o - f for (o,f) in zip(ob,fu)]
return np.sum([o**2./s**2. for (o,s) in zip(OC,si)])
#reduced chi^2
def chi2_red(ps, args):
x,obs,sigma,func = args
ob = flatten(obs)
si = flatten(sigma)
fu = flatten(func(x,ps))
OC = [o - f for (o,f) in zip(ob,fu)]
dof = len(ob) - len(ps)
return np.sum([o**2./s**2. for (o,s) in zip(OC,si)])/dof
#chi^2 as a list for the least squares fit
def chi2list(ps, args):
x,obs,sigma,func = args
ob = flatten(obs)
si = flatten(sigma)
fu = flatten(func(x,ps))
OC = [o - f for (o,f) in zip(ob,fu)]
return [o**2./s**2. for (o,s) in zip(OC,si)]
#draw initial walkers from the covariance matrix given by the least squares fit
def getwalkers(fit_results, walkers, n=1000):
best,cov=fit_results[:2]
pInits = np.random.multivariate_normal(best,cov*n,Nwalkers)
for j,p in enumerate(pInits):
for i,a in enumerate(p):
if (a > bnds[i][1] or a < bnds[i][0]):
pInits[j][i] = walkers[j][i]
return pInits
#draw random initial guesses for the least-squares fitting
def drawguesses():
initPosT = []
# I found it easier to select the parameters first this way...
for i,p in enumerate(params):
initPosT.append([x*(bnds[i][1]-bnds[i][0]) + bnds[i][0] for x in np.random.random(Nwalkers)])
#...and then transpose this matrix so that I can get an array of guesses
initPos = np.matrix.transpose(np.array(initPosT))
return initPos
#for emcee...
#likelihood
def lnlike(ps, args):
return -0.5*chi2(ps,args)
def gauss(x,m,s):
# return 1./(s*(2.*np.pi)**2.)*np.exp(-1.*(x - m)**2./(2.*s**2.))
return np.exp(-1.*(x - m)**2./(2.*s**2.)) #don't want the normalization because we want a peak at 1
def lnprior(ps):
if (bndsprior):
lp = 0. #ln(1)
for i,p in enumerate(ps):
if (p < bnds[i][0] or p > bnds[i][1]):
#doing this doesn't allow some walkers to advance.
return -np.inf #ln(0)
#it works better if we have some very small number
# return np.log(1.e-3)
#though probably the most realistic thing would be to have some Gaussian priors
#we'd want to create inputs in this class and set them in the driver, but just an example here
# mn = (bnds[i][0] + bnds[i][1])/2.
# si = abs(bnds[i][0] - bnds[i][1])/5. #5 is arbitrary
#only apply a Gaussian prior to the distance (the rest are just flat within the bounds)
if (i == 4):
mn = params[i]
si = eparams[i]
g = gauss(p, mn, si)
if (g > 0): #safety check
lp += np.log(g)
else:
return -np.inf
return 0. #ln(1)
#priors
#this assume flat priors (can make this more informed, given whatever prior information we think we know)
# def lnprior(ps):
# if (bndsprior):
# for i,p in enumerate(ps):
# if (p < bnds[i][0] or p > bnds[0][1]):
# return -np.inf #ln(0)
# return 0. #ln(1)
#now construct the probability as priors*likelihood (but again in the log)
def lnprob(ps, args):
lp = lnprior(ps)
if not np.isfinite(lp):
return -np.inf
return lp + lnlike(ps, args)
#plot the chains
def plotchains(sampler):
subn = len(params)*100+11
for i,p in enumerate(params):
for w in range(sampler.chain.shape[0]):
ax = pyplot.subplot(subn+i)
ax.plot(sampler.chain[w,:,i],color='black', linewidth=1, alpha=0.15)
if (len(params_name) >= i):
ax.set_ylabel(params_name[i])
ax.plot([Nburn,Nburn],[min(flatten(sampler.chain[:,:,i])),max(flatten(sampler.chain[:,:,i]))],'r--', linewidth=2)
#need another method to plot the fitted surface
#this is the main method that runs the fitting routine
def dofit():
#used for the fitting
vars = (vars1, vars2)
vals = (vals1, vals2)
evals = (evals1, evals2)
args = (vars, vals, evals, func)
if (doMCMC):
#OK now choose the walkers for the MCMC
p0 = np.zeros([Nwalkers,len(params)])
for i in range(0, Nwalkers):
for j in range(0, len(params)):
#p0[i][j] = params[j]*np.random.uniform(0.95,1.05,1)
p0[i][j] = np.random.normal(loc=params[j],scale=params[j]*0.005,size=1)
#inbounds = False
#if (inbounds):
# walkers = getwalkers(fres, inits)
#else:
# walkers = inits
walkers = p0
if (doplots):
f1 = corner.corner(walkers, labels = params_name, truths = params, range = [(0.999*min(walkers[:,i]),1.001*max(walkers[:,i])) for i in range(len(params))])
f1.savefig(f1name, dpi=300)
#now run emcee
print("Running emcee ...")
sampler = emcee.EnsembleSampler(Nwalkers, len(params), lnprob, args = (args,), threads = Nthreads)
for i, result in enumerate(sampler.sample(p0, iterations=Nemcee)):
if (i+1) % 100 == 0:
print(("{0:5.1%}".format(float(i) / Nemcee)))
sampler.reset()
sampler.run_mcmc(walkers, Nemcee, thin = Nthin)
#remove the burn-in and reshape the samples
samples = sampler.chain[:, Nburn:, :].reshape((-1, len(params)))
if (print_posterior):
ofile = open(posterior_file, 'w')
for s in samples:
for p in s:
ofile.write("%15.7e " % p,)
ofile.write("\n")
ofile.close()
print(len(distsave))
#print the results
pout = [(v[1], v[2]-v[1], v[1]-v[0]) for v in zip(*np.percentile(samples, [pr_lo, pr_mi, pr_hi], axis=0))]
#print("Acceptance fractions:")
#print((sampler.acceptance_fraction))
print("Median and mean acceptance fraction:")
print((np.median(sampler.acceptance_fraction)))
print((np.mean(sampler.acceptance_fraction)))
#print "Estimated autocorrelation time:"
#print((sampler.acor))
for i,p in enumerate(pout):
name = " "
if (len(params_name) >= i):
name = params_name[i]
print('%-10s : %10f +%10f -%10f' % ((name,)+tuple(p)))
results.append(tuple(p))
#plot the results
if (doplots):
#fit
#This one is not general
#set up shared axes and different plot sizes (not sure if this is the best way, but I think it works OK here)
'''
gs = pyplot.GridSpec(7, 1, hspace=0, height_ratios = [2, 1, 0.5, 2, 1, 0.5, 2] )
f2 = pyplot.figure(figsize=(5,12))
yval_mi1, yval_mi2 = func(vars, [pout[x][0] for x in range(len(pout))])
yval_lo1, yval_lo2 = func(vars, [pout[x][0] - pout[x][1] for x in range(len(pout))])
yval_hi1, yval_hi2 = func(vars, [pout[x][0] + pout[x][2] for x in range(len(pout))])
# ax1 = pyplot.subplot(511)
ax1 = f2.add_subplot(gs[0,:])
ax1.set_ylabel('flux$_1$')
ax1.errorbar(vars1, vals1, yerr=evals1, fmt='r.')
ax1.set_yscale("log", nonposy='clip')
ax1.plot(vars1, yval_mi1,'b')
ax1.plot(vars1, yval_lo1,'b--')
ax1.plot(vars1, yval_hi1,'b--')
ax1.set_xlim((1100,1700))
# ax2 = pyplot.subplot(512)
ax2 = f2.add_subplot(gs[1,:], sharex=ax1)
ax2.set_xlabel('Wavelength')
ax2.set_ylabel('Residuals')
resid = [ (y - yf) / ye for (y, yf, ye) in zip(vals1, yval_mi1, evals1)]
pyplot.plot(vars1, resid,'o',linestyle="None")
ax2.set_ylim((-5, 5))
pyplot.setp(ax1.get_xticklabels(), visible=False)
# ax3 = pyplot.subplot(513)
ax3 = f2.add_subplot(gs[3,:])
ax3.set_ylabel('flux$_2$')
ax3.errorbar(vars2, vals2, yerr=evals2, fmt='r.')
ax3.set_yscale("log", nonposy='clip')
ax3.plot(vars2, yval_mi2,'b')
ax3.plot(vars2, yval_lo2,'b--')
ax3.plot(vars2, yval_hi2,'b--')
ax3.set_xlim((1100,1700))
# ax4 = pyplot.subplot(514)
ax4 = f2.add_subplot(gs[4,:], sharex=ax3)
ax4.set_xlabel('Wavelength')
ax4.set_ylabel('Residuals')
resid = [ (y - yf) / ye for (y, yf, ye) in zip(vals2, yval_mi2, evals2)]
pyplot.plot(vars2, resid,'.',linestyle="None")
ax4.set_ylim((-5, 5))
pyplot.setp(ax3.get_xticklabels(), visible=False)
ax5 = pyplot.subplot(515)
ax5 = f2.add_subplot(gs[6,:])
ax5.set_xlabel(r'$\chi^2_{\rm red}$')
ax5.set_ylabel('N')
chi2a = [chi2_red(ps, args) for ps in samples]
n, bins, patches = ax5.hist(chi2a, 50)
pyplot.subplots_adjust(hspace = 0.4, top = 0.99, bottom = 0.07, left = 0.15, right = 0.95)
f2.savefig(f2name, dpi=300)
'''
#chains
f3 = pyplot.figure(figsize=(5,10))
plotchains(sampler)
pyplot.subplots_adjust(hspace = 0.4, top = 0.98, bottom = 0.03, left = 0.2, right = 0.95)
f3.savefig(f3name, dpi=300)
#posterior
f4 = corner.corner(samples, labels = params_name, quantiles=[0.16, 0.5, 0.84])
f4.savefig(f4name, dpi=300)
f5 = pyplot.figure(figsize=(12,5))
ax5 = f5.add_subplot(111)
flatsamples = sampler.flatchain
for s in flatsamples[np.random.randint(len(samples), size=1000)]:
sampley1, sampley2 = func(vars, s)
ax5.plot(vars1, sampley1, color='gray', alpha=0.2, linewidth=0.5)
ax5.errorbar(vars1, vals1, yerr=evals1, fmt='r.')
ax5.set_xlim(1100,1700)
f5.savefig("testspec_setdist_plummer.png", dpi=300)
return results
####################################################################################################
if __name__=="__main__":
#now print the results to the terminal
results = dofit()
print(results)
logg1 = results[0][0]
logg1err_plus = results[0][1]
logg1err_minus = results[0][2]
maxlogg1 = logg1 + logg1err_plus
minlogg1 = logg1 - logg1err_minus
temp1 = results[1][0]
temp1err_plus = results[1][1]
temp1err_minus = results[1][2]
maxtemp1 = temp1 + temp1err_plus
mintemp1 = temp1 - temp1err_minus
logg2 = results[2][0]
logg2err_plus = results[2][1]
logg2err_minus = results[2][2]
maxlogg2 = logg2 + logg2err_plus
minlogg2 = logg2 - logg2err_minus
temp2 = results[3][0]
temp2err_plus = results[3][1]
temp2err_minus = results[3][2]
maxtemp2 = temp2 + temp2err_plus
mintemp2 = temp2 - temp2err_minus
logtemp1 = np.log10(temp1)
logmaxtemp1 = np.log10(maxtemp1)
logmintemp1 = np.log10(mintemp1)
wd1values = np.array([logg1,logtemp1])
#wd1plusval = np.array([( logg1 + logg1err_plus, temp1 + temp1err_plus)])
wd1plusval = np.array([(maxlogg1, logmaxtemp1)])
#wd1minusval = np.array([( logg1 - logg1err_minus, temp1 - temp1err_minus)])
wd1minusval = np.array([( minlogg1, logmintemp1)])
wd2values = np.array([logg2,temp2])
wd2plusval = np.array([( maxlogg2, maxtemp2)])
wd2minusval = np.array([( minlogg2, mintemp2)])
findR1 = interpRo_He(wd1values)[0]
findR1_plus = interpRo_He(wd1plusval)[0]
findR1_minus = interpRo_He(wd1minusval)[0]
#findM1 = interpMo_He(wd1values)[0]
#findM1_plus = interpMo_He(wd1plusval)[0]
#findM1_minus = interpMo_He(wd1minusval)[0]
#findM1 = findM_He(wd1values)
#findM1_plus = findM_He(wd1plusval)
#findM1_minus = findM_He(wd1minusval)
mass_He = griddata((t_He, logg_He), Mo_He, (logtemp1, logg1), method='linear')
maxmass_He = griddata((t_He, logg_He), Mo_He, (logmaxtemp1, maxlogg1), method='linear')
minmass_He = griddata((t_He, logg_He), Mo_He, (logmintemp1, minlogg1), method='linear')
print("WD1 radius and errors")
print(findR1)
print((findR1 - findR1_plus))
print((findR1_minus - findR1))
print("WD1 mass and errors")
#print findM1
print(mass_He)
#print (findM1_plus - findM1)
print((maxmass_He - mass_He))
#print (findM1 - findM1_minus)
print((mass_He - minmass_He))
findR2 = interpRo(wd2values)[0]
findR2_plus = interpRo(wd2plusval)[0]
findR2_minus = interpRo(wd2minusval)[0]
#findM2 = interpMo(wd2values)[0]
#findM2_plus = interpMo(wd2plusval)[0]
#findM2_minus = interpMo(wd2minusval)[0]
#findM2 = findM(wd2values)
#findM2_plus = findM(wd2plusval)
#findM2_minus = findM(wd2minusval)
print("WD2 radius and errors")
print(findR2)
print((findR2 - findR2_plus))
print((findR2_minus - findR2))
mass_CO = griddata((temp, logg), Mo, (temp2, logg2), method='linear')
maxmass_CO = griddata((temp, logg), Mo, (maxtemp2, maxlogg2), method='linear')
minmass_CO = griddata((temp, logg), Mo, (mintemp2, minlogg2), method='linear')
print("WD2 mass and errors")
#print findM1
print(mass_CO)
#print (findM1_plus - findM1)
print((maxmass_CO - mass_CO))
#print (findM1 - findM1_minus)
print((mass_CO - minmass_CO))
#print "WD2 mass and errors"
#print findM2
#print (findM2_plus - findM2)
#print (findM2 - findM2_minus)