Exemple #1
0
    def testSumMagnitudes(self):
        """
        Test that the method sum_magnitudes in PhotometryGalaxies handles
        NaNs correctly.  Test it both in the vectorized and non-vectorized form.
        """
        mm_0 = 22.0

        bulge = 15.0 * np.ones(8)

        disk = 15.2 * np.ones(8)

        agn = 15.4 * np.ones(8)

        bulge[0] = np.NaN
        disk[1] = np.NaN
        agn[2] = np.NaN

        bulge[3] = np.NaN
        disk[3] = np.NaN

        bulge[4] = np.NaN
        agn[4] = np.NaN

        disk[5] = np.NaN
        agn[5] = np.NaN

        bulge[7] = np.NaN
        disk[7] = np.NaN
        agn[7] = np.NaN

        bulge_flux = np.power(10.0, -0.4 * (bulge - mm_0))
        disk_flux = np.power(10.0, -0.4 * (disk - mm_0))
        agn_flux = np.power(10.0, -0.4 * (agn - mm_0))

        answer = np.zeros(8)
        answer[0] = -2.5 * np.log10(disk_flux[0] + agn_flux[0]) + mm_0
        answer[1] = -2.5 * np.log10(bulge_flux[1] + agn_flux[1]) + mm_0
        answer[2] = -2.5 * np.log10(bulge_flux[2] + disk_flux[2]) + mm_0
        answer[3] = -2.5 * np.log10(agn_flux[3]) + mm_0
        answer[4] = -2.5 * np.log10(disk_flux[4]) + mm_0
        answer[5] = -2.5 * np.log10(bulge_flux[5]) + mm_0
        answer[6] = -2.5 * np.log10(bulge_flux[6] + disk_flux[6] +
                                    agn_flux[6]) + mm_0
        answer[7] = np.NaN

        phot = PhotometryGalaxies()
        test = phot.sum_magnitudes(bulge=bulge, disk=disk, agn=agn)

        np.testing.assert_array_almost_equal(test, answer, decimal=10)

        for ix, (bb, dd, aa, truth) in enumerate(zip(bulge, disk, agn,
                                                     answer)):
            test = phot.sum_magnitudes(bulge=bb, disk=dd, agn=aa)
            if ix < 7:
                self.assertAlmostEqual(test, truth, 10)
                self.assertFalse(np.isnan(test),
                                 msg='test is NaN; should not be')
            else:
                np.testing.assert_equal(test, np.NaN)
                np.testing.assert_equal(truth, np.NaN)
Exemple #2
0
    def _loadAgnSedList(self, wavelen_match):
        """
        Wraps the PhotometryGalaxies._loadAgnSedList method.

        If current chunk of objects is not represented in the global
        _sed_cache, this will call the base method defined in
        PhotometryGalaxies.

        Otherwise, it will read self._sedList from the cache.
        That way, the photometry getters defined in PhotometryGalaxies will
        not read in SEDs that have already been cached.
        """

        global _sed_cache

        object_names = self.column_by_name("uniqueId")

        if len(object_names) > 0:
            cache_name = "agn_%s_%s" % (object_names[0], object_names[-1])
        else:
            cache_name = None

        if cache_name not in _sed_cache:

            PhotometryGalaxies._loadAgnSedList(self, wavelen_match)

            if cache_name is not None:
                _sed_cache[cache_name] = copy.copy(self._agnSedList)
        else:
            self._agnSedList = copy.copy(_sed_cache[cache_name])
    def _loadAgnSedList(self, wavelen_match):
        """
        Wraps the PhotometryGalaxies._loadAgnSedList method.

        If current chunk of objects is not represented in the global
        _sed_cache, this will call the base method defined in
        PhotometryGalaxies.

        Otherwise, it will read self._sedList from the cache.
        That way, the photometry getters defined in PhotometryGalaxies will
        not read in SEDs that have already been cached.
        """

        global _sed_cache

        object_names = self.column_by_name("uniqueId")

        if len(object_names) > 0:
            cache_name = "agn_%s_%s" % (object_names[0], object_names[-1])
        else:
            cache_name = None

        if cache_name not in _sed_cache:

            PhotometryGalaxies._loadAgnSedList(self, wavelen_match)

            if cache_name is not None:
                _sed_cache[cache_name] = copy.copy(self._agnSedList)
        else:
            self._agnSedList = copy.copy(_sed_cache[cache_name])
    def testSumMagnitudes(self):
        """
        Test that the method sum_magnitudes in PhotometryGalaxies handles
        NaNs correctly.  Test it both in the vectorized and non-vectorized form.
        """
        mm_0 = 22.0

        bulge = 15.0*np.ones(8)

        disk = 15.2*np.ones(8)

        agn = 15.4*np.ones(8)

        bulge[0] = np.NaN
        disk[1] = np.NaN
        agn[2] = np.NaN

        bulge[3] = np.NaN
        disk[3] = np.NaN

        bulge[4] = np.NaN
        agn[4] = np.NaN

        disk[5] = np.NaN
        agn[5] = np.NaN

        bulge[7] = np.NaN
        disk[7] = np.NaN
        agn[7] = np.NaN

        bulge_flux = np.power(10.0, -0.4*(bulge-mm_0))
        disk_flux = np.power(10.0, -0.4*(disk-mm_0))
        agn_flux = np.power(10.0, -0.4*(agn-mm_0))

        answer = np.zeros(8)
        answer[0] = -2.5*np.log10(disk_flux[0]+agn_flux[0]) + mm_0
        answer[1] = -2.5*np.log10(bulge_flux[1]+agn_flux[1]) + mm_0
        answer[2] = -2.5*np.log10(bulge_flux[2]+disk_flux[2]) + mm_0
        answer[3] = -2.5*np.log10(agn_flux[3]) + mm_0
        answer[4] = -2.5*np.log10(disk_flux[4]) + mm_0
        answer[5] = -2.5*np.log10(bulge_flux[5]) + mm_0
        answer[6] = -2.5*np.log10(bulge_flux[6]+disk_flux[6]+agn_flux[6]) + mm_0
        answer[7] = np.NaN

        phot = PhotometryGalaxies()
        test = phot.sum_magnitudes(bulge=bulge, disk=disk, agn=agn)

        np.testing.assert_array_almost_equal(test, answer, decimal=10)

        for ix, (bb, dd, aa, truth) in enumerate(zip(bulge, disk, agn, answer)):
            test = phot.sum_magnitudes(bulge=bb, disk=dd, agn=aa)
            if ix < 7:
                self.assertAlmostEqual(test, truth, 10)
                self.assertFalse(np.isnan(test), msg='test is NaN; should not be')
            else:
                np.testing.assert_equal(test, np.NaN)
                np.testing.assert_equal(truth, np.NaN)
    def testGalaxyVariabilityInfrastructure(self):
        """
        Test that the variability design was correctly implemented in
        the case of galaxies
        """

        outputs = [
            'id', 'test_u', 'test_g', 'test_r', 'test_i', 'test_z', 'test_y',
            'test_bulge_u', 'test_bulge_g', 'test_bulge_r', 'test_bulge_i',
            'test_bulge_z', 'test_bulge_y', 'test_disk_u', 'test_disk_g',
            'test_disk_r', 'test_disk_i', 'test_disk_z', 'test_disk_y',
            'test_agn_u', 'test_agn_g', 'test_agn_r', 'test_agn_i',
            'test_agn_z', 'test_agn_y'
        ]

        baseline = GalaxyBaselineCatalogClass(self.galaxyDB,
                                              column_outputs=outputs)
        variable = GalaxyVariabilityCatalogClass(self.galaxyDB,
                                                 column_outputs=outputs)

        phot = PhotometryGalaxies()

        variable_indices = [19, 20, 21, 7, 16]

        for bb, vv in zip(baseline.iter_catalog(), variable.iter_catalog()):
            self.assertEqual(bb[0], vv[0])

            # test that the variable components are altered
            # the way they ought to be
            self.assertAlmostEqual(bb[19] + 1.0, vv[19], 10)
            self.assertAlmostEqual(bb[20] + 2.0, vv[20], 10)
            self.assertAlmostEqual(bb[21] + 3.0, vv[21], 10)
            self.assertAlmostEqual(bb[7] + 4.0, vv[7], 10)
            self.assertAlmostEqual(bb[16] + 5.0, vv[16], 10)

            # test that the components which do not vary are equal
            for ix in range(7, 25):
                if ix not in variable_indices:
                    self.assertAlmostEqual(bb[ix], vv[ix], 10)

            # test that the total magnitudes are correctly calculated
            for ix in range(6):

                self.assertAlmostEqual(
                    bb[ix + 1],
                    phot.sum_magnitudes(bulge=bb[7 + ix],
                                        disk=bb[13 + ix],
                                        agn=bb[19 + ix]), 10)

                self.assertAlmostEqual(
                    vv[ix + 1],
                    phot.sum_magnitudes(bulge=vv[7 + ix],
                                        disk=vv[13 + ix],
                                        agn=vv[19 + ix]), 10)
    def testGalaxyVariabilityInfrastructure(self):
        """
        Test that the variability design was correctly implemented in
        the case of galaxies
        """

        outputs = ['id',
                   'test_u', 'test_g', 'test_r', 'test_i', 'test_z', 'test_y',
                   'test_bulge_u', 'test_bulge_g', 'test_bulge_r', 'test_bulge_i',
                   'test_bulge_z', 'test_bulge_y',
                   'test_disk_u', 'test_disk_g', 'test_disk_r', 'test_disk_i',
                   'test_disk_z', 'test_disk_y',
                   'test_agn_u', 'test_agn_g', 'test_agn_r',
                   'test_agn_i', 'test_agn_z', 'test_agn_y']

        baseline = GalaxyBaselineCatalogClass(self.galaxyDB, column_outputs=outputs)
        variable = GalaxyVariabilityCatalogClass(self.galaxyDB, column_outputs=outputs)

        phot = PhotometryGalaxies()

        variable_indices = [19, 20, 21, 7, 16]

        for bb, vv in zip(baseline.iter_catalog(), variable.iter_catalog()):
            self.assertEqual(bb[0], vv[0])

            # test that the variable components are altered
            # the way they ought to be
            self.assertAlmostEqual(bb[19]+1.0, vv[19], 10)
            self.assertAlmostEqual(bb[20]+2.0, vv[20], 10)
            self.assertAlmostEqual(bb[21]+3.0, vv[21], 10)
            self.assertAlmostEqual(bb[7]+4.0, vv[7], 10)
            self.assertAlmostEqual(bb[16]+5.0, vv[16], 10)

            # test that the components which do not vary are equal
            for ix in range(7, 25):
                if ix not in variable_indices:
                    self.assertAlmostEqual(bb[ix], vv[ix], 10)

            # test that the total magnitudes are correctly calculated
            for ix in range(6):

                self.assertAlmostEqual(bb[ix+1],
                                       phot.sum_magnitudes(bulge=bb[7+ix],
                                                           disk=bb[13+ix],
                                                           agn=bb[19+ix]),
                                       10)

                self.assertAlmostEqual(vv[ix+1],
                                       phot.sum_magnitudes(bulge=vv[7+ix],
                                                           disk=vv[13+ix],
                                                           agn=vv[19+ix]),
                                       10)
    def testSumMagnitudesCatalog(self):
        """
        test that sum_magnitudes handles NaNs correctly in the context
        of a catalog by outputting a catalog of galaxies with NaNs in
        different component magnitudes, reading that catalog back in,
        and then calculating the summed magnitude by hand and comparing
        """

        obs_metadata = ObservationMetaData(mjd=50000.0,
                                           boundType='circle',
                                           pointingRA=0.0, pointingDec=0.0,
                                           boundLength=10.0)

        test_cat = galaxiesWithHoles(self.galaxy, obs_metadata=obs_metadata)
        dtype = np.dtype([('raJ2000', np.float),
                          ('decJ2000', np.float),
                          ('u', np.float), ('g', np.float), ('r', np.float),
                          ('i', np.float), ('z', np.float), ('y', np.float),
                          ('ub', np.float), ('gb', np.float), ('rb', np.float),
                          ('ib', np.float), ('zb', np.float), ('yb', np.float),
                          ('ud', np.float), ('gd', np.float), ('rd', np.float),
                          ('id', np.float), ('zd', np.float), ('yd', np.float),
                          ('ua', np.float), ('ga', np.float), ('ra', np.float),
                          ('ia', np.float), ('za', np.float), ('ya', np.float)])

        with lsst.utils.tests.getTempFilePath('.txt') as catName:
            test_cat.write_catalog(catName)
            data = np.genfromtxt(catName, dtype=dtype, delimiter=', ')
        self.assertGreater(len(data), 16)
        phot = PhotometryGalaxies()

        test = phot.sum_magnitudes(bulge=data['ub'], disk=data['ud'], agn=data['ua'])
        np.testing.assert_array_almost_equal(test, data['u'], decimal=10)

        test = phot.sum_magnitudes(bulge=data['gb'], disk=data['gd'], agn=data['ga'])
        np.testing.assert_array_almost_equal(test, data['g'], decimal=10)

        test = phot.sum_magnitudes(bulge=data['rb'], disk=data['rd'], agn=data['ra'])
        np.testing.assert_array_almost_equal(test, data['r'], decimal=10)

        test = phot.sum_magnitudes(bulge=data['ib'], disk=data['id'], agn=data['ia'])
        np.testing.assert_array_almost_equal(test, data['i'], decimal=10)

        test = phot.sum_magnitudes(bulge=data['zb'], disk=data['zd'], agn=data['za'])
        np.testing.assert_array_almost_equal(test, data['z'], decimal=10)

        test = phot.sum_magnitudes(bulge=data['yb'], disk=data['yd'], agn=data['ya'])
        np.testing.assert_array_almost_equal(test, data['y'], decimal=10)

        # make sure that there were some NaNs for our catalog to deal with (but that they were not
        # all NaNs
        for line in [data['u'], data['g'], data['r'], data['i'], data['z'], data['y'],
                     data['ub'], data['gb'], data['rb'], data['ib'], data['zb'], data['yb'],
                     data['ud'], data['gd'], data['rd'], data['id'], data['zd'], data['yd'],
                     data['ua'], data['ga'], data['ra'], data['ia'], data['za'], data['ya']]:

            ctNans = len(np.where(np.isnan(line))[0])
            self.assertGreater(ctNans, 0)
            self.assertLess(ctNans, len(line))
Exemple #8
0
    def testAlternateBandpassesGalaxies(self):
        """
        the same as testAlternateBandpassesStars, but for galaxies
        """

        obs_metadata_pointed = ObservationMetaData(mjd=50000.0,
                                                   boundType='circle',
                                                   pointingRA=0.0,
                                                   pointingDec=0.0,
                                                   boundLength=10.0)

        dtype = np.dtype([('galid', np.int), ('ra', np.float),
                          ('dec', np.float), ('uTotal', np.float),
                          ('gTotal', np.float), ('rTotal', np.float),
                          ('iTotal', np.float), ('zTotal', np.float),
                          ('uBulge', np.float), ('gBulge', np.float),
                          ('rBulge', np.float), ('iBulge', np.float),
                          ('zBulge', np.float), ('uDisk', np.float),
                          ('gDisk', np.float), ('rDisk', np.float),
                          ('iDisk', np.float), ('zDisk', np.float),
                          ('uAgn', np.float), ('gAgn', np.float),
                          ('rAgn', np.float), ('iAgn', np.float),
                          ('zAgn', np.float), ('bulgeName', str, 200),
                          ('bulgeNorm', np.float), ('bulgeAv', np.float),
                          ('diskName', str, 200), ('diskNorm', np.float),
                          ('diskAv', np.float), ('agnName', str, 200),
                          ('agnNorm', np.float), ('redshift', np.float)])

        test_cat = cartoonGalaxies(self.galaxy,
                                   obs_metadata=obs_metadata_pointed)
        with lsst.utils.tests.getTempFilePath('.txt') as catName:
            test_cat.write_catalog(catName)
            catData = np.genfromtxt(catName, dtype=dtype, delimiter=', ')

        self.assertGreater(len(catData), 0)

        cartoonDir = getPackageDir('sims_photUtils')
        cartoonDir = os.path.join(cartoonDir, 'tests', 'cartoonSedTestData')
        sedDir = getPackageDir('sims_sed_library')

        testBandpasses = {}
        keys = ['u', 'g', 'r', 'i', 'z']

        for kk in keys:
            testBandpasses[kk] = Bandpass()
            testBandpasses[kk].readThroughput(
                os.path.join(cartoonDir, "test_bandpass_%s.dat" % kk))

        imsimBand = Bandpass()
        imsimBand.imsimBandpass()

        specMap = defaultSpecMap

        ct = 0
        for line in catData:
            bulgeMagList = []
            diskMagList = []
            agnMagList = []
            if line['bulgeName'] == 'None':
                for bp in keys:
                    np.testing.assert_equal(line['%sBulge' % bp], np.NaN)
                    bulgeMagList.append(np.NaN)
            else:
                ct += 1
                dummySed = Sed()
                dummySed.readSED_flambda(
                    os.path.join(sedDir, specMap[line['bulgeName']]))
                fnorm = dummySed.calcFluxNorm(line['bulgeNorm'], imsimBand)
                dummySed.multiplyFluxNorm(fnorm)
                a_int, b_int = dummySed.setupCCM_ab()
                dummySed.addDust(a_int, b_int, A_v=line['bulgeAv'])
                dummySed.redshiftSED(line['redshift'], dimming=True)
                dummySed.resampleSED(wavelen_match=testBandpasses['u'].wavelen)
                for bpName in keys:
                    mag = dummySed.calcMag(testBandpasses[bpName])
                    self.assertAlmostEqual(mag, line['%sBulge' % bpName], 10)
                    bulgeMagList.append(mag)

            if line['diskName'] == 'None':
                for bp in keys:
                    np.assert_equal(line['%sDisk' % bp], np.NaN)
                    diskMagList.append(np.NaN)
            else:
                ct += 1
                dummySed = Sed()
                dummySed.readSED_flambda(
                    os.path.join(sedDir, specMap[line['diskName']]))
                fnorm = dummySed.calcFluxNorm(line['diskNorm'], imsimBand)
                dummySed.multiplyFluxNorm(fnorm)
                a_int, b_int = dummySed.setupCCM_ab()
                dummySed.addDust(a_int, b_int, A_v=line['diskAv'])
                dummySed.redshiftSED(line['redshift'], dimming=True)
                dummySed.resampleSED(wavelen_match=testBandpasses['u'].wavelen)
                for bpName in keys:
                    mag = dummySed.calcMag(testBandpasses[bpName])
                    self.assertAlmostEqual(mag, line['%sDisk' % bpName], 10)
                    diskMagList.append(mag)

            if line['agnName'] == 'None':
                for bp in keys:
                    np.testing.assert_true(line['%sAgn' % bp], np.NaN)
                    agnMagList.append(np.NaN)
            else:
                ct += 1
                dummySed = Sed()
                dummySed.readSED_flambda(
                    os.path.join(sedDir, specMap[line['agnName']]))
                fnorm = dummySed.calcFluxNorm(line['agnNorm'], imsimBand)
                dummySed.multiplyFluxNorm(fnorm)
                dummySed.redshiftSED(line['redshift'], dimming=True)
                dummySed.resampleSED(wavelen_match=testBandpasses['u'].wavelen)
                for bpName in keys:
                    mag = dummySed.calcMag(testBandpasses[bpName])
                    self.assertAlmostEqual(mag, line['%sAgn' % bpName], 10)
                    agnMagList.append(mag)

            totalMags = PhotometryGalaxies().sum_magnitudes(
                bulge=np.array(bulgeMagList),
                disk=np.array(diskMagList),
                agn=np.array(agnMagList))

            for testMag, bpName in zip(totalMags, keys):
                if np.isnan(line['%sTotal' % bpName]):
                    np.testing.assert_equal(testMag, np.NaN)
                else:
                    self.assertAlmostEqual(testMag, line['%sTotal' % bpName],
                                           10)

        self.assertGreater(ct, 0)
Exemple #9
0
    def testSumMagnitudesCatalog(self):
        """
        test that sum_magnitudes handles NaNs correctly in the context
        of a catalog by outputting a catalog of galaxies with NaNs in
        different component magnitudes, reading that catalog back in,
        and then calculating the summed magnitude by hand and comparing
        """

        obs_metadata = ObservationMetaData(mjd=50000.0,
                                           boundType='circle',
                                           pointingRA=0.0,
                                           pointingDec=0.0,
                                           boundLength=10.0)

        test_cat = galaxiesWithHoles(self.galaxy, obs_metadata=obs_metadata)
        dtype = np.dtype([('raJ2000', np.float), ('decJ2000', np.float),
                          ('u', np.float), ('g', np.float), ('r', np.float),
                          ('i', np.float), ('z', np.float), ('y', np.float),
                          ('ub', np.float), ('gb', np.float), ('rb', np.float),
                          ('ib', np.float), ('zb', np.float), ('yb', np.float),
                          ('ud', np.float), ('gd', np.float), ('rd', np.float),
                          ('id', np.float), ('zd', np.float), ('yd', np.float),
                          ('ua', np.float), ('ga', np.float), ('ra', np.float),
                          ('ia', np.float), ('za', np.float),
                          ('ya', np.float)])

        with lsst.utils.tests.getTempFilePath('.txt') as catName:
            test_cat.write_catalog(catName)
            data = np.genfromtxt(catName, dtype=dtype, delimiter=', ')
        self.assertGreater(len(data), 16)
        phot = PhotometryGalaxies()

        test = phot.sum_magnitudes(bulge=data['ub'],
                                   disk=data['ud'],
                                   agn=data['ua'])
        np.testing.assert_array_almost_equal(test, data['u'], decimal=10)

        test = phot.sum_magnitudes(bulge=data['gb'],
                                   disk=data['gd'],
                                   agn=data['ga'])
        np.testing.assert_array_almost_equal(test, data['g'], decimal=10)

        test = phot.sum_magnitudes(bulge=data['rb'],
                                   disk=data['rd'],
                                   agn=data['ra'])
        np.testing.assert_array_almost_equal(test, data['r'], decimal=10)

        test = phot.sum_magnitudes(bulge=data['ib'],
                                   disk=data['id'],
                                   agn=data['ia'])
        np.testing.assert_array_almost_equal(test, data['i'], decimal=10)

        test = phot.sum_magnitudes(bulge=data['zb'],
                                   disk=data['zd'],
                                   agn=data['za'])
        np.testing.assert_array_almost_equal(test, data['z'], decimal=10)

        test = phot.sum_magnitudes(bulge=data['yb'],
                                   disk=data['yd'],
                                   agn=data['ya'])
        np.testing.assert_array_almost_equal(test, data['y'], decimal=10)

        # make sure that there were some NaNs for our catalog to deal with (but that they were not
        # all NaNs
        for line in [
                data['u'], data['g'], data['r'], data['i'], data['z'],
                data['y'], data['ub'], data['gb'], data['rb'], data['ib'],
                data['zb'], data['yb'], data['ud'], data['gd'], data['rd'],
                data['id'], data['zd'], data['yd'], data['ua'], data['ga'],
                data['ra'], data['ia'], data['za'], data['ya']
        ]:

            ctNans = len(np.where(np.isnan(line))[0])
            self.assertGreater(ctNans, 0)
            self.assertLess(ctNans, len(line))
    def testGalaxyPhotometryStandAlone(self):
        objectID = ['Alice', 'Bob', 'Charlie']

        diskSeds = ['Const.80E07.02Z.spec','Inst.80E07.002Z.spec','Burst.19E07.0005Z.spec']
        diskMagNorm = [24.2, 28.1, 29.0]
        diskAv = [3.1, 3.2, 2.9]

        bulgeSeds = ['Inst.80E07.002Z.spec','Const.80E07.02Z.spec','Burst.19E07.0005Z.spec']
        bulgeMagNorm = [25.0, 28.0, 27.1]
        bulgeAv = [2.8, 3.2, 3.3]

        agnSeds = ['agn.spec', 'agn.spec', 'agn.spec']
        agnMagNorm = [22.0, 23.0, 26.0]

        redshift = [0.2, 0.3, 1.1]
        cosmologicalDistanceModulus = [5.0, 3.0, 4.5]

        diskSedsDummy = ['Inst.80E07.002Z.spec','Burst.19E07.0005Z.spec']
        diskMagNormDummy = [28.1, 29.0]
        diskAvDummy = [3.2, 2.9]

        bulgeSedsDummy = ['Const.80E07.02Z.spec','Burst.19E07.0005Z.spec']
        bulgeMagNormDummy = [28.0, 27.1]
        bulgeAvDummy = [3.2, 3.3]

        agnSeds = ['agn.spec', 'agn.spec', 'agn.spec']
        agnMagNorm = [22.0, 23.0, 26.0]

        agnSedsDummy = ['agn.spec', 'agn.spec']
        agnMagNormDummy = [22.0, 26.0]

        redshiftDummy = [0.3, 1.1]
        cosmologicalDistanceModulusDummy = [3.0, 4.5]

        phot = PhotometryGalaxies()
        phot.loadTotalBandpassesFromFiles(bandpassNames=['u','g','r','i','z','y'])

        self.assertRaises(RuntimeError, phot.calculate_magnitudes, objectID,
                          diskNames=diskSedsDummy, diskMagNorm=diskMagNorm, diskAv=diskAv,
                          redshift=redshift)

        self.assertRaises(RuntimeError, phot.calculate_magnitudes, objectID,
                          diskNames=diskSeds, diskMagNorm=diskMagNormDummy, diskAv=diskAvDummy,
                          redshift=redshift)

        self.assertRaises(RuntimeError, phot.calculate_magnitudes, objectID,
                          diskNames=diskSeds, diskMagNorm=diskMagNorm, diskAv=diskAvDummy,
                          redshift=redshift)

        self.assertRaises(RuntimeError, phot.calculate_magnitudes, objectID,
                          bulgeNames=bulgeSedsDummy, bulgeMagNorm=bulgeMagNorm, bulgeAv=bulgeAv,
                          redshift=redshift)

        self.assertRaises(RuntimeError, phot.calculate_magnitudes, objectID,
                          bulgeNames=bulgeSeds, bulgeMagNorm=bulgeMagNormDummy, bulgeAv=bulgeAv,
                          redshift=redshift)

        self.assertRaises(RuntimeError, phot.calculate_magnitudes, objectID,
                          bulgeNames=bulgeSeds, bulgeMagNorm=bulgeMagNorm, bulgeAv=bulgeAvDummy,
                          redshift=redshift)

        self.assertRaises(RuntimeError, phot.calculate_magnitudes, objectID,
                          agnNames=agnSedsDummy, agnMagNorm=agnMagNorm)

        self.assertRaises(RuntimeError, phot.calculate_magnitudes, objectID,
                          agnNames=agnSeds, agnMagNorm=agnMagNormDummy)

        self.assertRaises(RuntimeError, phot.calculate_magnitudes, objectID,
                          bulgeNames=bulgeSeds, bulgeMagNorm=bulgeMagNorm, bulgeAv=bulgeAv,
                          redshift=redshiftDummy)

        self.assertRaises(RuntimeError, phot.calculate_magnitudes, objectID,
                          bulgeNames=bulgeSeds, bulgeMagNorm=bulgeMagNorm, bulgeAv=bulgeAv,
                          redshift=redshift, cosmologicalDistanceModulus=cosmologicalDistanceModulusDummy)

        self.assertRaises(RuntimeError, phot.calculate_magnitudes, objectID,
                          bulgeNames=bulgeSeds, bulgeMagNorm=bulgeMagNorm, redshift=redshift)
        self.assertRaises(RuntimeError, phot.calculate_magnitudes, objectID,
                          bulgeNames=bulgeSeds, bulgeAv=bulgeAv, redshift=redshift)

        self.assertRaises(RuntimeError, phot.calculate_magnitudes, objectID,
                          diskNames=diskSeds, diskMagNorm=diskMagNorm, redshift=redshift)
        self.assertRaises(RuntimeError, phot.calculate_magnitudes, objectID,
                          diskNames=diskSeds, diskAv=diskAv, redshift=redshift)

        self.assertRaises(RuntimeError, phot.calculate_magnitudes, objectID,
                          agnNames=agnSeds, redshift=redshift)

        self.assertRaises(RuntimeError, phot.calculate_magnitudes, objectID,
                          bulgeNames=bulgeSeds, bulgeMagNorm=bulgeMagNorm, bulgeAv=bulgeAv)
        self.assertRaises(RuntimeError, phot.calculate_magnitudes, objectID,
                          diskNames=diskSeds, diskMagNorm=diskMagNorm, diskAv=diskAv)
        self.assertRaises(RuntimeError, phot.calculate_magnitudes, objectID,
                          agnNames=agnSeds, agnMagNorm=agnMagNorm)

        bulgeNamePossibilities = [bulgeSeds, None]
        diskNamePossibilities = [diskSeds, None]
        agnNamePossibilities = [agnSeds, None]

        for bulgeNames in bulgeNamePossibilities:
            for diskNames in diskNamePossibilities:
                for agnNames in agnNamePossibilities:

                    magnitudes = phot.calculate_magnitudes(objectID, redshift=redshift,
                                                           bulgeNames=bulgeNames, bulgeMagNorm=bulgeMagNorm, bulgeAv=bulgeAv,
                                                           diskNames=diskNames, diskMagNorm=diskMagNorm, diskAv=diskAv,
                                                           agnNames=agnNames, agnMagNorm=agnMagNorm)

                    for name in objectID:
                        for i in range(len(phot.bandpassDict)):
                            flux=0.0
                            if bulgeNames is None:
                                self.assertTrue(numpy.isnan(magnitudes[name]['bulge'][i]))
                            else:
                                self.assertTrue(magnitudes[name]['bulge'][i] is not None)
                                self.assertFalse(numpy.isnan(magnitudes[name]['bulge'][i]))
                                flux += numpy.power(10.0, -0.4*(magnitudes[name]['bulge'][i]-22.0))

                            if diskNames is None:
                                self.assertTrue(numpy.isnan(magnitudes[name]['disk'][i]))
                            else:
                                self.assertTrue(magnitudes[name]['disk'][i] is not None)
                                self.assertFalse(numpy.isnan(magnitudes[name]['disk'][i]))
                                flux += numpy.power(10.0, -0.4*(magnitudes[name]['disk'][i]-22.0))

                            if agnNames is None:
                                self.assertTrue(numpy.isnan(magnitudes[name]['agn'][i]))
                            else:
                                self.assertTrue(magnitudes[name]['agn'][i] is not None)
                                self.assertFalse(numpy.isnan(magnitudes[name]['agn'][i]))
                                flux += numpy.power(10.0, -0.4*(magnitudes[name]['agn'][i]-22.0))
    def testSumMagnitudesCatalog(self):
        """
        test that sum_magnitudes handles NaNs correctly in the context
        of a catalog by outputting a catalog of galaxies with NaNs in
        different component magnitudes, reading that catalog back in,
        and then calculating the summed magnitude by hand and comparing
        """

        catName = 'galaxiesWithHoles.txt'
        obs_metadata=ObservationMetaData(mjd=50000.0,
                               boundType='circle',unrefractedRA=0.0,unrefractedDec=0.0,
                               boundLength=10.0)
        test_cat=galaxiesWithHoles(self.galaxy,obs_metadata=obs_metadata)
        test_cat.write_catalog(catName)

        dtype = numpy.dtype([
                            ('raJ2000', numpy.float),
                            ('decJ2000', numpy.float),
                            ('u', numpy.float), ('g', numpy.float), ('r', numpy.float),
                            ('i', numpy.float), ('z', numpy.float), ('y', numpy.float),
                            ('ub', numpy.float), ('gb', numpy.float), ('rb', numpy.float),
                            ('ib', numpy.float), ('zb', numpy.float), ('yb', numpy.float),
                            ('ud', numpy.float), ('gd', numpy.float), ('rd', numpy.float),
                            ('id', numpy.float), ('zd', numpy.float), ('yd', numpy.float),
                            ('ua', numpy.float), ('ga', numpy.float), ('ra', numpy.float),
                            ('ia', numpy.float), ('za', numpy.float), ('ya', numpy.float)
                            ])



        data = numpy.genfromtxt(catName, dtype=dtype, delimiter=', ')
        self.assertTrue(len(data)>16)
        phot = PhotometryGalaxies()

        test = phot.sum_magnitudes(bulge=data['ub'], disk=data['ud'], agn=data['ua'])
        numpy.testing.assert_array_almost_equal(test, data['u'], decimal=10)

        test = phot.sum_magnitudes(bulge=data['gb'], disk=data['gd'], agn=data['ga'])
        numpy.testing.assert_array_almost_equal(test, data['g'], decimal=10)

        test = phot.sum_magnitudes(bulge=data['rb'], disk=data['rd'], agn=data['ra'])
        numpy.testing.assert_array_almost_equal(test, data['r'], decimal=10)

        test = phot.sum_magnitudes(bulge=data['ib'], disk=data['id'], agn=data['ia'])
        numpy.testing.assert_array_almost_equal(test, data['i'], decimal=10)

        test = phot.sum_magnitudes(bulge=data['zb'], disk=data['zd'], agn=data['za'])
        numpy.testing.assert_array_almost_equal(test, data['z'], decimal=10)

        test = phot.sum_magnitudes(bulge=data['yb'], disk=data['yd'], agn=data['ya'])
        numpy.testing.assert_array_almost_equal(test, data['y'], decimal=10)

        # make sure that there were some NaNs for our catalog to deal with (but that they were not
        # all NaNs
        for line in [data['u'], data['g'], data['r'], data['i'], data['z'], data['y'],
                     data['ub'], data['gb'], data['rb'], data['ib'], data['zb'], data['yb'],
                     data['ud'], data['gd'], data['rd'], data['id'], data['zd'], data['yd'],
                     data['ua'], data['ga'], data['ra'], data['ia'], data['za'], data['ya']]:

            ctNans = len(numpy.where(numpy.isnan(line))[0])
            self.assertTrue(ctNans>0)
            self.assertTrue(ctNans<len(line))

        if os.path.exists(catName):
            os.unlink(catName)
    def testGalaxyPhotometricUncertainties(self):
        """
        Test in the case of a catalog of galaxies
        """
        lsstDefaults = LSSTdefaults()
        phot = PhotometryGalaxies()
        phot.loadTotalBandpassesFromFiles()
        galDB = testGalaxyTileDBObj(driver=self.driver, host=self.host, database=self.dbName)
        galCat = testGalaxyCatalog(galDB, obs_metadata=self.obs_metadata)
        imsimband = Bandpass()
        imsimband.imsimBandpass()
        ct = 0
        for line in galCat.iter_catalog():
            bulgeSedName = line[50]
            diskSedName = line[51]
            agnSedName = line[52]
            magNormBulge = line[53]
            magNormDisk = line[54]
            magNormAgn = line[55]
            avBulge = line[56]
            avDisk = line[57]
            redshift = line[58]

            bulgeSed = Sed()
            bulgeSed.readSED_flambda(os.path.join(lsst.utils.getPackageDir('sims_sed_library'),
                                     defaultSpecMap[bulgeSedName]))
            fNorm=bulgeSed.calcFluxNorm(magNormBulge, imsimband)
            bulgeSed.multiplyFluxNorm(fNorm)

            diskSed = Sed()
            diskSed.readSED_flambda(os.path.join(lsst.utils.getPackageDir('sims_sed_library'),
                                    defaultSpecMap[diskSedName]))
            fNorm = diskSed.calcFluxNorm(magNormDisk, imsimband)
            diskSed.multiplyFluxNorm(fNorm)

            agnSed = Sed()
            agnSed.readSED_flambda(os.path.join(lsst.utils.getPackageDir('sims_sed_library'),
                                   defaultSpecMap[agnSedName]))
            fNorm = agnSed.calcFluxNorm(magNormAgn, imsimband)
            agnSed.multiplyFluxNorm(fNorm)

            phot.applyAv([bulgeSed, diskSed], [avBulge, avDisk])
            phot.applyRedshift([bulgeSed, diskSed, agnSed], [redshift, redshift, redshift])

            numpy.testing.assert_almost_equal(bulgeSed.wavelen, diskSed.wavelen)
            numpy.testing.assert_almost_equal(bulgeSed.wavelen, agnSed.wavelen)

            fl = bulgeSed.flambda + diskSed.flambda + agnSed.flambda

            totalSed = Sed(wavelen=bulgeSed.wavelen, flambda=fl)

            sedList = [totalSed, bulgeSed, diskSed, agnSed]

            for i, spectrum in enumerate(sedList):
                if i==0:
                    msgroot = 'failed on total'
                elif i==1:
                    msgroot = 'failed on bulge'
                elif i==2:
                    msgroot = 'failed on disk'
                elif i==3:
                    msgroot = 'failed on agn'

                for j, b in enumerate(self.bandpasses):
                    controlSigma = calcMagError_sed(spectrum, self.totalBandpasses[j],
                                             self.skySeds[j],
                                             self.hardwareBandpasses[j],
                                             seeing=lsstDefaults.seeing(b),
                                             photParams=PhotometricParameters())

                    testSigma = line[26+(i*6)+j]
                    msg = '%e neq %e; ' % (testSigma, controlSigma) + msgroot
                    self.assertAlmostEqual(testSigma, controlSigma, 10, msg=msg)
                    ct += 1

        self.assertTrue(ct>0)