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 builtins import zip 

2from builtins import str 

3from builtins import range 

4import unittest 

5import os 

6import numpy as np 

7import lsst.utils 

8import lsst.utils.tests 

9from lsst.sims.catUtils.matchSED.selectStarSED import selectStarSED 

10from lsst.sims.catUtils.matchSED.selectGalaxySED import selectGalaxySED 

11from lsst.sims.catUtils.matchSED.matchUtils import matchBase 

12from lsst.sims.catUtils.matchSED.matchUtils import matchStar 

13from lsst.sims.catUtils.matchSED.matchUtils import matchGalaxy 

14from lsst.sims.catUtils.dust.EBV import EBVbase as ebv 

15from lsst.sims.photUtils.Sed import Sed 

16from lsst.sims.photUtils.Bandpass import Bandpass 

17from lsst.sims.photUtils import BandpassDict 

18from lsst.utils import getPackageDir 

19from lsst.sims.utils.CodeUtilities import sims_clean_up 

20 

21 

22def setup_module(module): 

23 lsst.utils.tests.init() 

24 

25 

26class TestMatchBase(unittest.TestCase): 

27 

28 @classmethod 

29 def setUpClass(cls): 

30 

31 cls.galDir = os.path.join(getPackageDir('sims_photUtils'), 'tests/cartoonSedTestData/galaxySed/') 

32 cls.filterList = ('u', 'g', 'r', 'i', 'z') 

33 

34 @classmethod 

35 def tearDownClass(cls): 

36 sims_clean_up() 

37 del cls.galDir 

38 del cls.filterList 

39 

40 def testCalcMagNorm(self): 

41 

42 """Tests the calculation of magnitude normalization for an SED with the given magnitudes 

43 in the given bandpasses.""" 

44 

45 testUtils = matchBase() 

46 bandpassDir = os.path.join(lsst.utils.getPackageDir('throughputs'), 'sdss') 

47 testPhot = BandpassDict.loadTotalBandpassesFromFiles(self.filterList, 

48 bandpassDir = bandpassDir, 

49 bandpassRoot = 'sdss_') 

50 

51 unChangedSED = Sed() 

52 unChangedSED.readSED_flambda(str(self.galDir + os.listdir(self.galDir)[0])) 

53 

54 imSimBand = Bandpass() 

55 imSimBand.imsimBandpass() 

56 testSED = Sed() 

57 testSED.setSED(unChangedSED.wavelen, flambda = unChangedSED.flambda) 

58 magNorm = 20.0 

59 redVal = 0.1 

60 testSED.redshiftSED(redVal) 

61 fluxNorm = testSED.calcFluxNorm(magNorm, imSimBand) 

62 testSED.multiplyFluxNorm(fluxNorm) 

63 sedMags = testPhot.magListForSed(testSED) 

64 stepSize = 0.001 

65 testMagNorm = testUtils.calcMagNorm(sedMags, unChangedSED, testPhot, redshift = redVal) 

66 # Test adding in mag_errors. If an array of np.ones is passed in we should get same result 

67 testMagNormWithErr = testUtils.calcMagNorm(sedMags, unChangedSED, testPhot, 

68 mag_error = np.ones(len(sedMags)), redshift = redVal) 

69 # Also need to add in test for filtRange 

70 sedMagsIncomp = sedMags 

71 sedMagsIncomp[1] = None 

72 filtRangeTest = [0, 2, 3, 4] 

73 testMagNormFiltRange = testUtils.calcMagNorm(sedMagsIncomp, unChangedSED, testPhot, 

74 redshift = redVal, filtRange = filtRangeTest) 

75 self.assertAlmostEqual(magNorm, testMagNorm, delta = stepSize) 

76 self.assertAlmostEqual(magNorm, testMagNormWithErr, delta = stepSize) 

77 self.assertAlmostEqual(magNorm, testMagNormFiltRange, delta = stepSize) 

78 

79 def testCalcBasicColors(self): 

80 

81 """Tests the calculation of the colors of an SED in given bandpasses.""" 

82 

83 testUtils = matchBase() 

84 testSED = Sed() 

85 bandpassDir = os.path.join(lsst.utils.getPackageDir('throughputs'), 'sdss') 

86 testPhot = BandpassDict.loadTotalBandpassesFromFiles(self.filterList, 

87 bandpassDir = bandpassDir, 

88 bandpassRoot = 'sdss_') 

89 

90 testSED.readSED_flambda(str(self.galDir + os.listdir(self.galDir)[0])) 

91 testMags = testPhot.magListForSed(testSED) 

92 testColors = [] 

93 for filtNum in range(0, len(self.filterList)-1): 

94 testColors.append(testMags[filtNum] - testMags[filtNum+1]) 

95 

96 testOutput = testUtils.calcBasicColors([testSED], testPhot) 

97 np.testing.assert_equal([testColors], testOutput) 

98 

99 def testSEDCopyBasicColors(self): 

100 

101 """Tests that when makeCopy=True in calcBasicColors the SED object is unchanged after calling 

102 and that colors are still accurately calculated""" 

103 

104 testUtils = matchBase() 

105 testSED = Sed() 

106 copyTest = Sed() 

107 bandpassDir = os.path.join(lsst.utils.getPackageDir('throughputs'), 'sdss') 

108 testPhot = BandpassDict.loadTotalBandpassesFromFiles(self.filterList, 

109 bandpassDir = bandpassDir, 

110 bandpassRoot = 'sdss_') 

111 testSED.readSED_flambda(str(self.galDir + os.listdir(self.galDir)[0])) 

112 copyTest.setSED(wavelen = testSED.wavelen, flambda = testSED.flambda) 

113 testLambda = copyTest.wavelen[0] 

114 testMags = testPhot.magListForSed(testSED) 

115 testColors = [] 

116 for filtNum in range(0, len(self.filterList)-1): 

117 testColors.append(testMags[filtNum] - testMags[filtNum+1]) 

118 testOutput = testUtils.calcBasicColors([copyTest], testPhot, makeCopy=True) 

119 

120 self.assertEqual(testLambda, copyTest.wavelen[0]) 

121 np.testing.assert_equal([testColors], testOutput) 

122 

123 def testDeReddenMags(self): 

124 

125 """Test that consistent numbers come out of deReddening procedure""" 

126 

127 am = 0.5 

128 coeffs = np.ones(5) 

129 mags = np.arange(2, -3, -1) 

130 

131 testDeRed = matchBase().deReddenMags(am, mags, coeffs) 

132 

133 # Test Output 

134 np.testing.assert_equal(testDeRed, [mags-(am*coeffs)]) 

135 

136 

137class TestMatchStar(unittest.TestCase): 

138 

139 @classmethod 

140 def setUpClass(cls): 

141 

142 # Left this in after removing loading SEDs so that we can make sure that if the structure of 

143 # sims_sed_library changes in a way that affects testMatchSEDs we can detect it. 

144 

145 cls.kmTestName = 'km99_9999.fits_g99_9999' 

146 cls.mTestName = 'm99.99Full.dat' 

147 

148 # Set up Test Spectra Directory 

149 cls.testSpecDir = os.path.join(getPackageDir('sims_photUtils'), 'tests/cartoonSedTestData/starSed/') 

150 cls.testKDir = str(cls.testSpecDir + 'kurucz/') 

151 cls.testMLTDir = str(cls.testSpecDir + 'mlt/') 

152 cls.testWDDir = str(cls.testSpecDir + 'wDs/') 

153 

154 def testLoadKurucz(self): 

155 """Test SED loading algorithm by making sure SEDs are all accounted for """ 

156 # Test Matching to Kurucz SEDs 

157 loadTestKurucz = matchStar(kuruczDir = self.testKDir) 

158 testSEDs = loadTestKurucz.loadKuruczSEDs() 

159 

160 # Read in a list of the SEDs in the kurucz test sed directory 

161 testKuruczList = os.listdir(self.testKDir) 

162 

163 # First make sure that all SEDs are correctly accounted for if no subset provided 

164 testNames = [] 

165 for testSED in testSEDs: 

166 testNames.append(testSED.name) 

167 

168 # Python 3 replaces assertItemsEqual() with assertCountEqual() 

169 if hasattr(self, 'assertItemsEqual'): 

170 self.assertItemsEqual(testKuruczList, testNames) 

171 else: 

172 self.assertCountEqual(testKuruczList, testNames) 

173 

174 # Test same condition if subset is provided 

175 testSubsetList = ['km01_7000.fits_g40_7140.gz', 'kp01_7000.fits_g40_7240.gz'] 

176 testSEDsSubset = loadTestKurucz.loadKuruczSEDs(subset = testSubsetList) 

177 

178 # Next make sure that correct subset loads if subset is provided 

179 testSubsetNames = [] 

180 testSubsetLogZ = [] 

181 testSubsetLogG = [] 

182 testSubsetTemp = [] 

183 for testSED in testSEDsSubset: 

184 testSubsetNames.append(testSED.name) 

185 testSubsetLogZ.append(testSED.logZ) 

186 testSubsetLogG.append(testSED.logg) 

187 testSubsetTemp.append(testSED.temp) 

188 

189 # Python 3 replaces assertItemsEqual() with assertCountEqual() 

190 if hasattr(self, 'assertItemsEqual'): 

191 self.assertItemsEqual(testSubsetList, testSubsetNames) 

192 else: 

193 self.assertCountEqual(testSubsetList, testSubsetNames) 

194 

195 self.assertEqual(testSubsetLogZ, [-0.1, 0.1]) # Test both pos. and neg. get in right 

196 self.assertEqual(testSubsetLogG, [4.0, 4.0]) # Test storage of logg and temp 

197 self.assertEqual(testSubsetTemp, [7140, 7240]) 

198 

199 # Test that attributes have been assigned 

200 for testSED in testSEDsSubset: 

201 self.assertIsNotNone(testSED.name) 

202 self.assertIsNotNone(testSED.logZ) 

203 self.assertIsNotNone(testSED.logg) 

204 self.assertIsNotNone(testSED.temp) 

205 

206 def testLoadMLT(self): 

207 """Test SED loading algorithm by making sure SEDs are all accounted for""" 

208 # Test Matching to mlt SEDs 

209 loadTestMLT = matchStar(mltDir = self.testMLTDir) 

210 testSEDs = loadTestMLT.loadmltSEDs() 

211 

212 # Read in a list of the SEDs in the mlt test sed directory 

213 testMLTList = os.listdir(self.testMLTDir) 

214 

215 # First make sure that all SEDs are correctly accounted for if no subset provided 

216 testNames = [] 

217 for testSED in testSEDs: 

218 testNames.append(testSED.name) 

219 

220 # Python 3 replaces assertItemsEqual() with assertCountEqual() 

221 if hasattr(self, 'assertItemsEqual'): 

222 self.assertItemsEqual(testMLTList, testNames) 

223 else: 

224 self.assertCountEqual(testMLTList, testNames) 

225 

226 # Next make sure that correct subset loads if subset is provided 

227 testSubsetList = testMLTList[0:2] 

228 testSEDsubset = loadTestMLT.loadmltSEDs(subset = testSubsetList) 

229 testSubsetNames = [] 

230 for testSED in testSEDsubset: 

231 testSubsetNames.append(testSED.name) 

232 

233 # Python 3 replaces assertItemsEqual() with assertCountEqual() 

234 if hasattr(self, 'assertItemsEqual'): 

235 self.assertItemsEqual(testSubsetList, testSubsetNames) 

236 else: 

237 self.assertCountEqual(testSubsetList, testSubsetNames) 

238 

239 # Test that attributes have been assigned 

240 for testSED in testSEDsubset: 

241 self.assertIsNotNone(testSED.name) 

242 

243 def testLoadWD(self): 

244 """Test SED loading algorithm by making sure SEDs are all accounted for and 

245 that there are separate lists for H and HE.""" 

246 # Test Matching to WD SEDs 

247 loadTestWD = matchStar(wdDir = self.testWDDir) 

248 testSEDsH, testSEDsHE = loadTestWD.loadwdSEDs() 

249 

250 # Add extra step because WD SEDs are separated into helium and hydrogen 

251 testNames = [] 

252 for testH in testSEDsH: 

253 testNames.append(testH.name) 

254 for testHE in testSEDsHE: 

255 testNames.append(testHE.name) 

256 

257 # Read in a list of the SEDs in the wd test sed directory 

258 testWDList = os.listdir(self.testWDDir) 

259 

260 # First make sure that all SEDs are correctly accounted for if no subset provided 

261 

262 # Python 3 replaces assertItemsEqual() with assertCountEqual() 

263 if hasattr(self, 'assertItemsEqual'): 

264 self.assertItemsEqual(testNames, testWDList) 

265 else: 

266 self.assertCountEqual(testNames, testWDList) 

267 

268 # Test same condition if subset is provided 

269 testSubsetList = ['bergeron_10000_75.dat_10100.gz', 'bergeron_He_9000_80.dat_9400.gz'] 

270 

271 testSEDsSubsetH, testSEDsSubsetHE = selectStarSED(wdDir= 

272 self.testWDDir).loadwdSEDs(subset= 

273 testSubsetList) 

274 

275 testNamesSubset = [] 

276 for testH in testSEDsSubsetH: 

277 testNamesSubset.append(testH.name) 

278 for testHE in testSEDsSubsetHE: 

279 testNamesSubset.append(testHE.name) 

280 

281 # Next make sure that correct subset loads if subset is provided 

282 # Python 3 replaces assertItemsEqual() with assertCountEqual() 

283 if hasattr(self, 'assertItemsEqual'): 

284 self.assertItemsEqual(testNamesSubset, testSubsetList) 

285 else: 

286 self.assertCountEqual(testNamesSubset, testSubsetList) 

287 

288 # Make sure that the names get separated into correct wd type 

289 self.assertEqual(testSEDsSubsetH[0].name, testSubsetList[0]) 

290 self.assertEqual(testSEDsSubsetHE[0].name, testSubsetList[1]) 

291 

292 @classmethod 

293 def tearDownClass(cls): 

294 sims_clean_up() 

295 del cls.testSpecDir 

296 del cls.testKDir 

297 del cls.testMLTDir 

298 del cls.testWDDir 

299 

300 del cls.kmTestName 

301 del cls.mTestName 

302 

303 

304class TestMatchGalaxy(unittest.TestCase): 

305 

306 @classmethod 

307 def setUpClass(cls): 

308 

309 # Set up Test Spectra Directory 

310 cls.testSpecDir = os.path.join(getPackageDir('sims_photUtils'), 'tests/cartoonSedTestData/galaxySed/') 

311 

312 cls.filterList = ('u', 'g', 'r', 'i', 'z') 

313 

314 def testLoadBC03(self): 

315 """Test Loader for Bruzual and Charlot Galaxies""" 

316 loadTestBC03 = matchGalaxy(galDir = self.testSpecDir) 

317 testSEDs = loadTestBC03.loadBC03() 

318 

319 # Read in a list of the SEDs in the test galaxy sed directory 

320 testGalList = os.listdir(self.testSpecDir) 

321 

322 # Make sure the names of seds in folder and set that was read in are the same 

323 # This also tests that the name attribute is assigned to each Spectrum object correctly 

324 testNames = [] 

325 for testSED in testSEDs: 

326 testNames.append(testSED.name) 

327 

328 # Python 3 replaces assertItemsEqual() with assertCountEqual() 

329 if hasattr(self, 'assertItemsEqual'): 

330 self.assertItemsEqual(testGalList, testNames) 

331 else: 

332 self.assertCountEqual(testGalList, testNames) 

333 

334 # Test same condition if a subset is provided 

335 testSubsetList = testGalList[0:2] 

336 testSEDsubset = loadTestBC03.loadBC03(subset = testSubsetList) 

337 testSubsetNames = [] 

338 for testSED in testSEDsubset: 

339 testSubsetNames.append(testSED.name) 

340 

341 # Python 3 replaces assertItemsEqual() with assertCountEqual() 

342 if hasattr(self, 'assertItemsEqual'): 

343 self.assertItemsEqual(testSubsetList, testSubsetNames) 

344 else: 

345 self.assertCountEqual(testSubsetList, testSubsetNames) 

346 

347 # Test that attributes have been assigned 

348 for testSED in testSEDsubset: 

349 self.assertIsNotNone(testSED.name) 

350 self.assertIsNotNone(testSED.type) 

351 self.assertIsNotNone(testSED.age) 

352 self.assertIsNotNone(testSED.metallicity) 

353 

354 @classmethod 

355 def tearDownClass(cls): 

356 sims_clean_up() 

357 del cls.testSpecDir 

358 

359 

360class TestSelectGalaxySED(unittest.TestCase): 

361 

362 @classmethod 

363 def setUpClass(cls): 

364 

365 # Set up Test Spectra Directory 

366 cls.testSpecDir = os.path.join(getPackageDir('sims_photUtils'), 'tests/cartoonSedTestData/galaxySed/') 

367 

368 def testMatchToRestFrame(self): 

369 """Test that Galaxies with no effects added into catalog mags are matched correctly.""" 

370 rng = np.random.RandomState(42) 

371 galPhot = BandpassDict.loadTotalBandpassesFromFiles() 

372 

373 imSimBand = Bandpass() 

374 imSimBand.imsimBandpass() 

375 

376 testMatching = selectGalaxySED(galDir = self.testSpecDir) 

377 testSEDList = testMatching.loadBC03() 

378 

379 testSEDNames = [] 

380 testMags = [] 

381 testMagNormList = [] 

382 magNormStep = 1 

383 

384 for testSED in testSEDList: 

385 

386 getSEDMags = Sed() 

387 testSEDNames.append(testSED.name) 

388 getSEDMags.setSED(wavelen = testSED.wavelen, flambda = testSED.flambda) 

389 testMagNorm = np.round(rng.uniform(20.0, 22.0), magNormStep) 

390 testMagNormList.append(testMagNorm) 

391 fluxNorm = getSEDMags.calcFluxNorm(testMagNorm, imSimBand) 

392 getSEDMags.multiplyFluxNorm(fluxNorm) 

393 testMags.append(galPhot.magListForSed(getSEDMags)) 

394 

395 # Also testing to make sure passing in non-default bandpasses works 

396 # Substitute in nan values to simulate incomplete data. 

397 testMags[0][1] = np.nan 

398 testMags[0][2] = np.nan 

399 testMags[0][4] = np.nan 

400 testMags[1][1] = np.nan 

401 testMatchingResults = testMatching.matchToRestFrame(testSEDList, testMags, 

402 bandpassDict = galPhot) 

403 self.assertEqual(None, testMatchingResults[0][0]) 

404 self.assertEqual(testSEDNames[1:], testMatchingResults[0][1:]) 

405 self.assertEqual(None, testMatchingResults[1][0]) 

406 np.testing.assert_almost_equal(testMagNormList[1:], testMatchingResults[1][1:], decimal = magNormStep) 

407 

408 # Test Match Errors 

409 errMags = np.array((testMags[2], testMags[2], testMags[2], testMags[2])) 

410 errMags[1, 1] += 1. # Total MSE will be 2/(5 colors) = 0.4 

411 errMags[2, 0:2] = np.nan 

412 errMags[2, 3] += 1. # Total MSE will be 2/(3 colors) = 0.667 

413 errMags[3, :] = None 

414 errSED = testSEDList[2] 

415 testMatchingResultsErrors = testMatching.matchToRestFrame([errSED], errMags, 

416 bandpassDict = galPhot) 

417 np.testing.assert_almost_equal(np.array((0.0, 0.4, 2./3.)), testMatchingResultsErrors[2][0:3], 

418 decimal = 3) 

419 self.assertEqual(None, testMatchingResultsErrors[2][3]) 

420 

421 def testReddeningException(self): 

422 """Test that if reddening=True in matchToObserved CatRA & CatDec are defined or exception is raised""" 

423 testException = selectGalaxySED(galDir = self.testSpecDir) 

424 testSEDList = testException.loadBC03() 

425 magnitudes = [[1.0, 2.0, 3.0, 4.0, 5.0], [1.0, 2.0, 3.0, 4.0, 5.0]] 

426 redshifts = [1.0, 1.0] 

427 self.assertRaises(RuntimeError, testException.matchToObserved, testSEDList, magnitudes, redshifts, 

428 reddening = True) 

429 

430 def testMatchToObserved(self): 

431 """Test that Galaxy SEDs with extinction or redshift are matched correctly""" 

432 rng = np.random.RandomState(42) 

433 galPhot = BandpassDict.loadTotalBandpassesFromFiles() 

434 

435 imSimBand = Bandpass() 

436 imSimBand.imsimBandpass() 

437 

438 testMatching = selectGalaxySED(galDir = self.testSpecDir) 

439 testSEDList = testMatching.loadBC03() 

440 

441 testSEDNames = [] 

442 testRA = [] 

443 testDec = [] 

444 testRedshifts = [] 

445 testMagNormList = [] 

446 magNormStep = 1 

447 extCoeffs = [1.8140, 1.4166, 0.9947, 0.7370, 0.5790, 0.4761] 

448 testMags = [] 

449 testMagsRedshift = [] 

450 testMagsExt = [] 

451 

452 for testSED in testSEDList: 

453 

454 # As a check make sure that it matches when no extinction and no redshift are present 

455 getSEDMags = Sed() 

456 testSEDNames.append(testSED.name) 

457 getSEDMags.setSED(wavelen = testSED.wavelen, flambda = testSED.flambda) 

458 testMags.append(galPhot.magListForSed(getSEDMags)) 

459 

460 # Check Extinction corrections 

461 sedRA = rng.uniform(10, 170) 

462 sedDec = rng.uniform(10, 80) 

463 testRA.append(sedRA) 

464 testDec.append(sedDec) 

465 raDec = np.array((sedRA, sedDec)).reshape((2, 1)) 

466 ebvVal = ebv().calculateEbv(equatorialCoordinates = raDec) 

467 extVal = ebvVal*extCoeffs 

468 testMagsExt.append(galPhot.magListForSed(getSEDMags) + extVal) 

469 

470 # Setup magnitudes for testing matching to redshifted values 

471 getRedshiftMags = Sed() 

472 testZ = np.round(rng.uniform(1.1, 1.3), 3) 

473 testRedshifts.append(testZ) 

474 testMagNorm = np.round(rng.uniform(20.0, 22.0), magNormStep) 

475 testMagNormList.append(testMagNorm) 

476 getRedshiftMags.setSED(wavelen = testSED.wavelen, flambda = testSED.flambda) 

477 getRedshiftMags.redshiftSED(testZ) 

478 fluxNorm = getRedshiftMags.calcFluxNorm(testMagNorm, imSimBand) 

479 getRedshiftMags.multiplyFluxNorm(fluxNorm) 

480 testMagsRedshift.append(galPhot.magListForSed(getRedshiftMags)) 

481 

482 # Will also test in passing of non-default bandpass 

483 testNoExtNoRedshift = testMatching.matchToObserved(testSEDList, testMags, np.zeros(8), 

484 reddening = False, 

485 bandpassDict = galPhot) 

486 testMatchingEbvVals = testMatching.matchToObserved(testSEDList, testMagsExt, np.zeros(8), 

487 catRA = testRA, catDec = testDec, 

488 reddening = True, extCoeffs = extCoeffs, 

489 bandpassDict = galPhot) 

490 # Substitute in nan values to simulate incomplete data and make sure magnorm works too. 

491 testMagsRedshift[0][1] = np.nan 

492 testMagsRedshift[0][3] = np.nan 

493 testMagsRedshift[0][4] = np.nan 

494 testMagsRedshift[1][1] = np.nan 

495 testMatchingRedshift = testMatching.matchToObserved(testSEDList, testMagsRedshift, testRedshifts, 

496 dzAcc = 3, reddening = False, 

497 bandpassDict = galPhot) 

498 

499 self.assertEqual(testSEDNames, testNoExtNoRedshift[0]) 

500 self.assertEqual(testSEDNames, testMatchingEbvVals[0]) 

501 self.assertEqual(None, testMatchingRedshift[0][0]) 

502 self.assertEqual(testSEDNames[1:], testMatchingRedshift[0][1:]) 

503 self.assertEqual(None, testMatchingRedshift[1][0]) 

504 np.testing.assert_almost_equal(testMagNormList[1:], testMatchingRedshift[1][1:], 

505 decimal = magNormStep) 

506 

507 # Test Match Errors 

508 errMag = testMagsRedshift[2] 

509 errRedshift = testRedshifts[2] 

510 errMags = np.array((errMag, errMag, errMag, errMag)) 

511 errRedshifts = np.array((errRedshift, errRedshift, errRedshift, errRedshift)) 

512 errMags[1, 1] += 1. # Total MSE will be 2/(5 colors) = 0.4 

513 errMags[2, 0:2] = np.nan 

514 errMags[2, 3] += 1. # Total MSE will be 2/(3 colors) = 0.667 

515 errMags[3, :] = None 

516 errSED = testSEDList[2] 

517 testMatchingResultsErrors = testMatching.matchToObserved([errSED], errMags, errRedshifts, 

518 reddening = False, 

519 bandpassDict = galPhot, 

520 dzAcc = 3) 

521 np.testing.assert_almost_equal(np.array((0.0, 0.4, 2./3.)), testMatchingResultsErrors[2][0:3], 

522 decimal = 2) # Give a little more leeway due to redshifting effects 

523 self.assertEqual(None, testMatchingResultsErrors[2][3]) 

524 

525 @classmethod 

526 def tearDownClass(cls): 

527 sims_clean_up() 

528 del cls.testSpecDir 

529 

530 

531class TestSelectStarSED(unittest.TestCase): 

532 

533 @classmethod 

534 def setUpClass(cls): 

535 

536 # Left this in after removing loading SEDs so that we can make sure that if the structure of 

537 # sims_sed_library changes in a way that affects testMatchSEDs we can detect it. 

538 

539 cls.kmTestName = 'km99_9999.fits_g99_9999' 

540 cls.mTestName = 'm99.99Full.dat' 

541 

542 # Set up Test Spectra Directory 

543 cls.testSpecDir = os.path.join(getPackageDir('sims_photUtils'), 'tests/cartoonSedTestData/starSed/') 

544 cls.testKDir = str(cls.testSpecDir + 'kurucz/') 

545 cls.testMLTDir = str(cls.testSpecDir + 'mlt/') 

546 cls.testWDDir = str(cls.testSpecDir + 'wDs/') 

547 

548 def testReddeningException(self): 

549 """Test that if reddening=True in matchToObserved CatRA & CatDec are defined or exception is raised""" 

550 testException = selectStarSED(kuruczDir=self.testKDir, 

551 mltDir=self.testMLTDir, 

552 wdDir=self.testWDDir) 

553 testSEDList = testException.loadKuruczSEDs() 

554 magnitudes = [[1.0, 2.0, 3.0, 4.0, 5.0], [1.0, 2.0, 3.0, 4.0, 5.0]] 

555 self.assertRaises(RuntimeError, testException.findSED, testSEDList, magnitudes, 

556 reddening = True) 

557 

558 def testFindSED(self): 

559 """Pull SEDs from each type and make sure that each SED gets matched to itself. 

560 Includes testing with extinction and passing in only colors.""" 

561 rng = np.random.RandomState(42) 

562 bandpassDir = os.path.join(lsst.utils.getPackageDir('throughputs'), 'sdss') 

563 starPhot = BandpassDict.loadTotalBandpassesFromFiles(('u', 'g', 'r', 'i', 'z'), 

564 bandpassDir = bandpassDir, 

565 bandpassRoot = 'sdss_') 

566 

567 imSimBand = Bandpass() 

568 imSimBand.imsimBandpass() 

569 

570 testMatching = selectStarSED(kuruczDir=self.testKDir, 

571 mltDir=self.testMLTDir, 

572 wdDir=self.testWDDir) 

573 testSEDList = [] 

574 testSEDList.append(testMatching.loadKuruczSEDs()) 

575 testSEDList.append(testMatching.loadmltSEDs()) 

576 testSEDListH, testSEDListHE = testMatching.loadwdSEDs() 

577 testSEDList.append(testSEDListH) 

578 testSEDList.append(testSEDListHE) 

579 

580 testSEDNames = [] 

581 testMags = [] 

582 testMagNormList = [] 

583 magNormStep = 1 

584 

585 for typeList in testSEDList: 

586 if len(typeList) != 0: 

587 typeSEDNames = [] 

588 typeMags = [] 

589 typeMagNorms = [] 

590 for testSED in typeList: 

591 getSEDMags = Sed() 

592 typeSEDNames.append(testSED.name) 

593 getSEDMags.setSED(wavelen = testSED.wavelen, flambda = testSED.flambda) 

594 testMagNorm = np.round(rng.uniform(20.0, 22.0), magNormStep) 

595 typeMagNorms.append(testMagNorm) 

596 fluxNorm = getSEDMags.calcFluxNorm(testMagNorm, imSimBand) 

597 getSEDMags.multiplyFluxNorm(fluxNorm) 

598 typeMags.append(starPhot.magListForSed(getSEDMags)) 

599 testSEDNames.append(typeSEDNames) 

600 testMags.append(typeMags) 

601 testMagNormList.append(typeMagNorms) 

602 

603 # Since default bandpassDict should be SDSS ugrizy shouldn't need to specify it 

604 # Substitute in nan values to simulate incomplete data. 

605 for typeList, names, mags, magNorms in zip(testSEDList, testSEDNames, testMags, testMagNormList): 

606 if len(typeList) > 2: 

607 nanMags = np.array(mags) 

608 nanMags[0][0] = np.nan 

609 nanMags[0][2] = np.nan 

610 nanMags[0][3] = np.nan 

611 nanMags[1][1] = np.nan 

612 testMatchingResults = testMatching.findSED(typeList, nanMags, reddening = False) 

613 self.assertEqual(None, testMatchingResults[0][0]) 

614 self.assertEqual(names[1:], testMatchingResults[0][1:]) 

615 self.assertEqual(None, testMatchingResults[1][0]) 

616 np.testing.assert_almost_equal(magNorms[1:], testMatchingResults[1][1:], 

617 decimal = magNormStep) 

618 else: 

619 testMatchingResults = testMatching.findSED(typeList, mags, reddening = False) 

620 self.assertEqual(names, testMatchingResults[0]) 

621 np.testing.assert_almost_equal(magNorms, testMatchingResults[1], decimal = magNormStep) 

622 

623 # Test Null Values option 

624 nullMags = np.array(testMags[0]) 

625 nullMags[0][0] = -99. 

626 nullMags[0][4] = -99. 

627 nullMags[1][0] = -99. 

628 nullMags[1][1] = -99. 

629 testMatchingResultsNull = testMatching.findSED(testSEDList[0], nullMags, 

630 nullValues = -99., reddening = False) 

631 self.assertEqual(testSEDNames[0], testMatchingResultsNull[0]) 

632 np.testing.assert_almost_equal(testMagNormList[0], testMatchingResultsNull[1], 

633 decimal = magNormStep) 

634 

635 # Test Error Output 

636 errMags = np.array((testMags[0][0], testMags[0][0], testMags[0][0], testMags[0][0])) 

637 errMags[1, 1] += 1. # Total MSE will be 2/(4 colors) = 0.5 

638 errMags[2, 0:2] = np.nan 

639 errMags[2, 3] += 1. # Total MSE will be 2/(2 colors) = 1.0 

640 errMags[3, :] = None 

641 errSED = testSEDList[0][0] 

642 testMatchingResultsErrors = testMatching.findSED([errSED], errMags, reddening = False) 

643 np.testing.assert_almost_equal(np.array((0.0, 0.5, 1.0)), testMatchingResultsErrors[2][0:3], 

644 decimal = 3) 

645 self.assertEqual(None, testMatchingResultsErrors[2][3]) 

646 

647 # Now test what happens if we pass in a bandpassDict 

648 testMatchingResultsNoDefault = testMatching.findSED(testSEDList[0], testMags[0], 

649 bandpassDict = starPhot, 

650 reddening = False) 

651 self.assertEqual(testSEDNames[0], testMatchingResultsNoDefault[0]) 

652 np.testing.assert_almost_equal(testMagNormList[0], testMatchingResultsNoDefault[1], 

653 decimal = magNormStep) 

654 

655 # Test Reddening 

656 testRA = rng.uniform(10, 170, len(testSEDList[0])) 

657 testDec = rng.uniform(10, 80, len(testSEDList[0])) 

658 extFactor = .5 

659 raDec = np.array((testRA, testDec)) 

660 ebvVals = ebv().calculateEbv(equatorialCoordinates = raDec) 

661 extVals = ebvVals*extFactor 

662 testRedMags = [] 

663 for extVal, testMagSet in zip(extVals, testMags[0]): 

664 testRedMags.append(testMagSet + extVal) 

665 testMatchingResultsRed = testMatching.findSED(testSEDList[0], testRedMags, catRA = testRA, 

666 catDec = testDec, reddening = True, 

667 extCoeffs = np.ones(5)*extFactor) 

668 self.assertEqual(testSEDNames[0], testMatchingResultsRed[0]) 

669 np.testing.assert_almost_equal(testMagNormList[0], testMatchingResultsRed[1], 

670 decimal = magNormStep) 

671 

672 # Finally, test color input 

673 testColors = [] 

674 for testMagSet in testMags[0]: 

675 testColorSet = [] 

676 for filtNum in range(0, len(starPhot)-1): 

677 testColorSet.append(testMagSet[filtNum] - testMagSet[filtNum+1]) 

678 testColors.append(testColorSet) 

679 testMatchingColorsInput = testMatching.findSED(testSEDList[0], testMags[0], 

680 reddening = False, colors = testColors) 

681 self.assertEqual(testSEDNames[0], testMatchingColorsInput[0]) 

682 np.testing.assert_almost_equal(testMagNormList[0], testMatchingColorsInput[1], 

683 decimal = magNormStep) 

684 

685 @classmethod 

686 def tearDownClass(cls): 

687 sims_clean_up() 

688 del cls.testSpecDir 

689 del cls.testKDir 

690 del cls.testMLTDir 

691 del cls.testWDDir 

692 

693 del cls.kmTestName 

694 del cls.mTestName 

695 

696 

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

698 pass 

699 

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

701 lsst.utils.tests.init() 

702 unittest.main()