def testDefaultCustomCompoundCatalogDBObject(self):
        """
        Test that CompoundInstanceCatalog can properly parse multiple CompoundCatalogDBobjects
        """
        fileName = os.path.join(self.baseDir, 'simplest_compound_catalog.txt')

        # negativeDecComopound_table2 should not come into play, since the
        # multiple queries are directed at table1
        compoundCat = CompoundInstanceCatalog(
            [Cat1, Cat2, Cat3], [table1DB1, table1DB2, table2DB1],
            compoundDBclass=[negativeDecCompound_table2, negativeRaCompound])

        compoundCat.write_catalog(fileName)

        self.assertEqual(len(compoundCat._dbObjectGroupList), 2)
        self.assertEqual(len(compoundCat._dbObjectGroupList[0]), 2)
        self.assertEqual(len(compoundCat._dbObjectGroupList[1]), 1)
        self.assertIn(0, compoundCat._dbObjectGroupList[0])
        self.assertIn(1, compoundCat._dbObjectGroupList[0])
        self.assertIn(2, compoundCat._dbObjectGroupList[1])

        dtype = np.dtype([('id', np.int), ('raObs', np.float),
                          ('decObs', np.float), ('final_mag', np.float)])

        testData = np.genfromtxt(fileName, dtype=dtype)

        for line in testData:
            if line[0] < 2000:
                ix = line[0] - 1000
                self.assertAlmostEqual(line[1],
                                       -1.0 * self.table1Control['ra'][ix], 6)
                self.assertAlmostEqual(line[2], self.table1Control['dec'][ix],
                                       6)
                self.assertAlmostEqual(
                    self.table1Control['mag'][ix] +
                    self.table1Control['dmag'][ix], line[3], 6)
            elif line[0] < 3000:
                ix = line[0] - 2000
                self.assertAlmostEqual(
                    -2.0 * self.table1Control['ra'][ix] +
                    self.table1Control['dra'][ix], line[1], 6)
                self.assertAlmostEqual(
                    2.0 * self.table1Control['dec'][ix] +
                    self.table1Control['ddec'][ix], line[2], 6)
                self.assertAlmostEqual(
                    self.table1Control['mag'][ix] +
                    self.table1Control['dmag'][ix], line[3], 6)
            else:
                ix = line[0] - 3000
                self.assertAlmostEqual(line[1], self.table2Control['ra'][ix],
                                       6)
                self.assertAlmostEqual(line[2], self.table2Control['dec'][ix],
                                       6)
                self.assertAlmostEqual(line[3], self.table2Control['mag'][ix],
                                       6)

        if os.path.exists(fileName):
            os.unlink(fileName)
    def testCustomCompoundCatalogDBObject(self):
        """
        Test that CompoundInstanceCatalog behaves properly when passed a
        custom CompoundCatalogDBObject
        """
        fileName = os.path.join(self.baseDir, 'simplest_compound_catalog.txt')

        compoundCat = CompoundInstanceCatalog(
            [Cat1, Cat2, Cat3], [table1DB1, table1DB2, table2DB1],
            compoundDBclass=negativeRaCompound)

        compoundCat.write_catalog(fileName)

        self.assertEqual(len(compoundCat._dbObjectGroupList), 2)
        self.assertEqual(len(compoundCat._dbObjectGroupList[0]), 2)
        self.assertEqual(len(compoundCat._dbObjectGroupList[1]), 1)
        self.assertIn(0, compoundCat._dbObjectGroupList[0])
        self.assertIn(1, compoundCat._dbObjectGroupList[0])
        self.assertIn(2, compoundCat._dbObjectGroupList[1])

        dtype = np.dtype([('id', np.int), ('raObs', np.float),
                          ('decObs', np.float), ('final_mag', np.float)])

        testData = np.genfromtxt(fileName, dtype=dtype)

        for line in testData:
            if line[0] < 2000:
                ix = line[0] - 1000
                self.assertAlmostEqual(line[1],
                                       -1.0 * self.table1Control['ra'][ix], 6)
                self.assertAlmostEqual(line[2], self.table1Control['dec'][ix],
                                       6)
                self.assertAlmostEqual(
                    self.table1Control['mag'][ix] +
                    self.table1Control['dmag'][ix], line[3], 6)
            elif line[0] < 3000:
                ix = line[0] - 2000
                self.assertAlmostEqual(
                    -2.0 * self.table1Control['ra'][ix] +
                    self.table1Control['dra'][ix], line[1], 6)
                self.assertAlmostEqual(
                    2.0 * self.table1Control['dec'][ix] +
                    self.table1Control['ddec'][ix], line[2], 6)
                self.assertAlmostEqual(
                    self.table1Control['mag'][ix] +
                    self.table1Control['dmag'][ix], line[3], 6)
            else:
                ix = line[0] - 3000
                self.assertAlmostEqual(line[1], self.table2Control['ra'][ix],
                                       6)
                self.assertAlmostEqual(line[2], self.table2Control['dec'][ix],
                                       6)
                self.assertAlmostEqual(line[3], self.table2Control['mag'][ix],
                                       6)

        if os.path.exists(fileName):
            os.unlink(fileName)
    def testCustomCompoundCatalogDBObjectList(self):
        """
        Test that CompoundInstanceCatalog behaves properly when there are
        two sets of multiple queries, one to table1, one to table2
        """
        fileName = os.path.join(self.scratch_dir, 'simplest_compound_catalog.txt')

        compoundCat = CompoundInstanceCatalog([Cat1, Cat2, Cat3, Cat4],
                                              [table1DB1, table1DB2, table2DB1, table2DB2],
                                              compoundDBclass=[negativeRaCompound,
                                                               negativeDecCompound_table2])

        compoundCat.write_catalog(fileName)

        self.assertEqual(len(compoundCat._dbObjectGroupList), 2)
        self.assertEqual(len(compoundCat._dbObjectGroupList[0]), 2)
        self.assertEqual(len(compoundCat._dbObjectGroupList[1]), 2)
        self.assertIn(0, compoundCat._dbObjectGroupList[0])
        self.assertIn(1, compoundCat._dbObjectGroupList[0])
        self.assertIn(2, compoundCat._dbObjectGroupList[1])
        self.assertIn(3, compoundCat._dbObjectGroupList[1])

        dtype = np.dtype([('id', np.int),
                          ('raObs', np.float),
                          ('decObs', np.float),
                          ('final_mag', np.float)])

        testData = np.genfromtxt(fileName, dtype=dtype)

        for line in testData:
            if line[0] < 2000:
                ix = line[0]-1000
                self.assertAlmostEqual(line[1], -1.0*self.table1Control['ra'][ix], 6)
                self.assertAlmostEqual(line[2], self.table1Control['dec'][ix], 6)
                self.assertAlmostEqual(self.table1Control['mag'][ix]+self.table1Control['dmag'][ix],
                                       line[3], 6)
            elif line[0] < 3000:
                ix = line[0] - 2000
                self.assertAlmostEqual(-2.0*self.table1Control['ra'][ix]+self.table1Control['dra'][ix],
                                       line[1], 6)
                self.assertAlmostEqual(2.0*self.table1Control['dec'][ix]+self.table1Control['ddec'][ix],
                                       line[2], 6)
                self.assertAlmostEqual(self.table1Control['mag'][ix]+self.table1Control['dmag'][ix],
                                       line[3], 6)
            elif line[0] < 4000:
                ix = line[0] - 3000
                self.assertAlmostEqual(line[1], self.table2Control['ra'][ix], 6)
                self.assertAlmostEqual(line[2], -1.0*self.table2Control['dec'][ix], 6)
                self.assertAlmostEqual(line[3], self.table2Control['mag'][ix], 6)
            else:
                ix = line[0] - 4000
                self.assertAlmostEqual(line[1], 2.0*self.table2Control['ra'][ix], 6)
                self.assertAlmostEqual(line[2], -2.0*self.table2Control['dec'][ix], 6)
                self.assertAlmostEqual(line[3], self.table2Control['mag'][ix], 6)

        if os.path.exists(fileName):
            os.unlink(fileName)
Пример #4
0
    def writePhoSimCatalog(self, fileName):
        """
        write the phosim instance catalogs of stars, galaxies and supernovae in
        the Twinkles Sky to fileName

        Parameters
        ----------
        fileName : string, mandatory
            Name of the file to which the phoSim Instance Catalog will be
            written

        """
        starCat = CompoundInstanceCatalog(self.compoundStarICList,
                                          self.compoundStarDBList,
                                          obs_metadata=self.obs_metadata,
                                          constraint=self.brightestStarMag)

        starCat._active_connections += self.availableConnections  # append already open fatboy connections
        starCat.phoSimHeaderMap = TwinklesPhoSimHeader

        t_before_starCat = time.time()
        print("writing starCat ")
        starCat.write_catalog(fileName, chunk_size=10000)
        t_after_starCat = time.time()

        galCat = CompoundInstanceCatalog(
            self.compoundGalICList,
            self.compoundGalDBList,
            obs_metadata=self.obs_metadata,
            constraint=self.brightestGalMag,
            compoundDBclass=GalaxyCacheSprinklerObj)

        galCat._active_connections = starCat._active_connections  # pass along already open fatboy connections
        t_before_galCat = time.time()
        print("writing galCat")
        galCat.write_catalog(fileName,
                             write_mode='a',
                             chunk_size=10000,
                             write_header=False)

        t_after_galCat = time.time()
        snphosim = TwinklesCatalogSN(db_obj=self.snObj,
                                     obs_metadata=self.obs_metadata)
        ### Set properties
        snphosim.writeSedFile = True
        snphosim.suppressDimSN = True
        snphosim.sn_sedfile_prefix = self.sn_sedfile_prefix
        print("writing sne")
        t_before_snCat = time.time()
        snphosim.write_catalog(fileName,
                               write_header=False,
                               write_mode='a',
                               chunk_size=10000)
        t_after_snCat = time.time()
    def testDefaultCustomCompoundCatalogDBObject(self):
        """
        Test that CompoundInstanceCatalog can properly parse multiple CompoundCatalogDBobjects
        """
        fileName = os.path.join(self.scratch_dir, 'simplest_compound_catalog.txt')

        # negativeDecComopound_table2 should not come into play, since the
        # multiple queries are directed at table1
        compoundCat = CompoundInstanceCatalog([Cat1, Cat2, Cat3],
                                              [table1DB1, table1DB2, table2DB1],
                                              compoundDBclass=[negativeDecCompound_table2,
                                                               negativeRaCompound])

        compoundCat.write_catalog(fileName)

        self.assertEqual(len(compoundCat._dbObjectGroupList), 2)
        self.assertEqual(len(compoundCat._dbObjectGroupList[0]), 2)
        self.assertEqual(len(compoundCat._dbObjectGroupList[1]), 1)
        self.assertIn(0, compoundCat._dbObjectGroupList[0])
        self.assertIn(1, compoundCat._dbObjectGroupList[0])
        self.assertIn(2, compoundCat._dbObjectGroupList[1])

        dtype = np.dtype([('id', np.int),
                          ('raObs', np.float),
                          ('decObs', np.float),
                          ('final_mag', np.float)])

        testData = np.genfromtxt(fileName, dtype=dtype)

        for line in testData:
            if line[0] < 2000:
                ix = line[0]-1000
                self.assertAlmostEqual(line[1], -1.0*self.table1Control['ra'][ix], 6)
                self.assertAlmostEqual(line[2], self.table1Control['dec'][ix], 6)
                self.assertAlmostEqual(self.table1Control['mag'][ix]+self.table1Control['dmag'][ix],
                                       line[3], 6)
            elif line[0] < 3000:
                ix = line[0]-2000
                self.assertAlmostEqual(-2.0*self.table1Control['ra'][ix]+self.table1Control['dra'][ix],
                                       line[1], 6)
                self.assertAlmostEqual(2.0*self.table1Control['dec'][ix]+self.table1Control['ddec'][ix],
                                       line[2], 6)
                self.assertAlmostEqual(self.table1Control['mag'][ix]+self.table1Control['dmag'][ix],
                                       line[3], 6)
            else:
                ix = line[0]-3000
                self.assertAlmostEqual(line[1], self.table2Control['ra'][ix], 6)
                self.assertAlmostEqual(line[2], self.table2Control['dec'][ix], 6)
                self.assertAlmostEqual(line[3], self.table2Control['mag'][ix], 6)

        if os.path.exists(fileName):
            os.unlink(fileName)
Пример #6
0
    def testGalaxyAndStarCatalog(self):
        """
        Test GalaxyTileCompoundObj by creating a catalog of galaxy bulges, disks,
        agns, and stars using both the 'old fashioned way' (one catalog at a time), and
        using CompoundInstanceCatalog
        """
        controlFileName = os.path.join(self.baseDir, 'galStar_compound_control.txt')
        testFileName = os.path.join(self.baseDir, 'galStar_compound_test.txt')

        if os.path.exists(controlFileName):
            os.unlink(controlFileName)
        if os.path.exists(testFileName):
            os.unlink(testFileName)

        obs = ObservationMetaData(pointingRA=25.0, pointingDec=-45.0,
                                  boundType='circle', boundLength=0.05)

        dbBulge = GalaxyBulgeObj()
        dbDisk = GalaxyDiskObj()
        dbAgn = GalaxyAgnObj()
        dbStar = StarObj()

        catBulge = BulgeDiskCatalog(dbBulge, obs_metadata=obs)
        catDisk = BulgeDiskCatalog(dbDisk, obs_metadata=obs)
        catAgn = AgnCatalog(dbAgn, obs_metadata=obs)
        catStar = StarCatalog(dbStar, obs_metadata=obs)

        catBulge.write_catalog(controlFileName, write_header=False, chunk_size=10000)
        catDisk.write_catalog(controlFileName, write_mode='a', write_header=False, chunk_size=10000)
        catAgn.write_catalog(controlFileName, write_mode='a', write_header=False, chunk_size=10000)
        catStar.write_catalog(controlFileName, write_mode='a', write_header=False, chunk_size=10000)

        totalCat = CompoundInstanceCatalog([BulgeDiskCatalog, BulgeDiskCatalog, StarCatalog, AgnCatalog],
                                           [GalaxyBulgeObj, GalaxyDiskObj, StarObj, GalaxyAgnObj],
                                           obs_metadata=obs,
                                           compoundDBclass=GalaxyTileCompoundObj)

        totalCat.write_catalog(testFileName, write_header=False, chunk_size=10000)

        with open(controlFileName, 'r') as controlFile:
            control = controlFile.readlines()

        with open(testFileName, 'r') as testFile:
            test = testFile.readlines()

        for line in control:
            self.assertIn(line, test)

        for line in test:
            self.assertIn(line, control)
Пример #7
0
    def testGalaxyAndStarCatalog(self):
        """
        Test GalaxyTileCompoundObj by creating a catalog of galaxy bulges, disks,
        agns, and stars using both the 'old fashioned way' (one catalog at a time), and
        using CompoundInstanceCatalog
        """
        controlFileName = os.path.join(self.baseDir, 'galStar_compound_control.txt')
        testFileName = os.path.join(self.baseDir, 'galStar_compound_test.txt')

        if os.path.exists(controlFileName):
            os.unlink(controlFileName)
        if os.path.exists(testFileName):
            os.unlink(testFileName)

        obs = ObservationMetaData(pointingRA=25.0, pointingDec=-45.0,
                                  boundType='circle', boundLength=0.05)

        dbBulge = GalaxyBulgeObj()
        dbDisk = GalaxyDiskObj()
        dbAgn = GalaxyAgnObj()
        dbStar = StarObj()

        catBulge = BulgeDiskCatalog(dbBulge, obs_metadata=obs)
        catDisk = BulgeDiskCatalog(dbDisk, obs_metadata=obs)
        catAgn = AgnCatalog(dbAgn, obs_metadata=obs)
        catStar = StarCatalog(dbStar, obs_metadata=obs)

        catBulge.write_catalog(controlFileName, write_header=False, chunk_size=10000)
        catDisk.write_catalog(controlFileName, write_mode='a', write_header=False, chunk_size=10000)
        catAgn.write_catalog(controlFileName, write_mode='a', write_header=False, chunk_size=10000)
        catStar.write_catalog(controlFileName, write_mode='a', write_header=False, chunk_size=10000)

        totalCat = CompoundInstanceCatalog([BulgeDiskCatalog, BulgeDiskCatalog, StarCatalog, AgnCatalog],
                                           [GalaxyBulgeObj, GalaxyDiskObj, StarObj, GalaxyAgnObj],
                                           obs_metadata=obs,
                                           compoundDBclass=GalaxyTileCompoundObj)

        totalCat.write_catalog(testFileName, write_header=False, chunk_size=10000)

        with open(controlFileName, 'r') as controlFile:
            control = controlFile.readlines()

        with open(testFileName, 'r') as testFile:
            test = testFile.readlines()

        for line in control:
            self.assertIn(line, test)

        for line in test:
            self.assertIn(line, control)
    def testCompoundCatalog(self):
        """
        Test that a CompoundInstanceCatalog produces the expected output
        """
        fileName = os.path.join(self.scratch_dir, 'simplest_compound_catalog.txt')

        compoundCat = CompoundInstanceCatalog([Cat1, Cat2, Cat3], [table1DB1, table1DB2, table2DB1])

        compoundCat.write_catalog(fileName)

        self.assertEqual(len(compoundCat._dbObjectGroupList), 2)
        self.assertEqual(len(compoundCat._dbObjectGroupList[0]), 2)
        self.assertEqual(len(compoundCat._dbObjectGroupList[1]), 1)
        self.assertIn(0, compoundCat._dbObjectGroupList[0])
        self.assertIn(1, compoundCat._dbObjectGroupList[0])
        self.assertIn(2, compoundCat._dbObjectGroupList[1])

        dtype = np.dtype([('id', np.int),
                          ('raObs', np.float),
                          ('decObs', np.float),
                          ('final_mag', np.float)])

        testData = np.genfromtxt(fileName, dtype=dtype)

        for line in testData:
            if line[0] < 2000:
                ix = line[0]-1000
                self.assertAlmostEqual(line[1], self.table1Control['ra'][ix], 6)
                self.assertAlmostEqual(line[2], self.table1Control['dec'][ix], 6)
                self.assertAlmostEqual(self.table1Control['mag'][ix]+self.table1Control['dmag'][ix],
                                       line[3], 6)
            elif line[0] < 3000:
                ix = line[0] - 2000
                self.assertAlmostEqual(2.0*self.table1Control['ra'][ix]+self.table1Control['dra'][ix],
                                       line[1], 6)
                self.assertAlmostEqual(2.0*self.table1Control['dec'][ix]+self.table1Control['ddec'][ix],
                                       line[2], 6)
                self.assertAlmostEqual(self.table1Control['mag'][ix]+self.table1Control['dmag'][ix],
                                       line[3], 6)
            else:
                ix = line[0]-3000
                self.assertAlmostEqual(line[1], self.table2Control['ra'][ix], 6)
                self.assertAlmostEqual(line[2], self.table2Control['dec'][ix], 6)
                self.assertAlmostEqual(line[3], self.table2Control['mag'][ix], 6)

        if os.path.exists(fileName):
            os.unlink(fileName)
    def writePhoSimCatalog(self, fileName):
        """
        write the phosim instance catalogs of stars, galaxies and supernovae in
        the Twinkles Sky to fileName

        Parameters
        ----------
        fileName : string, mandatory
            Name of the file to which the phoSim Instance Catalog will be
            written

        """
        starCat = CompoundInstanceCatalog(self.compoundStarICList,
                                          self.compoundStarDBList,
                                          obs_metadata=self.obs_metadata,
                                          constraint=self.brightestStarMag)

        starCat._active_connections += self.availableConnections # append already open fatboy connections
        starCat.phoSimHeaderMap = TwinklesPhoSimHeader

        t_before_starCat = time.time()
        print("writing starCat ")
        starCat.write_catalog(fileName, chunk_size=10000)
        t_after_starCat = time.time()

        galCat = CompoundInstanceCatalog(self.compoundGalICList,
                                         self.compoundGalDBList,
                                         obs_metadata=self.obs_metadata,
                                         constraint=self.brightestGalMag,
                                         compoundDBclass=GalaxyCacheSprinklerObj)

        galCat._active_connections = starCat._active_connections  # pass along already open fatboy connections
        t_before_galCat = time.time()
        print("writing galCat")
        galCat.write_catalog(fileName, write_mode='a', chunk_size=10000,
                             write_header=False)

        t_after_galCat = time.time()
        snphosim = TwinklesCatalogSN(db_obj=self.snObj,
                                     obs_metadata=self.obs_metadata)
        ### Set properties
        snphosim.writeSedFile = True
        snphosim.suppressDimSN = True
        snphosim.sn_sedfile_prefix = self.sn_sedfile_prefix
        print("writing sne")
        t_before_snCat = time.time()
        snphosim.write_catalog(fileName, write_header=False,
                               write_mode='a', chunk_size=10000)
        t_after_snCat = time.time()
Пример #10
0
    def testCompoundCatalog(self):
        """
        This test writes a PhoSim input catalog and compares it, one line at a time
        to a previously written catalog that should be identical.

        This test uses CompoundInstanceCatalog
        """

        # first, generate the catalog without a CompoundInstanceCatalog
        single_catName = tempfile.mktemp(dir=ROOT,
                                         prefix='phoSimTestCatalog_single',
                                         suffix='.txt')

        testBulge = PhoSimCatalogSersic2D(self.bulgeDB,
                                          obs_metadata=self.obs_metadata)
        testDisk = PhoSimCatalogSersic2D(self.diskDB,
                                         obs_metadata=self.obs_metadata)
        testAgn = PhoSimCatalogZPoint(self.agnDB,
                                      obs_metadata=self.obs_metadata)
        testStar = PhoSimCatalogPoint(self.starDB,
                                      obs_metadata=self.obs_metadata)

        testBulge.phoSimHeaderMap = test_header_map
        testBulge.write_catalog(single_catName)
        testDisk.write_catalog(single_catName,
                               write_header=False,
                               write_mode='a')
        testAgn.write_catalog(single_catName,
                              write_header=False,
                              write_mode='a')
        testStar.write_catalog(single_catName,
                               write_header=False,
                               write_mode='a')

        # now, generate the catalog using CompoundInstanceCatalog
        #
        # because the CompoundCatalogDBObject requires that database
        # connection parameters be set in the input CatalogDBObject
        # daughter class definitions, we have to declare dummy
        # CatalogDBObject daughter classes below

        class dummyDBbase(object):
            driver = 'sqlite'
            database = 'PhoSimTestDatabase.db'

        dummyDBbase.database = self.tempDB

        class dummyBulgeDB(dummyDBbase, testGalaxyBulgeDBObj):
            objid = 'dummy_bulge'

        class dummyDiskDB(dummyDBbase, testGalaxyDiskDBObj):
            objid = 'dummy_disk'

        class dummyAgnDB(dummyDBbase, testGalaxyAgnDBObj):
            objid = 'dummy_agn'

        class dummyStarDB(dummyDBbase, testStarsDBObj):
            objid = 'dummy_stars'

        compoundCatalog = CompoundInstanceCatalog(
            [
                PhoSimCatalogSersic2D, PhoSimCatalogSersic2D,
                PhoSimCatalogZPoint, PhoSimCatalogPoint
            ], [dummyBulgeDB, dummyDiskDB, dummyAgnDB, dummyStarDB],
            obs_metadata=self.obs_metadata)

        self.assertEqual(len(compoundCatalog._dbObjectGroupList[0]), 3)

        compound_catName = tempfile.mktemp(dir=ROOT,
                                           prefix='phoSimTestCatalog_compound',
                                           suffix='.txt')

        compoundCatalog.phoSimHeaderMap = test_header_map
        compoundCatalog.write_catalog(compound_catName)

        # verify that the compound catalog is what we expect
        self.verify_catalog(compound_catName)

        # verify that the two catalogs are equivalent
        with open(single_catName, 'r') as single_file:
            with open(compound_catName, 'r') as compound_file:
                single_lines = single_file.readlines()
                compound_lines = compound_file.readlines()

                for line in single_lines:
                    self.assertIn(line, compound_lines)

                for line in compound_lines:
                    self.assertIn(line, single_lines)

        if os.path.exists(compound_catName):
            os.unlink(compound_catName)

        if os.path.exists(single_catName):
            os.unlink(single_catName)
Пример #11
0
    def testCompoundCatalog(self):
        """
        This test writes a PhoSim input catalog and compares it, one line at a time
        to a previously written catalog that should be identical.

        This test uses CompoundInstanceCatalog
        """

        # first, generate the catalog without a CompoundInstanceCatalog
        single_catName = tempfile.mktemp(dir=ROOT, prefix='phoSimTestCatalog_single',
                                         suffix='.txt')

        testBulge = PhoSimCatalogSersic2D(self.bulgeDB, obs_metadata = self.obs_metadata)
        testDisk = PhoSimCatalogSersic2D(self.diskDB, obs_metadata = self.obs_metadata)
        testAgn = PhoSimCatalogZPoint(self.agnDB, obs_metadata = self.obs_metadata)
        testStar = PhoSimCatalogPoint(self.starDB, obs_metadata = self.obs_metadata)

        testBulge.phoSimHeaderMap = test_header_map
        testBulge.write_catalog(single_catName)
        testDisk.write_catalog(single_catName, write_header=False, write_mode='a')
        testAgn.write_catalog(single_catName, write_header=False, write_mode='a')
        testStar.write_catalog(single_catName, write_header=False, write_mode='a')

        # now, generate the catalog using CompoundInstanceCatalog
        #
        # because the CompoundCatalogDBObject requires that database
        # connection parameters be set in the input CatalogDBObject
        # daughter class definitions, we have to declare dummy
        # CatalogDBObject daughter classes below

        class dummyDBbase(object):
            driver = 'sqlite'
            database = 'PhoSimTestDatabase.db'

        dummyDBbase.database = self.tempDB

        class dummyBulgeDB(dummyDBbase, testGalaxyBulgeDBObj):
            objid = 'dummy_bulge'

        class dummyDiskDB(dummyDBbase, testGalaxyDiskDBObj):
            objid = 'dummy_disk'

        class dummyAgnDB(dummyDBbase, testGalaxyAgnDBObj):
            objid = 'dummy_agn'

        class dummyStarDB(dummyDBbase, testStarsDBObj):
            objid = 'dummy_stars'

        compoundCatalog = CompoundInstanceCatalog([PhoSimCatalogSersic2D, PhoSimCatalogSersic2D,
                                                   PhoSimCatalogZPoint, PhoSimCatalogPoint],
                                                  [dummyBulgeDB, dummyDiskDB, dummyAgnDB, dummyStarDB],
                                                  obs_metadata=self.obs_metadata)

        self.assertEqual(len(compoundCatalog._dbObjectGroupList[0]), 3)

        compound_catName = tempfile.mktemp(dir=ROOT, prefix='phoSimTestCatalog_compound',
                                           suffix='.txt')

        compoundCatalog.phoSimHeaderMap = test_header_map
        compoundCatalog.write_catalog(compound_catName)

        # verify that the compound catalog is what we expect
        self.verify_catalog(compound_catName)

        # verify that the two catalogs are equivalent
        with open(single_catName, 'r') as single_file:
            with open(compound_catName, 'r') as compound_file:
                single_lines = single_file.readlines()
                compound_lines = compound_file.readlines()

                for line in single_lines:
                    self.assertIn(line, compound_lines)

                for line in compound_lines:
                    self.assertIn(line, single_lines)

        if os.path.exists(compound_catName):
            os.unlink(compound_catName)

        if os.path.exists(single_catName):
            os.unlink(single_catName)
    def test_compound_cat_compound_column(self):
        """
        Test filtering a CompoundInstanceCatalog on a compound column
        """
        class FilteringCatClass4(InstanceCatalog):
            column_outputs = ['id', 'a', 'b']
            cannot_be_null = ['a']

            @compound('a', 'b')
            def get_alphabet(self):
                a = self.column_by_name('ip1')
                a = a * a
                b = self.column_by_name('ip2')
                b = b * 0.25
                return np.array([np.where(a % 2 == 0, a, None), b])

        class FilteringCatClass5(InstanceCatalog):
            column_outputs = ['id', 'a', 'b', 'filter']
            cannot_be_null = ['b', 'filter']

            @compound('a', 'b')
            def get_alphabet(self):
                ii = self.column_by_name('ip1')
                return np.array([
                    self.column_by_name('ip2')**3,
                    np.where(ii % 2 == 1, ii, None)
                ])

            @cached
            def get_filter(self):
                ii = self.column_by_name('ip1')
                return np.where(ii % 3 != 0, ii, None)

        class DbClass(CatalogDBObject):
            host = None
            port = None
            database = self.db_name
            driver = 'sqlite'
            tableid = 'test'
            idColKey = 'id'

        class DbClass4(DbClass):
            objid = 'silliness4'

        class DbClass5(DbClass):
            objid = 'silliness5'

        cat_name = os.path.join(self.scratch_dir,
                                "compound_cat_compound_filter_cat.txt")
        if os.path.exists(cat_name):
            os.unlink(cat_name)

        cat = CompoundInstanceCatalog([FilteringCatClass4, FilteringCatClass5],
                                      [DbClass4, DbClass5])
        cat.write_catalog(cat_name)

        # now make sure that the catalog contains the expected data
        with open(cat_name, 'r') as input_file:
            input_lines = input_file.readlines()
        self.assertEqual(len(input_lines), 9)  # 8 data lines and a header

        first_cat_lines = [
            '1, 4, 0.75\n', '3, 16, 1.25\n', '5, 36, 1.75\n', '7, 64, 2.25\n',
            '9, 100, 2.75\n'
        ]

        second_cat_lines = ['0, 8, 1, 1\n', '4, 216, 5, 5\n', '6, 512, 7, 7\n']

        for i_line, line in enumerate(input_lines):
            if i_line is 0:
                continue
            elif i_line < 6:
                self.assertEqual(line, first_cat_lines[i_line - 1])
            else:
                self.assertEqual(line, second_cat_lines[i_line - 6])

        if os.path.exists(cat_name):
            os.unlink(cat_name)
    def test_compound_cat(self):
        """
        Test that a CompoundInstanceCatalog made up of InstanceCatalog classes that
        each filter on a different condition gives the correct outputs.
        """
        class FilteringCatClass1(InstanceCatalog):
            column_outputs = ['id', 'ip1t']
            cannot_be_null = ['ip1t']

            @cached
            def get_ip1t(self):
                base = self.column_by_name('ip1')
                output = []
                for bb in base:
                    if bb % 2 == 0:
                        output.append(bb)
                    else:
                        output.append(None)
                return np.array(output)

        class FilteringCatClass2(InstanceCatalog):
            column_outputs = ['id', 'ip2t']
            cannot_be_null = ['ip2t']

            @cached
            def get_ip2t(self):
                base = self.column_by_name('ip2')
                ii = self.column_by_name('id')
                return np.where(ii < 4, base, None)

        class FilteringCatClass3(InstanceCatalog):
            column_outputs = ['id', 'ip3t']
            cannot_be_null = ['ip3t']

            @cached
            def get_ip3t(self):
                base = self.column_by_name('ip3')
                ii = self.column_by_name('id')
                return np.where(ii > 5, base, None)

        class DbClass(CatalogDBObject):
            host = None
            port = None
            database = self.db_name
            driver = 'sqlite'
            tableid = 'test'
            objid = 'silliness'
            idColKey = 'id'

        class DbClass1(DbClass):
            objid = 'silliness1'

        class DbClass2(DbClass):
            objid = 'silliness2'

        class DbClass3(DbClass):
            objid = 'silliness3'

        cat = CompoundInstanceCatalog(
            [FilteringCatClass1, FilteringCatClass2, FilteringCatClass3],
            [DbClass1, DbClass2, DbClass3])

        cat_name = os.path.join(self.scratch_dir, "compound_filter_output.txt")
        if os.path.exists(cat_name):
            os.unlink(cat_name)

        cat.write_catalog(cat_name)

        with open(cat_name, 'r') as input_file:
            input_lines = input_file.readlines()

        self.assertEqual(len(input_lines), 14)

        # given that we know what the contents of each sub-catalog should be
        # and how they should be ordered, loop through the lines of the output
        # catalog, verifying that every line is where it ought to be
        for i_line, line in enumerate(input_lines):
            if i_line is 0:
                continue
            elif i_line < 6:
                ii = 2 * (i_line - 1) + 1
                self.assertEqual((ii + 1) % 2, 0)
                self.assertEqual(line, '%d, %d\n' % (ii, ii + 1))
            elif i_line < 10:
                ii = i_line - 6
                self.assertLess(ii, 4)
                self.assertEqual(line, '%d, %d\n' % (ii, ii + 2))
            else:
                ii = i_line - 10 + 6
                self.assertGreater(ii, 5)
                self.assertEqual(line, '%d, %d\n' % (ii, ii + 3))

        if os.path.exists(cat_name):
            os.unlink(cat_name)
    def testObservationMetaDataAndConstraint(self):
        """
        Test that CompoundInstanceCatalog handles ObservationMetaData
        and a constraint properly
        """
        fileName = os.path.join(self.baseDir,
                                'compound_obs_metadata_test_cat.txt')
        obs = ObservationMetaData(pointingRA=180.0,
                                  pointingDec=0.0,
                                  boundType='box',
                                  boundLength=(80.0, 25.0),
                                  mjd=53850.0)

        compoundCat = CompoundInstanceCatalog(
            [Cat1, Cat2, Cat3], [table1DB1, table1DB2, table2DB1],
            obs_metadata=obs,
            constraint='mag>20.0')

        compoundCat.write_catalog(fileName)
        dtype = np.dtype([('id', np.int), ('raObs', np.float),
                          ('decObs', np.float), ('final_mag', np.float)])

        testData = np.genfromtxt(fileName, dtype=dtype)

        table1_good_rows = []
        table2_good_rows = []
        for line in testData:
            if line[0] < 2000:
                ix = line[0] - 1000
                if ix not in table1_good_rows:
                    table1_good_rows.append(ix)
                self.assertAlmostEqual(line[1], self.table1Control['ra'][ix],
                                       6)
                self.assertAlmostEqual(line[2], self.table1Control['dec'][ix],
                                       6)
                self.assertAlmostEqual(
                    self.table1Control['mag'][ix] +
                    self.table1Control['dmag'][ix], line[3], 6)
                self.assertGreater(self.table1Control['ra'][ix], 100.0)
                self.assertLess(self.table1Control['ra'][ix], 260.0)
                self.assertGreater(self.table1Control['dec'][ix], -25.0)
                self.assertLess(self.table1Control['dec'][ix], 25.0)
                self.assertGreater(self.table1Control['mag'][ix], 20.0)
            elif line[0] < 3000:
                ix = line[0] - 2000
                if ix not in table1_good_rows:
                    table1_good_rows.append(ix)
                self.assertAlmostEqual(
                    2.0 * self.table1Control['ra'][ix] +
                    self.table1Control['dra'][ix], line[1], 6)
                self.assertAlmostEqual(
                    2.0 * self.table1Control['dec'][ix] +
                    self.table1Control['ddec'][ix], line[2], 6)
                self.assertAlmostEqual(
                    self.table1Control['mag'][ix] +
                    self.table1Control['dmag'][ix], line[3], 6)
                self.assertGreater(self.table1Control['ra'][ix], 100.0)
                self.assertLess(self.table1Control['ra'][ix], 260.0)
                self.assertGreater(self.table1Control['dec'][ix], -25.0)
                self.assertLess(self.table1Control['dec'][ix], 25.0)
                self.assertGreater(self.table1Control['mag'][ix], 20.0)
            else:
                ix = line[0] - 3000
                if ix not in table2_good_rows:
                    table2_good_rows.append(ix)
                self.assertAlmostEqual(line[1], self.table2Control['ra'][ix],
                                       6)
                self.assertAlmostEqual(line[2], self.table2Control['dec'][ix],
                                       6)
                self.assertAlmostEqual(line[3], self.table2Control['mag'][ix],
                                       6)
                self.assertGreater(self.table2Control['ra'][ix], 100.0)
                self.assertLess(self.table2Control['ra'][ix], 260.0)
                self.assertGreater(self.table2Control['dec'][ix], -25.0)
                self.assertLess(self.table2Control['dec'][ix], 25.0)
                self.assertGreater(self.table2Control['mag'][ix], 20.0)

        table1_bad_rows = [
            ii for ii in range(self.table1Control.shape[0])
            if ii not in table1_good_rows
        ]
        table2_bad_rows = [
            ii for ii in range(self.table2Control.shape[0])
            if ii not in table2_good_rows
        ]

        in_bounds = [
            rr > 100.0 and rr < 260.0 and dd > -25.0 and dd < 25.0
            and mm > 20.0
            for rr, dd, mm in zip(self.table1Control['ra'][table1_bad_rows],
                                  self.table1Control['dec'][table1_bad_rows],
                                  self.table1Control['mag'][table1_bad_rows])
        ]

        self.assertNotIn(True,
                         in_bounds,
                         msg='failed to assemble table1_bad_rows')

        in_bounds = [
            rr > 100.0 and rr < 260.0 and dd > -25.0 and dd < 25.0
            and mm > 20.0
            for rr, dd, mm in zip(self.table2Control['ra'][table2_bad_rows],
                                  self.table2Control['dec'][table2_bad_rows],
                                  self.table2Control['mag'][table2_bad_rows])
        ]

        self.assertNotIn(True,
                         in_bounds,
                         msg='failed to assemble table2_bad_rows')

        self.assertGreater(len(table1_good_rows), 0)
        self.assertGreater(len(table2_good_rows), 0)
        self.assertGreater(len(table1_bad_rows), 0)
        self.assertGreater(len(table2_bad_rows), 0)
        self.assertEqual(
            len(table1_good_rows) + len(table1_bad_rows),
            self.table1Control.shape[0])
        self.assertEqual(
            len(table2_good_rows) + len(table2_bad_rows),
            self.table2Control.shape[0])

        if os.path.exists(fileName):
            os.unlink(fileName)
Пример #15
0
    def test_compound_cat(self):
        """
        Test that a CompoundInstanceCatalog made up of InstanceCatalog classes that
        each filter on a different condition gives the correct outputs.
        """

        class FilteringCatClass1(InstanceCatalog):
            column_outputs = ['id', 'ip1t']
            cannot_be_null = ['ip1t']

            @cached
            def get_ip1t(self):
                base = self.column_by_name('ip1')
                output = []
                for bb in base:
                    if bb%2 == 0:
                        output.append(bb)
                    else:
                        output.append(None)
                return np.array(output)

        class FilteringCatClass2(InstanceCatalog):
            column_outputs = ['id', 'ip2t']
            cannot_be_null = ['ip2t']

            @cached
            def get_ip2t(self):
                base = self.column_by_name('ip2')
                ii = self.column_by_name('id')
                return np.where(ii < 4, base, None)

        class FilteringCatClass3(InstanceCatalog):
            column_outputs = ['id', 'ip3t']
            cannot_be_null = ['ip3t']

            @cached
            def get_ip3t(self):
                base = self.column_by_name('ip3')
                ii = self.column_by_name('id')
                return np.where(ii > 5, base, None)

        class DbClass(CatalogDBObject):
            host = None
            port = None
            database = self.db_name
            driver = 'sqlite'
            tableid = 'test'
            objid = 'silliness'
            idColKey = 'id'

        class DbClass1(DbClass):
            objid = 'silliness1'

        class DbClass2(DbClass):
            objid = 'silliness2'

        class DbClass3(DbClass):
            objid = 'silliness3'

        cat = CompoundInstanceCatalog([FilteringCatClass1,
                                       FilteringCatClass2,
                                       FilteringCatClass3],
                                      [DbClass1, DbClass2, DbClass3])

        cat_name = os.path.join(self.scratch_dir, "compound_filter_output.txt")
        if os.path.exists(cat_name):
            os.unlink(cat_name)

        cat.write_catalog(cat_name)

        with open(cat_name, 'r') as input_file:
            input_lines = input_file.readlines()

        self.assertEqual(len(input_lines), 14)

        # given that we know what the contents of each sub-catalog should be
        # and how they should be ordered, loop through the lines of the output
        # catalog, verifying that every line is where it ought to be
        for i_line, line in enumerate(input_lines):
            if i_line is 0:
                continue
            elif i_line < 6:
                ii = 2*(i_line-1) + 1
                self.assertEqual((ii+1) % 2, 0)
                self.assertEqual(line, '%d, %d\n' % (ii, ii+1))
            elif i_line < 10:
                ii = i_line - 6
                self.assertLess(ii, 4)
                self.assertEqual(line, '%d, %d\n' % (ii, ii+2))
            else:
                ii = i_line - 10 + 6
                self.assertGreater(ii, 5)
                self.assertEqual(line, '%d, %d\n' % (ii, ii+3))

        if os.path.exists(cat_name):
            os.unlink(cat_name)
    def testObservationMetaDataAndConstraint(self):
        """
        Test that CompoundInstanceCatalog handles ObservationMetaData
        and a constraint properly
        """
        fileName = os.path.join(self.scratch_dir, 'compound_obs_metadata_test_cat.txt')
        obs = ObservationMetaData(pointingRA = 180.0,
                                  pointingDec = 0.0,
                                  boundType = 'box',
                                  boundLength = (80.0, 25.0),
                                  mjd=53850.0)

        compoundCat = CompoundInstanceCatalog([Cat1, Cat2, Cat3], [table1DB1, table1DB2, table2DB1],
                                              obs_metadata=obs,
                                              constraint='mag>20.0')

        compoundCat.write_catalog(fileName)
        dtype = np.dtype([('id', np.int),
                          ('raObs', np.float),
                          ('decObs', np.float),
                          ('final_mag', np.float)])

        testData = np.genfromtxt(fileName, dtype=dtype)

        table1_good_rows = []
        table2_good_rows = []
        for line in testData:
            if line[0] < 2000:
                ix = line[0] - 1000
                if ix not in table1_good_rows:
                    table1_good_rows.append(ix)
                self.assertAlmostEqual(line[1], self.table1Control['ra'][ix], 6)
                self.assertAlmostEqual(line[2], self.table1Control['dec'][ix], 6)
                self.assertAlmostEqual(self.table1Control['mag'][ix]+self.table1Control['dmag'][ix],
                                       line[3], 6)
                self.assertGreater(self.table1Control['ra'][ix], 100.0)
                self.assertLess(self.table1Control['ra'][ix], 260.0)
                self.assertGreater(self.table1Control['dec'][ix], -25.0)
                self.assertLess(self.table1Control['dec'][ix], 25.0)
                self.assertGreater(self.table1Control['mag'][ix], 20.0)
            elif line[0] < 3000:
                ix = line[0] - 2000
                if ix not in table1_good_rows:
                    table1_good_rows.append(ix)
                self.assertAlmostEqual(2.0*self.table1Control['ra'][ix]+self.table1Control['dra'][ix],
                                       line[1], 6)
                self.assertAlmostEqual(2.0*self.table1Control['dec'][ix]+self.table1Control['ddec'][ix],
                                       line[2], 6)
                self.assertAlmostEqual(self.table1Control['mag'][ix]+self.table1Control['dmag'][ix],
                                       line[3], 6)
                self.assertGreater(self.table1Control['ra'][ix], 100.0)
                self.assertLess(self.table1Control['ra'][ix], 260.0)
                self.assertGreater(self.table1Control['dec'][ix], -25.0)
                self.assertLess(self.table1Control['dec'][ix], 25.0)
                self.assertGreater(self.table1Control['mag'][ix], 20.0)
            else:
                ix = line[0]-3000
                if ix not in table2_good_rows:
                    table2_good_rows.append(ix)
                self.assertAlmostEqual(line[1], self.table2Control['ra'][ix], 6)
                self.assertAlmostEqual(line[2], self.table2Control['dec'][ix], 6)
                self.assertAlmostEqual(line[3], self.table2Control['mag'][ix], 6)
                self.assertGreater(self.table2Control['ra'][ix], 100.0)
                self.assertLess(self.table2Control['ra'][ix], 260.0)
                self.assertGreater(self.table2Control['dec'][ix], -25.0)
                self.assertLess(self.table2Control['dec'][ix], 25.0)
                self.assertGreater(self.table2Control['mag'][ix], 20.0)

        table1_bad_rows = [ii for ii in range(self.table1Control.shape[0]) if ii not in table1_good_rows]
        table2_bad_rows = [ii for ii in range(self.table2Control.shape[0]) if ii not in table2_good_rows]

        in_bounds = [rr > 100.0 and rr < 260.0 and dd > -25.0 and dd < 25.0 and mm > 20.0
                     for rr, dd, mm in zip(self.table1Control['ra'][table1_bad_rows],
                                           self.table1Control['dec'][table1_bad_rows],
                                           self.table1Control['mag'][table1_bad_rows])]

        self.assertNotIn(True, in_bounds, msg='failed to assemble table1_bad_rows')

        in_bounds = [rr > 100.0 and rr < 260.0 and dd > -25.0 and dd < 25.0 and mm > 20.0
                     for rr, dd, mm in zip(self.table2Control['ra'][table2_bad_rows],
                                           self.table2Control['dec'][table2_bad_rows],
                                           self.table2Control['mag'][table2_bad_rows])]

        self.assertNotIn(True, in_bounds, msg='failed to assemble table2_bad_rows')

        self.assertGreater(len(table1_good_rows), 0)
        self.assertGreater(len(table2_good_rows), 0)
        self.assertGreater(len(table1_bad_rows), 0)
        self.assertGreater(len(table2_bad_rows), 0)
        self.assertEqual(len(table1_good_rows)+len(table1_bad_rows), self.table1Control.shape[0])
        self.assertEqual(len(table2_good_rows)+len(table2_bad_rows), self.table2Control.shape[0])

        if os.path.exists(fileName):
            os.unlink(fileName)
    def testConstraint(self):
        """
        Test that CompoundInstanceCatalog handles constraint
        properly
        """
        fileName = os.path.join(self.scratch_dir, 'compound_constraint_test_cat.txt')

        compoundCat = CompoundInstanceCatalog([Cat1, Cat2, Cat3], [table1DB1, table1DB2, table2DB1],
                                              constraint='mag>20.0')

        compoundCat.write_catalog(fileName)
        dtype = np.dtype([('id', np.int),
                          ('raObs', np.float),
                          ('decObs', np.float),
                          ('final_mag', np.float)])

        testData = np.genfromtxt(fileName, dtype=dtype)

        table1_good_rows = []
        table2_good_rows = []
        for line in testData:
            if line[0] < 2000:
                ix = line[0]-1000
                if ix not in table1_good_rows:
                    table1_good_rows.append(ix)
                self.assertAlmostEqual(line[1], self.table1Control['ra'][ix], 6)
                self.assertAlmostEqual(line[2], self.table1Control['dec'][ix], 6)
                self.assertAlmostEqual(self.table1Control['mag'][ix]+self.table1Control['dmag'][ix],
                                       line[3], 6)
                self.assertGreater(self.table1Control['mag'][ix], 20.0)
            elif line[0] < 3000:
                ix = line[0] - 2000
                if ix not in table1_good_rows:
                    table1_good_rows.append(ix)
                self.assertAlmostEqual(2.0*self.table1Control['ra'][ix]+self.table1Control['dra'][ix],
                                       line[1], 6)
                self.assertAlmostEqual(2.0*self.table1Control['dec'][ix]+self.table1Control['ddec'][ix],
                                       line[2], 6)
                self.assertAlmostEqual(self.table1Control['mag'][ix]+self.table1Control['dmag'][ix],
                                       line[3], 6)
                self.assertGreater(self.table1Control['mag'][ix], 20.0)
            else:
                ix = line[0]-3000
                if ix not in table2_good_rows:
                    table2_good_rows.append(ix)
                self.assertAlmostEqual(line[1], self.table2Control['ra'][ix], 6)
                self.assertAlmostEqual(line[2], self.table2Control['dec'][ix], 6)
                self.assertAlmostEqual(line[3], self.table2Control['mag'][ix], 6)
                self.assertGreater(self.table2Control['mag'][ix], 20.0)

        table1_bad_rows = [ii for ii in range(self.table1Control.shape[0]) if ii not in table1_good_rows]
        table2_bad_rows = [ii for ii in range(self.table2Control.shape[0]) if ii not in table2_good_rows]

        in_bounds = [mm > 20.0 for mm in self.table1Control['mag'][table1_bad_rows]]

        self.assertNotIn(True, in_bounds, msg='failed to assemble table1_bad_rows')

        in_bounds = [mm > 20.0 for mm in self.table2Control['mag'][table2_bad_rows]]

        self.assertNotIn(True, in_bounds, msg='failed to assemble table2_bad_rows')

        self.assertGreater(len(table1_good_rows), 0)
        self.assertGreater(len(table2_good_rows), 0)
        self.assertGreater(len(table1_bad_rows), 0)
        self.assertGreater(len(table2_bad_rows), 0)
        self.assertEqual(len(table1_good_rows)+len(table1_bad_rows), self.table1Control.shape[0])
        self.assertEqual(len(table2_good_rows)+len(table2_bad_rows), self.table2Control.shape[0])

        if os.path.exists(fileName):
            os.unlink(fileName)
Пример #18
0
    def test_compound_cat_compound_column(self):
        """
        Test filtering a CompoundInstanceCatalog on a compound column
        """

        class FilteringCatClass4(InstanceCatalog):
            column_outputs = ['id', 'a', 'b']
            cannot_be_null = ['a']

            @compound('a', 'b')
            def get_alphabet(self):
                a = self.column_by_name('ip1')
                a = a*a
                b = self.column_by_name('ip2')
                b = b*0.25
                return np.array([np.where(a % 2 == 0, a, None), b])

        class FilteringCatClass5(InstanceCatalog):
            column_outputs = ['id', 'a', 'b', 'filter']
            cannot_be_null = ['b', 'filter']

            @compound('a', 'b')
            def get_alphabet(self):
                ii = self.column_by_name('ip1')
                return np.array([self.column_by_name('ip2')**3,
                                 np.where(ii % 2 == 1, ii, None)])

            @cached
            def get_filter(self):
                ii = self.column_by_name('ip1')
                return np.where(ii % 3 != 0, ii, None)

        class DbClass(CatalogDBObject):
            host = None
            port = None
            database = self.db_name
            driver = 'sqlite'
            tableid = 'test'
            idColKey = 'id'

        class DbClass4(DbClass):
            objid = 'silliness4'

        class DbClass5(DbClass):
            objid = 'silliness5'

        cat_name = os.path.join(self.scratch_dir, "compound_cat_compound_filter_cat.txt")
        if os.path.exists(cat_name):
            os.unlink(cat_name)

        cat = CompoundInstanceCatalog([FilteringCatClass4, FilteringCatClass5], [DbClass4, DbClass5])
        cat.write_catalog(cat_name)

        # now make sure that the catalog contains the expected data
        with open(cat_name, 'r') as input_file:
            input_lines = input_file.readlines()
        self.assertEqual(len(input_lines), 9)  # 8 data lines and a header

        first_cat_lines = ['1, 4, 0.75\n', '3, 16, 1.25\n',
                           '5, 36, 1.75\n', '7, 64, 2.25\n',
                           '9, 100, 2.75\n']

        second_cat_lines = ['0, 8, 1, 1\n', '4, 216, 5, 5\n',
                            '6, 512, 7, 7\n']

        for i_line, line in enumerate(input_lines):
            if i_line is 0:
                continue
            elif i_line < 6:
                self.assertEqual(line, first_cat_lines[i_line-1])
            else:
                self.assertEqual(line, second_cat_lines[i_line-6])

        if os.path.exists(cat_name):
            os.unlink(cat_name)
    def testConstraint(self):
        """
        Test that CompoundInstanceCatalog handles constraint
        properly
        """
        fileName = os.path.join(self.baseDir,
                                'compound_constraint_test_cat.txt')

        compoundCat = CompoundInstanceCatalog(
            [Cat1, Cat2, Cat3], [table1DB1, table1DB2, table2DB1],
            constraint='mag>20.0')

        compoundCat.write_catalog(fileName)
        dtype = np.dtype([('id', np.int), ('raObs', np.float),
                          ('decObs', np.float), ('final_mag', np.float)])

        testData = np.genfromtxt(fileName, dtype=dtype)

        table1_good_rows = []
        table2_good_rows = []
        for line in testData:
            if line[0] < 2000:
                ix = line[0] - 1000
                if ix not in table1_good_rows:
                    table1_good_rows.append(ix)
                self.assertAlmostEqual(line[1], self.table1Control['ra'][ix],
                                       6)
                self.assertAlmostEqual(line[2], self.table1Control['dec'][ix],
                                       6)
                self.assertAlmostEqual(
                    self.table1Control['mag'][ix] +
                    self.table1Control['dmag'][ix], line[3], 6)
                self.assertGreater(self.table1Control['mag'][ix], 20.0)
            elif line[0] < 3000:
                ix = line[0] - 2000
                if ix not in table1_good_rows:
                    table1_good_rows.append(ix)
                self.assertAlmostEqual(
                    2.0 * self.table1Control['ra'][ix] +
                    self.table1Control['dra'][ix], line[1], 6)
                self.assertAlmostEqual(
                    2.0 * self.table1Control['dec'][ix] +
                    self.table1Control['ddec'][ix], line[2], 6)
                self.assertAlmostEqual(
                    self.table1Control['mag'][ix] +
                    self.table1Control['dmag'][ix], line[3], 6)
                self.assertGreater(self.table1Control['mag'][ix], 20.0)
            else:
                ix = line[0] - 3000
                if ix not in table2_good_rows:
                    table2_good_rows.append(ix)
                self.assertAlmostEqual(line[1], self.table2Control['ra'][ix],
                                       6)
                self.assertAlmostEqual(line[2], self.table2Control['dec'][ix],
                                       6)
                self.assertAlmostEqual(line[3], self.table2Control['mag'][ix],
                                       6)
                self.assertGreater(self.table2Control['mag'][ix], 20.0)

        table1_bad_rows = [
            ii for ii in range(self.table1Control.shape[0])
            if ii not in table1_good_rows
        ]
        table2_bad_rows = [
            ii for ii in range(self.table2Control.shape[0])
            if ii not in table2_good_rows
        ]

        in_bounds = [
            mm > 20.0 for mm in self.table1Control['mag'][table1_bad_rows]
        ]

        self.assertNotIn(True,
                         in_bounds,
                         msg='failed to assemble table1_bad_rows')

        in_bounds = [
            mm > 20.0 for mm in self.table2Control['mag'][table2_bad_rows]
        ]

        self.assertNotIn(True,
                         in_bounds,
                         msg='failed to assemble table2_bad_rows')

        self.assertGreater(len(table1_good_rows), 0)
        self.assertGreater(len(table2_good_rows), 0)
        self.assertGreater(len(table1_bad_rows), 0)
        self.assertGreater(len(table2_bad_rows), 0)
        self.assertEqual(
            len(table1_good_rows) + len(table1_bad_rows),
            self.table1Control.shape[0])
        self.assertEqual(
            len(table2_good_rows) + len(table2_bad_rows),
            self.table2Control.shape[0])

        if os.path.exists(fileName):
            os.unlink(fileName)