Hide keyboard shortcuts

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 __future__ import with_statement 

2from builtins import zip 

3from builtins import range 

4from builtins import object 

5import os 

6import numpy as np 

7import unittest 

8import shutil 

9import tempfile 

10import lsst.utils.tests 

11from lsst.sims.utils.CodeUtilities import sims_clean_up 

12from lsst.sims.catalogs.utils import makeStarTestDB, myTestStars 

13from lsst.sims.catalogs.utils import makeGalTestDB, myTestGals 

14from lsst.sims.catalogs.definitions import InstanceCatalog 

15from lsst.sims.catalogs.decorators import compound 

16from lsst.sims.catUtils.mixins import PhotometryStars, PhotometryGalaxies 

17from lsst.sims.photUtils import BandpassDict 

18 

19ROOT = os.path.abspath(os.path.dirname(__file__)) 

20 

21 

22def setup_module(module): 

23 lsst.utils.tests.init() 

24 

25 

26class FakeStellarVariabilityMixin(object): 

27 

28 @compound('delta_test_u', 'delta_test_g', 'delta_test_r') 

29 def get_variability(self): 

30 ra = self.column_by_name('raJ2000') 

31 return np.array([4.0*np.ones(len(ra)), 

32 10.0*np.ones(len(ra)), 

33 20.0*np.ones(len(ra))]) 

34 

35 

36class StellarBaselineCatalogClass(InstanceCatalog, PhotometryStars): 

37 

38 catalog_type = __file__ + 'stellar_baseline_catalog' 

39 

40 default_columns = [('galacticAv', 0.1, float)] 

41 

42 def get_sedFilename(self): 

43 

44 star_seds = ['km20_5750.fits_g40_5790', 

45 'kp10_9250.fits_g40_9250', 

46 'bergeron_6500_85.dat_6700'] 

47 

48 ra = self.column_by_name('raJ2000') 

49 return np.array([star_seds[i%3] for i in range(len(ra))]) 

50 

51 @compound('test_u', 'test_g', 'test_r', 'test_i', 'test_z', 'test_y') 

52 def get_test_mags(self): 

53 if not hasattr(self, 'variabilitybandpassDict'): 

54 self.variabilityBandpassDict = BandpassDict.loadTotalBandpassesFromFiles() 

55 

56 self._loadSedList(self.variabilityBandpassDict.wavelenMatch) 

57 if not hasattr(self, '_sedList'): 

58 return np.ones((6, 0)) 

59 

60 mag = self._quiescentMagnitudeGetter(self.variabilityBandpassDict, self.get_test_mags._colnames) 

61 

62 mag += self._variabilityGetter(self.get_test_mags._colnames) 

63 return mag 

64 

65class StellarVariabilityCatalogClass(StellarBaselineCatalogClass, FakeStellarVariabilityMixin): 

66 catalog_type = __file__ + 'stellar_variability_catalog_class' 

67 pass 

68 

69 

70class FakeGalaxyVariabilityMixin(object): 

71 

72 @compound('delta_test_agn_u', 'delta_test_agn_g', 'delta_test_agn_r', 

73 'delta_test_bulge_u', 'delta_test_disk_i') 

74 def get_variability(self): 

75 ra = self.column_by_name('raJ2000') 

76 return np.array([1.0*np.ones(len(ra)), 

77 2.0*np.ones(len(ra)), 

78 3.0*np.ones(len(ra)), 

79 4.0*np.ones(len(ra)), 

80 5.0*np.ones(len(ra))]) 

81 

82 

83class GalaxyBaselineCatalogClass(InstanceCatalog, PhotometryGalaxies): 

84 

85 catalog_type = __file__ + 'galaxy_baseline_catalog_class' 

86 

87 @compound('internalAvBulge', 'internalAvDisk') 

88 def get_internalAv(self): 

89 ra = self.column_by_name('raJ2000') 

90 return np.array([2.5*np.ones(len(ra)), 2.5*np.ones(len(ra))]) 

91 

92 @compound('sedFilenameBulge', 'sedFilenameDisk', 'sedFilenameAgn') 

93 def get_filenames(self): 

94 ra = self.column_by_name('raJ2000') 

95 

96 galaxy_seds = ['Const.80E07.02Z.spec', 'Inst.80E07.002Z.spec', 'Burst.19E07.0005Z.spec'] 

97 agn_sed = 'agn.spec' 

98 

99 agnSeds = [] 

100 for ii in range(len(ra)): 

101 agnSeds.append(agn_sed) 

102 

103 bulgeSeds = [galaxy_seds[(ii+1)%3] for ii in range(len(ra))] 

104 diskSeds = [galaxy_seds[ii%3] for ii in range(len(ra))] 

105 

106 return np.array([bulgeSeds, diskSeds, agnSeds]) 

107 

108 @compound('test_bulge_u', 'test_bulge_g', 'test_bulge_r', 

109 'test_bulge_i', 'test_bulge_z', 'test_bulge_y') 

110 def get_test_bulge_mags(self): 

111 

112 if not hasattr(self, 'testBandpassDict'): 

113 self.testBandpassDict = BandpassDict.loadTotalBandpassesFromFiles() 

114 

115 mag = self._quiescentMagnitudeGetter('bulge', self.testBandpassDict, 

116 self.get_test_bulge_mags._colnames) 

117 mag += self._variabilityGetter(self.get_test_bulge_mags._colnames) 

118 return mag 

119 

120 @compound('test_disk_u', 'test_disk_g', 'test_disk_r', 

121 'test_disk_i', 'test_disk_z', 'test_disk_y') 

122 def get_test_disk_mags(self): 

123 

124 if not hasattr(self, 'testBandpassDict'): 

125 self.testBandpassDict = BandpassDict.loadTotalBandpassesFromFiles() 

126 

127 mag = self._quiescentMagnitudeGetter('disk', self.testBandpassDict, 

128 self.get_test_disk_mags._colnames) 

129 mag += self._variabilityGetter(self.get_test_disk_mags._colnames) 

130 return mag 

131 

132 @compound('test_agn_u', 'test_agn_g', 'test_agn_r', 

133 'test_agn_i', 'test_agn_z', 'test_agn_y') 

134 def get_test_agn_mags(self): 

135 

136 if not hasattr(self, 'testBandpassDict'): 

137 self.testBandpassDict = BandpassDict.loadTotalBandpassesFromFiles() 

138 

139 mag = self._quiescentMagnitudeGetter('agn', self.testBandpassDict, 

140 self.get_test_agn_mags._colnames) 

141 mag += self._variabilityGetter(self.get_test_agn_mags._colnames) 

142 return mag 

143 

144 @compound('test_u', 'test_g', 'test_r', 'test_i', 'test_z', 'test_y') 

145 def get_test_total_mags(self): 

146 idList = self.column_by_name('uniqueId') 

147 numObj = len(idList) 

148 output = [] 

149 for columnName in self.get_test_total_mags._colnames: 

150 if columnName not in self._actually_calculated_columns: 

151 sub_list = [np.NaN]*numObj 

152 else: 

153 bandpass = columnName[-1] 

154 bulge = self.column_by_name('test_bulge_%s' % bandpass) 

155 disk = self.column_by_name('test_disk_%s' % bandpass) 

156 agn = self.column_by_name('test_agn_%s' % bandpass) 

157 sub_list = self.sum_magnitudes(bulge=bulge, disk=disk, agn=agn) 

158 

159 output.append(sub_list) 

160 return np.array(output) 

161 

162 

163class GalaxyVariabilityCatalogClass(GalaxyBaselineCatalogClass, FakeGalaxyVariabilityMixin): 

164 

165 catalog_type = __file__ + 'galaxy_variability_catalog_class' 

166 

167 pass 

168 

169 

170class VariabilityDesignTest(unittest.TestCase): 

171 """ 

172 This unit test case will test that the general 

173 variability design was correclty implemented. 

174 It will not test an particular model of variability. 

175 It will merely test that, given a mean and delta magnitude, 

176 the InstanceCatalog class can correctly calculate the varying 

177 magnitude of an object. 

178 """ 

179 

180 @classmethod 

181 def setUpClass(cls): 

182 cls.scratch_dir = tempfile.mkdtemp(dir=ROOT, prefix='VariabilityDesignTest-') 

183 

184 cls.starDbName = os.path.join(cls.scratch_dir, 'VariabilityInfrastructureTestStarDB.db') 

185 makeStarTestDB(filename=cls.starDbName) 

186 

187 cls.galaxyDbName = os.path.join(cls.scratch_dir, 'VariabilityInfrastructureTestGalaxyDB.db') 

188 makeGalTestDB(filename=cls.galaxyDbName) 

189 

190 @classmethod 

191 def tearDownClass(cls): 

192 sims_clean_up() 

193 if os.path.exists(cls.scratch_dir): 

194 shutil.rmtree(cls.scratch_dir) 

195 

196 def setUp(self): 

197 self.starDB = myTestStars(database=self.starDbName) 

198 self.galaxyDB = myTestGals(database=self.galaxyDbName) 

199 

200 def tearDown(self): 

201 del self.starDB 

202 del self.galaxyDB 

203 

204 def testStellarVariabilityInfrastructure(self): 

205 """ 

206 Test that the variability design was correctly implemented 

207 in the case of stars 

208 """ 

209 

210 outputs = ['id', 'test_u', 'test_g', 'test_r', 'test_i', 'test_z', 'test_y'] 

211 baseline = StellarBaselineCatalogClass(self.starDB, column_outputs=outputs) 

212 variable = StellarVariabilityCatalogClass(self.starDB, column_outputs=outputs) 

213 

214 for bb, vv in zip(baseline.iter_catalog(), variable.iter_catalog()): 

215 self.assertEqual(bb[0], vv[0]) 

216 self.assertAlmostEqual(vv[1]-bb[1], 4.0, 10) 

217 self.assertAlmostEqual(vv[2]-bb[2], 10.0, 10) 

218 self.assertAlmostEqual(vv[3]-bb[3], 20.0, 10) 

219 self.assertAlmostEqual(vv[4], bb[4], 10) 

220 self.assertAlmostEqual(vv[5], bb[5], 10) 

221 self.assertAlmostEqual(vv[6], bb[6], 10) 

222 

223 def testGalaxyVariabilityInfrastructure(self): 

224 """ 

225 Test that the variability design was correctly implemented in 

226 the case of galaxies 

227 """ 

228 

229 outputs = ['id', 

230 'test_u', 'test_g', 'test_r', 'test_i', 'test_z', 'test_y', 

231 'test_bulge_u', 'test_bulge_g', 'test_bulge_r', 'test_bulge_i', 

232 'test_bulge_z', 'test_bulge_y', 

233 'test_disk_u', 'test_disk_g', 'test_disk_r', 'test_disk_i', 

234 'test_disk_z', 'test_disk_y', 

235 'test_agn_u', 'test_agn_g', 'test_agn_r', 

236 'test_agn_i', 'test_agn_z', 'test_agn_y'] 

237 

238 baseline = GalaxyBaselineCatalogClass(self.galaxyDB, column_outputs=outputs) 

239 variable = GalaxyVariabilityCatalogClass(self.galaxyDB, column_outputs=outputs) 

240 

241 phot = PhotometryGalaxies() 

242 

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

244 

245 for bb, vv in zip(baseline.iter_catalog(), variable.iter_catalog()): 

246 self.assertEqual(bb[0], vv[0]) 

247 

248 # test that the variable components are altered 

249 # the way they ought to be 

250 self.assertAlmostEqual(bb[19]+1.0, vv[19], 10) 

251 self.assertAlmostEqual(bb[20]+2.0, vv[20], 10) 

252 self.assertAlmostEqual(bb[21]+3.0, vv[21], 10) 

253 self.assertAlmostEqual(bb[7]+4.0, vv[7], 10) 

254 self.assertAlmostEqual(bb[16]+5.0, vv[16], 10) 

255 

256 # test that the components which do not vary are equal 

257 for ix in range(7, 25): 

258 if ix not in variable_indices: 

259 self.assertAlmostEqual(bb[ix], vv[ix], 10) 

260 

261 # test that the total magnitudes are correctly calculated 

262 for ix in range(6): 

263 

264 self.assertAlmostEqual(bb[ix+1], 

265 phot.sum_magnitudes(bulge=bb[7+ix], 

266 disk=bb[13+ix], 

267 agn=bb[19+ix]), 

268 10) 

269 

270 self.assertAlmostEqual(vv[ix+1], 

271 phot.sum_magnitudes(bulge=vv[7+ix], 

272 disk=vv[13+ix], 

273 agn=vv[19+ix]), 

274 10) 

275 

276 

277class MemoryTestClass(lsst.utils.tests.MemoryTestCase): 

278 pass 

279 

280if __name__ == "__main__": 280 ↛ 281line 280 didn't jump to line 281, because the condition on line 280 was never true

281 lsst.utils.tests.init() 

282 unittest.main()