import unittest
import numpy.testing as testing
import numpy as np
import fitsio
import tempfile
import os

from redmapper import ColorBackground
from redmapper import ColorBackgroundGenerator
from redmapper import Configuration

class ColorBackgroundTestCase(unittest.TestCase):
    """
    Tests for the redmapper.ColorBackground and
    redmapper.ColorBackgroundGenerator classes.
    """
    def runTest(self):
        """
        Run the ColorBackground and ColorBackgroundGenerator tests.
        """

        file_name = 'test_dr8_col_bkg.fit'
        file_path = 'data_for_tests'

        cbkg = ColorBackground('%s/%s' % (file_path, file_name))

        col1 = np.array([0.572300, 1.39560])
        col2 = np.array([0.7894, 0.9564])
        refmags = np.array([17.587, 18.956])

        refmagindex = np.array([258, 395])
        col1index = np.array([1, 17])
        col2index = np.array([15, 19])

        # These are new values that are based on improvements in the binning.
        idl_bkg1 = np.array([0.76778, 0.80049])
        idl_bkg2 = np.array([0.04012, 0.10077])
        idl_bkg12 = np.array([0.01085, 0.081])

        # Test color1
        py_outputs = cbkg.lookup_diagonal(1, col1, refmags)
        testing.assert_almost_equal(py_outputs, idl_bkg1, decimal=5)

        # Test color2
        py_outputs = cbkg.lookup_diagonal(2, col2, refmags)
        testing.assert_almost_equal(py_outputs, idl_bkg2, decimal=5)

        # Test off-diagonal
        py_outputs = cbkg.lookup_offdiag(1, 2, col1, col2, refmags)
        testing.assert_almost_equal(py_outputs, idl_bkg12, decimal=5)

        # And a test sigma_g with the usehdrarea=True
        cbkg2 = ColorBackground('%s/%s' % (file_path, file_name), usehdrarea=True)

        col1 = np.array([0.572300, 1.39560, 1.0])
        col2 = np.array([0.7894, 0.9564, 1.0])
        refmags = np.array([17.587, 18.956, 25.0])

        idl_sigma_g1 = np.array([127.698, 591.112, np.inf])
        idl_sigma_g2 = np.array([7.569, 82.8938, np.inf])

        # Test color1
        py_outputs = cbkg2.sigma_g_diagonal(1, col1, refmags)
        testing.assert_almost_equal(py_outputs, idl_sigma_g1, decimal=3)

        # Test color2
        py_outputs = cbkg2.sigma_g_diagonal(2, col2, refmags)
        testing.assert_almost_equal(py_outputs, idl_sigma_g2, decimal=3)


        #####################################################
        # Now a test of the generation of a color background
        conf_filename = 'testconfig.yaml'
        config = Configuration(file_path + "/" + conf_filename)

        tfile = tempfile.mkstemp()
        os.close(tfile[0])
        config.bkgfile_color = tfile[1]
        config.d.nside = 128
        config.d.hpix = [8421]
        config.border = 0.0

        cbg = ColorBackgroundGenerator(config, minrangecheck=5)
        # Need to set clobber=True because the tempfile was created
        cbg.run(clobber=True)

        fits = fitsio.FITS(config.bkgfile_color)

        # Make sure we have 11 extensions
        testing.assert_equal(len(fits), 11)

        # These tests are obsolete, but could be refactored
        # Check the 01_01 and 01_02
        # bkg11 = fits['01_01_REF'].read()
        # bkg11_compare = fitsio.read(file_path + "/test_dr8_bkg_zredc_sub.fits", ext='01_01_REF')
        # testing.assert_almost_equal(bkg11['BC'], bkg11_compare['BC'], 3)
        # testing.assert_almost_equal(bkg11['N'], bkg11_compare['N'], 3)

        # bkg12 = fits['01_02_REF'].read()
        # bkg12_compare = fitsio.read(file_path + "/test_dr8_bkg_zredc_sub.fits", ext='01_02_REF')

        # testing.assert_almost_equal(bkg12['BC'], bkg12_compare['BC'], 2)
        # testing.assert_almost_equal(bkg12['N'], bkg12_compare['N'], 4)

        # And delete the tempfile
        os.remove(config.bkgfile_color)

if __name__=='__main__':
    unittest.main()

[((3829, 3844), 'unittest.main', 'unittest.main', ([], {}), '()\n', (3842, 3844), False, 'import unittest\n'), ((604, 653), 'redmapper.ColorBackground', 'ColorBackground', (["('%s/%s' % (file_path, file_name))"], {}), "('%s/%s' % (file_path, file_name))\n", (619, 653), False, 'from redmapper import ColorBackground\n'), ((670, 696), 'numpy.array', 'np.array', (['[0.5723, 1.3956]'], {}), '([0.5723, 1.3956])\n', (678, 696), True, 'import numpy as np\n'), ((715, 741), 'numpy.array', 'np.array', (['[0.7894, 0.9564]'], {}), '([0.7894, 0.9564])\n', (723, 741), True, 'import numpy as np\n'), ((760, 786), 'numpy.array', 'np.array', (['[17.587, 18.956]'], {}), '([17.587, 18.956])\n', (768, 786), True, 'import numpy as np\n'), ((810, 830), 'numpy.array', 'np.array', (['[258, 395]'], {}), '([258, 395])\n', (818, 830), True, 'import numpy as np\n'), ((851, 868), 'numpy.array', 'np.array', (['[1, 17]'], {}), '([1, 17])\n', (859, 868), True, 'import numpy as np\n'), ((889, 907), 'numpy.array', 'np.array', (['[15, 19]'], {}), '([15, 19])\n', (897, 907), True, 'import numpy as np\n'), ((1006, 1034), 'numpy.array', 'np.array', (['[0.76778, 0.80049]'], {}), '([0.76778, 0.80049])\n', (1014, 1034), True, 'import numpy as np\n'), ((1054, 1082), 'numpy.array', 'np.array', (['[0.04012, 0.10077]'], {}), '([0.04012, 0.10077])\n', (1062, 1082), True, 'import numpy as np\n'), ((1103, 1129), 'numpy.array', 'np.array', (['[0.01085, 0.081]'], {}), '([0.01085, 0.081])\n', (1111, 1129), True, 'import numpy as np\n'), ((1221, 1281), 'numpy.testing.assert_almost_equal', 'testing.assert_almost_equal', (['py_outputs', 'idl_bkg1'], {'decimal': '(5)'}), '(py_outputs, idl_bkg1, decimal=5)\n', (1248, 1281), True, 'import numpy.testing as testing\n'), ((1373, 1433), 'numpy.testing.assert_almost_equal', 'testing.assert_almost_equal', (['py_outputs', 'idl_bkg2'], {'decimal': '(5)'}), '(py_outputs, idl_bkg2, decimal=5)\n', (1400, 1433), True, 'import numpy.testing as testing\n'), ((1539, 1600), 'numpy.testing.assert_almost_equal', 'testing.assert_almost_equal', (['py_outputs', 'idl_bkg12'], {'decimal': '(5)'}), '(py_outputs, idl_bkg12, decimal=5)\n', (1566, 1600), True, 'import numpy.testing as testing\n'), ((1672, 1738), 'redmapper.ColorBackground', 'ColorBackground', (["('%s/%s' % (file_path, file_name))"], {'usehdrarea': '(True)'}), "('%s/%s' % (file_path, file_name), usehdrarea=True)\n", (1687, 1738), False, 'from redmapper import ColorBackground\n'), ((1755, 1786), 'numpy.array', 'np.array', (['[0.5723, 1.3956, 1.0]'], {}), '([0.5723, 1.3956, 1.0])\n', (1763, 1786), True, 'import numpy as np\n'), ((1805, 1836), 'numpy.array', 'np.array', (['[0.7894, 0.9564, 1.0]'], {}), '([0.7894, 0.9564, 1.0])\n', (1813, 1836), True, 'import numpy as np\n'), ((1855, 1887), 'numpy.array', 'np.array', (['[17.587, 18.956, 25.0]'], {}), '([17.587, 18.956, 25.0])\n', (1863, 1887), True, 'import numpy as np\n'), ((1912, 1948), 'numpy.array', 'np.array', (['[127.698, 591.112, np.inf]'], {}), '([127.698, 591.112, np.inf])\n', (1920, 1948), True, 'import numpy as np\n'), ((1972, 2006), 'numpy.array', 'np.array', (['[7.569, 82.8938, np.inf]'], {}), '([7.569, 82.8938, np.inf])\n', (1980, 2006), True, 'import numpy as np\n'), ((2100, 2164), 'numpy.testing.assert_almost_equal', 'testing.assert_almost_equal', (['py_outputs', 'idl_sigma_g1'], {'decimal': '(3)'}), '(py_outputs, idl_sigma_g1, decimal=3)\n', (2127, 2164), True, 'import numpy.testing as testing\n'), ((2258, 2322), 'numpy.testing.assert_almost_equal', 'testing.assert_almost_equal', (['py_outputs', 'idl_sigma_g2'], {'decimal': '(3)'}), '(py_outputs, idl_sigma_g2, decimal=3)\n', (2285, 2322), True, 'import numpy.testing as testing\n'), ((2507, 2553), 'redmapper.Configuration', 'Configuration', (["(file_path + '/' + conf_filename)"], {}), "(file_path + '/' + conf_filename)\n", (2520, 2553), False, 'from redmapper import Configuration\n'), ((2571, 2589), 'tempfile.mkstemp', 'tempfile.mkstemp', ([], {}), '()\n', (2587, 2589), False, 'import tempfile\n'), ((2598, 2616), 'os.close', 'os.close', (['tfile[0]'], {}), '(tfile[0])\n', (2606, 2616), False, 'import os\n'), ((2760, 2809), 'redmapper.ColorBackgroundGenerator', 'ColorBackgroundGenerator', (['config'], {'minrangecheck': '(5)'}), '(config, minrangecheck=5)\n', (2784, 2809), False, 'from redmapper import ColorBackgroundGenerator\n'), ((2924, 2957), 'fitsio.FITS', 'fitsio.FITS', (['config.bkgfile_color'], {}), '(config.bkgfile_color)\n', (2935, 2957), False, 'import fitsio\n'), ((3767, 3798), 'os.remove', 'os.remove', (['config.bkgfile_color'], {}), '(config.bkgfile_color)\n', (3776, 3798), False, 'import os\n')]