Coverage for tests/testCosmologyMixins.py : 44%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from builtins import zip
2from builtins import range
3import os
4import unittest
5import lsst.utils.tests
6import numpy as np
7import tempfile
9from lsst.sims.utils.CodeUtilities import sims_clean_up
10from lsst.sims.utils import ObservationMetaData
12from lsst.sims.photUtils import CosmologyObject
14from lsst.sims.catalogs.utils import myTestGals, makeGalTestDB
16from lsst.sims.catUtils.utils import testGalaxies
17from lsst.sims.catUtils.mixins import CosmologyMixin
19ROOT = os.path.abspath(os.path.dirname(__file__))
22def setup_module(module):
23 lsst.utils.tests.init()
26class cosmologicalGalaxyCatalog(testGalaxies, CosmologyMixin):
27 catalog_type = __file__ + 'cosmo_galaxy_catalog'
28 column_outputs = ['galid', 'lsst_u', 'lsst_g', 'lsst_r', 'lsst_i', 'lsst_z', 'lsst_y',
29 'uBulge', 'gBulge', 'rBulge', 'iBulge', 'zBulge', 'yBulge',
30 'uDisk', 'gDisk', 'rDisk', 'iDisk', 'zDisk', 'yDisk',
31 'uAgn', 'gAgn', 'rAgn', 'iAgn', 'zAgn', 'yAgn',
32 'redshift', 'cosmologicalDistanceModulus']
35class absoluteGalaxyCatalog(testGalaxies):
36 catalog_type = __file__ + 'abs_galaxy_catalog'
37 column_outputs = ['galid', 'lsst_u', 'lsst_g', 'lsst_r', 'lsst_i', 'lsst_z', 'lsst_y',
38 'uBulge', 'gBulge', 'rBulge', 'iBulge', 'zBulge', 'yBulge',
39 'uDisk', 'gDisk', 'rDisk', 'iDisk', 'zDisk', 'yDisk',
40 'uAgn', 'gAgn', 'rAgn', 'iAgn', 'zAgn', 'yAgn',
41 'redshift']
43 def get_cosmologicalDistanceModulus(self):
44 """
45 Must set this to zero rather than `None` so that PhotometryGalaxies
46 does not apply cosmological dimming
47 """
48 return np.zeros(len(self.column_by_name('galid')))
51class CosmologyMixinUnitTest(unittest.TestCase):
52 """
53 This class will test to make sure that our example CosmologyMixin
54 (defined in lsst/sims/photUtils/examples/CosmologyMixin.py)
55 can produce a catalog
56 """
58 @classmethod
59 def setUpClass(cls):
60 cls.dbName = tempfile.mktemp(prefix='cosmologyTestDB-', suffix=".db", dir=ROOT)
61 cls.dbSize = 100
62 if os.path.exists(cls.dbName):
63 os.unlink(cls.dbName)
64 makeGalTestDB(size=cls.dbSize, seedVal=1, filename=cls.dbName)
66 @classmethod
67 def tearDownClass(cls):
68 sims_clean_up()
69 if os.path.exists(cls.dbName):
70 os.unlink(cls.dbName)
72 del cls.dbName
73 del cls.dbSize
75 def setUp(self):
76 self.obs = ObservationMetaData(mjd=59580.0)
78 def testCosmologyCatalog(self):
79 """
80 Does a catalog get written?
81 """
82 dbObj = myTestGals(database=self.dbName)
83 cat = cosmologicalGalaxyCatalog(dbObj, obs_metadata=self.obs)
84 with lsst.utils.tests.getTempFilePath('.txt') as catName:
85 cat.write_catalog(catName)
87 def testCatalogDistanceModulus(self):
88 """
89 Does cosmologicalDistanceModulus get properly applied
90 """
91 dbObj = myTestGals(database=self.dbName)
92 cosmoCat = cosmologicalGalaxyCatalog(dbObj, obs_metadata=self.obs)
93 controlCat = absoluteGalaxyCatalog(dbObj, obs_metadata=self.obs)
94 cosmoIter = cosmoCat.iter_catalog(chunk_size=self.dbSize)
95 controlIter = controlCat.iter_catalog(chunk_size=self.dbSize)
97 cosmology = CosmologyObject()
99 for (cosmoRow, controlRow) in zip(cosmoIter, controlIter):
100 modulus = cosmology.distanceModulus(controlRow[25])
101 self.assertEqual(cosmoRow[0], controlRow[0])
102 self.assertEqual(cosmoRow[25], controlRow[25])
103 self.assertEqual(cosmoRow[26], modulus)
104 for i in range(1, 25):
105 self.assertAlmostEqual(cosmoRow[i], controlRow[i] + modulus, 6)
108class MemoryTestClass(lsst.utils.tests.MemoryTestCase):
109 pass
111if __name__ == "__main__": 111 ↛ 112line 111 didn't jump to line 112, because the condition on line 111 was never true
112 lsst.utils.tests.init()
113 unittest.main()