Beispiel #1
0
def _galCorr(cat, scale_factor, outputdir):
    'Helper function that uses the built in cat object'
    h = 0.7
    RBINS = np.logspace(-1, 1.25, 15)
    redshift = 1.0/scale_factor - 1.0

    print cat

    if outputdir[-1] != '/':
        outputdir+='/'

    #Note: Confusing name between cat and halocat. Consider changing.
    halocat = CachedHaloCatalog(simname = cat.simname, halo_finder = cat.halo_finder,version_name = cat.version_name, redshift = redshift)

    model = HodModelFactory(
            centrals_occupation=StepFuncCens(redshift=redshift),
            centrals_profile=TrivialPhaseSpace(redshift=redshift),
            satellites_occupation=StepFuncSats(redshift=redshift),
            satellites_profile=NFWPhaseSpace(redshift=redshift))

    model.populate_mock(halocat, Num_ptcl_requirement = 30)

    #Now, calculate with Halotools builtin
    #TODO include the fast version
    x, y, z = [model.mock.galaxy_table[c] for c in ['x','y','z'] ]
    pos = return_xyz_formatted_array(x,y,z)
    #TODO N procs
    xi_all = tpcf(pos*h, RBINS, period = model.mock.Lbox*h, num_threads =  cpu_count())

    np.savetxt(outputdir + 'xi_all_gal_%.3f_h.npy' %(scale_factor), xi_all)
Beispiel #2
0
    def __init__(self, config):
        super().__init__(config)
        self.cur_wp = np.zeros(11)

        if self['sim'] == "bolplanck":
            halocat = CachedHaloCatalog(simname='bolplanck')
        elif self['sim'] == "old":
            halocat = CachedHaloCatalog(
                fname='/home/lom31/Halo/hlist_1.00231.list.halotools_v0p1.hdf5',
                update_cached_fname=True)
            halocat.redshift = 0.
        elif self['sim'] == "smdpl":
            halocat = CachedHaloCatalog(
                fname=
                '/home/lom31/.astropy/cache/halotools/halo_catalogs/SMDPL/rockstar/2019-07-03-18-38-02-9731.dat.my_cosmosim_halos.hdf5',
                update_cached_fname=True)
            #halocat = CachedHaloCatalog(fname='/home/lom31/.astropy/cache/halotools/halo_catalogs/smdpl/rockstar/2019-07-03-18-38-02-9731.dat.my_cosmosim_halos.hdf5',update_cached_fname = True)
            halocat.redshift = 0.
        elif self['sim'] == "mdr1":
            halocat = CachedHaloCatalog(
                fname=
                '/home/lom31/.astropy/cache/halotools/halo_catalogs/multidark/rockstar/hlist_0.68215.list.halotools_v0p4.hdf5',
                update_cached_fname=True)

        if self['param'] == 'mvir':
            cens_occ_model = Zheng07Cens(threshold=-19)
            cens_prof_model = TrivialPhaseSpace()
            sats_occ_model = Zheng07Sats(modulate_with_cenocc=True,
                                         threshold=-19)
            sats_prof_model = NFWPhaseSpace()
        elif self['param'] == 'vmax':
            cens_occ_model = Zheng07Cens(prim_haloprop_key='halo_vmax',
                                         threshold=-19)
            cens_prof_model = TrivialPhaseSpace()
            sats_occ_model = Zheng07Sats(prim_haloprop_key='halo_vmax',
                                         threshold=-19,
                                         modulate_with_cenocc=True)
            sats_prof_model = NFWPhaseSpace()

        global model_instance

        model_instance = HodModelFactory(centrals_occupation=cens_occ_model,
                                         centrals_profile=cens_prof_model,
                                         satellites_occupation=sats_occ_model,
                                         satellites_profile=sats_prof_model)

        try:
            model_instance.mock.populate()
        except:
            model_instance.populate_mock(halocat)
Beispiel #3
0
    def __makemodel__(self):
        """
        Return the Zheng 07 HOD model.

        This model evaluates Eqs. 2 and 5 of Zheng et al. 2007
        """
        from halotools.empirical_models import HodModelFactory
        from halotools.empirical_models import Zheng07Sats, Zheng07Cens
        from halotools.empirical_models import NFWPhaseSpace, TrivialPhaseSpace

        model = {}

        # use concentration from halo table
        if 'halo_nfw_conc' in self._halos.halo_table.colnames:
            conc_mass_model = 'direct_from_halo_catalog'
        # use empirical prescription for c(M)
        else:
            conc_mass_model = 'dutton_maccio14'

        # occupation functions
        cenocc = Zheng07Cens(prim_haloprop_key=self.mass)
        satocc = Zheng07Sats(prim_haloprop_key=self.mass, modulate_with_cenocc=True, cenocc_model=cenocc)
        satocc._suppress_repeated_param_warning = True

        # add to model
        model['centrals_occupation'] = cenocc
        model['satellites_occupation'] = satocc

        # profile functions
        kws = {'cosmology':self.cosmo.to_astropy(), 'redshift':self.attrs['redshift'], 'mdef':self.attrs['mdef']}
        model['centrals_profile'] = TrivialPhaseSpace(**kws)
        model['satellites_profile'] = NFWPhaseSpace(conc_mass_model=conc_mass_model, **kws)

        return HodModelFactory(**model)
Beispiel #4
0
def decorated_hod_model():
    cen_occ_model = AssembiasZheng07Cens(prim_haloprop_key='halo_mvir',
                                         sec_haloprop_key='halo_nfw_conc')
    cen_prof_model = TrivialPhaseSpace()
    sat_occ_model = AssembiasZheng07Sats(prim_haloprop_key='halo_mvir',
                                         sec_haloprop_key='halo_nfw_conc')
    sat_prof_model = NFWPhaseSpace()
    return HodModelFactory(centrals_occupation=cen_occ_model,
                           centrals_profile=cen_prof_model,
                           satellites_occupation=sat_occ_model,
                           satellites_profile=sat_prof_model)
Beispiel #5
0
cens_occ_model = Zheng07Cens()
cens_prof_model = TrivialPhaseSpace()

#sats_occ_model =  Zheng07Sats(prim_haloprop_key = 'halo_vmax', modulate_with_cenocc=True)
sats_occ_model = Zheng07Sats(modulate_with_cenocc=True)
sats_prof_model = NFWPhaseSpace()

halocat = CachedHaloCatalog(
    fname=
    '/home/lom31/.astropy/cache/halotools/halo_catalogs/SMDPL/rockstar/2019-07-03-18-38-02-9731.dat.my_cosmosim_halos.hdf5',
    update_cached_fname=True)
halocat.redshift = 0.
pi_max = 60.
Lbox = 400.
model_instance = HodModelFactory(centrals_occupation=cens_occ_model,
                                 centrals_profile=cens_prof_model,
                                 satellites_occupation=sats_occ_model,
                                 satellites_profile=sats_prof_model)

try:
    model_instance.mock.populate()
except:
    model_instance.populate_mock(halocat)

alpha, logM0, logM1 = [1.16, 13.28 - 1.7, 13.28]
model_instance.param_dict['alpha'] = alpha
model_instance.param_dict['logM0'] = logM0
model_instance.param_dict['logM1'] = logM1

halo_table = halocat.halo_table
logMmin = np.logspace(11, 14, 100)
Beispiel #6
0
    def __init__(self, **kwargs):
        """
    Initialize a ABHodFitModel.
    """

        # first, set up appropriate priors on parameters
        if ('priors' in kwargs.keys()):
            self.set_prior(kwargs['priors'])
        else:
            self.set_prior(default_priors)

        # set up keys for the parameter names for plotting
        self.param_names = [
            'alpha', 'logM1', 'siglogM', 'logM0', 'logMmin', 'Acens', 'Asats'
        ]
        self.latex_param_names = [
            r'$\alpha$', r'$\log(M_1)$', r'$\sigma_{\log M}$', r'$\log(M_0)$',
            r'$\log(M_{\rm min})$', r'$\mathcal{A}_{\rm cens}$',
            r'$\mathcal{A}_{\rm sats}'
        ]

        # set up size parameters for any MCMC
        self.set_nwalkers(ndim=default_ndim, nwalkers=default_nwalkers)

        # if data is specified, load it into memory
        if 'rpcut' in kwargs.keys():
            self.rpcut = kwargs['rpcut']
        else:
            self.rpcut = default_rpcut

        if ('datafile' in kwargs.keys()):
            self.read_datafile(datafile=kwargs['datafile'])
        else:
            self.read_datafile(datafile=default_wp_datafile)

        if ('covarfile' in kwargs.keys()):
            self.read_covarfile(covarfile=kwargs['covarfile'])
        else:
            self.read_covarfile(covarfile=default_wp_covarfile)

        # if binfile is specified, load it into memory
        # these are Manodeep-style bins
        if ('binfile' in kwargs.keys()):
            self.binfile = kwargs['binfile']
        else:
            self.binfile = default_binfile

        # set up a default HOD Model
        if ('cen_occ_model' in kwargs.keys()):
            cen_occ_model = kwargs['cen_occ_model']
        else:
            cen_occ_model = AssembiasZheng07Cens(
                prim_haloprop_key='halo_mvir',
                sec_haloprop_key='halo_nfw_conc')

        if ('cen_prof_model' in kwargs.keys()):
            cen_prof_model = kwargs['cen_prof_model']
        else:
            cen_prof_model = TrivialPhaseSpace()

        if ('sat_occ_model' in kwargs.keys()):
            sat_occ_model = kwargs['sat_occ_model']
        else:
            sat_occ_model = AssembiasZheng07Sats(
                prim_haloprop_key='halo_mvir',
                sec_haloprop_key='halo_nfw_conc')

        if ('sat_prof_model' in kwargs.keys()):
            sat_prof_model = kwargs['sat_prof_model']
        else:
            sat_prof_model = NFWPhaseSpace()

        # Default HOD Model is Zheng07 with Heaviside Assembly Bias
        self.hod_model = HodModelFactory(centrals_occupation=cen_occ_model,
                                         centrals_profile=cen_prof_model,
                                         satellites_occupation=sat_occ_model,
                                         satellites_profile=sat_prof_model)

        # set pi_max for wp(rp) calculations
        self.pi_max = default_pi_max

        if ('simname' in kwargs.keys()):
            simname = kwargs['simname']
        else:
            simname = default_simname

        if ('halo_finder' in kwargs.keys()):
            halo_finder = kwargs['halo_finder']
        else:
            halo_finder = default_halofinder

        if ('redshift' in kwargs.keys()):
            redshift = kwargs['redshift']
        else:
            redshift = default_simredshift

        if ('version_name' in kwargs.keys()):
            version_name = kwargs['version_name']
        else:
            version_name = default_version_name

        # set default simulation halocatalog to work with
        self.halocatalog = CachedHaloCatalog(simname=simname,
                                             halo_finder=halo_finder,
                                             redshift=redshift,
                                             version_name=version_name)

        return None
Beispiel #7
0
class ABHodFitModel():
    """
  HOD with assembly bias Fit model class for wp(rp)
  """

    ###################################################################
    # Initialize an instance of the ABHodFitModel
    def __init__(self, **kwargs):
        """
    Initialize a ABHodFitModel.
    """

        # first, set up appropriate priors on parameters
        if ('priors' in kwargs.keys()):
            self.set_prior(kwargs['priors'])
        else:
            self.set_prior(default_priors)

        # set up keys for the parameter names for plotting
        self.param_names = [
            'alpha', 'logM1', 'siglogM', 'logM0', 'logMmin', 'Acens', 'Asats'
        ]
        self.latex_param_names = [
            r'$\alpha$', r'$\log(M_1)$', r'$\sigma_{\log M}$', r'$\log(M_0)$',
            r'$\log(M_{\rm min})$', r'$\mathcal{A}_{\rm cens}$',
            r'$\mathcal{A}_{\rm sats}'
        ]

        # set up size parameters for any MCMC
        self.set_nwalkers(ndim=default_ndim, nwalkers=default_nwalkers)

        # if data is specified, load it into memory
        if 'rpcut' in kwargs.keys():
            self.rpcut = kwargs['rpcut']
        else:
            self.rpcut = default_rpcut

        if ('datafile' in kwargs.keys()):
            self.read_datafile(datafile=kwargs['datafile'])
        else:
            self.read_datafile(datafile=default_wp_datafile)

        if ('covarfile' in kwargs.keys()):
            self.read_covarfile(covarfile=kwargs['covarfile'])
        else:
            self.read_covarfile(covarfile=default_wp_covarfile)

        # if binfile is specified, load it into memory
        # these are Manodeep-style bins
        if ('binfile' in kwargs.keys()):
            self.binfile = kwargs['binfile']
        else:
            self.binfile = default_binfile

        # set up a default HOD Model
        if ('cen_occ_model' in kwargs.keys()):
            cen_occ_model = kwargs['cen_occ_model']
        else:
            cen_occ_model = AssembiasZheng07Cens(
                prim_haloprop_key='halo_mvir',
                sec_haloprop_key='halo_nfw_conc')

        if ('cen_prof_model' in kwargs.keys()):
            cen_prof_model = kwargs['cen_prof_model']
        else:
            cen_prof_model = TrivialPhaseSpace()

        if ('sat_occ_model' in kwargs.keys()):
            sat_occ_model = kwargs['sat_occ_model']
        else:
            sat_occ_model = AssembiasZheng07Sats(
                prim_haloprop_key='halo_mvir',
                sec_haloprop_key='halo_nfw_conc')

        if ('sat_prof_model' in kwargs.keys()):
            sat_prof_model = kwargs['sat_prof_model']
        else:
            sat_prof_model = NFWPhaseSpace()

        # Default HOD Model is Zheng07 with Heaviside Assembly Bias
        self.hod_model = HodModelFactory(centrals_occupation=cen_occ_model,
                                         centrals_profile=cen_prof_model,
                                         satellites_occupation=sat_occ_model,
                                         satellites_profile=sat_prof_model)

        # set pi_max for wp(rp) calculations
        self.pi_max = default_pi_max

        if ('simname' in kwargs.keys()):
            simname = kwargs['simname']
        else:
            simname = default_simname

        if ('halo_finder' in kwargs.keys()):
            halo_finder = kwargs['halo_finder']
        else:
            halo_finder = default_halofinder

        if ('redshift' in kwargs.keys()):
            redshift = kwargs['redshift']
        else:
            redshift = default_simredshift

        if ('version_name' in kwargs.keys()):
            version_name = kwargs['version_name']
        else:
            version_name = default_version_name

        # set default simulation halocatalog to work with
        self.halocatalog = CachedHaloCatalog(simname=simname,
                                             halo_finder=halo_finder,
                                             redshift=redshift,
                                             version_name=version_name)

        return None
        ##############################################################################
        ##############################################################################

    # Set MCMC dimension and number of walkers
    def set_nwalkers(self, **kwargs):
        """
    Sets the number of MCMC dimensions and sets the number of walkers.

    Parameters
    ----------

    Takes keyword arguments ndim and nwalkers.
    """
        if ('ndim' in kwargs.keys()):
            self.ndim = kwargs['ndim']

        if ('nwalkers' in kwargs.keys()):
            self.nwalkers = kwargs['nwalkers']

        return None

    # Routine to set priors on model parameters
    def set_prior(self, prior_array):
        """
    Sets the model priors.

    Parameters
    -----------

    Takes keyword arguments r0min, r0max, gammamin, gammamax
    """

        self.priors = {}  # priors are stored in a dictionary
        if 'alpha' in prior_array.keys():
            self.priors['alpha'] = prior_array['alpha']

        if 'logM1' in prior_array.keys():
            self.priors['logM1'] = prior_array['logM1']

        if 'sigma_logM' in prior_array.keys():
            self.priors['sigma_logM'] = prior_array['sigma_logM']

        if 'logM0' in prior_array.keys():
            self.priors['logM0'] = prior_array['logM0']

        if 'logMmin' in prior_array.keys():
            self.priors['logMmin'] = prior_array['logMmin']

        if 'mean_occupation_centrals_assembias_param1' in prior_array.keys():
            self.priors[
                'mean_occupation_centrals_assembias_param1'] = prior_array[
                    'mean_occupation_centrals_assembias_param1']

        if 'mean_occupation_satellites_assembias_param1' in prior_array.keys():
            self.priors[
                'mean_occupation_satellites_assembias_param1'] = prior_array[
                    'mean_occupation_satellites_assembias_param1']

        return None

    # Read in the data
    def read_datafile(self, **kwargs):
        """
    Read in the data. This can assume the input_data_file attribute or it 
    can accept a new data file as a keyword argument, datafile.
    """
        if ('datafile' in kwargs.keys()):
            self.datafile = kwargs['datafile']

        col1, col2, col3 = np.loadtxt(self.datafile, unpack=True)
        self.Number_gals = col1[0]
        self.ngal = col2[0]
        self.ngalerr = col3[0]

        self.rp = col1[1:]
        self.wp = col2[1:]
        self.wperr = col3[1:]

        # Use data only out to the bin at rp=rpcut
        ikeep = np.where(self.rp <= self.rpcut)
        self.rp = self.rp[ikeep]
        self.wp = self.wp[ikeep]
        self.wpT = self.wp.T
        self.wperr = self.wperr[ikeep]

        # set the number of bins
        self.nrpbins = self.rp.size

        return None

    # Read in the data covariances
    def read_covarfile(self, **kwargs):
        """
    Read in the data covariances
    """

        if ('covarfile' in kwargs.keys()):
            self.covarfile = kwargs['covarfile']
        else:
            self.covarfile = default_wp_covarfile

        self.covar = np.loadtxt(self.covarfile, unpack=True)
        self.cov_inv = np.linalg.inv(self.covar)

    # A routine to give wp(rp) computed via a halo model.
    def wp_hod(self, hod_parameters):
        """
    An HOD model for wp(rp) computed by direct simulation 
    population.
    hod_parameters[0] : alpha
    hod_parameters[1] : logM1
    hod_parameters[2] : sigma_logM
    hod_parameters[3] : logM0
    hod_parameters[4] : logMmin
    hod_parameters[5] : Acen
    hod_parameters[6] : Asat
    """

        # The first step is to set the param_dict of the hod_model.
        self.hod_model.param_dict['alpha'] = hod_parameters[0]
        self.hod_model.param_dict['logM1'] = hod_parameters[1]
        self.hod_model.param_dict['sigma_logM'] = hod_parameters[2]
        self.hod_model.param_dict['logM0'] = hod_parameters[3]
        self.hod_model.param_dict['logMmin'] = hod_parameters[4]
        self.hod_model.param_dict[
            'mean_occupation_centrals_assembias_param1'] = hod_parameters[5]
        self.hod_model.param_dict[
            'mean_occupation_satellites_assembias_param1'] = hod_parameters[6]

        # Populate a mock galaxy catalog
        #self.hod_model.populate_mock()
        try:
            self.hod_model.mock.populate()
        except:
            self.hod_model.populate_mock(self.halocatalog)

        # Instruct wp(rp) routine to compute autocorrelation
        autocorr = 1
        # Number of threads
        nthreads = 4

        # use the z-direction as line-of-sight and add RSD
        z_distorted = self.hod_model.mock.galaxy_table[
            'z'] + self.hod_model.mock.galaxy_table['vz'] / 100.0

        # enforce periodicity of the box

        self.hod_model.mock.galaxy_table[
            'zdist'] = z_distorted % self.hod_model.mock.Lbox[0]

        # Return projected correlation function computed using
        # Manodeep Simha's optimized C code.
        cpout = np.array(
            _countpairs.countpairs_wp(
                self.hod_model.mock.Lbox[0], self.pi_max, nthreads,
                self.binfile,
                self.hod_model.mock.galaxy_table['x'].astype('float32'),
                self.hod_model.mock.galaxy_table['y'].astype('float32'),
                self.hod_model.mock.galaxy_table['zdist'].astype('float32')))
        return np.array(cpout[0])[:, 3]

# A routine to compute wp(rp) in a power-law model xi = (r/r0)^-gamma.

    def wp_powerlaw_model(self, rsep, r0, gamma):
        """
    Power law model for wp(rp) assuming that xi = (r/r0)^-gamma.
    """
        if rsep.any < 0.0:
            return np.inf
        return rsep * (rsep / r0)**(-gamma) * gammafn(0.5) * gammafn(
            (gamma - 1.0) / 2.0) / gammafn(gamma / 2.0)

    # A routine to compute the likelihood of a power-law wp(rp) given data.
    def lnlike(self, theta):
        """
   	Log likelihood of a power-law model.

   	Parameters
   	------------
   	theta : (alpha,logM1,sigma_logM,logM0,logMmin)
   	rp : numpy array containing separations
   	wp : numpy array containing projected correlation functions
   	wperr : numpy array containing errors on measured wp
    """

        # log likelihood from clustering
        wpmodel = self.wp_hod(theta)
        #print 'wpmodel shape = ',np.shape(wpmodel)
        #print 'wperr shape = ',np.shape(self.wperr)
        #print 'wp shape = ',np.shape(self.wp)
        #print 'rp shape = ',np.shape(self.rp)
        wp_dev = (wpmodel - self.wp)
        wplike = -0.5 * np.dot(np.dot(wp_dev, self.cov_inv), wp_dev)

        # log likelihood from number density
        number_gals = len(self.hod_model.mock.galaxy_table)
        ngal = number_gals / (self.hod_model.mock.Lbox[0]**3)
        ng_theory_error = ngal / np.sqrt(number_gals)
        nglike = -0.5 * ((ngal - self.ngal)**2 /
                         (self.ngalerr**2 + ng_theory_error**2))

        return wplike + nglike

# A prior function on the two parameters in the list theta

    def lnprior(self, theta):
        """
    Prior function

		Parameters
		----------
		theta : [alpha,logM1,sigma_logM,logM0,logMmin,Acen,Asat]
    Priors are so-called hard priors specified in self.priors.
    """
        alpha, logM1, sigma_logM, logM0, logMmin, Acen, Asat = theta
        if alpha < self.priors['alpha'][0]:
            return -np.inf
        if alpha > self.priors['alpha'][1]:
            return -np.inf

        if logM1 < self.priors['logM1'][0]:
            return -np.inf
        if logM1 > self.priors['logM1'][1]:
            return -np.inf

        if sigma_logM < self.priors['sigma_logM'][0]:
            return -np.inf
        if sigma_logM > self.priors['sigma_logM'][1]:
            return -np.inf

        if logM0 < self.priors['logM0'][0]:
            return -np.inf
        if logM0 > self.priors['logM0'][1]:
            return -np.inf

        if logMmin < self.priors['logMmin'][0]:
            return -np.inf
        if logMmin > self.priors['logMmin'][1]:
            return -np.inf

        if Acen < self.priors['mean_occupation_centrals_assembias_param1'][0]:
            return -np.inf
        if Acen > self.priors['mean_occupation_centrals_assembias_param1'][1]:
            return -np.inf

        if Asat < self.priors['mean_occupation_satellites_assembias_param1'][0]:
            return -np.inf
        if Asat > self.priors['mean_occupation_satellites_assembias_param1'][1]:
            return -np.inf

        return 0.0

    # The probability function including priors and likelihood
    def lnprob(self, theta):
        """
    Probability function to sample in an MCMC.
    """
        lp = self.lnprior(theta)
        if not np.isfinite(lp):
            return -np.inf
        return lp + self.lnlike(theta)

    # Set the starting position of an MCMC.
    def set_start_position(self, theta_start=default_start):
        """
    Set the starting position for the MCMC.
    """

        self.position = np.zeros([self.nwalkers, self.ndim])

        for iparam in range(self.ndim):
            self.position[:, iparam] = theta_start[
                iparam] + 0.05 * np.random.randn(self.nwalkers)

        return None

    # Fit the data with the hod model model
    def mcmcfit(self, theta_start, **kwargs):
        """
    Peform an MCMC fit to the wp data using the power-law model.
    """

        if ('samples' in kwargs.keys()):
            self.nsamples = kwargs['samples']
        else:
            self.nsamples = 100

        if ('nwalkers' in kwargs.keys()):
            self.set_nwalkers(nwalkers=kwargs['nwalkers'])

        self.wpsampler = emcee.EnsembleSampler(self.nwalkers, self.ndim,
                                               self.lnprob)

        self.set_start_position(theta_start)

        if ('burnin' in kwargs.keys()):
            self.nburnin = kwargs['burnin']
            self.position, self.prob, self.state = self.wpsampler.run_mcmc(
                self.position, self.nburnin)
            self.wpsampler.reset()
        else:
            self.nburnin = 0

        self.wpsampler.run_mcmc(self.position, self.nsamples)

        self.mcmcsamples = self.wpsampler.chain[:, :, :].reshape(
            (-1, self.ndim))
        self.lnprobability = self.wpsampler.lnprobability.reshape(-1, 1)

        self.compute_parameter_constraints()

        return None

    # given a set of MCMC samples in self.mcmcsamples, compute the 1-D parameter constraints.
    def compute_parameter_constraints(self):
        """
    Computes the 1D marginalized parameter constraints from 
    self.mcmcsamples.
    """
        self.alpha_mcmc, self.logM1_mcmc, self.sigma_logM_mcmc, self.logM0_mcmc, self.logMmin_mcmc, self.Acen_mcmc, self.Asat_mcmc = map(
            lambda v: (v[1], v[2] - v[1], v[1] - v[0]),
            zip(*np.percentile(self.mcmcsamples, [16, 50, 84], axis=0)))

        self.alpha_1side, self.logM1_1side, self.sigma_logM_1side, self.logM0_1side, self.logMmin_1side, self.Acen_1side, self.Asat_1side = map(
            lambda v: (v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]),
            zip(*np.percentile(
                self.mcmcsamples, [1, 5, 10, 16, 84, 90, 95, 99], axis=0)))

        return None

    # save chains to file
    def save_chains(self, **kwargs):
        """
    Save the chain to an ascii file.
    """

        if ('filename' in kwargs.keys()):
            fname = kwargs['filename']
        else:
            fname = self.datafile
            if fname.endswith('.dat'):
                fname = fname[:-4]
            fname = fname + '_abfit.chain'

        out_data = np.hstack((self.mcmcsamples, self.lnprobability))
        np.savetxt(fname, out_data, delimiter='  ')

        return None

    # load a chain from an existing chain file
    def load_chains(self, chainfile_names):
        """
    Load a pre-existing chain into memory from a file.
    """

        for chainfile in chainfile_names:

            read_data = np.loadtxt(chainfile, unpack=True).T
            samples = read_data[:, 0:self.ndim]
            lnprob = read_data[:, self.ndim]
            samples = samples.reshape(-1, self.ndim)
            lnprob = lnprob.reshape(-1, 1)

            if (hasattr(self, 'mcmcsamples')):
                self.mcmcsamples = np.concatenate((self.mcmcsamples, samples),
                                                  axis=0)
                self.lnprobability = np.concatenate(
                    (self.lnprobability, lnprob), axis=0)
            else:
                self.mcmcsamples = samples
                self.lnprobability = lnprob


#    self.mcmcsamples=self.mcmcsamples.reshape(-1,self.ndim)
#    self.lnprobability=self.lnprobability.reshape(-1,1)

        return None

    # plot the data
    def plot_data(self):
        """
    plot the data only
    """
        fig1 = plt.figure()
        plt.loglog(self.rp, self.wp, 'sk')
        plt.errorbar(self.rp, self.wp, yerr=self.wperr, fmt='sk', ecolor='k')
        plt.xlabel(r'$r_{\rm p}$')
        plt.ylabel(r'$w_{\rm p}(r_{\rm p})$')
        fig1.savefig('wpdata.png')

        return None

    # plot the mcmc run
    def plot_parameter_run(self):
        """
    plot the mcmc samples.
    """
        for idim in range(self.ndim):
            fig = plt.figure()
            plt.plot(range(len(self.mcmcsamples[:, idim])),
                     self.mcmcsamples[:, idim], 'k')
            plt.xlabel('sample number')
            plt.ylabel(self.latex_param_names[idim])
            filename = self.datafile
            if filename.endswith('.dat'):
                filename = filename[:-4]
            plabel = self.param_names[idim].strip('$')
            plabel = plabel.strip('\\')
            filename = filename + '_' + plabel + '_' + 'chain.png'
            fig.savefig(filename)
            del fig

    # plot fitting results
    def plot_chain_samples(self, **kwargs):
        """
    Plots samples from the mcmc chain alongside the data.
    """

        lnp_max = np.max(self.lnprobability)
        if ('deltachi2' in kwargs.keys()):
            lnp_threshold = lnp_max - kwargs['deltachi2'] / 2.0
        else:
            lnp_threshold = lnp_max - 0.5

        if ('samples' in kwargs.keys()):
            numsamples = kwargs['samples']
        else:
            numsamples = 50

        fig = plt.figure()

        #print ' + Beginning sample selection.'
        igood = np.where((self.lnprobability.reshape(-1) > lnp_threshold))
        #print 'numsamples = ',numsamples
        #print 'Length(igood) = ',len(igood[0])
        #print 'Shape(igood) = ',np.shape(igood)

        # reduce the number of samples if there are very few
        if len(igood[0]) < 1:
            print ' > Too few samples that satisfy criterion, n = ', len(
                igood[0])
            return None
        elif len(igood[0]) < numsamples:
            numsamples = len(igood[0]) - 1

        good_models = self.mcmcsamples[igood]

        #print 'igood is ',igood
        #print 'good_models = ',good_models
        #print 'numsamples = ',numsamples

        irandoms = np.random.randint(len(good_models), size=numsamples)
        #print 'len(irandoms) = ',len(irandoms)
        #print 'irandoms = ',irandoms

        for parameters in good_models[irandoms, :]:
            #print 'Within for loop'
            #print 'Parameters are',parameters
            plt.loglog(self.rp, self.wp_hod(parameters), color='k', alpha=0.07)

        plt.xlim(0.92 * np.min(self.rp), 1.05 * np.max(self.rp))

        plt.xticks(size=15)
        plt.yticks(size=15)
        plt.xlabel(r'$r_{\rm p}$ [$h^{-1}$Mpc]', fontsize=20)
        plt.ylabel(r'$w_{\rm p}(r_{\rm p})$ [$h^{-1}$Mpc]', fontsize=20)

        #plt.loglog(self.rp,self.wp,'sk')
        plt.errorbar(self.rp,
                     self.wp,
                     yerr=self.wperr,
                     fmt='s',
                     color='firebrick',
                     ecolor='firebrick')

        filename = self.datafile
        if filename.endswith('.dat'):
            filename = filename[:-4]
        filename = filename + '_chainsamples.pdf'
        fig.savefig(filename, format='pdf', bbox_inches='tight')
        Zheng07Sats.__init__(self, threshold = -21)

        HeavisideAssembias.__init__(self,
            method_name_to_decorate = 'mean_occupation',
            lower_assembias_bound = 0.,
            upper_assembias_bound = np.inf,
            **kwargs)

cens_occ_model =  AssembiasZheng07Cens(threshold = -21)
cens_prof_model = TrivialPhaseSpace()
sats_occ_model =  AssembiasZheng07Sats(threshold = -21)
sats_prof_model = NFWPhaseSpace()

model= HodModelFactory(
        centrals_occupation = cens_occ_model,
        centrals_profile = cens_prof_model,
        satellites_occupation = sats_occ_model,
        satellites_profile = sats_prof_model)

print model.param_dict
baseline_model = PrebuiltHodModelFactory("Zheng07" , threshold = -21)


############### Setting the model parameters to those of Guo 15#######
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'] = 1.0
baseline_model.param_dict['logM0'] =  12.59
Beispiel #9
0
    def tabulate(cls,
                 halocat,
                 tpcf,
                 *tpcf_args,
                 mode='auto',
                 Num_ptcl_requirement=sim_defaults.Num_ptcl_requirement,
                 prim_haloprop_key=model_defaults.prim_haloprop_key,
                 prim_haloprop_bins=100,
                 sec_haloprop_key=model_defaults.sec_haloprop_key,
                 sec_haloprop_percentile_bins=None,
                 sats_per_prim_haloprop=3e-12,
                 downsample=1.0,
                 verbose=False,
                 redshift_space_distortions=True,
                 cens_prof_model=None,
                 sats_prof_model=None,
                 project_xyz=False,
                 cosmology_obs=None,
                 num_threads=1,
                 **tpcf_kwargs):
        """
        Tabulates correlation functions for halos such that galaxy correlation
        functions can be calculated rapidly.

        Parameters
        ----------
        halocat : object
            Either an instance of `halotools.sim_manager.CachedHaloCatalog` or
            `halotools.sim_manager.UserSuppliedHaloCatalog`. This halo catalog
            is used to tabubulate correlation functions.

        tpcf : function
            The halotools correlation function for which values are tabulated.
            Positional arguments should be passed after this function.
            Additional keyword arguments for the correlation function are also
            passed through this function.

        *tpcf_args : tuple, optional
            Positional arguments passed to the ``tpcf`` function.

        mode : string, optional
            String describing whether an auto- ('auto') or a cross-correlation
            ('cross') function is going to be tabulated.

        Num_ptcl_requirement : int, optional
            Requirement on the number of dark matter particles in the halo
            catalog. The column defined by the ``prim_haloprop_key`` string
            will have a cut placed on it: all halos with
            halocat.halo_table[prim_haloprop_key] <
            Num_ptcl_requirement*halocat.particle_mass will be thrown out
            immediately after reading the original halo catalog in memory.
            Default value is set in
            `~halotools.sim_defaults.Num_ptcl_requirement`.

        prim_haloprop_key : string, optional
            String giving the column name of the primary halo property
            governing the occupation statistics of gal_type galaxies. Default
            value is specified in the model_defaults module.

        prim_haloprop_bins : int or list, optional
            Integer determining how many (logarithmic) bins in primary halo
            property will be used. If a list or numpy array is provided, these
            will be used as bins directly.

        sec_haloprop_key : string, optional
            String giving the column name of the secondary halo property
            governing the assembly bias. Must be a key in the table passed to
            the methods of `HeavisideAssembiasComponent`. Default value is
            specified in the `~halotools.empirical_models.model_defaults`
            module.

        sec_haloprop_percentile_bins : int, float or None, optional
            If an integer, it determines how many evenly spaced bins in the
            secondary halo property percentiles are going to be used. If a
            float between 0 and 1, it determines the split. If None is
            provided, no binning is applied.

        sats_per_prim_haloprop : float, optional
            Float determing how many satellites sample each halo. For each
            halo, the number is drawn from a Poisson distribution with an
            expectation value of ``sats_per_prim_haloprop`` times the primary
            halo property.

        downsample : float or function, optional
            Fraction between 0 and 1 used to downsample the total sample used
            to tabulate correlation functions. Values below unity can be used
            to reduce the computation time. It should not result in biases but
            the resulting correlation functions will be less accurate. If
            float, the same value is applied to all halos. If function, it
            should return the fraction is a function of the primary halo
            property.

        verbose : boolean, optional
            Boolean determing whether the progress should be displayed.

        redshift_space_distortions : boolean, optional
            Boolean determining whether redshift space distortions should be
            applied to halos/galaxies.

        cens_prof_model : object, optional
            Instance of `halotools.empirical_models.MonteCarloGalProf` that
            determines the phase space coordinates of centrals. If none is
            provided, `halotools.empirical_models.TrivialPhaseSpace` will be
            used.

        sats_prof_model : object, optional
            Instance of `halotools.empirical_models.MonteCarloGalProf` that
            determines the phase space coordinates of satellites. If none is
            provided, `halotools.empirical_models.NFWPhaseSpace` will be used.

        project_xyz : bool, optional
            If True, the coordinates will be projected along all three spatial
            axes. By default, only the projection onto the z-axis is used.

        cosmology_obs : object, optional
            Instance of an astropy `~astropy.cosmology`. This can be used to
            correct coordinates in the simulation for the Alcock-Paczynski (AP)
            effect, i.e. a mismatch between the cosmology of the model
            (simulation) and the cosmology used to interpret observations. Note
            that the cosmology of the simulation is part of the halocat object.
            If None, no correction for the AP effect is applied. Also, a
            correction for the AP effect is only applied for auto-correlation
            functions.

        num_threads : int, optional
            How many threads to use for the tabulation.

        **tpcf_kwargs : dict, optional
                Keyword arguments passed to the ``tpcf`` function.

        Returns
        -------
        halotab : TabCorr
            Object containing all necessary information to calculate
            correlation functions for arbitrary galaxy models.
        """

        if 'period' in tpcf_kwargs:
            print('Warning: TabCorr will pass the keyword argument "period" ' +
                  'to {} based on the Lbox argument of'.format(tpcf.__name__) +
                  ' the halo catalog. The value you provided will be ignored.')
            del tpcf_kwargs['period']

        halotab = cls()

        if cosmology_obs is not None and mode == 'auto':
            rp_stretch = (
                (cosmology_obs.comoving_distance(halocat.redshift) *
                 cosmology_obs.H0) /
                (halocat.cosmology.comoving_distance(halocat.redshift) *
                 halocat.cosmology.H0))
            pi_stretch = (halocat.cosmology.efunc(halocat.redshift) /
                          cosmology_obs.efunc(halocat.redshift))
            lbox_stretch = np.array([rp_stretch, rp_stretch, pi_stretch])
        else:
            lbox_stretch = np.ones(3)

        # First, we tabulate the halo number densities.
        halos = halocat.halo_table
        halos = halos[halos['halo_pid'] == -1]
        halos = halos[halos[prim_haloprop_key] >=
                      (Num_ptcl_requirement + 0.5) * halocat.particle_mass]

        if isinstance(prim_haloprop_bins, int):
            prim_haloprop_bins = np.linspace(
                np.log10(np.amin(halos[prim_haloprop_key])) - 1e-3,
                np.log10(np.amax(halos[prim_haloprop_key])) + 1e-3,
                prim_haloprop_bins + 1)
        elif isinstance(prim_haloprop_bins, (list, np.ndarray)):
            pass
        else:
            raise ValueError('prim_haloprop_bins must be an int, list or ' +
                             'numpy array.')

        if sec_haloprop_percentile_bins is None:
            sec_haloprop_percentile_bins = np.array([-1e-3, 1 + 1e-3])
        elif isinstance(sec_haloprop_percentile_bins, float):
            if not (0 < sec_haloprop_percentile_bins
                    and sec_haloprop_percentile_bins < 1):
                raise ValueError('sec_haloprop_percentile_bins must be ' +
                                 'between 0 and 1.')
            sec_haloprop_percentile_bins = np.array(
                [-1e-3, sec_haloprop_percentile_bins, 1 + 1e-3])
        elif isinstance(sec_haloprop_percentile_bins, int):
            sec_haloprop_percentile_bins = np.linspace(
                -1e-3, 1 + 1e-3, sec_haloprop_percentile_bins + 1)
        else:
            raise ValueError('sec_haloprop_percentile_bins must be an int, ' +
                             'float, list or numpy array.')

        halos[sec_haloprop_key +
              '_percentile'] = (compute_conditional_percentiles(
                  table=halos,
                  prim_haloprop_key=prim_haloprop_key,
                  sec_haloprop_key=sec_haloprop_key))

        halotab.gal_type = Table()

        n_h, prim_haloprop_bins, sec_haloprop_percentile_bins = (
            np.histogram2d(
                np.log10(halos[prim_haloprop_key]),
                halos[sec_haloprop_key + '_percentile'],
                bins=[prim_haloprop_bins, sec_haloprop_percentile_bins]))
        halotab.gal_type['n_h'] = n_h.ravel(order='F')

        grid = np.meshgrid(prim_haloprop_bins, sec_haloprop_percentile_bins)
        halotab.gal_type['log_prim_haloprop_min'] = grid[0][:-1, :-1].ravel()
        halotab.gal_type['log_prim_haloprop_max'] = grid[0][:-1, 1:].ravel()
        halotab.gal_type['sec_haloprop_percentile_min'] = (
            grid[1][:-1, :-1].ravel())
        halotab.gal_type['sec_haloprop_percentile_max'] = (
            grid[1][1:, :-1].ravel())

        halotab.gal_type = vstack([halotab.gal_type, halotab.gal_type])
        halotab.gal_type['gal_type'] = np.concatenate(
            (np.repeat('centrals'.encode('utf8'),
                       len(halotab.gal_type) // 2),
             np.repeat('satellites'.encode('utf8'),
                       len(halotab.gal_type) // 2)))
        halotab.gal_type['prim_haloprop'] = 10**(
            0.5 * (halotab.gal_type['log_prim_haloprop_min'] +
                   halotab.gal_type['log_prim_haloprop_max']))
        halotab.gal_type['sec_haloprop_percentile'] = (
            0.5 * (halotab.gal_type['sec_haloprop_percentile_min'] +
                   halotab.gal_type['sec_haloprop_percentile_max']))

        # Now, we tabulate the correlation functions.
        cens_occ_model = Zheng07Cens(prim_haloprop_key=prim_haloprop_key)
        if cens_prof_model is None:
            cens_prof_model = TrivialPhaseSpace(redshift=halocat.redshift)
        sats_occ_model = Zheng07Sats(prim_haloprop_key=prim_haloprop_key)
        if sats_prof_model is None:
            sats_prof_model = NFWPhaseSpace(redshift=halocat.redshift)

        model = HodModelFactory(centrals_occupation=cens_occ_model,
                                centrals_profile=cens_prof_model,
                                satellites_occupation=sats_occ_model,
                                satellites_profile=sats_prof_model)

        model.param_dict['logMmin'] = 0
        model.param_dict['sigma_logM'] = 0.1
        model.param_dict['alpha'] = 1.0
        model.param_dict['logM0'] = 0
        model.param_dict['logM1'] = -np.log10(sats_per_prim_haloprop)
        model.populate_mock(halocat, Num_ptcl_requirement=Num_ptcl_requirement)

        gals = model.mock.galaxy_table
        idx_gals, idx_halos = crossmatch(gals['halo_id'], halos['halo_id'])
        assert np.all(gals['halo_id'][idx_gals] == halos['halo_id'][idx_halos])
        gals[sec_haloprop_key + '_percentile'] = np.zeros(len(gals))
        gals[sec_haloprop_key +
             '_percentile'][idx_gals] = (halos[sec_haloprop_key +
                                               '_percentile'][idx_halos])

        if verbose:
            print("Number of tracer particles: {0}".format(len(gals)))

        for xyz in ['xyz', 'yzx', 'zxy']:

            if verbose and project_xyz:
                print("Projecting onto {0}-axis...".format(xyz[2]))

            pos_all = (return_xyz_formatted_array(
                x=gals[xyz[0]],
                y=gals[xyz[1]],
                z=gals[xyz[2]],
                velocity=gals['v' +
                              xyz[2]] if redshift_space_distortions else 0,
                velocity_distortion_dimension='z',
                period=halocat.Lbox,
                redshift=halocat.redshift,
                cosmology=halocat.cosmology) * lbox_stretch)

            period = halocat.Lbox * lbox_stretch

            # Get a list of the positions of each sub-population.
            i_prim = np.digitize(np.log10(gals[prim_haloprop_key]),
                                 bins=prim_haloprop_bins,
                                 right=False) - 1
            mask = (i_prim < 0) | (i_prim >= len(prim_haloprop_bins))
            i_sec = np.digitize(gals[sec_haloprop_key + '_percentile'],
                                bins=sec_haloprop_percentile_bins,
                                right=False) - 1
            i_type = np.where(gals['gal_type'] == 'centrals', 0, 1)

            # Throw out those that don't fall into any bin.
            pos_all = pos_all[~mask]

            i = (i_prim + i_sec * (len(prim_haloprop_bins) - 1) + i_type *
                 ((len(prim_haloprop_bins) - 1) *
                  (len(sec_haloprop_percentile_bins) - 1)))

            pos_all = pos_all[np.argsort(i)]
            counts = np.bincount(i, minlength=len(halotab.gal_type))

            assert len(counts) == len(halotab.gal_type)

            pos_bin = []
            for i in range(len(halotab.gal_type)):

                pos = pos_all[np.sum(counts[:i]):np.sum(counts[:i + 1]), :]
                if halotab.gal_type['gal_type'][i] == 'centrals':
                    # Make sure the number of halos are consistent.
                    try:
                        assert len(pos) == int(halotab.gal_type['n_h'][i])
                    except AssertionError:
                        raise RuntimeError('There was an internal error in ' +
                                           'TabCorr. If possible, please ' +
                                           'report this bug in the TabCorr ' +
                                           'GitHub repository.')
                else:
                    if len(pos) == 0 and halotab.gal_type['n_h'][i] != 0:
                        raise RuntimeError(
                            'There was at least one bin without satellite ' +
                            'tracers. Increase sats_per_prim_haloprop.')

                if len(pos) > 0:

                    if isinstance(downsample, float):
                        use = np.random.random(len(pos)) < downsample
                    else:
                        use = (np.random.random(len(pos)) < downsample(
                            halotab.gal_type['prim_haloprop'][i]))

                    # If the down-sampling reduced the number of tracers to at
                    # or below one, force at least 2 tracers to not bias the
                    # clustering estimates.
                    if np.sum(use) <= 1 and len(pos) > 1:
                        use = np.zeros(len(pos), dtype=bool)
                        use[np.random.choice(len(pos), size=2)] = True

                    pos = pos[use]

                pos_bin.append(pos)

            if mode == 'auto':
                combinations = itertools.combinations_with_replacement(
                    range(len(halotab.gal_type)), 2)
            else:
                combinations = range(len(halotab.gal_type))

            if xyz == 'xyz':
                tpcf_matrix, tpcf_shape = compute_tpcf_matrix(
                    mode,
                    pos_bin,
                    tpcf,
                    period,
                    tpcf_args,
                    tpcf_kwargs,
                    combinations,
                    num_threads=num_threads,
                    verbose=verbose)

            if not project_xyz or mode == 'cross':
                break
            elif xyz != 'xyz':
                tpcf_matrix += compute_tpcf_matrix(mode,
                                                   pos_bin,
                                                   tpcf,
                                                   period,
                                                   tpcf_args,
                                                   tpcf_kwargs,
                                                   combinations,
                                                   num_threads=num_threads,
                                                   verbose=verbose)[0]

        if project_xyz and mode == 'auto':
            tpcf_matrix /= 3.0

        if mode == 'auto':
            tpcf_matrix_flat = []
            for i in range(tpcf_matrix.shape[0]):
                tpcf_matrix_flat.append(
                    symmetric_matrix_to_array(tpcf_matrix[i]))
            tpcf_matrix = np.array(tpcf_matrix_flat)

        # Remove entries that don't have any halos.
        use = halotab.gal_type['n_h'] != 0
        halotab.gal_type = halotab.gal_type[use]
        if mode == 'auto':
            use = symmetric_matrix_to_array(np.outer(use, use))
        tpcf_matrix = tpcf_matrix[:, use]

        halotab.gal_type['n_h'] /= np.prod(halocat.Lbox * lbox_stretch)

        halotab.attrs = {}
        halotab.attrs['tpcf'] = tpcf.__name__
        halotab.attrs['mode'] = mode
        halotab.attrs['simname'] = halocat.simname
        halotab.attrs['redshift'] = halocat.redshift
        halotab.attrs['Num_ptcl_requirement'] = Num_ptcl_requirement
        halotab.attrs['prim_haloprop_key'] = prim_haloprop_key
        halotab.attrs['sec_haloprop_key'] = sec_haloprop_key

        halotab.tpcf_args = tpcf_args
        halotab.tpcf_kwargs = tpcf_kwargs
        halotab.tpcf_shape = tpcf_shape
        halotab.tpcf_matrix = tpcf_matrix

        halotab.init = True

        return halotab
Beispiel #10
0
    def tabulate(cls,
                 halocat,
                 tpcf,
                 *tpcf_args,
                 mode='auto',
                 Num_ptcl_requirement=sim_defaults.Num_ptcl_requirement,
                 cosmology=sim_defaults.default_cosmology,
                 prim_haloprop_key=model_defaults.prim_haloprop_key,
                 prim_haloprop_bins=100,
                 sec_haloprop_key=model_defaults.sec_haloprop_key,
                 sec_haloprop_percentile_bins=None,
                 sats_per_prim_haloprop=3e-12,
                 downsample=1.0,
                 verbose=False,
                 redshift_space_distortions=True,
                 cens_prof_model=None,
                 sats_prof_model=None,
                 project_xyz=False,
                 cosmology_ref=None,
                 comm=None,
                 **tpcf_kwargs):
        """
        Tabulates correlation functions for halos such that galaxy correlation
        functions can be calculated rapidly.

        Parameters
        ----------
        halocat : object
            Either an instance of `halotools.sim_manager.CachedHaloCatalog` or
            `halotools.sim_manager.UserSuppliedHaloCatalog`. This halo catalog
            is used to tabubulate correlation functions.

        tpcf : function
            The halotools correlation function for which values are tabulated.
            Positional arguments should be passed after this function.
            Additional keyword arguments for the correlation function are also
            passed through this function.

        *tpcf_args : tuple, optional
            Positional arguments passed to the ``tpcf`` function.

        mode : string, optional
            String describing whether an auto- ('auto') or a cross-correlation
            ('cross') function is going to be tabulated.

        Num_ptcl_requirement : int, optional
            Requirement on the number of dark matter particles in the halo
            catalog. The column defined by the ``prim_haloprop_key`` string
            will have a cut placed on it: all halos with
            halocat.halo_table[prim_haloprop_key] <
            Num_ptcl_requirement*halocat.particle_mass will be thrown out
            immediately after reading the original halo catalog in memory.
            Default value is set in
            `~halotools.sim_defaults.Num_ptcl_requirement`.

        cosmology : object, optional
            Instance of an astropy `~astropy.cosmology`. Default cosmology is
            set in `~halotools.sim_manager.sim_defaults`. This might be used to
            calculate phase-space distributions and redshift space distortions.

        prim_haloprop_key : string, optional
            String giving the column name of the primary halo property
            governing the occupation statistics of gal_type galaxies. Default
            value is specified in the model_defaults module.

        prim_haloprop_bins : int or list, optional
            Integer determining how many (logarithmic) bins in primary halo
            property will be used. If a list or numpy array is provided, these
            will be used as bins directly.

        sec_haloprop_key : string, optional
            String giving the column name of the secondary halo property
            governing the assembly bias. Must be a key in the table passed to
            the methods of `HeavisideAssembiasComponent`. Default value is
            specified in the `~halotools.empirical_models.model_defaults`
            module.

        sec_haloprop_percentile_bins : int, float, list or None, optional
            If an integer, it determines how many evenly spaced bins in the
            secondary halo property percentiles are going to be used. If a
            float between 0 and 1, it determines the split. Finally, if a list
            or numpy array, it directly describes the bins that are going to be
            used. If None is provided, no binning is applied.

        sats_per_prim_haloprop : float, optional
            Float determing how many satellites sample each halo. For each
            halo, the number is drawn from a Poisson distribution with an
            expectation value of ``sats_per_prim_haloprop`` times the primary
            halo property.

        downsample : float, optional
            Fraction between 0 and 1 used to downsample the total sample used
            to tabulate correlation functions. Values below unity can be used
            to reduce the computation time. It should not result in biases but
            the resulting correlation functions will be less accurate.

        verbose : boolean, optional
            Boolean determing whether the progress should be displayed.

        redshift_space_distortions : boolean, optional
            Boolean determining whether redshift space distortions should be
            applied to halos/galaxies.

        cens_prof_model : object, optional
            Instance of `halotools.empirical_models.MonteCarloGalProf` that
            determines the phase space coordinates of centrals. If none is
            provided, `halotools.empirical_models.TrivialPhaseSpace` will be
            used.

        sats_prof_model : object, optional
            Instance of `halotools.empirical_models.MonteCarloGalProf` that
            determines the phase space coordinates of satellites. If none is
            provided, `halotools.empirical_models.NFWPhaseSpace` will be used.

        project_xyz : bool, optional
            If True, the coordinates will be projected along all three spatial
            axes. By default, only the projection onto the z-axis is used.

        comm : MPI communicator
            If not None, then will distribute calculation via MPI

        **tpcf_kwargs : dict, optional
                Keyword arguments passed to the ``tpcf`` function.

        Returns
        -------
        halotab : TabCorr
            Object containing all necessary information to calculate
            correlation functions for arbitrary galaxy models.
        """

        if sec_haloprop_percentile_bins is None:
            sec_haloprop_percentile_bins = np.array([0, 1])
        elif isinstance(sec_haloprop_percentile_bins, float):
            sec_haloprop_percentile_bins = np.array(
                [0, sec_haloprop_percentile_bins, 1])

        if 'period' in tpcf_kwargs:
            print('Warning: TabCorr will pass the keyword argument "period" ' +
                  'to {} based on the Lbox argument of'.format(tpcf.__name__) +
                  ' the halo catalog. The value you provided will be ignored.')
            del tpcf_kwargs['period']

        halotab = cls()

        if cosmology_ref is not None and mode == 'auto':
            rp_stretch = (
                (cosmology_ref.comoving_distance(halocat.redshift) *
                 cosmology_ref.H0) /
                (cosmology.comoving_distance(halocat.redshift) * cosmology.H0))
            pi_stretch = (cosmology.efunc(halocat.redshift) /
                          cosmology_ref.efunc(halocat.redshift))
            lbox_stretch = np.array([rp_stretch, rp_stretch, pi_stretch])
        else:
            lbox_stretch = np.ones(3)

        # First, we tabulate the halo number densities.
        halos = halocat.halo_table
        halos = halos[halos['halo_pid'] == -1]
        halos = halos[halos[prim_haloprop_key] >=
                      (Num_ptcl_requirement - 0.5) * halocat.particle_mass]

        if isinstance(prim_haloprop_bins, int):
            prim_haloprop_bins = np.linspace(
                np.log10(np.amin(halos[prim_haloprop_key])) - 1e-3,
                np.log10(np.amax(halos[prim_haloprop_key])) + 1e-3,
                prim_haloprop_bins + 1)
        elif not isinstance(prim_haloprop_bins, (list, np.ndarray)):
            raise ValueError('prim_haloprop_bins must be an int, list or ' +
                             'numpy array.')

        halos[sec_haloprop_key +
              '_percentile'] = (compute_conditional_percentiles(
                  table=halos,
                  prim_haloprop_key=prim_haloprop_key,
                  sec_haloprop_key=sec_haloprop_key))

        halotab.gal_type = Table()

        n_h, prim_haloprop_bins, sec_haloprop_percentile_bins = (
            np.histogram2d(
                np.log10(halos[prim_haloprop_key]),
                halos[sec_haloprop_key + '_percentile'],
                bins=[prim_haloprop_bins, sec_haloprop_percentile_bins]))
        halotab.gal_type['n_h'] = n_h.ravel(order='F') / np.prod(
            halocat.Lbox * lbox_stretch)

        grid = np.meshgrid(prim_haloprop_bins, sec_haloprop_percentile_bins)
        halotab.gal_type['log_prim_haloprop_min'] = grid[0][:-1, :-1].ravel()
        halotab.gal_type['log_prim_haloprop_max'] = grid[0][:-1, 1:].ravel()
        halotab.gal_type['sec_haloprop_percentile_min'] = (
            grid[1][:-1, :-1].ravel())
        halotab.gal_type['sec_haloprop_percentile_max'] = (
            grid[1][1:, :-1].ravel())

        halotab.gal_type = vstack([halotab.gal_type, halotab.gal_type])
        halotab.gal_type['gal_type'] = np.concatenate(
            (np.repeat('centrals'.encode('utf8'),
                       len(halotab.gal_type) // 2),
             np.repeat('satellites'.encode('utf8'),
                       len(halotab.gal_type) // 2)))
        halotab.gal_type['prim_haloprop'] = 10**(
            0.5 * (halotab.gal_type['log_prim_haloprop_min'] +
                   halotab.gal_type['log_prim_haloprop_max']))
        halotab.gal_type['sec_haloprop_percentile'] = (
            0.5 * (halotab.gal_type['sec_haloprop_percentile_min'] +
                   halotab.gal_type['sec_haloprop_percentile_max']))

        # Now, we tabulate the correlation functions.
        cens_occ_model = Zheng07Cens(prim_haloprop_key=prim_haloprop_key)
        if cens_prof_model is None:
            cens_prof_model = TrivialPhaseSpace(redshift=halocat.redshift)
        sats_occ_model = Zheng07Sats(prim_haloprop_key=prim_haloprop_key)
        if sats_prof_model is None:
            sats_prof_model = NFWPhaseSpace(redshift=halocat.redshift)

        model = HodModelFactory(centrals_occupation=cens_occ_model,
                                centrals_profile=cens_prof_model,
                                satellites_occupation=sats_occ_model,
                                satellites_profile=sats_prof_model)

        model.param_dict['logMmin'] = 0
        model.param_dict['sigma_logM'] = 0.1
        model.param_dict['alpha'] = 1.0
        model.param_dict['logM0'] = 0
        model.param_dict['logM1'] = -np.log10(sats_per_prim_haloprop)
        model.populate_mock(halocat, Num_ptcl_requirement=Num_ptcl_requirement)
        gals = model.mock.galaxy_table
        gals = gals[np.random.random(len(gals)) < downsample]

        idx_gals, idx_halos = crossmatch(gals['halo_id'], halos['halo_id'])
        assert np.all(gals['halo_id'][idx_gals] == halos['halo_id'][idx_halos])
        gals[sec_haloprop_key + '_percentile'] = np.zeros(len(gals))
        gals[sec_haloprop_key +
             '_percentile'][idx_gals] = (halos[sec_haloprop_key +
                                               '_percentile'][idx_halos])

        if verbose:
            print("Number of tracer particles: {0}".format(len(gals)))

        for xyz in ['xyz', 'yzx', 'zxy']:
            pos_all = return_xyz_formatted_array(
                x=gals[xyz[0]],
                y=gals[xyz[1]],
                z=gals[xyz[2]],
                velocity=gals['v' +
                              xyz[2]] if redshift_space_distortions else 0,
                velocity_distortion_dimension='z',
                period=halocat.Lbox,
                redshift=halocat.redshift,
                cosmology=cosmology) * lbox_stretch

            pos = []
            n_gals = []
            for i in range(len(halotab.gal_type)):

                mask = ((10**(halotab.gal_type['log_prim_haloprop_min'][i]) <
                         gals[prim_haloprop_key]) &
                        (10**(halotab.gal_type['log_prim_haloprop_max'][i]) >=
                         gals[prim_haloprop_key]) &
                        (halotab.gal_type['sec_haloprop_percentile_min'][i] <
                         gals[sec_haloprop_key + '_percentile']) &
                        (halotab.gal_type['sec_haloprop_percentile_max'][i] >=
                         gals[sec_haloprop_key + '_percentile']) &
                        (halotab.gal_type['gal_type'][i] == gals['gal_type']))

                pos.append(pos_all[mask])
                n_gals.append(np.sum(mask))

            n_gals = np.array(n_gals)
            n_done = 0

            if verbose:
                print("Projecting onto {0}-axis...".format(xyz[2]))

            gal_type_index = np.arange(len(halotab.gal_type))

            if (comm is not None) & (has_mpi):
                size = comm.size
                rank = comm.rank
                gal_type_index = gal_type_index[rank::size]
                print('{}: len(gal_type_index)={}'.format(
                    rank, len(gal_type_index)))
            elif (comm is not None) & (not has_mpi):
                raise (ImportError(
                    "You passed something to the comm argument, but I couldn't import mpi4py"
                ))

            for i in gal_type_index:

                if mode == 'auto':
                    for k in np.arange(i, len(halotab.gal_type)):
                        if len(pos[i]) * len(pos[k]) > 0:

                            if verbose:
                                if comm:
                                    if comm.rank == 0:
                                        n_done += (n_gals[i] * n_gals[k] *
                                                   (2 if k != i else 1))
                                        print_progress(n_done /
                                                       np.sum(n_gals)**2)
                                else:
                                    n_done += (n_gals[i] * n_gals[k] *
                                               (2 if k != i else 1))
                                    print_progress(n_done / np.sum(n_gals)**2)
                            if i == k:
                                xi = tpcf(pos[i],
                                          *tpcf_args,
                                          sample2=pos[k] if k != i else None,
                                          do_auto=True,
                                          do_cross=False,
                                          period=halocat.Lbox * lbox_stretch,
                                          **tpcf_kwargs)
                            else:
                                xi = tpcf(pos[i],
                                          *tpcf_args,
                                          sample2=pos[k] if k != i else None,
                                          do_auto=False,
                                          do_cross=True,
                                          period=halocat.Lbox * lbox_stretch,
                                          **tpcf_kwargs)

                            if 'tpcf_matrix' not in locals():
                                tpcf_matrix = np.zeros(
                                    (len(xi.ravel()), len(halotab.gal_type),
                                     len(halotab.gal_type)))
                                tpcf_shape = xi.shape
                            tpcf_matrix[:, i, k] += xi.ravel()
                            tpcf_matrix[:, k, i] = tpcf_matrix[:, i, k]

                elif mode == 'cross':
                    if len(pos[i]) > 0:

                        if verbose:
                            n_done += n_gals[i]
                            print_progress(n_done / np.sum(n_gals))

                        xi = tpcf(pos[i],
                                  *tpcf_args,
                                  **tpcf_kwargs,
                                  period=halocat.Lbox * lbox_stretch)
                        if tpcf.__name__ == 'delta_sigma':
                            xi = xi[1]
                        if 'tpcf_matrix' not in locals():
                            tpcf_matrix = np.zeros(
                                (len(xi.ravel()), len(halotab.gal_type)))
                            tpcf_shape = xi.shape
                        tpcf_matrix[:, i] = xi.ravel()

            if not project_xyz or mode == 'cross':
                break

        if comm:
            tpcf_matrix = comm.allreduce(tpcf_matrix, op=MPI.SUM)

        if project_xyz and mode == 'auto':
            tpcf_matrix /= 3.0

        if mode == 'auto':
            tpcf_matrix_flat = []
            for i in range(tpcf_matrix.shape[0]):
                tpcf_matrix_flat.append(
                    symmetric_matrix_to_array(tpcf_matrix[i]))
            tpcf_matrix = np.array(tpcf_matrix_flat)

        halotab.attrs = {}
        halotab.attrs['tpcf'] = tpcf.__name__
        halotab.attrs['mode'] = mode
        halotab.attrs['simname'] = halocat.simname
        halotab.attrs['redshift'] = halocat.redshift
        halotab.attrs['Num_ptcl_requirement'] = Num_ptcl_requirement
        halotab.attrs['prim_haloprop_key'] = prim_haloprop_key
        halotab.attrs['sec_haloprop_key'] = sec_haloprop_key

        halotab.tpcf_args = tpcf_args
        halotab.tpcf_kwargs = tpcf_kwargs
        halotab.tpcf_shape = tpcf_shape
        halotab.tpcf_matrix = tpcf_matrix

        halotab.init = True

        return halotab
Beispiel #11
0
def main():

    # get simulation information
    if len(sys.argv)>1:
        sim_name = sys.argv[1]
        snapnum = int(sys.argv[2])
        shape_type = sys.argv[3]
        sample_name = sys.argv[4]
    else:
        sim_name = 'TNG300-1' # full physics high-res run
        snapnum = 99  # z=0
        shape_type = 'reduced'  # non-reduced, reduced, iterative
        sample_name = 'sample_3'

    # load a test halo catalog
    from halotools.sim_manager import CachedHaloCatalog
    halocat = CachedHaloCatalog(simname='bolplanck', halo_finder='rockstar',
                                redshift=0.0, dz_tol=0.1, version_name='halotools_v0p4')

    from halotools.empirical_models import HodModelFactory

    # define the central occupatoion model
    from halotools.empirical_models import TrivialPhaseSpace, Zheng07Cens
    cens_occ_model =  Zheng07Cens()
    cens_prof_model = TrivialPhaseSpace()

    # define the satellite occupation model
    from halotools.empirical_models import Zheng07Sats
    from halotools.empirical_models import NFWPhaseSpace, SubhaloPhaseSpace
    from intrinsic_alignments.ia_models.anisotropic_nfw_phase_space import AnisotropicNFWPhaseSpace
    sats_occ_model =  Zheng07Sats()
    #sats_prof_model = AnisotropicNFWPhaseSpace()
    sats_prof_model = SubhaloPhaseSpace('satellites', np.logspace(10.5, 15.2, 15))

    # define the alignment models
    from intrinsic_alignments.ia_models.ia_model_components import CentralAlignment,\
        RadialSatelliteAlignment,  MajorAxisSatelliteAlignment, HybridSatelliteAlignment
    central_orientation_model = CentralAlignment()
    satellite_orientation_model = RadialSatelliteAlignment()

    if sample_name == 'sample_1':
        cens_occ_model.param_dict['logMmin'] = 12.54
        cens_occ_model.param_dict['sigma_logM'] = 0.26

        sats_occ_model.param_dict['alpha'] = 1.0
        sats_occ_model.param_dict['logM0'] = 12.68
        sats_occ_model.param_dict['logM1'] = 13.48

        central_orientation_model.param_dict['central_alignment_strength'] = 0.755
        satellite_orientation_model.param_dict['satellite_alignment_strength'] = 0.279
    elif sample_name == 'sample_2':
        cens_occ_model.param_dict['logMmin'] = 11.93
        cens_occ_model.param_dict['sigma_logM'] = 0.26

        sats_occ_model.param_dict['alpha'] = 1.0
        sats_occ_model.param_dict['logM0'] = 12.05
        sats_occ_model.param_dict['logM1'] = 12.85

        central_orientation_model.param_dict['central_alignment_strength'] = 0.64
        satellite_orientation_model.param_dict['satellite_alignment_strength'] = 0.084
    elif sample_name =='sample_3':
        cens_occ_model.param_dict['logMmin'] = 11.61
        cens_occ_model.param_dict['sigma_logM'] = 0.26

        sats_occ_model.param_dict['alpha'] = 1.0
        sats_occ_model.param_dict['logM0'] = 11.8
        sats_occ_model.param_dict['logM1'] = 12.6

        central_orientation_model.param_dict['central_alignment_strength'] = 0.57172919
        satellite_orientation_model.param_dict['satellite_alignment_strength'] = 0.01995

    # combine model components
    model_instance = HodModelFactory(centrals_occupation = cens_occ_model,
                                 centrals_profile = cens_prof_model,
                                 satellites_occupation = sats_occ_model,
                                 satellites_profile = sats_prof_model,
                                 centrals_orientation = central_orientation_model,
                                 satellites_orientation = satellite_orientation_model,
                                 model_feature_calling_sequence = (
                                 'centrals_occupation',
                                 'centrals_profile',
                                 'satellites_occupation',
                                 'satellites_profile',
                                 'centrals_orientation',
                                 'satellites_orientation')
                                )
    
    # populate mock catalog
    model_instance.populate_mock(halocat)
    print("number of galaxies: ", len(model_instance.mock.galaxy_table))

    mock = model_instance.mock.galaxy_table

    # galaxy coordinates and orientations
    coords = np.vstack((mock['x'],
                        mock['y'],
                        mock['z'])).T

    orientations = np.vstack((mock['galaxy_axisA_x'],
                              mock['galaxy_axisA_y'],
                              mock['galaxy_axisA_z'])).T

    from halotools.mock_observables import tpcf, tpcf_jackknife

    rbins = np.logspace(-1,1.5,15)
    rbin_centers = (rbins[:-1]+rbins[1:])/2.0

    xi = tpcf(coords, rbins, period=halocat.Lbox)
    err=np.zeros(len(xi))

    # save measurements
    fpath = fpath = PROJECT_DIRECTORY + 'modelling_illustris/data/'
    fname = sim_name + '_' + str(snapnum) + '-' + sample_name +'_model_xi.dat'
    ascii.write([rbin_centers, xi, err],
                 fpath+fname,
                 names=['r','xi','err'],
                 overwrite=True)
# build the two models

std_model = PrebuiltHodModelFactory('zheng07')

centrals_occupation = AssembiasZheng07Cens()
centrals_profile = TrivialPhaseSpace()
satellites_occupation = AssembiasZheng07Sats()
satellites_profile = NFWPhaseSpace()

satellites_occupation._suppress_repeated_param_warning = True

model_dict = ({'centrals_occupation': centrals_occupation,
    'centrals_profile': centrals_profile,
    'satellites_occupation': satellites_occupation,
    'satellites_profile': satellites_profile})
ab_model = HodModelFactory(**model_dict)

################################################################################
# Initially populate both models

halocat = CachedHaloCatalog(simname='bolplanck')
__  = halocat.halo_table['halo_x']

std_model.populate_mock(halocat)
ab_model.populate_mock(halocat)

################################################################################
np.seterr(divide='ignore', invalid='ignore')  # ignore divide by zero in e.g. DD/RR


def calculate_hod(halo_table, galaxy_table):
Beispiel #13
0
def main():

    nu_cens = np.linspace(0,1,2)
    nu_sats = np.linspace(-0.3,0.8, 2)

    # get simulation information
    if len(sys.argv)>1:
        sim_name = sys.argv[1]
        snapnum = int(sys.argv[2])
        shape_type = sys.argv[3]
        sample_name = sys.argv[4]
    else:
        sim_name = 'TNG300-1' # full physics high-res run
        snapnum = 99  # z=0
        shape_type = 'reduced'  # non-reduced, reduced, iterative
        sample_name = 'sample_3'

    # load a test halo catalog
    from halotools.sim_manager import CachedHaloCatalog
    halocat = CachedHaloCatalog(simname='bolplanck', halo_finder='rockstar',
                                redshift=0.0, dz_tol=0.1, version_name='halotools_v0p4')

    from halotools.empirical_models import HodModelFactory

    # define the central occupatoion model
    from halotools.empirical_models import TrivialPhaseSpace, Zheng07Cens
    cens_occ_model =  Zheng07Cens()
    cens_prof_model = TrivialPhaseSpace()

    # define the satellite occupation model
    from halotools.empirical_models import Zheng07Sats
    from halotools.empirical_models import NFWPhaseSpace, SubhaloPhaseSpace
    from intrinsic_alignments.ia_models.anisotropic_nfw_phase_space import AnisotropicNFWPhaseSpace
    sats_occ_model =  Zheng07Sats()
    #sats_prof_model = AnisotropicNFWPhaseSpace()
    sats_prof_model = SubhaloPhaseSpace('satellites', np.logspace(10.5, 15.2, 15))

    # define the alignment models
    from intrinsic_alignments.ia_models.ia_model_components import CentralAlignment,\
        RadialSatelliteAlignment,  MajorAxisSatelliteAlignment, HybridSatelliteAlignment
    central_orientation_model = CentralAlignment()
    satellite_orientation_model = RadialSatelliteAlignment()

    if sample_name == 'sample_1':
        cens_occ_model.param_dict['logMmin'] = 12.54
        cens_occ_model.param_dict['sigma_logM'] = 0.26

        sats_occ_model.param_dict['alpha'] = 1.0
        sats_occ_model.param_dict['logM0'] = 12.68
        sats_occ_model.param_dict['logM1'] = 13.48

        central_orientation_model.param_dict['central_alignment_strength'] = 0.755
        satellite_orientation_model.param_dict['satellite_alignment_strength'] = 0.279
    elif sample_name == 'sample_2':
        cens_occ_model.param_dict['logMmin'] = 11.93
        cens_occ_model.param_dict['sigma_logM'] = 0.26

        sats_occ_model.param_dict['alpha'] = 1.0
        sats_occ_model.param_dict['logM0'] = 12.05
        sats_occ_model.param_dict['logM1'] = 12.85

        central_orientation_model.param_dict['central_alignment_strength'] = 0.64
        satellite_orientation_model.param_dict['satellite_alignment_strength'] = 0.084
    elif sample_name =='sample_3':
        cens_occ_model.param_dict['logMmin'] = 11.61
        cens_occ_model.param_dict['sigma_logM'] = 0.26

        sats_occ_model.param_dict['alpha'] = 1.0
        sats_occ_model.param_dict['logM0'] = 11.8
        sats_occ_model.param_dict['logM1'] = 12.6

        central_orientation_model.param_dict['central_alignment_strength'] = 0.57172919
        satellite_orientation_model.param_dict['satellite_alignment_strength'] = 0.01995

    # combine model components
    model_instance = HodModelFactory(centrals_occupation = cens_occ_model,
                                 centrals_profile = cens_prof_model,
                                 satellites_occupation = sats_occ_model,
                                 satellites_profile = sats_prof_model,
                                 centrals_orientation = central_orientation_model,
                                 satellites_orientation = satellite_orientation_model,
                                 model_feature_calling_sequence = (
                                 'centrals_occupation',
                                 'centrals_profile',
                                 'satellites_occupation',
                                 'satellites_profile',
                                 'centrals_orientation',
                                 'satellites_orientation')
                                )
    
    from intrinsic_alignments.utils.jackknife_observables import jackknife_ed_3d
    from halotools.mock_observables.alignments import ed_3d

    rbins = np.logspace(-1,1.5,15)
    rbin_centers = (rbins[:-1]+rbins[1:])/2.0

    N1 = len(nu_cens)
    N2 = len(nu_sats)
    
    fpath = fpath = PROJECT_DIRECTORY + 'modelling_illustris/data/'
    fname = sim_name + '_' + str(snapnum) + '-' + sample_name +'_model_ed_grid.dat'

    outF = open(fpath + fname, "w")
    
    for i in range(0,N1):
        for j in range(0,N2):

            print(i, j)

            # assign parameters
            central_orientation_model.param_dict['central_alignment_strength'] = nu_cens[i]
            satellite_orientation_model.param_dict['satellite_alignment_strength'] = nu_sats[j]
            
            # populate mock catalog
            start = time.time()
            model_instance.populate_mock(halocat)
            print("time to populate mock: ", time.time() - start)

            mock = model_instance.mock.galaxy_table

            # galaxy coordinates and orientations
            coords = np.vstack((mock['x'],
                                mock['y'],
                                mock['z'])).T

            orientations = np.vstack((mock['galaxy_axisA_x'],
                                      mock['galaxy_axisA_y'],
                                      mock['galaxy_axisA_z'])).T

            # calculate ED
            start = time.time()
            ed = ed_3d(coords, orientations,coords,
                       rbins, period=halocat.Lbox,
                       num_threads=4)
            print("time to calculate ED stat: ", time.time() - start)

            # calculate EE
            start = time.time()
            ee = ee_3d(coords, orientations, coords, orientations,
                       rbins, period=halocat.Lbox,
                       num_threads=4)
            print("time to calculate EE stat: ", time.time() - start)

            s = str(nu_cens[i]) + ' ' + str(nu_sats[j]) + ' ' + np.array_str(ed)[1:-1] + ' ' + np.array_str(ee)[1:-1]
            outF.write(s)
            outF.write("\n")
    
    outF.close()
Beispiel #14
0
    def to_halotools(cosmo, redshift, mdef, concentration_key=None, **kwargs):
        """
        Return the Zheng 07 HOD model.

        See :func:`halotools.empirical_models.zheng07_model_dictionary`.

        Parameters
        ----------
        cosmo :
            the nbodykit or astropy Cosmology object to use in the model
        redshift : float
            the desired redshift of the model
        mdef : str, optional
            string specifying mass definition, used for computing default
            halo radii and concentration; should be 'vir' or 'XXXc' or
            'XXXm' where 'XXX' is an int specifying the overdensity
        concentration_key : str
            the name of the column that will specify concentration; if not
            provided, the analytic formula from
            `Dutton and Maccio 2014 <https://arxiv.org/abs/1402.7073>`_
            is used.
        **kwargs :
            additional keywords passed to the model components; see the
            Halotools documentation for further details

        Returns
        -------
        :class:`~halotools.empirical_models.HodModelFactory`
            the halotools object implementing the HOD model
        """
        from halotools.empirical_models import Zheng07Sats, Zheng07Cens, NFWPhaseSpace, TrivialPhaseSpace
        from halotools.empirical_models import HodModelFactory

        kwargs.setdefault('modulate_with_cenocc', True)

        # need astropy Cosmology
        if isinstance(cosmo, Cosmology):
            cosmo = cosmo.to_astropy()

        # determine concentration key
        if concentration_key is None:
            conc_mass_model = 'dutton_maccio14'
        else:
            conc_mass_model = 'direct_from_halo_catalog'

        # determine mass column
        mass_key = 'halo_m' + mdef

        # occupation functions
        cenocc = Zheng07Cens(prim_haloprop_key=mass_key, **kwargs)
        satocc = Zheng07Sats(prim_haloprop_key=mass_key,
                             cenocc_model=cenocc,
                             **kwargs)
        satocc._suppress_repeated_param_warning = True

        # profile functions
        kwargs.update({'cosmology': cosmo, 'redshift': redshift, 'mdef': mdef})
        censprof = TrivialPhaseSpace(**kwargs)
        satsprof = NFWPhaseSpace(conc_mass_model=conc_mass_model, **kwargs)

        # make the model
        model = {}
        model['centrals_occupation'] = cenocc
        model['centrals_profile'] = censprof
        model['satellites_occupation'] = satocc
        model['satellites_profile'] = satsprof
        return HodModelFactory(**model)
Beispiel #15
0
         1.88033657e+00, 2.33929965e+00, 2.93594021e+00, 3.46849456e+00,
         4.26576964e+00, 5.18755609e+00, 6.10833984e+00, 7.04389976e+00,
         7.09565921e+00
     ]])

err = np.sqrt(np.array([cov[i, i] for i in range(len(cov))]))
bin_cen = (bin_edges[1:] + bin_edges[:-1]) / 2.

cens_occ_model = Zheng07Cens(prim_haloprop_key='halo_vmax')
cens_prof_model = TrivialPhaseSpace()

sats_occ_model = Zheng07Sats(prim_haloprop_key='halo_vmax')
sats_prof_model = NFWPhaseSpace()

model_instance = HodModelFactory(centrals_occupation=cens_occ_model,
                                 centrals_profile=cens_prof_model,
                                 satellites_occupation=sats_occ_model,
                                 satellites_profile=sats_prof_model)
halocat = CachedHaloCatalog(simname='bolplanck', redshift=0.0)
model_instance.populate_mock(halocat)


#log liklihood
def lnlike(theta):  #, wp_val, wperr, model_instance):
    logMmin, sigma_logM, alpha, logM0, logM1 = theta
    model_instance.param_dict['logMmin'] = logMmin
    model_instance.param_dict['sigma_logM'] = sigma_logM
    model_instance.param_dict['alpha'] = alpha
    model_instance.param_dict['logM0'] = logM0
    model_instance.param_dict['logM1'] = logM1

    model_instance.mock.populate()
Beispiel #16
0
            lower_assembias_bound = 0,
            upper_assembias_bound = np.inf,
            **kwargs)



cens_occ_model =  Zheng07Cens(threshold = -21)
cens_prof_model = TrivialPhaseSpace()

from halotools.empirical_models import NFWPhaseSpace
sats_occ_model =  AssembiasZheng07Sats()
sats_prof_model = NFWPhaseSpace()

model_instance = HodModelFactory(
        centrals_occupation = cens_occ_model,
        centrals_profile = cens_prof_model,
        satellites_occupation = sats_occ_model,
        satellites_profile = sats_prof_model)

model_instance.param_dict['mean_occupation_satellites_assembias_param1'] = -1.
model_instance.populate_mock(simname = 'multidark')


x = model_instance.mock.galaxy_table['x']
y = model_instance.mock.galaxy_table['y']
z = model_instance.mock.galaxy_table['z']
vz = model_instance.mock.galaxy_table['vz']

pos = return_xyz_formatted_array(x, y, z, velocity = vz, velocity_distortion_dimension = 'z')