Ejemplo n.º 1
0
    def test_zred_runpixels(self):
        """
        Test redmapper.ZredRunPixels, computing zreds for all the galaxies
        in a pixelized galaxy catalog.
        """

        file_path = 'data_for_tests'
        configfile = 'testconfig.yaml'

        config = Configuration(os.path.join(file_path, configfile))

        config.d.hpix = 2163
        config.d.nside = 64
        config.border = 0.0

        self.test_dir = tempfile.mkdtemp(dir='./', prefix='TestRedmapper-')
        config.zredfile = os.path.join(self.test_dir, 'zreds', 'testing_zreds_master_table.fit')

        # FIXME: try an illegal one...

        zredRunpix = ZredRunPixels(config)
        zredRunpix.run()

        # Check that the zred file has been built...

        self.assertTrue(os.path.isfile(config.zredfile))

        # Read in just the galaxies...
        gals0 = GalaxyCatalog.from_galfile(config.galfile, nside=config.d.nside, hpix=config.d.hpix, border=config.border)

        # And with the zreds...
        gals = GalaxyCatalog.from_galfile(config.galfile, zredfile=config.zredfile, nside=config.d.nside, hpix=config.d.hpix, border=config.border)

        # Confirm they're the same galaxies...
        testing.assert_array_almost_equal(gals0.ra, gals.ra)

        # Confirm the zreds are okay
        self.assertGreater(np.min(gals.zred), 0.0)
        self.assertGreater(np.min(gals.chisq), 0.0)
        self.assertLess(np.max(gals.lkhd), 0.0)

        zredfile = os.path.join(file_path, 'zreds_test', 'dr8_test_zreds_master_table.fit')
        gals_compare = GalaxyCatalog.from_galfile(config.galfile, zredfile=zredfile,
                                                  nside=config.d.nside, hpix=config.d.hpix, border=config.border)

        zredstr = RedSequenceColorPar(config.parfile)
        mstar_input = zredstr.mstar(gals_compare.zred_uncorr)
        mstar = zredstr.mstar(gals_compare.zred_uncorr)

        ok, = np.where((gals_compare.refmag < (mstar_input - 2.5*np.log10(0.15))) |
                       (gals_compare.refmag < (mstar - 2.5*np.log10(0.15))))

        delta_zred_uncorr = gals.zred_uncorr[ok] - gals_compare.zred_uncorr[ok]

        use, = np.where(np.abs(delta_zred_uncorr) < 1e-3)
        testing.assert_array_less(0.98, float(use.size) / float(ok.size))
Ejemplo n.º 2
0
    def test_zred_runcat(self):
        """
        Test redmapper.ZredRunCatalog, computing zreds for all the galaxies in
        a single catalog file.
        """

        file_path = 'data_for_tests'
        configfile = 'testconfig.yaml'

        config = Configuration(os.path.join(file_path, configfile))

        self.test_dir = tempfile.mkdtemp(dir='./', prefix='TestRedmapper-')
        config.outpath = self.test_dir
        outfile = os.path.join(self.test_dir, 'test_zred_out.fits')

        tab = fitsio.read(config.galfile, ext=1, lower=True)
        galfile = os.path.join(os.path.dirname(config.galfile),
                               tab[0]['filenames'][0].decode())

        zredRuncat = ZredRunCatalog(config)

        zredRuncat.run(galfile, outfile)

        # This exercises the reading code
        gals = GalaxyCatalog.from_galfile(galfile, zredfile=outfile)

        self.assertGreater(np.min(gals.zred), 0.0)
        self.assertGreater(np.min(gals.chisq), 0.0)
        self.assertLess(np.max(gals.lkhd), 0.0)

        # And compare to the "official" run...
        config.zredfile = os.path.join(file_path, 'zreds_test',
                                       'dr8_test_zreds_master_table.fit')
        ztab = fitsio.read(config.zredfile, ext=1, lower=True)
        zredfile = os.path.join(os.path.dirname(config.zredfile),
                                ztab[0]['filenames'][0].decode())

        gals_compare = GalaxyCatalog.from_galfile(galfile, zredfile=zredfile)

        zredstr = RedSequenceColorPar(config.parfile)
        mstar_input = zredstr.mstar(gals_compare.zred_uncorr)
        mstar = zredstr.mstar(gals_compare.zred_uncorr)

        ok, = np.where(
            (gals_compare.refmag < (mstar_input - 2.5 * np.log10(0.15)))
            | (gals_compare.refmag < (mstar - 2.5 * np.log10(0.15))))

        delta_zred_uncorr = gals.zred_uncorr[ok] - gals_compare.zred_uncorr[ok]

        use, = np.where(np.abs(delta_zred_uncorr) < 1e-3)
        testing.assert_array_less(0.98, float(use.size) / float(ok.size))
Ejemplo n.º 3
0
    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
Ejemplo n.º 4
0
    def test_zred(self):
        """
        Test redmapper.ZredColor, looping over galaxies.
        """

        file_path = 'data_for_tests'

        zred_filename = 'test_dr8_pars.fit'
        zredstr = RedSequenceColorPar(file_path + '/' + zred_filename)

        galaxy_filename = 'test_dr8_gals_with_zred.fit'
        galaxies = GalaxyCatalog.from_fits_file(file_path + '/' + galaxy_filename)

        galaxies_input = copy.deepcopy(galaxies)

        # start with the first one...
        zredc = ZredColor(zredstr)

        zredc.compute_zred(galaxies[0])

        starttime = time.time()
        zredc.compute_zreds(galaxies)

        print("Ran %d galaxies in %.3f seconds" % (galaxies.size,
                                                   time.time() - starttime))

        # Only compare galaxies that are brighter than 0.15L* in either old OR new
        # Otherwise, we're just comparing how the codes handle "out-of-range"
        # galaxies, and that does not matter
        mstar_input = zredstr.mstar(galaxies_input.zred_uncorr)
        mstar = zredstr.mstar(galaxies.zred_uncorr)

        ok, = np.where((galaxies.refmag < (mstar_input - 2.5*np.log10(0.15))) |
                       (galaxies.refmag < (mstar - 2.5*np.log10(0.15))))

        delta_zred_uncorr = galaxies.zred_uncorr[ok] - galaxies_input.zred_uncorr[ok]

        use, = np.where(np.abs(delta_zred_uncorr) < 1e-3)

        testing.assert_array_less(0.9, float(use.size) / float(ok.size))

        delta_zred = galaxies.zred[ok[use]] - galaxies_input.zred[ok[use]]
        use2, = np.where(np.abs(delta_zred) < 1e-3)
        testing.assert_array_less(0.99, float(use2.size) / float(delta_zred.size))

        delta_zred2 = galaxies.zred2[ok[use]] - galaxies_input.zred2[ok[use]]
        use2, = np.where(np.abs(delta_zred) < 1e-3)
        testing.assert_array_less(0.99, float(use2.size) / float(delta_zred2.size))

        delta_zred_uncorr_e = galaxies.zred_uncorr_e[ok[use]] - galaxies_input.zred_uncorr_e[ok[use]]
        use2, = np.where(np.abs(delta_zred_uncorr_e) < 1e-3)
        testing.assert_array_less(0.98, float(use2.size) / float(delta_zred_uncorr_e.size))

        delta_zred_e = galaxies.zred_e[ok[use]] - galaxies_input.zred_e[ok[use]]
        use2, = np.where(np.abs(delta_zred_e) < 1e-3)
        testing.assert_array_less(0.98, float(use2.size) / float(delta_zred_e.size))

        delta_zred2_e = galaxies.zred2_e[ok[use]] - galaxies_input.zred2_e[ok[use]]
        use2, = np.where(np.abs(delta_zred2_e) < 1e-3)
        testing.assert_array_less(0.98, float(use2.size) / float(delta_zred2_e.size))
Ejemplo n.º 5
0
    def test_zred_runcat(self):
        """
        Test redmapper.ZredRunCatalog, computing zreds for all the galaxies in
        a single catalog file.
        """

        file_path = 'data_for_tests'
        configfile = 'testconfig.yaml'

        config = Configuration(os.path.join(file_path, configfile))

        self.test_dir = tempfile.mkdtemp(dir='./', prefix='TestRedmapper-')
        config.outpath = self.test_dir
        outfile = os.path.join(self.test_dir, 'test_zred_out.fits')

        tab = fitsio.read(config.galfile, ext=1, lower=True)
        galfile = os.path.join(os.path.dirname(config.galfile), tab[0]['filenames'][0].decode())

        zredRuncat = ZredRunCatalog(config)

        zredRuncat.run(galfile, outfile)

        # This exercises the reading code
        gals = GalaxyCatalog.from_galfile(galfile, zredfile=outfile)

        self.assertGreater(np.min(gals.zred), 0.0)
        self.assertGreater(np.min(gals.chisq), 0.0)
        self.assertLess(np.max(gals.lkhd), 0.0)

        # And compare to the "official" run...
        config.zredfile = os.path.join(file_path, 'zreds_test', 'dr8_test_zreds_master_table.fit')
        ztab = fitsio.read(config.zredfile, ext=1, lower=True)
        zredfile = os.path.join(os.path.dirname(config.zredfile), ztab[0]['filenames'][0].decode())

        gals_compare = GalaxyCatalog.from_galfile(galfile, zredfile=zredfile)

        zredstr = RedSequenceColorPar(config.parfile)
        mstar_input = zredstr.mstar(gals_compare.zred_uncorr)
        mstar = zredstr.mstar(gals_compare.zred_uncorr)

        ok, = np.where((gals_compare.refmag < (mstar_input - 2.5*np.log10(0.15))) |
                       (gals_compare.refmag < (mstar - 2.5*np.log10(0.15))))

        delta_zred_uncorr = gals.zred_uncorr[ok] - gals_compare.zred_uncorr[ok]

        use, = np.where(np.abs(delta_zred_uncorr) < 1e-3)
        testing.assert_array_less(0.98, float(use.size) / float(ok.size))
Ejemplo n.º 6
0
    def test_galaxycatalog_read(self):
        """
        Run redmapper.GalaxyCatalog tests,
        """

        file_path = 'data_for_tests'

        galfile = 'pixelized_dr8_test/dr8_test_galaxies_master_table.fit'

        gals_all = GalaxyCatalog.from_galfile(file_path + '/' + galfile)

        # check that we got the expected number...
        testing.assert_equal(gals_all.size, 14449)

        # read in a subregion, no border
        gals_sub = GalaxyCatalog.from_galfile(file_path + '/' + galfile,
                                              hpix=2163, nside=64)

        theta = (90.0 - gals_all.dec) * np.pi/180.
        phi = gals_all.ra * np.pi/180.
        ipring_all = hp.ang2pix(64, theta, phi)
        use, = np.where(ipring_all == 2163)

        testing.assert_equal(gals_sub.size, use.size)

        # read in a subregion, with border
        gals_sub = GalaxyCatalog.from_galfile(file_path + '/' + galfile,
                                              hpix=9218, nside=128, border=0.1)

        # this isn't really a big enough sample catalog to fully test...
        testing.assert_equal(gals_sub.size, 2511)

        # and test the matching...

        indices, dists = gals_all.match_one(140.5, 65.0, 0.2)
        testing.assert_equal(indices.size, 521)
        testing.assert_array_less(dists, 0.2)

        i0, i1, dists = gals_all.match_many([140.5,141.2],
                                            [65.0, 65.2], [0.2,0.1])
        testing.assert_equal(i0.size, 666)
        test, = np.where(i0 == 0)
        testing.assert_equal(test.size, 521)
        testing.assert_array_less(dists[test], 0.2)
        test, = np.where(i0 == 1)
        testing.assert_equal(test.size, 666 - 521)
        testing.assert_array_less(dists[test], 0.1)
Ejemplo n.º 7
0
    def test_depthfit(self):
        """
        Test depth fitting with redmapper.DepthLim
        """

        file_path = "data_for_tests"
        conf_filename = "testconfig.yaml"
        config = Configuration(file_path + "/" + conf_filename)

        gals = GalaxyCatalog.from_galfile(config.galfile)

        np.random.seed(seed=12345)

        # First creation of depth object
        dlim = DepthLim(gals.refmag, gals.refmag_err)

        # This has been inspected that it makes sense.
        # Also, one should really be using a depth map

        testing.assert_almost_equal(dlim.initpars['EXPTIME'], 104.782066, 0)
        testing.assert_almost_equal(dlim.initpars['LIMMAG'], 20.64819717, 0)

        # And take a subpixel

        gals = GalaxyCatalog.from_galfile(config.galfile, hpix=8421, nside=128)

        config.mask_mode = 0
        mask = get_mask(config)
        maskgal_index = mask.select_maskgals_sample()
        mask.read_maskgals(config.maskgalfile)

        dlim.calc_maskdepth(mask.maskgals, gals.refmag, gals.refmag_err)

        pars, fail = calcErrorModel(gals.refmag, gals.refmag_err, calcErr=False)

        testing.assert_almost_equal(pars['EXPTIME'], 63.73879623, 0)
        testing.assert_almost_equal(pars['LIMMAG'], 20.68231583, 0)

        # And make sure the maskgals are getting the right constant value
        testing.assert_almost_equal(pars['EXPTIME'], mask.maskgals.exptime.min())
        testing.assert_almost_equal(pars['EXPTIME'], mask.maskgals.exptime.max())
        testing.assert_almost_equal(pars['LIMMAG'], mask.maskgals.limmag.min())
        testing.assert_almost_equal(pars['LIMMAG'], mask.maskgals.limmag.max())
Ejemplo n.º 8
0
    def runTest(self):
        """
        Run the ClusterFit test.
        """
        random.seed(seed=12345)

        file_path = 'data_for_tests'
        conf_filename = 'testconfig.yaml'
        config = Configuration(file_path + '/' + conf_filename)

        gals = GalaxyCatalog.from_galfile(config.galfile)

        # temporary hack...
        dist = esutil.coords.sphdist(142.12752, 65.103898, gals.ra, gals.dec)
        mpc_scale = np.radians(1.) * config.cosmo.Da(0, 0.227865)
        r = np.clip(mpc_scale * dist, 1e-6, None)
        use, = np.where(r < 0.75)

        st = np.argsort(r[use])

        cbkg = ColorBackground(config.bkgfile_color, usehdrarea=True)
        zredstr = RedSequenceColorPar(None, config=config)

        cluster = Cluster(r0=0.5,
                          beta=0.0,
                          config=config,
                          cbkg=cbkg,
                          neighbors=gals[use[st]],
                          zredstr=zredstr)
        cluster.ra = 142.12752
        cluster.dec = 65.103898
        cluster.redshift = 0.227865
        cluster.update_neighbors_dist()

        mask = HPMask(cluster.config)
        maskgal_index = mask.select_maskgals_sample(maskgal_index=0)
        depthstr = DepthMap(cluster.config)
        mask.set_radmask(cluster)
        depthstr.calc_maskdepth(mask.maskgals, cluster.ra, cluster.dec,
                                cluster.mpc_scale)

        lam = cluster.calc_richness_fit(mask,
                                        1,
                                        centcolor_in=1.36503,
                                        calc_err=False)
        testing.assert_almost_equal(lam, 16.174486160, decimal=5)
        testing.assert_almost_equal(cluster.neighbors.pcol[0:4],
                                    np.array([0.94243, 0., 0.06338, 0.16077]),
                                    5)

        lam = cluster.calc_richness_fit(mask, 1, calc_err=False)
        testing.assert_almost_equal(lam, 16.2049961, decimal=5)
Ejemplo n.º 9
0
    def runTest(self):
        """
        Run the ClusterFit test.
        """
        random.seed(seed=12345)

        file_path = 'data_for_tests'
        conf_filename = 'testconfig.yaml'
        config = Configuration(file_path + '/' + conf_filename)

        gals = GalaxyCatalog.from_galfile(config.galfile)

        # temporary hack...
        dist = esutil.coords.sphdist(142.12752, 65.103898, gals.ra, gals.dec)
        mpc_scale = np.radians(1.) * config.cosmo.Da(0, 0.227865)
        r = np.clip(mpc_scale * dist, 1e-6, None)
        use, = np.where(r < 0.75)

        st = np.argsort(r[use])

        cbkg = ColorBackground(config.bkgfile_color, usehdrarea=True)
        zredstr = RedSequenceColorPar(None, config=config)

        cluster = Cluster(r0=0.5, beta=0.0, config=config, cbkg=cbkg, neighbors=gals[use[st]], zredstr=zredstr)
        cluster.ra = 142.12752
        cluster.dec = 65.103898
        cluster.redshift = 0.227865
        cluster.update_neighbors_dist()

        mask = HPMask(cluster.config)
        maskgal_index = mask.select_maskgals_sample(maskgal_index=0)
        depthstr = DepthMap(cluster.config)
        mask.set_radmask(cluster)
        depthstr.calc_maskdepth(mask.maskgals, cluster.ra, cluster.dec, cluster.mpc_scale)

        lam = cluster.calc_richness_fit(mask, 1, centcolor_in=1.36503, calc_err=False)
        testing.assert_almost_equal(lam, 16.174486160, decimal=5)
        testing.assert_almost_equal(cluster.neighbors.pcol[0:4], np.array([0.94243, 0., 0.06338, 0.16077]), 5)

        lam = cluster.calc_richness_fit(mask, 1, calc_err=False)
        testing.assert_almost_equal(lam, 16.2049961, decimal=5)
Ejemplo n.º 10
0
    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)
Ejemplo n.º 11
0
    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
Ejemplo n.º 12
0
    def test_redmagic_calibrate(self):
        """
        """

        np.random.seed(12345)

        file_path = 'data_for_tests'
        conf_filename = 'testconfig_redmagic.yaml'
        config = Configuration(os.path.join(file_path, conf_filename))

        self.test_dir = tempfile.mkdtemp(dir='./', prefix='TestRedmapper-')
        config.outpath = self.test_dir

        testgals = GalaxyCatalog.from_fits_file(
            os.path.join('data_for_tests', 'redmagic_test',
                         'redmagic_test_input_gals.fit'))

        testgals.add_fields([('mag', 'f4', 5), ('mag_err', 'f4', 5)])

        redmagic_cal = RedmagicCalibrator(config)
        # We have to have do_run=False here because we don't have a real
        # galaxy training set with associated zreds!
        redmagic_cal.run(gals=testgals, do_run=False)

        # Read in the calibrated parameters

        self.assertTrue(os.path.isfile(config.redmagicfile))

        cal = fitsio.read(config.redmagicfile, ext=1)

        # Check that they are what we think they should be
        # (these checks are arbitrary, just to make sure nothing has changed)

        testing.assert_almost_equal(
            cal['cmax'][0, :], np.array([1.14588386, 3.89420298, -0.36792211]))
        testing.assert_almost_equal(
            cal['bias'][0, :], np.array([-0.09999912, -0.04537928,
                                         0.01599778]))
        testing.assert_almost_equal(
            cal['eratio'][0, :], np.array([1.4999998, 1.48021495, 0.50000003]))

        pngs = glob.glob(os.path.join(self.test_dir, '*.png'))
        self.assertEqual(len(pngs), 3)

        # This is a hack of the volume limit mask to change from the one used for
        # calibration to the one used for the run (which uses a different footprint
        # because of reasons)

        config_regular = Configuration(
            os.path.join(file_path, 'testconfig.yaml'))
        maskfile = config_regular.maskfile

        config = Configuration(redmagic_cal.runfile)

        cal, hdr = fitsio.read(config.redmagicfile, ext=1, header=True)
        config.maskfile = maskfile
        os.remove(cal['vmaskfile'][0].decode().rstrip())
        mask = VolumeLimitMask(config, cal['etamin'], use_geometry=True)

        # Now test the running, using the output file which has valid galaxies/zreds
        run_redmagic = RunRedmagicTask(redmagic_cal.runfile)
        run_redmagic.run()

        # check that we have a redmagic catalog
        rmcatfile = config.redmapper_filename('redmagic_%s' % ('highdens'))
        self.assertTrue(os.path.isfile(rmcatfile))

        # And a random catalog
        rmrandfile = config.redmapper_filename('redmagic_%s_randoms' %
                                               ('highdens'))
        self.assertTrue(
            os.path.isfile(
                config.redmapper_filename('redmagic_%s_randoms' %
                                          ('highdens'))))

        # And check that the plot is there

        pngs = glob.glob(os.path.join(self.test_dir, '*.png'))
        self.assertEqual(len(pngs), 4)

        # And that we have the desired number of redmagic and randoms
        red_cat = GalaxyCatalog.from_fits_file(rmcatfile)
        rand_cat = GalaxyCatalog.from_fits_file(rmrandfile)

        self.assertEqual(rand_cat.size, red_cat.size * 10)

        # And confirm that all the randoms are in the footprint
        zmax = mask.calc_zmax(rand_cat.ra, rand_cat.dec)
        self.assertTrue(np.all(rand_cat.z < zmax))
Ejemplo n.º 13
0
    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']])
Ejemplo n.º 14
0
    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)
Ejemplo n.º 15
0
    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
Ejemplo n.º 16
0
    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)
Ejemplo n.º 17
0
    def runTest(self):
        """
        Run the ClusterCatalog tests.
        """

        random.seed(seed=12345)

        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)
        maskgal_index = mask.select_maskgals_sample(maskgal_index=0)
        depthstr = DepthMap(config)

        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, 24.4121723)
Ejemplo n.º 18
0
    def test_galaxycatalog_create(self):
        """
        Run `redmapper.GalaxyCatalogMaker` tests.
        """

        # Make a test directory
        self.test_dir = tempfile.mkdtemp(dir='./', prefix='TestRedmapper-')

        info_dict = {'LIM_REF': 21.0,
                     'REF_IND': 3,
                     'AREA': 25.0,
                     'NMAG': 5,
                     'MODE': 'SDSS',
                     'ZP': 22.5,
                     'U_IND': 0,
                     'G_IND': 1,
                     'R_IND': 2,
                     'I_IND': 3,
                     'Z_IND': 3}

        # Test 1: read in catalog, write it to a single file, and then
        # try to split it up
        # make sure that it makes it properly, and that the output number
        # of files is the same as input number of files.

        configfile = os.path.join('data_for_tests', 'testconfig.yaml')
        config = Configuration(configfile)
        gals = GalaxyCatalog.from_galfile(config.galfile)
        tab = Entry.from_fits_file(config.galfile)

        maker = GalaxyCatalogMaker(os.path.join(self.test_dir, 'test_working'), info_dict, nside=tab.nside)
        maker.append_galaxies(gals._ndarray)
        maker.finalize_catalog()

        tab2 = Entry.from_fits_file(os.path.join(self.test_dir, 'test_working_master_table.fit'))
        self.assertEqual(tab.nside, tab2.nside)
        self.assertEqual(tab.filenames.size, tab2.filenames.size)

        for filename in tab2.filenames:
            self.assertTrue(os.path.isfile(os.path.join(self.test_dir, filename.decode())))

        # Test 2: Make a catalog that has an incomplete dtype
        dtype = [('id', 'i8'),
                 ('ra', 'f8')]
        maker = GalaxyCatalogMaker(os.path.join(self.test_dir, 'test'), info_dict)
        testgals = np.zeros(10, dtype=dtype)
        self.assertRaises(RuntimeError, maker.append_galaxies, testgals)

        # Test 3: make a catalog that has the wrong number of magnitudes
        dtype = GalaxyCatalogMaker.get_galaxy_dtype(3)
        testgals = np.zeros(10, dtype=dtype)
        self.assertRaises(RuntimeError, maker.append_galaxies, testgals)

        # Test 4: make a catalog that has some NaNs
        dtype = GalaxyCatalogMaker.get_galaxy_dtype(info_dict['NMAG'])
        testgals = np.ones(10, dtype=dtype)
        testgals['mag'][0, 1] = np.nan
        self.assertRaises(RuntimeError, maker.append_galaxies, testgals)

        # Test 5: make a catalog that has ra/dec out of range
        testgals = np.ones(10, dtype=dtype)
        testgals['ra'][1] = -1.0
        self.assertRaises(RuntimeError, maker.append_galaxies, testgals)

        testgals = np.ones(10, dtype=dtype)
        testgals['dec'][1] = -100.0
        self.assertRaises(RuntimeError, maker.append_galaxies, testgals)

        # Test 6: make a catalog that has mag > 90.0
        testgals = np.ones(10, dtype=dtype)
        testgals['mag'][0, 1] = 100.0
        self.assertRaises(RuntimeError, maker.append_galaxies, testgals)

        # Test 7: make a catalog that has mag_err == 0.0
        testgals = np.ones(10, dtype=dtype)
        testgals['mag_err'][0, 1] = 0.0
        self.assertRaises(RuntimeError, maker.append_galaxies, testgals)
Ejemplo n.º 19
0
    def test_redmagic_calibrate(self):
        """
        """

        np.random.seed(12345)

        file_path = 'data_for_tests'
        conf_filename = 'testconfig_redmagic.yaml'
        config = Configuration(os.path.join(file_path, conf_filename))

        self.test_dir = tempfile.mkdtemp(dir='./', prefix='TestRedmapper-')
        config.outpath = self.test_dir

        testgals = GalaxyCatalog.from_fits_file(os.path.join('data_for_tests', 'redmagic_test', 'redmagic_test_input_gals.fit'))

        testgals.add_fields([('mag', 'f4', 5), ('mag_err', 'f4', 5)])

        redmagic_cal = RedmagicCalibrator(config)
        # We have to have do_run=False here because we don't have a real
        # galaxy training set with associated zreds!
        redmagic_cal.run(gals=testgals, do_run=False)

        # Read in the calibrated parameters

        self.assertTrue(os.path.isfile(config.redmagicfile))

        cal = fitsio.read(config.redmagicfile, ext=1)

        # Check that they are what we think they should be
        # (these checks are arbitrary, just to make sure nothing has changed)

        testing.assert_almost_equal(cal['cmax'][0, :], np.array([1.14588386, 3.89420298, -0.36792211]))
        testing.assert_almost_equal(cal['bias'][0, :], np.array([-0.09999912, -0.04537928, 0.01599778]))
        testing.assert_almost_equal(cal['eratio'][0, :], np.array([1.4999998, 1.48021495, 0.50000003]))

        pngs = glob.glob(os.path.join(self.test_dir, '*.png'))
        self.assertEqual(len(pngs), 3)

        # This is a hack of the volume limit mask to change from the one used for
        # calibration to the one used for the run (which uses a different footprint
        # because of reasons)

        config_regular = Configuration(os.path.join(file_path, 'testconfig.yaml'))
        maskfile = config_regular.maskfile

        config = Configuration(redmagic_cal.runfile)

        cal, hdr = fitsio.read(config.redmagicfile, ext=1, header=True)
        config.maskfile = maskfile
        os.remove(cal['vmaskfile'][0].decode().rstrip())
        mask = VolumeLimitMask(config, cal['etamin'], use_geometry=True)

        # Now test the running, using the output file which has valid galaxies/zreds
        run_redmagic = RunRedmagicTask(redmagic_cal.runfile)
        run_redmagic.run()

        # check that we have a redmagic catalog
        rmcatfile = config.redmapper_filename('redmagic_%s' % ('highdens'))
        self.assertTrue(os.path.isfile(rmcatfile))

        # And a random catalog
        rmrandfile = config.redmapper_filename('redmagic_%s_randoms' % ('highdens'))
        self.assertTrue(os.path.isfile(config.redmapper_filename('redmagic_%s_randoms' % ('highdens'))))

        # And check that the plot is there

        pngs = glob.glob(os.path.join(self.test_dir, '*.png'))
        self.assertEqual(len(pngs), 4)

        # And that we have the desired number of redmagic and randoms
        red_cat = GalaxyCatalog.from_fits_file(rmcatfile)
        rand_cat = GalaxyCatalog.from_fits_file(rmrandfile)

        self.assertEqual(rand_cat.size, red_cat.size * 10)

        # And confirm that all the randoms are in the footprint
        zmax = mask.calc_zmax(rand_cat.ra, rand_cat.dec)
        self.assertTrue(np.all(rand_cat.z < zmax))
Ejemplo n.º 20
0
    def test_zred_runpixels(self):
        """
        Test redmapper.ZredRunPixels, computing zreds for all the galaxies
        in a pixelized galaxy catalog.
        """

        file_path = 'data_for_tests'
        configfile = 'testconfig.yaml'

        config = Configuration(os.path.join(file_path, configfile))

        config.d.hpix = 2163
        config.d.nside = 64
        config.border = 0.0

        self.test_dir = tempfile.mkdtemp(dir='./', prefix='TestRedmapper-')
        config.zredfile = os.path.join(self.test_dir, 'zreds',
                                       'testing_zreds_master_table.fit')

        # FIXME: try an illegal one...

        zredRunpix = ZredRunPixels(config)
        zredRunpix.run()

        # Check that the zred file has been built...

        self.assertTrue(os.path.isfile(config.zredfile))

        # Read in just the galaxies...
        gals0 = GalaxyCatalog.from_galfile(config.galfile,
                                           nside=config.d.nside,
                                           hpix=config.d.hpix,
                                           border=config.border)

        # And with the zreds...
        gals = GalaxyCatalog.from_galfile(config.galfile,
                                          zredfile=config.zredfile,
                                          nside=config.d.nside,
                                          hpix=config.d.hpix,
                                          border=config.border)

        # Confirm they're the same galaxies...
        testing.assert_array_almost_equal(gals0.ra, gals.ra)

        # Confirm the zreds are okay
        self.assertGreater(np.min(gals.zred), 0.0)
        self.assertGreater(np.min(gals.chisq), 0.0)
        self.assertLess(np.max(gals.lkhd), 0.0)

        zredfile = os.path.join(file_path, 'zreds_test',
                                'dr8_test_zreds_master_table.fit')
        gals_compare = GalaxyCatalog.from_galfile(config.galfile,
                                                  zredfile=zredfile,
                                                  nside=config.d.nside,
                                                  hpix=config.d.hpix,
                                                  border=config.border)

        zredstr = RedSequenceColorPar(config.parfile)
        mstar_input = zredstr.mstar(gals_compare.zred_uncorr)
        mstar = zredstr.mstar(gals_compare.zred_uncorr)

        ok, = np.where(
            (gals_compare.refmag < (mstar_input - 2.5 * np.log10(0.15)))
            | (gals_compare.refmag < (mstar - 2.5 * np.log10(0.15))))

        delta_zred_uncorr = gals.zred_uncorr[ok] - gals_compare.zred_uncorr[ok]

        use, = np.where(np.abs(delta_zred_uncorr) < 1e-3)
        testing.assert_array_less(0.98, float(use.size) / float(ok.size))
Ejemplo n.º 21
0
    def test_zred(self):
        """
        Test redmapper.ZredColor, looping over galaxies.
        """

        file_path = 'data_for_tests'

        zred_filename = 'test_dr8_pars.fit'
        zredstr = RedSequenceColorPar(file_path + '/' + zred_filename)

        galaxy_filename = 'test_dr8_gals_with_zred.fit'
        galaxies = GalaxyCatalog.from_fits_file(file_path + '/' +
                                                galaxy_filename)

        galaxies_input = copy.deepcopy(galaxies)

        # start with the first one...
        zredc = ZredColor(zredstr)

        zredc.compute_zred(galaxies[0])

        starttime = time.time()
        zredc.compute_zreds(galaxies)

        print("Ran %d galaxies in %.3f seconds" %
              (galaxies.size, time.time() - starttime))

        # Only compare galaxies that are brighter than 0.15L* in either old OR new
        # Otherwise, we're just comparing how the codes handle "out-of-range"
        # galaxies, and that does not matter
        mstar_input = zredstr.mstar(galaxies_input.zred_uncorr)
        mstar = zredstr.mstar(galaxies.zred_uncorr)

        ok, = np.where((galaxies.refmag < (mstar_input - 2.5 * np.log10(0.15)))
                       | (galaxies.refmag < (mstar - 2.5 * np.log10(0.15))))

        delta_zred_uncorr = galaxies.zred_uncorr[
            ok] - galaxies_input.zred_uncorr[ok]

        use, = np.where(np.abs(delta_zred_uncorr) < 1e-3)

        testing.assert_array_less(0.9, float(use.size) / float(ok.size))

        delta_zred = galaxies.zred[ok[use]] - galaxies_input.zred[ok[use]]
        use2, = np.where(np.abs(delta_zred) < 1e-3)
        testing.assert_array_less(0.99,
                                  float(use2.size) / float(delta_zred.size))

        delta_zred2 = galaxies.zred2[ok[use]] - galaxies_input.zred2[ok[use]]
        use2, = np.where(np.abs(delta_zred) < 1e-3)
        testing.assert_array_less(0.99,
                                  float(use2.size) / float(delta_zred2.size))

        delta_zred_uncorr_e = galaxies.zred_uncorr_e[
            ok[use]] - galaxies_input.zred_uncorr_e[ok[use]]
        use2, = np.where(np.abs(delta_zred_uncorr_e) < 1e-3)
        testing.assert_array_less(
            0.98,
            float(use2.size) / float(delta_zred_uncorr_e.size))

        delta_zred_e = galaxies.zred_e[ok[use]] - galaxies_input.zred_e[
            ok[use]]
        use2, = np.where(np.abs(delta_zred_e) < 1e-3)
        testing.assert_array_less(0.98,
                                  float(use2.size) / float(delta_zred_e.size))

        delta_zred2_e = galaxies.zred2_e[ok[use]] - galaxies_input.zred2_e[
            ok[use]]
        use2, = np.where(np.abs(delta_zred2_e) < 1e-3)
        testing.assert_array_less(0.98,
                                  float(use2.size) / float(delta_zred2_e.size))
Ejemplo n.º 22
0
    def runTest(self):

        file_path = 'data_for_tests'

        zred_filename = 'test_dr8_pars.fit'
        zredstr = RedSequenceColorPar(file_path + '/' + zred_filename)

        galaxy_filename = 'test_dr8_gals_with_zred.fit'
        galaxies = GalaxyCatalog.from_fits_file(file_path + '/' +
                                                galaxy_filename)

        galaxies_input = copy.deepcopy(galaxies)

        # start with the first one...
        zredc = ZredColor(zredstr, adaptive=True)

        zredc.compute_zred(galaxies[0])

        starttime = time.time()
        for i, g in enumerate(galaxies):
            try:
                zredc.compute_zred(g)
            except:
                print("Crashed on %d" % (i))

        print("Ran %d galaxies in %.3f seconds" %
              (galaxies.size, time.time() - starttime))

        # make sure we have reasonable consistency...
        # It seems that at least some of the discrepancies are caused by
        # slight bugs in the IDL implementation.

        delta_zred_uncorr = galaxies.zred_uncorr - galaxies_input.zred_uncorr

        use, = np.where(np.abs(delta_zred_uncorr) < 1e-3)

        testing.assert_array_less(0.9, float(use.size) / float(galaxies.size))

        delta_zred = galaxies.zred[use] - galaxies_input.zred[use]
        use2, = np.where(np.abs(delta_zred) < 1e-3)
        testing.assert_array_less(0.99,
                                  float(use2.size) / float(delta_zred.size))

        delta_zred2 = galaxies.zred2[use] - galaxies_input.zred2[use]
        use2, = np.where(np.abs(delta_zred) < 1e-3)
        testing.assert_array_less(0.99,
                                  float(use2.size) / float(delta_zred2.size))

        delta_zred_uncorr_e = galaxies.zred_uncorr_e[
            use] - galaxies_input.zred_uncorr_e[use]
        use2, = np.where(np.abs(delta_zred_uncorr_e) < 1e-3)
        testing.assert_array_less(
            0.98,
            float(use2.size) / float(delta_zred_uncorr_e.size))

        delta_zred_e = galaxies.zred_e[use] - galaxies_input.zred_e[use]
        use2, = np.where(np.abs(delta_zred_e) < 1e-3)
        testing.assert_array_less(0.98,
                                  float(use2.size) / float(delta_zred_e.size))

        delta_zred2_e = galaxies.zred2_e[use] - galaxies_input.zred2_e[use]
        use2, = np.where(np.abs(delta_zred2_e) < 1e-3)
        testing.assert_array_less(0.98,
                                  float(use2.size) / float(delta_zred2_e.size))
Ejemplo n.º 23
0
    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