Ejemplo n.º 1
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.º 2
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.º 3
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.º 4
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.º 5
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.º 6
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.º 7
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.º 8
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.º 9
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.º 10
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))