def _draw_source_redshifts(zsrc, zsrc_min, zsrc_max, ngals): """Set source galaxy redshifts either set to a fixed value or draw from a predefined distribution. Return a table (GCData) of the source galaxies Uses a sampling technique found in Numerical Recipes in C, Chap 7.2: Transformation Method. Pulling out random values from a given probability distribution. Parameters ---------- ngals : float Number of galaxies to generate zsrc : float or str Choose the source galaxy distribution to be fixed or drawn from a predefined distribution. float : All sources galaxies at this fixed redshift str : Draws individual source gal redshifts from predefined distribution. Options are: chang13 or desc_srd zsrc_min : float The minimum source redshift allowed. zsrc_max : float, optional If source redshifts are drawn, the maximum source redshift Returns ------- galaxy_catalog : clmm.GCData Table of true and 'measured' photometric redshifts, which here the same. Redshift photometric errors are then added using _compute_photoz_pdfs. Notes ----- Much of this code in this function was adapted from the Dallas group """ # Set zsrc to constant value if isinstance(zsrc, float): zsrc_list = np.ones(ngals) * zsrc # Draw zsrc from Chang et al. 2013 elif zsrc == 'chang13': zsrc_list = _draw_random_points_from_distribution( zsrc_min, zsrc_max, ngals, _chang_z_distrib) # Draw zsrc from the distribution used in the DESC SRD (arxiv:1809.01669) elif zsrc == 'desc_srd': zsrc_list = _draw_random_points_from_distribution( zsrc_min, zsrc_max, ngals, _srd_z_distrib) # Draw zsrc from a uniform distribution between zmin and zmax elif zsrc == 'uniform': zsrc_list = np.random.uniform(zsrc_min, zsrc_max, ngals) # Invalid entry else: raise ValueError( "zsrc must be a float, chang13 or desc_srd. You set: {}".format( zsrc)) return GCData([zsrc_list, zsrc_list], names=('ztrue', 'z'))
def test_print_gc(): # Cluster with empty galcat cl = clmm.GalaxyCluster(unique_id='1', ra=161.3, dec=34., z=0.3, galcat=GCData()) print(cl) assert isinstance(cl.__str__(), str) assert isinstance(cl.__repr__(), str) # Cluster with galcat galcat = GCData( [[120.1, 119.9, 119.9], [41.9, 42.2, 42.2], [1, 1, 1], [1, 2, 3]], names=('ra', 'dec', 'z', 'id')) cl = clmm.GalaxyCluster(unique_id='1', ra=161.3, dec=34., z=0.3, galcat=galcat) print(cl) assert isinstance(cl.__str__(), str) assert isinstance(cl.__repr__(), str)
def test_save_load(): cl1 = clmm.GalaxyCluster(unique_id='1', ra=161.3, dec=34., z=0.3, galcat=GCData()) cl1.save('testcluster.pkl') cl2 = clmm.GalaxyCluster.load('testcluster.pkl') os.system('rm testcluster.pkl') assert_equal(cl2.unique_id, cl1.unique_id) assert_equal(cl2.ra, cl1.ra) assert_equal(cl2.dec, cl1.dec) assert_equal(cl2.z, cl1.z)
def test_initialization(): testdict1 = { 'unique_id': '1', 'ra': 161.3, 'dec': 34., 'z': 0.3, 'galcat': GCData() } cl1 = clmm.GalaxyCluster(**testdict1) assert_equal(testdict1['unique_id'], cl1.unique_id) assert_equal(testdict1['ra'], cl1.ra) assert_equal(testdict1['dec'], cl1.dec) assert_equal(testdict1['z'], cl1.z) assert isinstance(cl1.galcat, GCData)
def test_compute_critical_surface_density(modeling_data): """ Validation test for critical surface density """ reltol = modeling_data['theory_reltol'] cfg = load_validation_config() assert_allclose(theo.compute_critical_surface_density(cfg['cosmo'], z_cluster=cfg['TEST_CASE']['z_cluster'], z_source=cfg['TEST_CASE']['z_source']), cfg['TEST_CASE']['nc_Sigmac'], reltol) # Check errors for z<0 assert_raises(ValueError, theo.compute_critical_surface_density, cfg['cosmo'], z_cluster=-0.2, z_source=0.3) assert_raises(ValueError, theo.compute_critical_surface_density, cfg['cosmo'], z_cluster=0.2, z_source=-0.3) # Check behaviour when sources are in front of the lens z_cluster = 0.3 z_source = 0.2 assert_allclose(theo.compute_critical_surface_density(cfg['cosmo'], z_cluster=z_cluster, z_source=z_source), np.inf, 1.0e-10) z_source = [0.2, 0.12, 0.25] assert_allclose(theo.compute_critical_surface_density(cfg['cosmo'], z_cluster=z_cluster, z_source=z_source), [np.inf, np.inf, np.inf], 1.0e-10) # Check usage with cluster object function z_src = np.array([cfg['TEST_CASE']['z_source']]) cluster = GalaxyCluster(unique_id='blah', ra=0, dec=0, z=cfg['TEST_CASE']['z_cluster'], galcat=GCData([0*z_src, 0*z_src, z_src], names=('ra', 'dec', 'z'))) cluster.add_critical_surface_density(cfg['cosmo']) assert_allclose(cluster.galcat['sigma_c'], cfg['TEST_CASE']['nc_Sigmac'], reltol) # Object Oriented tests m = theo.Modeling() m.set_cosmo(cfg['cosmo']) assert_allclose(m.eval_critical_surface_density(cfg['TEST_CASE']['z_cluster'], cfg['TEST_CASE']['z_source']), cfg['TEST_CASE']['nc_Sigmac'], reltol) # Check behaviour when sources are in front of the lens z_cluster = 0.3 z_source = 0.2 assert_allclose(m.eval_critical_surface_density(z_cluster, z_source), np.inf, 1.0e-10) z_source = [0.2, 0.12, 0.25] assert_allclose(m.eval_critical_surface_density(z_cluster, z_source), [np.inf, np.inf, np.inf], 1.0e-10)
def test_make_radial_profiles(): # Set up a cluster object and compute cross and tangential shears ra_lens, dec_lens, z_lens = 120., 42., 0.5 gals = GCData({ 'ra': np.array([120.1, 119.9, 119.9]), 'dec': np.array([41.9, 42.2, 42.2]), 'id': np.array([1, 2, 3]), 'e1': np.array([0.2, 0.4, 0.4]), 'e2': np.array([0.3, 0.5, 0.5]), 'z': np.ones(3), }) angsep_units, bin_units = 'radians', 'radians' # Set up radial values bins_radians = np.array([0.002, 0.003, 0.004]) expected_radius = [0.0021745039090962414, 0.0037238407383072053] expected_flat = { 'angsep': np.array([0.0021745039090962414, 0.0037238407383072053, 0.0037238407383072053]), 'cross_shear': np.array([0.2780316984090899, 0.6398792901134982, 0.6398792901134982]), 'tan_shear': np.array([-0.22956126563459447, -0.02354769805831558, -0.02354769805831558]), } expected_curve = {# <<TO BE ADDED IN THE FUTURE>> 'angsep': np.array([]), 'cross_shear': np.array([]), 'tan_shear': np.array([]), } # Geometries to test geo_tests = [('flat', expected_flat), ('curve', expected_curve)] geo_tests = geo_tests[:1] # <<DELETE THIS LINE WHEN CURVE VALUES ADDED>> for geometry, expected in geo_tests: ####################################### ### Use without cluster object ######## ####################################### angsep, tshear, xshear = da.compute_tangential_and_cross_components(ra_lens=ra_lens, dec_lens=dec_lens, ra_source=gals['ra'], dec_source=gals['dec'], shear1=gals['e1'], shear2=gals['e2'], geometry=geometry) # Tests passing int as bins arg makes the correct bins bins = 2 vec_bins = clmm.utils.make_bins(np.min(angsep), np.max(angsep), bins) testing.assert_array_equal(da.make_radial_profile([tshear, xshear, gals['z']], angsep, angsep_units, bin_units, bins=bins)[0], da.make_radial_profile([tshear, xshear, gals['z']], angsep, angsep_units, bin_units, bins=vec_bins)[0]) # Test the outputs of compute_tangential_and_cross_components just to be safe testing.assert_allclose(angsep, expected['angsep'], **TOLERANCE, err_msg="Angular Separation not correct when testing shear profiles") testing.assert_allclose(tshear, expected['tan_shear'], **TOLERANCE, err_msg="Tangential Shear not correct when testing shear profiles") testing.assert_allclose(xshear, expected['cross_shear'], **TOLERANCE, err_msg="Cross Shear not correct when testing shear profiles") # Test default behavior, remember that include_empty_bins=False excludes all bins with N>=1 profile = da.make_radial_profile([tshear, xshear, gals['z']], angsep, angsep_units, bin_units, bins=bins_radians, include_empty_bins=False) _test_profile_table_output(profile, bins_radians[1], expected_radius[1], bins_radians[2], expected['tan_shear'][1], expected['cross_shear'][1], [2]) # Test metadata testing.assert_array_equal(profile.meta['bin_units'], bin_units) testing.assert_array_equal(profile.meta['cosmo'], None) # Test simple unit convesion profile = da.make_radial_profile([tshear, xshear, gals['z']], angsep*180./np.pi, 'degrees', bin_units, bins=bins_radians, include_empty_bins=False) _test_profile_table_output(profile, bins_radians[1], expected_radius[1], bins_radians[2], expected['tan_shear'][1], expected['cross_shear'][1], [2]) # including empty bins profile = da.make_radial_profile([tshear, xshear, gals['z']], angsep, angsep_units, bin_units, bins=bins_radians, include_empty_bins=True) _test_profile_table_output(profile, bins_radians[:-1], expected_radius, bins_radians[1:], expected['tan_shear'][:-1], expected['cross_shear'][:-1], [1,2]) # test with return_binnumber profile, binnumber = da.make_radial_profile([tshear, xshear, gals['z']], angsep, angsep_units, bin_units, bins=bins_radians, include_empty_bins=True, return_binnumber=True) _test_profile_table_output(profile, bins_radians[:-1], expected_radius, bins_radians[1:], expected['tan_shear'][:-1], expected['cross_shear'][:-1], [1,2]) testing.assert_array_equal(binnumber, [1, 2, 2]) ################################### ### Test with cluster object ###### ################################### cluster = clmm.GalaxyCluster(unique_id='blah', ra=ra_lens, dec=dec_lens, z=z_lens, galcat=gals['ra', 'dec', 'e1', 'e2', 'z', 'id']) cluster.compute_tangential_and_cross_components(geometry=geometry) cluster.make_radial_profile(bin_units, bins=bins_radians, include_empty_bins=False) # Test default behavior, remember that include_empty_bins=False excludes all bins with N>=1 _test_profile_table_output(cluster.profile, bins_radians[1], expected_radius[1], bins_radians[2], expected['tan_shear'][1], expected['cross_shear'][1], [2], p0='gt', p1='gx') # including empty bins cluster.make_radial_profile(bin_units, bins=bins_radians, include_empty_bins=True, table_name='profile2') _test_profile_table_output(cluster.profile2, bins_radians[:-1], expected_radius, bins_radians[1:], expected['tan_shear'][:-1], expected['cross_shear'][:-1], [1,2], p0='gt', p1='gx') # Test with galaxy id's cluster.make_radial_profile(bin_units, bins=bins_radians, include_empty_bins=True, gal_ids_in_bins=True, table_name='profile3') _test_profile_table_output(cluster.profile3, bins_radians[:-1], expected_radius, bins_radians[1:], expected['tan_shear'][:-1], expected['cross_shear'][:-1], [1,2], [[1],[2,3]], p0='gt', p1='gx') # Test it runs with galaxy id's and int bins cluster.make_radial_profile(bin_units, bins=5, include_empty_bins=True, gal_ids_in_bins=True, table_name='profile3') # And overwriting table cluster.make_radial_profile(bin_units, bins=bins_radians, include_empty_bins=True, gal_ids_in_bins=True, table_name='profile3') _test_profile_table_output(cluster.profile3, bins_radians[:-1], expected_radius, bins_radians[1:], expected['tan_shear'][:-1], expected['cross_shear'][:-1], [1,2], [[1],[2,3]], p0='gt', p1='gx') # Test it runs with galaxy id's and int bins and no empty bins cluster.make_radial_profile(bin_units, bins=bins_radians, include_empty_bins=False, gal_ids_in_bins=True, table_name='profile3') _test_profile_table_output(cluster.profile3, bins_radians[1], expected_radius[1], bins_radians[2], expected['tan_shear'][1], expected['cross_shear'][1], [2], p0='gt', p1='gx') ######################################## ### Basic tests of cluster object ###### ######################################## # Test error of missing redshift cluster = clmm.GalaxyCluster(unique_id='blah', ra=ra_lens, dec=dec_lens, z=z_lens, galcat=gals['ra', 'dec', 'e1', 'e2']) cluster.compute_tangential_and_cross_components() testing.assert_raises(TypeError, cluster.make_radial_profile, bin_units) # Test error of missing shear cluster = clmm.GalaxyCluster(unique_id='blah', ra=ra_lens, dec=dec_lens, z=z_lens, galcat=gals['ra', 'dec', 'e1', 'e2', 'z', 'id']) testing.assert_raises(TypeError, cluster.make_radial_profile, bin_units) # Test error with overwrite=False cluster.compute_tangential_and_cross_components() cluster.make_radial_profile(bin_units, bins=bins_radians, table_name='profile3') testing.assert_raises(AttributeError, cluster.make_radial_profile, bin_units, bins=bins_radians, table_name='profile3', overwrite=False) # Check error of missing id's cluster_noid = clmm.GalaxyCluster(unique_id='blah', ra=ra_lens, dec=dec_lens, z=z_lens, galcat=gals['ra', 'dec', 'e1', 'e2', 'z']) cluster_noid.compute_tangential_and_cross_components() testing.assert_raises(TypeError, cluster_noid.make_radial_profile, bin_units, gal_ids_in_bins=True)
def test_compute_tangential_and_cross_components(modeling_data): # Input values reltol = modeling_data['dataops_reltol'] ra_lens, dec_lens, z_lens = 120., 42., 0.5 gals = GCData({ 'ra': np.array([120.1, 119.9]), 'dec': np.array([41.9, 42.2]), 'id': np.array([1, 2]), 'e1': np.array([0.2, 0.4]), 'e2': np.array([0.3, 0.5]), 'z': np.array([1.,2.]), }) # Correct values expected_flat = { 'angsep': np.array([0.0021745039090962414, 0.0037238407383072053]), 'cross_shear': np.array([0.2780316984090899, 0.6398792901134982]), 'tangential_shear': np.array([-0.22956126563459447, -0.02354769805831558]), # DeltaSigma expected values for clmm.Cosmology(H0=67.66, Omega_dm0=0.262, Omega_b0=0.049) 'cross_DS': np.array([8.58093068e+14, 1.33131522e+15]), #[1224.3326297393244, 1899.6061989365176])*0.7*1.0e12*1.0002565513832675 'tangential_DS': np.array([-7.08498103e+14, -4.89926917e+13]), #[-1010.889584349285, -69.9059242788237])*0.7*1.0e12*1.0002565513832675 } expected_curve = {# <<TO BE ADDED IN THE FUTURE>> 'angsep': np.array([]), 'cross_shear': np.array([]), 'tangential_shear': np.array([]), 'cross_DS': np.array([]), 'tangential_DS': np.array([]), } # Geometries to test geo_tests = [('flat', expected_flat), ('curve', expected_curve)] geo_tests = geo_tests[:1] # <<DELETE THIS LINE WHEN CURVE VALUES ADDED>> # test incosnsitent data testing.assert_raises(TypeError, da.compute_tangential_and_cross_components, ra_lens=ra_lens, dec_lens=dec_lens, ra_source=gals['ra'][0], dec_source=gals['dec'], shear1=gals['e1'], shear2=gals['e2']) testing.assert_raises(TypeError, da.compute_tangential_and_cross_components, ra_lens=ra_lens, dec_lens=dec_lens, ra_source=gals['ra'][:1], dec_source=gals['dec'], shear1=gals['e1'], shear2=gals['e2']) # test not implemented geometry testing.assert_raises(NotImplementedError, da.compute_tangential_and_cross_components, ra_lens=ra_lens, dec_lens=dec_lens, ra_source=gals['ra'], dec_source=gals['dec'], shear1=gals['e1'], shear2=gals['e2'], geometry='something crazy') for geometry, expected in geo_tests: # Pass arrays directly into function angsep, tshear, xshear = da.compute_tangential_and_cross_components(ra_lens=ra_lens, dec_lens=dec_lens, ra_source=gals['ra'], dec_source=gals['dec'], shear1=gals['e1'], shear2=gals['e2'], geometry=geometry) testing.assert_allclose(angsep, expected['angsep'], **TOLERANCE, err_msg="Angular Separation not correct when passing lists") testing.assert_allclose(tshear, expected['tangential_shear'], **TOLERANCE, err_msg="Tangential Shear not correct when passing lists") testing.assert_allclose(xshear, expected['cross_shear'], **TOLERANCE, err_msg="Cross Shear not correct when passing lists") # Pass LISTS into function angsep, tshear, xshear = da.compute_tangential_and_cross_components(ra_lens=ra_lens, dec_lens=dec_lens, ra_source=list(gals['ra']), dec_source=list(gals['dec']), shear1=list(gals['e1']), shear2=list(gals['e2']), geometry=geometry) testing.assert_allclose(angsep, expected['angsep'], **TOLERANCE, err_msg="Angular Separation not correct when passing lists") testing.assert_allclose(tshear, expected['tangential_shear'], **TOLERANCE, err_msg="Tangential Shear not correct when passing lists") testing.assert_allclose(xshear, expected['cross_shear'], **TOLERANCE, err_msg="Cross Shear not correct when passing lists") # Use the cluster method cluster = clmm.GalaxyCluster(unique_id='blah', ra=ra_lens, dec=dec_lens, z=z_lens, galcat=gals['ra', 'dec', 'e1', 'e2']) # Test error with bad name/missing column testing.assert_raises(TypeError, cluster.compute_tangential_and_cross_components, shape_component1='crazy name') # Test output for geometry, expected in geo_tests: angsep3, tshear3, xshear3 = cluster.compute_tangential_and_cross_components(geometry=geometry) testing.assert_allclose(angsep3, expected['angsep'], **TOLERANCE, err_msg="Angular Separation not correct when using cluster method") testing.assert_allclose(tshear3, expected['tangential_shear'], **TOLERANCE, err_msg="Tangential Shear not correct when using cluster method") testing.assert_allclose(xshear3, expected['cross_shear'], **TOLERANCE, err_msg="Cross Shear not correct when using cluster method") # Check behaviour for the deltasigma option. cosmo = clmm.Cosmology(H0=70.0, Omega_dm0=0.275, Omega_b0=0.025) # test missing info for is_deltasigma=True testing.assert_raises(TypeError, da.compute_tangential_and_cross_components, ra_lens=ra_lens, dec_lens=dec_lens, ra_source=gals['ra'], dec_source=gals['dec'], shear1=gals['e1'], shear2=gals['e2'], is_deltasigma=True, cosmo=None, z_lens=z_lens, z_source=gals['z']) testing.assert_raises(TypeError, da.compute_tangential_and_cross_components, ra_lens=ra_lens, dec_lens=dec_lens, ra_source=gals['ra'], dec_source=gals['dec'], shear1=gals['e1'], shear2=gals['e2'], is_deltasigma=True, cosmo=cosmo, z_lens=None, z_source=gals['z']) testing.assert_raises(TypeError, da.compute_tangential_and_cross_components, ra_lens=ra_lens, dec_lens=dec_lens, ra_source=gals['ra'], dec_source=gals['dec'], shear1=gals['e1'], shear2=gals['e2'], is_deltasigma=True, cosmo=cosmo, z_lens=z_lens, z_source=None) # check values for DeltaSigma for geometry, expected in geo_tests: angsep_DS, tDS, xDS = da.compute_tangential_and_cross_components( ra_lens=ra_lens, dec_lens=dec_lens, ra_source=gals['ra'], dec_source=gals['dec'], shear1=gals['e1'], shear2=gals['e2'], is_deltasigma=True, cosmo=cosmo, z_lens=z_lens, z_source=gals['z'], geometry=geometry) testing.assert_allclose(angsep_DS, expected['angsep'], reltol, err_msg="Angular Separation not correct") testing.assert_allclose(tDS, expected['tangential_DS'], reltol, err_msg="Tangential Shear not correct") testing.assert_allclose(xDS, expected['cross_DS'], reltol, err_msg="Cross Shear not correct") # Tests with the cluster object # cluster object missing source redshift, and function call missing cosmology cluster = clmm.GalaxyCluster(unique_id='blah', ra=ra_lens, dec=dec_lens, z=z_lens, galcat=gals['ra', 'dec', 'e1', 'e2']) testing.assert_raises(TypeError, cluster.compute_tangential_and_cross_components, is_deltasigma=True) # cluster object OK but function call missing cosmology cluster = clmm.GalaxyCluster(unique_id='blah', ra=ra_lens, dec=dec_lens, z=z_lens, galcat=gals['ra', 'dec', 'e1', 'e2','z']) testing.assert_raises(TypeError, cluster.compute_tangential_and_cross_components, is_deltasigma=True) # check values for DeltaSigma for geometry, expected in geo_tests: angsep_DS, tDS, xDS = cluster.compute_tangential_and_cross_components(cosmo=cosmo, is_deltasigma=True, geometry=geometry) testing.assert_allclose(angsep_DS, expected['angsep'], reltol, err_msg="Angular Separation not correct when using cluster method") testing.assert_allclose(tDS, expected['tangential_DS'], reltol, err_msg="Tangential Shear not correct when using cluster method") testing.assert_allclose(xDS, expected['cross_DS'], reltol, err_msg="Cross Shear not correct when using cluster method")
def test_integrity(): # Converge on name # Ensure we have all necessary values to make a GalaxyCluster assert_raises(TypeError, clmm.GalaxyCluster, ra=161.3, dec=34., z=0.3, galcat=GCData()) assert_raises(TypeError, clmm.GalaxyCluster, unique_id=1, dec=34., z=0.3, galcat=GCData()) assert_raises(TypeError, clmm.GalaxyCluster, unique_id=1, ra=161.3, z=0.3, galcat=GCData()) assert_raises(TypeError, clmm.GalaxyCluster, unique_id=1, ra=161.3, dec=34., galcat=GCData()) # Test that we get errors when we pass in values outside of the domains assert_raises(ValueError, clmm.GalaxyCluster, unique_id=1, ra=-360.3, dec=34., z=0.3, galcat=GCData()) assert_raises(ValueError, clmm.GalaxyCluster, unique_id=1, ra=360.3, dec=34., z=0.3, galcat=GCData()) assert_raises(ValueError, clmm.GalaxyCluster, unique_id=1, ra=161.3, dec=95., z=0.3, galcat=GCData()) assert_raises(ValueError, clmm.GalaxyCluster, unique_id=1, ra=161.3, dec=-95., z=0.3, galcat=GCData()) assert_raises(ValueError, clmm.GalaxyCluster, unique_id=1, ra=161.3, dec=34., z=-0.3, galcat=GCData()) # Test that inputs are the correct type assert_raises(TypeError, clmm.GalaxyCluster, unique_id=None, ra=161.3, dec=34., z=0.3, galcat=GCData()) assert_raises(TypeError, clmm.GalaxyCluster, unique_id=1, ra=161.3, dec=34., z=0.3, galcat=1) assert_raises(TypeError, clmm.GalaxyCluster, unique_id=1, ra=161.3, dec=34., z=0.3, galcat=[]) assert_raises(TypeError, clmm.GalaxyCluster, unique_id=1, ra=None, dec=34., z=0.3, galcat=GCData()) assert_raises(TypeError, clmm.GalaxyCluster, unique_id=1, ra=161.3, dec=None, z=0.3, galcat=GCData()) assert_raises(TypeError, clmm.GalaxyCluster, unique_id=1, ra=161.3, dec=34., z=None, galcat=GCData()) # Test that id can support numbers and strings assert isinstance( clmm.GalaxyCluster(unique_id=1, ra=161.3, dec=34., z=0.3, galcat=GCData()).unique_id, str) # assert clmm.GalaxyCluster(unique_id=1.0, ra=161.3, dec=34., z=0.3, galcat=GCData()).unique_id == '1' assert isinstance( clmm.GalaxyCluster(unique_id='1', ra=161.3, dec=34., z=0.3, galcat=GCData()).unique_id, str) # Test that ra/dec/z can be converted from int/str to float if needed assert clmm.GalaxyCluster('1', '161.', '55.', '.3', GCData()) assert clmm.GalaxyCluster('1', 161, 55, 1, GCData())
def test_make_radial_profiles(): # Set up a cluster object and compute cross and tangential shears ra_lens, dec_lens, z_lens = 120., 42., 0.5 ra_source = np.array([120.1, 119.9, 119.9]) dec_source = np.array([41.9, 42.2, 42.2]) id_source = np.array([1, 2, 3]) shear1 = np.array([0.2, 0.4, 0.4]) shear2 = np.array([0.3, 0.5, 0.5]) z_sources = np.ones(3) angsep_units, bin_units = 'radians', 'radians' # Set up radial values bins_radians = np.array([0.002, 0.003, 0.004]) expected_radius = [0.0021745039090962414, 0.0037238407383072053] ####################################### ### Use without cluster object ######## ####################################### angsep, tshear, xshear = da.compute_tangential_and_cross_components(ra_lens=ra_lens, dec_lens=dec_lens, ra_source=ra_source, dec_source=dec_source, shear1=shear1, shear2=shear2) # Tests passing int as bins arg makes the correct bins bins = 2 vec_bins = clmm.utils.make_bins(np.min(angsep), np.max(angsep), bins) testing.assert_array_equal(da.make_radial_profile([tshear, xshear, z_sources], angsep, angsep_units, bin_units, bins=bins)[0], da.make_radial_profile([tshear, xshear, z_sources], angsep, angsep_units, bin_units, bins=vec_bins)[0]) # Test the outputs of compute_tangential_and_cross_components just to be safe expected_angsep = np.array([0.0021745039090962414, 0.0037238407383072053, 0.0037238407383072053]) expected_cross_shear = np.array([0.2780316984090899, 0.6398792901134982, 0.6398792901134982]) expected_tan_shear = np.array([-0.22956126563459447, -0.02354769805831558, -0.02354769805831558]) testing.assert_allclose(angsep, expected_angsep, **TOLERANCE, err_msg="Angular Separation not correct when testing shear profiles") testing.assert_allclose(tshear, expected_tan_shear, **TOLERANCE, err_msg="Tangential Shear not correct when testing shear profiles") testing.assert_allclose(xshear, expected_cross_shear, **TOLERANCE, err_msg="Cross Shear not correct when testing shear profiles") # Test default behavior, remember that include_empty_bins=False excludes all bins with N>=1 profile = da.make_radial_profile([tshear, xshear, z_sources], angsep, angsep_units, bin_units, bins=bins_radians, include_empty_bins=False) _test_profile_table_output(profile, bins_radians[1], expected_radius[1], bins_radians[2], expected_tan_shear[1], expected_cross_shear[1], [2]) # Test metadata testing.assert_array_equal(profile.meta['bin_units'], bin_units) testing.assert_array_equal(profile.meta['cosmo'], None) # Test simple unit convesion profile = da.make_radial_profile([tshear, xshear, z_sources], angsep*180./np.pi, 'degrees', bin_units, bins=bins_radians, include_empty_bins=False) _test_profile_table_output(profile, bins_radians[1], expected_radius[1], bins_radians[2], expected_tan_shear[1], expected_cross_shear[1], [2]) # including empty bins profile = da.make_radial_profile([tshear, xshear, z_sources], angsep, angsep_units, bin_units, bins=bins_radians, include_empty_bins=True) _test_profile_table_output(profile, bins_radians[:-1], expected_radius, bins_radians[1:], expected_tan_shear[:-1], expected_cross_shear[:-1], [1,2]) # test with return_binnumber profile, binnumber = da.make_radial_profile([tshear, xshear, z_sources], angsep, angsep_units, bin_units, bins=bins_radians, include_empty_bins=True, return_binnumber=True) _test_profile_table_output(profile, bins_radians[:-1], expected_radius, bins_radians[1:], expected_tan_shear[:-1], expected_cross_shear[:-1], [1,2]) testing.assert_array_equal(binnumber, [1, 2, 2]) ################################### ### Test with cluster object ###### ################################### # Test error of missing redshift cluster = clmm.GalaxyCluster(unique_id='blah', ra=ra_lens, dec=dec_lens, z=z_lens, galcat=GCData([ra_source, dec_source, shear1, shear2], names=('ra', 'dec', 'e1', 'e2'))) cluster.compute_tangential_and_cross_components() testing.assert_raises(TypeError, cluster.make_radial_profile, bin_units) # Test error of missing shear cluster = clmm.GalaxyCluster(unique_id='blah', ra=ra_lens, dec=dec_lens, z=z_lens, galcat=GCData([ra_source, dec_source, shear1, shear2, z_sources, id_source], names=('ra', 'dec', 'e1', 'e2', 'z', 'id'))) testing.assert_raises(TypeError, cluster.make_radial_profile, bin_units) # Test default behavior, remember that include_empty_bins=False excludes all bins with N>=1 cluster.compute_tangential_and_cross_components() cluster.make_radial_profile(bin_units, bins=bins_radians, include_empty_bins=False) _test_profile_table_output(cluster.profile, bins_radians[1], expected_radius[1], bins_radians[2], expected_tan_shear[1], expected_cross_shear[1], [2], p0='gt', p1='gx') # including empty bins cluster.make_radial_profile(bin_units, bins=bins_radians, include_empty_bins=True, table_name='profile2') _test_profile_table_output(cluster.profile2, bins_radians[:-1], expected_radius, bins_radians[1:], expected_tan_shear[:-1], expected_cross_shear[:-1], [1,2], p0='gt', p1='gx') # Test with galaxy id's cluster.make_radial_profile(bin_units, bins=bins_radians, include_empty_bins=True, gal_ids_in_bins=True, table_name='profile3') _test_profile_table_output(cluster.profile3, bins_radians[:-1], expected_radius, bins_radians[1:], expected_tan_shear[:-1], expected_cross_shear[:-1], [1,2], [[1],[2,3]], p0='gt', p1='gx') # Test it runs with galaxy id's and int bins cluster.make_radial_profile(bin_units, bins=5, include_empty_bins=True, gal_ids_in_bins=True, table_name='profile3') # And overwriting table cluster.make_radial_profile(bin_units, bins=bins_radians, include_empty_bins=True, gal_ids_in_bins=True, table_name='profile3') _test_profile_table_output(cluster.profile3, bins_radians[:-1], expected_radius, bins_radians[1:], expected_tan_shear[:-1], expected_cross_shear[:-1], [1,2], [[1],[2,3]], p0='gt', p1='gx') # Test it runs with galaxy id's and int bins and no empty bins cluster.make_radial_profile(bin_units, bins=bins_radians, include_empty_bins=False, gal_ids_in_bins=True, table_name='profile3') _test_profile_table_output(cluster.profile3, bins_radians[1], expected_radius[1], bins_radians[2], expected_tan_shear[1], expected_cross_shear[1], [2], p0='gt', p1='gx') # Test error with overwrite=False testing.assert_raises(AttributeError, cluster.make_radial_profile, bin_units, bins=bins_radians, include_empty_bins=True, gal_ids_in_bins=True, table_name='profile3', overwrite=False) # Check error of missing id's cluster_noid = clmm.GalaxyCluster(unique_id='blah', ra=ra_lens, dec=dec_lens, z=z_lens, galcat=GCData([ra_source, dec_source, shear1, shear2, z_sources], names=('ra', 'dec', 'e1', 'e2', 'z'))) cluster_noid.compute_tangential_and_cross_components() testing.assert_raises(TypeError, cluster_noid.make_radial_profile, bin_units, gal_ids_in_bins=True)
def test_compute_tangential_and_cross_components(modeling_data): # Input values ra_lens, dec_lens, z_lens = 120., 42., 0.5 ra_source = np.array([120.1, 119.9]) dec_source = np.array([41.9, 42.2]) z_source = np.array([1.,2.]) shear1 = np.array([0.2, 0.4]) shear2 = np.array([0.3, 0.5]) # Correct values expected_angsep = np.array([0.0021745039090962414, 0.0037238407383072053]) expected_cross_shear = np.array([0.2780316984090899, 0.6398792901134982]) expected_tangential_shear = np.array([-0.22956126563459447, -0.02354769805831558]) # DeltaSigma expected values for clmm.Cosmology(H0=70.0, Omega_dm0=0.275, Omega_b0=0.025) expected_cross_DS = np.array([1224.3326297393244, 1899.6061989365176])*0.7*1.0e12*1.0002565513832675 expected_tangential_DS = np.array([-1010.889584349285, -69.9059242788237])*0.7*1.0e12*1.0002565513832675 # test incosnsitent data testing.assert_raises(TypeError, da.compute_tangential_and_cross_components, ra_lens=ra_lens, dec_lens=dec_lens, ra_source=ra_source[0], dec_source=dec_source, shear1=shear1, shear2=shear2) testing.assert_raises(TypeError, da.compute_tangential_and_cross_components, ra_lens=ra_lens, dec_lens=dec_lens, ra_source=ra_source[:1], dec_source=dec_source, shear1=shear1, shear2=shear2) # test not implemented geometry testing.assert_raises(NotImplementedError, da.compute_tangential_and_cross_components, ra_lens=ra_lens, dec_lens=dec_lens, ra_source=ra_source, dec_source=dec_source, shear1=shear1, shear2=shear2, geometry='something crazy') # Pass arrays directly into function angsep, tshear, xshear = da.compute_tangential_and_cross_components(ra_lens=ra_lens, dec_lens=dec_lens, ra_source=ra_source, dec_source=dec_source, shear1=shear1, shear2=shear2) testing.assert_allclose(angsep, expected_angsep, **TOLERANCE, err_msg="Angular Separation not correct when passing lists") testing.assert_allclose(tshear, expected_tangential_shear, **TOLERANCE, err_msg="Tangential Shear not correct when passing lists") testing.assert_allclose(xshear, expected_cross_shear, **TOLERANCE, err_msg="Cross Shear not correct when passing lists") # Pass LISTS into function angsep, tshear, xshear = da.compute_tangential_and_cross_components(ra_lens=ra_lens, dec_lens=dec_lens, ra_source=list(ra_source), dec_source=list(dec_source), shear1=list(shear1), shear2=list(shear2)) testing.assert_allclose(angsep, expected_angsep, **TOLERANCE, err_msg="Angular Separation not correct when passing lists") testing.assert_allclose(tshear, expected_tangential_shear, **TOLERANCE, err_msg="Tangential Shear not correct when passing lists") testing.assert_allclose(xshear, expected_cross_shear, **TOLERANCE, err_msg="Cross Shear not correct when passing lists") # Use the cluster method cluster = clmm.GalaxyCluster(unique_id='blah', ra=ra_lens, dec=dec_lens, z=z_lens, galcat=GCData([ra_source, dec_source, shear1, shear2], names=('ra', 'dec', 'e1', 'e2'))) # Test error with bad name/missing column testing.assert_raises(TypeError, cluster.compute_tangential_and_cross_components, shape_component1='crazy name') # Test output angsep3, tshear3, xshear3 = cluster.compute_tangential_and_cross_components() testing.assert_allclose(angsep3, expected_angsep, **TOLERANCE, err_msg="Angular Separation not correct when using cluster method") testing.assert_allclose(tshear3, expected_tangential_shear, **TOLERANCE, err_msg="Tangential Shear not correct when using cluster method") testing.assert_allclose(xshear3, expected_cross_shear, **TOLERANCE, err_msg="Cross Shear not correct when using cluster method") # Check behaviour for the deltasigma option. cosmo = clmm.Cosmology(H0=70.0, Omega_dm0=0.275, Omega_b0=0.025) # test missing info for is_deltasigma=True testing.assert_raises(TypeError, da.compute_tangential_and_cross_components, ra_lens=ra_lens, dec_lens=dec_lens, ra_source=ra_source, dec_source=dec_source, shear1=shear1, shear2=shear2, is_deltasigma=True, cosmo=None, z_lens=z_lens, z_source=z_source) testing.assert_raises(TypeError, da.compute_tangential_and_cross_components, ra_lens=ra_lens, dec_lens=dec_lens, ra_source=ra_source, dec_source=dec_source, shear1=shear1, shear2=shear2, is_deltasigma=True, cosmo=cosmo, z_lens=None, z_source=z_source) testing.assert_raises(TypeError, da.compute_tangential_and_cross_components, ra_lens=ra_lens, dec_lens=dec_lens, ra_source=ra_source, dec_source=dec_source, shear1=shear1, shear2=shear2, is_deltasigma=True, cosmo=cosmo, z_lens=z_lens, z_source=None) # check values for DeltaSigma angsep_DS, tDS, xDS = da.compute_tangential_and_cross_components( ra_lens=ra_lens, dec_lens=dec_lens, ra_source=ra_source, dec_source=dec_source, shear1=shear1, shear2=shear2, is_deltasigma=True, cosmo=cosmo, z_lens=z_lens, z_source=z_source) testing.assert_allclose(angsep_DS, expected_angsep, **TOLERANCE, err_msg="Angular Separation not correct") testing.assert_allclose(tDS, expected_tangential_DS, **TOLERANCE, err_msg="Tangential Shear not correct") testing.assert_allclose(xDS, expected_cross_DS, **TOLERANCE, err_msg="Cross Shear not correct") # Tests with the cluster object # cluster object missing source redshift, and function call missing cosmology cluster = clmm.GalaxyCluster(unique_id='blah', ra=ra_lens, dec=dec_lens, z=z_lens, galcat=GCData([ra_source, dec_source, shear1, shear2], names=('ra', 'dec', 'e1', 'e2'))) testing.assert_raises(TypeError, cluster.compute_tangential_and_cross_components, is_deltasigma=True) # cluster object OK but function call missing cosmology cluster = clmm.GalaxyCluster(unique_id='blah', ra=ra_lens, dec=dec_lens, z=z_lens, galcat=GCData([ra_source, dec_source, shear1, shear2, z_source], names=('ra', 'dec', 'e1', 'e2','z'))) testing.assert_raises(TypeError, cluster.compute_tangential_and_cross_components, is_deltasigma=True) # check values for DeltaSigma angsep_DS, tDS, xDS = cluster.compute_tangential_and_cross_components(cosmo=cosmo, is_deltasigma=True) testing.assert_allclose(angsep_DS, expected_angsep, **TOLERANCE, err_msg="Angular Separation not correct when using cluster method") testing.assert_allclose(tDS, expected_tangential_DS, **TOLERANCE, err_msg="Tangential Shear not correct when using cluster method") testing.assert_allclose(xDS, expected_cross_DS, **TOLERANCE, err_msg="Cross Shear not correct when using cluster method")
def test_init(): gcdata = GCData() assert_equal(None, gcdata.meta['cosmo'])
def test_update_cosmo(): # Define inputs cosmo1 = Cosmology(H0=70.0, Omega_dm0=0.3 - 0.045, Omega_b0=0.045) desc1 = cosmo1.get_desc() gcdata = GCData() # check it has __str__ adn __repr__ gcdata.__str__() gcdata.__repr__() # manual update gcdata.update_cosmo_ext_valid(gcdata, cosmo1, overwrite=False) assert_equal(desc1, gcdata.meta['cosmo']) # check that adding cosmo metadata manually is forbidden assert_raises(ValueError, gcdata.meta.__setitem__, 'cosmo', None) assert_raises(ValueError, gcdata.meta.__setitem__, 'cosmo', cosmo1) # update_cosmo funcs # input_cosmo=None, data_cosmo=None gcdata = GCData() gcdata.update_cosmo_ext_valid(gcdata, None, overwrite=False) assert_equal(None, gcdata.meta['cosmo']) gcdata = GCData() gcdata.update_cosmo_ext_valid(gcdata, None, overwrite=True) assert_equal(None, gcdata.meta['cosmo']) gcdata = GCData() gcdata.update_cosmo(None, overwrite=False) assert_equal(None, gcdata.meta['cosmo']) gcdata = GCData() gcdata.update_cosmo(None, overwrite=True) assert_equal(None, gcdata.meta['cosmo']) # input_cosmo!=None, data_cosmo=None gcdata = GCData() gcdata.update_cosmo_ext_valid(gcdata, cosmo1, overwrite=True) assert_equal(desc1, gcdata.meta['cosmo']) gcdata = GCData() gcdata.update_cosmo(cosmo1, overwrite=False) assert_equal(desc1, gcdata.meta['cosmo']) gcdata = GCData() gcdata.update_cosmo(cosmo1, overwrite=True) assert_equal(desc1, gcdata.meta['cosmo']) # input_cosmo=data_cosmo!=None gcdata = GCData() gcdata.update_cosmo(cosmo1) gcdata.update_cosmo_ext_valid(gcdata, cosmo1, overwrite=False) assert_equal(desc1, gcdata.meta['cosmo']) gcdata = GCData() gcdata.update_cosmo(cosmo1) gcdata.update_cosmo_ext_valid(gcdata, cosmo1, overwrite=True) assert_equal(desc1, gcdata.meta['cosmo']) gcdata = GCData() gcdata.update_cosmo(cosmo1) gcdata.update_cosmo(cosmo1, overwrite=False) assert_equal(desc1, gcdata.meta['cosmo']) gcdata = GCData() gcdata.update_cosmo(cosmo1) gcdata.update_cosmo(cosmo1, overwrite=True) assert_equal(desc1, gcdata.meta['cosmo']) # input_cosmo(!=None) != data_cosmo(!=None) cosmo2 = Cosmology(H0=60.0, Omega_dm0=0.3 - 0.045, Omega_b0=0.045) desc2 = cosmo2.get_desc() gcdata = GCData() gcdata.update_cosmo(cosmo1) assert_raises(TypeError, gcdata.update_cosmo_ext_valid, gcdata, cosmo2, overwrite=False) assert_raises(TypeError, gcdata.update_cosmo_ext_valid, gcdata, cosmo2) gcdata = GCData() gcdata.update_cosmo(cosmo1) gcdata.update_cosmo_ext_valid(gcdata, cosmo2, overwrite=True) assert_equal(desc2, gcdata.meta['cosmo']) gcdata = GCData() gcdata.update_cosmo(cosmo1) gcdata.update_cosmo(cosmo1, overwrite=False) assert_equal(desc1, gcdata.meta['cosmo']) gcdata = GCData() gcdata.update_cosmo(cosmo1) assert_raises(TypeError, gcdata.update_cosmo, cosmo2, overwrite=False) assert_raises(TypeError, gcdata.update_cosmo, cosmo2)
def _draw_source_redshifts(zsrc, zsrc_min, zsrc_max, ngals): """Set source galaxy redshifts either set to a fixed value or draw from a predefined distribution. Return a table (GCData) of the source galaxies Uses a sampling technique found in Numerical Recipes in C, Chap 7.2: Transformation Method. Pulling out random values from a given probability distribution. Parameters ---------- ngals : float Number of galaxies to generate zsrc : float or str Choose the source galaxy distribution to be fixed or drawn from a predefined distribution. float : All sources galaxies at this fixed redshift str : Draws individual source gal redshifts from predefined distribution. Options are: chang13 zsrc_min : float The minimum source redshift allowed. zsrc_max : float, optional If source redshifts are drawn, the maximum source redshift Returns ------- galaxy_catalog : clmm.GCData Table of true and 'measured' photometric redshifts, which here the same. Redshift photometric errors are then added using _compute_photoz_pdfs. Notes ----- Much of this code in this function was adapted from the Dallas group """ # Set zsrc to constant value if isinstance(zsrc, float): zsrc_list = np.ones(ngals) * zsrc # Draw zsrc from Chang et al. 2013 elif zsrc == 'chang13': def integrated_pzfxn(zmax, func): """Integrated redshift distribution function for transformation method""" val, _ = integrate.quad(func, zsrc_min, zmax) return val vectorization_integrated_pzfxn = np.vectorize(integrated_pzfxn) zsrc_domain = np.arange(zsrc_min, zsrc_max, 0.001) # Cumulative probability function of the redshift distribution probdist = vectorization_integrated_pzfxn(zsrc_domain, _chang_z_distrib) uniform_deviate = np.random.uniform(probdist.min(), probdist.max(), ngals) zsrc_list = interp1d(probdist, zsrc_domain, kind='linear')(uniform_deviate) # Draw zsrc from a uniform distribution between zmin and zmax elif zsrc == 'uniform': zsrc_list = np.random.uniform(zsrc_min, zsrc_max, ngals) # Invalid entry else: raise ValueError( "zsrc must be a float or chang13. You set: {}".format(zsrc)) return GCData([zsrc_list, zsrc_list], names=('ztrue', 'z'))