def cofe(self,save=False,texcmd=False): """ Plots C/O as a function of Fe/H save - save the file """ p = getelnum.Getelnum('O') cmd0 = 'SELECT distinct(mystars.oid),'+\ ' mystars.o_abund,mystars.c_abund,mystars.fe_abund '+\ ' FROM mystars LEFT JOIN exo ON exo.oid = mystars.oid '+\ ' WHERE '+postfit.globcut('C')+' AND '+postfit.globcut('O') qtype= [('oid','|S10'),('o_abund',float),('c_abund',float),('feh',float)] #Grab Planet Sample cmd = cmd0 +' AND exo.name IS NOT NULL' self.cur.execute(cmd) #pull arrays and subtract solar offset arrhost = np.array( self.cur.fetchall() ,dtype=qtype) nhost = len(arrhost) #Grab Comparison Sample cmd = cmd0 +' AND exo.name IS NULL' self.cur.execute(cmd) #pull arrays and subtract solar offset arrcomp = np.array( self.cur.fetchall() ,dtype=qtype) ncomp = len(arrcomp) #calculate C/O logeps(c) - logeps(o) c2ohost = 10**(arrhost['c_abund']-arrhost['o_abund']) c2ocomp = 10**(arrcomp['c_abund']-arrcomp['o_abund']) f = plt.figure( figsize=(6,4) ) ax = plt.subplot(111) ax.plot(arrcomp['feh'],c2ocomp,'bo') ax.plot(arrhost['feh'],c2ohost,'go') ax.legend(('Comparision','Hosts'),loc='best') ax.set_xlabel('[Fe/H]') ax.set_ylabel('C/O') c2o = 10**(self.stats['C']-self.stats['O']) yerr = np.array([s.std_dev() for s in c2o]).reshape((2,1)) ax = self.errpt(ax,(0.1,0.7),xerr=p.feherr,yerr=yerr) ax.set_ybound(0,ax.get_ylim()[1]) ax.axhline(1.) plt.show() if save: plt.savefig('Thesis/pyplots/cofe.ps') return ax
def __init__(self): ###Pull up database. self.conn = sqlite3.connect(os.environ['STARSDB']) self.cur = self.conn.cursor() elements = ['O','C'] self.stats = {} for elstr in elements: cmd = 'SELECT %s_abund,%s_staterrlo,%s_staterrhi FROM mystars '\ % (elstr,elstr,elstr) wcmd = ' WHERE '+postfit.globcut(elstr) self.cur.execute(cmd+wcmd) temptype =[('abund',float),('staterrlo',float),('staterrhi',float)] arr = np.array(self.cur.fetchall(),dtype=temptype) m,slo,shi = np.mean(arr['abund']),np.abs(np.median(arr['staterrlo'])),np.median(arr['staterrhi']) self.stats[elstr] = unumpy.uarray(([m,m],[slo,shi]))
def compmany(self,elstr='o'): if elstr == 'o': tables = ['mystars','luckstars','ramstars'] if elstr =='c': tables = ['mystars','luckstars','red06'] ncomp = len(tables) for i in range(ncomp): for j in range(ncomp): if i != j: tabx = tables[i] taby = tables[j] colx = tabx+'.'+elstr+'_abund' coly = taby+'.'+elstr+'_abund' cut = ' WHERE '+tabx+'.oid = '+taby+'.oid '+'AND '+\ colx+' IS NOT NULL AND '+coly+' IS NOT NULL ' if tabx == 'mystars' or taby == 'mystars': cut = cut+' AND '+postfit.globcut(elstr) ax = plt.subplot(ncomp,ncomp,i*ncomp+j+1) cmd = 'SELECT DISTINCT '+colx+','+coly+' FROM '+tabx+','+taby+cut self.cur.execute(cmd) arr = np.array(self.cur.fetchall()) if len(arr) > 0: (x,y) = arr[:,0],arr[:,1] ax.scatter(x-x.mean(),y-y.mean()) ax.set_xlabel(tabx) ax.set_ylabel(taby) xlim = ax.get_xlim() x = np.linspace(xlim[0],xlim[1],10) ax.plot(x,x,color='red') plt.draw() self.conn.close()
def feh(self,save=False,noratio=False): """ Show the trends of X/Fe as a function of Fe/H. save - saves the file noratio - plots X/H as opposed to X/Fe """ #pull in fitted abundances from tfit lines = [6300,6587] nel = len(lines) flags = ['dn','dk'] binmin = -0.5 binwid = 0.1 nbins = 11 qtype= [('abund',float),('feh',float),('staterrlo',float), ('staterrhi',float),('pop_flag','|S10')] bins = np.linspace(binmin,binmin+binwid*nbins,nbins) subplot = ((1,2)) f = plt.figure( figsize=(6,6) ) f.subplots_adjust(hspace=0.0001) ax = [] for i in range(nel): if i is 0: ax.append(plt.subplot(nel,1,i+1)) else: ax.append(plt.subplot(nel,1,i+1,sharex=ax[0])) p = getelnum.Getelnum(lines[i]) elstr = p.elstr cmd = 'SELECT %s_abund,fe_abund,%s_staterrlo,%s_staterrhi,pop_flag FROM mystars ' % (elstr,elstr,elstr) wcmd = ' WHERE '+postfit.globcut(elstr)+' AND pop_flag = "dn"' self.cur.execute(cmd+wcmd) arrthin = np.array(self.cur.fetchall(),dtype=qtype) arrthin['abund'] -= p.abnd_sol wcmd = ' WHERE '+postfit.globcut(elstr)+' AND pop_flag = "dk"' self.cur.execute(cmd+wcmd) arrthick = np.array(self.cur.fetchall(), dtype=qtype) arrthick['abund'] -= p.abnd_sol if noratio: ythin = arrthin['abund'] ythick = arrthick['abund'] ax[i].set_ylabel('[%s/H]' % (elstr) ) savename = 'xonh' else: ythin = arrthin['abund'] -arrthin['feh'] ythick = arrthick['abund'] -arrthick['feh'] ax[i].set_ylabel('[%s/Fe]' % (elstr) ) savename = 'xonfe' ### Compute Avg Thin disk in bins ### binind = np.digitize(arrthin['feh'],bins) binx,biny = [] , [] for j in np.arange(nbins-1)+1: ind = (np.where(binind == j))[0] midbin = binmin+binwid*(j-0.5) binmean = np.mean(ythin[ind]) nbin = len(ythin[ind]) # numer of points in a bin mederr = 0.5*(arrthin['staterrhi'][ind]-arrthin['staterrlo'][ind]) pull = (ythin[ind] - binmean)/mederr binx.append(midbin) biny.append(binmean) print 'Bin %.2f: n = %i, stdpull = %.2f ' % (midbin,nbin,np.std(pull)) binx,biny = np.array(binx),np.array(biny) ax[i].plot(arrthin['feh'],ythin,'bo') ax[i].plot(arrthick['feh'],ythick,'go') ax[i].plot(binx,biny,'rx-',lw=2,ms=5,mew=2) #plot typical errorbars yerr = np.array([s.std_dev() for s in self.stats[elstr]]) if not noratio: yerr = np.sqrt(yerr**2 + p.feherr**2) yerr = yerr.reshape((2,1)) ax[i] = self.errpt(ax[i],(0.9,0.9),xerr=p.feherr,yerr=yerr) if i is nel-1: ax[i].set_xlabel('[Fe/H]') else: ax[i].set_xticklabels('',visible=False) #remove overlapping zeroes yticks = ax[i].get_yticks()[2:] #hack ax[i].set_yticks(yticks) leg = ax[0].legend( ('Thin Disk','Thick Disk','Binned Thin Disk'), loc='best') if save: plt.savefig('Thesis/pyplots/%s.ps' % savename)
def exo(self,save=False,prob=True,texcmd=False): """ Show a histogram of Carbon and Oxygen for planet harboring stars, and comparison stars. """ f = plt.figure( figsize=(6,8) ) f.subplots_adjust(hspace=0.0001) ax1 = plt.subplot(211) elements = ['O','C','Fe'] nel = len(elements) sol_abnd = [8.7,8.5,0] ax = [] #empty list to store axes outex = [] statdict = { 'host':{},'comp':{} } for i in range(nel): #loop over the different elements if i is 0: ax.append(plt.subplot(nel,1,i+1)) else: ax.append(plt.subplot(nel,1,i+1,sharex=ax[0])) elstr = elements[i] if elstr is 'Fe': cmd0 = 'SELECT distinct(mystars.oid),mystars.'+elstr+'_abund '+\ ' FROM mystars LEFT JOIN exo ON exo.oid = mystars.oid WHERE ' else: cmd0 = 'SELECT distinct(mystars.oid),mystars.'+elstr+'_abund '+\ ' FROM mystars LEFT JOIN exo ON exo.oid = mystars.oid '+\ ' WHERE '+postfit.globcut(elstr)+' AND ' #Grab Planet Sample cmd = cmd0 +' exo.name IS NOT NULL' self.cur.execute(cmd) #pull arrays and subtract solar offset arrhost = np.array( self.cur.fetchall() ,dtype=float)[:,1] - sol_abnd[i] nhost = len(arrhost) #Grab Comparison Sample cmd = cmd0 +' exo.name IS NULL' self.cur.execute(cmd) #pull arrays and subtract solar offset arrcomp = np.array( self.cur.fetchall() ,dtype=float)[:,1] - sol_abnd[i] ncomp = len(arrcomp) binwid = 0.1 rng = [-0.5,0.5] nbins = (rng[1]-rng[0])/binwid if prob: exohist,bins = np.histogram(arrhost,bins=nbins,range=rng) comphist,bins = np.histogram(arrcomp,bins=nbins,range=rng) exohisterr = unumpy.uarray( (exohist,np.sqrt(exohist))) comphisterr = unumpy.uarray( (comphist,np.sqrt(comphist))) ratio = exohisterr/(comphisterr+exohisterr)*100. y = unumpy.nominal_values(ratio) yerr = unumpy.std_devs(ratio) x = bins[:-1]+0.5*binwid ax[i].hist(x,bins=bins,weights=y) ax[i].errorbar(x,y,yerr=yerr,marker='o',elinewidth=2,ls='None') else: # Make histogram ax[i].hist([arrcomp,arrhost], bins=nbins,range=rng, histtype='bar', label=['Comparison','Planet Hosts']) ax[i].legend(loc='upper left') if i is nel-1: ax[i].set_xlabel('[X/H]') ax[i].set_ylabel('% Stars with Planets') else: ax[i].set_xlabel('['+elstr+'/H]') ax[i].set_xticklabels('',visible=False) #remove overlapping zeroes yticks = ax[i].get_yticks()[1:] ax[i].set_yticks(yticks) #Annotate to show which lable we're on inv = ax[i].transData.inverted() txtpt = inv.transform( ax[i].transAxes.transform( (0.05,0.85) ) ) ax[i].annotate('[%s/H]' % elstr,txtpt) ######## Compute KS - statistic or probablity ######### mhost,shost,mcomp,scomp = np.mean(arrhost),np.std(arrhost),\ np.mean(arrcomp),np.std(arrcomp) D,p = stats.ks_2samp(arrhost,arrcomp) if save: # element number mean std ncomp compmean compstd KS p outex.append(r'$\text{[%s/H]}$ & %i & %.2f & %.2f & & %i & %.2f & %.2f & %s \\' % (elstr,nhost,mhost,shost,ncomp,mcomp,scomp,flt2tex.flt2tex(p,sigfig=1) ) ) else: print elstr+' in stars w/ planets: N = %i Mean = %f Std %f ' \ % (nhost,mhost,shost) print elstr+' in stars w/o planets: N = %i Mean = %f Std %f ' \ % (ncomp,mcomp,scomp) print 'KS Test: D = %f p = %f ' % (D,p) statdict['host'][elstr] = {'n':nhost,'m':mhost,'s':shost} statdict['comp'][elstr] = {'n':ncomp,'m':mcomp,'s':scomp} plt.draw() if save: plt.savefig('Thesis/pyplots/exo.ps') f = open('Thesis/tables/exo.tex','w') f.writelines(outex) if texcmd: return statdict
def comp(self,save=False,texcmd=False): """ Plots my results as a function of literature save - saves the file """ tables = [['ben05'],['luckstars']] offset = [[0],[8.5]] literr = [[0.06],[0.1]] lines = [6300,6587] color = ['blue','red','green'] f = plt.figure( figsize=(6,8) ) ax1 = plt.subplot(211) ax2 = plt.subplot(212) ax1.set_xlabel('[O/H], Bensby 2005') ax2.set_xlabel('[C/H], Luck 2006') ax1.set_ylabel('[O/H], This Work') ax2.set_ylabel('[C/H], This Work') ax = [ax1,ax2] ncomp = [] stdcomp = [] for i in [0,1]: xtot =[] #total array of comparison studies ytot =[] #total array of comparison studies p = getelnum.Getelnum(lines[i]) elstr = p.elstr abnd_sol = p.abnd_sol print abnd_sol for j in range(len(tables[i])): table = tables[i][j] #SELECT cmd = """ SELECT DISTINCT mystars.%s_abund,mystars.%s_staterrlo, mystars.%s_staterrhi,%s.%s_abund """ % (elstr,elstr,elstr,table,elstr) if table is 'luckstars': cmd += ','+table+'.c_staterr ' #FROM WHERE cmd +=""" FROM mystars,%s WHERE mystars.oid = %s.oid AND %s.%s_abund IS NOT NULL AND %s""" % (table,table,table,elstr,postfit.globcut(elstr)) if table is 'luckstars': cmd = cmd+' AND '+table+'.c_staterr < 0.3' self.cur.execute(cmd) arr = np.array(self.cur.fetchall()) x = arr[:,3] - offset[i][j] y = arr[:,0] -abnd_sol ###pull literature errors### if table is 'ben05': xerr = np.zeros( (2,len(x)) ) + 0.06 if table is 'luckstars': xerr = arr[:,4].tolist() xerr = np.array([xerr,xerr]) yerr = np.abs(arr[:,1:3]) print cmd n = len(x) ncomp.append(n) print str(n) + 'comparisons' ax[i].errorbar(x,y,xerr=xerr,yerr=yerr.transpose(),color=color[j], marker='o',ls='None',capsize=0,markersize=5) xtot.append( x.tolist() ) ytot.append( y.tolist() ) line = np.linspace(-3,3,10) xtot=np.array(xtot) ytot=np.array(ytot) symerr = (yerr[:,0]+yerr[:,1])/2. ax[i].plot(line,line) ax[i].set_xlim((-0.6,+0.6)) ax[i].set_ylim((-0.6,+0.6)) S = np.std(xtot[0]-ytot[0]) print S stdcomp.append(S) plt.draw() if texcmd: return ncomp,stdcomp if save: plt.savefig('Thesis/pyplots/comp.ps')
def abundhist(self,save=False,texcmd=False,uplim=False): """ Plot the distributions of abundances. Possibly not needed with the exo plot. save - save the plot texcmd - returns what the tex command dumper wants """ #pull in fitted abundances from tfit line = [6300,6587] subplot = ((1,2)) f = plt.figure( figsize=(6,6) ) f.subplots_adjust(hspace=0.0001) ax1 = plt.subplot(211) ax1.set_xticklabels('',visible=False) ax1.set_yticks(np.arange(0,200,50)) ax2 = plt.subplot(212,sharex=ax1) ax2.set_yticks(np.arange(0,200,50)) ax2.set_xlabel('[X/H]') ax = (ax1,ax2) nstars = [] outex = [] for i in range(2): p = getelnum.Getelnum(line[i]) elstr = p.elstr cut = postfit.globcut(elstr,uplim=uplim) cmd = 'SELECT %s_abund from MYSTARS WHERE %s'% (elstr,cut) self.cur.execute(cmd) out = self.cur.fetchall() abund = np.array(out,dtype=float).flatten()-p.abnd_sol ax[i].set_ylabel('Number of Stars') ax[i].hist(abund,range=(-1,1),bins=20,fc='gray') ax[i].set_ylim(0,200) #Annotate to show which lable we're on antxt = '[%s/H]' % elstr inv = ax[i].transData.inverted() txtpt = inv.transform( ax[i].transAxes.transform( (0.05,0.85) ) ) ax[i].annotate(antxt,txtpt) N,m,s,min,max = abund.size,abund.mean(), \ abund.std(),abund.min(),abund.max() nstars.append(N) if save: #output moments for tex write up outex.append(r'$\text {%s}$& %i & %.2f & %.2f & %.2f & %.2f\\' % (antxt,N,m,s,min,max)) else: print 'N, mean, std, min, max' + antxt print '(%i,%f,%f,%f,%f)' % (N,m,s,min,max) if texcmd: return nstars if save: plt.savefig('Thesis/pyplots/abundhist.ps') f = open('Thesis/tables/abundhist.tex','w') f.writelines(outex)
def dump(file='Thesis/texcmd.tex'): """ Dumps the short hands used in the latex paper. """ conn = sqlite3.connect(os.environ['STARSDB']) cur = conn.cursor() lines = [6300,6587] el = ['O','C'] f = open(file,'w') line = [] plotter = plotgen.Plotgen() ncomp,stdcomp = plotter.comp(texcmd=True) nstars= plotter.abundhist(texcmd=True) #number of different disk populations popnames = ['Thin','Thick','Halo','Bdr'] popsym = ['dn','dk','h','dn/dk'] npoptot = [] for i in range(len(popnames)): cmd = """ SELECT count(pop_flag) FROM mystars WHERE pop_flag = "%s" AND ((%s) OR (%s)) """ % (popsym[i],postfit.globcut('O'),postfit.globcut('C') ) print cmd cur.execute(cmd) npop = (cur.fetchall())[0][0] npoptot.append(npop) line.append(r'\nc{\n%s}{%i} %% # of stars in %s pop.' % (popnames[i],npop,popnames[i])) npoptot = np.array(npoptot).sum() line.append(r'\nc{\nPop}{%i} %% # of stars with pop prob' % npoptot) #Cuts specific to each line. for i in range(len(lines)): p = getelnum.Getelnum(el[i]) line.append(r'\nc{\vsiniCut%s}{%i} %% vsinicut for %s' % (el[i],p.vsinicut,el[i])) line.append(r'\nc{\teffCut%slo}{%i} %% teff for %s' % (el[i],p.teffrng[0],el[i])) line.append(r'\nc{\teffCut%shi}{%i} %% teff for %s' % (el[i],p.teffrng[1],el[i])) fitabund,x,x,abund = postfit.tfit(lines[i]) maxTcorr = max(np.abs(fitabund-abund)) line.append(r'\nc{\maxT%s}{%.2f} %% max temp correction %s' % (el[i],maxTcorr,el[i])) line.append(r'\nc{\nComp%s}{%i} %% # of comparison stars %s' % (el[i],ncomp[i],el[i])) line.append(r'\nc{\StdComp%s}{%.2f} %% std of comparison stars %s' % (el[i],stdcomp[i],el[i])) line.append(r'\nc{\nStars%s}{%i} %% Number of stars with %s analysis' % (el[i],nstars[i],el[i])) #Values sepecific to both lines line.append(r'\nc{\coThresh}{%.2f} %% Theshhold for high co' % (p.coThresh)) line.append(r'\nc{\scatterCut}{%.2f} %% Cut on the scatter' % (p.scattercut)) line.append(r'\nc{\teffSol}{%i} %% Solar Effective Temp' % (p.teff_sol)) statdict = plotter.exo(texcmd=True) for pop in statdict.keys(): for elstr in statdict[pop].keys(): for moment in statdict[pop][elstr].keys(): value = statdict[pop][elstr][moment] if moment[0] is 'n': line.append(r'\nc{\%s%s%s}{%i} %% %s - %s - %s'% (pop,elstr,moment,value,pop,elstr,moment)) else: line.append(r'\nc{\%s%s%s}{%.2f} %% %s - %s - %s'% (pop,elstr,moment,value,pop,elstr,moment)) statdict = table.dump_stars(texcmd=True) for key in statdict.keys(): value = statdict[key] if key[0] is 'n': #treat integers a certain way line.append(r'\nc{\%s}{%d} %% '% (key,value)) else: line.append(r'\nc{\%s}{%.2f} %% '% (key,value)) for l in line: l = l.replace('\nc','\newcommand') f.write(l+'\n') return line
def dump_stars(save=True,texcmd=False): conn = sqlite3.connect(os.environ['STARSDB']) cur = conn.cursor() elements = ['O','C'] cuts={} texdict = {} cuts['C'] = postfit.globcut('C',table='t3') cuts['O'] = postfit.globcut('O',table='t2') cmd0 = """ SELECT distinct(t1.name),t1.vmag,t1.d,t1.teff,t1.logg,t1.pop_flag,t1.monh,t1.ni_abund, t2.o_nfits,t2.o_abund,t2.o_staterrlo,t2.o_staterrhi, t3.c_nfits,t3.c_abund,t3.c_staterrlo,t3.c_staterrhi, exo.name FROM mystars t1 LEFT JOIN mystars t3 ON t1.id = t3.id AND (%s) LEFT JOIN mystars t2 ON t1.id = t2.id AND (%s) LEFT JOIN exo ON exo.oid = t1.oid WHERE (%s) OR (%s) ORDER BY t1.name """ % (cuts['C'],cuts['O'],cuts['C'],cuts['O']) temptype = [('name','|S10'),('vmag',float),('d',int),('teff',int), ('logg',float),('pop_flag','|S10'),('monh',float),('ni_abund',float), ('o_nfits',float),('o_abund',float),('o_staterrlo',float),('o_staterrhi',float), ('c_nfits',float),('c_abund',float),('c_staterrlo',float),('c_staterrhi',float), ('exoname','|S10')] cur.execute(cmd0) out = cur.fetchall()#,dtype=temptype) out = np.array(out,dtype=temptype) outstr = [] out['o_nfits'][np.where(np.isnan(out['o_nfits']))[0]] = 0 out['c_nfits'][np.where(np.isnan(out['c_nfits']))[0]] = 0 #subtract of solar abundance elements = ['O','C'] abund,errhi,errlo,abndone,logeps = {},{},{},{},{} c2oarr = np.array([]) for el in elements: p = getelnum.Getelnum(el) elo = el.lower() abund[el] = out['%s_abund' % elo] - p.abnd_sol logeps[el] = out['%s_abund' % elo] errhi[el] = np.abs(out['%s_staterrlo' % elo]) errlo[el] = out['%s_staterrhi' % elo] for i in range(len(out)): abundarr = np.array([abund['C'][i],abund['O'][i]]) if np.isnan(abundarr).any(): c2ostr = '$nan_{nan}^{+nan}$' else: for el in elements: cent = logeps[el][i] err = [errlo[el][i],errhi[el][i]] abndone[el] = unumpy.uarray(([cent,cent],err)) c2o = 10**(abndone['C']-abndone['O']) c2ostr = '$%.2f_{-%.2f}^{+%.2f}$' % (c2o[0].nominal_value,c2o[0].std_dev(),c2o[1].std_dev() ) c2oarr = np.append(c2oarr,c2o[0].nominal_value) o = out[i] planetstr = 'no' if o['exoname'] != 'None': planetstr = 'yes' #Indentation character a = '\\\[0ex] ' #Global parameters for stars a += '%s & %.2f & %d & %d & '%(o['name'],o['vmag'],o['d'],o['teff']) a += '%.2f & %s & %s &'%(o['logg'],o['pop_flag'],planetstr) #Metalicity and Nickle abundances a += '%.2f & %.2f & '%(o['monh'],o['ni_abund']) #Oxygen a += '%d & $%.2f_{%.2f}^{+%.2f}$ & '%(o['o_nfits'],abund['O'][i],o['o_staterrlo'],o['o_staterrhi']) #Carbon a += '%d & $%.2f_{%.2f}^{+%.2f}$ & '%(o['c_nfits'],abund['C'][i],o['c_staterrlo'],o['c_staterrhi']) #The Ratio a += '%s \\\ \n ' % c2ostr a = a.replace('$nan_{nan}^{+nan}$',r'\nd') a = a.replace('None',r'\nd') a = a.replace('nan',r'\nd') outstr.append(a) c2ohist = np.histogram(c2oarr,bins = [0.,p.coThresh,1000.])[0] # Total number of c2o measurments texdict['nPlanetTot'] = len(np.where(out['exoname'] != 'None')[0]) texdict['nStarsTot'] = len(out) texdict['ncoStarsTot'] = len(c2oarr) texdict['ncoLtThresh'],texdict['ncoGtThresh'] = tuple(c2ohist) texdict['coMin'],texdict['coMax'] = c2oarr.min(),c2oarr.max() if texcmd: return texdict if save: f = open('Thesis/tables/bigtable.tex','w') f.writelines(outstr)