Esempio n. 1
0
def _generate_galaxy_catalog(cluster_m,
                             cluster_z,
                             cluster_c,
                             cosmo,
                             ngals,
                             Delta_SO,
                             zsrc,
                             zsrc_min=0.4,
                             zsrc_max=7.,
                             shapenoise=None,
                             photoz_sigma_unscaled=None):
    """A private function that skips the sanity checks on derived properties. This
    function should only be used when called directly from `generate_galaxy_catalog`.
    Takes the same parameters and returns the same things as the before mentioned function.

    For a more detailed description of each of the parameters, see the documentation of
    `generate_galaxy_catalog`.
    """
    # Set the source galaxy redshifts
    galaxy_catalog = _draw_source_redshifts(zsrc, cluster_z, zsrc_min,
                                            zsrc_max, ngals)

    # Add photo-z errors and pdfs to source galaxy redshifts
    if photoz_sigma_unscaled is not None:
        galaxy_catalog = _compute_photoz_pdfs(galaxy_catalog,
                                              photoz_sigma_unscaled, ngals)

    # Draw galaxy positions
    galaxy_catalog = _draw_galaxy_positions(galaxy_catalog, ngals, cluster_z,
                                            cosmo)

    # Compute the shear on each source galaxy
    gamt = predict_reduced_tangential_shear(galaxy_catalog['r_mpc'],
                                            mdelta=cluster_m,
                                            cdelta=cluster_c,
                                            z_cluster=cluster_z,
                                            z_source=galaxy_catalog['z'],
                                            cosmo=cosmo,
                                            delta_mdef=Delta_SO,
                                            halo_profile_model='nfw',
                                            z_src_model='single_plane')
    galaxy_catalog['gammat'] = gamt

    # Add shape noise to source galaxy shears
    if shapenoise is not None:
        galaxy_catalog['gammat'] += shapenoise * np.random.standard_normal(
            ngals)

    # Compute ellipticities
    galaxy_catalog['posangle'] = np.arctan2(galaxy_catalog['y_mpc'],
                                            galaxy_catalog['x_mpc'])
    galaxy_catalog['e1'] = -galaxy_catalog['gammat'] * np.cos(
        2 * galaxy_catalog['posangle'])
    galaxy_catalog['e2'] = -galaxy_catalog['gammat'] * np.sin(
        2 * galaxy_catalog['posangle'])

    if photoz_sigma_unscaled is not None:
        return galaxy_catalog['ra', 'dec', 'e1', 'e2', 'z', 'pzbins', 'pzpdf']
    return galaxy_catalog['ra', 'dec', 'e1', 'e2', 'z']
Esempio n. 2
0
def test_shear_convergence_unittests():
    """ Unit and validation tests for the shear and convergence calculations """
    helper_physics_functions(md.predict_tangential_shear)
    helper_physics_functions(md.predict_convergence)
    helper_physics_functions(md.predict_reduced_tangential_shear)

    # Validation Tests =========================
    # NumCosmo makes different choices for constants (Msun). We make this conversion
    # by passing the ratio of SOLAR_MASS in kg from numcosmo and CLMM
    cfg = load_validation_config()
    constants_conversion = clc.SOLAR_MASS.value / cfg['TEST_CASE']['Msun[kg]']

    # First compute SigmaCrit to correct cosmology changes
    cosmo = cfg['cosmo']
    sigma_c = md.get_critical_surface_density(cosmo,
                                              cfg['GAMMA_PARAMS']['z_cluster'],
                                              cfg['z_source'])

    # Patch a conversion for cluster_toolkit computations
    cosmo['Omega_c'] = cosmo['Omega_c'] * constants_conversion
    cosmo['Omega_b'] = cosmo['Omega_b'] * constants_conversion

    # Compute sigma_c in the new cosmology and get a correction factor
    sigcrit_corr = cfg['SIGMAC_PHYSCONST_CORRECTION']
    sigma_c_undo = md.get_critical_surface_density(
        cosmo, cfg['GAMMA_PARAMS']['z_cluster'], +cfg['z_source'])
    sigmac_corr = sigma_c_undo / sigma_c / sigcrit_corr

    # Chech error is raised if too small radius
    assert_raises(ValueError, md.predict_tangential_shear, 1.e-12, 1.e15, 4,
                  0.2, 0.45, cosmo)

    # Validate tangential shear
    gammat = md.predict_tangential_shear(cosmo=cosmo, **cfg['GAMMA_PARAMS'])
    assert_allclose(gammat * sigmac_corr, cfg['numcosmo_profiles']['gammat'],
                    1.0e-8)

    # Validate convergence
    kappa = md.predict_convergence(cosmo=cosmo, **cfg['GAMMA_PARAMS'])
    assert_allclose(kappa * sigmac_corr, cfg['numcosmo_profiles']['kappa'],
                    1.0e-8)

    # Validate reduced tangential shear
    assert_allclose(
        md.predict_reduced_tangential_shear(cosmo=cosmo,
                                            **cfg['GAMMA_PARAMS']),
        gammat / (1.0 - kappa), 1.0e-10)
    assert_allclose(gammat / (1. / sigmac_corr - kappa),
                    cfg['numcosmo_profiles']['gt'], 1.0e-6)
Esempio n. 3
0
def test_shear_convergence_unittests(modeling_data):
    """ Unit and validation tests for the shear and convergence calculations """
    helper_physics_functions(md.predict_tangential_shear)
    helper_physics_functions(md.predict_convergence)
    helper_physics_functions(md.predict_reduced_tangential_shear)
    helper_physics_functions(md.predict_magnification)

    # Validation Tests -------------------------
    # NumCosmo makes different choices for constants (Msun). We make this conversion
    # by passing the ratio of SOLAR_MASS in kg from numcosmo and CLMM
    cfg = load_validation_config()
    constants_conversion = clc.SOLAR_MASS.value / cfg['TEST_CASE']['Msun[kg]']

    # First compute SigmaCrit to correct cosmology changes
    cosmo = cfg['cosmo']
    sigma_c = md.get_critical_surface_density(cosmo,
                                              cfg['GAMMA_PARAMS']['z_cluster'],
                                              cfg['z_source'])

    # Compute sigma_c in the new cosmology and get a correction factor
    sigma_c_undo = md.get_critical_surface_density(
        cosmo, cfg['GAMMA_PARAMS']['z_cluster'], cfg['z_source'])
    sigmac_corr = (sigma_c_undo / sigma_c)

    # Chech error is raised if too small radius
    assert_raises(ValueError, md.predict_tangential_shear, 1.e-12, 1.e15, 4,
                  0.2, 0.45, cosmo)

    # Validate tangential shear
    gammat = md.predict_tangential_shear(cosmo=cosmo, **cfg['GAMMA_PARAMS'])
    assert_allclose(gammat * sigmac_corr, cfg['numcosmo_profiles']['gammat'],
                    1.0e-8)

    # Validate convergence
    kappa = md.predict_convergence(cosmo=cosmo, **cfg['GAMMA_PARAMS'])
    assert_allclose(kappa * sigmac_corr, cfg['numcosmo_profiles']['kappa'],
                    1.0e-8)

    # Validate reduced tangential shear
    assert_allclose(
        md.predict_reduced_tangential_shear(cosmo=cosmo,
                                            **cfg['GAMMA_PARAMS']),
        gammat / (1.0 - kappa), 1.0e-10)
    assert_allclose(gammat * sigmac_corr / (1. - (kappa * sigmac_corr)),
                    cfg['numcosmo_profiles']['gt'], 1.0e-6)

    # Validate magnification
    assert_allclose(
        md.predict_magnification(cosmo=cosmo, **cfg['GAMMA_PARAMS']),
        1. / ((1 - kappa)**2 - abs(gammat)**2), 1.0e-10)
    assert_allclose(1. / ((1 - kappa)**2 - abs(gammat)**2),
                    cfg['numcosmo_profiles']['mu'], 4.0e-7)

    # Check that shear, reduced shear and convergence return zero and magnification returns one if source is in front of the cluster
    # First, check for a array of radius and single source z
    r = np.logspace(-2, 2, 10)
    z_cluster = 0.3
    z_source = 0.2

    assert_allclose(
        md.predict_convergence(r,
                               mdelta=1.e15,
                               cdelta=4.,
                               z_cluster=z_cluster,
                               z_source=z_source,
                               cosmo=cosmo), np.zeros(len(r)), 1.0e-10)
    assert_allclose(
        md.predict_tangential_shear(r,
                                    mdelta=1.e15,
                                    cdelta=4.,
                                    z_cluster=z_cluster,
                                    z_source=z_source,
                                    cosmo=cosmo), np.zeros(len(r)), 1.0e-10)
    assert_allclose(
        md.predict_reduced_tangential_shear(r,
                                            mdelta=1.e15,
                                            cdelta=4.,
                                            z_cluster=z_cluster,
                                            z_source=z_source,
                                            cosmo=cosmo), np.zeros(len(r)),
        1.0e-10)
    assert_allclose(
        md.predict_magnification(r,
                                 mdelta=1.e15,
                                 cdelta=4.,
                                 z_cluster=z_cluster,
                                 z_source=z_source,
                                 cosmo=cosmo), np.ones(len(r)), 1.0e-10)

    # Second, check a single radius and array of source z
    r = 1.
    z_source = [0.25, 0.1, 0.14, 0.02]
    assert_allclose(
        md.predict_convergence(r,
                               mdelta=1.e15,
                               cdelta=4.,
                               z_cluster=z_cluster,
                               z_source=z_source,
                               cosmo=cosmo), np.zeros(len(z_source)), 1.0e-10)
    assert_allclose(
        md.predict_tangential_shear(r,
                                    mdelta=1.e15,
                                    cdelta=4.,
                                    z_cluster=z_cluster,
                                    z_source=z_source,
                                    cosmo=cosmo), np.zeros(len(z_source)),
        1.0e-10)
    assert_allclose(
        md.predict_reduced_tangential_shear(r,
                                            mdelta=1.e15,
                                            cdelta=4.,
                                            z_cluster=z_cluster,
                                            z_source=z_source,
                                            cosmo=cosmo),
        np.zeros(len(z_source)), 1.0e-10)
    assert_allclose(
        md.predict_magnification(r,
                                 mdelta=1.e15,
                                 cdelta=4.,
                                 z_cluster=z_cluster,
                                 z_source=z_source,
                                 cosmo=cosmo), np.ones(len(z_source)), 1.0e-10)