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)
    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)
Exemple #3
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 #4
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))