コード例 #1
0
ファイル: gel.py プロジェクト: ericmjl/pydna
def interpolator(mwstd):
    """docstring."""
    interpolator = _CubicSpline(
        [len(fr) for fr in mwstd[::-1]],
        [fr.rf for fr in mwstd[::-1]],
        bc_type="natural",
        extrapolate=False,
    )
    interpolator.mwstd = mwstd
    return interpolator
コード例 #2
0
        def get_pnonlin_of_l_and_z(emu_pars_dict, z_vec, l_vec):
            """
            * NESTED FUNCTION * (defined inside get_pconv)

            Signature:          get_P_of_l_and_z(emu_pars_dict, z_vec, l_vec)

            Description:        This function computes the different k
                                ranges for the given cosmology at all
                                different redshifts and computes the power
                                spectrum for these redshifts at these k modes.

            Input types:        emu_pars_dict - dictionary
                                z_vec - numpy.ndarray
                                l_vec - numpy.ndarray

            Output type:        numpy.ndarray
            """
            P = get_pnonlin(emu_pars_dict, z_vec)  # call EuclidEmulator

            pnonlin_array = []
            for i, z in enumerate(z_vec):
                func = _CubicSpline(_np.log10(P['k']),
                                    _np.log10(P['P_nonlin']['z' + str(i)]))
                k = _cc.l_to_k(emu_pars_dict, l_vec, z)  # needs to be called
                # inside loop because
                # different z-values
                # lead to different
                # results

                # evaluate the interpolating function of Pnl for all k in the
                # range allowed by EuclidEmulator (this range is given by
                # P['k'])
                pmatternl = [
                    10.0**func(_np.log10(kk)) for kk in k
                    if kk >= P['k'].min() and kk <= P['k'].max()
                ]
                # for k values below the lower bound or above the upper bound
                # of this k range, set the contributions to 0.0
                p_toosmall = [0.0 for kk in k if kk < P['k'].min()]
                p_toobig = [0.0 for kk in k if kk > P['k'].max()]

                pnonlin_array.append(
                    _np.array(p_toosmall + pmatternl + p_toobig))

            # get the non-linear matter power spectrum in units of [(Mpc/h)^3]
            return _np.asarray(pnonlin_array).transpose()
コード例 #3
0
ファイル: _ee_lens.py プロジェクト: miknab/EuclidEmulator
def lens_efficiency(sourcedist, dcomov, dcomov_lim, prec=12):
    """
    Signature:    lens_efficiency(sourcedist, dcomov, dcomov_lim)

    Description:  Computes the lens efficiency function q (see e.g. equation
                  (24)the review article by Martin Kilbinger "Cosmology with
                  cosmic shear observations: a review", July 21, 2015,
                  arXiv:1411.0115v2), given a source distribution function n and
                  two comoving distances.

    Input types:  sourcedist - np.ndarray
                  dcomov - float (or int) or np.ndarray
                  dcomov_lim - float

    Output types: float (or np.ndarray if type(dcomov)=np.ndarray)
    """

    # interpolate the source distribution function
    nfunc = _CubicSpline(_np.log10(sourcedist['chi']),
                         _np.log10(sourcedist['n']))

    result = []

    if isinstance(dcomov, _np.ndarray):

        for distance in dcomov:
            chi = _np.linspace(distance, dcomov_lim, 2**prec + 1)
            sourcedistribution = 10**nfunc(_np.log10(chi))
            integrand = sourcedistribution * (1 - distance / chi)
            result.append(_romb(integrand, chi[1] - chi[0]))

        return _np.asarray(result)

    elif isinstance(dcomov, (float, int)):
        chi = _np.linspace(dcomov, dcomov_lim, 2**prec + 1)
        sourcedistribution = 10**nfunc(_np.log10(chi))
        integrand = sourcedistribution * (1 - dcomov / chi)

        return _romb(integrand, chi[1] - chi[0])

    else:
        raise (TypeError,
               "The second argument 'dcomov' must be either a float,\
                          an integer or a np.ndarray.\n")
コード例 #4
0
def get_boost(emu_pars_dict, redshifts, custom_kvec=None, verbose=True):
    """
    Signature:   get_boost(emu_pars_dict, redshifts [, custom_kvec=None, verbose=True])

    Description: Computes the non-linear boost factor for a cosmology
                 defined in emu_pars_dict (a python dictionary containing
                 the values for the 6 LCDM parameters) at specified
                 redshift stored in a list or numpy.ndarray.
                 Optionally, a list or numpy.ndarray of k modes can be
                 passed to the function via the keyword argument "kvec".
                 Then, by setting verbose=False, it is possible to fully
                 suppress any verbose information about how the code
                 progresses. 

    Input types: python dictionary (with the six cosmological parameters)
                 list or numpy.ndarray (with redshift values)

                 :OPTIONAL:
                 list or numpy.ndarray (with k mode values)
                 boolean (verbosity)

    Output type: python dictionary

    Related:     get_plin, get_pnonlin
    """
    # Check cosmological parameter ranges
    _inp.check_param_range(emu_pars_dict)

    if isinstance(redshifts, (int, float)):
        redshifts = _np.asarray([redshifts])
    else:
        redshifts = _np.asarray(redshifts)

    for z in redshifts:
        assert z <= 5.0, "EuclidEmulator allows only redshifts z <= 5.0.\n"

    if not isinstance(emu_pars_dict, (dict,)):
        print "The cosmological parameters must be passed as a python \
               dictionary.\n"
        _sys.exit()

    boost_data = _eeb.emu_boost(_np.array([emu_pars_dict['om_b'],
                                           emu_pars_dict['om_m'],
                                           emu_pars_dict['n_s'],
                                           emu_pars_dict['h'],
                                           emu_pars_dict['w_0'],
                                           emu_pars_dict['sigma_8']]),
                                redshifts, verbose)

    kvals = boost_data.k
    k_shape = kvals.shape

    do_extrapolate_above = False
    do_extrapolate_below = False
    if not(custom_kvec is None):
        upper_mask = custom_kvec < max(kvals)
        lower_mask = custom_kvec > min(kvals)
        mask = [u and l for (u,l) in zip(lower_mask, upper_mask)]
        custom_k_within_range = custom_kvec[mask]
        custom_k_below = custom_kvec[[not(l) for l in lower_mask]]
        custom_k_above = custom_kvec[[not(u) for u in upper_mask]]

        if any(custom_kvec > max(kvals)):
            wrn_message = ("EuclidEmulator emulates the non-linear correction in \n"
                           "the interval [6.87215e-3 h/Mpc, 5.52669h/Mpc]. You are \n"
                           "requesting k modes beyond k_max = 5.52669h/Mpc. \n"
                           "Higher k modes constantly extrapolated.")
            if verbose:
                _warnings.warn(wrn_message)
            do_extrapolate_above = True

        if any(custom_kvec < min(kvals)):
            wrn_message = ("EuclidEmulator emulates the non-linear correction in \n"
                           "the interval [6.87215e-3 h/Mpc, 5.52669h/Mpc]. You are \n"
                           "requesting k modes below k_min = 6.87215h/Mpc. \n"
                           "Lower k modes constantly extrapolated.")
            if verbose:
                _warnings.warn(wrn_message)
            do_extrapolate_below = True

    len_kvec = len(kvals)
    len_redshifts = len(redshifts)

    if len_redshifts > 1:
        bvals = {}
        for i in range(len_redshifts):
            tmp = boost_data.boost[i*len_kvec:(i+1)*len_kvec]
            if not(custom_kvec is None):
                bvals['z'+str(i)] = 10.0**_CubicSpline(_np.log10(kvals),
                                                       _np.log10(tmp.reshape(k_shape))
                                                      )(_np.log10(custom_k_within_range))

                #Extrapolate if necessary
                if do_extrapolate_below:
                    # below the k_min of EuclidEmulator, we are in the linear regime where
                    # the boost factor is unity by construction
                    b_extrap = _np.ones_like(custom_k_below)
                    bvals['z'+str(i)]= _np.concatenate((b_extrap, bvals['z'+str(i)]))

                if do_extrapolate_above:
                    # We extrapolate by setting all b(k > k_max) to b(k_max)
                    b_extrap = bvals['z'+str(i)][-1] * _np.ones_like(custom_k_above)
                    bvals['z'+str(i)] = _np.concatenate((bvals['z'+str(i)], b_extrap))

            else:
                bvals['z'+str(i)] = tmp.reshape(k_shape)
    else:
        tmp = boost_data.boost
        if not(custom_kvec is None):
            bvals = 10.0**_CubicSpline(_np.log10(kvals), 
                                       _np.log10(tmp.reshape(k_shape))
                                      )(_np.log10(custom_k_within_range))

            #Extrapolate if necessary
            if do_extrapolate_below:
                # below the k_min of EuclidEmulator, we are in the linear regime where
                # the boost factor is unity by construction
                b_extrap = _np.ones_like(custom_k_below)
                bvals = _np.concatenate((b_extrap,bvals))

            if do_extrapolate_above:
                # We extrapolate by setting all b(k > k_max) to b(k_max)
                b_extrap = bvals[-1] * _np.ones_like(custom_k_above)
                bvals = _np.concatenate((bvals, b_extrap))

        else:
            bvals = tmp.reshape(k_shape)

    if not(custom_kvec is None):       # This could probably be done cleaner!
        kvals = custom_kvec

    return {'k': kvals, 'B': bvals}