def resample_posterior_product(self,bv_arr,metallicity_arr,bv_errs=None,measure_err_arr=None,\ upperLim_arr=None,maxAge_arr = None, \ pdfPage=None,showPlot=False,showStars=False,title=None,givenAge=None,givenErr = None, sample_num=None,numIter=4): if (bv_errs is None): bv_errs = [self.const.BV_UNCERTAINTY] * len(bv_arr) if upperLim_arr is None: upperLim_arr = [False] * len(metallicity_arr) if maxAge_arr is None: maxAge_arr = [None] * len(metallicity_arr) if measure_err_arr is None: measure_err_arr = [self.const.MEASURE_ERR] * len(metallicity_arr) if sample_num is None: sample_num = 15 #len(bv_arr) - 5 ln_prob = np.zeros(len(self.const.AGE)) star_post = [] resample_args = (bv_arr, metallicity_arr, bv_errs, measure_err_arr, upperLim_arr, maxAge_arr) args = (pdfPage, showPlot, showStars, title, givenAge, givenErr) post = prob.resample(self.posterior_product, resample_args, args, sample_num, numIter) prob.normalize(self.const.AGE, post) p_struct = posterior() p_struct.array = post p_struct.stats = prob.stats(self.const.AGE, post) if (showPlot or pdfPage): title = title if title else 'Resampled Posterior Product Age Distribution' my_plot.posterior(self.const.AGE, p_struct.array, p_struct.stats,title,\ pdfPage,showPlot,star_post,givenAge,givenErr=givenErr) return p_struct
def posterior_product(self,bv_arr,metallicity_arr,bv_errs=None,measure_err_arr=None,\ upperLim_arr=None,maxAge_arr = None, \ pdfPage=None,showPlot=False,showStars=False,title=None,givenAge=None,givenErr = None): if (bv_errs is None): bv_errs = [self.const.BV_UNCERTAINTY] * len(bv_arr) if upperLim_arr is None: upperLim_arr = [False] * len(metallicity_arr) if maxAge_arr is None: maxAge_arr = [None] * len(metallicity_arr) if measure_err_arr is None: measure_err_arr = [self.const.MEASURE_ERR] * len(metallicity_arr) ln_prob = np.zeros(len(self.const.AGE)) star_post = [] np.seterr(divide='ignore') if self.metal == 'lithium' and np.mean(metallicity_arr) < 3: metallicity_arr = np.power(10, metallicity_arr) sec_per_star = 0.5 start = time.time() for i in range(len(bv_arr)): star_time = time.time() y = self.likelihood(bv_arr[i],bv_errs[i],metallicity_arr[i],measure_err_arr[i],\ upperLim_arr[i]) * self.prior(maxAge_arr[i]) prob.normalize(self.const.AGE, y) inds = np.nonzero(y)[0] ln_prob += np.log(y) if (showStars): star_post.append(y) utils.progress_bar( float(i + 1) / len(bv_arr), int((len(bv_arr) - (i + 1)) * sec_per_star)) #exp moving average sec_per_star = sec_per_star + 0.1 * ( (time.time() - star_time) - sec_per_star) #sec_per_star = (time.time() - start)/float(i+1) print("Finished %d stars. Average time per star: %.2f seconds." \ % (len(bv_arr),(time.time() - start)/len(bv_arr))) post = np.exp(ln_prob - np.max(ln_prob)) #prevent underflow prob.normalize(self.const.AGE, post) p_struct = posterior() p_struct.array = post p_struct.stats = prob.stats(self.const.AGE, post) p_struct.stars_posteriors = star_post if (showPlot or pdfPage): my_plot.posterior(self.const.AGE, p_struct.array, p_struct.stats,title,\ pdfPage,showPlot,star_post,givenAge,givenErr=givenErr,\ bv_arr = bv_arr,metal=self.metal) return p_struct
def robs_fomalhaut(): import astropy from scipy.signal import savgol_filter t = astropy.io.fits.open(join('data','Fom-age-pdf.fits')) data = t[0].data x = np.linspace(np.min(data)-10,np.max(data)+10,1000) cdf = np.array([(data < n).sum() for n in x],dtype='float')/len(x) cdf /= cdf[-1] smoothed = savgol_filter(cdf, 51, 3) pdf = np.gradient(smoothed) pdf = savgol_filter(pdf,101,3) prob.normalize(x,pdf) #plt.plot(x,pdf,label='PDF') #plt.hist(data,bins=100,density=True) #plt.show() pdf[0:2],pdf[-2:] = [0,0],[0,0] return my_fits.piecewise(x,pdf)
def get_posterior(self,bv,metallicity,pdfPage=None,showPlot=False,\ bv_uncertainty=None,measure_err = None,upperLim=False,\ maxAge=None,givenAge=None,givenErr = None,mamajekAge=None,title=None,\ logPlot = False): if bv is None and self.metal == 'calcium': bv = 0.65 assert self.const.BV_RANGE[0] <= bv <= self.const.BV_RANGE[1], \ "B-V of %.2f out of range. Valid range: " % bv + str(self.const.BV_RANGE) if self.metal == 'calcium': assert self.const.METAL_RANGE[0] <= metallicity <= self.const.METAL_RANGE[1], \ "Indicator value %.2f out of range. Valid range: " % metallicity + str(self.const.METAL_RANGE) elif self.metal == 'lithium': assert self.const.METAL_RANGE_LIN[0] <= metallicity <= self.const.METAL_RANGE_LIN[1], \ "Indicator value %.2f out of range. Valid range: " \ % metallicity + str(self.const.METAL_RANGE_LIN) + " mA" if mamajekAge == True and self.const.METAL_RANGE[0] <= metallicity <= \ self.const.METAL_RANGE[1]: mamajekAge = utils.getMamaAge(metallicity) posterior_arr = self.likelihood(bv,bv_uncertainty,metallicity,measure_err,\ upperLim) * self.prior(maxAge) if all(posterior_arr == 0): print( "Posterior not well defined. Area is zero so adding constant") posterior_arr += 1 prob.normalize(self.const.AGE, posterior_arr) p_struct = posterior() p_struct.array = posterior_arr p_struct.stats = prob.stats(self.const.AGE, posterior_arr, upperLim) p_struct.upperLim = upperLim if (showPlot or pdfPage): if (title == None): title = '(B-V)o = '+'%.2f' % bv \ +', ' + self.metal + ' = %.2f' % metallicity my_plot.posterior(self.const.AGE, p_struct.array, p_struct.stats,title,pdfPage,\ showPlot,givenAge=givenAge,givenErr=givenErr, mamajekAge=mamajekAge,logPlot=logPlot) return p_struct
def notable_stars(): li_const = utils.init_constants('lithium') name = join('plots','notable_stars.pdf') names = ["HR 2562","HD 206893","TW PsA"] bv = [.45,.44,1.1] bv_err = [np.sqrt(.014**2+.01**2),np.sqrt(.02**2+.01**2),.03] # .03 b-v error for TW PsA? rhk = [-4.55,-4.466,None] mamaAge = [utils.getMamaAge(rhk[0]),utils.getMamaAge(rhk[1]),None] li = [21,28.5,33] li_err = [5,7,2] markers = ['*','*','*'] markerSize = 25 colors = ['gold','green','darkmagenta'] age = [None,None,440] age_range = [[300,900],[200,2100],[400,480]] pp=PdfPages(name) my_plot.metal_vs_bv(bv_ca,ca_fits,'calcium',None,False,specific_clusters=[0,5,7,8], legend=False,textlabels=True) plt.plot(bv,rhk,marker='s',markersize=markerSize,color='w',linestyle='None',zorder=9) for i in [0,1]: plt.plot(bv[i],rhk[i],marker=markers[i],markersize=markerSize,color=colors[i], linestyle='None',zorder=10,label=names[i]) plt.legend() plt.xlim([.42,.9]) pp.savefig() #plt.show() plt.close() my_plot.metal_vs_bv(bv_m,fits,'lithium',None,False,upper_lim=upper_lim,specific_clusters=[0,1,4,6,8,9]) plt.plot(bv,li,marker='s',markersize=markerSize,color='w',linestyle='None',zorder=9) for i in [0,1,2]: plt.plot(bv[i],np.log10(li[i]),marker=markers[i],markersize=markerSize,color=colors[i], linestyle='None',zorder=10,label=names[i]) plt.legend() pp.savefig() #plt.show() plt.close() baf_ca = baffles.age_estimator('calcium') baf_li = baffles.age_estimator('lithium') for i in [0,1]: print(names[i]) plt.plot([],[],'C0',linewidth=2,label='Final Age Posterior') p_li = baf_li.get_posterior(bv[i],li[i],bv_uncertainty=bv_err[i],measure_err=li_err[i],upperLim=False) p_ca = baf_ca.get_posterior(None,rhk[i]) product = prob.normalize(const.AGE,p_ca.array*p_li.array) prod_stats=prob.stats(const.AGE,product) my_plot.posterior(const.AGE, product, prod_stats,names[i],logPlot=False) plt.plot(const.AGE, p_ca.array,color='C3',label="Calcium Posterior") plt.plot(const.AGE, p_li.array,color='C2',label="Lithium Posterior") plt.axvspan(age_range[i][0],age_range[i][1], alpha=0.2, color='r', label=r'Literature age: %d - %d Myr' % tuple(age_range[i]),zorder=0) plt.axvline(x=mamaAge[i],color='C5',linestyle='--',label='MH08 age: %d' % mamaAge[i]) #plt.xlim([0,490]) plt.legend() pp.savefig() #plt.show() plt.close() print("%d Myr (68\\%%CI: %d - %d Myr)" % (utils.round_sigs(p_ca.stats[2],2), utils.round_sigs(p_ca.stats[1],2),utils.round_sigs(p_ca.stats[3],2))) print("%.1f Gyr (68\\%%CI: %.1f - %.1f Gyr)" % (p_li.stats[2]/1000,p_li.stats[1]/1000,p_li.stats[3]/1000)) print("%d Myr, with a 68\\%% confidence interval between %d Myr - %d Myr" % (utils.round_sigs(prod_stats[2],2), utils.round_sigs(prod_stats[1],2),utils.round_sigs(prod_stats[3],2))) print("TW PsA") plt.axvspan(age_range[-1][0],age_range[-1][1], alpha=0.2, color='r',zorder = 0) plt.axvspan(360-140,360+140, alpha=0.2, color='b',zorder=0) p_li = baf_li.get_posterior(bv[2],li[2],bv_uncertainty=bv_err[2],measure_err=li_err[2],upperLim=False) print("we report an age of %d Myr with a 68\\%% confidence interval between %d Myr - %d Myr\ (third panel of Fig. \\ref{fig:notable_stars}), consistent with Mamajek's lithium age,\ but a factor of $\sim$%.1f too young for his final adopted age." % (p_li.stats[2],p_li.stats[1],p_li.stats[3], 440/p_li.stats[2])) my_plot.posterior(const.AGE, p_li.array, p_li.stats,names[2],None,False, logPlot=False) plt.axvline(x=age[-1],color='r',label=r'Literature age: %d $\pm$ 40 Myr' % age[-1]) plt.axvline(x=360,color='b',label=r"M'12 Li age: 360 $\pm$ 140 Myr") plt.xlim([-30,510]) plt.legend() pp.savefig() #plt.show() plt.close() plt.axvline(x=age[-1],color='r',label=r'Literature age: %d $\pm$ 40 Myr' % age[-1]) plt.axvspan(age_range[-1][0],age_range[-1][1], alpha=0.2, color='r',zorder = 0) robs_f = robs_fomalhaut() plt.plot(const.AGE,robs_f(const.AGE),'k--',label='Nielsen 2019 Fomalhaut PDF') plt.plot(const.AGE,p_li.array,'g',label='BAFFLES Li posterior') y = prob.normalize(const.AGE,p_li.array*robs_f(const.AGE)) plt.plot(const.AGE,y,color = 'C0',linewidth=2,label='Final Age') stat = prob.stats(const.AGE,y) print("to get a final age for the system, $%d^{+%d}_{%d}$ Myr." % (stat[2],stat[3]-stat[2],stat[1]-stat[2])) plt.vlines(x=stat[2],ymin= 0,ymax= y[bisect.bisect_left(const.AGE,stat[2])], \ label='Final median age: %.3g Myr' % stat[2] ,color = 'orange') plt.fill_between(const.AGE,y, where= (const.AGE >= stat[1]) & (const.AGE <= stat[3]),color='.4', \ label='68%% CI: %.2g - %.2g' % (stat[1],stat[-2])) plt.title(names[2],size=my_plot.TITLE_SIZE) plt.xlim([0,1200]) plt.legend() plt.ylabel('Probability Density (Myr^-1)',size=my_plot.AXIS_LABEL_SIZE) plt.xlabel("Age (Myr)",size=my_plot.AXIS_LABEL_SIZE) plt.tight_layout() plt.minorticks_on() plt.tick_params(axis='both',which='both',right=True,top=True) pp.savefig() #plt.show() plt.close() printName(name) pp.close()
def baffles_age(bv=None, rhk=None, li=None, bv_err=None, li_err=None, upperLim=False, maxAge=None, fileName='baffles', pdfPage=None, showPlots=True, savePlots=False, savePostAsText=False): if (not rhk and not li): raise RuntimeError( "Must provide at least one of calcium logR'HK or lithium EW") if (li and not bv): raise RuntimeError("Must provide B-V value with lithium EW") if (not pdfPage and savePlots): pdfPage = PdfPages(fileName + '.pdf') p = None if (rhk): baf = age_estimator('calcium') p = baf.get_posterior(bv, rhk, pdfPage, showPlots, bv_err, li_err, upperLim=None, maxAge=maxAge, mamajekAge=utils.getMamaAge(rhk)) if (savePostAsText): np.savetxt(fileName + "_calcium.csv", list(zip(const.AGE, p.array)), delimiter=",") print("Ca Median Age: %.3g Myr, 68%% CI: %.3g - %.3g Myr, 95%% CI: %.3g - %.3g Myr" \ % (p.stats[2],p.stats[1],p.stats[3],p.stats[0],p.stats[4])) p2 = None if (li): baf2 = age_estimator('lithium') p2 = baf2.get_posterior(bv, li, pdfPage, showPlots, bv_err, li_err, upperLim=upperLim, maxAge=maxAge) if (savePostAsText): np.savetxt(fileName + "_lithium.csv", list(zip(const.AGE, p2.array)), delimiter=",") if p2.upperLim: print("1 sig lower-lim: %.3g Myr, 2 sig lower-lim: \ %.3g Myr, 3 sig: %.3g Myr" % (p2.stats[2], p2.stats[1], p2.stats[0])) else: print( "Li Median Age: %.3g Myr, 68%% CI: %.3g - %.3g Myr, 95%% CI: \ %.3g - %.3g Myr" % (p2.stats[2], p2.stats[1], p2.stats[3], p2.stats[0], p2.stats[4])) p3 = None if (p and p2): title = 'Final Posterior' y = p.array * p2.array prob.normalize(const.AGE, y) stats = prob.stats(const.AGE, y) my_plot.posterior(const.AGE, y, prob.stats(const.AGE, y), title, pdfPage, showPlots) p3 = posterior() p3.array, p3.stats = y, stats print("Final Median Age: %.3g Myr, 68%% CI: %.3g - %.3g, 95%% CI: %.3g - %.3g" \ % (stats[2],stats[1],stats[3],stats[0],stats[4])) if (savePostAsText): np.savetxt(fileName + "_product.csv", list(zip(const.AGE, y)), delimiter=",") if pdfPage: pdfPage.close() if p3 is not None: return p3 return p if p is not None else p2
def fit_histogram(metal, residual_arr=None, fromFile=True, saveToFile=False): if fromFile: [x, pdf, cdf] = np.load(join('grids', metal + '_likelihood_fit.npy')) return piecewise(x, pdf), piecewise(x, cdf) const = utils.init_constants(metal) assert residual_arr is not None or fromFile, "Must provide residuals if not \ reading from file" mu = np.mean(residual_arr) sigma = np.std(residual_arr) x = np.linspace( np.min(residual_arr) - .5, np.max(residual_arr) + .5, 1000) #1000 for linear? lim = 2 if metal == 'calcium': #lim = 5 lim = 1 x = np.linspace( np.min(residual_arr) - .5, np.max(residual_arr) + .1, 800) before, after = np.linspace(-lim, min(x), 50), np.linspace(max(x), lim, 50) x = np.concatenate((before, x, after)) cdf = np.array([(residual_arr < n).sum() for n in x], dtype='float') cdf /= cdf[-1] if metal == 'calcium': smoothed = savgol_filter(cdf, 145, 3) smoothed = savgol_filter(smoothed, 55, 3) else: smoothed = savgol_filter(cdf, 85, 3) smoothed = savgol_filter(smoothed, 55, 3) #smoothed = savgol_filter(cdf, 55, 3) #smoothed = savgol_filter(smoothed, 25, 3) #smoothed = savgol_filter(smoothed, 9, 3) pdf = np.gradient(smoothed) prob.normalize(x, pdf) inds = np.nonzero(pdf > max(pdf) / 2)[0] #inds = np.nonzero(pdf > 1.5)[0] if metal=='lithium' else \ # np.nonzero(pdf > max(pdf)/2)[0] i, j = inds[0], inds[-1] def exp_fit(x, a, b, c): return a * np.exp(b * x + c) popt, pcov = curve_fit(exp_fit, x[:i], pdf[:i], p0=[5, 5, -1]) pdf[:i] = exp_fit(x[:i], *popt) popt, pcov = curve_fit(exp_fit, x[j:], pdf[j:], p0=[.5, -9, 2.5]) pdf[j:] = exp_fit(x[j:], *popt) pdf = savgol_filter(pdf, 9, 3) if metal == 'calcium': m, n = np.nonzero(pdf >= 0.32)[0][0], np.nonzero(pdf >= 0.45)[0][0] pdf[m:n] = piecewise([x[m], x[n]], [pdf[m], pdf[n]])(x[m:n]) pdf = savgol_filter(pdf, 21, 3) pdf[:2] = [0, 0] pdf[-2:] = [0, 0] prob.normalize(x, pdf) cdf = integrate.cumtrapz(pdf, x=x, initial=0) cdf /= cdf[-1] if saveToFile: np.save(join('grids', metal + '_likelihood_fit'), [x, pdf, cdf]) return piecewise(x, pdf), piecewise(x, cdf)
def baffles_vs_mamajek(bv_rhk, fits, i, pdfPage=None, showPlots=False, title=None, mamaProduct=False): import ca_constants as const baf = baffles.age_estimator('calcium') #baf.make_grids(bv_rhk,fits,omit_cluster=i) my_ages = [] my_error = [] mamajek_ages = [] post_prod = 0 for j in range(len(bv_rhk[i][0])): b, r = bv_rhk[i][0][j], bv_rhk[i][1][j] mamajek_ages.append(utils.getMamaAge(r)) post = baf.get_posterior(b, r) post_prod += np.log(post.array) stat = post.stats my_ages.append(stat[2]) my_error.append((stat[2] - stat[1], stat[3] - stat[2])) post_prod = prob.normalize(const.AGE, np.exp(post_prod)) baffles_age = prob.stats(const.AGE, post_prod)[2] plt.Line2D([0], [0], color='C%d' % i, marker=const.MARKERS[i], label=const.CLUSTER_NAMES[i]) plt.axis([.4, 14000, .4, 14000]) plt.title(const.CLUSTER_NAMES[i], size=TITLE_SIZE) plt.xlabel(r'Age derived from M & H (2008)', size=AXIS_LABEL_SIZE) plt.ylabel(u'BAFFLES Age (Myr)', size=AXIS_LABEL_SIZE) for j in range(len(my_ages)): if (j == 0): plt.errorbar(mamajek_ages[j], my_ages[j], np.array([my_error[j]]).T, color=const.COLORS[i], marker=const.MARKERS[i]) else: plt.errorbar(mamajek_ages[j], my_ages[j], np.array([my_error[j]]).T, color=const.COLORS[i], marker=const.MARKERS[i]) plt.hlines(y=baffles_age,xmin= 0,xmax= baffles_age, \ label='BAFFLES cluster age: %.3g Myr' % baffles_age ,linestyle='--',color = 'C0') if mamaProduct: age = utils.getMamaProductAge(bv_rhk[i][1]) plt.vlines(x=age,ymin= 0,ymax= age, \ label='M & H (2008): %.3g Myr' % age ,linestyle='--',color = 'C2') plt.plot([0, 10000], [0, 10000], dashes=[2, 2], color='k') plt.plot(const.CLUSTER_AGES[i], const.CLUSTER_AGES[i], marker='*', markersize=18, color='C1', linestyle='None', label='Isochronal cluster age: %d Myr' % const.CLUSTER_AGES[i], alpha=1, zorder=10) ax = plt.gca() ax.set_yscale('log') ax.set_xscale('log') plt.legend(loc=2) plt.tight_layout() plt.minorticks_on() plt.tick_params(axis='both', which='both', right=True, top=True) if (pdfPage): pdfPage.savefig() if (showPlots): plt.show() if pdfPage: plt.close()
def make_table(MR=False): ca_const = utils.init_constants('calcium') li_const = utils.init_constants('lithium') empty = '' """ table = [] #[Object,RA,Dec,Sp Type,B-V,R'HK,Li EW,Source] #first read in all the 4 tables and create a single big table, which then I sort and merge t = np.genfromtxt('data/nielsen_2010_table2.csv',delimiter=',',dtype=str,skip_header=1) for row in t: if not utils.isFloat(row[1]) or not (.45 <= float(row[1]) <= 1.9): continue arr = [] arr.append(row[21].strip()) ra,dec = ra_dec(row[22]) arr.append(ra) arr.append(dec) arr.append(row[4].strip()) arr.append(row[1]) arr.append(row[13]) arr.append(row[7]) arr.append("1") if arr[0] == '' or not (utils.isFloat(arr[5]) or utils.isFloat(arr[6])): continue table.append(arr) bv_to_teff = my_fits.magic_table_convert('bv','teff') t = np.genfromtxt('data/brandt_2014_table.csv',delimiter=',',dtype=str,skip_header=2) for row in t: bv = None if utils.isFloat(row[2]) and utils.isFloat(row[3]): bv = float(row[2]) - float(row[3]) if bv is None or not (.45 <= bv <= 1.9): continue arr = [] arr.append(row[14].strip()) ra,dec = ra_dec(row[15]) arr.append(ra) arr.append(dec) arr.append(row[4].strip()) arr.append("%f" % bv) arr.append(row[7]) if row[9].find('A') != -1: nli = float(row[9].split()[-1]) teff = bv_to_teff(bv) ew = 10** my_fits.teff_nli_to_li([teff],[nli])[0] arr.append("%d" % ew) elif utils.isFloat(row[9]): arr.append(row[9]) else: arr.append(empty) arr.append("2") if arr[0] == '' or not (utils.isFloat(arr[5]) or utils.isFloat(arr[6])): continue table.append(arr) t = np.genfromtxt("data/nearbyStars_Boro_Saikia_2018.txt",delimiter='\t',dtype=str,skip_header=58) for row in t: if not utils.isFloat(row[5]) or not (.45 <= float(row[5]) <= 1.9): continue arr = [] arr.append(row[16].strip()) ra,dec = ra_dec(row[17]) arr.append(ra) arr.append(dec) arr.append(row[18].strip()) arr.append(row[5]) arr.append(row[10]) arr.append(empty) arr.append("3") if arr[0] == '' or not (utils.isFloat(arr[5]) or utils.isFloat(arr[6])): continue table.append(arr) t = np.genfromtxt("data/guillot_2009_li_survey.txt",delimiter='\t',dtype=str,skip_header=77) for row in t: if not utils.isFloat(row[7]) or not (.45 <= float(row[7]) <= 1.9): continue arr = [] arr.append(row[22].strip()) ra,dec = ra_dec(row[23]) arr.append(ra) arr.append(dec) arr.append(row[24].strip()) arr.append(row[7]) arr.append(empty) arr.append(row[16]) arr.append("4") if arr[0] == '' or not (utils.isFloat(arr[5]) or utils.isFloat(arr[6])): continue table.append(arr) table = np.array(table) name_sorted = table[table[:,0].argsort()] thinned = [] #averaging b-v,measurements, sources as 1,4 for name in set(name_sorted[:,0]): subset = name_sorted[name_sorted[:,0]==name] if len(subset) == 1: thinned.append(subset[0]) else: arr = copy.deepcopy(subset[0]) arr[4] = average(subset[:,4]) arr[5] = average(subset[:,5]) arr[6] = average(subset[:,6]) x = list(set(subset[:,7])) x.sort() arr[7] = ','.join(x) thinned.append(arr) thinned = np.array(thinned) final_table = thinned[thinned[:,1].argsort()] np.save("final_table",final_table) exit() """ final_table = np.load("data/merged_nielsen_brandt_saikia_guillot.npy") delimiterMR = ',' baf_li = baffles.age_estimator('lithium') baf_ca = baffles.age_estimator('calcium') #[Object,RA,Dec,Sp Type,B-V,R'HK,Li EW,Source] f = open("baffles_table2_latex.txt", 'w+') fMR = open("baffles_table2.csv", 'w+') cdf = ['2.5%', '16%', '50%', '84%', '97.5%'] column_head = [ 'Name', 'RA', 'Dec', 'Sp. Type', 'B-V', "logR'HK", 'Li EW', 'Ref.' ] column_head += ["R'HK Age at CDF=" + x for x in cdf] column_head += ["Li EW Age at CDF=" + x for x in cdf] column_head += ["Final Age at CDF=" + x for x in cdf] units = [ '', 'h m s', 'h m s', '', 'mags', " ", 'mA', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '' ] fMR.write(delimiterMR.join(column_head)) fMR.write('\n') fMR.write(delimiterMR.join(units)) fMR.write('\n') for row in final_table: arr = [] arrMR = [] arr += [x.replace('V* ', '').replace('_', '-') for x in row[0:4]] arrMR += [x.replace('$', '').replace('V* ', '') for x in row[0:4]] bv = float(row[4]) arr.append("%.2f" % bv) arrMR.append("%.3g" % bv) p_ca, p_li = None, None if utils.isFloat(row[5]): rhk = float(row[5]) arr.append('$%.2f$' % rhk) arrMR.append('%.3f' % rhk) if ca_const.inRange(bv, rhk): p_ca = baf_ca.get_posterior(bv, rhk, showPlot=False) else: arr.append(empty) arrMR.append(empty) ew = None if utils.isFloat(row[6]): ew = float(row[6]) arr.append('%d' % ew) arrMR.append('%g' % ew) else: arr.append(empty) arrMR.append(empty) arr.append(row[7]) arrMR.append(row[7].replace(',', ';')) if bv is not None and ew is not None and ew > 0 and li_const.inRange( bv, np.log10(ew)): p_li = baf_li.get_posterior(bv, ew, showPlot=False) if p_ca is not None: arr += printStats(p_ca.stats) arrMR += printStats(p_ca.stats, MR=True) else: arr += [empty] * 5 arrMR += [empty] * 5 if p_li is not None: arr += printStats(p_li.stats) arrMR += printStats(p_li.stats, MR=True) else: arr += [empty] * 5 arrMR += [empty] * 5 if p_ca is None and p_li is None: continue if p_ca is not None and p_li is not None: prod = p_ca.array * p_li.array prob.normalize(ca_const.AGE, prod) stats = prob.stats(ca_const.AGE, prod) arr += printStats(stats) arrMR += printStats(stats, MR=True) elif p_ca is not None: arr += printStats(p_ca.stats) arrMR += printStats(p_ca.stats, MR=True) elif p_li is not None: arr += printStats(p_li.stats) arrMR += printStats(p_li.stats, MR=True) else: arr += [empty] * 5 arrMR += [empty] * 5 f.write(' & '.join(arr) + " \\\\") f.write('\n') fMR.write(delimiterMR.join(arrMR)) fMR.write('\n') f.close() fMR.close()