Exemple #1
0
def lnprior(params, vsin_obs, sig_vsin_obs, plx_obs, sig_plx_obs, ranges,
            model, stellar_prior, npy_star, pdf_mas, pdf_obl, pdf_age, pdf_plx,
            pdf_ebv, grid_mas, grid_obl, grid_age, grid_plx, grid_ebv):

    if model == 'befavor':
        Mstar, oblat, Hfrac, cosi, plx, ebv = params[0], params[1],\
            params[2], params[3], params[4], params[5]
    if model == 'aara' or model == 'acol' or model == 'bcmi':
        Mstar, oblat, Hfrac, cosi, plx, ebv = params[0], params[1],\
            params[2], params[6], params[7], params[8]
    if model == 'beatlas':
        Mstar, oblat, Hfrac, cosi, plx, ebv = params[0], params[1],\
            0.3, params[4], params[5], params[6]

    # Reading Stellar Priors
    if stellar_prior is True:
        temp, idx_mas = find_nearest(grid_mas, value=Mstar)
        temp, idx_obl = find_nearest(grid_obl, value=oblat)
        temp, idx_age = find_nearest(grid_age, value=Hfrac)
        temp, idx_plx = find_nearest(grid_plx, value=plx)
        temp, idx_ebv = find_nearest(grid_ebv, value=ebv)
        chi2_stellar_prior = Mstar * pdf_mas[idx_mas] +\
            oblat * pdf_obl[idx_obl] + \
            Hfrac * pdf_age[idx_age] + \
            plx * pdf_plx[idx_plx] + \
            ebv * pdf_ebv[idx_ebv]
    else:
        chi2_stellar_prior = 0.0

    t = hfrac2tms(Hfrac)

    Rpole, logL, _ = geneva_interp_fast(Mstar, oblat, t)

    wcrit = np.sqrt(8. / 27. * G * Mstar * Msun / (Rpole * Rsun)**3)

    vsini = oblat2w(oblat) * wcrit * (Rpole * Rsun * oblat) *\
        np.sin(np.arccos(cosi)) * 1e-5

    chi2_vsi = (vsin_obs - vsini)**2 / sig_vsin_obs**2.

    chi2_plx = (plx_obs - plx)**2 / sig_plx_obs**2.

    chi2_prior = chi2_vsi + chi2_plx + chi2_stellar_prior

    return -0.5 * chi2_prior
Exemple #2
0
def plot_fit_last(par, lbd, logF, dlogF, minfo, listpar, lbdarr, logF_grid,
                  isig, dims, Nwalk, Nmcmc, ranges, include_rv, npy, log_scale,
                  model):
    '''
    Plots best model fit over data

    Usage:
    plot_fit(par, lbd, logF, dlogF, minfo, logF_grid, isig, Nwalk, Nmcmc)
    where
    par = np.array([Mstar, oblat, Sig0, Rd, n, cosi, dist])
    '''
    # model parameters
    if include_rv is True:
        if model == 'befavor':
            Mstar, oblat, Hfrac, cosi, dist, ebv, rv = par
        if model == 'befavor_new':
            Mstar, oblat, t, cosi, dist, ebv, rv = par
        if model == 'aara' or model == 'acol' or\
           model == 'bcmi':
            Mstar, oblat, Hfrac, Sig0, Rd, n, cosi, dist, ebv, rv = par
        if model == 'bcmi_pol' or model == 'aara_pol':
            Mstar, oblat, Hfrac, Sig0, Rd, n, cosi = par
        if model == 'beatlas':
            Mstar, oblat, Sig0, n, cosi, dist, ebv, rv = par
            Hfrac = 0.30
        lim = 3
        lim2 = 2
    else:
        if model == 'befavor':
            Mstar, oblat, Hfrac, cosi, dist, ebv = par
        if model == 'befavor_new':
            Mstar, oblat, t, cosi, dist, ebv = par
        if model == 'aara' or model == 'acol' or\
           model == 'bcmi':
            Mstar, oblat, Hfrac, Sig0, Rd, n, cosi, dist, ebv = par
        if model == 'bcmi_pol' or model == 'aara_pol':
            Mstar, oblat, Hfrac, Sig0, Rd, n, cosi = par
        if model == 'beatlas':
            Mstar, oblat, Sig0, n, cosi, dist, ebv = par
            Hfrac = 0.30

        lim = 2
        lim2 = 1
        rv = 3.1

    # Rpole, Lstar, Teff = vkg.geneve_par(Mstar, oblat, Hfrac, folder_tables)
    # t = np.max(np.array([hfrac2tms(hfrac), 0.]))
    if model != 'befavor_new':
        t = np.max(np.array([hfrac2tms(Hfrac), 0.]))
    Rpole, logL = geneva_interp_fast(Mstar,
                                     oblat,
                                     t,
                                     neighbours_only=True,
                                     isRpole=False)

    if model != 'bcmi_pol' and model != 'aara_pol':
        norma = (10. / dist)**2  # (Lstar*Lsun) / (4. * pi * (dist*pc)**2)
    uplim = dlogF == 0
    keep = np.logical_not(uplim)

    # ***
    chain = np.load(npy)
    par_list = chain[:, -1, :]

    # interpolate model
    if model == 'beatlas':
        logF_mod = griddataBAtlas(minfo, logF_grid, par[:-lim], listpar, dims,
                                  isig)
    elif model == 'befavor_new':
        # print(isig, par[:-lim])
        logF_mod = bat.griddataBA(minfo,
                                  logF_grid,
                                  par[:-lim],
                                  isig=isig,
                                  silent=True)
    elif model == 'bcmi_pol' or model == 'aara_pol':
        logF_mod = griddataBA(minfo, logF_grid, par, listpar, dims)
    else:
        logF_mod = griddataBA(minfo, logF_grid, par[:-lim], listpar, dims)

    logF_list = np.zeros([len(par_list), len(logF_mod)])
    chi2 = np.zeros(len(logF_list))
    for i in range(len(par_list)):
        if model == 'beatlas':
            logF_list[i] = griddataBAtlas(minfo, logF_grid, par_list[i, :-lim],
                                          listpar, dims, isig)
        elif model == 'befavor_new':
            logF_list[i] = bat.griddataBA(minfo,
                                          logF_grid,
                                          par_list[i, :-lim],
                                          isig=isig,
                                          silent=True)
        elif model == 'bcmi_pol' or model == 'aara_pol':
            logF_list[i] = griddataBA(minfo, logF_grid, par_list[i, :],
                                      listpar, dims)
        else:
            logF_list[i] = griddataBA(minfo, logF_grid, par_list[i, :-lim],
                                      listpar, dims)

    # convert to physical units
    if model != 'bcmi_pol' and model != 'aara_pol':
        logF_mod += np.log10(norma)
        logF_list += np.log10(norma)
    for j in range(len(logF_list)):
        chi2[j] = np.sum(
            (logF[keep] - logF_list[j][keep])**2 / (dlogF[keep])**2)

    flux = 10.**logF
    dflux = dlogF * flux
    flux_mod = 10.**logF_mod
    if model != 'bcmi_pol' and model != 'aara_pol':
        flux_mod = pyasl.unred(lbd * 1e4, flux_mod, ebv=-1 * ebv, R_V=rv)

    # Plot definitions
    bottom, left = 0.79, 0.51  #0.80, 0.48  # 0.75, 0.48
    width, height = 0.96 - left, 0.97 - bottom
    plt.axes([left, bottom, width, height])

    # plot fit
    if log_scale is True:
        for i in range(len(par_list)):
            if model != 'bcmi_pol' and model != 'aara_pol':
                ebv_temp = np.copy(par_list[i][-lim2])
                F_temp = pyasl.unred(lbd * 1e4,
                                     10**logF_list[i],
                                     ebv=-1 * ebv_temp,
                                     R_V=rv)
                plt.plot(lbd, lbd * F_temp, color='gray', alpha=0.1)

            else:
                F_temp = pyasl.unred(lbd * 1e4,
                                     10**logF_list[i],
                                     ebv=0,
                                     R_V=rv)
                plt.plot(lbd, F_temp, color='gray', alpha=0.1)

        if model != 'bcmi_pol' and model != 'aara_pol':
            plt.plot(lbd,
                     lbd * flux_mod,
                     color='red',
                     ls='-',
                     lw=3.5,
                     alpha=0.4,
                     label='$\mathrm{Best\, fit}$')
            plt.errorbar(lbd,
                         lbd * flux,
                         yerr=lbd * dflux,
                         ls='',
                         marker='o',
                         alpha=0.5,
                         ms=10,
                         color='blue',
                         linewidth=2)
        else:
            plt.plot(lbd,
                     flux_mod,
                     color='red',
                     ls='-',
                     lw=3.5,
                     alpha=0.4,
                     label='$\mathrm{Best\, fit}$')
            plt.errorbar(lbd,
                         flux,
                         yerr=dflux,
                         ls='',
                         marker='o',
                         alpha=0.5,
                         ms=10,
                         color='blue',
                         linewidth=2)

        if model != 'bcmi_pol' and model != 'aara_pol':
            plt.ylabel(
                r'$\lambda F_{\lambda}\,\mathrm{[erg\, s^{-1}\, cm^{-2}]}$',
                fontsize=17)
            plt.xscale('log')
        else:
            plt.ylabel(r'$P [\%]$', fontsize=17)
        plt.tick_params(labelbottom='off')

    else:

        for i in range(len(par_list)):
            if model != 'bcmi_pol' and model != 'aara_pol':
                ebv_temp = np.copy(par_list[i][-lim2])
                F_temp = pyasl.unred(lbd * 1e4,
                                     10**logF_list[i],
                                     ebv=-1 * ebv_temp,
                                     R_V=rv)
                plt.plot(lbd, lbd * F_temp, color='gray', alpha=0.1)
            else:
                F_temp = pyasl.unred(lbd * 1e4,
                                     10**logF_list[i],
                                     ebv=0,
                                     R_V=rv)
                plt.plot(lbd, F_temp, color='gray', alpha=0.1)

        if model != 'bcmi_pol' and model != 'aara_pol':
            plt.plot(lbd,
                     lbd * flux_mod,
                     color='red',
                     ls='-',
                     lw=3.5,
                     alpha=0.4,
                     label='$\mathrm{Best\, fit}$')
            plt.errorbar(lbd,
                         lbd * flux,
                         yerr=lbd * dflux,
                         ls='',
                         marker='o',
                         alpha=0.5,
                         ms=10,
                         color='blue',
                         linewidth=2)
            plt.ylabel(
                r'$\lambda F_{\lambda}\,\mathrm{[erg\, s^{-1}\, cm^{-2}]}$',
                fontsize=20)
        else:
            plt.plot(lbd,
                     flux_mod,
                     color='red',
                     ls='-',
                     lw=3.5,
                     alpha=0.4,
                     label='$\mathrm{Best\, fit}$')
            plt.errorbar(lbd,
                         flux,
                         yerr=dflux,
                         ls='',
                         marker='o',
                         alpha=0.5,
                         ms=10,
                         color='blue',
                         linewidth=2)
            plt.ylabel(r'$P [\%]$', fontsize=20)

        plt.tick_params(labelbottom='off')
        plt.xscale('linear')
    plt.xlim(min(lbd), max(lbd))
    if model != 'bcmi_pol' and model != 'aara_pol':
        plt.yscale('log')
    if model == 'aara' or model == 'beatlas' or\
       model == 'acol' or model == 'bcmi':
        plt.xscale('log')

    if log_scale is True:
        plt.xscale('log')
    else:
        plt.xscale('linear')
    if model == 'bcmi_pol' or model == 'aara_pol':
        plt.xlim(0.9 * min(lbd), 1.1 * max(lbd))

    plt.tick_params(direction='in',
                    length=6,
                    width=2,
                    colors='gray',
                    which='both')
    plt.legend(loc='upper right')

    return
Exemple #3
0
def plot_residuals(par, lbd, logF, dlogF, minfo, listpar, lbdarr, logF_grid,
                   isig, dims, Nwalk, Nmcmc, ranges, include_rv, npy,
                   log_scale, model):
    '''
    Plots best model fit over data

    Usage:
    plot_fit(par, lbd, logF, dlogF, minfo, logF_grid, isig, Nwalk, Nmcmc)
    where
    par = np.array([Mstar, oblat, Sig0, Rd, n, cosi, dist])
    '''
    # model parameters
    if include_rv is True:
        if model == 'befavor':
            Mstar, oblat, Hfrac, cosi, dist, ebv, rv = par
        if model == 'befavor_new':
            Mstar, oblat, t, cosi, dist, ebv, rv = par
        if model == 'aara' or model == 'acol' or\
           model == 'bcmi':
            Mstar, oblat, Hfrac, Sig0, Rd, n, cosi, dist, ebv, rv = par
        if model == 'bcmi_pol' or model == 'aara_pol':
            Mstar, oblat, Hfrac, Sig0, Rd, n, cosi = par
        if model == 'beatlas':
            Mstar, oblat, Sig0, n, cosi, dist, ebv, rv = par
            Hfrac = 0.30
        lim = 3
        lim2 = 2
    else:
        if model == 'befavor':
            Mstar, oblat, Hfrac, cosi, dist, ebv = par
        if model == 'befavor_new':
            Mstar, oblat, t, cosi, dist, ebv = par
        if model == 'aara' or model == 'acol' or model == 'bcmi':
            Mstar, oblat, Hfrac, Sig0, Rd, n, cosi, dist, ebv = par
        if model == 'bcmi_pol' or model == 'aara_pol':
            Mstar, oblat, Hfrac, Sig0, Rd, n, cosi = par
        if model == 'beatlas':
            Mstar, oblat, Sig0, n, cosi, dist, ebv = par
            Hfrac = 0.3

        lim = 2
        lim2 = 1
        rv = 3.1

    # Rpole, Lstar, Teff = vkg.geneve_par(Mstar, oblat, Hfrac, folder_tables)
    # t = np.max(np.array([hfrac2tms(hfrac), 0.]))
    if model != 'befavor_new':
        t = np.max(np.array([hfrac2tms(Hfrac), 0.]))
    Rpole, logL = geneva_interp_fast(Mstar,
                                     oblat,
                                     t,
                                     neighbours_only=True,
                                     isRpole=False)

    if model != 'bcmi_pol' and model != 'aara_pol':
        norma = (10. / dist)**2  # (Lstar*Lsun) / (4. * pi * (dist*pc)**2)
    uplim = dlogF == 0
    keep = np.logical_not(uplim)

    # ***
    chain = np.load(npy)
    par_list = chain[:, -1, :]

    # interpolate models
    if model == 'beatlas':
        logF_mod = griddataBAtlas(minfo, logF_grid, par[:-lim], listpar, dims,
                                  isig)
    elif model == 'befavor_new':
        logF_mod = bat.griddataBA(minfo,
                                  logF_grid,
                                  par[:-lim],
                                  isig=isig,
                                  silent=True)
    elif model == 'bcmi_pol' or model == 'aara_pol':
        logF_mod = griddataBA(minfo, logF_grid, par, listpar, dims)
    else:
        logF_mod = griddataBA(minfo, logF_grid, par[:-lim], listpar, dims)

    logF_list = np.zeros([len(par_list), len(logF_mod)])
    chi2 = np.zeros(len(logF_list))
    for i in range(len(par_list)):
        if model == 'beatlas':
            logF_list[i] = griddataBAtlas(minfo, logF_grid, par_list[i, :-lim],
                                          listpar, dims, isig)
        elif model == 'befavor_new':
            logF_list[i] = bat.griddataBA(minfo,
                                          logF_grid,
                                          par_list[i, :-lim],
                                          isig=isig,
                                          silent=True)
        elif model == 'bcmi_pol' or model == 'aara_pol':
            logF_list[i] = griddataBA(minfo, logF_grid, par_list[i, :],
                                      listpar, dims)
        else:
            logF_list[i] = griddataBA(minfo, logF_grid, par_list[i, :-lim],
                                      listpar, dims)

    # convert to physical units
    if model != 'bcmi_pol' and model != 'aara_pol':
        logF_mod += np.log10(norma)
        logF_list += np.log10(norma)
    for j in range(len(logF_list)):
        chi2[j] = np.sum(
            (logF[keep] - logF_list[j][keep])**2 / (dlogF[keep])**2)

    # chib = chi2[np.argsort(chi2)[-30:]] / max(chi2)

    flux = 10.**logF
    dflux = dlogF * flux
    flux_mod = 10.**logF_mod
    if model != 'bcmi_pol' and model != 'aara_pol':
        flux_mod = pyasl.unred(lbd * 1e4, flux_mod, ebv=-1 * ebv, R_V=rv)

    # Plot definitions
    bottom, left = 0.73, 0.51
    width, height = 0.96 - left, 0.785 - bottom
    plt.axes([left, bottom, width, height])

    # plot fit
    for i in range(len(par_list)):
        if model != 'bcmi_pol' and model != 'aara_pol':
            ebv_temp = np.copy(par_list[i][-lim2])
            F_temp = pyasl.unred(lbd * 1e4,
                                 10**logF_list[i],
                                 ebv=-1 * ebv_temp,
                                 R_V=rv)
            plt.plot(lbd, (flux - F_temp) / dflux, 'bs', alpha=0.2)
        else:
            F_temp = pyasl.unred(lbd * 1e4, 10**logF_list[i], ebv=0, R_V=rv)
            plt.plot(lbd, (flux - F_temp) / dflux, 'bs', alpha=0.2)
    # plt.plot(lbd, (flux - flux_mod) / dflux, 'bs', markersize=5, alpha=0.1)
    # plt.ylabel('$(\mathrm{F}-F_\mathrm{model})/\sigma$', fontsize=20)
    plt.ylabel('$(F-F_\mathrm{m})/\sigma$', fontsize=17)
    plt.xlabel('$\lambda\,\mathrm{[\mu m]}$', fontsize=17)
    # plt.ylim(-100, 100)
    plt.hlines(y=0,
               xmin=min(lbd),
               xmax=max(lbd),
               linestyles='--',
               color='black')

    plt.xlim(min(lbd), max(lbd))
    if model == 'aara' or model == 'beatlas' or\
       model == 'acol' or model == 'bcmi':
        plt.xscale('log')

    if log_scale is True:
        plt.xscale('log')
    else:
        plt.xscale('linear')

    if model == 'bcmi_pol' or model == 'aara_pol':
        plt.xlim(0.9 * min(lbd), 1.1 * max(lbd))

    plt.tick_params(direction='in',
                    length=6,
                    width=2,
                    colors='gray',
                    which='both')
    #plt.legend(loc='upper right')

    return
Exemple #4
0
def emcee_inference(star, Ndim, ranges, lbdarr, wave, logF, dlogF, minfo,
                    listpar, logF_grid, vsin_obs, sig_vsin_obs, incl, dist_pc,
                    sig_dist_pc, isig, dims, include_rv, a_parameter,
                    af_filter, tag, plot_fits, long_process, log_scale, model,
                    acrux, pool, Nproc, stellar_prior, npy_star, pdf_mas,
                    pdf_obl, pdf_age, grid_mas, grid_obl, grid_age, band,
                    lbd_range):

    #star, Ndim, ranges, lbdarr, wave, logF, dlogF, minfo,
    #                    listpar, logF_grid, vsin_obs, sig_vsin_obs, dist_pc,
    #                    sig_dist_pc, isig, dims, include_rv, a_parameter,
    #                    af_filter, tag, plot_fits, long_process, log_scale,
    #                    model, acrux, pool, Nproc, stellar_prior, npy_star,
    #                    pdf_mas, pdf_obl, pdf_age, grid_mas,
    #                    grid_obl, grid_age, band):

    if long_process is True:
        #Nwalk = 2500 #500  # 200  # 500
        #nint_burnin = 500 #100  # 50
        #nint_mcmc = 5000  # 500  # 1000
        Nwalk = 500  # 200  # 500
        nint_burnin = 100  # 50
        nint_mcmc = 1000  # 500  # 1000
    else:
        Nwalk = 50
        nint_burnin = 20
        nint_mcmc = 100

    p0 = [
        np.random.rand(Ndim) * (ranges[:, 1] - ranges[:, 0]) + ranges[:, 0]
        for i in range(Nwalk)
    ]
    start_time = time.time()

    with Pool() as pool:
        if acrux is True:
            sampler = emcee.EnsembleSampler(
                Nwalk,
                Ndim,
                lnprob,
                args=[
                    incl, wave, logF, dlogF, minfo, listpar, logF_grid,
                    vsin_obs, sig_vsin_obs, dist_pc, sig_dist_pc, isig, ranges,
                    dims, include_rv, model, stellar_prior, npy_star, pdf_mas,
                    pdf_obl, pdf_age, grid_mas, grid_obl, grid_age
                ],
                a=a_parameter,
                pool=pool)
        else:
            sampler = emcee.EnsembleSampler(
                Nwalk,
                Ndim,
                lnprob,
                args=[
                    incl, wave, logF, dlogF, minfo, listpar, logF_grid,
                    vsin_obs, sig_vsin_obs, dist_pc, sig_dist_pc, isig, ranges,
                    dims, include_rv, model, stellar_prior, npy_star, pdf_mas,
                    pdf_obl, pdf_age, grid_mas, grid_obl, grid_age
                ],
                a=a_parameter)

        sampler_tmp = run_emcee(p0,
                                sampler,
                                nint_burnin,
                                nint_mcmc,
                                Ndim,
                                file_name=star)
    print("--- %s minutes ---" % ((time.time() - start_time) / 60))

    sampler, params_fit, errors_fit, maxprob_index,\
        minprob_index, af = sampler_tmp

    if include_rv is True:
        mass_true, obt_true, xc_true,\
            cos_true, ebv_true, dist_true, rv_true = params_fit
    else:
        if model == 'befavor' or model == 'befavor_new':
            mass_true, obt_true, xc_true,\
                cos_true, ebv_true, dist_true = params_fit
        if model == 'aara' or model == 'acol' or\
           model == 'bcmi':
            mass_true, obt_true, xc_true, n0, Rd, n_true,\
                cos_true, ebv_true, dist_true = params_fit
        if model == 'bcmi_pol' or model == 'aara_pol':
            mass_true, obt_true, xc_true, n0, Rd, n_true,\
                cos_true = params_fit
        if model == 'beatlas':
            mass_true, obt_true, rh0_true, nix_true,\
                inc_true, dis_true, ebv_true = params_fit
    # if model is False:
    #     angle_in_rad = np.arccos(params_fit[6])
    #     params_fit[6] = (np.arccos(params_fit[6])) * (180. / np.pi)
    #     errors_fit[6] = (errors_fit[6] / (np.sqrt(1. -
    #                      (np.cos(angle_in_rad)**2.)))) * (180. / np.pi)

    chain = sampler.chain

    if af_filter is True:
        acceptance_fractions = sampler.acceptance_fraction
        chain = chain[(acceptance_fractions >= 0.20)
                      & (acceptance_fractions <= 0.50)]
        af = acceptance_fractions[(acceptance_fractions >= 0.20)
                                  & (acceptance_fractions <= 0.50)]
        af = np.mean(af)

    af = str('{0:.2f}'.format(af))

    # Saving first sample
    # file_npy = 'figures/' + str(star) + '/' + 'Walkers_' +\
    #     str(Nwalk) + '_Nmcmc_' + str(nint_mcmc) +\
    #     '_af_' + str(af) + '_a_' + str(a_parameter) +\
    #     tag + ".npy"
    # np.save(file_npy, chain)
    current_folder = 'figures/' + str(star) + '/' + str(lbd_range) + '/'
    file_npy = current_folder + str(star) + ".npy"
    np.save(file_npy, chain)

    # plot results
    mpl.rcParams['mathtext.fontset'] = 'stix'
    mpl.rcParams['font.family'] = 'STIXGeneral'
    mpl.rcParams['font.size'] = 16

    # Loading first dataframe
    chain_1 = np.load(file_npy)
    Ndim_1 = np.shape(chain_1)[-1]
    flatchain_1 = chain_1.reshape((-1, Ndim_1))

    if model == 'befavor' or 'befavor_new':
        mas_1 = flatchain_1[:, 0]
        obl_1 = flatchain_1[:, 1]
        age_1 = flatchain_1[:, 2]
        inc_1 = flatchain_1[:, 3]
        dis_1 = flatchain_1[:, 4]
        ebv_1 = flatchain_1[:, 5]
    if model == 'aara' or model == 'acol' or\
       model == 'bcmi':
        mas_1 = flatchain_1[:, 0]
        obl_1 = flatchain_1[:, 1]
        age_1 = flatchain_1[:, 2]
        rh0_1 = flatchain_1[:, 3]
        rdk_1 = flatchain_1[:, 4]
        nix_1 = flatchain_1[:, 5]
        inc_1 = flatchain_1[:, 6]
        dis_1 = flatchain_1[:, 7]
        ebv_1 = flatchain_1[:, 8]
    if model == 'bcmi_pol' or model == 'aara_pol':
        mas_1 = flatchain_1[:, 0]
        obl_1 = flatchain_1[:, 1]
        age_1 = flatchain_1[:, 2]
        rh0_1 = flatchain_1[:, 3]
        rdk_1 = flatchain_1[:, 4]
        nix_1 = flatchain_1[:, 5]
        inc_1 = flatchain_1[:, 6]
    if model == 'beatlas':
        mas_1 = flatchain_1[:, 0]
        obl_1 = flatchain_1[:, 1]
        rh0_1 = flatchain_1[:, 2]
        nix_1 = flatchain_1[:, 3]
        inc_1 = flatchain_1[:, 4]
        dis_1 = flatchain_1[:, 5]
        ebv_1 = flatchain_1[:, 6]

    if include_rv is True:
        rv_1 = flatchain_1[:, 6]
        par_list = np.zeros([len(mas_1), 7])
    else:
        if model == 'befavor' or model == 'befavor_new':
            par_list = np.zeros([len(mas_1), 6])
        if model == 'aara' or model == 'acol' or\
           model == 'bcmi':
            par_list = np.zeros([len(mas_1), 9])
        if model == 'bcmi_pol' or model == 'aara_pol':
            par_list = np.zeros([len(mas_1), 7])
        if model == 'beatlas':
            par_list = np.zeros([len(mas_1), 7])

    for i in range(len(mas_1)):
        if include_rv is True:
            par_list[i] = [
                mas_1[i], obl_1[i], age_1[i], inc_1[i], dis_1[i], ebv_1[i],
                rv_1[i]
            ]
        else:
            if model == 'befavor' or model == 'befavor_new':
                par_list[i] = [
                    mas_1[i], obl_1[i], age_1[i], inc_1[i], dis_1[i], ebv_1[i]
                ]
            if model == 'aara' or model == 'acol' or\
               model == 'bcmi':
                par_list[i] = [
                    mas_1[i], obl_1[i], age_1[i], rh0_1[i], rdk_1[i], nix_1[i],
                    inc_1[i], dis_1[i], ebv_1[i]
                ]
            if model == 'bcmi_pol' or model == 'aara_pol':
                par_list[i] = [
                    mas_1[i], obl_1[i], age_1[i], rh0_1[i], rdk_1[i], nix_1[i],
                    inc_1[i]
                ]
            if model == 'beatlas':
                par_list[i] = [
                    mas_1[i], obl_1[i], rh0_1[i], nix_1[i], inc_1[i], dis_1[i],
                    ebv_1[i]
                ]

    # plot corner
    if include_rv is True:
        samples = np.vstack((mas_1, obl_1, age_1, inc_1, dis_1, ebv_1, rv_1)).T
    else:
        if model == 'befavor' or model == 'befavor_new':
            samples = np.vstack((mas_1, obl_1, age_1, inc_1, dis_1, ebv_1)).T
        if model == 'aara' or model == 'acol' or\
           model == 'bcmi':
            samples = np.vstack((mas_1, obl_1, age_1, rh0_1, rdk_1, nix_1,
                                 inc_1, dis_1, ebv_1)).T
        if model == 'bcmi_pol' or model == 'aara_pol':
            samples = np.vstack(
                (mas_1, obl_1, age_1, rh0_1, rdk_1, nix_1, inc_1)).T
        if model == 'beatlas':
            samples = np.vstack(
                (mas_1, obl_1, rh0_1, nix_1, inc_1, dis_1, ebv_1)).T

    k, arr_t_tc, arr_Xc = t_tms_from_Xc(mass_true,
                                        savefig=False,
                                        plot_fig=False)
    ttms_ip = np.arange(0.001, 1., 0.001)
    Xc_ip = np.interp(ttms_ip, arr_t_tc[k], arr_Xc[k])

    for i in range(len(samples)):
        if model == 'befavor' or model == 'aara' or\
           model == 'acol' or model == 'bcmi' or\
           model == 'befavor_new' or model == 'bcmi_pol' or\
           model == 'aara_pol':
            # Calculating logg for the non_rotating case
            Mstar, oblat, Hfrac = samples[i][0], samples[i][1],\
                samples[i][2]
        else:
            Mstar, oblat, Hfrac = samples[i][0], samples[i][1], 0.3

        if model != 'befavor_new':
            t = np.max(np.array([hfrac2tms(Hfrac), 0.]))
        else:
            t = np.copy(Hfrac)

        Rpole, logL = geneva_interp_fast(Mstar,
                                         oblat,
                                         t,
                                         neighbours_only=True,
                                         isRpole=False)

        # Converting oblat to W
        samples[i][1] = obl2W(samples[i][1])

        if model == 'befavor':
            # Converting angles to degrees
            samples[i][3] = (np.arccos(samples[i][3])) * (180. / np.pi)
            # Converting Xc to t(tms)
            samples[i][2] = ttms_ip[find_nearest(Xc_ip, samples[i][2])[1]]
        if model == 'befavor_new':
            # Converting angles to degrees
            samples[i][3] = (np.arccos(samples[i][3])) * (180. / np.pi)
        if model == 'aara':
            # Converting Xc to t(tms)
            samples[i][2] = ttms_ip[find_nearest(Xc_ip, samples[i][2])[1]]
            samples[i][5] = samples[i][5] + 1.5
            samples[i][6] = (np.arccos(samples[i][6])) * (180. / np.pi)
        if model == 'acol' or model == 'bcmi' or model == 'bcmi_pol' or\
           model == 'aara_pol':
            # Converting Xc to t(tms)
            samples[i][2] = ttms_ip[find_nearest(Xc_ip, samples[i][2])[1]]
            samples[i][6] = (np.arccos(samples[i][6])) * (180. / np.pi)
        if model == 'beatlas':
            samples[i][4] = (np.arccos(samples[i][4])) * (180. / np.pi)
        if model == 'aara_pol':
            samples[i][5] = samples[i][5] + 1.5
    # plot corner
    quantiles = [0.16, 0.5, 0.84]
    if include_rv is True:
        labels = [
            r'$M\,[\mathrm{M_\odot}]$', r'$W$', r"$t/t_\mathrm{ms}$",
            r'$i[\mathrm{^o}]$', r'$d\,[pc]$', r'E(B-V)', r'$R_\mathrm{V}$'
        ]
    else:
        if model == 'befavor' or model == 'befavor_new':
            labels = [
                r'$M\,[\mathrm{M_\odot}]$', r'$W$', r"$t/t_\mathrm{ms}$",
                r'$i[\mathrm{^o}]$', r'$d\,[\mathrm{pc}]$', r'E(B-V)'
            ]
        if model == 'aara' or model == 'acol' or\
           model == 'bcmi':
            labels = [
                r'$M\,[\mathrm{M_\odot}]$', r'$W$', r"$t/t_\mathrm{ms}$",
                r'$\log \, n_0 \, [\mathrm{cm^{-3}}]$',
                r'$R_\mathrm{D}\, [R_\star]$', r'$n$', r'$i[\mathrm{^o}]$',
                r'$d\,[\mathrm{pc}]$', r'E(B-V)'
            ]
        if model == 'bcmi_pol' or model == 'aara_pol':
            labels = [
                r'$M\,[\mathrm{M_\odot}]$', r'$W$', r"$t/t_\mathrm{ms}$",
                r'$\log \, n_0$', r'$R_\mathrm{D}\, [R_\star]$', r'$n$',
                r'$i[\mathrm{^o}]$'
            ]
        if model == 'beatlas':
            labels = [
                r'$M\,[\mathrm{M_\odot}]$', r'$W$', r'$\Sigma_0$', r'$n$',
                r'$i[\mathrm{^o}]$', r'$d\,[pc]$', r'E(B-V)'
            ]
    if model == 'befavor':
        ranges[1] = obl2W(ranges[1])
        ranges[2][0] = ttms_ip[find_nearest(Xc_ip, ranges[2][1])[1]]
        ranges[2][1] = ttms_ip[find_nearest(Xc_ip, ranges[2][0])[1]]
        ranges[3] = (np.arccos(ranges[3])) * (180. / np.pi)
        ranges[3] = np.array([ranges[3][1], ranges[3][0]])
    if model == 'befavor_new':
        ranges[1] = obl2W(ranges[1])
        ranges[3] = (np.arccos(ranges[3])) * (180. / np.pi)
        ranges[3] = np.array([ranges[3][1], ranges[3][0]])
    if model == 'aara':
        ranges[1] = obl2W(ranges[1])
        ranges[2][0] = ttms_ip[find_nearest(Xc_ip, ranges[2][1])[1]]
        ranges[2][1] = ttms_ip[find_nearest(Xc_ip, ranges[2][0])[1]]
        ranges[3] = np.array([ranges[3][1], ranges[3][0]])
        ranges[5] = ranges[5] + 1.5
        ranges[6] = (np.arccos(ranges[6])) * (180. / np.pi)
        ranges[6] = np.array([ranges[6][1], ranges[6][0]])
    if model == 'acol' or model == 'bcmi' or model == 'bcmi_pol' or\
       model == 'aara_pol':
        ranges[1] = obl2W(ranges[1])
        ranges[2][0] = ttms_ip[find_nearest(Xc_ip, ranges[2][1])[1]]
        ranges[2][1] = ttms_ip[find_nearest(Xc_ip, ranges[2][0])[1]]
        ranges[3] = np.array([ranges[3][1], ranges[3][0]])
        ranges[6] = (np.arccos(ranges[6])) * (180. / np.pi)
        ranges[6] = np.array([ranges[6][1], ranges[6][0]])
    if model == 'beatlas':
        ranges[1] = obl2W(ranges[1])
        ranges[3] = np.array([ranges[3][-1], ranges[3][0]])
        ranges[4] = (np.arccos(ranges[4])) * (180. / np.pi)
        ranges[4] = np.array([ranges[4][1], ranges[4][0]])

    corner(samples,
           labels=labels,
           range=ranges,
           quantiles=quantiles,
           plot_contours=True,
           smooth=2.,
           smooth1d=False,
           plot_datapoints=True,
           label_kwargs={'fontsize': 17},
           title_kwargs={'fontsize': 17},
           truths=None,
           show_titles=True,
           color_hist='black',
           plot_density=True,
           fill_contours=False,
           no_fill_contours=False,
           normed=True)

    if plot_fits is True:
        plot_fit_last(params_fit, wave, logF, dlogF, minfo, listpar, lbdarr,
                      logF_grid, isig, dims, Nwalk, nint_mcmc, ranges,
                      include_rv, file_npy, log_scale, model)


    fig_name = 'Walkers_' + np.str(Nwalk) + '_Nmcmc_' +\
        np.str(nint_mcmc) + '_af_' + str(af) + '_a_' +\
        str(a_parameter) + tag

    plot_residuals(params_fit, wave, logF, dlogF, minfo, listpar, lbdarr,
                   logF_grid, isig, dims, Nwalk, nint_mcmc, ranges, include_rv,
                   file_npy, log_scale, model)
    plt.savefig(current_folder + fig_name + '_' + lbd_range + '.png', dpi=100)

    plot_convergence(file_npy, file_npy[:-4] + '_convergence', model)

    # Saving the chord diagram
    if model == 'aara' or model == 'bcmi' or model == 'acol':
        chord_plot_complete(folder='figures/', file=str(star))
    elif model == 'bcmi_pol' or model == 'aara_pol':
        chord_plot_pol(folder='figures/', file=str(star))
    else:
        chord_plot(folder='figures/', file=str(star))

    return
Exemple #5
0
def lnprior(params, incl, vsin_obs, sig_vsin_obs, dist_pc, sig_dist_pc, model,
            stellar_prior, npy_star, pdf_mas, pdf_obl, pdf_age, grid_mas,
            grid_obl, grid_age):

    if model == 'befavor':
        Mstar, oblat, Hfrac, cosi, dist = params[0], params[1],\
            params[2], params[3], params[4]
    if model == 'befavor_new':
        Mstar, oblat, ttms, cosi, dist = params[0], params[1],\
            params[2], params[3], params[4]
    if model == 'aara' or model == 'acol' or\
       model == 'bcmi':
        Mstar, oblat, Hfrac, cosi, dist = params[0], params[1],\
            params[2], params[6], params[7]
    if model == 'bcmi_pol' or model == 'aara_pol':
        Mstar, oblat, Hfrac, cosi = params[0], params[1],\
            params[2], params[6]
    if model == 'beatlas':
        Mstar, oblat, Hfrac, cosi, dist = params[0], params[1],\
            0.3, params[4], params[5]

    # Reading Stellar Priors
    # print(Mstar, oblat, Hfrac)
    if stellar_prior is True:
        temp, idx_mas = find_nearest(grid_mas, value=Mstar)
        temp, idx_obl = find_nearest(grid_obl, value=oblat)
        temp, idx_age = find_nearest(grid_age, value=Hfrac)
        chi2_stellar_prior = 1 / pdf_mas[idx_mas] +\
            1 / pdf_obl[idx_obl] + \
            1 / pdf_age[idx_age]
        # chi2_stellar_prior = Mstar * pdf_mas[idx_mas] +\
        #     oblat * pdf_obl[idx_obl] + \
        #     Hfrac * pdf_age[idx_age]
    else:
        chi2_stellar_prior = 0.0

    # Rpole, Lstar, Teff = vkg.geneve_par(Mstar, oblat, Hfrac, folder_tables)
    if model == 'befavor_new':
        t = np.copy(ttms)
    else:
        t = np.max(np.array([hfrac2tms(Hfrac), 0.]))

    Rpole, logL = geneva_interp_fast(Mstar,
                                     oblat,
                                     t,
                                     neighbours_only=True,
                                     isRpole=False)

    wcrit = np.sqrt(8. / 27. * G * Mstar * Msun / (Rpole * Rsun)**3)

    vsini = oblat2w(oblat) * wcrit * (Rpole * Rsun * oblat) *\
        np.sin(np.arccos(cosi)) * 1e-5

    if sig_vsin_obs == 0.0:
        sig_vsin_obs = 0.1 * vsin_obs
    chi2_vsi = (vsin_obs - vsini)**2 / sig_vsin_obs**2.
    #chi2_incl = 0.
    #chi2_incl = (incl - np.degrees(np.arccos(cosi)))**2 / 4.0**2.
    '''
    # Inclination prior added by Keegan 18-10-30
    inc_obs = 43.0
    inc = (np.arccos(cosi))*180.0/np.pi #include the cosi so that it iterates over the mcmc for this walker
    sig_inc_obs = 8.0
    chi2_incl = (inc_obs - inc)**2 / sig_inc_obs**2.
    
    # t/tms prior added by Keegan 19-01-22
    ttms_obs = 0.44
    sig_ttms_obs = 0.25
    chi2_ttms = (ttms_obs - t)**2 / sig_ttms_obs**2.
    
    #Mass prior added by Keegan 19-01-22
    #Mstar_obs = 10.0
    #sig_Mstar_obs = 1.5
    #chi2_Mstar = (Mstar_obs - Mstar)**2 / sig_Mstar_obs**2.
    
    '''
    if model != 'bcmi_pol' and model != 'aara_pol':
        chi2_dis = (dist_pc - dist)**2 / sig_dist_pc**2.
        chi2_prior = chi2_vsi + chi2_dis + chi2_stellar_prior  # + chi2_incl + chi2_ttms# + chi2_Mstar
    else:
        chi2_prior = chi2_vsi + chi2_stellar_prior  # + chi2_incl + chi2_ttms# + chi2_Mstar

    #turning priors off
    #chi2_prior = 0

    return -0.5 * chi2_prior