示例#1
0
    def test_getCF(self):
        """Test getCF() directly, without first processing by child classes."""
        stile_args = {'ra_units': 'degrees', 'dec_units': 'degrees', 'min_sep': 0.05, 'max_sep': 1,
                      'sep_units': 'degrees', 'nbins': 20}
        cf = stile.sys_tests.CorrelationFunctionSysTest()
        dh = temp_data_handler()
        lens_data = stile.ReadASCIITable('../examples/example_lens_catalog.dat',
                    fields={'id': 0, 'ra': 1, 'dec': 2, 'z': 3, 'g1': 4, 'g2': 5})
        source_data = stile.ReadASCIITable('../examples/example_source_catalog.dat',
                    fields={'id': 0, 'ra': 1, 'dec': 2, 'z': 3, 'g1': 4, 'g2': 5})
        lens_catalog = treecorr.Catalog(ra=numpy.array([lens_data['ra']]),
                                        dec=numpy.array([lens_data['dec']]),
                                        ra_units='degrees', dec_units='degrees')
        source_catalog = treecorr.Catalog(ra=source_data['ra'], dec=source_data['dec'],
                                          g1=source_data['g1'], g2=source_data['g2'],
                                          ra_units='degrees', dec_units='degrees')
        results = cf.getCF('ng', lens_catalog, source_catalog, **stile_args)
        numpy.testing.assert_array_equal(*helper.FormatSame(results, self.expected_result))
        results2 = cf.getCF('ng', lens_data, source_data, config=stile_args)
        self.assertEqual(self.expected_result.dtype.names, results.dtype.names)
        # Missing necessary data file
        numpy.testing.assert_equal(results, results2)
        self.assertRaises(TypeError,
                          cf.getCF, {}, 'ng',
                          file_name='../examples/example_lens_catalog.dat')
        # Nonsensical correlation type
        self.assertRaises(ValueError, cf.getCF, 'hello', lens_data, source_data, config=stile_args)

        # Then, test a test that uses .getCF().
        realshear = stile.sys_tests.GalaxyShearSysTest()
        results3 = realshear(lens_data, source_data, config=stile_args)
        numpy.testing.assert_equal(results, results3)
示例#2
0
    def test_WriteASCIITable(self):
        """Test the ability to write an ASCII table."""
        # Must be done after test_read_ASCII_table() since it uses the read_ASCII_table function!
        handle, filename = tempfile.mkstemp()
        stile.file_io.WriteASCIITable(filename, self.table1)
        results = stile.ReadASCIITable(filename)
        # genfromtxt will cleverly figure out the last two columns are ints, which we want it to,
        # but that causes problems here.
        try:
            numpy.testing.assert_equal(results, self.table1)
            raise RuntimeError('Arrays are equal, but should not have the same format')
        except AssertionError:
            pass
        numpy.testing.assert_equal(results.astype('f'), self.table1.astype('f'))

        # check field reordering
        field_list = ['f3', 'f4', 'f6', 'f0', 'f2', 'f1', 'f5']
        stile.file_io.WriteASCIITable(filename, self.table1, fields=field_list)
        results = stile.ReadASCIITable(filename)
        numpy.testing.assert_equal(results.astype('f'), self.table1[field_list].astype('f'))
        os.close(handle)
        if not stile.file_io.has_fits:
            # If there is a FITS handler, this table would be written as a FITS table, so we
            # shouldn't check it.  But otherwise these will be ASCII files, so test that
            # WriteTable() is sending them through that function properly.
            handle, filename = tempfile.mkstemp()
            stile.file_io.WriteTable(filename, self.table1)
            results = stile.ReadASCIITable(filename)
            numpy.testing.assert_equal(self.table1.astype('f'), results.astype('f'))
            os.close(handle)
示例#3
0
 def test_plot(self):
     """ Test that the plotting routines successfully generate a plot """
     stile_args = {
         'ra_units': 'degrees',
         'dec_units': 'degrees',
         'min_sep': 0.05,
         'max_sep': 1,
         'sep_units': 'degrees',
         'nbins': 20
     }
     cf = stile.sys_tests.CorrelationFunctionSysTest()
     lens_data = stile.ReadASCIITable(
         '../examples/example_lens_catalog.dat',
         fields={
             'id': 0,
             'ra': 1,
             'dec': 2,
             'z': 3,
             'g1': 4,
             'g2': 5
         })
     source_data = stile.ReadASCIITable(
         '../examples/example_source_catalog.dat',
         fields={
             'id': 0,
             'ra': 1,
             'dec': 2,
             'z': 3,
             'g1': 4,
             'g2': 5
         })
     object_list = [
         'GalaxyShear', 'BrightStarShear', 'StarXGalaxyShear',
         'StarXStarShear'
     ]
     for object_type in object_list:
         obj = stile.CorrelationFunctionSysTest(object_type)
         results = obj(lens_data, source_data, **stile_args)
         pl = obj.plot(results)
         self.assertIsInstance(pl, matplotlib.figure.Figure)
     # Test underscore protection. If there's an underscore in the radius label somewhere,
     # get rid of the non-underscore versions to make sure we hit that branch of the code,
     # then test the plotting again
     names = list(results.dtype.names)
     names_no_units = [n.split(' [')[0] for n in names]
     if 'R_nom' in names_no_units or 'R_nominal' in names_no_units:
         for rname in ['meanR', '<R>', 'R']:
             if rname in names_no_units:
                 index = names_no_units.index(rname)
                 names[index] = 'old_' + names[index]
         results.dtype.names = names
         pl = obj.plot(results)
         self.assertIsInstance(pl, matplotlib.figure.Figure)
         pl.savefig('examine.png')
示例#4
0
 def test_ReadASCIITable(self):
     """Test the ability to read in an ASCII table."""
     # ReadASCIITable is a wrapper for numpy.genfromtxt() that turns things into formatted
     # arrays if necessary, so we don't really need to test much of the functionality--just make
     # sure that both natively formatted arrays (table_with_string) and natively raw arrays
     # (treecorr_output) are both returned as formatted arrays.
     results = stile.ReadASCIITable('test_data/TreeCorr_output.dat', comments='#')
     numpy.testing.assert_equal(results, self.table1)
     results = stile.ReadASCIITable('test_data/table_with_string.dat')
     numpy.testing.assert_equal(results, self.table2_withstring)
     results = stile.ReadTable('test_data/table_with_string.dat')
     numpy.testing.assert_equal(results, self.table2_withstring)