def get_perturbed_cosmology(cosmo_old,
                            parameter,
                            epsilon,
                            log_param_deriv=False):
    """get a perturbed cosmology dictionary needed for get_perturbed_cosmopie
        inputs:
            cosmo_old: fiducial cosmology
            paramter: name of parameter to change
            epsilon: amount to change parameter by
            log_param_deriv: if True, do log change in parameter"""
    cosmo_new = cosmo_old.copy()
    if cosmo_new.get(parameter) is not None:

        if log_param_deriv:
            cosmo_new[parameter] = np.exp(
                np.log(cosmo_old[parameter]) + epsilon)
        else:
            cosmo_new[parameter] += epsilon

        if cosmo_old['de_model'] == 'w0wa':
            if parameter == 'w':
                cosmo_new['w0'] = cosmo_new['w']
            elif parameter == 'w0':
                cosmo_new['w'] = cosmo_new['w0']

        if parameter not in cp.P_SPACES.get(
                cosmo_new.get('p_space')) and parameter not in cp.DE_METHODS[
                    cosmo_new.get('de_model')]:
            warn('parameter \'' + str(parameter) +
                 '\' may not support derivatives with the parameter set \'' +
                 str(cosmo_new.get('p_space')) + '\'')
        return cp.add_derived_pars(cosmo_new)
    else:
        raise ValueError('undefined parameter in cosmology \'' +
                         str(parameter) + '\'')
def test_power_agreement():
    """test agreement of powers extracted in two different cosmological parametrizations"""
    cosmo_base = defaults.cosmology_wmap.copy()
    cosmo_base = cp.add_derived_pars(cosmo_base, 'jdem')
    cosmo_base['de_model'] = 'constant_w'
    cosmo_base['w'] = -1.
    power_params = defaults.power_params.copy()
    power_params.camb['maxkh'] = 3.
    power_params.camb['kmax'] = 10.
    power_params.camb['npoints'] = 1000
    power_params.camb['accuracy'] = 2
    power_params.camb['leave_h'] = False

    cosmo_jdem = cosmo_base.copy()
    cosmo_jdem['p_space'] = 'jdem'
    C_fid_jdem = cp.CosmoPie(cosmo_jdem, 'jdem')
    P_jdem = mps.MatterPower(C_fid_jdem, power_params.copy())
    C_fid_jdem.set_power(P_jdem)

    cosmo_lihu = cosmo_base.copy()
    cosmo_lihu['p_space'] = 'lihu'
    C_fid_lihu = cp.CosmoPie(cosmo_lihu, 'lihu')
    P_lihu = mps.MatterPower(C_fid_lihu, power_params.copy())
    C_fid_lihu.set_power(P_lihu)

    jdem_pars = np.array(['ns', 'Omegamh2', 'Omegabh2', 'OmegaLh2', 'LogAs'])
    jdem_eps = np.array([0.002, 0.00025, 0.0001, 0.00025, 0.1])
    C_pert_jdem = ppr.get_perturbed_cosmopies(C_fid_jdem, jdem_pars, jdem_eps)

    lihu_pars = np.array(['ns', 'Omegach2', 'Omegabh2', 'h', 'LogAs'])
    lihu_eps = np.array([0.002, 0.00025, 0.0001, 0.00025, 0.1])
    C_pert_lihu = ppr.get_perturbed_cosmopies(C_fid_lihu, lihu_pars, lihu_eps)

    response_pars = np.array(
        ['Omegach2', 'Omegabh2', 'Omegamh2', 'OmegaLh2', 'h'])
    response_derivs_jdem = np.zeros((response_pars.size, 3))
    response_derivs_jdem_pred = np.array(
        [[1., 0., 1., 0., 1. / (2. * C_fid_jdem.cosmology['h'])],
         [-1., 1., 0., 0., 0.],
         [0., 0., 0., 1., 1. / (2. * C_fid_jdem.cosmology['h'])]]).T
    response_derivs_lihu = np.zeros((response_pars.size, 3))
    response_derivs_lihu_pred = np.array(
        [[1., 0., 1., -1., 0.], [0., 1., 1., -1., 0.],
         [0., 0., 0., 2. * C_fid_lihu.cosmology['h'], 1.]]).T
    for i in range(0, response_pars.size):
        for j in range(1, 4):
            response_derivs_jdem[i, j - 1] = (
                C_pert_jdem[j, 0].cosmology[response_pars[i]] -
                C_pert_jdem[j, 1].cosmology[response_pars[i]]) / (jdem_eps[j] *
                                                                  2.)
            response_derivs_lihu[i, j - 1] = (
                C_pert_lihu[j, 0].cosmology[response_pars[i]] -
                C_pert_lihu[j, 1].cosmology[response_pars[i]]) / (lihu_eps[j] *
                                                                  2.)
    assert np.allclose(response_derivs_jdem_pred, response_derivs_jdem)
    assert np.allclose(response_derivs_lihu_pred, response_derivs_lihu)

    power_derivs_jdem = np.zeros((3, C_fid_jdem.k.size))
    power_derivs_lihu = np.zeros((3, C_fid_lihu.k.size))

    for pmodel in ['linear', 'fastpt', 'halofit']:
        for j in range(1, 4):
            power_derivs_jdem[
                j - 1] = (C_pert_jdem[j, 0].P_lin.get_matter_power(
                    [0.], pmodel=pmodel)[:, 0] -
                          C_pert_jdem[j, 1].P_lin.get_matter_power(
                              [0.], pmodel=pmodel)[:, 0]) / (jdem_eps[j] * 2.)
            power_derivs_lihu[
                j - 1] = (C_pert_lihu[j, 0].P_lin.get_matter_power(
                    [0.], pmodel=pmodel)[:, 0] -
                          C_pert_lihu[j, 1].P_lin.get_matter_power(
                              [0.], pmodel=pmodel)[:, 0]) / (lihu_eps[j] * 2.)

        assert np.allclose((power_derivs_jdem[1] + power_derivs_jdem[0] -
                            power_derivs_jdem[2]),
                           power_derivs_lihu[1],
                           rtol=1.e-2,
                           atol=1.e-4 * np.max(np.abs(power_derivs_lihu[1])))
        assert np.allclose((power_derivs_jdem[0] - power_derivs_jdem[2]),
                           power_derivs_lihu[0],
                           rtol=1.e-2,
                           atol=1.e-4 * np.max(np.abs(power_derivs_lihu[0])))
        assert np.allclose(power_derivs_jdem[2] * 2 *
                           C_fid_lihu.cosmology['h'],
                           power_derivs_lihu[2],
                           rtol=1.e-2,
                           atol=1.e-4 * np.max(np.abs(power_derivs_lihu[2])))
def test_agreement_with_sigma8():
    """test sigma8 works basic to jdem"""
    cosmo_base = defaults.cosmology_wmap.copy()
    cosmo_base = cp.add_derived_pars(cosmo_base, 'jdem')
    cosmo_base['de_model'] = 'constant_w'
    cosmo_base['w'] = -1.
    cosmo_base['sigma8'] = 0.7925070693605805
    power_params = defaults.power_params.copy()
    power_params.camb['maxkh'] = 3.
    power_params.camb['kmax'] = 10.
    power_params.camb['npoints'] = 1000
    power_params.camb['accuracy'] = 2
    power_params.camb['leave_h'] = False
    power_params_jdem = deepcopy(power_params)
    power_params_jdem.camb['force_sigma8'] = False
    power_params_basi = deepcopy(power_params)
    power_params_basi.camb['force_sigma8'] = True

    cosmo_jdem = cosmo_base.copy()
    cosmo_jdem['p_space'] = 'jdem'
    C_fid_jdem = cp.CosmoPie(cosmo_jdem, 'jdem')
    P_jdem = mps.MatterPower(C_fid_jdem, power_params_jdem)
    C_fid_jdem.set_power(P_jdem)

    cosmo_basi = cosmo_base.copy()
    cosmo_basi['p_space'] = 'basic'
    C_fid_basi = cp.CosmoPie(cosmo_basi, 'basic')
    P_basi = mps.MatterPower(C_fid_basi, power_params_basi)
    C_fid_basi.set_power(P_basi)

    zs = np.arange(0.2, 1.41, 0.40)
    z_fine = np.linspace(0.001, 1.4, 1000)

    geo_jdem = FullSkyGeo(zs, C_fid_jdem, z_fine)
    geo_basi = FullSkyGeo(zs, C_fid_basi, z_fine)

    jdem_pars = np.array(
        ['ns', 'Omegamh2', 'Omegabh2', 'OmegaLh2', 'LogAs', 'w'])
    jdem_eps = np.array([0.002, 0.00025, 0.0001, 0.00025, 0.01, 0.01])

    basi_pars = np.array(['ns', 'Omegamh2', 'Omegabh2', 'h', 'sigma8', 'w'])
    basi_eps = np.array([0.002, 0.00025, 0.0001, 0.00025, 0.001, 0.01])

    sw_params = defaults.sw_survey_params.copy()
    len_params = defaults.lensing_params.copy()
    sw_observable_list = defaults.sw_observable_list.copy()
    nz_wfirst_lens = NZWFirstEff(defaults.nz_params_wfirst_lens.copy())
    prior_params = defaults.prior_fisher_params.copy()
    basis_params = defaults.basis_params.copy()

    sw_survey_jdem = sws.SWSurvey(geo_jdem, 'wfirst', C_fid_jdem, sw_params,
                                  jdem_pars, jdem_eps, sw_observable_list,
                                  len_params, nz_wfirst_lens)

    sw_survey_basi = sws.SWSurvey(geo_basi, 'wfirst', C_fid_basi, sw_params,
                                  basi_pars, basi_eps, sw_observable_list,
                                  len_params, nz_wfirst_lens)
    #need to fix As because the code cannot presently do this
    for itr in range(0, basi_pars.size):
        for i in range(0, 2):
            cosmo_alt_basi = sw_survey_basi.len_pow.Cs_pert[
                itr, i].cosmology.copy()
            n_As = 10
            logAs = np.linspace(cosmo_alt_basi['LogAs'] * 0.9,
                                cosmo_alt_basi['LogAs'] * 1.1, n_As)
            As = np.exp(logAs)
            sigma8s = np.zeros(n_As)
            for itr2 in xrange(0, n_As):
                cosmo_alt_basi['As'] = As[itr2]
                cosmo_alt_basi['LogAs'] = logAs[itr2]
                sigma8s[itr2] = camb_sigma8(cosmo_alt_basi,
                                            power_params_basi.camb)
            logAs_interp = InterpolatedUnivariateSpline(sigma8s[::-1],
                                                        logAs[::-1],
                                                        ext=2,
                                                        k=3)

            sw_survey_basi.len_pow.Cs_pert[
                itr, i].cosmology['LogAs'] = logAs_interp(
                    sw_survey_basi.len_pow.Cs_pert[itr, i].cosmology['sigma8'])
            sw_survey_basi.len_pow.Cs_pert[itr, i].cosmology['As'] = np.exp(
                sw_survey_basi.len_pow.Cs_pert[itr, i].cosmology['LogAs'])

    dO_dpar_jdem = sw_survey_jdem.get_dO_I_dpar_array()
    dO_dpar_basi = sw_survey_basi.get_dO_I_dpar_array()

    response_pars = np.array([
        'ns', 'Omegach2', 'Omegabh2', 'Omegamh2', 'OmegaLh2', 'h', 'LogAs',
        'w', 'sigma8'
    ])

    l_max = 24

    r_max_jdem = geo_jdem.r_fine[-1]
    k_cut_jdem = 30. / r_max_jdem
    basis_jdem = SphBasisK(r_max_jdem,
                           C_fid_jdem,
                           k_cut_jdem,
                           basis_params,
                           l_ceil=l_max,
                           needs_m=True)
    SS_jdem = SuperSurvey(np.array([sw_survey_jdem]),
                          np.array([]),
                          basis_jdem,
                          C_fid_jdem,
                          prior_params,
                          get_a=False,
                          do_unmitigated=True,
                          do_mitigated=False)

    r_max_basi = geo_basi.r_fine[-1]
    k_cut_basi = 30. / r_max_basi
    basis_basi = SphBasisK(r_max_basi,
                           C_fid_basi,
                           k_cut_basi,
                           basis_params,
                           l_ceil=l_max,
                           needs_m=True)
    SS_basi = SuperSurvey(np.array([sw_survey_basi]),
                          np.array([]),
                          basis_basi,
                          C_fid_basi,
                          prior_params,
                          get_a=False,
                          do_unmitigated=True,
                          do_mitigated=False)

    #dO_dpar_jdem_to_basi = np.zeros_like(dO_dpar_jdem)
    #dO_dpar_basi_to_jdem = np.zeros_like(dO_dpar_basi)

    project_basi_to_jdem = np.zeros((jdem_pars.size, basi_pars.size))

    response_derivs_jdem = np.zeros((response_pars.size, jdem_pars.size))
    response_derivs_basi = np.zeros((response_pars.size, basi_pars.size))
    for i in range(0, response_pars.size):
        for j in range(0, jdem_pars.size):
            response_derivs_jdem[i, j] = (
                sw_survey_jdem.len_pow.Cs_pert[j,
                                               0].cosmology[response_pars[i]] -
                sw_survey_jdem.len_pow.Cs_pert[j, 1].cosmology[
                    response_pars[i]]) / (jdem_eps[j] * 2.)
            response_derivs_basi[i, j] = (
                sw_survey_basi.len_pow.Cs_pert[j,
                                               0].cosmology[response_pars[i]] -
                sw_survey_basi.len_pow.Cs_pert[j, 1].cosmology[
                    response_pars[i]]) / (basi_eps[j] * 2.)

    project_jdem_to_basi = np.zeros((basi_pars.size, jdem_pars.size))
    project_basi_to_jdem = np.zeros((jdem_pars.size, basi_pars.size))
    for itr1 in range(0, basi_pars.size):
        for itr2 in range(0, response_pars.size):
            if response_pars[itr2] in jdem_pars:
                name = response_pars[itr2]
                i = np.argwhere(jdem_pars == name)[0, 0]
                project_jdem_to_basi[itr1, i] = response_derivs_basi[itr2,
                                                                     itr1]
    for itr1 in range(0, jdem_pars.size):
        for itr2 in range(0, response_pars.size):
            if response_pars[itr2] in basi_pars:
                name = response_pars[itr2]
                i = np.argwhere(basi_pars == name)[0, 0]
                project_basi_to_jdem[itr1, i] = response_derivs_jdem[itr2,
                                                                     itr1]
    assert np.allclose(np.dot(dO_dpar_jdem, project_jdem_to_basi.T),
                       dO_dpar_basi,
                       rtol=1.e-3,
                       atol=np.max(dO_dpar_basi) * 1.e-4)
    assert np.allclose(np.dot(dO_dpar_basi, project_basi_to_jdem.T),
                       dO_dpar_jdem,
                       rtol=1.e-3,
                       atol=np.max(dO_dpar_jdem) * 1.e-4)

    #basi p_space cannot currently do priors by itself
    f_p_priors_basi = np.dot(
        project_jdem_to_basi,
        np.dot(SS_jdem.multi_f.fisher_priors.get_fisher(),
               project_jdem_to_basi.T))

    for i in range(0, 1):
        f_np_jdem = SS_jdem.f_set_nopriors[i][2].get_fisher().copy()
        f_np_basi = SS_basi.f_set_nopriors[i][2].get_fisher().copy()
        f_np_jdem_to_basi = np.dot(project_jdem_to_basi,
                                   np.dot(f_np_jdem, project_jdem_to_basi.T))
        f_np_basi_to_jdem = np.dot(project_basi_to_jdem,
                                   np.dot(f_np_basi, project_basi_to_jdem.T))
        assert np.allclose(f_np_jdem_to_basi, f_np_basi, rtol=1.e-2)
        assert np.allclose(f_np_basi_to_jdem, f_np_jdem, rtol=1.e-2)

        f_p_jdem = SS_jdem.f_set[i][2].get_fisher().copy()
        f_p_basi = SS_basi.f_set_nopriors[i][2].get_fisher().copy(
        ) + f_p_priors_basi.copy()
        f_p_jdem_to_basi = np.dot(project_jdem_to_basi,
                                  np.dot(f_p_jdem, project_jdem_to_basi.T))
        f_p_basi_to_jdem = np.dot(project_basi_to_jdem,
                                  np.dot(f_p_basi, project_basi_to_jdem.T))
        assert np.allclose(f_p_jdem_to_basi, f_p_basi, rtol=1.e-2)
        assert np.allclose(f_p_basi_to_jdem, f_p_jdem, rtol=1.e-2)
    print(f_np_jdem / f_np_basi_to_jdem)
    print(f_np_basi / f_np_jdem_to_basi)
def test_pipeline_consistency():
    """test full pipeline consistency with rotation jdem vs lihu"""
    cosmo_base = defaults.cosmology_wmap.copy()
    cosmo_base = cp.add_derived_pars(cosmo_base, 'jdem')
    cosmo_base['de_model'] = 'constant_w'
    cosmo_base['w'] = -1.
    power_params = defaults.power_params.copy()
    power_params.camb['maxkh'] = 1.
    power_params.camb['kmax'] = 1.
    power_params.camb['npoints'] = 1000
    power_params.camb['accuracy'] = 2
    power_params.camb['leave_h'] = False

    cosmo_jdem = cosmo_base.copy()
    cosmo_jdem['p_space'] = 'jdem'
    C_fid_jdem = cp.CosmoPie(cosmo_jdem, 'jdem')
    P_jdem = mps.MatterPower(C_fid_jdem, power_params.copy())
    C_fid_jdem.set_power(P_jdem)

    cosmo_lihu = cosmo_base.copy()
    cosmo_lihu['p_space'] = 'lihu'
    C_fid_lihu = cp.CosmoPie(cosmo_lihu, 'lihu')
    P_lihu = mps.MatterPower(C_fid_lihu, power_params.copy())
    C_fid_lihu.set_power(P_lihu)

    zs = np.arange(0.2, 1.41, 0.40)
    z_fine = np.linspace(0.001, 1.4, 1000)

    geo_jdem = FullSkyGeo(zs, C_fid_jdem, z_fine)
    geo_lihu = FullSkyGeo(zs, C_fid_lihu, z_fine)

    jdem_pars = np.array(
        ['ns', 'Omegamh2', 'Omegabh2', 'OmegaLh2', 'LogAs', 'w'])
    jdem_eps = np.array([0.002, 0.00025, 0.0001, 0.00025, 0.1, 0.01])

    lihu_pars = np.array(['ns', 'Omegach2', 'Omegabh2', 'h', 'LogAs', 'w'])
    lihu_eps = np.array([0.002, 0.00025, 0.0001, 0.00025, 0.1, 0.01])

    sw_params = defaults.sw_survey_params.copy()
    len_params = defaults.lensing_params.copy()
    sw_observable_list = defaults.sw_observable_list.copy()
    nz_wfirst_lens = NZWFirstEff(defaults.nz_params_wfirst_lens.copy())
    prior_params = defaults.prior_fisher_params.copy()
    basis_params = defaults.basis_params.copy()

    sw_survey_jdem = sws.SWSurvey(geo_jdem, 'wfirst', C_fid_jdem, sw_params,
                                  jdem_pars, jdem_eps, sw_observable_list,
                                  len_params, nz_wfirst_lens)
    sw_survey_lihu = sws.SWSurvey(geo_lihu, 'wfirst', C_fid_lihu, sw_params,
                                  lihu_pars, lihu_eps, sw_observable_list,
                                  len_params, nz_wfirst_lens)

    #dO_dpar_jdem = sw_survey_jdem.get_dO_I_dpar_array()
    #dO_dpar_lihu = sw_survey_lihu.get_dO_I_dpar_array()

    response_pars = np.array([
        'ns', 'Omegach2', 'Omegabh2', 'Omegamh2', 'OmegaLh2', 'h', 'LogAs', 'w'
    ])
    response_derivs_jdem_pred = np.array(
        [[1., 0., 0., 0., 0., 0., 0., 0.],
         [0., 1., 0., 1., 0., 1. / (2. * C_fid_jdem.cosmology['h']), 0., 0.],
         [0., -1., 1., 0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 1., 1. / (2. * C_fid_jdem.cosmology['h']), 0., 0.],
         [0., 0., 0., 0., 0., 0., 1., 0.], [0., 0., 0., 0., 0., 0., 0., 1.]]).T
    response_derivs_lihu_pred = np.array(
        [[1., 0., 0., 0., 0., 0., 0., 0.], [0., 1., 0., 1., -1., 0., 0., 0.],
         [0., 0., 1., 1., -1., 0., 0., 0.],
         [0., 0., 0., 0., 2. * C_fid_lihu.cosmology['h'], 1., 0., 0.],
         [0., 0., 0., 0., 0., 0., 1., 0.], [0., 0., 0., 0., 0., 0., 0., 1.]]).T

    l_max = 24

    r_max_jdem = geo_jdem.r_fine[-1]
    k_cut_jdem = 30. / r_max_jdem
    basis_jdem = SphBasisK(r_max_jdem,
                           C_fid_jdem,
                           k_cut_jdem,
                           basis_params,
                           l_ceil=l_max,
                           needs_m=True)
    SS_jdem = SuperSurvey(np.array([sw_survey_jdem]),
                          np.array([]),
                          basis_jdem,
                          C_fid_jdem,
                          prior_params,
                          get_a=False,
                          do_unmitigated=True,
                          do_mitigated=False)

    r_max_lihu = geo_lihu.r_fine[-1]
    k_cut_lihu = 30. / r_max_lihu
    basis_lihu = SphBasisK(r_max_lihu,
                           C_fid_lihu,
                           k_cut_lihu,
                           basis_params,
                           l_ceil=l_max,
                           needs_m=True)
    SS_lihu = SuperSurvey(np.array([sw_survey_lihu]),
                          np.array([]),
                          basis_lihu,
                          C_fid_lihu,
                          prior_params,
                          get_a=False,
                          do_unmitigated=True,
                          do_mitigated=False)

    #dO_dpar_jdem_to_lihu = np.zeros_like(dO_dpar_jdem)
    #dO_dpar_lihu_to_jdem = np.zeros_like(dO_dpar_lihu)

    project_lihu_to_jdem = np.zeros((jdem_pars.size, lihu_pars.size))

    #f_g_jdem_to_lihu = np.zeros((lihu_pars.size,lihu_pars.size))
    #f_g_lihu_to_jdem = np.zeros((jdem_pars.size,jdem_pars.size))
    response_derivs_jdem = np.zeros((response_pars.size, jdem_pars.size))
    response_derivs_lihu = np.zeros((response_pars.size, lihu_pars.size))
    for i in range(0, response_pars.size):
        for j in range(0, jdem_pars.size):
            response_derivs_jdem[i, j] = (
                sw_survey_jdem.len_pow.Cs_pert[j,
                                               0].cosmology[response_pars[i]] -
                sw_survey_jdem.len_pow.Cs_pert[j, 1].cosmology[
                    response_pars[i]]) / (jdem_eps[j] * 2.)
            response_derivs_lihu[i, j] = (
                sw_survey_lihu.len_pow.Cs_pert[j,
                                               0].cosmology[response_pars[i]] -
                sw_survey_lihu.len_pow.Cs_pert[j, 1].cosmology[
                    response_pars[i]]) / (lihu_eps[j] * 2.)
    assert np.allclose(response_derivs_jdem, response_derivs_jdem_pred)
    assert np.allclose(response_derivs_lihu, response_derivs_lihu_pred)

    project_jdem_to_lihu = np.zeros((lihu_pars.size, jdem_pars.size))
    project_lihu_to_jdem = np.zeros((jdem_pars.size, lihu_pars.size))
    for itr1 in range(0, lihu_pars.size):
        for itr2 in range(0, response_pars.size):
            if response_pars[itr2] in jdem_pars:
                name = response_pars[itr2]
                i = np.argwhere(jdem_pars == name)[0, 0]
                project_jdem_to_lihu[itr1, i] = response_derivs_lihu[itr2,
                                                                     itr1]
    for itr1 in range(0, jdem_pars.size):
        for itr2 in range(0, response_pars.size):
            if response_pars[itr2] in lihu_pars:
                name = response_pars[itr2]
                i = np.argwhere(lihu_pars == name)[0, 0]
                project_lihu_to_jdem[itr1, i] = response_derivs_jdem[itr2,
                                                                     itr1]
    #assert np.allclose(np.dot(dO_dpar_jdem,project_jdem_to_lihu.T),dO_dpar_lihu,rtol=1.e-3,atol=np.max(dO_dpar_lihu)*1.e-4)
    #assert np.allclose(np.dot(dO_dpar_lihu,project_lihu_to_jdem.T),dO_dpar_jdem,rtol=1.e-3,atol=np.max(dO_dpar_jdem)*1.e-4)

    #lihu p_space cannot currently do priors by itself
    f_p_priors_lihu = np.dot(
        project_jdem_to_lihu,
        np.dot(SS_jdem.multi_f.fisher_priors.get_fisher(),
               project_jdem_to_lihu.T))

    f_set_jdem_in = np.zeros(3, dtype=object)
    f_set_lihu_in = np.zeros(3, dtype=object)
    for i in range(0, 3):
        f_set_jdem_in[i] = SS_jdem.f_set_nopriors[i][2].get_fisher().copy()
        f_set_lihu_in[i] = SS_lihu.f_set_nopriors[i][2].get_fisher().copy()

    f_np_lihu2 = rotate_jdem_to_lihu(f_set_jdem_in, C_fid_jdem)
    f_np_jdem2 = rotate_lihu_to_jdem(f_set_lihu_in, C_fid_lihu)
    f_np_lihu3 = rotate_jdem_to_lihu(f_np_jdem2, C_fid_jdem)
    f_np_jdem3 = rotate_lihu_to_jdem(f_np_lihu2, C_fid_lihu)

    for i in range(0, 3):
        f_np_jdem = SS_jdem.f_set_nopriors[i][2].get_fisher().copy()
        f_np_lihu = SS_lihu.f_set_nopriors[i][2].get_fisher().copy()
        f_np_jdem_to_lihu = np.dot(project_jdem_to_lihu,
                                   np.dot(f_np_jdem, project_jdem_to_lihu.T))
        f_np_lihu_to_jdem = np.dot(project_lihu_to_jdem,
                                   np.dot(f_np_lihu, project_lihu_to_jdem.T))
        assert np.allclose(f_set_lihu_in[i], f_np_lihu3[i])
        assert np.allclose(f_set_jdem_in[i], f_np_jdem3[i])
        assert np.allclose(f_np_jdem_to_lihu, f_np_lihu2[i])
        assert np.allclose(f_np_lihu_to_jdem, f_np_jdem2[i])
        assert np.allclose(f_np_jdem_to_lihu, f_np_lihu, rtol=1.e-3)
        assert np.allclose(f_np_lihu_to_jdem, f_np_jdem, rtol=1.e-3)
        assert np.allclose(f_set_lihu_in[i], f_np_lihu2[i], rtol=1.e-3)
        assert np.allclose(f_set_jdem_in[i], f_np_jdem2[i], rtol=1.e-3)
        assert np.allclose(f_np_jdem3[i], f_np_jdem2[i], rtol=1.e-3)
        assert np.allclose(f_np_lihu3[i], f_np_lihu2[i], rtol=1.e-3)
        f_p_jdem = SS_jdem.f_set[i][2].get_fisher().copy()
        f_p_lihu = SS_lihu.f_set_nopriors[i][2].get_fisher().copy(
        ) + f_p_priors_lihu.copy()
        f_p_jdem_to_lihu = np.dot(project_jdem_to_lihu,
                                  np.dot(f_p_jdem, project_jdem_to_lihu.T))
        f_p_lihu_to_jdem = np.dot(project_lihu_to_jdem,
                                  np.dot(f_p_lihu, project_lihu_to_jdem.T))
        assert np.allclose(f_p_jdem_to_lihu, f_p_lihu, rtol=1.e-3)
        assert np.allclose(f_p_lihu_to_jdem, f_p_jdem, rtol=1.e-3)
示例#5
0
def test_hmf():
    """run various hmf tests as a block"""
    cosmo_fid = defaults.cosmology.copy()

    cosmo_fid['h'] = 0.65
    cosmo_fid['Omegamh2'] = 0.148
    cosmo_fid['Omegabh2'] = 0.02
    cosmo_fid['OmegaLh2'] = 0.65 * 0.65**2
    cosmo_fid['sigma8'] = 0.92
    cosmo_fid['ns'] = 1.
    cosmo_fid = cp.add_derived_pars(cosmo_fid, p_space='basic')
    power_params = defaults.power_params.copy()
    power_params.camb['force_sigma8'] = True
    power_params.camb['npoints'] = 1000
    power_params.camb['maxkh'] = 20000.
    power_params.camb['kmax'] = 100.  #0.899999976158
    power_params.camb['accuracy'] = 2.
    C = cp.CosmoPie(cosmo_fid, 'basic')
    P = mps.MatterPower(C, power_params)
    C.set_power(P)
    params = defaults.hmf_params.copy()
    params['z_min'] = 0.0
    params['z_max'] = 5.0
    params['log10_min_mass'] = 6
    params['log10_max_mass'] = 18.63
    params['n_grid'] = 1264
    params['n_z'] = 5. / 0.5
    hmf = ST_hmf(C, params=params)
    Ms = hmf.mass_grid

    do_sanity_checks = True
    if do_sanity_checks:
        print("sanity")
        #some sanity checks
        zs = np.arange(0.001, 5., 0.1)
        Gs = C.G_norm(zs)

        #arbitrary input M(z) cutoff
        m_z_in = np.exp(np.linspace(np.log(np.min(Ms)), np.log(1.e15),
                                    zs.size))
        m_z_in[0] = Ms[0]

        #check normalized to unity (all dark matter is in some halo)

        #        f_norm_residual = trapz(hmf.f_sigma(Ms,Gs).T,np.log(hmf.sigma[:-1:]**-1),axis=1)
        #assert np.allclose(np.zeros(Gs.size)+1.,norm_residual)
        _, _, dndM = hmf.mass_func(Ms, Gs)
        n_avgs_alt = np.zeros(zs.size)
        bias_n_avgs_alt = np.zeros(zs.size)
        dndM_G_alt = np.zeros((Ms.size, zs.size))
        for i in range(0, zs.size):
            n_avgs_alt[i] = hmf.n_avg(m_z_in[i], zs[i])
            bias_n_avgs_alt[i] = hmf.bias_n_avg(m_z_in[i], zs[i])
            dndM_G_alt[:, i] = hmf.dndM_G(Ms, Gs[i])
        dndM_G = hmf.dndM_G(Ms, Gs)
        #consistency checks for vector method
        assert np.allclose(dndM_G, dndM_G_alt)
        n_avgs = hmf.n_avg(m_z_in, zs)
        bias_n_avgs = hmf.bias_n_avg(m_z_in, zs)
        assert np.allclose(n_avgs, n_avgs_alt)
        assert np.all(n_avgs >= 0.)
        assert np.allclose(bias_n_avgs, bias_n_avgs_alt)
        #check integrating dn/dM over all M actually gives n
        assert np.allclose(trapz(dndM.T, Ms, axis=1),
                           hmf.n_avg(np.zeros(zs.size) + Ms[0], zs))
        test_xs = np.outer(hmf.nu_of_M(Ms), 1. / Gs**2)
        test_integrand = hmf.f_sigma(Ms, Gs) * hmf.bias_G(
            Ms, Gs, hmf.bias_norm(Gs))
        #not sure why, but this is true (if ignore h)
        #test_term = np.trapz(test_integrand,test_xs,axis=0)*hmf.f_norm(Gs)
        #assert np.allclose(1.,test_term,rtol=1e-3)
        b_norm_residual = np.trapz(test_integrand, test_xs, axis=0)
        assert np.allclose(np.zeros(zs.size) + 1., b_norm_residual)
        #b_norm_residual_alt = np.trapz(hmf.f_sigma(Ms,Gs)*hmf.bias_G(Ms,Gs),test_xs,axis=0)
        b_norm_residual_alt2 = np.trapz(hmf.f_sigma(Ms, Gs) *
                                        hmf.bias_G(Ms, Gs, 1.),
                                        test_xs,
                                        axis=0)
        #check including norm_in behaves appropriately does not matter
        #assert np.allclose(b_norm_residual_alt,b_norm_residual_alt2)
        assert np.allclose(b_norm_residual,
                           b_norm_residual_alt2 / hmf.bias_norm(Gs))
        #hcekc all the matter in a halo if include normalization factor
        assert np.allclose(
            np.trapz(hmf.f_sigma(Ms, 1., 1.), np.log(1. / hmf.sigma[:-1:])),
            hmf.f_norm(1.))

        #sanity check M_star
        assert np.round(np.log10(hmf.M_star())) == 13.
        assert np.isclose(C.sigma_r(0., 8. / C.h),
                          cosmo_fid['sigma8'],
                          rtol=1.e-3)
        assert np.isclose(C.sigma_r(1., 8. / C.h),
                          C.G_norm(1.) * cosmo_fid['sigma8'],
                          rtol=1.e-3)

        #M_restrict = 10**np.linspace(np.log10(4.*10**13),16,100)
        #n_restrict = hmf.n_avg(M_restrict,0.)
        nu = hmf.nu_of_M(Ms)
        bias_nu = hmf.bias_nu(nu)
        bias_G = hmf.bias_G(Ms, 1.)
        bias_z = hmf.bias(Ms, 0.)
        assert np.allclose(bias_nu, bias_G)
        assert np.allclose(bias_nu, bias_z)
        assert np.allclose(bias_G, bias_z)
        assert np.all(bias_nu >= 0)
        #        dndm = hmf.dndM_G(Ms,1.)
        #        bias_avg = np.trapz(bias_nu*dndm,Ms)

        n_avg2 = hmf.n_avg(Ms, 0.)
        assert np.all(n_avg2 >= 0.)
        #        bias_n_avg1 = bias_nu*n_avg2
        #bias_n_avg2 = hmf.bias_n_avg(Ms)

        #        integ_pred = np.trapz(hmf.f_sigma(Ms,1.,1.),np.log(1./hmf.sigma[:-1:]))
        #        integ_res = np.trapz(Ms*hmf.dndM_G(Ms,1.),Ms)/C.rho_bar(0.)
        #assert np.isclose(integ_res,integ_pred,rtol=1.e-2)
        #xs n#np.linspace(np.log(np.sqrt(nu[0])),np.log(nu[-1]),10000)
        #xs = np.exp(np.linspace(np.log(np.sqrt(nu[0]),np.log(1.e36),10000))
        #F = -cumtrapz(1./np.sqrt(2.*np.pi)*np.exp(-xs[::-1]**2/2.),xs[::-1])[::-1]
        #cons_res = np.trapz(hmf.dndM_G(Ms,1.)*Ms*hmf.bias(Ms,1.),Ms)/C.rho_bar(0.)
        assert np.isclose(np.trapz(bias_nu * hmf.f_nu(nu), np.sqrt(nu)),
                          1.,
                          rtol=1.e-1)
        #assert np.isclose(np.trapz(hmf.f_nu(nu)/np.sqrt(nu),np.sqrt(nu)),1.,rtol=2.e-1)

        #bias_avg = np.trapz(hmf.dndM_G(Ms,1.)*hmf.bias_G(Ms,1.),Ms)/np.trapz(hmf.dndM_G(Ms,1.),Ms)
        #check for various edge cases of n_avg and bias_n_avg
        z2s = np.array([0., 1.])
        m2s = np.array([1.e8, 1.e9])
        n1 = hmf.n_avg(m2s[0], z2s[0])
        n2 = hmf.n_avg(m2s[0], z2s)
        n3 = hmf.n_avg(m2s, z2s[0])
        n4 = hmf.n_avg(m2s, z2s)
        n5 = hmf.n_avg(m2s[1], z2s[1])
        n6 = hmf.n_avg(m2s[1], z2s[0])
        n7 = hmf.n_avg(m2s[0], z2s[1])
        assert np.isclose(n1, n2[0])
        assert np.isclose(n1, n3[0])
        assert np.isclose(n1, n4[0])
        assert np.isclose(n5, n4[1])
        assert np.isclose(n2[1], n7)
        assert np.isclose(n3[1], n6)
        bn1 = hmf.bias_n_avg(m2s[0], z2s[0])
        bn2 = hmf.bias_n_avg(m2s[0], z2s)
        bn3 = hmf.bias_n_avg(m2s, z2s[0])
        bn4 = hmf.bias_n_avg(m2s, z2s)
        bn5 = hmf.bias_n_avg(m2s[1], z2s[1])
        bn6 = hmf.bias_n_avg(m2s[1], z2s[0])
        bn7 = hmf.bias_n_avg(m2s[0], z2s[1])
        assert np.isclose(bn1, bn2[0])
        assert np.isclose(bn1, bn3[0])
        assert np.isclose(bn1, bn4[0])
        assert np.isclose(bn5, bn4[1])
        assert np.isclose(bn2[1], bn7)
        assert np.isclose(bn3[1], bn6)

        n_avgs_0 = hmf.n_avg(hmf.mass_grid, 0.)
        n_avgs_1 = np.zeros(hmf.mass_grid.size)
        for itr in range(0, hmf.mass_grid.size):
            n_avgs_1[itr] = hmf.n_avg(hmf.mass_grid[itr], 0.)
        assert np.allclose(n_avgs_0, n_avgs_1)

        bn_avgs_0 = hmf.bias_n_avg(hmf.mass_grid, 0.)
        bn_avgs_1 = np.zeros(hmf.mass_grid.size)
        for itr in range(0, hmf.mass_grid.size):
            bn_avgs_1[itr] = hmf.bias_n_avg(hmf.mass_grid[itr], 0.)
        assert np.allclose(bn_avgs_0, bn_avgs_1)

        print("PASS: sanity passed")

    do_plot_test2 = True
    if do_plot_test2:
        #Ms = 10**(np.linspace(11,14,500))
        # dndM_G=hmf.dndM_G(Ms,Gs)
        do_jenkins_comp = True
        #should look like dotted line in figure 3 of    arXiv:astro-ph/0005260
        if do_jenkins_comp:
            print("jenkins_comp")
            zs = np.array([0.])
            Gs = C.G_norm(zs)
            dndM_G = hmf.f_sigma(Ms, Gs)
            input_dndm = np.loadtxt('test_inputs/hmf/dig_jenkins_fig3.csv',
                                    delimiter=',')
            res_j = np.exp(input_dndm[:, 1])
            res_i = InterpolatedUnivariateSpline(1. / hmf.sigma[:-1:],
                                                 dndM_G,
                                                 k=3,
                                                 ext=2)(np.exp(input_dndm[:,
                                                                          0]))
            assert np.all(np.abs((res_j / res_i - 1.)[0:19]) < 0.03)
            print("PASS: jenkins_comp")

        do_sheth_bias_comp1 = True
        #agrees pretty well, maybe as well as it should
        #compare to rightmost arXiv:astro-ph/9901122 figure 3
        if do_sheth_bias_comp1:
            print("sheth_bias_comp1")
            cosmo_fid2 = cosmo_fid.copy()
            cosmo_fid2['Omegamh2'] = 0.3 * 0.7**2
            cosmo_fid2['Omegabh2'] = 0.05 * 0.7**2
            cosmo_fid2['OmegaLh2'] = 0.7 * 0.7**2
            cosmo_fid2['sigma8'] = 0.9
            cosmo_fid2['h'] = 0.7
            cosmo_fid2['ns'] = 1.0
            cosmo_fid2 = cp.add_derived_pars(cosmo_fid2, p_space='basic')
            C2 = cp.CosmoPie(cosmo_fid2, 'basic')
            P2 = mps.MatterPower(C2, power_params)
            C2.set_power(P2)
            params2 = params.copy()
            hmf2 = ST_hmf(C2, params=params2)
            zs = np.array([0., 1., 2., 4.])
            Gs = C2.G_norm(zs)
            input_bias = np.loadtxt('test_inputs/hmf/dig_sheth_fig3.csv',
                                    delimiter=',')
            bias = hmf2.bias_G(10**input_bias[:, 0], Gs)
            error = np.abs(1. - 10**input_bias[:, 1:5] / bias**2)
            error_max = np.array([0.06, 0.4, 0.2, 0.4])
            assert np.all(np.max(error, axis=0) < error_max)
            print("PASS: sheth_bias_comp1")

        do_sheth_bias_comp2 = True
        #agrees well
        #compare to bottom arXiv:astro-ph/9901122 figure 4
        if do_sheth_bias_comp2:
            print("sheth_bias_comp2")
            cosmo_fid2 = cosmo_fid.copy()
            cosmo_fid2['Omegamh2'] = 0.3 * 0.7**2
            cosmo_fid2['Omegabh2'] = 0.05 * 0.7**2
            cosmo_fid2['OmegaLh2'] = 0.7 * 0.7**2
            cosmo_fid2['sigma8'] = 0.9
            cosmo_fid2['h'] = 0.7
            cosmo_fid2['ns'] = 1.0
            cosmo_fid2 = cp.add_derived_pars(cosmo_fid2, p_space='basic')
            C2 = cp.CosmoPie(cosmo_fid2, 'basic')
            P2 = mps.MatterPower(C2, power_params)
            C2.set_power(P2)
            C2.k = P2.k
            params2 = params.copy()
            hmf2 = ST_hmf(C2, params=params2)
            input_bias = np.loadtxt('test_inputs/hmf/dig_sheth2.csv',
                                    delimiter=',')
            bias = hmf2.bias_nu(10**input_bias[:, 0])
            observable = 1. + (bias - 1.) * hmf2.delta_c
            error = np.abs(1. - 10**input_bias[:, 1] / observable)
            error_max = 0.07
            assert np.all(np.max(error) < error_max)
            print("PASS: sheth_bias_comp2")

        #should agree, does
        #match fig 2 arXiv:astro-ph/0203169
        do_hu_bias_comp2 = True
        if do_hu_bias_comp2:
            print("hu_bias_comp2")
            zs = np.array([0.])
            Gs = C.G_norm(zs)
            bias = hmf.bias_G(Ms, Gs, 1.)[:, 0]  #why 1
            #maybe should have k pivot 0.01
            input_bias = np.loadtxt('test_inputs/hmf/dig_hu_bias2.csv',
                                    delimiter=',')
            #            masses = 10**np.linspace(np.log10(10**11),np.log10(10**16),100)
            bias_hu_i = 10**input_bias[:,
                                       1]  #InterpolatedUnivariateSpline(10**input_bias[:,0],10**input_bias[:,1])(masses)
            bias_i = hmf.bias_G(10**input_bias[:, 0], Gs, 1.)[:, 0]
            assert np.max(np.abs(bias_hu_i / bias_i - 1.)) < 0.06
            print("PASS: hu_bias_comp2 passed")

        #should agree, does
        #match fig 1 arXiv:astro-ph/0203169
        do_hu_sigma_comp = True
        if do_hu_sigma_comp:
            print("hu_sigma_comp")
            assert np.isclose(hmf.M_star(), 1.2 * 10**13, rtol=1.e-1)
            input_sigma = np.loadtxt('test_inputs/hmf/dig_hu_sigma.csv',
                                     delimiter=',')
            res_sigma = InterpolatedUnivariateSpline(hmf.R,
                                                     hmf.sigma,
                                                     k=3,
                                                     ext=2)(10**input_sigma[:,
                                                                            0])
            assert np.all(
                np.abs((res_sigma / 10**input_sigma[:, 1])[4::] - 1.) < 0.02)
            print("PASS: hu_sigma_comp")