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 
Example #2
0
def test_distance(name): 
    # read in observations 
    dat_dir = os.environ['GALPOPFM_DIR']
    fsdss = os.path.join(dat_dir, 'obs', 'tinker_SDSS_centrals_M9.7.valueadd.hdf5') 
    sdss = h5py.File(fsdss, 'r') 
    
    F_mag_sdss = sdss['ABSMAG'][...][:,0]
    N_mag_sdss = sdss['ABSMAG'][...][:,1]
    R_mag_sdss = sdss['ABSMAG'][...][:,4]

    Haflux_sdss = sdss['HAFLUX'][...]
    Hbflux_sdss = sdss['HBFLUX'][...]
    #Ha_sdss = Haflux_sdss * (4.*np.pi * (sdss['Z'][...] * 2.9979e10/2.2685e-18)**2) * 1e-17
    #Hb_sdss = Hbflux_sdss * (4.*np.pi * (sdss['Z'][...] * 2.9979e10/2.2685e-18)**2) * 1e-17

    x_obs = dustInfer.sumstat_obs(F_mag_sdss, N_mag_sdss, R_mag_sdss, Haflux_sdss, Hbflux_sdss, sdss['Z'][...])

    # read SED for sims 
    sim_sed = dustInfer._read_sed(name) 

    # pass through the minimal amount of memory 
    wlim = (sim_sed['wave'] > 1e3) & (sim_sed['wave'] < 1e4) 
    sim_sed_wlim = {}
    sim_sed_wlim['mstar']   = sim_sed['mstar'] 
    sim_sed_wlim['wave']    = sim_sed['wave'][wlim] 
    for k in ['sed_noneb', 'sed_onlyneb']: 
        sim_sed_wlim[k] = sim_sed[k][:,wlim]
    
    import time 
    for i in range(10): 
        t0 = time.time()
        theta = np.array([0.1, 0.2+0.05*float(i), 1./0.44]) 
        x_model = dustInfr.sumstat_model(theta, sim_sed_wlim, dem='slab_calzetti')
        print('summary sstatitics takes %.f' % (time.time() - t0))

        t0 = time.time()
        print(dustInfer.distance_metric(x_model, x_obs)) 
        print('distance takes %.f' % (time.time() - t0))
    return None 
Example #3
0
elif statistic == '2d':
    if distance_method == 'L2':
        eps0 = [4.e-5, 0.002, 0.0005]
    elif distance_method == 'L1':
        eps0 = [0.01, 0.2, 0.1]
elif statistic == '3d':
    if distance_method == 'L2':
        eps0 = [4.e-5, 0.01]
    elif distance_method == 'L1':
        eps0 = [0.01, 0.2]
    elif distance_method == 'L2_only':
        eps0 = [rho_max_3d]
######################################################
# this will run on all processes =X
# read SED for sims
sim_sed = dustInfer._read_sed(sim)

# pass through the minimal amount of memory
wlim = (sim_sed['wave'] > 1e3) & (sim_sed['wave'] < 8e3)
# only keep centrals and impose mass limit as well.
# the lower limit log M* > 9.4 is padded by >0.25 dex to conservatively account
# for log M* and R magnitude scatter
downsample = np.zeros(len(sim_sed['logmstar'])).astype(bool)
downsample[::5] = True
f_downsample = 0.2
#downsample[:] = True
#f_downsample = 1.

mlim = (sim_sed['logmstar'] > 9.4)
cuts = mlim & downsample  # mass limit
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 
def abc_attenuationt(T, sim='simba', dem='slab_calzetti', abc_dir=None):
    '''
    '''
    # 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) 

    wave = np.linspace(1e3, 1e4, 101) 
    flux = np.ones(len(wave))
    i3000 = (np.abs(wave - 3000.)).argmin()  # index at 3000A
    
    # read simulations 
    _sim_sed = dustInfer._read_sed(sim) 
    cens = _sim_sed['censat'].astype(bool) & (_sim_sed['logmstar'] > 9.4)#
    logms = _sim_sed['logmstar'][cens].copy()
    logsfr = _sim_sed['logsfr.100'][cens].copy() 
    
    A_lambdas, highmass, sfing = [], [], [] 
    for i in np.arange(np.sum(cens))[::100]:  
        if dem == 'slab_calzetti': 
            A_lambda = -2.5 * np.log10(dustFM.DEM_slabcalzetti(theta_med, wave,
                flux, logms[i], logsfr[i], nebular=False)) 
        elif dem == 'slab_noll_m': 
            A_lambda = -2.5 * np.log10(dustFM.DEM_slab_noll_m(theta_med, wave, 
                flux, logms[i], logsfr[i], nebular=False)) 
        elif dem == 'slab_noll_msfr': 
            A_lambda = -2.5 * np.log10(dustFM.DEM_slab_noll_msfr(theta_med, wave, 
                flux, logms[i], logsfr[i], nebular=False)) 
        A_lambdas.append(A_lambda) 

        if logms[i] > 10.5: 
            highmass.append(True)
        else: highmass.append(False)

        if logsfr[i] - logms[i] > -11.: sfing.append(True)
        else: sfing.append(False)

    fig = plt.figure(figsize=(10,10))
    sub = fig.add_subplot(311)
    for hm, A_lambda in zip(highmass, A_lambdas): 
        if hm: 
            sub.plot(wave, A_lambda, c='C1', lw=0.1) 
        else: 
            sub.plot(wave, A_lambda, c='C0', lw=0.1) 
    sub.set_xlim(1.5e3, 1e4) 
    sub.set_xticklabels([]) 
    sub.set_ylabel(r'$A_\lambda$', fontsize=25) 
    sub.set_yscale('log') 
    sub.set_ylim(1e-4, 20.) 
    
    sub = fig.add_subplot(312)
    for sf, A_lambda in zip(sfing, A_lambdas): 
        if sf: 
            sub.plot(wave, A_lambda, c='C0', lw=0.1) 
        else: 
            sub.plot(wave, A_lambda, c='C1', lw=0.1) 
    sub.set_xlim(1.5e3, 1e4) 
    sub.set_xticklabels([]) 
    sub.set_ylabel(r'$A_\lambda$', fontsize=25) 
    sub.set_yscale('log') 
    sub.set_ylim(0.1, 20.) 

    sub = fig.add_subplot(313)
    for A_lambda in A_lambdas: 
        sub.plot(wave, A_lambda/A_lambda[i3000], c='k', lw=0.1) 
    sub.set_xlabel('Wavelength [$A$]', fontsize=25) 
    sub.set_xlim(1.5e3, 1e4) 
    sub.set_ylim(0., 10.) 
    sub.set_ylabel(r'$A_\lambda/A_{3000}$', fontsize=25) 
    fig.savefig(os.path.join(abc_dir, 'abc_attenuation.t%i.png' % T), bbox_inches='tight') 
    return None 
Example #6
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