def test_integrity(): # Converge on name # Ensure we have all necessary values to make a GalaxyCluster testing.assert_raises(TypeError, clmm.GalaxyCluster, ra=161.3, dec=34., z=0.3, galcat=Table()) testing.assert_raises(TypeError, clmm.GalaxyCluster, unique_id=1, dec=34., z=0.3, galcat=Table()) testing.assert_raises(TypeError, clmm.GalaxyCluster, unique_id=1, ra=161.3, z=0.3, galcat=Table()) testing.assert_raises(TypeError, clmm.GalaxyCluster, unique_id=1, ra=161.3, dec=34., galcat=Table()) # Test that we get errors when we pass in values outside of the domains testing.assert_raises(ValueError, clmm.GalaxyCluster, unique_id=1, ra=-360.3, dec=34., z=0.3, galcat=Table()) testing.assert_raises(ValueError, clmm.GalaxyCluster, unique_id=1, ra=360.3, dec=34., z=0.3, galcat=Table()) testing.assert_raises(ValueError, clmm.GalaxyCluster, unique_id=1, ra=161.3, dec=95., z=0.3, galcat=Table()) testing.assert_raises(ValueError, clmm.GalaxyCluster, unique_id=1, ra=161.3, dec=-95., z=0.3, galcat=Table()) testing.assert_raises(ValueError, clmm.GalaxyCluster, unique_id=1, ra=161.3, dec=34., z=-0.3, galcat=Table()) # Test that inputs are the correct type testing.assert_raises(TypeError, clmm.GalaxyCluster, unique_id=1, ra=161.3, dec=34., z=0.3, galcat=1) testing.assert_raises(TypeError, clmm.GalaxyCluster, unique_id=1, ra=161.3, dec=34., z=0.3, galcat=[]) testing.assert_raises(TypeError, clmm.GalaxyCluster, unique_id=1, ra=None, dec=34., z=0.3, galcat=Table()) testing.assert_raises(TypeError, clmm.GalaxyCluster, unique_id=1, ra=161.3, dec=None, z=0.3, galcat=Table()) testing.assert_raises(TypeError, clmm.GalaxyCluster, unique_id=1, ra=161.3, dec=34., z=None, galcat=Table()) # Test that id can support numbers and strings assert isinstance(clmm.GalaxyCluster(unique_id=1, ra=161.3, dec=34., z=0.3, galcat=Table()).unique_id, str) # assert clmm.GalaxyCluster(unique_id=1.0, ra=161.3, dec=34., z=0.3, galcat=Table()).unique_id == '1' assert isinstance(clmm.GalaxyCluster(unique_id='1', ra=161.3, dec=34., z=0.3, galcat=Table()).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', Table()) assert clmm.GalaxyCluster('1', 161, 55, 1, Table())
def test_integrity_of_lensfuncs(): ra_source, dec_source = [120.1, 119.9, 119.9], [41.9, 42.2, 42.2] id_source, z_sources = [1, 2, 3], [1, 1, 1] galcat = GCData([ra_source, dec_source, z_sources, id_source], names=('ra', 'dec', 'z', 'id')) galcatNoZ = GCData([ra_source, dec_source, id_source], names=('ra', 'dec', 'id')) cosmo = clmm.Cosmology(H0=70.0, Omega_dm0=0.275, Omega_b0=0.025) # Missing cosmo cl = clmm.GalaxyCluster(unique_id='1', ra=161.3, dec=34., z=0.3, galcat=galcat) assert_raises(TypeError, cl.add_critical_surface_density, None) # Missing cl redshift cl = clmm.GalaxyCluster(unique_id='1', ra=161.3, dec=34., z=0.3, galcat=galcat) cl.z = None assert_raises(TypeError, cl.add_critical_surface_density, cosmo) # Missing galaxy redshift cl = clmm.GalaxyCluster(unique_id='1', ra=161.3, dec=34., z=0.3, galcat=galcatNoZ) assert_raises(TypeError, cl.add_critical_surface_density, cosmo)
def mass_mock_cluster(mass=15.,guess=15.): # Set up mock cluster ngals=50000 data = mock.generate_galaxy_catalog(10**mass, 0.3, 4, cosmo, 0.8, ngals=ngals) # Check whether the given ngals is the retrieved ngals assert_equal(len(data['ra']),ngals) # Check that there are no galaxies with |e|>1 assert_equal(np.count_nonzero((data['e1']>1) | (data['e1']<-1)),0) assert_equal(np.count_nonzero((data['e2']>1) | (data['e2']<-1)),0) # Create shear profile cl = clmm.GalaxyCluster("test_cluster", 0.0, 0.0, 0.3, data) theta, g_t, g_x = cl.compute_tangential_and_cross_components(geometry="flat") binned = cl.make_radial_profile("Mpc", bins=da.make_bins(0.5, 5.0, 100), cosmo=cosmo, include_empty_bins=False) popt,pcov = fitters['curve_fit'](lambda r, logm: nfw_shear_profile(r, logm, 0.8), binned['radius'], binned['gt'], np.ones_like(binned['gt'])*1.e-5, bounds=[13.,17.],p0=guess) return popt[0]
def test_plot_profiles(): # Input values ra_lens, dec_lens, z_lens = 120., 42., 0.5 ra_source = [120.1, 119.9] dec_source = [41.9, 42.2] z_source = [1., 2.] shear1 = [0.2, 0.4] shear2 = [0.3, 0.5] # Set up radial values bins_radians = [0.002, 0.003, 0.004] bin_units = 'radians' # create cluster cluster = clmm.GalaxyCluster( unique_id='test', 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'))) cluster.compute_tangential_and_cross_components() cluster.make_radial_profile(bin_units, bins=bins_radians, include_empty_bins=True) # missing profile name assert_raises(ValueError, cluster.plot_profiles, table_name='made_up_table') # missing shear component assert_raises(ValueError, cluster.plot_profiles, cross_component='made_up_component') # check basic plot is working cluster.plot_profiles() # check it passes missing a component error cluster.plot_profiles(cross_component_error='made_up_component')
def test_initialization(): testdict1 = {'unique_id': '1', 'ra': 161.3, 'dec': 34., 'z': 0.3, 'galcat': Table()} cl1 = clmm.GalaxyCluster(**testdict1) testing.assert_equal(testdict1['unique_id'], cl1.unique_id) testing.assert_equal(testdict1['ra'], cl1.ra) testing.assert_equal(testdict1['dec'], cl1.dec) testing.assert_equal(testdict1['z'], cl1.z) assert isinstance(cl1.galcat, Table)
def test_save_load(): cl1 = clmm.GalaxyCluster(unique_id='1', ra=161.3, dec=34., z=0.3, galcat=Table()) cl1.save('testcluster.pkl') cl2 = clmm.load_cluster('testcluster.pkl') os.system('rm testcluster.pkl') testing.assert_equal(cl2.unique_id, cl1.unique_id) testing.assert_equal(cl2.ra, cl1.ra) testing.assert_equal(cl2.dec, cl1.dec) testing.assert_equal(cl2.z, cl1.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_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")
filters=(coord_filters + z_filters + mag_filters)) # To compute a reduced tangential shear profile using CLMM, we first need to transform the shear into ellipticities. e1, e2 = clmm.utils.convert_shapes_to_epsilon(gal_cat['shear_1'], gal_cat['shear_2'], shape_definition='shear', kappa=gal_cat['convergence']) #store the results into an CLMM GCData (currently it's simply an astropy table) dat = clmm.GCData([ gal_cat['ra'], gal_cat['dec'], e1, e2, gal_cat['redshift'], gal_cat['galaxy_id'] ], names=('ra', 'dec', 'e1', 'e2', 'z', 'id')) cl = clmm.GalaxyCluster(str(id_cl), ra_cl, dec_cl, z_cl, dat) # Quick check of the redshift distribution of the galaxies in the catalog print(f'Number of galaxies in the catalog: Ngal = {len(cl.galcat)}') plt.figure() plt.hist(cl.galcat['z'], bins=30) plt.xlabel('z') plt.ylabel('Count') plt.savefig(outpath + 'redshift.png') plt.close() # Compute the tangential and cross shear profile bin_edges = clmm.dataops.make_bins(0.15, 10, 15, method='evenlog10width') # in Mpc cl.compute_tangential_and_cross_components(geometry="flat") cl.make_radial_profile("Mpc",
# #### To compute a reduced tangential shear profile using CLMM, we first need to transform the shear into ellipticities. # - The CLMM function `convert_shapes_to_epsilon` convert any shape measurements into the corresponding ellipticities ($\epsilon$ definition). # - Then, we build the astropy table of the galaxy catalog that will be used to instantiate a CLMM GalaxyCluster object. # In[12]: e1, e2 = clmm.utils.convert_shapes_to_epsilon(gal_cat['shear_1'],gal_cat['shear_2'], shape_definition='shear',kappa=gal_cat['convergence']) #store the results into an CLMM GCData (currently it's simply an astropy table) dat = clmm.GCData([gal_cat['ra'],gal_cat['dec'], e1, e2, gal_cat['redshift'],gal_cat['galaxy_id']], names=('ra','dec', 'e1', 'e2', 'z','id')) cl = clmm.GalaxyCluster(str(id_cl), ra_cl, dec_cl, z_cl, dat) # In[20]: # Quick check of the redshift distribution of the galaxies in the catalog print(f'Number of galaxies in the catalog: Ngal = {len(cl.galcat)}') #plt.rc("text", usetex=True) #plt.rc("font", size=18, family="serif") #plot number versus z #fig = plt.figure() plt.hist(cl.galcat['z'], bins=30);
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_print_gc(): cl = clmm.GalaxyCluster(unique_id='1', ra=161.3, dec=34., z=0.3, galcat=Table()) print(cl) assert isinstance(cl.__str__(), str)
def test_make_shear_profiles(): # Set up a cluster object and compute cross and tangential shears ra_lens, dec_lens = 120., 42. ra_source_list = np.array([120.1, 119.9, 119.9]) dec_source_list = np.array([41.9, 42.2, 42.2]) shear1 = np.array([0.2, 0.4, 0.4]) shear2 = np.array([0.3, 0.5, 0.5]) z_sources = np.ones(3) cluster = clmm.GalaxyCluster( unique_id='blah', ra=ra_lens, dec=dec_lens, z=0.5, galcat=Table( [ra_source_list, dec_source_list, shear1, shear2, z_sources], names=('ra', 'dec', 'e1', 'e2', 'z'))) # Test error of missing redshift cluster_noz = clmm.GalaxyCluster( unique_id='blah', ra=ra_lens, dec=dec_lens, z=0.5, galcat=Table([ra_source_list, dec_source_list, shear1, shear2], names=('ra', 'dec', 'e1', 'e2'))) cluster_noz.compute_shear() testing.assert_raises(TypeError, pa.make_shear_profile, cluster_noz, 'radians', 'radians') # Test error of missing shear testing.assert_raises(TypeError, pa.make_shear_profile, cluster, 'radians', 'radians') angsep, tshear, xshear = pa.compute_shear(cluster=cluster, add_to_cluster=True) # Test the outputs of compute_shear 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") # Tests passing int as bins arg makes the correct bins bins = 2 vec_bins = clmm.utils.make_bins(np.min(cluster.galcat['theta']), np.max(cluster.galcat['theta']), bins) testing.assert_array_equal( pa.make_shear_profile(cluster, 'radians', 'radians', bins=bins), pa.make_shear_profile(cluster, 'radians', 'radians', bins=vec_bins)) # Make the shear profile and check it bins_radians = np.array([0.002, 0.003, 0.004]) expected_radius = [0.0021745039090962414, 0.0037238407383072053] # remember that include_empty_bins=False excludes all bins with N>=1 profile = pa.make_shear_profile(cluster, 'radians', 'radians', bins=bins_radians, include_empty_bins=False) testing.assert_allclose(profile['radius_min'], bins_radians[1], **TOLERANCE, err_msg="Minimum radius in bin not expected.") testing.assert_allclose(profile['radius'], expected_radius[1], **TOLERANCE, err_msg="Mean radius in bin not expected.") testing.assert_allclose(profile['radius_max'], bins_radians[2], **TOLERANCE, err_msg="Maximum radius in bin not expected.") testing.assert_allclose(profile['gt'], expected_tan_shear[1], **TOLERANCE, err_msg="Tangential shear in bin not expected") testing.assert_allclose(profile['gx'], expected_cross_shear[1], **TOLERANCE, err_msg="Cross shear in bin not expected") testing.assert_array_equal(profile['n_src'], [2]) # Repeat the same tests when we call make_shear_profile through the GalaxyCluster method profile2 = cluster.make_shear_profile('radians', 'radians', bins=bins_radians, include_empty_bins=False) testing.assert_allclose(profile2['radius_min'], bins_radians[1], **TOLERANCE, err_msg="Minimum radius in bin not expected.") testing.assert_allclose(profile2['radius'], expected_radius[1], **TOLERANCE, err_msg="Mean radius in bin not expected.") testing.assert_allclose(profile2['radius_max'], bins_radians[2], **TOLERANCE, err_msg="Maximum radius in bin not expected.") testing.assert_allclose(profile2['gt'], expected_tan_shear[1], **TOLERANCE, err_msg="Tangential shear in bin not expected") testing.assert_allclose(profile2['gx'], expected_cross_shear[1], **TOLERANCE, err_msg="Cross shear in bin not expected") testing.assert_array_equal(profile['n_src'], [2]) # including empty bins profile3 = pa.make_shear_profile(cluster, 'radians', 'radians', bins=bins_radians, include_empty_bins=True) testing.assert_allclose(profile3['radius_min'], bins_radians[:-1], **TOLERANCE, err_msg="Minimum radius in bin not expected.") testing.assert_allclose(profile3['radius'], expected_radius, **TOLERANCE, err_msg="Mean radius in bin not expected.") testing.assert_allclose(profile3['radius_max'], bins_radians[1:], **TOLERANCE, err_msg="Maximum radius in bin not expected.") testing.assert_allclose(profile3['gt'], expected_tan_shear[:-1], **TOLERANCE, err_msg="Tangential shear in bin not expected") testing.assert_allclose(profile3['gx'], expected_cross_shear[:-1], **TOLERANCE, err_msg="Cross shear in bin not expected") testing.assert_array_equal(profile3['n_src'], [1, 2]) # Repeat the same tests when we call make_shear_profile through the GalaxyCluster method profile4 = cluster.make_shear_profile('radians', 'radians', bins=bins_radians, include_empty_bins=True) testing.assert_allclose(profile4['radius_min'], bins_radians[:-1], **TOLERANCE, err_msg="Minimum radius in bin not expected.") testing.assert_allclose(profile4['radius'], expected_radius, err_msg="Mean radius in bin not expected.") testing.assert_allclose(profile4['radius_max'], bins_radians[1:], **TOLERANCE, err_msg="Maximum radius in bin not expected.") testing.assert_allclose(profile4['gt'], expected_tan_shear[:-1], **TOLERANCE, err_msg="Tangential shear in bin not expected") testing.assert_allclose(profile4['gx'], expected_cross_shear[:-1], **TOLERANCE, err_msg="Cross shear in bin not expected") testing.assert_array_equal(profile4['n_src'], [1, 2])
def test_compute_shear(): # Input values ra_lens, dec_lens = 120., 42. ra_source_list = np.array([120.1, 119.9]) dec_source_list = np.array([41.9, 42.2]) shear1 = np.array([0.2, 0.4]) shear2 = np.array([0.3, 0.5]) # Make GalaxyCluster object cluster = clmm.GalaxyCluster( unique_id='blah', ra=ra_lens, dec=dec_lens, z=0.5, galcat=Table([ra_source_list, dec_source_list, shear1, shear2], names=('ra', 'dec', 'e1', 'e2'))) # 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]) # Pass arrays directly into function angsep, tshear, xshear = pa.compute_shear(ra_lens=ra_lens, dec_lens=dec_lens, ra_source_list=ra_source_list, dec_source_list=dec_source_list, shear1=shear1, shear2=shear2, add_to_cluster=False) 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 cluster object into the function angsep2, tshear2, xshear2 = pa.compute_shear(cluster=cluster) testing.assert_allclose( angsep2, expected_angsep, **TOLERANCE, err_msg="Angular Separation not correct when passing cluster") testing.assert_allclose( tshear2, expected_tangential_shear, **TOLERANCE, err_msg="Tangential Shear not correct when passing cluster") testing.assert_allclose( xshear2, expected_cross_shear, **TOLERANCE, err_msg="Cross Shear not correct when passing cluster") # Use the cluster method angsep3, tshear3, xshear3 = cluster.compute_shear() 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")