def test_readbkg(self): """ Test reading of a redmapper background file. """ file_name, file_path = 'test_bkg.fit', 'data_for_tests' # test that we fail if we try a non-existent file self.assertRaises(IOError, Background, 'nonexistent.fit') # test that we fail if we read a non-fits file self.assertRaises(IOError, Background,'%s/testconfig.yaml' % (file_path)) # test that we fail if we try a file without the right header info self.assertRaises(IOError, Background, '%s/test_dr8_pars.fit' % (file_path)) bkg = Background('%s/%s' % (file_path, file_name)) # test creation of lookup table inputs = [(172,15,64), (323,3,103), (9,19,21), (242,4,87), (70,12,58), (193,6,39), (87,14,88), (337,5,25), (333,8,9)] py_outputs = np.array([bkg.sigma_g[idx] for idx in inputs]) idl_outputs = np.array([0.32197464, 6.4165196, 0.0032830855, 1.4605126, 0.0098356586, 0.79848081, 0.011284498, 9.3293247, 8.7064905]) testing.assert_almost_equal(py_outputs, idl_outputs, decimal=1) # test functionality of lookup table z = 0.23185321 chisq = np.array([0.13315917, 3.57059131, 3.71567741, 2.46307987, 9.16647519, 8.24240144, -1., 1.19503491]) refmag = np.array([1000., 15.05129281, 16.81049236, 18.07566359, 19.88279, 15.56617587, 18.55626717, 15.00271158]) py_outputs = bkg.sigma_g_lookup(z, chisq, refmag) idl_outputs = np.array([np.inf, 0.0012997627, 0.56412143, 6.4126010, 43.4550, 0.012194233, np.inf, 0.0]) # idl_ouputs[4] = 42.555183 testing.assert_almost_equal(py_outputs, idl_outputs, decimal=4) ########################################### ## And test the zred background code ########################################### zredbkg = ZredBackground('%s/%s' % (file_path, file_name)) # test creation of lookup table inputs = [(60, 50), (200, 100), (300, 120)] py_outputs = np.array([zredbkg.sigma_g[idx] for idx in inputs]) idl_outputs = np.array([1.16810, 28.4379, 373.592]) testing.assert_almost_equal(py_outputs, idl_outputs, decimal=4) # test functionality of lookup table zred = np.array([0.2154, 0.2545, 0.2876]) refmag = np.array([18.015,18.576,19.234]) idl_outputs = np.array([710.17102,1000.1127,1718.0394]) py_outputs = zredbkg.sigma_g_lookup(zred, refmag) testing.assert_almost_equal(py_outputs, idl_outputs, decimal=3)
def _setup_cluster(self): """ Set up the cluster to run through the centering code. """ file_path = 'data_for_tests' cluster = Cluster() cluster.config = Configuration( os.path.join(file_path, 'testconfig.yaml')) tempcat = fitsio.read( os.path.join(file_path, 'test_wcen_zred_data.fit')) temp_neighbors = np.zeros(tempcat[0]['RAS'].size, dtype=[('RA', 'f8'), ('DEC', 'f8'), ('DIST', 'f4'), ('R', 'f4'), ('P', 'f4'), ('PFREE', 'f4'), ('PMEM', 'f4'), ('MAG', 'f4', 5), ('MAG_ERR', 'f4', 5), ('REFMAG', 'f4'), ('REFMAG_ERR', 'f4'), ('CHISQ', 'f4'), ('ZRED', 'f4'), ('ZRED_E', 'f4'), ('ZRED_CHISQ', 'f4')]) temp_neighbors['RA'] = tempcat[0]['RAS'] temp_neighbors['DEC'] = tempcat[0]['DECS'] temp_neighbors['R'] = tempcat[0]['R'] temp_neighbors['P'] = tempcat[0]['PVALS'] temp_neighbors['PFREE'] = tempcat[0]['WVALS'] temp_neighbors['PMEM'] = tempcat[0]['WTVALS'] temp_neighbors['REFMAG'] = tempcat[0]['REFMAG_TOTAL'] temp_neighbors['ZRED'] = tempcat[0]['GZREDS'] temp_neighbors['ZRED_E'] = tempcat[0]['GZREDE'] temp_neighbors['ZRED_CHISQ'] = tempcat[0]['GCHISQ'] temp_neighbors['DIST'] = tempcat[0]['R'] / ( np.radians(1.) * cluster.config.cosmo.Da(0, tempcat[0]['ZCLUSTER'])) neighbors = GalaxyCatalog(temp_neighbors) cluster.set_neighbors(neighbors) zred_filename = 'test_dr8_pars.fit' cluster.zredstr = RedSequenceColorPar(os.path.join( file_path, 'test_dr8_pars.fit'), fine=True, zrange=[0.25, 0.35]) cluster.bkg = Background(os.path.join(file_path, 'test_bkg.fit')) cluster.zredbkg = ZredBackground( os.path.join(file_path, 'test_bkg.fit')) cluster.redshift = tempcat[0]['ZCLUSTER'] cluster.ra = tempcat[0]['RAC'] cluster.dec = tempcat[0]['DECC'] cluster.r_lambda = 1.0 * (tempcat[0]['LAMBDA'] / 100.0)**0.2 cluster.Lambda = tempcat[0]['LAMBDA'] cluster.scaleval = tempcat[0]['SCALEVAL'] return cluster
def runTest(self): """ First test the filters: nfw, lum, and bkg """ # all new... file_path = 'data_for_tests' cluster = Cluster() conf_filename = 'testconfig.yaml' cluster.config = Configuration(file_path + '/' + conf_filename) filename = 'test_cluster_members.fit' neighbors = GalaxyCatalog.from_fits_file(file_path + '/' + filename) cluster.set_neighbors(neighbors) zred_filename = 'test_dr8_pars.fit' cluster.zredstr = RedSequenceColorPar(file_path + '/' + zred_filename, fine=True) bkg_filename = 'test_bkg.fit' cluster.bkg = Background('%s/%s' % (file_path, bkg_filename)) hdr=fitsio.read_header(file_path+'/'+filename,ext=1) #cluster.z = hdr['Z'] #cluster.update_z(hdr['Z']) cluster.redshift = hdr['Z'] richness_compare = hdr['LAMBDA'] richness_compare_err = hdr['LAMBDA_E'] scaleval_compare = hdr['SCALEVAL'] cpars_compare = np.array([hdr['CPARS0'], hdr['CPARS1'], hdr['CPARS2'], hdr['CPARS3']]) cval_compare = hdr['CVAL'] mstar_compare = hdr['MSTAR'] cluster.ra = hdr['RA'] cluster.dec = hdr['DEC'] mask = HPMask(cluster.config) mask.set_radmask(cluster) depthstr = DepthMap(cluster.config) depthstr.calc_maskdepth(mask.maskgals, cluster.ra, cluster.dec, cluster.mpc_scale) # Test the NFW profile on its own # (this works to 5 decimal places because of the 2*pi*r scaling) nfw_python = cluster._calc_radial_profile() testing.assert_almost_equal(nfw_python, neighbors.nfw/(2.*np.pi*neighbors.r),5) # Test the luminosity # The problem is that the IDL code has multiple ways of computing the index. # And this should be fixed here. I don't want to duplicate dumb ideas. #mstar = cluster.zredstr.mstar(cluster.z) #testing.assert_almost_equal(mstar, mstar_compare, 3) #maxmag = mstar - 2.5*np.log10(cluster.config.lval_reference) #lum_python = cluster._calc_luminosity(maxmag) #testing.assert_almost_equal(lum_python, neighbors.lumwt, 3) # Test theta_i #theta_i_python = calc_theta_i(neighbors.refmag, neighbors.refmag_err, # maxmag, cluster.zredstr.limmag) #testing.assert_almost_equal(theta_i_python, neighbors.theta_i, 3) # Test the background # Note that this uses the input chisq values bkg_python = cluster.calc_bkg_density(cluster.neighbors.r, cluster.neighbors.chisq, cluster.neighbors.refmag) # this is cheating here... to_test, = np.where((cluster.neighbors.refmag < cluster.bkg.refmagbins[-1])) #testing.assert_almost_equal(bkg_python[to_test], neighbors.bcounts[to_test], 3) #testing.assert_allclose(bkg_python[to_test], neighbors.bcounts[to_test], # rtol=1e-4, atol=0) # skip this test for now. Blah. # Now the cluster tests # cluster.neighbors.dist = np.degrees(cluster.neighbors.r / cluster.cosmo.Da(0, cluster.redshift)) seed = 0 random.seed(seed = 0) richness = cluster.calc_richness(mask) # these are regression tests. Various mask issues make the matching # to idl for the time being testing.assert_almost_equal(cluster.Lambda, 23.86299324) testing.assert_almost_equal(cluster.lambda_e, 2.4780307) #testing.assert_almost_equal(cluster.neighbors.theta_i, # neighbors.theta_i, 3) #testing.assert_almost_equal(cluster.neighbors.theta_r, # neighbors.theta_r, 3) #testing.assert_almost_equal(cluster.neighbors.p, # neighbors.p, 3) return
def runTest(self): file_path = 'data_for_tests' conffile = 'testconfig.yaml' config = Configuration(file_path + '/' + conffile) gals_all = GalaxyCatalog.from_galfile(config.galfile) zred_filename = 'test_dr8_pars.fit' zredstr = RedSequenceColorPar(file_path + '/' + zred_filename, fine=True) bkg_filename = 'test_bkg.fit' bkg = Background('%s/%s' % (file_path, bkg_filename)) mask = HPMask(config) depthstr = DepthMap(config) #cosmo = Cosmo() testcatfile = 'test_cluster_pos.fit' cat = ClusterCatalog.from_catfile(file_path + '/' + testcatfile, zredstr=zredstr, config=config, bkg=bkg) # test single neighbors... c0 = cat[0] c0.find_neighbors(0.2, gals_all) c1 = cat[1] c1.find_neighbors(0.2, gals_all) testing.assert_equal(c0.neighbors.size, 580) testing.assert_equal(c1.neighbors.size, 298) testing.assert_array_less(c0.neighbors.dist, 0.2) testing.assert_array_less(c1.neighbors.dist, 0.2) # and multi-match... i0, i1, dist = gals_all.match_many(cat.ra, cat.dec, 0.2) u0, = np.where(i0 == 0) testing.assert_equal(c0.neighbors.size, u0.size) u1, = np.where(i0 == 1) testing.assert_equal(c1.neighbors.size, u1.size) # and compute the richness on the first one... mask.set_radmask(c0) depthstr.calc_maskdepth(mask.maskgals, c0.ra, c0.dec, c0.mpc_scale) richness = c0.calc_richness(mask) # Make sure the numbers were propagated to the parent catalog testing.assert_equal(richness, cat.Lambda[0]) testing.assert_equal(c0.Lambda_e, cat.Lambda_e[0]) testing.assert_equal(c0.scaleval, cat.scaleval[0]) # And make sure the numbers are correct testing.assert_almost_equal(richness, 23.86299324)
def test_readbkg(self): """ Part one of this file checks that the background file exists and is formatted correctly. Then we load the background file and pull out some sources in the background, and compare the IDL outputs to thoe of the sigma_g attribute of the background. The initialization of the sigma_g values as well as the 'lookup' or spline used over these valus is also tested. Note that the background is characterized by sigma_g as functions of (z,chisq,refmag). """ file_name, file_path = 'test_bkg.fit', 'data_for_tests' # test that we fail if we try a non-existent file self.assertRaises(IOError, Background, 'nonexistent.fit') # test that we fail if we read a non-fits file self.assertRaises(IOError, Background,'%s/testconfig.yaml' % (file_path)) # test that we fail if we try a file without the right header info self.assertRaises(IOError, Background, '%s/test_dr8_pars.fit' % (file_path)) bkg = Background('%s/%s' % (file_path, file_name)) # test creation of lookup table inputs = [(172,15,64), (323,3,103), (9,19,21), (242,4,87), (70,12,58), (193,6,39), (87,14,88), (337,5,25), (333,8,9)] py_outputs = np.array([bkg.sigma_g[idx] for idx in inputs]) idl_outputs = np.array([0.32197464, 6.4165196, 0.0032830855, 1.4605126, 0.0098356586, 0.79848081, 0.011284498, 9.3293247, 8.7064905]) testing.assert_almost_equal(py_outputs, idl_outputs, decimal=1) # test functionality of lookup table z = 0.23185321 chisq = np.array([0.13315917, 3.57059131, 3.71567741, 2.46307987, 9.16647519, 8.24240144, -1., 1.19503491]) refmag = np.array([1000., 15.05129281, 16.81049236, 18.07566359, 19.88279, 15.56617587, 18.55626717, 15.00271158]) py_outputs = bkg.sigma_g_lookup(z, chisq, refmag) idl_outputs = np.array([np.inf, 0.0012997627, 0.56412143, 6.4126010, 43.4550, 0.012194233, np.inf, 0.0]) # idl_ouputs[4] = 42.555183 testing.assert_almost_equal(py_outputs, idl_outputs, decimal=4) ########################################### ## And test the zred background code ########################################### zredbkg = ZredBackground('%s/%s' % (file_path, file_name)) # test creation of lookup table inputs = [(60, 50), (200, 100), (300, 120)] py_outputs = np.array([zredbkg.sigma_g[idx] for idx in inputs]) idl_outputs = np.array([1.16810, 28.4379, 373.592]) testing.assert_almost_equal(py_outputs, idl_outputs, decimal=4) # test functionality of lookup table zred = np.array([0.2154, 0.2545, 0.2876]) refmag = np.array([18.015,18.576,19.234]) idl_outputs = np.array([710.17102,1000.1127,1718.0394]) py_outputs = zredbkg.sigma_g_lookup(zred, refmag) testing.assert_almost_equal(py_outputs, idl_outputs, decimal=3)
def runTest(self): """ Run the ClusterTest """ # all new... random.seed(seed=12345) file_path = 'data_for_tests' cluster = Cluster() conf_filename = 'testconfig.yaml' cluster.config = Configuration(file_path + '/' + conf_filename) filename = 'test_cluster_members.fit' neighbors = GalaxyCatalog.from_fits_file(file_path + '/' + filename) cluster.set_neighbors(neighbors) zred_filename = 'test_dr8_pars.fit' cluster.zredstr = RedSequenceColorPar(file_path + '/' + zred_filename, fine=True) bkg_filename = 'test_bkg.fit' cluster.bkg = Background('%s/%s' % (file_path, bkg_filename)) hdr = fitsio.read_header(file_path + '/' + filename, ext=1) cluster.redshift = hdr['Z'] richness_compare = hdr['LAMBDA'] richness_compare_err = hdr['LAMBDA_E'] scaleval_compare = hdr['SCALEVAL'] cpars_compare = np.array( [hdr['CPARS0'], hdr['CPARS1'], hdr['CPARS2'], hdr['CPARS3']]) cval_compare = hdr['CVAL'] mstar_compare = hdr['MSTAR'] cluster.ra = hdr['RA'] cluster.dec = hdr['DEC'] mask = HPMask(cluster.config) maskgal_index = mask.select_maskgals_sample(maskgal_index=0) mask.set_radmask(cluster) depthstr = DepthMap(cluster.config) depthstr.calc_maskdepth(mask.maskgals, cluster.ra, cluster.dec, cluster.mpc_scale) # Test the NFW profile on its own # (this works to 5 decimal places because of the 2*pi*r scaling) nfw_python = cluster._calc_radial_profile() testing.assert_almost_equal(nfw_python, neighbors.nfw / (2. * np.pi * neighbors.r), 5) # Test the background # Note that this uses the input chisq values bkg_python = cluster.calc_bkg_density(cluster.neighbors.r, cluster.neighbors.chisq, cluster.neighbors.refmag) # this is cheating here... to_test, = np.where( (cluster.neighbors.refmag < cluster.bkg.refmagbins[-1])) seed = 0 random.seed(seed=0) richness = cluster.calc_richness(mask) # these are regression tests. Various mask issues make the matching # to idl for the time being testing.assert_almost_equal(cluster.Lambda, 24.366407, 5) testing.assert_almost_equal(cluster.lambda_e, 2.5137918, 5) return
def runTest(self): """ Run tests on redmapper.Zlambda """ random.seed(seed=12345) file_path = 'data_for_tests' conf_filename = 'testconfig.yaml' config = Configuration(file_path + '/' + conf_filename) filename = 'test_cluster_members.fit' neighbors = GalaxyCatalog.from_fits_file(file_path + '/' + filename) zred_filename = 'test_dr8_pars.fit' zredstr = RedSequenceColorPar(file_path + '/' + zred_filename,fine = True) bkg_filename = 'test_bkg.fit' bkg = Background('%s/%s' % (file_path, bkg_filename)) cluster = Cluster(config=config, zredstr=zredstr, bkg=bkg, neighbors=neighbors) hdr=fitsio.read_header(file_path+'/'+filename,ext=1) cluster.redshift = hdr['Z'] richness_compare = hdr['LAMBDA'] richness_compare_err = hdr['LAMBDA_E'] cluster.ra = hdr['RA'] cluster.dec = hdr['DEC'] #Set up the mask mask = HPMask(cluster.config) #Create the mask maskgal_index = mask.select_maskgals_sample(maskgal_index=0) mask.set_radmask(cluster) #depthstr depthstr = DepthMap(cluster.config) depthstr.calc_maskdepth(mask.maskgals, cluster.ra, cluster.dec, cluster.mpc_scale) cluster.neighbors.dist = np.degrees(cluster.neighbors.r/cluster.cosmo.Dl(0,cluster.redshift)) # make a zlambda object zlam = Zlambda(cluster) z_lambda, z_lambda_e = zlam.calc_zlambda(cluster.redshift, mask, calc_err=True, calcpz=True) # I am not sure why this isn't repeatable better than this testing.assert_almost_equal(cluster.z_lambda, 0.22666427, 6) testing.assert_almost_equal(cluster.z_lambda_err, 0.00443601, 4) # zlambda_err test z_lambda_err = zlam._zlambda_calc_gaussian_err(cluster.z_lambda) testing.assert_almost_equal(z_lambda_err, 0.0063738347, 5) # and test the correction on its own corr_filename = 'test_dr8_zlambdacorr.fit' zlambda_corr = ZlambdaCorrectionPar(file_path + '/' + corr_filename, zlambda_pivot=30.0) zlam_in = 0.227865 zlam_e_in = 0.00629995 zlam_out = 0.228654 zlam_e_out = 0.00840213 zlam_new, zlam_e_new = zlambda_corr.apply_correction(24.5, zlam_in, zlam_e_in) testing.assert_almost_equal(zlam_new, zlam_out, 5) testing.assert_almost_equal(zlam_e_new, zlam_e_out, 5)
def runTest(self): file_path = 'data_for_tests' #The configuration #Used by the mask, background and richness calculation conf_filename = 'testconfig.yaml' config = Configuration(file_path + '/' + conf_filename) filename = 'test_cluster_members.fit' neighbors = GalaxyCatalog.from_fits_file(file_path + '/' + filename) zred_filename = 'test_dr8_pars.fit' zredstr = RedSequenceColorPar(file_path + '/' + zred_filename, fine=True) bkg_filename = 'test_bkg.fit' bkg = Background('%s/%s' % (file_path, bkg_filename)) cluster = Cluster(config=config, zredstr=zredstr, bkg=bkg, neighbors=neighbors) hdr = fitsio.read_header(file_path + '/' + filename, ext=1) #cluster.z = hdr['Z'] #cluster.update_z(hdr['Z']) cluster.redshift = hdr['Z'] richness_compare = hdr['LAMBDA'] richness_compare_err = hdr['LAMBDA_E'] cluster.ra = hdr['RA'] cluster.dec = hdr['DEC'] #Set up the mask mask = HPMask(cluster.config) #Create the mask mask.set_radmask(cluster) #depthstr depthstr = DepthMap(cluster.config) depthstr.calc_maskdepth(mask.maskgals, cluster.ra, cluster.dec, cluster.mpc_scale) cluster.neighbors.dist = np.degrees( cluster.neighbors.r / cluster.cosmo.Dl(0, cluster.redshift)) #set seed seed = 0 random.seed(seed=seed) # make a zlambda object zlam = Zlambda(cluster) z_lambda, z_lambda_e = zlam.calc_zlambda(cluster.redshift, mask, calc_err=True, calcpz=True) testing.assert_almost_equal(cluster.z_lambda, 0.22700983) testing.assert_almost_equal(cluster.z_lambda_err, 0.00448909596) # zlambda_err test z_lambda_err = zlam._zlambda_calc_gaussian_err(cluster.z_lambda) testing.assert_almost_equal(z_lambda_err, 0.006303830) # and test the correction on its own corr_filename = 'test_dr8_zlambdacorr.fit' zlambda_corr = ZlambdaCorrectionPar(file_path + '/' + corr_filename, 30.0) zlam_in = 0.227865 zlam_e_in = 0.00629995 zlam_out = 0.228654 zlam_e_out = 0.00840213 zlam_new, zlam_e_new = zlambda_corr.apply_correction( 24.5, zlam_in, zlam_e_in) testing.assert_almost_equal(zlam_new, zlam_out, 5) testing.assert_almost_equal(zlam_e_new, zlam_e_out, 5)
def runTest(self): file_path = 'data_for_tests' cluster = Cluster() conf_filename = 'testconfig.yaml' cluster.config = Configuration(file_path + '/' + conf_filename) filename = 'test_wcen_zred_data.fit' tempcat = fitsio.read(file_path + '/' + filename, ext=1) temp_neighbors = np.zeros(tempcat[0]['RAS'].size, dtype=[('RA', 'f8'), ('DEC', 'f8'), ('DIST', 'f4'), ('R', 'f4'), ('P', 'f4'), ('PFREE', 'f4'), ('PMEM', 'f4'), ('MAG', 'f4', 5), ('MAG_ERR', 'f4', 5), ('REFMAG', 'f4'), ('REFMAG_ERR', 'f4'), ('CHISQ', 'f4'), ('ZRED', 'f4'), ('ZRED_E', 'f4'), ('ZRED_CHISQ', 'f4')]) temp_neighbors['RA'] = tempcat[0]['RAS'] temp_neighbors['DEC'] = tempcat[0]['DECS'] temp_neighbors['R'] = tempcat[0]['R'] temp_neighbors['P'] = tempcat[0]['PVALS'] temp_neighbors['PFREE'] = tempcat[0]['WVALS'] temp_neighbors['PMEM'] = tempcat[0]['WTVALS'] temp_neighbors['REFMAG'] = tempcat[0]['REFMAG_TOTAL'] temp_neighbors['ZRED'] = tempcat[0]['GZREDS'] temp_neighbors['ZRED_E'] = tempcat[0]['GZREDE'] temp_neighbors['ZRED_CHISQ'] = tempcat[0]['GCHISQ'] temp_neighbors['DIST'] = tempcat[0]['R'] / ( np.radians(1.) * cluster.config.cosmo.Da(0, tempcat[0]['ZCLUSTER'])) neighbors = GalaxyCatalog(temp_neighbors) cluster.set_neighbors(neighbors) zred_filename = 'test_dr8_pars.fit' cluster.zredstr = RedSequenceColorPar(file_path + '/' + zred_filename, fine=True) bkg_filename = 'test_bkg.fit' cluster.bkg = Background('%s/%s' % (file_path, bkg_filename)) cluster.zredbkg = ZredBackground('%s/%s' % (file_path, bkg_filename)) cluster.redshift = tempcat[0]['ZCLUSTER'] cluster.ra = tempcat[0]['RAC'] cluster.dec = tempcat[0]['DECC'] cluster.r_lambda = 1.0 * (tempcat[0]['LAMBDA'] / 100.0)**0.2 cluster.Lambda = tempcat[0]['LAMBDA'] cluster.scaleval = tempcat[0]['SCALEVAL'] corr_filename = 'test_dr8_zlambdacorr.fit' zlambda_corr = ZlambdaCorrectionPar(file_path + '/' + corr_filename, 30.0) # And the meat of it... cent = CenteringWcenZred(cluster, zlambda_corr=zlambda_corr) cent.find_center() testing.assert_almost_equal(cent.p_cen, tempcat[0]['PCEN'][tempcat[0]['GOOD']], 5) testing.assert_almost_equal(cent.q_cen, tempcat[0]['QCEN'][tempcat[0]['GOOD']], 4) testing.assert_almost_equal(cent.p_sat, tempcat[0]['PSAT'], 4) testing.assert_almost_equal(cent.p_fg, tempcat[0]['PFG'], 4) testing.assert_array_equal(cent.index, tempcat[0]['USE'][tempcat[0]['GOOD']])