def _examine_distance(T, sim='simba', dem='slab_calzetti'):
    ''' compare ABC summary statistics to data 
    '''
    # read pool 
    theta_T = np.loadtxt(os.path.join(abc_dir, 'theta.t%i.dat' % T)) 
    rho_T   = np.loadtxt(os.path.join(abc_dir, 'rho.t%i.dat' % T)) 
    w_T     = np.loadtxt(os.path.join(abc_dir, 'w.t%i.dat' % T)) 
    theta_med = np.median(theta_T, axis=0) 

    # read simulations 
    _sim_sed = dustInfer._read_sed(sim) 
    wlim = (_sim_sed['wave'] > 1e3) & (_sim_sed['wave'] < 8e3) 
    #cens = _sim_sed['censat'].astype(bool) & (_sim_sed['logmstar'] > 8.5) 
    downsample = np.zeros(len(_sim_sed['logmstar'])).astype(bool)
    downsample[::10] = True
    f_downsample = 0.1
    cens = _sim_sed['censat'].astype(bool) & (_sim_sed['logmstar'] > 9.4) & downsample

    sim_sed = {} 
    sim_sed['sim']          = sim
    sim_sed['logmstar']     = _sim_sed['logmstar'][cens].copy()
    sim_sed['logsfr.100']   = _sim_sed['logsfr.100'][cens].copy() 
    sim_sed['wave']         = _sim_sed['wave'][wlim].copy()
    sim_sed['sed_noneb']    = _sim_sed['sed_noneb'][cens,:][:,wlim].copy() 
    sim_sed['sed_onlyneb']  = _sim_sed['sed_onlyneb'][cens,:][:,wlim].copy() 

    # read SDSS observable
    mag_edges, gr_edges, fuvnuv_edges, _x_obs, err_x = np.load(os.path.join(dat_dir, 'obs',
            'tinker_SDSS_centrals_M9.7.Mr_complete.Mr_GR_FUVNUV.npy'),
            allow_pickle=True)
    x_obs = [np.sum(_x_obs), _x_obs]
    print('----------------------------------------') 
    print('median posterior theta:', theta_med) 
    x_mod = dustInfer.sumstat_model(theta_med, sed=sim_sed, dem=dem,
            f_downsample=f_downsample) 
    rho = dustInfer.distance_metric(x_obs, x_mod, method='L2', x_err=err_x)
    print('L2', rho)
    rho = dustInfer.distance_metric(x_obs, x_mod, method='L1', x_err=err_x)
    print('L1', rho)
    data_mod = dustInfer.sumstat_model(theta_med, sed=sim_sed, dem=dem,
            f_downsample=f_downsample, return_datavector=True)
    print('%f < R < %f' % (-1*data_mod[0].max(), -1*data_mod[0].min()))
    print('%f < G-R < %f' % (data_mod[1].min(), data_mod[1].max()))
    print('%f < FUV-NUV < %f' % (data_mod[2].min(), data_mod[2].max()))
    print('theta:', np.array([2., 2.]))
    x_mod = dustInfer.sumstat_model(np.array([2., 2.]), sed=sim_sed, dem=dem,
            f_downsample=f_downsample) 
    rho = dustInfer.distance_metric(x_obs, x_mod, method='L2', x_err=err_x)
    print('L2', rho)
    rho = dustInfer.distance_metric(x_obs, x_mod, method='L1', x_err=err_x)
    print('L1', rho)
    data_mod = dustInfer.sumstat_model(np.array([2., 2.]), sed=sim_sed, dem=dem,
            f_downsample=f_downsample, return_datavector=True)
    print('%f < R < %f' % (-1*data_mod[0].max(), -1*data_mod[0].min()))
    print('%f < G-R < %f' % (data_mod[1].min(), data_mod[1].max()))
    print('%f < FUV-NUV < %f' % (data_mod[2].min(), data_mod[2].max()))

    return None 
Пример #2
0
def _sumstat_model_wrap(theta, dem=dem):
    print('    [%s]' % ','.join('%.1f' % tt for tt in theta))
    x_mod = dustInfer.sumstat_model(theta,
                                    sed=shared_sim_sed,
                                    dem=dem,
                                    f_downsample=f_downsample,
                                    statistic=statistic,
                                    noise=True,
                                    sfr0_prescription=sfr0)
    if distance_method == 'L2_only':
        # nbar not included in distance metric
        return x_mod[1:]
    return x_mod
def abc_sumstat(T, sim='simba', dem='slab_calzetti', abc_dir=None):
    ''' compare ABC summary statistics to data 
    '''
    ####################################################################################
    # read in SDSS measurements 
    ####################################################################################
    Rmag_edges, gr_edges, fuvnuv_edges, x_obs, x_obs_err = np.load(os.path.join(dat_dir, 'obs', 
        'tinker_SDSS_centrals_M9.7.Mr_complete.Mr_GR_FUVNUV.npy'), allow_pickle=True)
    dRmag   = Rmag_edges[1] - Rmag_edges[0]
    dGR     = gr_edges[1] - gr_edges[0]
    dfuvnuv = fuvnuv_edges[1] - fuvnuv_edges[0]
    #ranges = [(Rmag_edges[0], Rmag_edges[-1]), (gr_edges[0], gr_edges[-1]),
    #        (fuvnuv_edges[0], fuvnuv_edges[-1])]
    ranges = [(Rmag_edges[0], Rmag_edges[-1]), (-1., 3.), (-1., 10.)]
    nbar_obs = np.sum(x_obs)
    x_obs = [nbar_obs, x_obs]
    ####################################################################################
    # read pool 
    theta_T = np.loadtxt(os.path.join(abc_dir, 'theta.t%i.dat' % T)) 
    rho_T   = np.loadtxt(os.path.join(abc_dir, 'rho.t%i.dat' % T)) 
    w_T     = np.loadtxt(os.path.join(abc_dir, 'w.t%i.dat' % T)) 
    theta_med = np.median(theta_T, axis=0) 
    print('median ABC theta', theta_med)

    # read simulations 
    _sim_sed = dustInfer._read_sed(sim) 
    wlim = (_sim_sed['wave'] > 1e3) & (_sim_sed['wave'] < 8e3) 
    downsample = np.zeros(len(_sim_sed['logmstar'])).astype(bool)
    downsample[::10] = True
    f_downsample = 0.1
    cens = _sim_sed['censat'].astype(bool) & (_sim_sed['logmstar'] > 9.4) & downsample

    sim_sed = {} 
    sim_sed['sim']          = sim
    sim_sed['logmstar']     = _sim_sed['logmstar'][cens].copy()
    sim_sed['logsfr.100']   = _sim_sed['logsfr.100'][cens].copy() 
    sim_sed['wave']         = _sim_sed['wave'][wlim].copy()
    sim_sed['sed_noneb']    = _sim_sed['sed_noneb'][cens,:][:,wlim].copy() 
    sim_sed['sed_onlyneb']  = _sim_sed['sed_onlyneb'][cens,:][:,wlim].copy() 

    x_mod = dustInfer.sumstat_model(theta_med, sed=sim_sed, dem=dem,
            f_downsample=f_downsample, statistic='3d')
    data_mod = dustInfer.sumstat_model(theta_med, sed=sim_sed, dem=dem,
            f_downsample=f_downsample, return_datavector=True)
    print('%f < R < %f' % (-1*data_mod[0].max(), -1*data_mod[0].min()))
    print('%f < G-R < %f' % (data_mod[1].min(), data_mod[1].max()))
    print('%f < FUV-NUV < %f' % (data_mod[2].min(), data_mod[2].max()))
    ########################################################################
    fig = plt.figure(figsize=(10,10))
    sub = fig.add_subplot(221)
    sub.pcolormesh(Rmag_edges, gr_edges, dfuvnuv * np.sum(x_obs[1], axis=2).T,
            vmin=1e-5, vmax=1e-2, norm=mpl.colors.LogNorm(), cmap='Greys')
    sub.text(0.95, 0.95, r'SDSS', ha='right', va='top', transform=sub.transAxes, fontsize=25)
    sub.set_xlim(20., 23) 
    sub.set_xticks([20., 21., 22., 23]) 
    sub.set_xticklabels([])
    sub.set_ylabel(r'$G-R$', fontsize=20) 
    sub.set_ylim(ranges[1]) 

    sub = fig.add_subplot(222)
    sub.text(0.95, 0.95, r'SIMBA', ha='right', va='top', transform=sub.transAxes, fontsize=25)
    sub.pcolormesh(Rmag_edges, gr_edges, dfuvnuv * np.sum(x_mod[1], axis=2).T, 
            vmin=1e-5, vmax=1e-2, norm=mpl.colors.LogNorm(), cmap='Oranges')
    sub.set_xlim(20., 23) 
    sub.set_xticks([20., 21., 22., 23]) 
    sub.set_xticklabels([])
    sub.set_ylim(ranges[1]) 
    sub.set_yticklabels([])
    
    sub = fig.add_subplot(223)
    h = sub.pcolormesh(Rmag_edges, fuvnuv_edges, dGR * np.sum(x_obs[1], axis=1).T,
            vmin=1e-5, vmax=1e-2, norm=mpl.colors.LogNorm(), cmap='Greys')
    sub.set_xlabel(r'$M_r$', fontsize=20) 
    sub.set_xlim(20., 23) 
    sub.set_xticks([20., 21., 22., 23]) 
    sub.set_xticklabels([-20, -21, -22, -23]) 
    sub.set_ylabel(r'$FUV - NUV$', fontsize=20) 
    sub.set_ylim(ranges[2]) 

    sub = fig.add_subplot(224)
    sub.pcolormesh(Rmag_edges, fuvnuv_edges, dGR * np.sum(x_mod[1], axis=1).T,
            vmin=1e-5, vmax=1e-2, norm=mpl.colors.LogNorm(), cmap='Oranges')
    sub.set_xlabel(r'$M_r$', fontsize=20) 
    sub.set_xlim(20., 23) 
    sub.set_xticks([20., 21., 22., 23]) 
    sub.set_xticklabels([-20, -21, -22, -23]) 
    sub.set_ylim(ranges[2]) 
    sub.set_yticklabels([])

    fig.subplots_adjust(wspace=0.1, hspace=0.1, right=0.85)
    cbar_ax = fig.add_axes([0.875, 0.15, 0.02, 0.7])
    fig.colorbar(h, cax=cbar_ax)

    fig.savefig(os.path.join(abc_dir, 'abc_sumstat.t%i.png' % T), bbox_inches='tight') 
    return None 
Пример #4
0
def abc_sumstat(T, sim='simba', dem='slab_calzetti', sfr0_prescription='adhoc', abc_dir=None):
    ''' compare ABC summary statistics to data 
    '''
    ####################################################################################
    # read in SDSS measurements 
    ####################################################################################
    r_edges, gr_edges, fn_edges, x_obs = dustInfer.sumstat_obs(statistic='2d', return_bins=True)
    dr  = r_edges[1] - r_edges[0]
    dgr = gr_edges[1] - gr_edges[0]
    dfn = fn_edges[1] - fn_edges[0]
    ranges = [(r_edges[0], r_edges[-1]), (-1., 3.), (-1., 10.)]
    nbar_obs, x_obs_gr, x_obs_fn = x_obs
    ####################################################################################
    # read pool 
    ####################################################################################
    theta_T = np.loadtxt(os.path.join(abc_dir, 'theta.t%i.dat' % T)) 
    rho_T   = np.loadtxt(os.path.join(abc_dir, 'rho.t%i.dat' % T)) 
    w_T     = np.loadtxt(os.path.join(abc_dir, 'w.t%i.dat' % T)) 
    theta_med = np.median(theta_T, axis=0) 
    ####################################################################################
    # read simulations 
    ####################################################################################
    _sim_sed = dustInfer._read_sed(sim) 
    wlim = (_sim_sed['wave'] > 1e3) & (_sim_sed['wave'] < 8e3) 
    cuts = (_sim_sed['logmstar'] > 9.4)

    sim_sed = {} 
    sim_sed['sim']          = sim
    sim_sed['logmstar']     = _sim_sed['logmstar'][cuts].copy()
    sim_sed['logsfr.inst']  = _sim_sed['logsfr.inst'][cuts].copy() 
    sim_sed['wave']         = _sim_sed['wave'][wlim].copy()
    sim_sed['sed_noneb']    = _sim_sed['sed_noneb'][cuts,:][:,wlim].copy() 
    sim_sed['sed_onlyneb']  = _sim_sed['sed_onlyneb'][cuts,:][:,wlim].copy() 

    nbar_mod, x_mod_gr, x_mod_fn = dustInfer.sumstat_model(theta_med, sed=sim_sed, dem=dem,
            statistic='2d', sfr0_prescription=sfr0_prescription) 
    ########################################################################
    print('obs nbar = %.4e' % nbar_obs)
    print('mod nbar = %.4e' % nbar_mod)
    ########################################################################
    fig = plt.figure(figsize=(10,10))
    sub = fig.add_subplot(221)
    sub.pcolormesh(r_edges, gr_edges, x_obs_gr.T,
            vmin=1e-5, vmax=1e-2, norm=mpl.colors.LogNorm(), cmap='Greys')
    sub.text(0.95, 0.95, r'SDSS', ha='right', va='top', transform=sub.transAxes, fontsize=25) 
    sub.set_xlim(20., 23) 
    sub.set_xticks([20., 21., 22., 23]) 
    sub.set_xticklabels([])
    sub.set_ylabel(r'$G-R$', fontsize=20) 
    sub.set_ylim(ranges[1]) 

    sub = fig.add_subplot(222)
    sub.pcolormesh(r_edges, gr_edges, x_mod_gr.T, 
            vmin=1e-5, vmax=1e-2, norm=mpl.colors.LogNorm(), cmap='Oranges')
    sub.text(0.95, 0.95, sim_sed['sim'].upper(), ha='right', va='top', transform=sub.transAxes, fontsize=25)
    sub.set_xlim(20., 23) 
    sub.set_xticks([20., 21., 22., 23]) 
    sub.set_xticklabels([])
    sub.set_ylim(ranges[1]) 
    sub.set_yticklabels([])

    sub = fig.add_subplot(223)
    h = sub.pcolormesh(r_edges, fn_edges, x_obs_fn.T,
            vmin=1e-5, vmax=1e-2, norm=mpl.colors.LogNorm(), cmap='Greys')
    sub.set_xlabel(r'$M_r$', fontsize=20) 
    sub.set_xlim(20., 23) 
    sub.set_xticks([20., 21., 22., 23]) 
    sub.set_xticklabels([-20, -21, -22, -23]) 
    sub.set_ylabel(r'$FUV - NUV$', fontsize=20) 
    sub.set_ylim(ranges[2]) 

    sub = fig.add_subplot(224)
    sub.pcolormesh(r_edges, fn_edges, x_mod_fn.T,
            vmin=1e-5, vmax=1e-2, norm=mpl.colors.LogNorm(), cmap='Oranges')
    sub.set_xlabel(r'$M_r$', fontsize=20) 
    sub.set_xlim(20., 23) 
    sub.set_xticks([20., 21., 22., 23]) 
    sub.set_xticklabels([-20, -21, -22, -23]) 
    sub.set_ylim(ranges[2]) 
    sub.set_yticklabels([])

    fig.subplots_adjust(wspace=0.1, hspace=0.1, right=0.85)
    cbar_ax = fig.add_axes([0.875, 0.15, 0.02, 0.7])
    fig.colorbar(h, cax=cbar_ax)
    try: 
        fig.savefig(os.path.join(abc_dir, 'abc_sumstat.t%i.png' % T), bbox_inches='tight') 
    except RuntimeError: 
        fig.savefig(os.path.join(abc_dir, 'abc_sumstat.t%i.pdf' % T), bbox_inches='tight') 
    plt.close() 
    return None 
def _sumstat_model_wrap(theta, dem=dem): 
    x_mod = dustInfer.sumstat_model(theta, sed=shared_sim_sed, dem=dem,
            f_downsample=f_downsample, statistic=statistic) 
    return x_mod