Ejemplo n.º 1
0
def main():
    # setups
    dh = dummy.DummyDataHandler()
    bin_list = [
        stile.BinStep('ra', low=-1, high=1, step=1),
        stile.BinStep('dec', low=-1, high=1, step=1)
    ]
    sys_test = stile.CorrelationFunctionSysTest(type='GalaxyShear')

    stile_args = {
        'ra_units': 'degrees',
        'dec_units': 'degrees',
        'min_sep': 0.05,
        'max_sep': 1,
        'sep_units': 'degrees',
        'nbins': 20
    }
    data_ids = dh.listData(object_types=['galaxy lens', 'galaxy'],
                           epoch='single',
                           extent='field',
                           data_format='table')

    # do a test without binning
    data = dh.getData(data_ids[0], 'galaxy lens', 'single', 'field', 'table')
    data2 = dh.getData(data_ids[1], 'galaxy', 'single', 'field', 'table')

    # run the test
    results = sys_test(data, data2=data2, config=stile_args)

    fig = sys_test.plot(results)
    fig.savefig(sys_test.short_name + '.png')

    stile.WriteASCIITable('realshear.dat', results)
    print "Done with unbinned systematics test"

    # do with binning
    data = dh.getData(data_ids[0], 'galaxy lens', 'single', 'field', 'table')
    # turns a list of binning schemes into a pseudo-nested list of single bins
    expanded_bin_list = stile.ExpandBinList(bin_list)
    handles_list = []
    deletes_list = []
    # for each set of bins, do the systematics test as above
    for bin_list in expanded_bin_list:
        bins_name = '-'.join([bl.short_name for bl in bin_list])
        data2 = dh.getData(data_ids[1],
                           'galaxy',
                           'single',
                           'field',
                           'table',
                           bin_list=bin_list)
        results = sys_test(data, data2=data2, config=stile_args)
        stile.WriteASCIITable('realshear-' + bins_name + '.dat', results)
        fig = sys_test.plot(results)
        fig.savefig(sys_test.short_name + bins_name + '.png')
        print "Done with binned systematics test", bins_name
Ejemplo n.º 2
0
    def test_ExpandBinList(self):
        """Test the function that takes a set of objects which each generate a list and returns all
        possible sets of one object from each list, in the order we expect it to do that."""

        # ExpandBinList needs something that returns callable object
        def return_objs(x, n):
            def func():
                return [str(nn) + x for nn in range(n)]

            return func

        results = stile.ExpandBinList(
            [return_objs('a', 3),
             return_objs('b', 2),
             return_objs('c', 4)])
        numpy.testing.assert_equal(results, [('0a', '0b', '0c'),
                                             ('0a', '0b', '1c'),
                                             ('0a', '0b', '2c'),
                                             ('0a', '0b', '3c'),
                                             ('0a', '1b', '0c'),
                                             ('0a', '1b', '1c'),
                                             ('0a', '1b', '2c'),
                                             ('0a', '1b', '3c'),
                                             ('1a', '0b', '0c'),
                                             ('1a', '0b', '1c'),
                                             ('1a', '0b', '2c'),
                                             ('1a', '0b', '3c'),
                                             ('1a', '1b', '0c'),
                                             ('1a', '1b', '1c'),
                                             ('1a', '1b', '2c'),
                                             ('1a', '1b', '3c'),
                                             ('2a', '0b', '0c'),
                                             ('2a', '0b', '1c'),
                                             ('2a', '0b', '2c'),
                                             ('2a', '0b', '3c'),
                                             ('2a', '1b', '0c'),
                                             ('2a', '1b', '1c'),
                                             ('2a', '1b', '2c'),
                                             ('2a', '1b', '3c')])
        numpy.testing.assert_equal(stile.ExpandBinList(None), [])
        numpy.testing.assert_equal(stile.ExpandBinList([]), [])
        bin_obj0 = stile.BinStep('column_0', low=0, high=6, n_bins=2)
        bin_obj1 = stile.BinList('column_1', [0, 2, 4])
        results = stile.ExpandBinList([bin_obj0, bin_obj1])
        expected_results = [(stile.binning.SingleBin('column_0',
                                                     low=0,
                                                     high=3,
                                                     short_name='b'),
                             stile.binning.SingleBin('column_1',
                                                     low=0,
                                                     high=2,
                                                     short_name='b')),
                            (stile.binning.SingleBin('column_0',
                                                     low=0,
                                                     high=3,
                                                     short_name='b'),
                             stile.binning.SingleBin('column_1',
                                                     low=2,
                                                     high=4,
                                                     short_name='b')),
                            (stile.binning.SingleBin('column_0',
                                                     low=3,
                                                     high=6,
                                                     short_name='b'),
                             stile.binning.SingleBin('column_1',
                                                     low=0,
                                                     high=2,
                                                     short_name='b')),
                            (stile.binning.SingleBin('column_0',
                                                     low=3,
                                                     high=6,
                                                     short_name='b'),
                             stile.binning.SingleBin('column_1',
                                                     low=2,
                                                     high=4,
                                                     short_name='b'))]
        numpy.testing.assert_equal(len(results), len(expected_results))
        for rpair, epair in zip(results, expected_results):
            self.assertTrue(compare_single_bin(rpair[0], epair[0]))
            self.assertTrue(compare_single_bin(rpair[1], epair[1]))
        self.assertRaises(TypeError, stile.ExpandBinList, bin_obj0, bin_obj1)
Ejemplo n.º 3
0
    def test_BinStep_SingleBin_creation(self):
        """Test that the constructor for SingleBin and BinStep objects behaves appropriately given
        various inputs."""
        # All of these should return the same objects (the expected_obj_list), except the final one,
        # which should return them in the reverse order.
        lhs = stile.BinStep('field_0', low=0, high=6, step=1)
        lhn = stile.BinStep('field_0', low=0, high=6, n_bins=6)
        lsn = stile.BinStep('field_0', low=0, step=1, n_bins=6)
        hsn = stile.BinStep('field_0', high=6, step=1, n_bins=6)
        reverse_lhs = stile.BinStep('field_0', low=6, high=0, step=-1)

        expected_obj_list = [
            stile.binning.SingleBin('field_0', low=0, high=1, short_name='b'),
            stile.binning.SingleBin('field_0', low=1, high=2, short_name='b'),
            stile.binning.SingleBin('field_0', low=2, high=3, short_name='b'),
            stile.binning.SingleBin('field_0', low=3, high=4, short_name='b'),
            stile.binning.SingleBin('field_0', low=4, high=5, short_name='b'),
            stile.binning.SingleBin('field_0', low=5, high=6, short_name='b')
        ]

        names = [
            "passed low, high, and step", "passed low, high, and n_bins",
            "passed low, step, and n_bins", "passed high, step, and n_bins",
            "passed low, high, and step with low and high reversed"
        ]
        objs = [lhs, lhn, lsn, hsn, reverse_lhs]
        for obj, name in zip(objs, names):
            obj_list = obj()
            if obj == reverse_lhs:
                obj_list.reverse()
            self.assertEqual(len(obj_list),
                             6,
                             msg='BinStep (' + name +
                             ') created wrong number of SingleBins!')
            for i in range(len(obj_list)):
                self.assertTrue(
                    compare_single_bin(obj_list[i], expected_obj_list[i]),
                    msg='BinStep (' + name + ') created incorrect SingleBins!')

        # As above, but using logarithmic bins.
        lhs = stile.BinStep('field_0',
                            low=0.25,
                            high=8,
                            step=numpy.log(2.),
                            use_log=True)
        lhn = stile.BinStep('field_0',
                            low=0.25,
                            high=8,
                            n_bins=5,
                            use_log=True)
        lsn = stile.BinStep('field_0',
                            low=0.25,
                            step=numpy.log(2.),
                            n_bins=5,
                            use_log=True)
        hsn = stile.BinStep('field_0',
                            high=8,
                            step=numpy.log(2.),
                            n_bins=5,
                            use_log=True)
        reverse_lhs = stile.BinStep('field_0',
                                    low=8,
                                    high=0.25,
                                    step=-numpy.log(2.),
                                    use_log=True)

        expected_obj_list = [
            stile.binning.SingleBin('field_0',
                                    low=0.25,
                                    high=0.5,
                                    short_name='b'),
            stile.binning.SingleBin('field_0',
                                    low=0.5,
                                    high=1.,
                                    short_name='b'),
            stile.binning.SingleBin('field_0', low=1., high=2.,
                                    short_name='b'),
            stile.binning.SingleBin('field_0', low=2., high=4.,
                                    short_name='b'),
            stile.binning.SingleBin('field_0', low=4., high=8., short_name='b')
        ]

        names = [
            "passed low, high, and step", "passed low, high, and n_bins",
            "passed low, step, and n_bins", "passed high, step, and n_bins",
            "passed low, high, and step with low and high reversed"
        ]
        objs = [lhs, lhn, lsn, hsn, reverse_lhs]
        for obj, name in zip(objs, names):
            obj_list = obj()
            if obj == reverse_lhs:
                obj_list.reverse()
            self.assertEqual(len(obj_list),
                             5,
                             msg='Log BinStep (' + name +
                             ') created wrong number of SingleBins!')
            for i in range(len(obj_list)):
                self.assertTrue(
                    compare_single_bin(obj_list[i], expected_obj_list[i]),
                    msg='BinStep (' + name + ') created incorrect SingleBins!')
Ejemplo n.º 4
0
 def test_bin_creation_errors(self):
     """Test for initialization errors and proper treatment of weird arguments."""
     # Invalid bounds in logarithmic BinStep
     self.assertRaises(ValueError,
                       stile.BinStep,
                       'c',
                       low=0,
                       high=10,
                       step=1,
                       use_log=True)
     self.assertRaises(ValueError,
                       stile.BinStep,
                       'c',
                       low=10,
                       high=-1,
                       step=-1,
                       use_log=True)
     # Various not-enough-arguments errors to BinStep (probably overkill)
     self.assertRaises(TypeError, stile.BinStep)
     self.assertRaises(TypeError, stile.BinStep, 'c')
     self.assertRaises(TypeError, stile.BinStep, 'c', low=1)
     self.assertRaises(TypeError, stile.BinStep, 'c', low=1, high=2)
     self.assertRaises(TypeError, stile.BinStep, 'c', low=1, step=2)
     self.assertRaises(TypeError, stile.BinStep, 'c', low=1, n_bins=2)
     self.assertRaises(TypeError, stile.BinStep, 'c', step=1)
     self.assertRaises(TypeError, stile.BinStep, 'c', step=1, n_bins=2)
     self.assertRaises(TypeError, stile.BinStep, 'c', step=1, high=2)
     self.assertRaises(TypeError, stile.BinStep, 'c', n_bins=1)
     self.assertRaises(TypeError, stile.BinStep, 'c', n_bins=1, high=2)
     self.assertRaises(TypeError, stile.BinStep, 'c', high=2)
     # Inconsistent and nonsense arguments to BinStep
     self.assertRaises(ValueError,
                       stile.BinStep,
                       'c',
                       low=1,
                       high=0,
                       step=0.5)
     self.assertRaises(ValueError,
                       stile.BinStep,
                       'c',
                       low=0,
                       high=1,
                       step=-0.5)
     self.assertRaises(ValueError,
                       stile.BinStep,
                       'c',
                       low=0,
                       high=5,
                       step=1,
                       n_bins=7)
     stile.BinStep('c', low=0, high=-1, step=-0.5)  # actually consistent
     self.assertRaises(ValueError,
                       stile.BinStep,
                       'c',
                       low=1,
                       high=1,
                       step=0.5)
     self.assertRaises(ValueError,
                       stile.BinStep,
                       'c',
                       low=1,
                       high=2,
                       n_bins=-1)
     self.assertRaises(TypeError, stile.BinStep, 0, low=0, high=5, step=1)
     # Wrong arguments to BinList
     self.assertRaises(TypeError, stile.BinList, 'c', [1, 2, 3], n_bins=1)
     self.assertRaises(ValueError, stile.BinList, 'c', [1, 3, 2])
     self.assertRaises(TypeError, stile.BinList, 0, [1, 3])
     self.assertRaises(TypeError, stile.BinList, 'c', [])
     self.assertRaises(TypeError, stile.BinList, [1, 3, 2])
     self.assertRaises(TypeError, stile.BinList, 'c')
Ejemplo n.º 5
0
    def test_BinStep_log(self):
        """Test that BinStep objects with logarithmic spacing behave appropriately."""
        lhs = stile.BinStep('field_0',
                            low=0.25,
                            high=8,
                            step=numpy.log(2.),
                            use_log=True)
        lhn = stile.BinStep('field_0',
                            low=0.25,
                            high=8,
                            n_bins=5,
                            use_log=True)
        lsn = stile.BinStep('field_0',
                            low=0.25,
                            step=numpy.log(2.),
                            n_bins=5,
                            use_log=True)
        hsn = stile.BinStep('field_0',
                            high=8,
                            step=numpy.log(2.),
                            n_bins=5,
                            use_log=True)
        reverse_lhs = stile.BinStep('field_0',
                                    low=8,
                                    high=0.25,
                                    step=-numpy.log(2.),
                                    use_log=True)
        names = [
            "passed low, high, and step", "passed low, high, and n_bins",
            "passed low, step, and n_bins", "passed high, step, and n_bins",
            "passed low, high, and step with low and high reversed"
        ]

        objs = [lhs, lhn, lsn, hsn, reverse_lhs]

        expected_bin_array_1 = [
            self.bin_array_1[:0], self.bin_array_1[0], self.bin_array_1[1],
            self.bin_array_1[2:4], self.bin_array_1[4]
        ]
        expected_bin_array_2 = [
            self.bin_array_2[:0], self.bin_array_2[:0], self.bin_array_2[0],
            self.bin_array_2[1:3], self.bin_array_2[3:]
        ]
        expected_bin_array_3 = [
            self.bin_array_3[:0], self.bin_array_3[:2], self.bin_array_3[:0],
            self.bin_array_3[4], self.bin_array_3[2:4]
        ]
        expected_bin_array_4 = [
            self.bin_array_4[:0], self.bin_array_4[0], self.bin_array_4[:0],
            self.bin_array_4[:0], self.bin_array_4[:0]
        ]
        expected_bin_array_5 = [
            self.bin_array_5[:0], self.bin_array_5[:0], self.bin_array_5[0],
            self.bin_array_5[1], self.bin_array_5[2]
        ]
        expected_bin_array_6 = [
            self.bin_array_6[:0], self.bin_array_6[:0], self.bin_array_6[1],
            self.bin_array_6[:0], self.bin_array_6[:0]
        ]

        for obj, name in zip(objs, names):
            err_msg = (
                "Logarithmic BinStep test (" + name +
                ") failed to produce correct binning for array %s in bin # %i")
            obj_list = obj()
            self.assertEqual(
                len(obj_list),
                5,
                msg=
                ('Wrong number of bins created from logarithmic BinStep with '
                 + name + ': ' + str(len(obj_list))))
            if obj == reverse_lhs:
                obj_list.reverse()
            for i, singlebin in enumerate(obj_list):
                results = singlebin(self.bin_array_1)
                numpy.testing.assert_equal(results,
                                           expected_bin_array_1[i],
                                           err_msg=err_msg %
                                           (self.bin_array_1, i))
                results = singlebin(self.bin_array_2)
                numpy.testing.assert_equal(results,
                                           expected_bin_array_2[i],
                                           err_msg=err_msg %
                                           (self.bin_array_2, i))
                results = singlebin(self.bin_array_3)
                numpy.testing.assert_equal(results,
                                           expected_bin_array_3[i],
                                           err_msg=err_msg %
                                           (self.bin_array_3, i))
                results = singlebin(self.bin_array_4)
                numpy.testing.assert_equal(results,
                                           expected_bin_array_4[i],
                                           err_msg=err_msg %
                                           (self.bin_array_4, i))
                results = singlebin(self.bin_array_5)
                numpy.testing.assert_equal(results,
                                           expected_bin_array_5[i],
                                           err_msg=err_msg %
                                           (self.bin_array_5, i))
                results = singlebin(self.bin_array_6)
                numpy.testing.assert_equal(results,
                                           expected_bin_array_6[i],
                                           err_msg=err_msg %
                                           (self.bin_array_6, i))
Ejemplo n.º 6
0
    def test_BinStep_linear(self):
        """Test that BinStep objects with linear spacing behave appropriately."""
        lhs = stile.BinStep('field_0', low=0, high=6, step=1)
        lhn = stile.BinStep('field_0', low=0, high=6, n_bins=6)
        lsn = stile.BinStep('field_0', low=0, step=1, n_bins=6)
        hsn = stile.BinStep('field_0', high=6, step=1, n_bins=6)
        reverse_lhs = stile.BinStep('field_0', low=6, high=0, step=-1)

        names = [
            "passed low, high, and step", "passed low, high, and n_bins",
            "passed low, step, and n_bins", "passed high, step, and n_bins",
            "passed low, high, and step with low and high reversed"
        ]
        objs = [lhs, lhn, lsn, hsn, reverse_lhs]

        # Expected results; each item of the list is the result of the n-th SingleBin.
        # Formatted arrays don't compare properly to non-formatted arrays, so we use slices of the
        # original array to ensure the formatting matches properly even for empty (formatted)
        # arrays.
        expected_bin_array_1 = [
            self.bin_array_1[0], self.bin_array_1[1], self.bin_array_1[2],
            self.bin_array_1[3], self.bin_array_1[4], self.bin_array_1[:0]
        ]
        expected_bin_array_2 = [
            self.bin_array_2[:0], self.bin_array_2[0], self.bin_array_2[1],
            self.bin_array_2[2], self.bin_array_2[3], self.bin_array_2[4]
        ]
        expected_bin_array_3 = [
            self.bin_array_3[0:2], self.bin_array_3[:0], self.bin_array_3[:0],
            self.bin_array_3[4], self.bin_array_3[3], self.bin_array_3[2]
        ]
        expected_bin_array_4 = [
            self.bin_array_4[0], self.bin_array_4[:0], self.bin_array_4[:0],
            self.bin_array_4[:0], self.bin_array_4[:0], self.bin_array_4[:0]
        ]
        expected_bin_array_5 = [
            self.bin_array_5[:0], self.bin_array_5[0], self.bin_array_5[:0],
            self.bin_array_5[1], self.bin_array_5[:0], self.bin_array_5[2]
        ]
        expected_bin_array_6 = [
            self.bin_array_6[:0], self.bin_array_6[1], self.bin_array_6[:0],
            self.bin_array_6[:0], self.bin_array_6[:0], self.bin_array_6[:0]
        ]

        for obj, name in zip(objs, names):
            err_msg = (
                "BinStep test (" + name +
                ") failed to produce correct binning for array %s in bin # %i")
            obj_list = obj()
            self.assertEqual(
                len(obj_list),
                6,
                msg=('Wrong number of bins created from BinStep with ' + name +
                     ': ' + str(len(obj_list))))

            if obj == reverse_lhs:
                obj_list.reverse()
            for i, singlebin in enumerate(obj_list):
                results = singlebin(self.bin_array_1)
                numpy.testing.assert_equal(results,
                                           expected_bin_array_1[i],
                                           err_msg=err_msg %
                                           (self.bin_array_1, i))
                results = singlebin(self.bin_array_2)
                numpy.testing.assert_equal(results,
                                           expected_bin_array_2[i],
                                           err_msg=err_msg %
                                           (self.bin_array_2, i))
                results = singlebin(self.bin_array_3)
                numpy.testing.assert_equal(results,
                                           expected_bin_array_3[i],
                                           err_msg=err_msg %
                                           (self.bin_array_3, i))
                results = singlebin(self.bin_array_4)
                numpy.testing.assert_equal(results,
                                           expected_bin_array_4[i],
                                           err_msg=err_msg %
                                           (self.bin_array_4, i))
                results = singlebin(self.bin_array_5)
                numpy.testing.assert_equal(results,
                                           expected_bin_array_5[i],
                                           err_msg=err_msg %
                                           (self.bin_array_5, i))
                results = singlebin(self.bin_array_6)
                numpy.testing.assert_equal(results,
                                           expected_bin_array_6[i],
                                           err_msg=err_msg %
                                           (self.bin_array_6, i))