示例#1
0
    def test_run_colormem(self):
        """
        Run tests of redmapper.RunColormem
        """

        random.seed(seed=12345)

        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

        # First, we need the red galaxy model

        config.specfile_train = os.path.join(file_path, 'test_dr8_spec.fit')
        config.zrange = [0.1, 0.2]

        config.redgalfile = config.redmapper_filename('test_redgals')
        config.redgalmodelfile = config.redmapper_filename('test_redgalmodel')

        selred = SelectSpecRedGalaxies(config)
        selred.run()

        # Main test...
        config.zmemfile = config.redmapper_filename('test_zmem')

        rcm = RunColormem(config)
        rcm.run()
        rcm.output_training()

        # Check that the files are there...
        self.assertTrue(os.path.isfile(config.zmemfile))

        mem = fitsio.read(config.zmemfile, ext=1)

        testing.assert_equal(mem.size, 16)
        testing.assert_array_almost_equal(
            mem['pcol'][0:3], np.array([0.94829756, 0.83803916, 0.88315928]))
        testing.assert_array_almost_equal(
            mem['z'][0:3], np.array([0.191279, 0.188257, 0.186945]))
示例#2
0
    def test_run_colormem(self):
        """
        Run tests of redmapper.RunColormem
        """

        random.seed(seed=12345)

        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

        # First, we need the red galaxy model

        config.specfile_train = os.path.join(file_path, 'test_dr8_spec.fit')
        config.zrange = [0.1,0.2]

        config.redgalfile = config.redmapper_filename('test_redgals')
        config.redgalmodelfile = config.redmapper_filename('test_redgalmodel')

        selred = SelectSpecRedGalaxies(config)
        selred.run()

        # Main test...
        config.zmemfile = config.redmapper_filename('test_zmem')

        rcm = RunColormem(config)
        rcm.run()
        rcm.output_training()

        # Check that the files are there...
        self.assertTrue(os.path.isfile(config.zmemfile))

        mem = fitsio.read(config.zmemfile, ext=1)

        testing.assert_equal(mem.size, 16)
        testing.assert_array_almost_equal(mem['pcol'][0:3], np.array([0.94829756, 0.83803916, 0.88315928]))
        testing.assert_array_almost_equal(mem['z'][0:3], np.array([0.191279, 0.188257, 0.186945]))
示例#3
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))
示例#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))
示例#5
0
    def runTest(self):
        """
        Test redmapper.pipeline.RedmapperConsolidateTask
        """

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

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

        config.consolidate_lambda_cuts = np.array([5.0, 20.0])
        config.consolidate_vlim_lstars = np.array([])

        # Make 3 fake simple catalogs...
        random.seed(seed=12345)
        nside = 4

        test_arr = np.zeros(5, dtype=[('mem_match_id', 'i4'),
                                      ('ra', 'f8'),
                                      ('dec', 'f8'),
                                      ('lambda', 'f4'),
                                      ('lnlamlike', 'f4')])

        # Do cat0, pixel 0
        theta, phi = hp.pix2ang(nside, 0)
        test_arr['mem_match_id'] = np.arange(test_arr.size) + 1
        test_arr['lambda'][:] = 100.0
        test_arr['lnlamlike'] = random.random(size=test_arr.size) * 100
        test_arr['ra'][:] = np.degrees(phi)
        test_arr['dec'][:] = 90.0 - np.degrees(theta)
        cat0 = ClusterCatalog(test_arr)
        cat0.to_fits_file(config.redmapper_filename('cat_4_00000_final'))
        cat0.to_fits_file(config.redmapper_filename('cat_4_00000_final_members'))

        # Do cat1, pixel 1
        theta, phi = hp.pix2ang(nside, 1)
        test_arr['mem_match_id'] = np.arange(test_arr.size) + 1
        test_arr['lambda'][:] = 100.0
        test_arr['lnlamlike'] = random.random(size=test_arr.size) * 100
        test_arr['ra'][:] = np.degrees(phi)
        test_arr['dec'][:] = 90.0 - np.degrees(theta)
        cat1 = ClusterCatalog(test_arr)
        cat1.to_fits_file(config.redmapper_filename('cat_4_00001_final'))
        cat1.to_fits_file(config.redmapper_filename('cat_4_00001_final_members'))

        # Do cat2, pixel 2
        theta, phi = hp.pix2ang(nside, 2)
        test_arr['mem_match_id'] = np.arange(test_arr.size) + 1
        test_arr['lambda'][:] = 100.0
        test_arr['lnlamlike'] = random.random(size=test_arr.size) * 100
        test_arr['ra'][:] = np.degrees(phi)
        test_arr['dec'][:] = 90.0 - np.degrees(theta)
        cat2 = ClusterCatalog(test_arr)
        cat2.to_fits_file(config.redmapper_filename('cat_4_00002_final'))
        cat2.to_fits_file(config.redmapper_filename('cat_4_00002_final_members'))

        # need to write config out in test directory...
        config_file = config.redmapper_filename('testconfig', filetype='yaml')
        config.output_yaml(config_file)

        # Consolidate them together...
        consol = RedmapperConsolidateTask(config_file)
        consol.run(match_spec=False, do_plots=False)

        # Check that the ordering is correct, etc.
        catfile = config.redmapper_filename('redmapper_v%s_lgt20_catalog' % (config.version))
        memfile = config.redmapper_filename('redmapper_v%s_lgt20_catalog_members' % (config.version))
        self.assertTrue(os.path.isfile(catfile))
        self.assertTrue(os.path.isfile(memfile))

        cat = fitsio.read(catfile, ext=1)
        self.assertEqual(cat.size, cat0.size + cat1.size + cat2.size)

        # Sort by mem_match_id, these should be reverse sorted in lnlamlike
        st = np.argsort(cat['mem_match_id'])
        self.assertTrue(np.all(np.diff(cat['lnlamlike'][st[::-1]]) >= 0))

        # And check that the ra/dec/lnlamlike match...
        test0 = {}
        for cluster in cat0:
            test0[cluster.lnlamlike] = cluster.ra
        ctr = 0
        for i in range(cat.size):
            if cat['lnlamlike'][i] in test0:
                if cat['ra'][i] == test0[cat['lnlamlike'][i]]:
                    ctr += 1
        self.assertEqual(ctr, cat0.size)