def apply_pbcs(catalog, Lbox=250., epsilon=0.0001):
    catalog['x'], catalog['vx'] = enforce_periodicity_of_box(
        catalog['x'], Lbox, velocity=catalog['vx'])
    catalog['y'], catalog['vy'] = enforce_periodicity_of_box(
        catalog['y'], Lbox, velocity=catalog['vy'])
    catalog['z'], catalog['vz'] = enforce_periodicity_of_box(
        catalog['z'], Lbox, velocity=catalog['vz'])

    catalog['x'][catalog['x'] == Lbox] = Lbox - epsilon
    catalog['y'][catalog['y'] == Lbox] = Lbox - epsilon
    catalog['z'][catalog['z'] == Lbox] = Lbox - epsilon
    return catalog
def create_galsampled_dc2(umachine,
                          target_halos,
                          halo_indices,
                          galaxy_indices,
                          Lbox_target=256.):
    """
    """
    dc2 = Table()
    dc2['source_halo_id'] = umachine['hostid'][galaxy_indices]
    dc2['target_halo_id'] = np.repeat(target_halos['halo_id'][halo_indices],
                                      target_halos['richness'][halo_indices])

    idxA, idxB = crossmatch(dc2['target_halo_id'], target_halos['halo_id'])

    msg = "target IDs do not match!"
    assert np.all(dc2['source_halo_id'][idxA] == target_halos['source_halo_id']
                  [idxB]), msg

    target_halo_keys = ('x', 'y', 'z', 'vx', 'vy', 'vz')
    for key in target_halo_keys:
        dc2['target_halo_' + key] = 0.
        dc2['target_halo_' + key][idxA] = target_halos[key][idxB]
    dc2['target_halo_mass'] = 0.
    dc2['target_halo_mass'][idxA] = target_halos['fof_halo_mass'][idxB]

    source_galaxy_keys = ('host_halo_mvir', 'upid', 'host_centric_x',
                          'host_centric_y', 'host_centric_z',
                          'host_centric_vx', 'host_centric_vy',
                          'host_centric_vz', 'obs_sm', 'obs_sfr',
                          'sfr_percentile_fixed_sm')
    for key in source_galaxy_keys:
        dc2[key] = umachine[key][galaxy_indices]

    x_init = dc2['target_halo_x'] + dc2['host_centric_x']
    vx_init = dc2['target_halo_vx'] + dc2['host_centric_vx']
    dc2['x'], dc2['vx'] = enforce_periodicity_of_box(x_init,
                                                     Lbox_target,
                                                     velocity=vx_init)

    y_init = dc2['target_halo_y'] + dc2['host_centric_y']
    vy_init = dc2['target_halo_vy'] + dc2['host_centric_vy']
    dc2['y'], dc2['vy'] = enforce_periodicity_of_box(y_init,
                                                     Lbox_target,
                                                     velocity=vy_init)

    z_init = dc2['target_halo_z'] + dc2['host_centric_z']
    vz_init = dc2['target_halo_vz'] + dc2['host_centric_vz']
    dc2['z'], dc2['vz'] = enforce_periodicity_of_box(z_init,
                                                     Lbox_target,
                                                     velocity=vz_init)

    return dc2
Exemple #3
0
    def _sum_stat(self, theta, prior_range=None):
        
        self.model.param_dict['logM0'] = theta[0]
        self.model.param_dict['sigma_logM'] = theta[1]
        self.model.param_dict['logMmin'] = theta[2]
        self.model.param_dict['alpha'] = theta[3]
        self.model.param_dict['logM1'] = theta[4]

        self.model.populate_mock(self.halocat) 
        x = self.model.mock.galaxy_table['x']
        y = self.model.mock.galaxy_table['y']
        z = self.model.mock.galaxy_table['z']
        vz = self.model.mock.galaxy_table['vz']
        # applying RSD
        pos = return_xyz_formatted_array(x, y, z, velocity = vz, velocity_distortion_dimension = 'z')
        # enforcing PBC
        pos = enforce_periodicity_of_box(pos, self.boxsize)
        pos = pos.astype(np.float32)
        x, y, z = pos[:,0] , pos[:,1] , pos[:,2]
        results_wp = _countpairs.countpairs_wp(self.boxsize, self.pimax, 
                                           self.nthreads,
                                           self.binfile, x, y, z)
        wp = np.array(results_wp)[:,3]
        nbar = 1.*len(pos)/(self.boxsize)**3.

        return nbar , wp
Exemple #4
0
def measure_nbar_clustering(Mr):
    '''measure wp for the galaxy catalog
       with the luminosity threshold Mr'''

    #Corrfunc settings for wp measurements:

    boxsize = 250
    nthreads = 4
    pimax = 40.0
    binfile = path.join(path.dirname(path.abspath(__file__)),
                        "../", "bin")
    autocorr = 1

    filename = util.dat_dir()+'AM_Mr'+str(Mr)+'dat'
    cat = np.loadtxt(filename)
    pos = cat[:,1:4]
    x, y, z = pos[:,0], pos[:,1], pos[:,2] 
    vel = cat[:,4:7]
    vx, vy, vz =  vel[:,0], vel[:,1], vel[:,2] 
    #applying RSD:
    pos = return_xyz_formatted_array(x, y, z, velocity = vz, velocity_distortion_dimension = 'z')
    #enforcing periodic boundary conditions:
    pos = enforce_periodicity_of_box(pos, boxsize)
    pos = pos.astype(np.float32)
    x, y, z = pos[:,0] , pos[:,1] , pos[:,2]
    wp_result = _countpairs.countpairs_wp(boxsize, pimax, 
                                   nthreads, binfile, 
				   x, y, z)
    nbar = 1.*len(pos)/boxsize**3.
    wp = np.array(wp_result)[:,3]

    return nbar , wp    
Exemple #5
0
    def _sum_stat(self, theta, prior_range=None):
        
        self.model.param_dict['logM0'] = theta[0]
        self.model.param_dict['sigma_logM'] = theta[1]
        self.model.param_dict['logMmin'] = theta[2]
        self.model.param_dict['alpha'] = theta[3]
        self.model.param_dict['logM1'] = theta[4]
        gmff = []
        for i in xrange(1):
          self.model.populate_mock(self.halocat) 
          x = self.model.mock.galaxy_table['x']
          y = self.model.mock.galaxy_table['y']
          z = self.model.mock.galaxy_table['z']
          vz = self.model.mock.galaxy_table['vz']
          # applying RSD
          pos = return_xyz_formatted_array(x, y, z, velocity = vz, velocity_distortion_dimension = 'z')
          # enforcing PBC
          pos = enforce_periodicity_of_box(pos, self.boxsize)
          bperp = 0.14
          bpar = 0.75
          Lbox = np.array([self.boxsize, self.boxsize, self.boxsize])
          period = Lbox
          groups = FoFGroups(pos, b_perp=bperp, b_para=bpar, Lbox = Lbox , period=Lbox) 
  
          group_ids = groups.group_ids
          group_richness = richness(group_ids)
          gmff.append(np.histogram(np.array(group_richness) ,self.data_gmf_bin)[0] / (self.data_gmf_bin_width * self.boxsize**3.))
        gmf = np.mean(np.array(gmff) , axis = 0)
        nbar = 1.*len(pos)/(self.boxsize)**3.

        return nbar , gmf
Exemple #6
0
def compute_jackknife_covariance(Mr , nsub):
    
    box_size = 250.
    filename = util.dat_dir()+'AM_Mr'+str(Mr)+'dat'
    cat = np.loadtxt(filename)
    pos = cat[:,1:4]  #positions of all galaxies in the box
    vel = cat[:,4:7]  #velocities of all galaxies
    
    x , y , z = pos[:,0], pos[:,1], pos[:,2]
    vx, vy, vz = vel[:,0], vel[:,1], vel[:,2]
    rsd_pos = return_xyz_formatted_array(x, y, z, velocity = vz, velocity_distortion_dimension = 'z')
    rsd_pos = enforce_periodicity_of_box(rsd_pos, box_size)
    
    number_of_subboxes = nsub ** 3
   
    #Corrfunc settings for wp measurements:
    
    subbox_size = box_size / nsub
    nthreads = 4
    pimax = 25.0
    binfile = path.join(path.dirname(path.abspath(__file__)),
                        "../", "bin")
    len_wp = len(np.loadtxt(binfile))
    wps = np.zeros((number_of_subboxes , len_wp)) 
    nbars = np.zeros((number_of_subboxes , 1))

    for subvol_index in xrange(number_of_subboxes):

        sub_rsd_pos = mask_positions(rsd_pos , subvol_index , nsub)
        sub_rsd_pos = sub_rsd_pos.astype(np.float32)
        sub_x, sub_y, sub_z = sub_rsd_pos[:,0] , sub_rsd_pos[:,1] , sub_rsd_pos[:,2]
        wp_result = _countpairs.countpairs_wp(subbox_size, pimax, 
                                   nthreads, binfile, 
				   sub_x, sub_y, sub_z)
        nbars[subvol_index] = 1.*len(sub_rsd_pos)/subbox_size**3.
        wps[subvol_index] = np.array(wp_result)[:,3]
           
    nbar_covar = np.array([np.var(nbars)])    
    wp_covar = np.cov(wps.T)    

    return nbar_covar , wp_covar
def apply_pbcs(catalog, Lbox=250.):
    catalog['x'] = enforce_periodicity_of_box(catalog['x'], Lbox)
    catalog['y'] = enforce_periodicity_of_box(catalog['y'], Lbox)
    catalog['z'] = enforce_periodicity_of_box(catalog['z'], Lbox)
    return catalog
"""
"""
from slidingpercentile import python_sliding_rank
import os
from astropy.table import Table
from halotools.empirical_models import enforce_periodicity_of_box

dirname = "/Users/aphearin/Dropbox/protoDC2"
protoDC2_basename = "protoDC2_snapnum_487.hdf5"
protoDC2_z0 = Table.read(os.path.join(dirname, protoDC2_basename), path='data')

protoDC2_z0['x'] = enforce_periodicity_of_box(protoDC2_z0['x'], 256.)
protoDC2_z0['y'] = enforce_periodicity_of_box(protoDC2_z0['y'], 256.)
protoDC2_z0['z'] = enforce_periodicity_of_box(protoDC2_z0['z'], 256.)

w = 1001
print("...calculating sliding ranks")
satmask = protoDC2_z0['isCentral'] == 0
satranks = python_sliding_rank(protoDC2_z0['totalMassStellar'][satmask],
                               protoDC2_z0['totalStarFormationRate'][satmask],
                               w)
cenranks = python_sliding_rank(protoDC2_z0['totalMassStellar'][~satmask],
                               protoDC2_z0['totalStarFormationRate'][~satmask],
                               w)

protoDC2_z0['totalStarFormationRate_percentile_fixed_totalMassStellar'] = -1.
protoDC2_z0['totalStarFormationRate_percentile_fixed_totalMassStellar'][
    satmask] = (1. + satranks[satmask]) / float(w + 1)
protoDC2_z0['totalStarFormationRate_percentile_fixed_totalMassStellar'][
    ~satmask] = (1. + cenranks[~satmask]) / float(w + 1)
Exemple #9
0
def main():
     
    cov = np.loadtxt("../../data/wpxicov_dr72_bright0_mr21.0_z0.159_nj400")
    f_MD = (1. + 71.74*10**6. / (1000.)**3.)
    f_bol = (1. + 71.74*10.**6. / (250.)**3.)
      
    print("covariance correction factor=" , f_bol/f_MD)
    cov = cov*f_bol/f_MD
   
    model.param_dict['logM0'] =  12.59
    model.param_dict['sigma_logM'] =  0.49
    model.param_dict['logMmin'] =  12.78
    model.param_dict['alpha'] =  1.14
    model.param_dict['logM1'] =  13.99

    model.param_dict['mean_occupation_satellites_assembias_param1'] = 0.0
    halocat = CachedHaloCatalog(simname = 'bolplanck', redshift = 0, halo_finder = 'rockstar')
    model.populate_mock(halocat, enforce_PBC = True)
    pos = three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z')
  
    print("modelnumber density=" , len(pos)/halocat.Lbox**3.)
    print("data number density=" , 1.16*10**-3.) 
    x = model.mock.galaxy_table['x']
    y = model.mock.galaxy_table['y']
    z = model.mock.galaxy_table['z']
    vz = model.mock.galaxy_table['vz']

    # applying RSD
    pos = return_xyz_formatted_array(x, y, z, velocity = vz, velocity_distortion_dimension = 'z')
    # enfprcing PBC
    pos =  enforce_periodicity_of_box(pos, halocat.Lbox)

    tstart = time.time()
    t0 = tstart
    pos = pos.astype(np.float32)
    x, y, z = pos[:,0] , pos[:,1] , pos[:,2]
    t1 = time.time()
    print("Done reading the data - time taken = {0:10.1f} seconds"
          .format(t1 - t0))
    print("Beginning Correlation functions calculations")
    boxsize = halocat.Lbox
    nthreads = 4
    pimax = 40.0
    binfile = path.join(path.dirname(path.abspath(__file__)),
                        "../../", "bin")
    autocorr = 1
    numbins_to_print = 12
    
    print("\nRunning 2-D projected correlation function wp(rp)")
    results_wp = _countpairs.countpairs_wp(boxsize, pimax, nthreads,
                                           binfile, x, y, z)
    print("\n#            ******    wp: first {0} bins  *******         "
          .format(numbins_to_print))
    print("#      rmin        rmax       rpavg        wp       npairs")
    print("##########################################################")
    for ibin in range(numbins_to_print):
        items = results_wp[ibin]
        print("{0:12.4f} {1:12.4f} {2:10.4f} {3:10.1f} {4:10d}"
              .format(items[0], items[1], items[2], items[3], items[4]))
    print("-----------------------------------------------------------")
    
    data_wp = np.loadtxt("../../data/wpxi_dr72_bright0_mr21.0_z0.159_nj400")[:,1]
    print(data_wp.shape)
    data_wp_error = np.sqrt(np.diag(cov)[:12])
    print(data_wp_error.shape) 
    rbins = np.loadtxt(binfile)
    rs = np.mean(rbins , axis = 1)
    plt.figure(figsize=(10,10))
    plt.errorbar(rs , data_wp , data_wp_error , fmt=".k" , capsize = 2)
    plt.plot(rs , np.array(results_wp)[:,3])
    plt.loglog()
    plt.savefig("wp.pdf")
def build_output_snapshot_mock(umachine,
                               target_halos,
                               halo_indices,
                               galaxy_indices,
                               Lbox_target=256.):
    """
    """
    dc2 = Table()
    dc2['source_halo_id'] = umachine['hostid'][galaxy_indices]
    dc2['target_halo_id'] = np.repeat(
        target_halos['fof_halo_tag'][halo_indices],
        target_halos['richness'][halo_indices])

    idxA, idxB = crossmatch(dc2['target_halo_id'],
                            target_halos['fof_halo_tag'])

    msg = "target IDs do not match!"
    assert np.all(dc2['source_halo_id'][idxA] == target_halos['source_halo_id']
                  [idxB]), msg

    dc2['target_halo_x'] = 0.
    dc2['target_halo_y'] = 0.
    dc2['target_halo_z'] = 0.
    dc2['target_halo_vx'] = 0.
    dc2['target_halo_vy'] = 0.
    dc2['target_halo_vz'] = 0.

    dc2['target_halo_x'][idxA] = target_halos['fof_halo_center_x'][idxB]
    dc2['target_halo_y'][idxA] = target_halos['fof_halo_center_y'][idxB]
    dc2['target_halo_z'][idxA] = target_halos['fof_halo_center_z'][idxB]

    dc2['target_halo_vx'][idxA] = target_halos['fof_halo_mean_vx'][idxB]
    dc2['target_halo_vy'][idxA] = target_halos['fof_halo_mean_vy'][idxB]
    dc2['target_halo_vz'][idxA] = target_halos['fof_halo_mean_vz'][idxB]

    dc2['target_halo_mass'] = 0.
    dc2['target_halo_mass'][idxA] = target_halos['fof_halo_mass'][idxB]

    source_galaxy_keys = ('host_halo_mvir', 'upid', 'host_centric_x',
                          'host_centric_y', 'host_centric_z',
                          'host_centric_vx', 'host_centric_vy',
                          'host_centric_vz', 'obs_sm', 'obs_sfr',
                          'sfr_percentile_fixed_sm', 'rmag',
                          'sdss_petrosian_gr', 'sdss_petrosian_ri', 'size_kpc')
    for key in source_galaxy_keys:
        dc2[key] = umachine[key][galaxy_indices]

    x_init = dc2['target_halo_x'] + dc2['host_centric_x']
    vx_init = dc2['target_halo_vx'] + dc2['host_centric_vx']
    dc2['x'], dc2['vx'] = enforce_periodicity_of_box(x_init,
                                                     Lbox_target,
                                                     velocity=vx_init)

    y_init = dc2['target_halo_y'] + dc2['host_centric_y']
    vy_init = dc2['target_halo_vy'] + dc2['host_centric_vy']
    dc2['y'], dc2['vy'] = enforce_periodicity_of_box(y_init,
                                                     Lbox_target,
                                                     velocity=vy_init)

    z_init = dc2['target_halo_z'] + dc2['host_centric_z']
    vz_init = dc2['target_halo_vz'] + dc2['host_centric_vz']
    dc2['z'], dc2['vz'] = enforce_periodicity_of_box(z_init,
                                                     Lbox_target,
                                                     velocity=vz_init)
    return dc2
def load_orphan_subhalos():
    """
    """
    dirname = "/Users/aphearin/work/sims/bolplanck/orphan_catalog_z0"
    basename = "cross_matched_orphan_catalog.hdf5"

    halo_table = Table.read(os.path.join(dirname, basename), path='data')

    Lbox = 250.
    halo_table['x'] = enforce_periodicity_of_box(halo_table['x'], Lbox)
    halo_table['y'] = enforce_periodicity_of_box(halo_table['y'], Lbox)
    halo_table['z'] = enforce_periodicity_of_box(halo_table['z'], Lbox)

    halo_table['vmax_at_mpeak_percentile'] = np.load(
        os.path.join(dirname, 'vmax_percentile.npy'))
    halo_table['noisy_vmax_at_mpeak_percentile'] = noisy_percentile(
        halo_table['vmax_at_mpeak_percentile'], 0.5)

    halo_table['orphan_mass_loss_percentile'] = -1.
    halo_table['orphan_mass_loss_percentile'][halo_table['orphan']] = np.load(
            os.path.join(dirname, 'orphan_mass_loss_percentile.npy'))
    halo_table['orphan_vmax_at_mpeak_percentile'] = -1.
    halo_table['orphan_vmax_at_mpeak_percentile'][halo_table['orphan']] = np.load(
            os.path.join(dirname, 'orphan_vmax_at_mpeak_percentile.npy'))
    halo_table['orphan_vmax_loss_percentile'] = -1.
    halo_table['orphan_vmax_loss_percentile'][halo_table['orphan']] = np.load(
            os.path.join(dirname, 'orphan_vmax_loss_percentile.npy'))

    halo_table['orphan_fixed_mpeak_mhost_percentile'] = -1.
    halo_table['orphan_fixed_mpeak_mhost_percentile'][halo_table['orphan']] = np.load(
            os.path.join(dirname, 'orphan_fixed_mpeak_mhost_percentile.npy'))

    halo_table['zpeak'] = 1./halo_table['mpeak_scale']-1.

    halo_table['zpeak_no_splashback'] = 0.
    satmask = halo_table['upid'] != -1
    halo_table['zpeak_no_splashback'][satmask] = halo_table['zpeak'][satmask]

    rvir_peak_physical_unity_h = halo_mass_to_halo_radius(halo_table['mpeak'],
                                Planck15, halo_table['zpeak'], 'vir')
    rvir_peak_physical = rvir_peak_physical_unity_h/Planck15.h
    halo_table['rvir_zpeak'] = rvir_peak_physical*1000.

    rvir_peak_no_spl_physical_unity_h = halo_mass_to_halo_radius(halo_table['mpeak'],
                                Planck15, halo_table['zpeak_no_splashback'], 'vir')
    rvir_peak_no_spl_physical = rvir_peak_no_spl_physical_unity_h/Planck15.h
    halo_table['rvir_zpeak_no_splashback'] = rvir_peak_no_spl_physical*1000.

    halo_table['hostid'] = np.nan
    hostmask = halo_table['upid'] == -1
    halo_table['hostid'][hostmask] = halo_table['halo_id'][hostmask]
    halo_table['hostid'][~hostmask] = halo_table['upid'][~hostmask]

    idxA, idxB = crossmatch(halo_table['hostid'], halo_table['halo_id'])
    halo_table['host_mvir'] = np.nan
    halo_table['host_mvir'][idxA] = halo_table['mvir'][idxB]

    halo_table = halo_table[~np.isnan(halo_table['host_mvir'])]

    halo_table['frac_mpeak_remaining'] = halo_table['mvir']/halo_table['mpeak']
    halo_table['frac_vpeak_remaining'] = halo_table['vmax']/halo_table['vmax_at_mpeak']

    return halo_table