コード例 #1
0
ファイル: baffles.py プロジェクト: adamstanfordmoore/BAFFLES
    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
コード例 #2
0
ファイル: baffles.py プロジェクト: adamstanfordmoore/BAFFLES
    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
コード例 #3
0
ファイル: baffles.py プロジェクト: adamstanfordmoore/BAFFLES
    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
コード例 #4
0
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()
コード例 #5
0
ファイル: baffles.py プロジェクト: adamstanfordmoore/BAFFLES
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