Example #1
0
def test_tduty_tdelay_dt_grid(run):
    '''
    '''
    file_name = ''.join([UT.fig_dir(), 'tduty_tdelay_dt_grid.', run, '.p'])
    grid = pickle.load(open(file_name, 'rb'))

    fig = plt.figure(figsize=(10,10))
    sub = fig.add_subplot(111, projection='3d')
    #sub.view_init(45, 60)

    tduty = grid[0,:]
    tdelay = grid[1,:] 
    dt_abias = grid[2,:]
    sigMstar = grid[3,:]
    l2 = grid[4,:]
    print 'L2 = ', l2.min(), l2.max() 

    scat = sub.scatter(dt_abias, tdelay, sigMstar, c=tduty)#, facecolors=cm.viridis(tduty))
    sub.set_xlabel('$\Delta t_{abias}$ Gyr', fontsize=15)
    sub.set_ylabel('$t_{delay}$ Gyr', fontsize=15)
    sub.set_zlabel('$\sigma_{log\,M_*}$ Gyr', fontsize=15)
    fig.colorbar(scat, shrink=0.5, aspect=10, cmap='hot', label='$t_{duty}$ Gyr')

    for angle in range(0, 360):
        sub.view_init(10, angle)
        fig_name = ''.join([UT.fig_dir(), 'tduty_tdelay_dt_grid.', run, '.', str(angle), '.png'])
        fig.savefig(fig_name)
    return None 
Example #2
0
def lnL_pca_kde(mock, ell=0, rebin=None, krange=None): 
    ''' ***TESTED: expectedly, more discrepant for low number of 
    mock catalogs. For Nseries monopole with 1000 mocks, no 
    significant discrepancy in the likelihood distribution 
    *** 
    Test whether or not the Gaussian KDE approximation of pdfs 
    is sufficiently accurate by comparing the likelihood estimated
    from NG.lnL_pca vs NG.lnL_pca_gauss. If they are highly 
    discrepant, then KDE estimate of the pdfs are not very accurate. 
    '''
    Pk = NG.dataX(mock, ell=ell, rebin=rebin, krange=krange)
    pca_gauss = NG.lnL_pca_gauss(Pk, Pk)
    pca_kde = NG.lnL_pca(Pk, Pk) 

    prettyplot()
    fig = plt.figure()
    sub = fig.add_subplot(111)
    nbin = 32 
    sub.hist(pca_gauss, bins=nbin, range=[-2.2*Pk.shape[1], -0.5*Pk.shape[1]], 
            normed=True, alpha=0.75, label='Gaussian $\mathcal{L^\mathtt{pseudo}}$')
    sub.hist(pca_kde, bins=nbin, range=[-2.2*Pk.shape[1], -0.8*Pk.shape[1]], 
            normed=True, alpha=0.75, label='$\mathcal{L^\mathtt{pseudo}}$ KDE estimate')
    sub.set_xlabel('log $\mathcal{L}$', fontsize=25)
    sub.set_xlim([-2.2*Pk.shape[1], -0.5*Pk.shape[1]])
    sub.legend(loc='upper left', prop={'size': 20}) 

    if rebin is None: # save fig
        f = ''.join([UT.fig_dir(), 'tests/test.lnL_kde_test.', mock, '.ell', str(ell), '.png'])
    else: 
        f = ''.join([UT.fig_dir(), 'tests/test.lnL_kde_test.', mock, '.ell', str(ell), '.rebin', str(rebin), '.png'])
    fig.savefig(f, bbox_inches='tight') 
    return None 
Example #3
0
def lnL_sys(mock, ell=0, rebin=None, sys='fc'): 
    ''' Compare the pseudo gaussian L with no systematics, ICA L estimation with 
    no systematics, and ICA L estimation with fiber collisions.
    '''
    # Likelihood without systematics 
    Pk_nosys = NG.dataX(mock, ell=ell, rebin=rebin, sys=None)
    gauss = NG.lnL_pca_gauss(Pk_nosys, Pk_nosys)
    ica_nosys = NG.lnL_ica(Pk_nosys, Pk_nosys)
    
    # Likelihood with specified systematics 
    Pk_sys = NG.dataX(mock, ell=ell, rebin=rebin, sys=sys)
    ica_sys = NG.lnL_ica(Pk_sys, Pk_sys)
    
    prettyplot()
    fig = plt.figure()
    sub = fig.add_subplot(111)
    nbin = 32
    sub.hist(gauss, bins=nbin, range=[-2.2*Pk_nosys.shape[0], -0.8*Pk_nosys.shape[0]], 
            normed=True, alpha=0.75, label='Gaussian $\mathcal{L^\mathtt{pseudo}}$; no sys.')
    sub.hist(ica_nosys, bins=nbin, range=[-2.2*Pk_nosys.shape[0], -0.8*Pk_nosys.shape[0]], 
            normed=True, alpha=0.75, label='ICA; no sys.')
    sub.hist(ica_sys, bins=nbin, range=[-2.2*Pk_nosys.shape[0], -0.8*Pk_nosys.shape[0]], 
            normed=True, alpha=0.75, label='ICA; w/ sys.')
    sub.set_xlabel('log $\mathcal{L}$', fontsize=25)
    sub.set_xlim([-2.2*Pk_nosys.shape[0], -0.5*Pk_nosys.shape[0]])
    sub.legend(loc='upper left', prop={'size': 20}) 

    if rebin is None: # save fig
        f = ''.join([UT.fig_dir(), 'tests/test.lnL_sys.', mock, '.ell', str(ell), '.png'])
    else: 
        f = ''.join([UT.fig_dir(), 'tests/test.lnL_sys.', mock, '.ell', str(ell), '.rebin', str(rebin), '.png'])
    fig.savefig(f, bbox_inches='tight') 
    return None 
Example #4
0
def whiten(mock, ell=0, rebin=None, krange=None, method='choletsky'): 
    ''' ***TESTED: Choletsky decomposition fails for full binned Nseries
    P(k) because the precision matrix estimate is not positive definite***
    test the data whitening. 
    '''
    Pk = NG.dataX(mock, ell=ell, rebin=rebin, krange=krange)
    X, _ = NG.meansub(Pk)
    X_w, W = NG.whiten(X, method=method) # whitened data
    
    prettyplot()
    fig = plt.figure(figsize=(15,7))
    sub = fig.add_subplot(121)
    for i in range(X.shape[1]): 
        sub.plot(range(X_w.shape[0]), X_w[:,i])
    
    sub.set_xlim([0, X.shape[0]]) 
    sub.set_xlabel('$\mathtt{k}$ bins', fontsize=25)
    sub.set_ylim([-7., 7.])
    sub.set_ylabel('$\mathtt{W^{T} (P^i_'+str(ell)+'- \overline{P_'+str(ell)+'})}$', fontsize=25)
    
    C_Xw = np.cov(X_w.T)
    sub = fig.add_subplot(122)
    im = sub.imshow(C_Xw, interpolation='none')
    fig.colorbar(im, ax=sub) 
    
    if rebin is None: 
        f = ''.join([UT.fig_dir(), 'tests/test.whiten.', method, '.', mock, '.ell', str(ell), '.png'])
    else: 
        f = ''.join([UT.fig_dir(), 'tests/test.whiten.', method, '.', mock, '.ell', str(ell), '.rebin', str(rebin), '.png'])
    fig.savefig(f, bbox_inches='tight') 
    return None 
Example #5
0
def ica(mock, ell=0, rebin=None): 
    ''' *** TESTED *** 
    Test that the ICA works!
    '''
    Pk = NG.dataX(mock, ell=ell, rebin=rebin)
    X, _ = NG.meansub(Pk)
    X_w, W = NG.whiten(X) # whitened data
    X_ica, W = NG.Ica(X_w)
    
    # compare covariance? 
    C_X = np.cov(X.T)
    C_Xica = np.cov(X_ica.T) 

    prettyplot()
    fig = plt.figure(figsize=(20, 8))
    sub = fig.add_subplot(121)
    im = sub.imshow(np.log10(C_X), interpolation='none')
    sub.set_title('log(Cov.) of Data')
    fig.colorbar(im, ax=sub) 

    sub = fig.add_subplot(122)
    im = sub.imshow(C_Xica, interpolation='none')
    fig.colorbar(im, ax=sub) 
    sub.set_title('Cov. of ICA transformed Data')
    # save fig
    if rebin is None: 
        f = ''.join([UT.fig_dir(), 'tests/test.ICAcov.', mock, '.ell', str(ell), '.png'])
    else: 
        f = ''.join([UT.fig_dir(), 'tests/test.ICAcov.', mock, '.ell', str(ell), '.rebin', str(rebin), '.png'])
    fig.savefig(f, bbox_inches='tight') 
    return None
Example #6
0
def test_SumData(): 
    ''' Make sure abcee.SumData returns something sensible with some hardcoded values 
     
    Takes roughly 0.7 seconds 
    
    Notes
    -----
    * SumData returns sensible SMFs when compared to Li & White (2009) scaled by 
    1-f_sat from Wetzel et al.(2013).  
    '''
    t0 = time.time() 
    output = abcee.SumData(['smf'], info=True, nsnap0=15, sigma_smhm=0.2) 
    m_arr, phi_arr, err_arr = Obvs.MF_data()

    fcen = np.array([1. - Obvs.f_sat(mm, 0.05) for mm in m_arr]) 

    fig = plt.figure()
    sub = fig.add_subplot(111)
    sub.plot(output[0][0], output[0][1]) 
    sub.errorbar(m_arr, fcen * phi_arr, yerr=err_arr*np.sqrt(1./fcen)) 
    sub.set_xlim([9., 12.])
    sub.set_xlabel('$log\;M_*$', fontsize=25)
    sub.set_ylim([1e-6, 10**-1.75])
    sub.set_yscale('log')
    sub.set_ylabel('$\Phi$', fontsize=25)
    fig.savefig(''.join([UT.fig_dir(), 'tests/test.SumData.smf.png']), bbox_inches='tight') 
    return None 
Example #7
0
def lnL_pca_gauss(mock, ell=0, krange=None, NorS='ngc'): 
    ''' ***TESTED***
    Test that lnL_pca_gauss is consistent with chi-squared calculated directly 
    from the mocks with the covariance matrix. 
    '''
    # Calculate the lnL_pca_gauss
    Pk = NG.X_pk(mock, ell=ell, krange=krange, NorS=NorS)
    dpk , mu_pk = NG.meansub(Pk) 
    pca_gauss = NG.lnL_pca_gauss(dpk, Pk)

    # calculate chi-squared 
    C_X = np.cov(Pk.T) 
    chi2 = np.zeros(Pk.shape[0])
    for i in range(Pk.shape[0]): 
        chi2[i] = -0.5 * np.sum(np.dot(dpk[i,:], np.linalg.solve(C_X, dpk[i,:].T)))
    
    offset = chi2 - pca_gauss
    print offset

    fig = plt.figure()
    sub = fig.add_subplot(111)
    nbin = 32 
    sub.hist(pca_gauss, bins=nbin, normed=True, alpha=0.75, label='PCA Gaussian')
    sub.hist(chi2, bins=nbin, normed=True, alpha=0.75, label=r'$\chi^2$')
    sub.set_xlabel('log $\mathcal{L}$', fontsize=25)
    sub.set_xlim([-2.2*Pk.shape[1], 0.])
    sub.legend(loc='upper left', prop={'size': 20}) 

    f = ''.join([UT.fig_dir(), 'tests/test.lnL_pca_gauss_test.', mock, '.ell', str(ell), '.png'])
    fig.savefig(f, bbox_inches='tight') 
    return None 
Example #8
0
def gmf(Mr=20, Nmock=500): 
    '''
    Plot Group Multiplicty Function of fake observations 
    '''
    prettyplot() 
    pretty_colors = prettycolors() 
    
    # import fake obs GMF
    gmf, sig_gmf = Data.data_gmf(Mr=Mr, Nmock=Nmock)
    # group richness bins
    gmf_bin = Data.data_gmf_bins()

    fig = plt.figure(1) 
    sub = fig.add_subplot(111)

    sub.errorbar(
            0.5*(gmf_bin[:-1]+gmf_bin[1:]), gmf, yerr=sig_gmf,
            fmt="ok", capsize=1.0
            )
    sub.set_xlim([1, 60])
    sub.set_yscale('log')
    sub.set_ylabel(r"Group Multiplicity Function (h$^{3}$ Mpc$^{-3}$)", fontsize=20)
    sub.set_xlabel(r"$\mathtt{Group\;\;Richness}$", fontsize=20)
    # save to file 
    fig_file = ''.join([util.fig_dir(), 
        'gmf.Mr', str(Mr), '.Nmock', str(Nmock), '.png'])
    fig.savefig(fig_file, bbox_inches='tight')
    return None
Example #9
0
def test_SFMS_highz(run, T, nsnap=15, lit='lee', nsnap0=15, downsampled='14'): 
    ''' Compare the best-fit SFMS parameters from ABC to literature at high z  
    '''
    if lit == 'lee': 
        lit_label = 'Lee et al. (2015)'
    # median ABC theta 
    abcout = abcee.readABC(run, T)
    theta_med = [UT.median(abcout['theta'][:, i], weights=abcout['w'][:]) for i in range(len(abcout['theta'][0]))]

    subcat_sim = abcee.model(run, theta_med, nsnap0=nsnap0, downsampled=downsampled) 

    m_arr = np.arange(8., 12.1, 0.1)
    sfr_abc = Obvs.SSFR_SFMS(m_arr, UT.z_nsnap(nsnap), theta_SFMS=subcat_sim['theta_sfms']) + m_arr

    sfr_obv = Obvs.SSFR_SFMS_obvs(m_arr, UT.z_nsnap(nsnap), lit=lit) + m_arr
    
    fig = plt.figure()
    sub = fig.add_subplot(111)

    sub.fill_between(m_arr,  sfr_abc-0.3, sfr_abc+0.3, color='b', alpha=0.25, linewidth=0, edgecolor=None, 
            label=r'ABC $\theta_{median}$')
    sub.plot(m_arr, sfr_obv+0.3, ls='--', c='k', label=lit_label) 
    sub.plot(m_arr, sfr_obv-0.3, ls='--', c='k') 

    sub.set_xlim([8., 12.])
    sub.set_xlabel('$\mathtt{log\;M_*}$', fontsize=25)
    sub.set_ylim([-4., 3.])
    sub.set_ylabel('$\mathtt{log\;SFR}$', fontsize=25)
    sub.legend(loc='best') 
    fig.savefig(UT.fig_dir()+'SFMS.z'+str(round(UT.z_nsnap(nsnap),2))+'.'+run+'.'+lit+'.png', bbox_inches='tight')
    plt.close()
    return None 
def SFMS_z1(): 
    ''' Comparison of different SFMS observations at z~1.
    '''
    # Lee et al. (2015)
    logSFR_lee = lambda mm: 1.53 - np.log10(1 + (10**mm/10**(10.10))**-1.26)
    # Noeske et al. (2007) 0.85 < z< 1.10 (by eye)
    logSFR_noeske = lambda mm: (1.580 - 1.064)/(11.229 - 10.029)*(mm - 10.0285) + 1.0637
    # Moustakas et al. (2013) 0.8 < z < 1. (by eye)  
    logSFR_primus = lambda mm: (1.3320 - 1.296)/(10.49 - 9.555) * (mm-9.555) + 1.297
    
    # prior range
    logSFR_prior_min = lambda mm: (0.6 * (mm - 10.5) + 0.9 * (1.-0.0502) - 0.11) 
    logSFR_prior_max = lambda mm: (0.6 * (mm - 10.5) + 2. * (1.-0.0502) - 0.11) 


    fig = plt.figure(1)
    sub = fig.add_subplot(111)
    marr = np.linspace(9., 12., 100)
    sub.plot(marr, logSFR_lee(marr), label='Lee et al.(2015)')
    sub.plot(marr, logSFR_noeske(marr), label='Noeske et al.(2007)')
    sub.plot(marr, logSFR_primus(marr), label='Moustakas et al.(2013)')
    sub.fill_between(marr, logSFR_prior_min(marr), logSFR_prior_max(marr), label='Prior', alpha=0.5)
    sub.legend(loc='lower right') 
    sub.set_xlim([9.5, 11.5])
    fig.savefig(UT.fig_dir()+'test.z_1.sfms.png')
    plt.close() 
    return None
Example #11
0
def plotCMS_SMF_MS(cenque='default', evol_dict=None): 
    '''Plot the stellar mass function of the integrated SFR main sequence population 
    and compare it to the theoretic stellar mass function of the main sequence 
    population 
    '''
    # import evolved galaxy population 
    MSpop = CMS.EvolvedGalPop(cenque=cenque, evol_dict=evol_dict) 
    print MSpop.File()
    MSpop.Read() 
    MSonly = np.where(MSpop.t_quench == 999.)   # remove the quenching galaxies

    smf = Obvs.getSMF(MSpop.mass[MSonly], weights=MSpop.weight_down[MSonly])
    prettyplot() 
    pretty_colors = prettycolors() 

    # analytic SMF 
    theory_smf = Obvs.getSMF(MSpop.M_sham[MSonly], weights=MSpop.weight_down[MSonly])

    fig = plt.figure()
    sub = fig.add_subplot(111)

    sub.plot(theory_smf[0], theory_smf[1], lw=3, c='k', label='Theory')
    sub.plot(smf[0], smf[1], lw=3, c=pretty_colors[3], ls='--', label='MS Only')

    sub.set_ylim([10**-5, 10**-1])
    sub.set_xlim([6., 12.0])
    sub.set_yscale('log')
    sub.set_xlabel(r'Mass $\mathtt{M_*}$', fontsize=25) 
    sub.set_ylabel(r'Stellar Mass Function $\mathtt{\Phi}$', fontsize=25) 
    sub.legend(loc='upper right', frameon=False)
    
    fig_file = ''.join([UT.fig_dir(), 'SMF_MS.CMS', MSpop._Spec_str(), '.png']) 
    fig.savefig(fig_file, bbox_inches='tight') 
    plt.close() 
    return None
def test_Svd():
    '''
    '''
    k_arr, pk_lhd = Dat.X_lhd(0.0,
                              1,
                              4,
                              1,
                              obvs='pk',
                              ell=0,
                              Nmesh=360,
                              rsd=True,
                              krange=[0.01, 0.5],
                              karr=True,
                              HODrange='sinha2017prior_narrow',
                              samples=40,
                              method='mdu',
                              silent=True)
    svd = Comp.Svd()
    _ = svd.fit(pk_lhd)
    pcs = svd.transform(pk_lhd)
    assert np.allclose(svd.inv_transform(pcs), pk_lhd)

    fig = plt.figure()
    sub = fig.add_subplot(111)

    sub.plot(range(len(svd.exp_var_ratio) + 1),
             np.concatenate([np.array([0]),
                             np.cumsum(svd.exp_var_ratio)]))
    sub.set_ylim([5e-1, 1.])
    sub.set_yscale('log')

    f = ''.join([UT.fig_dir(), 'tests/plk_svd.exp_var.png'])
    fig.savefig(f, bbox_inches='tight')
    return None
def test_PC(n_comp):
    '''
    '''
    k_arr, pk_lhd = Dat.X_lhd(0.0,
                              1,
                              4,
                              1,
                              obvs='pk',
                              ell=0,
                              Nmesh=360,
                              rsd=True,
                              krange=[0.01, 0.5],
                              karr=True,
                              HODrange='sinha2017prior_narrow',
                              samples=40,
                              method='mdu',
                              silent=True)
    svd = Comp.Svd(n_comp=n_comp)
    _ = svd.fit(pk_lhd)
    pcs = svd.transform(pk_lhd)

    fig = plt.figure(figsize=(4 * n_comp, 4))
    for i in range(n_comp):
        sub = fig.add_subplot(1, n_comp, i + 1)
        sub.hist(pcs[:, i], normed=True)
        sub.set_xlabel('PC ' + str(i + 1), fontsize=20)
    f = ''.join([UT.fig_dir(), 'tests/plk_svd.pc.', str(n_comp), 'comp.png'])
    fig.savefig(f, bbox_inches='tight')
    return None
Example #14
0
def xi(Mr=20, Nmock=500): 
    '''
    Plot xi(r) of the fake observations
    '''
    prettyplot() 
    pretty_colors = prettycolors() 

    xir, cii = Data.data_xi(Mr=Mr, Nmock=Nmock)
    rbin = Data.data_xi_bins(Mr=Mr) 
    
    fig = plt.figure(1) 
    sub = fig.add_subplot(111)
    sub.plot(rbin, rbin*xir, c='k', lw=1)
    sub.errorbar(rbin, rbin*xir, yerr = rbin*cii**0.5 , fmt="ok", ms=1, capsize=2, alpha=1.)

    sub.set_xlim([0.1, 15])
    sub.set_ylim([1, 10])
    sub.set_yscale("log")
    sub.set_xscale("log")

    sub.set_xlabel(r'$\mathtt{r}\; (\mathtt{Mpc})$', fontsize=25)
    sub.set_ylabel(r'$\mathtt{r} \xi_{\rm gg}$', fontsize=25)

    fig_file = ''.join([util.fig_dir(),
        'xi.Mr', str(Mr), '.Nmock', str(Nmock), '.png'])
    fig.savefig(fig_file, bbox_inches='tight')
    plt.close()
    return None
Example #15
0
def lnL(mock, ell=0, rebin=None, krange=None): 
    ''' Test the ICA likelihood estimation and pseudo gaussian likelihood 
    '''
    Pk = NG.dataX(mock, ell=ell, rebin=rebin, krange=krange)
    ica = NG.lnL_ica(Pk, Pk) 
    gauss = NG.lnL_pca_gauss(Pk, Pk)
    
    prettyplot()
    fig = plt.figure()
    sub = fig.add_subplot(111)
    nbin = 32
    sub.hist(gauss, bins=nbin, range=[-2.2*Pk.shape[1], -0.8*Pk.shape[1]], 
            normed=True, alpha=0.75, label='Gaussian $\mathcal{L^\mathtt{pseudo}}$')
    sub.hist(ica, bins=nbin, range=[-2.2*Pk.shape[1], -0.8*Pk.shape[1]], 
            normed=True, alpha=0.75, label='ICA')
    sub.set_xlabel('log $\mathcal{L}$', fontsize=25)
    sub.set_xlim([-2.2*Pk.shape[1], -0.5*Pk.shape[1]])
    sub.legend(loc='upper left', prop={'size': 20}) 

    str_rebin = ''
    if rebin is not None: 
        str_rebin = '.rebin'+str(rebin)
    str_krange = ''
    if krange is not None: 
        str_krange = '.kmin'+str(krange[0])+'.kmax'+str(krange[1])
    
    f = ''.join([UT.fig_dir(), 'tests/test.lnL.', mock, '.ell', str(ell), 
        str_rebin, str_krange, '.png'])
    fig.savefig(f, bbox_inches='tight') 
    return None 
Example #16
0
def Plk_fiducial(nreal): 
    ''' ***TESTED*** 
    Test that the methods Data.X_fid and Data.Fiducial_Obvs
    are returning sensible results for obvs = 'plk'
    '''
    karr, Xfid = Data.X_fid(nreal, 4, obvs='plk', karr=True)
    muX = np.sum(Xfid, axis=0)/float(Xfid.shape[0])  
    sigX = np.sqrt(np.diag(np.cov(Xfid.T)))
    
    fig = plt.figure()
    sub = fig.add_subplot(111)
    sub.errorbar(karr, muX, yerr=sigX, fmt='.k') 
    for i in range(Xfid.shape[0]): 
        sub.plot(karr, Xfid[i,:], c='k', lw=0.1) 

    sub.set_xlim([1e-2, 0.5]) 
    sub.set_xlabel('$k$', fontsize=20) 
    sub.set_xscale('log') 
    sub.set_ylim([2e3, 2e5]) 
    sub.set_ylabel('$P(k)$', fontsize=20) 
    sub.set_yscale('log') 

    f = ''.join([UT.fig_dir(), 'tests/test_data.Plk_fiducial.png']) 
    fig.savefig(f, bbox_inches='tight') 
    return None
Example #17
0
def TrueObservables(Mr=21, b_normal=0.25): 
    ''' Plot xi and gmf for the data 
    '''
    # xi data 
    xi_data = Data.data_xi(Mr=Mr, b_normal=b_normal)
    cov_data = Data.data_cov(Mr=Mr, b_normal=b_normal, inference='mcmc') 
    data_xi_cov = cov_data[1:16, 1:16]
    xi_r_bin = Data.data_xi_bin(Mr=Mr)
    # gmf data 
    data_gmf = Data.data_gmf(Mr=Mr, b_normal=b_normal)
    cov_data = Data.data_cov(Mr=Mr, b_normal=b_normal, inference='mcmc') 
    data_gmf_cov = cov_data[16:, 16:]

    r_binedge = Data.data_gmf_bins()
    gmf_r_bin = 0.5 * (r_binedge[:-1] + r_binedge[1:]) 
   
    prettyplot()
    pretty_colors = prettycolors() 

    fig = plt.figure(figsize=(12,6))
    sub_xi = fig.add_subplot(121) 
    sub_gmf = fig.add_subplot(122)
   
    #sub_xi.errorbar(xi_r_bin, xi_data, yerr = np.sqrt(np.diag(data_xi_cov)), fmt="o", color='k', 
    #        markersize=0, lw=0, capsize=3, elinewidth=1.5)
    #sub_xi.scatter(xi_r_bin, xi_data, c='k', s=10, lw=0)
    sub_xi.fill_between(xi_r_bin, 
            xi_data-np.sqrt(np.diag(data_xi_cov)), xi_data+np.sqrt(np.diag(data_xi_cov)), 
            color=pretty_colors[1])
    sub_xi.set_yscale('log')
    sub_xi.set_xscale('log')
    sub_xi.set_xlim(0.1, 20)
    sub_xi.set_xlabel(r'$\mathtt{r}\; [\mathtt{Mpc}/h]$', fontsize=25)
    sub_xi.set_ylabel(r'$\xi(r)$', fontsize=25)

    #sub_gmf.errorbar(gmf_r_bin, data_gmf, yerr=np.sqrt(np.diag(data_gmf_cov)), 
    #        fmt="o", color='k', 
    #        markersize=0, lw=0, capsize=4, elinewidth=2)
    #sub_gmf.scatter(gmf_r_bin, data_gmf, s=15, lw=0, c='k', label='Mock Observation')
    sub_gmf.fill_between(gmf_r_bin, 
            data_gmf-np.sqrt(np.diag(data_gmf_cov)), data_gmf+np.sqrt(np.diag(data_gmf_cov)), 
            color=pretty_colors[1])
    sub_gmf.set_xlim(1, 20)
    sub_gmf.set_xlabel(r'$\mathtt{N}$ (Group Richness)', fontsize=25)

    sub_gmf.yaxis.tick_right()
    sub_gmf.yaxis.set_ticks_position('both')
    sub_gmf.yaxis.set_label_position('right') 
    sub_gmf.set_ylim([10**-7, 2.0*10**-4])
    sub_gmf.set_yscale('log')
    sub_gmf.set_ylabel(r'$\zeta(\mathtt{N})$', fontsize=25)

    fig.subplots_adjust(hspace=0.05)
    fig_name = ''.join([ut.fig_dir(), 
        'paper.data_observables', 
        '.pdf'])
    fig.savefig(fig_name, bbox_inches='tight', dpi=150) 
    plt.close()
    return None 
Example #18
0
def TrueObservables(Mr=21, b_normal=0.25):
    ''' Plot xi and gmf for the data 
    '''
    # xi data
    xi_data = Data.data_xi(Mr=Mr, b_normal=b_normal)
    cov_data = Data.data_cov(Mr=Mr, b_normal=b_normal, inference='mcmc')
    data_xi_cov = cov_data[1:16, 1:16]
    xi_r_bin = Data.data_xi_bin(Mr=Mr)
    # gmf data
    data_gmf = Data.data_gmf(Mr=Mr, b_normal=b_normal)
    cov_data = Data.data_cov(Mr=Mr, b_normal=b_normal, inference='mcmc')
    data_gmf_cov = cov_data[16:, 16:]

    r_binedge = Data.data_gmf_bins()
    gmf_r_bin = 0.5 * (r_binedge[:-1] + r_binedge[1:])

    prettyplot()
    pretty_colors = prettycolors()

    fig = plt.figure(figsize=(12, 6))
    sub_xi = fig.add_subplot(121)
    sub_gmf = fig.add_subplot(122)

    #sub_xi.errorbar(xi_r_bin, xi_data, yerr = np.sqrt(np.diag(data_xi_cov)), fmt="o", color='k',
    #        markersize=0, lw=0, capsize=3, elinewidth=1.5)
    #sub_xi.scatter(xi_r_bin, xi_data, c='k', s=10, lw=0)
    sub_xi.fill_between(xi_r_bin,
                        xi_data - np.sqrt(np.diag(data_xi_cov)),
                        xi_data + np.sqrt(np.diag(data_xi_cov)),
                        color=pretty_colors[1])
    sub_xi.set_yscale('log')
    sub_xi.set_xscale('log')
    sub_xi.set_xlim(0.1, 20)
    sub_xi.set_xlabel(r'$\mathtt{r}\; [\mathtt{Mpc}/h]$', fontsize=25)
    sub_xi.set_ylabel(r'$\xi(r)$', fontsize=25)

    #sub_gmf.errorbar(gmf_r_bin, data_gmf, yerr=np.sqrt(np.diag(data_gmf_cov)),
    #        fmt="o", color='k',
    #        markersize=0, lw=0, capsize=4, elinewidth=2)
    #sub_gmf.scatter(gmf_r_bin, data_gmf, s=15, lw=0, c='k', label='Mock Observation')
    sub_gmf.fill_between(gmf_r_bin,
                         data_gmf - np.sqrt(np.diag(data_gmf_cov)),
                         data_gmf + np.sqrt(np.diag(data_gmf_cov)),
                         color=pretty_colors[1])
    sub_gmf.set_xlim(1, 20)
    sub_gmf.set_xlabel(r'$\mathtt{N}$ (Group Richness)', fontsize=25)

    sub_gmf.yaxis.tick_right()
    sub_gmf.yaxis.set_ticks_position('both')
    sub_gmf.yaxis.set_label_position('right')
    sub_gmf.set_ylim([10**-7, 2.0 * 10**-4])
    sub_gmf.set_yscale('log')
    sub_gmf.set_ylabel(r'$\zeta(\mathtt{N})$', fontsize=25)

    fig.subplots_adjust(hspace=0.05)
    fig_name = ''.join([ut.fig_dir(), 'paper.data_observables', '.pdf'])
    fig.savefig(fig_name, bbox_inches='tight', dpi=150)
    plt.close()
    return None
Example #19
0
def plotCMS_SFH_AsBias(cenque='default', evol_dict=None): 
    ''' Plot the star formation history of star forming main sequence galaxies
    and their corresponding halo mass acretion history 
    '''
    # import evolved galaxy population 
    egp = CMS.EvolvedGalPop(cenque=cenque, evol_dict=evol_dict) 
    egp.Read() 

    MSonly = np.where(egp.t_quench == 999.)     # only keep main-sequence galaxies
    MSonly_rand = np.random.choice(MSonly[0], size=100)

    z_acc, t_acc = UT.zt_table()
    t_cosmic  = t_acc[1:16]
    dt = 0.1
    
    t_arr = np.arange(t_cosmic.min(), t_cosmic.max()+dt, dt)

    prettyplot() 
    pretty_colors = prettycolors() 
    fig = plt.figure()
    sub1 = fig.add_subplot(211)
    sub2 = fig.add_subplot(212)
    
    dsfrt = np.zeros((len(t_arr), len(MSonly_rand)))
    for i_t, tt in enumerate(t_arr): 
        dsfr = SFR.dSFR_MS(tt, egp.sfh_dict) 
        for ii, i_gal in enumerate(MSonly_rand): 
            dsfrt[i_t,ii] = dsfr[i_gal]
    
    i_col = 0 
    for ii, i_gal in enumerate(MSonly_rand): 
        tlim = np.where(t_arr > egp.tsnap_genesis[i_gal]) 

        tcoslim = np.where(t_cosmic >= egp.tsnap_genesis[i_gal]) 
        halo_hist = (egp.Mhalo_hist[i_gal])[tcoslim][::-1]
        halo_exists = np.where(halo_hist != -999.) 
        if np.power(10, halo_hist[halo_exists]-egp.halo_mass[i_gal]).min() < 0.7:  
            if i_col < 5: 
                sub1.plot(t_arr[tlim], dsfrt[:,ii][tlim], lw=3, c=pretty_colors[i_col])
                sub2.plot(t_cosmic[tcoslim][::-1][halo_exists], 
                        np.power(10, halo_hist[halo_exists]-egp.halo_mass[i_gal]), 
                        lw=3, c=pretty_colors[i_col])
                i_col += 1 

    sub1.set_xlim([5.0, 13.5])
    sub1.set_xticklabels([])
    sub1.set_ylim([-1., 1.])
    sub1.set_ylabel(r'$\mathtt{SFR(t) - <SFR_{MS}(t)>}$', fontsize=25)
    
    sub2.set_xlim([5.0, 13.5])
    sub2.set_xlabel(r'$\mathtt{t_{cosmic}}$', fontsize=25)
    sub2.set_ylim([0.0, 1.1])
    sub2.set_ylabel(r'$\mathtt{M_h(t)}/\mathtt{M_h(t_f)}$', fontsize=25)
    fig_file = ''.join([UT.fig_dir(), 'SFH_AsBias.CMS', egp._Spec_str(), '.png']) 
    fig.savefig(fig_file, bbox_inches='tight') 
    plt.close()
    return None
Example #20
0
def plotCMS_SFMS(cenque='default', evol_dict=None):
    ''' Plot the SFR-M* relation of the SFMS and compare to expectations
    '''
    # import evolved galaxy population 
    cms = CMS.CentralMS(cenque=cenque)
    cms._Read_CenQue()
    MSpop = CMS.EvolvedGalPop(cenque=cenque, evol_dict=evol_dict) 
    MSpop.Read() 
    MSonly = np.where(MSpop.t_quench == 999.) 
    
    delta_m = 0.5
    mbins = np.arange(MSpop.mass[MSonly].min(), MSpop.mass[MSonly].max(), delta_m) 
    
    sfr_percent = np.zeros((len(mbins)-1, 4))
    for im, mbin in enumerate(mbins[:-1]):  
        within = np.where(
                (MSpop.mass[MSonly] > mbin) & 
                (MSpop.mass[MSonly] <= mbin + delta_m) 
                ) 
        #sfr_percent[im,:] = np.percentile(MSpop.sfr[MSonly[0][within]], [2.5, 16, 84, 97.5])
        sfr_percent[im,:] = UT.weighted_quantile(MSpop.sfr[MSonly[0][within]], 
                [0.025, 0.16, 0.84, 0.975], weights=MSpop.weight_down[MSonly[0][within]])
        
    prettyplot() 
    pretty_colors = prettycolors()
    fig = plt.figure()
    sub = fig.add_subplot(111)

    # parameterized observed SFMS 
    obvs_mu_sfr = SFR.AverageLogSFR_sfms(0.5*(mbins[:-1]+mbins[1:]), 0.05, sfms_dict=cms.sfms_dict)
    obvs_sig_sfr = SFR.ScatterLogSFR_sfms(0.5*(mbins[:-1]+mbins[1:]), 0.05, sfms_dict=cms.sfms_dict)
    sub.fill_between(0.5*(mbins[:-1] + mbins[1:]), 
            obvs_mu_sfr - 2.*obvs_sig_sfr, obvs_mu_sfr + 2.*obvs_sig_sfr, 
            color=pretty_colors[1], alpha=0.3, edgecolor=None, lw=0) 
    sub.fill_between(0.5*(mbins[:-1] + mbins[1:]), 
            obvs_mu_sfr - obvs_sig_sfr, obvs_mu_sfr + obvs_sig_sfr, 
            color=pretty_colors[1], alpha=0.5, edgecolor=None, lw=0, label='Observations') 

    # simulation 
    sub.fill_between(0.5*(mbins[:-1] + mbins[1:]), sfr_percent[:,0], sfr_percent[:,-1],  
            color=pretty_colors[3], alpha=0.3, edgecolor=None, lw=0) 
    sub.fill_between(0.5*(mbins[:-1] + mbins[1:]), sfr_percent[:,1], sfr_percent[:,-2], 
            color=pretty_colors[3], alpha=0.5, edgecolor=None, lw=0, label='Simulated') 

    # x,y axis
    sub.set_xlim([9.0, 12.0]) 
    sub.set_xlabel(r'$\mathtt{M_*}\; [\mathtt{M}_\odot]$', fontsize=25)
    sub.set_ylim([-2., 1.]) 
    sub.set_ylabel(r'$\mathtt{SFR}\; [\mathtt{M}_\odot/\mathtt{yr}]$', fontsize=25)

    sub.legend(loc='lower right', numpoints=1, markerscale=1.) 

    fig_file = ''.join([UT.fig_dir(), 'SFMS.CMS', MSpop._Spec_str(), '.png'])
    plt.savefig(fig_file, bbox_inches='tight') 
    plt.close()
    return None
Example #21
0
def whiten_recon(mock, ell=0, rebin=None, krange=None, method='choletsky'): 
    ''' ***TESTED: The whitening matrices reconstruct the P(k)s*** 
    Test whether P(k) can be reconstructed using the whitening matrix  
    '''
    Pk, k = NG.dataX(mock, ell=ell, rebin=rebin, krange=krange, k_arr=True)
    X, mu_X = NG.meansub(Pk)
    X_w, W = NG.whiten(X, method=method) # whitened data
    
    prettyplot()
    fig = plt.figure(figsize=(15,7))
    sub = fig.add_subplot(121)
    for i in range(X.shape[0]): 
        sub.plot(k, Pk[i,:])
    if krange is None: 
        sub.set_xlim([1e-3, 0.5])
    else: 
        sub.set_xlim(krange)
    sub.set_xscale('log')
    sub.set_xlabel('$\mathtt{k}$', fontsize=25)
    sub.set_yscale('log') 
    sub.set_ylim([2e3, 2.5e5])
    
    np.random.seed(7)
    sub = fig.add_subplot(122)
    for i in range(X.shape[0]): 
        X_noise = np.random.normal(size=X_w.shape[1])
        X_rec = np.linalg.solve(W.T, X_noise.T)
        sub.plot(k, X_rec.T + mu_X)
    if krange is None: 
        sub.set_xlim([1e-3, 0.5])
    else: 
        sub.set_xlim(krange)
    sub.set_xscale('log')
    sub.set_xlabel('$\mathtt{k}$', fontsize=25)
    sub.set_yscale('log') 
    sub.set_ylim([2e3, 2.5e5])

    if rebin is None: 
        f = ''.join([UT.fig_dir(), 'tests/test.whiten_recon.', method, '.', mock, '.ell', str(ell), '.png'])
    else: 
        f = ''.join([UT.fig_dir(), 'tests/test.whiten_recon.', method, '.', mock, '.ell', str(ell), '.rebin', str(rebin), '.png'])
    fig.savefig(f, bbox_inches='tight') 
    return None 
Example #22
0
def Plk_halo(mneut=0.0, nzbin=4, zspace=False):
    ''' **TESTED --- Nov 7, 2017 ** 
    Test the Plk_halo 
    '''
    p0ks, p2ks, p4ks = [], [], []
    for ireal in range(1, 101):
        # read all 100 realizations
        plk_i = Obvs.Plk_halo(mneut, ireal, nzbin, zspace=zspace)
        if ireal == 1: k = plk_i['k']
        p0ks.append(plk_i['p0k'])
        p2ks.append(plk_i['p2k'])
        p4ks.append(plk_i['p4k'])

    fig = plt.figure()
    sub = fig.add_subplot(111)
    for p0k, p2k, p4k in zip(p0ks, p2ks, p4ks):
        sub.plot(k, k * p0k, c='k', lw=0.1)
        sub.plot(k, k * p2k, c='b', lw=0.1)
        sub.plot(k, k * p4k, c='r', lw=0.1)
    # plot the average
    sub.plot(k,
             k * np.average(np.array(p0ks), axis=0),
             c='k',
             lw=2,
             ls='--',
             label='$\ell=0$')
    sub.plot(k,
             k * np.average(np.array(p2ks), axis=0),
             c='b',
             lw=2,
             ls='--',
             label='$\ell=2$')
    sub.plot(k,
             k * np.average(np.array(p4ks), axis=0),
             c='r',
             lw=2,
             ls='--',
             label='$\ell=4$')

    sub.set_xlim([0.01, 0.15])
    sub.set_xlabel('k', fontsize=20)

    sub.set_ylim([-2000., 2500.])
    sub.set_ylabel('$k P(k)$', fontsize=20)
    sub.legend(loc='lower right', prop={'size': 15})
    if zspace: str_space = 'z'
    else: str_space = 'r'
    fig.savefig(''.join([
        UT.fig_dir(), 'tests/plk_halo.',
        str(mneut), 'eV.nzbin',
        str(nzbin), '.', str_space, 'space.png'
    ]),
                bbox_inches='tight')
    return None
Example #23
0
def plotCMS_SMHMR_MS(cenque='default', evol_dict=None): 
    ''' Plot the Stellar Mass to Halo Mass Relation of the evolved Gal population 
    '''
    # import evolved galaxy population 
    egp = CMS.EvolvedGalPop(cenque=cenque, evol_dict=evol_dict) 
    egp.Read() 
    
    # Calculate the SMHMR for the EvolvedGalPop
    m_halo_bin = np.arange(10., 15.25, 0.25)    # M_halo bins 
    m_halo_mid = 0.5 * (m_halo_bin[:-1] + m_halo_bin[1:]) # M_halo bin mid

    smhmr = np.zeros((len(m_halo_bin)-1, 5)) 
    for im, m_mid in enumerate(m_halo_mid): 
        inbin = np.where(
                (egp.halo_mass > m_halo_bin[im]) &
                (egp.halo_mass <= m_halo_bin[im+1])) 
        smhmr[im,:] = np.percentile(egp.mass[inbin], [2.5, 16, 50, 84, 97.5])
        #smhmr[im,:] = UT.weighted_quantile(egp.mass[inbin], [0.025, 0.16, 0.50, 0.84, 0.975], 
        #        weights=egp.weight_down[inbin])

    # "observations" with observed scatter of 0.2 dex 
    obvs_smhmr = np.zeros((len(m_halo_bin)-1, 2))
    obvs_smhmr[:,0] = smhmr[:,2] - 0.2      
    obvs_smhmr[:,1] = smhmr[:,2] + 0.2      

    prettyplot() 
    pretty_colors = prettycolors() 
    fig = plt.figure()
    sub = fig.add_subplot(111)

    sub.fill_between(m_halo_mid, smhmr[:,0], smhmr[:,-1], 
        color=pretty_colors[1], alpha=0.25, edgecolor=None, lw=0) 
    sub.fill_between(m_halo_mid, smhmr[:,1], smhmr[:,-2], 
        color=pretty_colors[1], alpha=0.5, edgecolor=None, lw=0, 
        label=r'$\mathtt{Simulated}$') 
    
    sub.plot(m_halo_mid, obvs_smhmr[:,0], 
        color='k', ls='--', lw=3) 
    sub.plot(m_halo_mid, obvs_smhmr[:,1], 
        color='k', ls='--', lw=3, 
        label=r"$\sigma = \mathtt{0.2}$") 

    sub.set_xlim([10., 15.0])
    sub.set_ylim([9., 12.0])
    sub.set_xlabel(r'$\mathtt{log}\;\mathtt{M_{halo}}\;\;[\mathtt{M_\odot}]$', fontsize=25) 
    sub.set_ylabel(r'$\mathtt{log}\;\mathtt{M_{*}}\;\;[\mathtt{M_\odot}]$', fontsize=25) 

    sub.legend(loc='upper right') 
    
    fig_file = ''.join([UT.fig_dir(), 'SMHMR_MS.CMS', egp._Spec_str(), '.png']) 
    fig.savefig(fig_file, bbox_inches='tight') 
    plt.close() 
    return None
def SFMS_Mfid_z(): 
    ''' Comparison of different SFMS observations from z~0.05 to 1 at fiducial 
    mass log M = 10.5. We include the compilation fit from Speagle et al. (2014).  
    These comparisons are mainly intended to determine whether the prior for 
    the logSFR_MS parameters are sensible. 
    '''
    # z = 0 
    # fit from SDSS group catalog
    logSFR_sdss = lambda mm: (0.5757 * (mm - 10.5) - 0.13868)

    # Lee et al. (2015)
    logSFR_lee = lambda mm: 1.53 - np.log10(1 + (10**mm/10**(10.10))**-1.26)
    # Noeske et al. (2007) 0.85 < z< 1.10 (by eye)
    logSFR_noeske = lambda mm: (1.580 - 1.064)/(11.229 - 10.029)*(mm - 10.0285) + 1.0637
    # Moustakas et al. (2013) 0.8 < z < 1. (by eye)  
    logSFR_primus = lambda mm: (1.3320 - 1.296)/(10.49 - 9.555) * (mm-9.555) + 1.297

    # Speagle's SFMS bestfit
    logSFR_speagle = lambda mm, tt: (0.84 - 0.026 * tt) * mm - (6.51 - 0.11 * tt) 
   
    logSFR_min = lambda mm, zz: logSFR_sdss(mm) + (zz - 0.05) 
    logSFR_max = lambda mm, zz: logSFR_sdss(mm) + 2. * (zz - 0.05)
 
    zarr = np.linspace(0., 1., 5) 
    tarr = UT.t_from_z(zarr) 

    fig = plt.figure(1)
    sub = fig.add_subplot(111) 
    sub.scatter([0.05], [logSFR_sdss(10.5)], label='SDSS group catalog')  
    sub.scatter([0.95], [logSFR_lee(10.5)], label='Lee et al.(2015)')
    sub.scatter([1.00], [logSFR_noeske(10.5)], label='Noeske et al.(2007)')
    sub.scatter([0.97], [logSFR_primus(10.5)], label='Moustakas et al.(2013)')
    sub.plot(zarr, [logSFR_speagle(10.5, tt) for tt in tarr], 
            c='k', lw=2, label='Speagle')  
    sub.plot(zarr, 
            [logSFR_speagle(10.5, tt) - \
                    (logSFR_speagle(10.5, UT.t_from_z(0.05)) - logSFR_sdss(10.5)) 
                    for tt in tarr], 
            c='k', lw=2, ls='--', label='Speagle offset')  

    sub.fill_between(zarr, 
            [logSFR_min(10.5, zz) for zz in zarr], 
            [logSFR_max(10.5, zz) for zz in zarr], label='Prior', alpha=0.5)

    sub.legend(loc='lower right') 
    sub.set_xlim([0., 1.])
    sub.set_xlabel('Redshift ($z$)', fontsize=20) 
    sub.set_ylim([-2, 2])
    sub.set_ylabel('log SFR ($M_{*,fid} = 10^{10.5} M_\odot$)', fontsize=20) 

    fig.savefig(UT.fig_dir()+'test.sfms.Mfid.z.png', bbox_inches='tight')
    plt.close() 
    return None
Example #25
0
def test_dMh_dMstar(run, theta, sigma_smhm=0.2, nsnap0=15, downsampled='14', flag=None): 
    '''
    '''
    subcat_sim = abcee.model(run, theta, 
            nsnap0=nsnap0, sigma_smhm=sigma_smhm, downsampled=downsampled) 

    isSF = np.where((subcat_sim['gclass'] == 'sf') & 
            (subcat_sim['weights'] > 0.) & 
            (subcat_sim['nsnap_start'] == nsnap0))[0] # only SF galaxies 
    assert subcat_sim['m.star'][isSF].min() > 0.
    #print subcat_sim['m.star'][isSF].min(), subcat_sim['m.star'][isSF].max() 
    #print subcat_sim['m.star0'][isSF].min(), subcat_sim['m.star0'][isSF].max() 

    dMh = np.log10(10**subcat_sim['halo.m'][isSF] - 10**subcat_sim['halo.m0'][isSF])
    dMstar = np.log10(10**subcat_sim['m.star'][isSF] - 10**subcat_sim['m.star0'][isSF])

    fig = plt.figure(figsize=(26,6)) 
    sub = fig.add_subplot(141)
    scat = sub.scatter(subcat_sim['halo.m0'][isSF], subcat_sim['halo.m'][isSF], 
            lw=0, s=5, c='k', cmap='hot') 
    sub.set_xlim([9.75, 14.5])
    sub.set_xlabel('$\mathtt{log\;M_h(z_0 \sim 1)}$', fontsize=20)
    sub.set_ylim([9.75, 14.5])
    sub.set_ylabel('$\mathtt{log\;M_h(z_f \sim 0)}$', fontsize=20)
    sub = fig.add_subplot(142)
    scat = sub.scatter(subcat_sim['m.star0'][isSF], subcat_sim['m.star'][isSF], 
            lw=0, s=5, c='k', cmap='hot') 
    sub.set_xlim([7., 12.])
    sub.set_xlabel('$\mathtt{log\;M_*(z_0 \sim 1)}$', fontsize=20)
    sub.set_ylim([7., 12.])
    sub.set_ylabel('$\mathtt{log\;M_*(z_f \sim 0)}$', fontsize=20)

    sub = fig.add_subplot(143)
    scat = sub.scatter(dMh, dMstar, lw=0, s=5, c=subcat_sim['halo.m'][isSF], cmap='hot', 
            vmin=10., vmax=14.5) 
    fig.colorbar(scat, ax=sub)
    sub.set_xlim([8., 14.5])
    sub.set_xlabel('$\mathtt{log(\; \Delta M_h\;)}$', fontsize=20)
    sub.set_ylim([9, 11.25])
    sub.set_ylabel('$\mathtt{log(\; \Delta M_*\;)}$', fontsize=20)
    
    plt.show()
    raise ValueError
    if flag is None: 
        flag_str = ''
    else: 
        flag_str = '.'+flag 
    
    fig.savefig(''.join([UT.fig_dir(), 'dMh_dMstar.', run, '.sig_smhm', str(sigma_smhm), 
        flag_str, '.png']), bbox_inches='tight')
    plt.close() 
    return None 
Example #26
0
def test_tdelay_dt_grid(run, tduty):
    file_name = ''.join([UT.fig_dir(), 'tduty_tdelay_dt_grid.', run, '.p'])
    grid = pickle.load(open(file_name, 'rb'))
    
    tdutys = grid[0,:]
    tdelay = grid[1,:] 
    dt_abias = grid[2,:]
    sigMstar = grid[3,:]
    l2 = grid[4,:]
    
    duty = np.where(tdutys == tduty)

    fig = plt.figure() 
    sub = fig.add_subplot(111)
    scat = sub.scatter(dt_abias[duty], tdelay[duty], c=sigMstar[duty], lw=0, s=100)
    plt.colorbar(scat)
    sub.set_xlabel('$\Delta t$ Gyr', fontsize=25)
    sub.set_ylabel('$t_{delay}$ Gyr', fontsize=25)

    fig_name = ''.join([UT.fig_dir(), 'tdelay_dt_grid.', run, '.tduty', str(tduty), '.png'])
    fig.savefig(fig_name, bbox_inches='tight') 
    return None
Example #27
0
def thetaLHD():
    ''' ***TESTED***
    Test thetaLHD using the HOD parameter priors 
    '''
    # from Sinha et al (2017) prior
    theta_lbl = [
        'log $M_\mathrm{min}$', '$\sigma_{\mathrm{log}\,M}$', 'log $M_0$',
        'log $M_1$', r'$\alpha$'
    ]
    HOD_range_min = [11., 0.001, 6., 12., 0.001]
    HOD_range_max = [12.2, 1., 14., 14., 2.]

    samples = 17
    m_list = ['maximin', 'centermaximin', 'mdu', 'nohl']
    for meth in m_list:
        lhcube = lhd.thetaLHD([HOD_range_min, HOD_range_max],
                              samples=samples,
                              method=meth)

        fig = plt.figure(figsize=(9, 9))
        for i in range(lhcube.shape[1]):
            for j in range(lhcube.shape[1]):
                if i < j:
                    sub = fig.add_subplot(lhcube.shape[1], lhcube.shape[1],
                                          lhcube.shape[1] * j + i + 1)
                    sub.scatter(lhcube[:, i], lhcube[:, j])
                    sub.set_xlim([HOD_range_min[i], HOD_range_max[i]])
                    if j == lhcube.shape[1] - 1:
                        sub.set_xlabel(theta_lbl[i], fontsize=20)
                    else:
                        sub.set_xticks([])
                    sub.set_ylim([HOD_range_min[j], HOD_range_max[j]])
                    if i == 0: sub.set_ylabel(theta_lbl[j], fontsize=20)
                    else: sub.set_yticks([])
                elif i == j:
                    sub = fig.add_subplot(lhcube.shape[1], lhcube.shape[1],
                                          lhcube.shape[1] * j + i + 1)
                    sub.hist(lhcube[:, i],
                             range=[HOD_range_min[i], HOD_range_max[i]],
                             normed=True)
                    sub.set_xlim([HOD_range_min[i], HOD_range_max[i]])
                    if i != 0: sub.set_yticks([])
                    else: sub.set_ylabel(theta_lbl[j], fontsize=20)
                    if i != lhcube.shape[1] - 1: sub.set_xticks([])
                    else: sub.set_xlabel(theta_lbl[i], fontsize=20)
        f = ''.join([
            UT.fig_dir(), 'tests/test.thetaLHD.HOD.', meth, '.',
            str(samples), '.samples.png'
        ])
        fig.savefig(f, bbox_inches='tight')
    return None
def SFMS_comparison(): 
    ''' Comparison of different SFMS observations at z~0.05 and 1. We 
    include the compilation fit from Speagle et al. (2014).  
    '''
    # z = 0 
    # fit from SDSS group catalog
    logSFR_sdss = lambda mm: (0.5757 * (mm - 10.5) - 0.13868)

    # Lee et al. (2015)
    logSFR_lee = lambda mm: 1.53 - np.log10(1 + (10**mm/10**(10.10))**-1.26)
    # Noeske et al. (2007) 0.85 < z< 1.10 (by eye)
    logSFR_noeske = lambda mm: (1.580 - 1.064)/(11.229 - 10.029)*(mm - 10.0285) + 1.0637
    # Moustakas et al. (2013) 0.8 < z < 1. (by eye)  
    logSFR_primus = lambda mm: (1.3320 - 1.296)/(10.49 - 9.555) * (mm-9.555) + 1.297

    # Speagle's SFMS bestfit
    logSFR_speagle = lambda mm, tt: (0.84 - 0.026 * tt) * mm - (6.51 - 0.11 * tt) 
    
    # priors 
    logSFR_prior = lambda mm, zz, p1, p2: logSFR_sdss(mm) + (zz - 0.05) * (p1 * (mm - 10.5) + p2)  
    marr = np.linspace(9., 12., 100)

    fig = plt.figure(1)
    sub = fig.add_subplot(121) # z = 0.05 subplot 
    sub.plot(marr, logSFR_sdss(marr), label='SDSS group catalog')  
    sub.plot(marr, logSFR_speagle(marr, 13.5), label='Speagle $z \sim 0.05$')  
    sub.legend(loc='lower right') 
    sub.set_xlim([9.5, 11.5])
    sub.set_ylim([-2, 3])
    
    sub = fig.add_subplot(122) # z = 1 subplot 
    sub.plot(marr, logSFR_lee(marr), label='Lee et al.(2015)')
    sub.plot(marr, logSFR_noeske(marr), label='Noeske et al.(2007)')
    sub.plot(marr, logSFR_primus(marr), label='Moustakas et al.(2013)')
    sub.plot(marr, logSFR_speagle(marr, 5.7), label='Speagle $z \sim 1$')  

    logSFRmin, logSFRmax = [], [] 
    parr = [[-0.5, 1], [-0.5, 2.], [0.5, 1.], [0.5, 2.]]
    for mm in marr: 
        logSFRmin.append(
                np.min([logSFR_prior(mm, 1., pp[0], pp[1]) for pp in parr]))
        logSFRmax.append(
                np.max([logSFR_prior(mm, 1., pp[0], pp[1]) for pp in parr]))

    sub.fill_between(marr, logSFRmin, logSFRmax, label='Prior', alpha=0.5)
    sub.legend(loc='lower right') 
    sub.set_xlim([9.5, 11.5])
    sub.set_ylim([-2, 3])
    fig.savefig(UT.fig_dir()+'test.sfms.comparison.png')
    plt.close() 
    return None
Example #29
0
def Plk_halo_mneut_ratio(nzbin=4, zspace=False):
    ''' Plot the ratio of P_l^mneut(k)/P_l^0.0eV 
    for different neutrino masses 
    '''
    mneuts = [0.0, 0.06, 0.10, 0.15, 0.6]  # eV
    p0ks_mneut, p2ks_mneut, p4ks_mneut = [], [], []
    for mneut in mneuts:
        p0ks, p2ks, p4ks = [], [], []
        for ireal in range(1, 101):
            # read all 100 realizations
            plk_i = Obvs.Plk_halo(mneut, ireal, nzbin, zspace=zspace)
            if ireal == 1: k = plk_i['k']
            p0ks.append(plk_i['p0k'])
            p2ks.append(plk_i['p2k'])
            p4ks.append(plk_i['p4k'])
        # plot the average
        p0ks_mneut.append(np.average(np.array(p0ks), axis=0))
        p2ks_mneut.append(np.average(np.array(p2ks), axis=0))
        p4ks_mneut.append(np.average(np.array(p4ks), axis=0))

    plks_mneut = [p0ks_mneut, p2ks_mneut, p4ks_mneut]
    fig = plt.figure(figsize=(15, 5))
    for i, ell in enumerate([0, 2, 4]):
        sub = fig.add_subplot(1, 3, i + 1)
        for ii in range(len(mneuts)):
            sub.plot(k,
                     plks_mneut[i][ii] / plks_mneut[i][0],
                     lw=2,
                     label=r'$\sum m_\nu = $ ' + str(mneuts[ii]) + 'eV')
        if i == 0:
            sub.legend(loc='lower right', prop={'size': 12})
        else:
            sub.set_yticks([])
        sub.set_xscale('log')
        sub.set_xlim([0.01, 0.5])
        sub.set_xlabel('k', fontsize=20)

        sub.set_ylim([0.9, 1.15])
        sub.set_ylabel('$P_{' + str(ell) + '}(k)/P_{' + str(ell) +
                       '}^{0.0\mathrm{eV}}(k)$',
                       fontsize=20)

    if zspace: str_space = 'z'
    else: str_space = 'r'
    fig.savefig(''.join([
        UT.fig_dir(), 'tests/plk_halo.mneuts_ratio.nzbin',
        str(nzbin), '.', str_space, 'space.png'
    ]),
                bbox_inches='tight')
    return None
Example #30
0
def p_Xw_i_outlier(mock, ell=0, rebin=None, krange=None, method='choletsky'):
    ''' Examine the pdf of X_w^i components that deviate significantly from  
    N(0,1) 
    '''
    Pk = NG.dataX(mock, ell=ell, rebin=rebin, krange=krange)
    X, _ = NG.meansub(Pk)
    X_w, W = NG.whiten(X, method=method) # whitened data
    
    # calculate the chi-squared values of each p(X_w^i)  
    x = np.arange(-5., 5.1, 0.1)
    chi2 = np.zeros(X_w.shape[1])
    for i_bin in range(X_w.shape[1]): 
        kern = gkde(X_w[:,i_bin]) # gaussian KDE kernel using "rule of thumb" scott's rule. 
        chi2[i_bin] = np.sum((UT.gauss(x, 1., 0.) - kern.evaluate(x))**2)/np.float(len(x))
    
    # plot the most discrepant components. 
    prettyplot()
    fig = plt.figure()
    sub = fig.add_subplot(111)
    i_sort = np.argsort(chi2)
    print 'outlier bins = ', i_sort[-5:]
    for i_bin in i_sort[-10:]: 
        kern = gkde(X_w[:,i_bin]) # gaussian KDE kernel using "rule of thumb" scott's rule. 
        sub.plot(x, kern.evaluate(x))
    sub.plot(x, UT.gauss(x, 1., 0.), c='k', lw=3, label='$\mathcal{N}(0,1)$')
    sub.set_xlim([-2.5, 2.5])
    sub.set_xlabel('$\mathtt{X^{i}_{W}}$', fontsize=25) 
    sub.set_ylim([0., 0.6])
    sub.set_ylabel('$\mathtt{P(X^{i}_{W})}$', fontsize=25) 
    sub.legend(loc='upper right') 
    
    if rebin is None: 
        f = ''.join([UT.fig_dir(), 'tests/test.p_Xw_i_outlier.', method, '.', mock, '.ell', str(ell), '.png'])
    else: 
        f = ''.join([UT.fig_dir(), 'tests/test.p_Xw_i_outlier.', method, '.', mock, '.ell', str(ell), '.rebin', str(rebin), '.png'])
    fig.savefig(f, bbox_inches='tight') 
    return None
Example #31
0
def dataX(mock, ell=0, rebin=None, krange=None): 
    ''' ***TESTED***
    Test the data X calculation 
    '''
    Pk = NG.dataX(mock, ell=ell, rebin=rebin, krange=krange)
    X, _ = NG.meansub(Pk)
    
    prettyplot()
    fig = plt.figure()
    sub = fig.add_subplot(111)

    for i in range(X.shape[0]): 
        sub.plot(range(X.shape[1]), X[i,:])
    
    sub.set_xlim([0, X.shape[1]]) 
    sub.set_xlabel('$\mathtt{k}$ bins', fontsize=25)
    sub.set_ylim([-1e5, 1e5])
    sub.set_ylabel('$\mathtt{P^i_'+str(ell)+'(k) - \overline{P_'+str(ell)+'(k)}}$', fontsize=25)
    if rebin is not None: 
        f = ''.join([UT.fig_dir(), 'tests/test.dataX.', mock, '.ell', str(ell), '.rebin', str(rebin), '.png'])
    else: 
        f = ''.join([UT.fig_dir(), 'tests/test.dataX.', mock, '.ell', str(ell), '.png'])
    fig.savefig(f, bbox_inches='tight') 
    return None 
Example #32
0
def plot_thetas(theta,
                w,
                t,
                truths=None,
                plot_range=None,
                theta_filename=None,
                output_dir=None):
    ''' Corner plots of input theta values
    '''
    if output_dir is None:
        fig_dir = util.fig_dir()
    else:
        fig_dir = output_dir

    for ww in ['weighted', 'unweighted']:
        if ww == 'weighted':
            weights = w.flatten()
        elif ww == 'unweighted':
            weights = None

        # weighted theta
        fig = corner.corner(theta,
                            weights=weights,
                            truths=truths,
                            truth_color='#ee6a50',
                            labels=[
                                r'$\mathtt{\log\;M_{0}}$',
                                r'$\mathtt{\log\;\sigma_{\logM}}$',
                                r'$\mathtt{\log\;M_{min}}$',
                                r'$\mathtt{\alpha}$', r'$\mathtt{\log\;M_{1}}$'
                            ],
                            label_kwargs={'fontsize': 25},
                            range=plot_range,
                            quantiles=[0.16, 0.5, 0.84],
                            show_titles=True,
                            title_args={"fontsize": 12},
                            plot_datapoints=True,
                            fill_contours=True,
                            levels=[0.68, 0.95],
                            color='b',
                            bins=20,
                            smooth=1.0)

        fig_file = ''.join([theta_filename.split('.dat')[0], '.', ww, '.png'])
        plt.savefig(fig_file)
        plt.close()

    return None
Example #33
0
def IntegTest(): 
    ''' Test the convergence of integration methods by trying both euler and 
    RK4 integration for different time steps and then comparing the resulting SMF 

    Conclusions
    -----------
    * The integration is more or less converged after tstep <= 0.5 dt_min especially for 
        RK4. So for calculations use tstep = 0.5 dt_min. 
    '''
    prettyplot() 
    pretty_colors = prettycolors() 
    fig = plt.figure(figsize=(15,6))
    
    for ii, integ in enumerate(['euler', 'rk4']): 
        sub = fig.add_subplot(1,2,ii+1)
        for i_t, tstep in enumerate([0.001, 0.01, 0.1]): 
            evol_dict = {
                    'sfh': {'name': 'random_step', 'dt_min': 0.1, 'dt_max': 0.5, 'sigma': 0.3,
                        'assembly_bias': 'acc_hist', 'halo_prop': 'frac', 'sigma_bias': 0.3}, 
                    'mass': {'type': integ, 'f_retain': 0.6, 't_step': tstep} 
                    } 
            EGP = CMS.EvolvedGalPop(cenque='default', evol_dict=evol_dict)
            if not os.path.isfile(EGP.File()): 
                EGP.Write() 
            EGP.Read()
            MSonly = np.where(EGP.t_quench == 999.)   # remove the quenching galaxies

            smf = Obvs.getSMF(EGP.mass[MSonly], weights=EGP.weight_down[MSonly])
            sub.plot(smf[0], smf[1], lw=3, 
                    c=pretty_colors[i_t], ls='--', 
                    label=''.join([integ, ';', r'$\mathtt{t_{step} =', str(tstep),'}$']))

        # analytic SMF for comparison 
        theory_smf = Obvs.getSMF(EGP.M_sham[MSonly], weights=EGP.weight_down[MSonly])
        sub.plot(theory_smf[0], theory_smf[1], lw=2, c='k', label='Theory')

        sub.set_ylim([10**-5, 10**-1])
        sub.set_xlim([8., 12.0])
        sub.set_yscale('log')
        sub.set_xlabel(r'Mass $\mathtt{M_*}$', fontsize=25) 
        if ii == 0: 
            sub.set_ylabel(r'Stellar Mass Function $\mathtt{\Phi}$', fontsize=25) 
        sub.legend(loc='upper right', frameon=False)
        
    fig_file = ''.join([UT.fig_dir(), 'IntegTest.png']) 
    fig.savefig(fig_file, bbox_inches='tight') 
    plt.close() 
    return None
def Galaxies():
    ''' Test that the Galaxies function correctly populats halo catalogs with an HOD 
    '''
    # HOD parameters (Zheng+2007 Mr < -21)
    p_hod = {
        'logM0': 11.92,
        'sigma_logM': 0.39,
        'logMmin': 12.79,
        'alpha': 1.15,
        'logM1': 13.94
    }

    # import Neutrino halo with 0.0eV realization 1 at z=0
    halos = Dat.NeutHalos(0.0, 1, 4)
    halos['RSDPosition'] = FM.RSD(halos)
    # measure the P_l(k) of the galaxy catalog
    plk_halo = FM.Observables(halos, observable='plk', rsd=True, Nmesh=256)

    # now run HOD on the halo catalog
    gals = FM.Galaxies(halos, p_hod, seed=10)
    gals['RSDPosition'] = FM.RSD(gals)

    # measure the P_l(k) of the galaxy catalog
    plk = FM.Observables(gals, observable='plk', rsd=True, Nmesh=256)

    fig = plt.figure()
    sub = fig.add_subplot(121)
    for ell in [0, 2, 4]:
        sub.plot(plk['k'], plk['k'] * plk['p' + str(ell) + 'k'])
        sub.plot(plk_halo['k'],
                 plk_halo['k'] * plk_halo['p' + str(ell) + 'k'],
                 ls='--')
    sub.set_xlabel('$k$', fontsize=20)
    sub.set_xlim([0.01, 0.5])
    sub.set_xscale('log')
    sub.set_ylabel('$k P_\ell(k)$', fontsize=20)
    sub = fig.add_subplot(122)
    sub.plot(plk['k'], plk['p0k'])
    sub.plot(plk_halo['k'], plk_halo['p0k'], ls='--')
    sub.set_xlabel('$k$', fontsize=20)
    sub.set_xlim([0.01, 0.5])
    sub.set_xscale('log')
    sub.set_ylabel('$P_\ell(k)$', fontsize=20)
    sub.set_yscale('log')
    fig.savefig(''.join([UT.fig_dir(), 'tests/hodgalaxy_pk.png']),
                bbox_inches='tight')
    return None
Example #35
0
def LHD():
    ''' *** TESTED *** 
    Testing the different methods of LHD 
    '''
    dim = 5
    samples = 17
    m_list = ['maximin', 'centermaximin', 'mdu', 'nohl']

    for meth in m_list:
        lhcube = lhd.LHD(dim, samples=samples, method=meth, niter=1000)

        fig = plt.figure(figsize=(9, 9))
        for i in range(lhcube.shape[1]):
            for j in range(lhcube.shape[1]):
                if i < j:
                    sub = fig.add_subplot(lhcube.shape[1], lhcube.shape[1],
                                          lhcube.shape[1] * j + i + 1)
                    sub.scatter(lhcube[:, i], lhcube[:, j])
                    sub.set_xlim([0., 1.])
                    if j == lhcube.shape[1] - 1:
                        sub.set_xticks([0., 0.5, 1.])
                        sub.set_xlabel(r'$\theta_' + str(i) + '$', fontsize=20)
                    else:
                        sub.set_xticks([])
                    sub.set_ylim([0., 1.])
                    if i == 0:
                        sub.set_yticks([0., 0.5, 1.])
                        sub.set_ylabel(r'$\theta_' + str(j) + '$', fontsize=20)
                    else:
                        sub.set_yticks([])
                elif i == j:
                    sub = fig.add_subplot(lhcube.shape[1], lhcube.shape[1],
                                          lhcube.shape[1] * j + i + 1)
                    sub.hist(lhcube[:, i], range=[0., 1], normed=True)
                    sub.set_xlim([0., 1.])
                    if i != 0:
                        sub.set_yticks([])
                    if i != lhcube.shape[1] - 1:
                        sub.set_xticks([])
        f = ''.join([
            UT.fig_dir(), 'tests/test.LHD.', meth, '.',
            str(dim), 'dim.',
            str(samples), '.samples.png'
        ])
        fig.savefig(f, bbox_inches='tight')
    return None
Example #36
0
def tdelay_dt_mcmc(run, theta, Niter=20, Nwalkers=10, Ndim=2, sigma_smhm=0.2, nsnap0=15, downsampled='14', flag=None, continue_chain=False): 
    '''
    '''
    if Ndim == 2: 
        tdelay_range = [0., 3.]#np.arange(0., 3., 0.5)
        dt_range = [0.1, 4.]

    # new chain 
    chain_file = ''.join([UT.fig_dir(), run, '.tdelay_dt_mcmc.chain.dat']) 
    if os.path.isfile(chain_file) and continue_chain:   
        print 'Continuing previous MCMC chain!'
        sample = np.loadtxt(chain_file) 
        Niter = Niter - (np.float(len(sample))/np.float(Nwalkers)) # Number of chains left to finish 
        if Niter <= 0: 
            raise ValueError
            print Niter, ' iterations left to finish'
    else: 
        f = open(chain_file, 'w')
        f.close()
        # Initializing Walkers
        pos0 = [np.array([np.random.uniform(tdelay_range[0], tdelay_range[1]), np.random.uniform(dt_range[0], dt_range[1])]) for i in range(Nwalkers)]

    pool = MPIPool()
    if not pool.is_master():
        pool.wait()
        sys.exit(0)

    # Initializing the emcee sampler
    kwargs = {
            'theta': theta, 
            'sigma_smhm': 0.2, 
            'nsnap0': 15, 
            'downsampled': '14', 
            }
    sampler = emcee.EnsembleSampler(Nwalkers, Ndim, sigM, pool=pool, kwargs=kwargs)
    for result in sampler.sample(pos0, iterations=Niter, storechain=False):
        position = result[0]
        #print position
        f = open(chain_file, 'a')
        for k in range(position.shape[0]): 
            output_str = '\t'.join(position[k].astype('str')) + '\n'
            f.write(output_str)
        f.close()
    pool.close()

    return None 
Example #37
0
def plot_thetas(theta, w , t, truths=None, plot_range=None,
        theta_filename=None, output_dir=None):
    ''' Corner plots of input theta values
    '''
    if output_dir is None:
        fig_dir = util.fig_dir()
    else:
        fig_dir = output_dir
    
    for ww in [ 'weighted', 'unweighted']: 
        if ww == 'weighted': 
            weights = w.flatten()
        elif ww == 'unweighted': 
            weights = None 

        # weighted theta
        fig = corner.corner(
                theta,
                weights=weights,
                truths=truths,
                truth_color='#ee6a50',
                labels=[
                    r'$\mathtt{\log\;M_{0}}$',
                    r'$\mathtt{\log\;\sigma_{\logM}}$',
                    r'$\mathtt{\log\;M_{min}}$',
                    r'$\mathtt{\alpha}$',
                    r'$\mathtt{\log\;M_{1}}$'
                    ],
                label_kwargs={'fontsize': 25},
                range=plot_range,
                quantiles=[0.16,0.5,0.84],
                show_titles=True,
                title_args={"fontsize": 12},
                plot_datapoints=True,
                fill_contours=True,
                levels=[0.68, 0.95],
                color='b',
                bins=20,
                smooth=1.0)
        
        fig_file = ''.join([theta_filename.split('.dat')[0], '.', ww, '.png'])
        plt.savefig(fig_file)
        plt.close()

    return None 
Example #38
0
def tduty_tdelay_dt_grid(run, theta, sigma_smhm=0.2, nsnap0=15, downsampled='14', flag=None): 
    ''' plot sigma_M* on a grid of t_delay and dt 
    '''
    delt = 0.5
    tdelays = np.arange(0., 3., delt)
    dts = np.arange(0., 4.5, delt)
    dts[0] += 0.1 # dt = 0 not allowed
    tdutys = np.array([0.5, 1., 2., 3., 5., 10.])

    subcat_dat = abcee.Data(nsnap0=nsnap0, sigma_smhm=sigma_smhm) # 'data'
    sumdata = abcee.SumData(['smf'], nsnap0=nsnap0, sigma_smhm=sigma_smhm)  

    smhmr = Obvs.Smhmr()
    grid = np.zeros((5, len(tdutys)*len(tdelays)*len(dts)))
    ii = 0  
    for i_duty, tduty in enumerate(tdutys):
        for i_t, tdelay in enumerate(tdelays): 
            for i_d, dt in enumerate(dts): 
                theta_i = np.concatenate([theta, np.array([tduty, tdelay, dt])])
                #try: 
                subcat_sim = abcee.model(run, theta_i, 
                        nsnap0=nsnap0, sigma_smhm=sigma_smhm, downsampled=downsampled) 
                sumsim = abcee.SumSim(['smf'], subcat_sim)
                
                isSF = np.where(subcat_sim['gclass'] == 'sf') # only SF galaxies 
                # calculate sigma_M* at M_h = 12
                try: 
                    m_mid, mu_mhalo, sig_mhalo, cnts = smhmr.Calculate(subcat_sim['halo.m'][isSF], subcat_sim['m.star'][isSF], 
                            dmhalo=0.2, weights=subcat_sim['weights'][isSF])
                    grid[3,ii] = sig_mhalo[np.argmin(np.abs(m_mid-12.))]
                    # rho 
                    grid[4, ii] = abcee.L2_logSMF(sumsim, sumdata)
                except ValueError: 
                    grid[3,ii] = -999.
                    grid[4, ii] = 999. 
                print 'tduty = ', tduty, 'tdelay = ', tdelay, ', dt = ', dt, ', sigma = ', grid[3, ii]
                grid[0, ii] = tduty
                grid[1, ii] = tdelay
                grid[2, ii] = dt 
                ii += 1

    # save sig_Mstar values to file  
    file_name = ''.join([UT.fig_dir(), 'tduty_tdelay_dt_grid.', run, '.p'])
    pickle.dump(grid, open(file_name, 'wb'))
    return None 
Example #39
0
def p_Xw_i_MISE(mock, ell=0, rebin=None, krange=None, method='choletsky', b=0.1):
    ''' Examine the pdf of X_w^i components that deviate significantly from  
    N(0,1) based on MISE 
    '''
    Pk = NG.dataX(mock, ell=ell, rebin=rebin, krange=krange)
    X, _ = NG.meansub(Pk)
    X_w, W = NG.whiten(X, method=method) # whitened data
    
    # calculate the chi-squared values of each p(X_w^i)  
    x = np.arange(-5., 5.1, 0.1)
    mise = np.zeros(X_w.shape[1])
    for i_bin in range(X_w.shape[1]): 
        mise[i_bin] = NG.MISE(X_w[:,i_bin], b=b) 

    # plot the most discrepant components. 
    prettyplot()
    fig = plt.figure()
    sub = fig.add_subplot(111)
    i_sort = np.argsort(mise)
    print 'outlier bins = ', i_sort[-5:]
    print 'mise = ', mise[i_sort[-5:]]

    nbin = int(10./b)
    for i_bin in i_sort[-10:]: 
        hb_Xi, Xi_edges = np.histogram(X_w[:,i_bin], bins=nbin, range=[-5., 5.], normed=True) 
        p_X_w_arr = UT.bar_plot(Xi_edges, hb_Xi)
        sub.plot(p_X_w_arr[0], p_X_w_arr[1])

    sub.plot(x, UT.gauss(x, 1., 0.), c='k', lw=3, label='$\mathcal{N}(0,1)$')
    sub.set_xlim([-2.5, 2.5])
    sub.set_xlabel('$\mathtt{X^{i}_{W}}$', fontsize=25) 
    sub.set_ylim([0., 0.6])
    sub.set_ylabel('$\mathtt{P(X^{i}_{W})}$', fontsize=25) 
    sub.legend(loc='upper right') 
    
    str_rebin = ''
    if rebin is not None: 
        str_rebin = '.rebin'+str(rebin)

    f = ''.join([UT.fig_dir(), 'tests/test.p_Xw_i_outlier.', method, '.', mock, '.ell', str(ell), 
        str_rebin, '.b', str(b), '.png'])
    fig.savefig(f, bbox_inches='tight') 
    return None
Example #40
0
def PlotCovariance(obvs, Mr=21, b_normal=0.25, inference='mcmc'):
    ''' Plot the covariance matrix for a specified obvs 
    '''
    # import the covariance matrix 
    covar = Data.data_cov(Mr=Mr, b_normal=b_normal, inference=inference)

    if obvs == 'xi': 
        obvs_cov = covar[1:16 , 1:16]
        r_bin = Data.xi_binedges()
    elif obvs == 'gmf': 
        obvs_cov = covar[17:, 17:]
        binedges = Data.data_gmf_bins()
        r_bin = 0.5 * (binedges[:-1] + binedges[1:]) 
    
    n_bin = int(np.sqrt(obvs_cov.size))
    
    # calculate the reduced covariance for plotting
    red_covar = np.zeros([n_bin, n_bin])
    for ii in range(n_bin): 
        for jj in range(n_bin): 
            red_covar[ii][jj] = obvs_cov[ii][jj]/np.sqrt(obvs_cov[ii][ii] * obvs_cov[jj][jj])
    
    prettyplot()
    fig = plt.figure()
    sub = fig.add_subplot(111)
    cont = sub.pcolormesh(r_bin, r_bin, red_covar, cmap=plt.cm.afmhot_r)
    plt.colorbar(cont)

    sub.set_xlim([r_bin[0], r_bin[-1]])
    sub.set_ylim([r_bin[0], r_bin[-1]])
    sub.set_xscale('log')
    sub.set_yscale('log')

    sub.set_xlabel(r'$\mathtt{r}\;[\mathtt{Mpc/h}$]', fontsize=25)
    sub.set_ylabel(r'$\mathtt{r}\;[\mathtt{Mpc/h}$]', fontsize=25)
    fig_file = ''.join([util.fig_dir(),
        obvs.upper(), 'covariance',
        '.Mr', str(Mr), 
        '.bnorm', str(round(b_normal,2)), 
        '.', inference, '_inf.png'])
    fig.savefig(fig_file, bbox_inches='tight') 
    plt.close()
    return None 
def HODemulator_readHODLHD(HODrange='sinha2017prior_narrow',
                           method='mdu',
                           samples=17):
    ''' ***TESTED Nov 13, 2017*** 
    Test HODemulator.read_HODLHD
    '''
    emu = Emu.HODemulator()
    lhcube = emu.read_HODLHD(HODrange=HODrange, method=method, samples=samples)
    fig = plt.figure(figsize=(9, 9))
    for i in range(lhcube.shape[1]):
        for j in range(lhcube.shape[1]):
            if i < j:
                sub = fig.add_subplot(lhcube.shape[1], lhcube.shape[1],
                                      lhcube.shape[1] * j + i + 1)
                sub.scatter(lhcube[:, i], lhcube[:, j])
                sub.set_xlim([emu.HODrange_min[i], emu.HODrange_max[i]])
                if j == lhcube.shape[1] - 1:
                    sub.set_xlabel(emu.HOD_labels[i], fontsize=20)
                else:
                    sub.set_xticks([])
                sub.set_ylim([emu.HODrange_min[j], emu.HODrange_max[j]])
                if i == 0:
                    sub.set_ylabel(emu.HOD_labels[j], fontsize=20)
                else:
                    sub.set_yticks([])
            elif i == j:
                sub = fig.add_subplot(lhcube.shape[1], lhcube.shape[1],
                                      lhcube.shape[1] * j + i + 1)
                sub.hist(lhcube[:, i],
                         range=[emu.HODrange_min[i], emu.HODrange_max[i]],
                         normed=True)
                sub.set_xlim([emu.HODrange_min[i], emu.HODrange_max[i]])
                if i != 0:
                    sub.set_yticks([])
                if i != lhcube.shape[1] - 1:
                    sub.set_xticks([])
    f = ''.join([
        UT.fig_dir(), 'tests/test.HODemulator.LHD.', method, '.',
        str(samples), '.samples.png'
    ])
    fig.savefig(f, bbox_inches='tight')
    return None
Example #42
0
def plotCMS_SMF(cenque='default', evol_dict=None): 
    ''' Plot the stellar mass function of the integrated SFR population 
    and compare it to the theoretic stellar mass function
    '''
    # calculate SMF 
    cq = CMS.CentralQuenched(cenque=cenque)  # quenched population
    cq._Read_CenQue()
    # import evolved galaxy population 
    MSpop = CMS.EvolvedGalPop(cenque=cenque, evol_dict=evol_dict) 
    MSpop.Read() 

    smf = Obvs.getSMF(
            np.array(list(MSpop.mass)+list(cq.mass)), 
            weights=np.array(list(MSpop.weight_down) + list(cq.weight_down))
            )
    prettyplot() 
    pretty_colors = prettycolors() 

    # analytic SMF 
    theory_smf = Obvs.getSMF(
            np.array(list(MSpop.M_sham)+list(cq.M_sham)),
            weights=np.array(list(MSpop.weight_down) + list(cq.weight_down))
            )
    #Obvs.analyticSMF(0.1, source='li-march')

    fig = plt.figure()
    sub = fig.add_subplot(111)

    sub.plot(theory_smf[0], theory_smf[1], lw=3, c='k', label='Theory')
    sub.plot(smf[0], smf[1], lw=3, c=pretty_colors[3], ls='--', label='Simul.')

    sub.set_ylim([10**-5, 10**-1])
    sub.set_xlim([6., 12.0])
    sub.set_yscale('log')
    sub.set_xlabel(r'Mass $\mathtt{M_*}$', fontsize=25) 
    sub.set_ylabel(r'Stellar Mass Function $\mathtt{\Phi}$', fontsize=25) 
    sub.legend(loc='upper right', frameon=False)
    
    fig_file = ''.join([UT.fig_dir(), 'SMF.CMS', MSpop._Spec_str(), '.png']) 
    fig.savefig(fig_file, bbox_inches='tight') 
    plt.close() 
    return None
Example #43
0
def test_tduty_tdelay_dt_grid_dtaxis(run, tdutyy, tdelayy):
    ''' Check the dependence of sigma_logM* to dt_abias. It's weird that 
    dt_abias has such a significant effect... 
    '''
    file_name = ''.join([UT.fig_dir(), 'tduty_tdelay_dt_grid.', run, '.p'])
    grid = pickle.load(open(file_name, 'rb'))
    
    tduty = grid[0,:]
    tdelay = grid[1,:] 
    dt_abias = grid[2,:]
    sigMstar = grid[3,:]
    l2 = grid[4,:]

    lim = np.where((tdelay == tdelayy) & (tduty == tdutyy))
        
    for ii in range(len(lim[0])): 
        print dt_abias[lim[0][ii]], sigMstar[lim[0][ii]]

    #abcee.qaplotABC(run, 10, sigma_smhm=0.2, theta=np.array([1.35, 0.6, grid[0,i_min], grid[1,i_min], grid[2,i_min]])) 
    return None
Example #44
0
def test_tduty_tdelay_dt_grid_best(run):
    '''
    '''
    file_name = ''.join([UT.fig_dir(), 'tduty_tdelay_dt_grid.', run, '.p'])
    grid = pickle.load(open(file_name, 'rb'))
    
    tduty = grid[0,:]
    tdelay = grid[1,:] 
    dt_abias = grid[2,:]
    sigMstar = grid[3,:]
    l2 = grid[4,:]

    lim = np.where(tdelay > 1.)
    
    i_min = np.argmin(sigMstar[lim])
    i_min = lim[0][i_min]
    print grid[:,i_min]

    abcee.qaplotABC(run, 10, sigma_smhm=0.2, theta=np.array([1.35, 0.6, grid[0,i_min], grid[1,i_min], grid[2,i_min]])) 
    return None
Example #45
0
def plotCMS_SFH(cenque='default', evol_dict=None): 
    ''' Plot the star formation history of star forming main sequence galaxies
    '''
    # import evolved galaxy population 
    egp = CMS.EvolvedGalPop(cenque=cenque, evol_dict=evol_dict) 
    egp.Read() 

    MSonly = np.where(egp.t_quench == 999.)     # only keep main-sequence galaxies
    MSonly_rand = np.random.choice(MSonly[0], size=10)

    z_acc, t_acc = UT.zt_table()
    t_cosmic  = t_acc[1:16]
    dt = 0.1
    
    t_arr = np.arange(t_cosmic.min(), t_cosmic.max()+dt, dt)

    prettyplot() 
    pretty_colors = prettycolors() 
    fig = plt.figure()
    sub = fig.add_subplot(111)
    
    dsfrt = np.zeros((len(t_arr), len(MSonly_rand)))
    for i_t, tt in enumerate(t_arr): 
        dsfr = SFR.dSFR_MS(tt, egp.sfh_dict) 
        for ii, i_gal in enumerate(MSonly_rand): 
            dsfrt[i_t,ii] = dsfr[i_gal]
    
    for ii, i_gal in enumerate(MSonly_rand): 
        tlim = np.where(t_arr > egp.tsnap_genesis[i_gal]) 
        sub.plot(t_arr[tlim], dsfrt[:,ii][tlim], lw=3, c=pretty_colors[ii])

    sub.set_xlim([5.0, 13.5])
    sub.set_xlabel(r'$\mathtt{t_{cosmic}}$', fontsize=25)
    sub.set_ylim([-1., 1.])
    sub.set_ylabel(r'$\mathtt{SFR(t) - <SFR_{MS}(t)>}$', fontsize=25)

    fig_file = ''.join([UT.fig_dir(), 'SFH.CMS', egp._Spec_str(), '.png']) 
    fig.savefig(fig_file, bbox_inches='tight') 
    plt.close()
Example #46
0
def X_gmf_all():
    ''' ***TESTED -- Nov 8, 2017***
    Test to make sure that NG.X_gmf_all returns correct values
    '''
    X, nbins = NG.X_gmf_all(n_arr=True) 
    nmid = 0.5*(nbins[1:] + nbins[:-1])
    assert X.shape[1] == len(nmid)
    assert X.shape[0] == 20000

    fig = plt.figure(figsize=(5,5)) 
    sub = fig.add_subplot(111)
    for i in np.random.choice(range(X.shape[0]), 1000, replace=False):
        sub.plot(nmid, X[i,:], c='k', lw=0.01)
    sub.plot(nmid, np.average(X, axis=0), c='r', lw=2, ls='--')
    # x-axis
    sub.set_xlim([0., 180.]) 
    sub.set_xlabel('$N$', fontsize=20) 
    # y-axis
    sub.set_yscale('log') 
    sub.set_ylabel(r'$\zeta(N)$', fontsize=20) 
    fig.savefig(''.join([UT.fig_dir(), 'tests/X_gmf_all.png']), bbox_inches='tight') 
    plt.close() 
    return None 
Example #47
0
def Plk_LHD(nreal): 
    ''' Test that Data.X_lhd returns sensible P_l(k) 
    for the LHD 
    '''
    karr, Xlhd = Data.X_lhd(0.0, nreal, 4, 1, obvs='plk', karr=True, 
            HODrange='sinha2017prior_narrow', samples=17, method='mdu') 
    lhcube = LHD.HOD_LHD(HODrange='sinha2017prior_narrow', samples=17, method='mdu') 
    
    fig = plt.figure(figsize=(12,4))
    sub = fig.add_subplot(131)
    sub1 = fig.add_subplot(132) 
    sub2 = fig.add_subplot(133) 
    for i in range(10):#Xlhd.shape[0]): 
        sub.plot(karr, Xlhd[i,:], c='C'+str(i % 10)) 
        sub1.scatter([lhcube[i,0]], [lhcube[i,1]], c='C'+str(i % 10))
        sub2.scatter([lhcube[i,3]], [lhcube[i,4]], c='C'+str(i % 10))

    sub.set_xlim([1e-2, 0.5]) 
    sub.set_xlabel('$k$', fontsize=20) 
    sub.set_xscale('log') 
    sub.set_ylim([2e3, 2e5]) 
    sub.set_ylabel('$P(k)$', fontsize=20) 
    sub.set_yscale('log') 
    
    sub1.set_xlim([11., 12.2]) 
    sub1.set_xlabel('log $M_\mathrm{min}$', fontsize=20) 
    sub1.set_ylim([0.001, 1.]) 
    sub1.set_ylabel('$\sigma_{\mathrm{log}\,M}$', fontsize=20)
    
    sub2.set_xlim([12., 14]) 
    sub2.set_xlabel('log $M_1$', fontsize=20) 
    sub2.set_ylim([0.5, 1.5]) 
    sub2.set_ylabel(r'$\alpha$', fontsize=20)

    f = ''.join([UT.fig_dir(), 'tests/test_data.Plk_lhd.png']) 
    fig.savefig(f, bbox_inches='tight') 
    return None
Example #48
0
def plot_data(Mr=21, output_dir=None):
    '''
    plot summary statistics of the data
    '''
    if output_dir is None:
        fig_dir = util.fig_dir()
    else:
        fig_dir = output_dir

    cov = Data.data_cov()
    xi_err = np.sqrt(np.diag(cov)[1:16])
    gmf_err = np.sqrt(np.diag(cov)[16:])

    fig, axes = plt.subplots(1, 2, figsize=(10, 5))
    fig.subplots_adjust(wspace=0.4, hspace=0.4)
    ax = axes[0]
    x = Data.xi_binedges()
    y = Data.data_xi()
    ax.errorbar(0.5 * (x[:-1] + x[1:]), y, yerr=xi_err, fmt=".k", capsize=0)
    ax.set_yscale('log')
    ax.set_xscale('log')
    ax.set_xlim(0.2, 22)
    ax.set_xlabel(r'$r[\mathrm{Mpc}/h]$')
    ax.set_ylabel(r'$\xi(r)$')

    ax = axes[1]
    x = Data.gmf_bins()
    y = Data.data_gmf()
    ax.errorbar(0.5 * (x[:-1] + x[1:]), y, yerr=gmf_err, fmt=".k", capsize=0)
    ax.set_xlim(1, 20)
    ax.set_yscale('log')
    ax.set_xlabel(r'Group Richness $N$')
    ax.set_ylabel(r'$\zeta(N)$')

    fig_file = ''.join([fig_dir, "data", '_Mr', str(Mr), '.pdf'])
    plt.savefig(fig_file)
    plt.close()
Example #49
0
def plot_data(Mr = 21 , output_dir = None):
    '''
    plot summary statistics of the data
    '''
    if output_dir is None:
        fig_dir = util.fig_dir()
    else:
        fig_dir = output_dir

    cov = Data.data_cov()
    xi_err = np.sqrt(np.diag(cov)[1:16])
    gmf_err = np.sqrt(np.diag(cov)[16:]) 
   
    fig , axes = plt.subplots(1, 2, figsize=(10, 5))
    fig.subplots_adjust(wspace=0.4, hspace=0.4)
    ax = axes[0]
    x = Data.xi_binedges()
    y = Data.data_xi() 
    ax.errorbar(0.5*(x[:-1]+x[1:]), y, yerr=xi_err, fmt=".k", capsize=0)
    ax.set_yscale('log')
    ax.set_xscale('log')
    ax.set_xlim(0.2, 22)
    ax.set_xlabel(r'$r[\mathrm{Mpc}/h]$')
    ax.set_ylabel(r'$\xi(r)$')
    
    ax = axes[1]
    x = Data.gmf_bins()
    y = Data.data_gmf() 
    ax.errorbar(0.5*(x[:-1]+x[1:]), y, yerr=gmf_err, fmt=".k", capsize=0)
    ax.set_xlim(1, 20)
    ax.set_yscale('log')
    ax.set_xlabel(r'Group Richness $N$')
    ax.set_ylabel(r'$\zeta(N)$')

    fig_file = ''.join([fig_dir, "data", '_Mr', str(Mr),'.pdf'])
    plt.savefig(fig_file)
    plt.close()
def HODemulator_readNeutObvs(obvs='p0k',
                             mneut=0.0,
                             nreal=1,
                             nzbin=4,
                             seed_hod=1,
                             Nmesh=360,
                             rsd=True,
                             HODrange='sinha2017prior_narrow',
                             method='mdu',
                             samples=17):
    ''' ***TESTED Nov 13, 2017***
    Test HODemulator.read_HODLHD
    '''
    emu = Emu.HODemulator()
    lhcube = emu.read_HODLHD(HODrange=HODrange, method=method, samples=samples)
    plks = emu.read_NeutObvs(obvs,
                             mneut,
                             nreal,
                             nzbin,
                             seed_hod,
                             Nmesh=Nmesh,
                             rsd=rsd)
    fig = plt.figure()
    sub = fig.add_subplot(111)
    for i in range(plks.shape[0]):
        sub.plot(emu.k, plks[i, :])
    # x-axis
    sub.set_xscale('log')
    sub.set_xlim([0.01, 0.5])
    sub.set_xlabel('k', fontsize=25)
    # y-axis
    sub.set_yscale('log')
    sub.set_ylabel('$P(k)$', fontsize=25)
    f = ''.join([UT.fig_dir(), 'tests/test.HODemulator.', obvs, '.png'])
    fig.savefig(f, bbox_inches='tight')
    return None
Example #51
0
def Subvolume_Analytic(N_sub, ratio=False): 
    ''' Test the 2PCF estimates from MultiDark subvolume versus the 
    analytic 2PCF for the entire MultiDark volume

    Parameters
    ----------
    N_sub : (int)
        Number of subvolumes to sample

    '''
    prettyplot()
    pretty_colors = prettycolors()

    pickle_file = ''.join([
        '/export/bbq2/hahn/ccppabc/dump/', 
        'xi_subvolume_test', 
        '.Nsub', str(N_sub), 
        '.p'])
    
    fig = plt.figure(1)
    sub = fig.add_subplot(111)

    xi_bin = xi_binedges() 
    
    if os.path.isfile(pickle_file):
        data_dump = pickle.load(open(pickle_file, 'rb'))
        full_xi = data_dump['full_xi']
    else: 
        # Entire MultiDark Volume (Analytic xi) 
        model = PrebuiltHodModelFactory('zheng07', threshold=-21)
        halocat = CachedHaloCatalog(simname = 'multidark', redshift = 0, halo_finder = 'rockstar')
        
        model.populate_mock(halocat)
        pos = three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z')
        
        # while the estimator claims to be Landy-Szalay, I highly suspect it
        # actually uses Landy-Szalay since DR pairs cannot be calculated from 
        # analytic randoms
        full_xi = tpcf(pos, xi_bin, period=model.mock.Lbox, max_sample_size=int(2e5), estimator='Landy-Szalay', num_threads=1)
        data_dump = {} 
        data_dump['full_xi'] = full_xi

    if not ratio:  
        sub.plot(0.5*(xi_bin[:-1]+xi_bin[1:]), full_xi, lw=2, ls='-', c='k', label=r'Analytic $\xi$ Entire Volume') 
    
    if not os.path.isfile(pickle_file):
        # MultiDark SubVolume (precomputed RR pairs) 
        sub_model = PrebuiltHodModelFactory('zheng07', threshold=-21)
        sub_model.new_haloprop_func_dict = {'sim_subvol': util.mk_id_column}
        sub_halocat = CachedHaloCatalog(simname = 'multidark', redshift = 0, halo_finder = 'rockstar')
        RR = data_RR()
        randoms = data_random()
        NR = len(randoms)
    
    for method in ['Landy-Szalay', 'Natural']:
        
        if method == 'Landy-Szalay': 
            iii = 3
        elif method == 'Natural': 
            iii = 5
        
        if not os.path.isfile(pickle_file): 
            sub_xis_list = [] 
            sub_xis = np.zeros(len(full_xi)) 

            for ii in range(1,N_sub+1): 
                # randomly sample one of the subvolumes
                rint = ii #np.random.randint(1, 125)
                simsubvol = lambda x: util.mask_func(x, rint)
                sub_model.populate_mock(sub_halocat, masking_function=simsubvol, enforce_PBC=False)
                   
                pos = three_dim_pos_bundle(sub_model.mock.galaxy_table, 'x', 'y', 'z')

                xi, yi , zi = util.random_shifter(rint)
                temp_randoms = randoms.copy()
                temp_randoms[:,0] += xi
                temp_randoms[:,1] += yi
                temp_randoms[:,2] += zi
                
                rmax = xi_bin.max()
                approx_cell1_size = [rmax , rmax , rmax]
                approx_cellran_size = [rmax , rmax , rmax]

                sub_xi = tpcf(
                        pos, xi_bin, pos, 
                        randoms=temp_randoms, 
                        period = None, 
                        max_sample_size=int(1e5), 
                        estimator=method, 
                        approx_cell1_size = approx_cell1_size, 
                        approx_cellran_size = approx_cellran_size,
                        RR_precomputed=RR,
                        NR_precomputed=NR)
                label = None 
                if ii == N_sub - 1: 
                    label = 'Subvolumes'
                
                #if not ratio: 
                #    sub.plot(0.5*(xi_bin[:-1]+xi_bin[1:]), sub_xi, lw=0.5, ls='--', c=pretty_colors[iii])
                sub_xis += sub_xi
                sub_xis_list.append(sub_xi)

            sub_xi_avg = sub_xis/np.float(N_sub)

            data_dump[method] = {} 
            data_dump[method]['sub_xi_avg'] = sub_xi_avg
            data_dump[method]['sub_xis_list'] = sub_xis_list 
        else: 
            sub_xis_list = data_dump[method]['sub_xis_list']
            sub_xi_avg = data_dump[method]['sub_xi_avg'] 

        if not ratio: 
            sub.plot(0.5*(xi_bin[:-1]+xi_bin[1:]), sub_xi_avg, 
                    lw=2, ls='--', c=pretty_colors[iii], label='Subvolume '+method)
        else: 
            sub.plot(0.5*(xi_bin[:-1]+xi_bin[1:]), sub_xi_avg/full_xi, 
                    lw=2, ls='--', c=pretty_colors[iii], label='Subvolume '+method)
    
    if not os.path.isfile(pickle_file): 
        pickle.dump(data_dump, open(pickle_file, 'wb')) 

    sub.set_xlim([0.1, 50.])
    sub.set_xlabel('r', fontsize=30)
    sub.set_xscale('log')
    
    if not ratio: 
        sub.set_ylabel(r"$\xi \mathtt{(r)}$", fontsize=25)
        sub.set_yscale('log')
    else: 
        sub.set_ylabel(r"$\overline{\xi^\mathtt{sub}}/\xi^\mathtt{all}$", fontsize=25)

    sub.legend(loc='lower left')
    
    if ratio: 
        fig_file = ''.join([util.fig_dir(), 'test_xi_subvolume_analytic.Nsub', str(N_sub), '.ratio.png'])
    else:
        fig_file = ''.join([util.fig_dir(), 'test_xi_subvolume_analytic.Nsub', str(N_sub), '.png'])
    fig.savefig(fig_file, bbox_inches='tight', dpi=100)
    plt.close()
    return None 
Example #52
0
def plot_occupations_total(obs = "gmf", clotter = True):

    npts = 2e3
    mass = np.logspace(10, 14, npts)
    pretty_color = prettycolors()
    if clotter == False:

        print "running compute_occupation_prediction first"

    else:


       ylabel = r'$\langle N_{\rm g}(M_{h})\rangle$'
       xlabel = r'$M_{h}\; [h^{-1} \; M_{\odot}]$'


       totdec_18 = np.loadtxt("results/ntot_"+obs+"_dec_18.0.dat")
       totdec_19 = np.loadtxt("results/ntot_"+obs+"_dec_19.0.dat")
       totdec_20 = np.loadtxt("results/ntot_"+obs+"_dec_20.0.dat") 
         
       tothod_18 = np.loadtxt("results/ntot_"+obs+"_hod_18.0.dat")
       tothod_19 = np.loadtxt("results/ntot_"+obs+"_hod_19.0.dat")
       tothod_20 = np.loadtxt("results/ntot_"+obs+"_hod_20.0.dat") 
       
       fig = plt.figure(1, figsize=(15,5))
       gs = gridspec.GridSpec(1, 3)# height_ratios=[2.5, 1], width_ratios=[1,1])  
       pretty_colors=prettycolors()
      
       ##### MR18 DECvsHOD ######

       ax = plt.subplot(gs[0])

       a_dec, b_dec, c_dec = np.percentile(totdec_18, [16, 50, 84], axis=0) 
       a_hod, b_hod, c_hod = np.percentile(tothod_18, [16, 50, 84], axis=0) 

       ax.plot(mass, b_dec, color=pretty_colors[1], linewidth=2.) 
       ax.plot(mass, b_hod, color=pretty_colors[7], linewidth=2.)
       ax.fill_between(mass, a_dec, c_dec, color=pretty_colors[1], alpha=0.3, edgecolor=pretty_colors[1] ) 
       ax.fill_between(mass, a_hod, c_hod, color=pretty_colors[7], alpha=0.3, edgecolor=pretty_colors[7]) 
 
       blue_line = mlines.Line2D([], [], ls = '-', c = pretty_color[1], linewidth=2, label = '$Heaviside$ $AB$')
       red_line = mlines.Line2D([], [], ls = '-', c = pretty_color[7], linewidth=2, label = '$Standard$ $HOD$')
       import matplotlib as mpl
       #mpl.rc('font',family='serif')
       plt.legend(handles=[blue_line, red_line], frameon=False, loc= 2 , fontsize=15) 
       ax.set_ylabel(ylabel, fontsize=27)
       ax.set_xlabel(xlabel, fontsize=27)
       ax.set_yscale('log') 
       ax.set_xscale('log')
       ax.set_xlim([10**10, 8. * 10**13]) 
       ax.set_ylim([0.1,100])
       ax.text(4 * 10**12. , 50, r'$M_{r}<-18$', fontsize=18) 

       ##### Mr19 DECvsHOD ######

       ax = plt.subplot(gs[1])

       a_dec, b_dec, c_dec = np.percentile(totdec_19, [16, 50, 84], axis=0) 
       a_hod, b_hod, c_hod = np.percentile(tothod_19, [16, 50, 84], axis=0) 

       ax.plot(mass, b_dec, color=pretty_colors[1], linewidth=2.) 
       ax.plot(mass, b_hod, color=pretty_colors[7], linewidth=2.)
       ax.fill_between(mass, a_dec, c_dec, color=pretty_colors[1], alpha=0.3, edgecolor=pretty_colors[1] ) 
       ax.fill_between(mass, a_hod, c_hod, color=pretty_colors[7], alpha=0.3, edgecolor=pretty_colors[7]) 
 
       blue_line = mlines.Line2D([], [], ls = '-', c = pretty_color[1], linewidth=2, label = '$Heaviside$ $AB$ ')
       red_line = mlines.Line2D([], [], ls = '-', c = pretty_color[7], linewidth=2, label = '$Standard$ $HOD$')
       import matplotlib as mpl
       #mpl.rc('font',family='serif')
       #plt.legend(handles=[blue_line, red_line], frameon=False, loc= 2 , fontsize=15) 
       ax.set_xlabel(xlabel, fontsize=27)
       ax.set_yscale('log') 
       ax.set_xscale('log')
       ax.set_xlim([10**10, 8. * 10**13]) 
       ax.set_ylim([0.1,100])
       ax.text(4 * 10**12. , 50, r'$M_{r}<-19$', fontsize=18) 
       ax.set_yticklabels([])

       ##### Mr20 DECvsHOD #######      
 
       ax = plt.subplot(gs[2])
       a_dec, b_dec, c_dec = np.percentile(totdec_20, [16, 50, 84], axis=0) 
       a_hod, b_hod, c_hod = np.percentile(tothod_20, [16, 50, 84], axis=0) 

       ax.plot(mass, b_dec, color=pretty_colors[1], linewidth=2.) 
       ax.plot(mass, b_hod, color=pretty_colors[7], linewidth=2.)
       ax.fill_between(mass, a_dec, c_dec, color=pretty_colors[1], alpha=0.3, edgecolor=pretty_colors[1] ) 
       ax.fill_between(mass, a_hod, c_hod, color=pretty_colors[7], alpha=0.3, edgecolor=pretty_colors[7]) 
 
       blue_line = mlines.Line2D([], [], ls = '-', c = pretty_color[1], linewidth=2, label = '$Heaviside$ $AB$')
       red_line = mlines.Line2D([], [], ls = '-', c = pretty_color[7], linewidth=2, label = '$Standard$ $HOD$')
       import matplotlib as mpl
       #mpl.rc('font',family='serif')
       #plt.legend(handles=[blue_line, red_line], frameon=False, loc= 2 , fontsize=15) 
       ax.set_xlabel(xlabel, fontsize=27)
       ax.set_yscale('log') 
       ax.set_xscale('log')
       ax.set_xlim([10**10, 8. * 10**13]) 
       ax.set_ylim([0.1,100])
       ax.text(4 * 10**12. , 50, r'$M_{r}<-20$', fontsize=18) 
       ax.set_yticklabels([])
       
       fig.subplots_adjust(wspace=0.0, hspace=0.0)
       fig_name = ''.join([ut.fig_dir(), 
        'paper', 
        '.abvshod.',
        obs,
        '.pdf'])
       fig.savefig(fig_name, bbox_inches='tight')
       plt.close()
       
    return None 
Example #53
0
def logSFR_initiate(SHsnaps, indices, theta_sfh=None, theta_sfms=None, testing=False):
    ''' initiate log SFR function for Evolver.Evolve() method
    '''
    nsnap0 = SHsnaps['metadata']['nsnap0']
    mu_sfr0 = SFR_sfms(SHsnaps['m.star0'][indices], UT.z_nsnap(SHsnaps['nsnap_start'][indices]), theta_sfms)

    if theta_sfh['name'] == 'constant_offset':
        # constant d_logSFR 
        F_sfr = _logSFR_dSFR 
        sfr_kwargs = {'dSFR': SHsnaps['sfr0'][indices] - mu_sfr0,  # offset
                'theta_sfms': theta_sfms}

    elif theta_sfh['name'] == 'corr_constant_offset': 
        # constant d_logSFR (assigned based on halo accretion rate) 
        #dSFR0 = SHsnaps['sfr0'][indices] - mu_sfr

        # now rank order it based on halo accretion rate
        dMhalo = SHsnaps['halo.m'][indices] - SHsnaps['halo.m0'][indices]
        
        m_kind = SHsnaps[theta_sfh['m.kind']+'0'][indices] # how do we bin the SFRs?
        dm_kind = theta_sfh['dm.kind'] # bins to rank order
    
        # scatter from noise -- subtract intrinsic assembly bias scatter from sig_SFMS 
        sig_noise = np.sqrt(0.3**2 - theta_sfh['sig_abias']**2)

        # slow and inefficient 

        dsfr = np.repeat(-999., len(dMhalo))
        for nn in np.arange(1, 21): 
            started = np.where(SHsnaps['nsnap_start'][indices] == nn)[0]

            m_kind_bin = np.arange(m_kind[started].min(), m_kind[started].max()+dm_kind, dm_kind)
            ibin = np.digitize(m_kind[started], m_kind_bin) 
            for i in np.unique(ibin): 
                inbin = np.where(ibin == i)

                isort = np.argsort(dMhalo[started[inbin]])

                dsfr[started[inbin[0][isort]]] = \
                        np.sort(np.random.randn(len(inbin[0]))) * theta_sfh['sig_abias']

        assert dsfr.min() != -999.
        
        if sig_noise > 0.: 
            dsfr += sig_noise * np.random.randn(len(dMhalo))
        
        # correct initial SFR to match long-term assembly bias 
        SHsnaps['sfr0'][indices] = mu_sfr0 + dsfr

        F_sfr = _logSFR_dSFR 

        sfr_kwargs = {'dSFR': dsfr, 'theta_sfms': theta_sfms}

    elif theta_sfh['name'] == 'random_step': 
        # completely random 
        # amplitude is sampled randomly from a Gaussian with sig_logSFR = 0.3 
        # time steps are sampled randomly from a unifrom distribution [dt_min, dt_max]
        if 'dt_min' not in theta_sfh: 
            raise ValueError
        if 'dt_max' not in theta_sfh: 
            raise ValueError
                
        # Random step function duty cycle 
        del_t_max = UT.t_nsnap(1) - UT.t_nsnap(nsnap0) #'nsnap_start'][indices].max()) 
        
        # the range of the steps 
        tshift_min = theta_sfh['dt_min'] 
        tshift_max = theta_sfh['dt_max'] 

        # get the times when the amplitude changes 
        n_col = int(np.ceil(del_t_max/tshift_min))+1  # number of columns 
        n_gal = len(indices)

        tshift = np.zeros((n_gal, n_col))
        tshift[:,1:] = np.random.uniform(tshift_min, tshift_max, size=(n_gal, n_col-1))
        tsteps = np.cumsum(tshift , axis=1) + np.tile(UT.t_nsnap(SHsnaps['nsnap_start'][indices]), (n_col, 1)).T
        del tshift
        # make sure everything evolves properly until the end
        assert tsteps[range(n_gal), n_col-1].min() > UT.t_nsnap(1)

        dlogSFR_amp = np.random.randn(n_gal, n_col) * theta_sfh['sigma']
        dlogSFR_amp[:,0] = SHsnaps['sfr0'][indices] - mu_sfr0

        F_sfr = _logSFR_dSFR_tsteps
        
        sfr_kwargs = {'dlogSFR_amp': dlogSFR_amp, 'tsteps': tsteps,'theta_sfms': theta_sfms}
    
    elif theta_sfh['name'] == 'random_step_fluct': 
        # completely random amplitude that is sampled from a Gaussian with sig_logSFR
        # EXCEPT each adjacent timesteps have alternating sign amplitudes
        # time steps have width specified tduty 
        dt_tot= UT.t_nsnap(1) - UT.t_nsnap(nsnap0) # total cosmic time of evolution 

        tduty = theta_sfh['tduty'] # dutycycle time

        # get the times when the amplitude changes 
        n_col = int(np.ceil(dt_tot / tduty))+2  # number of columns 
        n_gal = len(indices)    # number of galaxies
        tshift = np.tile(tduty, (n_gal, n_col))
        tshift[:,0] = 0.
        tshift[:,1] = np.random.uniform(0., tduty, n_gal) 
        tsteps = np.cumsum(tshift , axis=1) + np.tile(UT.t_nsnap(SHsnaps['nsnap_start'][indices]), (n_col, 1)).T
        del tshift
        # make sure everything evolves properly until the end
        assert tsteps[range(n_gal), n_col-1].min() > UT.t_nsnap(1)
        
        # dlogSFR absolute amplitue 
        dlogSFR_amp = np.abs(np.random.randn(n_gal, n_col)) * theta_sfh['sigma']
        dlogSFR_amp[:,0] = SHsnaps['sfr0'][indices] - mu_sfr0 # dlogSFR at nsnap_start 

        # now make every other time step has fluctuating signs!
        pos = dlogSFR_amp[:,0] >= 0.
        neg = ~pos

        fluct = np.ones(n_col-1)
        fluct[2 * np.arange(int(np.ceil(float(n_col-1)/2.)))] *= -1.
        
        dlogSFR_amp[pos,1:] *= fluct 
        dlogSFR_amp[neg,1:] *= -1. * fluct

        # testing the distribution of delta SFR
        if testing: 
            for i in range(n_col):
                print('std of dlogSFR amp = %f' % np.std(dlogSFR_amp))
                plt.hist(dlogSFR_amp[:,i], range=(-1., 1.), 
                        linewidth=2, histtype='step', density=True, label='Step '+str(i)) 
                plt.legend()
                plt.xlabel(r'$\Delta\log\,\mathrm{SFR}$', fontsize=20)
                plt.xlim([-1., 1.])
            plt.savefig(''.join([UT.fig_dir(), 'random_step_fluct_test.png']), bbox_inches='tight')

        F_sfr = _logSFR_dSFR_tsteps
        sfr_kwargs = {'dlogSFR_amp': dlogSFR_amp, 'tsteps': tsteps,'theta_sfms': theta_sfms}

    elif theta_sfh['name'] == 'random_step_abias_dt': 
        # SFH where the amplitude w.r.t. the SFS is correlated with halo mass growth over 
        # tdyn(t). The amplitude is sampled at every time step specified by `tduty`
        dt_tot= UT.t_nsnap(1) - UT.t_nsnap(nsnap0) # total cosmic time of evolution 
        tduty = theta_sfh['tduty'] # dutycycle time
        sigma_int = np.sqrt(theta_sfh['sigma_tot']**2 - theta_sfh['sigma_corr']**2) # intrinsic sigma 

        # get the times when the amplitude changes 
        n_col = int(np.ceil(dt_tot / tduty))+2  # number of columns 
        n_gal = len(indices)    # number of galaxies
        tshift = np.tile(tduty, (n_gal, n_col))
        tshift[:,0] = 0.
        tshift[:,1] = np.random.uniform(0., tduty, n_gal) 
        tsteps = np.cumsum(tshift , axis=1) + \
                np.tile(UT.t_nsnap(SHsnaps['nsnap_start'][indices]), (n_col, 1)).T
        del tshift

        # M_h of the galaxies throughout the snapshots 
        Mh_snaps = np.zeros((n_gal, nsnap0+9), dtype=np.float32)
        Mh_snaps[:,0] = SHsnaps['halo.m'][indices]
        Mm_snaps = np.zeros((n_gal, nsnap0+9), dtype=np.float32)
        Mm_snaps[:,0] = SHsnaps['m.max'][indices]
        for isnap in range(2, nsnap0+10): 
            Mh_snaps[:,isnap-1] = SHsnaps['halo.m.snap'+str(isnap)][indices]
            Mm_snaps[:,isnap-1] = SHsnaps['m.max.snap'+str(isnap)][indices]
        
        t_snaps = UT.t_nsnap(range(1, nsnap0+10))

        # now we need to use these M_h to calculate the halo growth rates
        # at every time step t_i over the range t_i - dt_dMh to t_i. This means
        # we need to get M_h at both t_i and t_i-dt_dMh...
        f_dMh = np.zeros(tsteps.shape, dtype=np.float32) # growth rate of halos over t_i and t_i - dt_Mh
        #Mh_ts = np.zeros(tsteps.shape, dtype=np.float32)
        Mm_ts = np.zeros(tsteps.shape, dtype=np.float32)
        iii, iiii = 0, 0 
        for i_g in range(n_gal): 
            in_sim = (Mm_snaps[i_g,:] > 0.)
            t_snaps_i = t_snaps[in_sim]
            Mh_snaps_i = np.power(10., Mh_snaps[i_g, in_sim]) 
            Mm_snaps_i = np.power(10., Mm_snaps[i_g, in_sim]) 
            
            t_i = tsteps[i_g,:] # t_i
            t_imdt = t_i - UT.tdyn_t(t_i) # t_i - tdyn 
            #t_imdt = tsteps[i_g,:] - theta_sfh['dt_dMh'] # t_i - dt_dMh

            Mh_ti = np.interp(t_i, t_snaps_i[::-1], Mh_snaps_i[::-1]) 
            Mh_timdt = np.interp(t_imdt, t_snaps_i[::-1], Mh_snaps_i[::-1]) 
            
            f_dMh[i_g,:] = Mh_ti / Mh_timdt #1. - Mh_timdt /  
            #Mh_ts[i_g,:] = np.log10(Mh_ti)
            Mm_ti = np.interp(t_i, t_snaps_i[::-1], Mm_snaps_i[::-1]) 
            Mm_ts[i_g,:] = np.log10(Mm_ti)
            
            if testing: 
                if (SHsnaps['nsnap_start'][indices][i_g] == 15) and (iii < 10): 
                    fig = plt.figure(figsize=(8,4)) 
                    sub = fig.add_subplot(121)
                    sub.plot(t_snaps_i, Mh_snaps_i/Mh_snaps_i[0], c='k', ls='--') 
                    sub.fill_between([t_imdt[0], t_i[0]], [0., 0.], [1.2, 1.2], color='C0')
                    sub.fill_between([t_imdt[-2], t_i[-2]], [0., 0.], [1.2, 1.2], color='C1')
                    sub.vlines(UT.t_nsnap(15), 0., 1.2) 
                    sub.vlines(UT.t_nsnap(1),  0., 1.2) 
                    sub.set_xlim([0., 14.]) 
                    sub.set_ylim([0., 1.2]) 

                    sub = fig.add_subplot(122)
                    sub.plot(t_i, f_dMh[i_g,:], c='k', ls='--') 
                    sub.scatter([t_i[0]], [f_dMh[i_g,0]], c='C0')
                    sub.scatter([t_i[-2]], [f_dMh[i_g,-2]], c='C1')
                    sub.vlines(UT.t_nsnap(15), -0.2, 5.) 
                    sub.vlines(UT.t_nsnap(1), -0.2, 5.) 
                    sub.set_xlim([0., 14.]) 
                    sub.set_ylim([0., 5.]) 

                    fig.savefig(''.join([UT.fig_dir(), 'abiastest', str(iii), '.png']), bbox_inches='tight')
                    plt.close()
                    iii += 1 
                
                if in_sim[15]: 
                    if Mh_snaps_i[0] < Mh_snaps_i[14]: 
                        if iiii > 10: continue 
                        fig = plt.figure(figsize=(8,4)) 
                        sub = fig.add_subplot(121)
                        sub.plot(t_snaps_i, Mh_snaps_i/Mh_snaps_i[0], c='k', ls='--') 
                        sub.fill_between([t_imdt[0], t_i[0]], [0., 0.], [1.2, 1.2], color='C0')
                        sub.fill_between([t_imdt[-2], t_i[-2]], [0., 0.], [1.2, 1.2], color='C1')
                        sub.vlines(UT.t_nsnap(15), 0., 1.2) 
                        sub.vlines(UT.t_nsnap(1),  0., 1.2) 
                        sub.set_xlim([0., 14.]) 
                        sub.set_ylim([0., 1.2]) 

                        sub = fig.add_subplot(122)
                        sub.plot(t_i, f_dMh[i_g,:], c='k', ls='--') 
                        sub.scatter([t_i[0]], [f_dMh[i_g,0]], c='C0')
                        sub.scatter([t_i[-2]], [f_dMh[i_g,-2]], c='C1')
                        sub.vlines(UT.t_nsnap(15), -0.2, 1.) 
                        sub.vlines(UT.t_nsnap(1), -0.2, 1.) 
                        sub.set_xlim([0., 14.]) 
                        sub.set_ylim([0., 5.]) 
                        sub.text(0.05, 0.05, r'$\log\,M_h='+str(round(np.log10(Mh_snaps_i[0]),2))+'$',
                                fontsize=15, ha='left', va='bottom', transform=sub.transAxes)
                        fig.savefig(''.join([UT.fig_dir(), 'weird_abiastest', str(iiii), '.png']), bbox_inches='tight')
                        plt.close()
                        iiii += 1

        # calculate the d(log SFR) amplitude at t_steps 
        # at each t_step, for a given halo mass bin of 0.2 dex, 
        # rank order the SFRs and the halo growth rate 
        dlogSFR_amp = np.zeros(f_dMh.shape, dtype=np.float32)
        dlogMh = 0.1
        for ii in range(f_dMh.shape[1]): 
            f_dMh_i = f_dMh[:,ii]
            Mh_ti = Mm_ts[:,ii] 
            mh_bins = np.arange(Mh_ti.min(), Mh_ti.max(), dlogMh)
            #mh_bins = np.linspace(Mh_ti.min(), Mh_ti.max(), 100)
            
            if testing: 
                fig = plt.figure(1, figsize=(4*np.int(np.ceil(float(len(mh_bins))/3.)+1.), 8))
            for i_m in range(len(mh_bins)-1): 
                inbin = ((Mh_ti >= mh_bins[i_m]) & (Mh_ti < mh_bins[i_m]+dlogMh))
                #inbin = ((Mh_ti >= mh_bins[i_m]) & (Mh_ti < mh_bins[i_m+1]))
                n_bin = np.sum(inbin)
                 
                isort = np.argsort(-1. * f_dMh_i[inbin])

                irank = np.zeros(n_bin)
                irank[isort] = (np.arange(n_bin) + 0.5)/np.float(n_bin)
                
                # i_rank = 1/2 (1 - erf(x/sqrt(2)))
                # x = (SFR - avg_SFR)/sigma_SFR
                # dSFR = sigma_SFR * sqrt(2) * erfinv(1 - 2 i_rank)
                dlogSFR_amp[inbin, ii] = \
                        theta_sfh['sigma_corr'] * 1.41421356 * erfinv(1. - 2. * irank) 
                
                if testing: 
                    sub = fig.add_subplot(3, np.int(np.ceil(np.float(len(mh_bins))/3.)+1), i_m+1)
                    sub.scatter(f_dMh_i[inbin], 0.3 * np.random.randn(n_bin), c='k', s=2)
                    sub.scatter(f_dMh_i[inbin], dlogSFR_amp[inbin, ii] + sigma_int * np.random.randn(n_bin), c='r', s=2, lw=0)
                    sub.set_xlim([-0.5, 1.])
                    sub.set_ylim([-1., 1.])
            if testing: 
                plt.savefig(''.join([UT.fig_dir(), 'random_step_abias_dt_test', str(ii), '.png']), bbox_inches='tight')
                plt.close()

                fig = plt.figure(figsize=(8,4)) 
                sub = fig.add_subplot(121)
                DFM.hist2d(Mh_ti, f_dMh_i, levels=[0.68, 0.95], range=[[10., 14.],[0., 10.]], color='k', 
                        plot_datapoints=False, fill_contours=False, plot_density=True, ax=sub)
                sub.set_xlim([10., 14.]) 
                sub = fig.add_subplot(122)
                DFM.hist2d(Mh_ti, dlogSFR_amp[:,ii], levels=[0.68, 0.95], range=[[10., 14.],[-1., 1.]], color='k', 
                        plot_datapoints=False, fill_contours=False, plot_density=True, ax=sub)
                sub.set_xlim([10., 14.]) 
                fig.savefig(''.join([UT.fig_dir(), 'abias_fdMh', str(ii), '.png']), bbox_inches='tight')
                plt.close()

        del f_dMh
        del Mm_ts
        
        # add in intrinsic scatter
        dlogSFR_int = np.random.randn(n_gal, n_col) * sigma_int 
        dlogSFR_amp += dlogSFR_int
        SHsnaps['sfr0'][indices] = mu_sfr0 + dlogSFR_amp[:,0] # change SFR_0 so that it's assembly biased as well
        
        F_sfr = _logSFR_dSFR_tsteps
        sfr_kwargs = {'dlogSFR_amp': dlogSFR_amp, 'tsteps': tsteps,'theta_sfms': theta_sfms}
    else:
        raise NotImplementedError
    return F_sfr, sfr_kwargs
Example #54
0
def plot_occupations_decs(obs = "gmf", galtype = "ncen", clotter = True):

    npts = 2e3
    mass = np.logspace(10, 14, npts)
    pretty_color = prettycolors()
    if clotter == False:

        print "running compute_occupation_prediction first"

    else:

       if galtype == "ncen":

           ylabel = r'$\langle N_{\rm cen}(M_{h})\rangle$'
           xlabel = r'$M_{h}\; [h^{-1} \; M_{\odot}]$'
       if galtype == "nsat":

           ylabel = r'$\langle N_{\rm sat}(M_{h})\rangle$'
           xlabel = r'$M_{h}\; [h^{-1} \; M_{\odot}]$'

       old_18 = np.loadtxt("results/"+galtype+"_old_"+obs+"_dec_18.0.dat")
       young_18 = np.loadtxt("results/"+galtype+"_young_"+obs+"_dec_18.0.dat")
       old_19 = np.loadtxt("results/"+galtype+"_old_"+obs+"_dec_19.0.dat")
       young_19 = np.loadtxt("results/"+galtype+"_young_"+obs+"_dec_19.0.dat")
       old_20 = np.loadtxt("results/"+galtype+"_old_"+obs+"_dec_20.0.dat")
       young_20 = np.loadtxt("results/"+galtype+"_young_"+obs+"_dec_20.0.dat") 
         
       fig = plt.figure(1, figsize=(15,5))
       gs = gridspec.GridSpec(1, 3)# height_ratios=[2.5, 1], width_ratios=[1,1])  
       pretty_colors=prettycolors()
      
       ##### MR18 DEC ######

       ax = plt.subplot(gs[0])
       a_old, b_old, c_old = np.percentile(old_18, [16, 50, 84], axis=0) 
       a_young, b_young, c_young = np.percentile(young_18, [16, 50, 84], axis=0) 

       ax.plot(mass, b_young, color=pretty_colors[1], linewidth=2.) 
       ax.plot(mass, b_old, color=pretty_colors[4], linewidth=2.)
       ax.fill_between(mass, a_young, c_young, color=pretty_colors[1], alpha=0.5, edgecolor=pretty_colors[1] ) 
       ax.fill_between(mass, a_old, c_old, color=pretty_colors[4], alpha=0.5, edgecolor=pretty_colors[4]) 
 
       blue_line = mlines.Line2D([], [], ls = '-', c = pretty_color[1], linewidth=2, label = '$type-1$ $halos$')
       red_line = mlines.Line2D([], [], ls = '-', c = pretty_color[4], linewidth=2, label = '$type-2$ $halos$')
       import matplotlib as mpl
       #mpl.rc('font',family='serif')
       plt.legend(handles=[blue_line, red_line], frameon=False, loc= 2 , fontsize=15) 
       ax.set_ylabel(ylabel, fontsize=27)
       ax.set_xlabel(xlabel, fontsize=27)
       ax.set_yscale('log') 
       ax.set_xscale('log')
       ax.set_xlim([10**10, 8. * 10**13]) 
       if galtype == "ncen":
           ax.set_ylim([0.1, 2]) 
           ax.text(4 * 10**12. , 1.3, r'$M_{r}<-18$', fontsize=18) 
       if galtype == "nsat":
           ax.set_ylim([0.1,100])
           ax.text(4 * 10**12. , 50, r'$M_{r}<-18$', fontsize=18) 
       ##### Mr19 DEC ######

       ax = plt.subplot(gs[1])
       a_old, b_old, c_old = np.percentile(old_19, [16, 50, 84], axis=0) 
       a_young, b_young, c_young = np.percentile(young_19, [16, 50, 84], axis=0) 

       ax.plot(mass, b_young, color=pretty_colors[1], linewidth=2.) 
       ax.plot(mass, b_old, color=pretty_colors[4], linewidth=2.)
       ax.fill_between(mass, a_young, c_young, color=pretty_colors[1], alpha=0.5, edgecolor=pretty_colors[1]) 
       ax.fill_between(mass, a_old, c_old, color=pretty_colors[4], alpha=0.5, edgecolor=pretty_colors[4]) 
 
       blue_line = mlines.Line2D([], [], ls = '-', c = pretty_color[3], linewidth=2, label = 'high-concentration halos')
       red_line = mlines.Line2D([], [], ls = '-', c = pretty_color[4], linewidth=2, label = 'low-concentration halos')
       ax.set_yscale('log') 
       ax.set_xscale('log')
       ax.set_yticklabels([])
       ax.set_xlabel(xlabel, fontsize=27)
       ax.set_xlim([10**10, 8.*10**13]) 
       if galtype == "ncen":
           ax.set_ylim([0.1, 2]) 
           ax.text(4 * 10**12. , 1.3, r'$M_{r}<-19$', fontsize=18) 
       if galtype == "nsat":
           ax.set_ylim([0.1,100])
           ax.text(4 * 10**12. , 50, r'$M_{r}<-19$', fontsize=18) 
       ##### Mr20 DEC #######      
 
       ax = plt.subplot(gs[2])
       a_old, b_old, c_old = np.percentile(old_20, [16, 50, 84], axis=0) 
       a_young, b_young, c_young = np.percentile(young_20, [16, 50, 84], axis=0) 

       ax.plot(mass, b_young, color=pretty_colors[1], linewidth=2.) 
       ax.plot(mass, b_old, color=pretty_colors[4], linewidth=2.)
       ax.fill_between(mass, a_young, c_young, color=pretty_colors[1], alpha=0.5, edgecolor=pretty_colors[1]) 
       ax.fill_between(mass, a_old, c_old, color=pretty_colors[4], alpha=0.5, edgecolor=pretty_colors[4]) 
 
       blue_line = mlines.Line2D([], [], ls = '-', c = pretty_color[3], linewidth=2, label = 'high-concentration halos')
       red_line = mlines.Line2D([], [], ls = '-', c = pretty_color[4], linewidth=2, label = 'low-concentration halos')
       
       ax.set_yscale('log') 
       ax.set_xscale('log')
       ax.set_yticklabels([])
       ax.set_xlabel(xlabel, fontsize=27)
       ax.set_xlim([10**10, 8. * 10**13]) 
       if galtype == "ncen":
           ax.set_ylim([0.1, 2]) 
           ax.text(4 * 10**12. , 1.3, r'$M_{r}<-20$', fontsize=18) 
       if galtype == "nsat":
           ax.set_ylim([0.1,100])
           ax.text(4 * 10**12. , 50, r'$M_{r}<-20$', fontsize=18) 
       fig.subplots_adjust(wspace=0.0, hspace=0.0)
       fig_name = ''.join([ut.fig_dir(), 
        'paper', 
        '.abhod.',
        galtype,
        obs,
        '.pdf'])
       fig.savefig(fig_name, bbox_inches='tight')
       plt.close()
       
    return None 
Example #55
0
def div_K(div_func='kl'):
    ''' compare the KL or Renyi divergence for the following with their using different K values 
    - D( gauss(C_X) || gauss(C_X) ) 
    - D( mock X || gauss(C_X))
    - D( mock X || p(X) KDE)
    - D( mock X || p(X) GMM) 
    - D( mock X || PI p(X^i_ICA) KDE)
    - D( mock X || PI p(X^i_ICA) GMM)
    '''
    lbls = [r'$D( P(k) \parallel \mathcal{N}({\bf C}))$',
            r'$D( P(k) \parallel p_\mathrm{KDE}(P(k)))$',
            r'$D( P(k) \parallel p_\mathrm{GMM}(P(k)))$',
            r'$D( P(k) \parallel \prod_i p_\mathrm{KDE}(P(k)_i^\mathrm{ICA}))$', 
            r'$D( P(k) \parallel \prod_i p_\mathrm{GMM}(P(k)_i^\mathrm{ICA}))$']

    fig = plt.figure(figsize=(20,4))
    for i_obv, obv in enumerate(['pk.ngc', 'gmf']):
        if obv == 'pk.ngc': 
            Nref = 2000
            if div_func == 'kl': hranges = [[-0.5, 0.5], [-0.5, 7.], [-0.5, 0.5], [-0.5, 0.5], [-0.5, 0.5]]##7.]
            else: hranges = [[-0.5, 0.5] for i in range(5)]
            Ks = [5, 10, 15] 
        elif obv == 'gmf': 
            Nref = 10000
            hranges = [[-0.1, 0.4], [-0.1, 0.4], [-0.1, 0.4], [-0.1, 0.4], [-0.1, 0.4]]##7.]
            Ks = [10] 

        for K in Ks: 
            fs = ['pX_gauss.K'+str(K), 'pX_scottKDE.K'+str(K), 'pX_GMM.K'+str(K)+'.ncomp30', 
                'pXi_ICA_scottKDE.K'+str(K), 'pXi_ICA_GMM.K'+str(K)+'.ncomp30'] 
            divs, divs_ref = [], [] 
            for f in fs: 
                f_div = ''.join([UT.dat_dir(), 'diverg.', obv, '.', f, '.Nref', str(Nref), '.', 
                    div_func, '.dat']) 
                try: 
                    div = np.loadtxt(f_div)
                except IOError: 
                    print f_div
                    continue 
                divs.append(div) 
         
            nbins = 50
            bkgd = fig.add_subplot(2,1,i_obv+1, frameon=False)
            for i_div, div, lbl in zip(range(len(fs)), divs, lbls): 
                sub = fig.add_subplot(2,5,len(fs)*i_obv+i_div+1)
                y_max = 0. 
                hh = np.histogram(div, normed=True, range=hranges[i_div], bins=nbins)
                bp = UT.bar_plot(*hh) 
                sub.fill_between(bp[0], np.zeros(len(bp[0])), bp[1], edgecolor='none') 
                y_max = max(y_max, bp[1].max()) 
                sub.set_xlim(hranges[i_div])  
                sub.set_ylim([0., y_max*1.4]) 
                if i_obv == 0: 
                    sub.set_title(lbl) 
    
        if div_func == 'kl': 
            bkgd.set_xlabel(r'KL divergence', fontsize=20, labelpad=20)
        elif div_func == 'renyi0.5': 
            bkgd.set_xlabel(r'R\'enyi-$\alpha$ divergence', fontsize=20, labelpad=20)
        bkgd.set_xticklabels([])
        bkgd.set_yticklabels([])
        bkgd.tick_params(labelcolor='none', top='off', bottom='off', left='off', right='off')

    fig.subplots_adjust(wspace=.15, hspace=0.3)
    f_fig = ''.join([UT.fig_dir(), 'tests/Ktest_kNNdiverg.', div_func, '.png'])
    fig.savefig(f_fig, bbox_inches='tight') 
    return None
Example #56
0
def p_Xw_i(mock, ell=0, rebin=None, krange=None, ica=False, pca=False): 
    ''' Test the probability distribution function of each X_w^i
    component -- p(X_w^i). First compare the histograms of p(X_w^i) 
    with N(0,1). Then compare the gaussian KDE of p(X_w^i).
    '''
    Pk = NG.dataX(mock, ell=ell, rebin=rebin, krange=krange)
    X, _ = NG.meansub(Pk)
    str_w = 'W'
    if ica and pca: 
        raise ValueError
    if ica: # ICA components
        # ICA components do not need to be Gaussian.
        # in fact the whole point of the ICA transform
        # is to capture the non-Gaussianity...
        X_white, _ = NG.whiten(X) # whitened data
        X_w, _ = NG.Ica(X_white) 
        str_w = 'ICA'
    if pca: # PCA components
        X_w, _ = NG.whiten(X, method='pca') # whitened data
        str_w = 'PCA'
    if not ica and not pca: # just whitened 
        X_w, W = NG.whiten(X) # whitened data
    
    # p(X_w^i) histograms
    fig = plt.figure(figsize=(15,7))
    sub = fig.add_subplot(121)
    for i_bin in range(X_w.shape[1]): 
        p_X_w, edges = np.histogram(X_w[:,i_bin], normed=True)
        p_X_w_arr = UT.bar_plot(edges, p_X_w)
        sub.plot(p_X_w_arr[0], p_X_w_arr[1])
    x = np.arange(-5., 5.1, 0.1)
    sub.plot(x, UT.gauss(x, 1., 0.), c='k', lw=3, label='$\mathcal{N}(0,1)$')
    sub.set_xlim([-2.5, 2.5])
    sub.set_xlabel('$\mathtt{X_{'+str_w+'}}$', fontsize=25) 
    sub.set_ylim([0., 0.6])
    sub.set_ylabel('$\mathtt{P(X_{'+str_w+'})}$', fontsize=25) 
    sub.legend(loc='upper right') 

    # p(X_w^i) gaussian KDE fits  
    pdfs = NG.p_Xw_i(X_w, range(X_w.shape[1]), x=x)

    sub = fig.add_subplot(122)
    for i_bin in range(X_w.shape[1]): 
        sub.plot(x, pdfs[i_bin])
    sub.plot(x, UT.gauss(x, 1., 0.), c='k', lw=3, label='$\mathcal{N}(0,1)$')
    sub.set_xlim([-2.5, 2.5])
    sub.set_xlabel('$\mathtt{X_{W}}$', fontsize=25) 
    sub.set_ylim([0., 0.6])
    sub.set_ylabel('$\mathtt{P(X_{W})}$', fontsize=25) 
    sub.legend(loc='upper right') 

    str_ica, str_pca = '', ''
    if ica: 
        str_ica = '.ICA'
    if pca: 
        str_pca = '.PCA'

    if rebin is None: 
        f = ''.join([UT.fig_dir(), 'tests/test.p_Xw_i', str_pca, str_ica, '.', mock, '.ell', str(ell), '.png'])
    else: 
        f = ''.join([UT.fig_dir(), 'tests/test.p_Xw_i', str_pca, str_ica, '.', mock, '.ell', str(ell), '.rebin', str(rebin), '.png'])
    fig.savefig(f, bbox_inches='tight') 
    return None 
Example #57
0
def Subvolume_FullvolumeCut(N_sub, ratio=False): 
    ''' Test the 2PCF estimates from MultiDark subvolume versus the 
    2PCF for the entire MultiDark volume WITHOUT periodic boundary conditions
    and actual pair counts, CUT into subvolumes of the same size *AFTER*
    populate mock 


    Parameters
    ----------
    N_sub : (int)
        Number of subvolumes to sample

    '''
    prettyplot()
    pretty_colors = prettycolors()

    pickle_file = ''.join([
        '/export/bbq2/hahn/ccppabc/dump/', 
        'xi_subvolume_fullvolume_cut_test', 
        '.Nsub', str(N_sub), 
        '.p'])
    
    fig = plt.figure(1)
    sub = fig.add_subplot(111)

    xi_bin = xi_binedges() 
    
    # Entire MultiDark Volume (No Periodic Boundary Conditions) 
    model = PrebuiltHodModelFactory('zheng07', threshold=-21)
    halocat = CachedHaloCatalog(simname = 'multidark', redshift = 0, halo_finder = 'rockstar')

    sub_RR = data_RR(box='md_sub')
    sub_randoms = data_random(box='md_sub')
    sub_NR = len(sub_randoms)

    rmax = xi_bin.max()
    full_approx_cell1_size = [rmax , rmax , rmax]
    full_approx_cellran_size = [rmax , rmax , rmax]

    model.populate_mock(halocat, enforce_PBC=False)
    subvol_id = util.mk_id_column(table=model.mock.galaxy_table)
    full_pos = three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z')
    
    # Full Volume 
    if os.path.isfile(pickle_file):
        data_dump = pickle.load(open(pickle_file, 'rb'))
        full_xi = data_dump['full_xi']
    else: 
        model = PrebuiltHodModelFactory('zheng07', threshold=-21)
        halocat = CachedHaloCatalog(simname = 'multidark', redshift = 0, halo_finder = 'rockstar')

        full_randoms = data_random(box='md_all')
        full_RR = data_RR(box='md_all')
        full_NR = len(full_randoms)

        rmax = xi_bin.max()
        full_approx_cell1_size = [rmax , rmax , rmax]
        full_approx_cellran_size = [rmax , rmax , rmax]

        model.populate_mock(halocat, enforce_PBC=False)
        full_pos = three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z')
        
        full_xi = tpcf(
                full_pos, xi_bin, 
                randoms=full_randoms, period=None, 
                do_auto=True,
                do_cross=False, 
                num_threads=5, 
                max_sample_size=int(full_pos.shape[0]), estimator='Natural', 
                approx_cell1_size=full_approx_cell1_size, 
                approx_cellran_size=full_approx_cellran_size,
                RR_precomputed = full_RR,
                NR_precomputed = full_NR)
        data_dump = {} 
        data_dump['full_xi'] = full_xi
    
    if not ratio:  
        sub.plot(0.5*(xi_bin[:-1]+xi_bin[1:]), full_xi, 
                lw=2, ls='-', c='k', label=r'Full Volume') 

    if os.path.isfile(pickle_file):
        fullcut_xi_list = data_dump['fullcut_xi']['fullcut_xi_list']
        fullcut_xi_avg =  data_dump['fullcut_xi']['fullcut_xi_avg']
    else: 
        data_dump['fullcut_xi'] = {}
        fullcut_xi_list = [] 
        fullcut_xi_tot = np.zeros(len(xi_bin)-1)
        for id in np.unique(subvol_id)[:N_sub]: 
            print 'Subvolume ', id
            in_cut = np.where(subvol_id == id) 

            fullcut_pos = full_pos[in_cut]

            fullcut_xi = tpcf(
                    fullcut_pos, xi_bin, 
                    randoms=sub_randoms, period=None, 
                    do_auto=True,
                    do_cross=False, 
                    num_threads=5, 
                    max_sample_size=int(fullcut_pos.shape[0]), estimator='Natural', 
                    approx_cell1_size=full_approx_cell1_size, 
                    approx_cellran_size=full_approx_cellran_size,
                    RR_precomputed=sub_RR,
                    NR_precomputed=sub_NR)

            fullcut_xi_list.append(fullcut_xi)
            fullcut_xi_tot += fullcut_xi
        
        fullcut_xi_avg = fullcut_xi_tot / np.float(N_sub)
        data_dump['fullcut_xi']['fullcut_xi_list']= fullcut_xi_list
        data_dump['fullcut_xi']['fullcut_xi_avg']= fullcut_xi_avg

    if not ratio:  
        sub.plot(0.5*(xi_bin[:-1]+xi_bin[1:]), fullcut_xi_avg, 
                lw=2, ls='-', c='k', label=r'Full Volume Cut Average') 
    else: 
        sub.plot(0.5*(xi_bin[:-1]+xi_bin[1:]), fullcut_xi_avg/full_xi, 
                lw=2, ls='-', c='k', label=r'Full Volume Cut Average') 
    
    if not os.path.isfile(pickle_file):
        # MultiDark SubVolume (precomputed RR pairs) 
        sub_model = PrebuiltHodModelFactory('zheng07', threshold=-21)
        sub_model.new_haloprop_func_dict = {'sim_subvol': util.mk_id_column}
        sub_halocat = CachedHaloCatalog(simname = 'multidark', redshift = 0, halo_finder = 'rockstar')
        sub_RR = data_RR(box='md_sub')
        sub_randoms = data_random(box='md_sub')
        sub_NR = len(sub_randoms)
    
        
        sub_xis_list = [] 
        sub_xis = np.zeros(len(full_xi)) 

        for ii in range(1,N_sub): 
            print 'Subvolume ', ii 
            # randomly sample one of the subvolumes
            rint = ii #np.random.randint(1, 125)
            simsubvol = lambda x: util.mask_func(x, rint)
            sub_model.populate_mock(sub_halocat, masking_function=simsubvol, enforce_PBC=False)
               
            pos = three_dim_pos_bundle(sub_model.mock.galaxy_table, 'x', 'y', 'z')

            xi, yi , zi = util.random_shifter(rint)
            temp_randoms = sub_randoms.copy()
            temp_randoms[:,0] += xi
            temp_randoms[:,1] += yi
            temp_randoms[:,2] += zi
            
            rmax = xi_bin.max()
            sub_approx_cell1_size = [rmax , rmax , rmax]
            sub_approx_cellran_size = [rmax , rmax , rmax]

            sub_xi = tpcf(
                    pos, xi_bin,  
                    randoms=temp_randoms, 
                    period = None, 
                    do_auto=True,
                    do_cross=False, 
                    num_threads=5, 
                    max_sample_size=int(pos.shape[0]), 
                    estimator='Natural', 
                    approx_cell1_size = sub_approx_cell1_size, 
                    approx_cellran_size = sub_approx_cellran_size,
                    RR_precomputed=sub_RR,
                    NR_precomputed=sub_NR)

            label = None 
            if ii == N_sub - 1: 
                label = 'Subvolumes'
            
            sub_xis += sub_xi
            sub_xis_list.append(sub_xi)

        sub_xi_avg = sub_xis/np.float(N_sub)

        data_dump['Natural'] = {} 
        data_dump['Natural']['sub_xi_avg'] = sub_xi_avg
        data_dump['Natural']['sub_xis_list'] = sub_xis_list 
    else: 
        sub_xis_list = data_dump['Natural']['sub_xis_list']
        sub_xi_avg = data_dump['Natural']['sub_xi_avg'] 

    if not os.path.isfile(pickle_file): 
        pickle.dump(data_dump, open(pickle_file, 'wb')) 

    if not ratio: 
        sub.plot(0.5*(xi_bin[:-1]+xi_bin[1:]), sub_xi_avg, 
                lw=2, ls='--', c=pretty_colors[3], label='Subvolume')
    else: 
        sub.plot(0.5*(xi_bin[:-1]+xi_bin[1:]), sub_xi_avg/full_xi, 
                lw=2, ls='--', c=pretty_colors[3], label='Subvolume')

    sub.set_xlim([0.1, 50.])
    sub.set_xlabel('r', fontsize=30)
    sub.set_xscale('log')
    
    if not ratio: 
        sub.set_ylabel(r"$\xi \mathtt{(r)}$", fontsize=25)
        sub.set_yscale('log')
    else: 
        sub.set_ylabel(r"$\overline{\xi^\mathtt{sub}}/\xi^\mathtt{all}$", fontsize=25)

    sub.legend(loc='lower left')
    
    if ratio: 
        fig_file = ''.join([util.fig_dir(), 'test_xi_subvolume_fullvolume_cut.Nsub', str(N_sub), '.ratio.png'])
    else:
        fig_file = ''.join([util.fig_dir(), 'test_xi_subvolume_fullvolume_cut.Nsub', str(N_sub), '.png'])
    fig.savefig(fig_file, bbox_inches='tight', dpi=100)
    plt.close()
    return None