Coverage for tests/test_colorterm.py: 29%

70 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-23 17:26 -0700

1# 

2# LSST Data Management System 

3# Copyright 2008, 2009, 2010 LSST Corporation. 

4# 

5# This product includes software developed by the 

6# LSST Project (http://www.lsst.org/). 

7# 

8# This program is free software: you can redistribute it and/or modify 

9# it under the terms of the GNU General Public License as published by 

10# the Free Software Foundation, either version 3 of the License, or 

11# (at your option) any later version. 

12# 

13# This program is distributed in the hope that it will be useful, 

14# but WITHOUT ANY WARRANTY; without even the implied warranty of 

15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

16# GNU General Public License for more details. 

17# 

18# You should have received a copy of the LSST License Statement and 

19# the GNU General Public License along with this program. If not, 

20# see <http://www.lsstcorp.org/LegalNotices/>. 

21# 

22 

23import unittest 

24import pickle 

25 

26import astropy.units as u 

27 

28import lsst.utils.tests 

29from lsst.meas.algorithms import LoadReferenceObjectsTask 

30from lsst.pipe.tasks.colorterms import Colorterm, ColortermDict, ColortermLibrary, ColortermNotFoundError 

31 

32# From the last page of http://www.naoj.org/staff/nakata/suprime/illustration/colorterm_report_ver3.pdf 

33# Transformation for griz band between SDSS and SC (estimated with GS83 SEDs) 

34hamamatsu = ColortermLibrary(data={ 

35 "ham*": ColortermDict(data={ 

36 "g": Colorterm(primary="g", secondary="r", c0=-0.00928, c1=-0.0824), 

37 "r": Colorterm(primary="r", secondary="i", c0=-0.00282, c1=-0.0498, c2=-0.0149), 

38 "i": Colorterm(primary="i", secondary="z", c0=0.00186, c1=-0.140, c2=-0.0196), 

39 "z": Colorterm(primary="z", secondary="i", c0=-4.03e-4, c1=0.0967, c2=0.0210), 

40 }) 

41}) 

42 

43 

44def setup_module(module): 

45 lsst.utils.tests.init() 

46 

47 

48class ColortermTestCase(unittest.TestCase): 

49 """A test case for MaskedImage""" 

50 

51 def setUp(self): 

52 # A list of simple fake sources. The values are chosen so that the colorterm corrections are 

53 # predictable. 

54 self.sources = (dict(g=0.0, r=0.0, fluxErr_g=0.0, fluxErr_r=0.0, true_g=-0.00928, true_fluxErr_g=0.0), 

55 dict(g=0.0, r=-1.0, fluxErr_g=1.0, fluxErr_r=1.0, true_g=-0.09168, 

56 true_fluxErr_g=0.92129230974756315)) 

57 self.colorterms = hamamatsu 

58 

59 def testTransformSource(self): 

60 """Check if we can use colour terms""" 

61 

62 ct = self.colorterms.getColorterm("g", photoCatName="ham") 

63 

64 for s in self.sources: 

65 self.assertEqual(ct.transformSource(s), s["true_g"]) 

66 

67 def testLibraryAccess(self): 

68 """Test ColortermLibrary.getColorterm""" 

69 ctg = self.colorterms.getColorterm("g", photoCatName="ham") # exact match 

70 self.assertEqual(ctg.primary, "g") 

71 self.assertEqual(ctg.secondary, "r") 

72 self.assertAlmostEqual(ctg.c0, -0.00928) 

73 self.assertAlmostEqual(ctg.c1, -0.0824) 

74 self.assertAlmostEqual(ctg.c2, 0) 

75 

76 ctr = self.colorterms.getColorterm("r", photoCatName="hambone") # glob should expand 

77 self.assertEqual(ctr.primary, "r") 

78 self.assertEqual(ctr.secondary, "i") 

79 self.assertAlmostEqual(ctr.c0, -0.00282) 

80 self.assertAlmostEqual(ctr.c1, -0.0498) 

81 self.assertAlmostEqual(ctr.c2, -0.0149) 

82 

83 # bad filter name 

84 self.assertRaises(ColortermNotFoundError, self.colorterms.getColorterm, "x", photoCatName="ham") 

85 

86 # bad catalog name: not in library 

87 self.assertRaises(ColortermNotFoundError, self.colorterms.getColorterm, "r", photoCatName="eggs") 

88 

89 # bad catalog name: glob expression 

90 self.assertRaises(ColortermNotFoundError, self.colorterms.getColorterm, "r", photoCatName="ha*") 

91 

92 def testTransformMags(self): 

93 """Check if we can use colour terms via transformMags""" 

94 

95 ct = self.colorterms.getColorterm("g", photoCatName="ham") 

96 

97 for s in self.sources: 

98 self.assertEqual(ct.transformMags(s[ct.primary], s[ct.secondary]), s["true_g"]) 

99 

100 def testPropagateFluxErrors(self): 

101 """Check if we can propagate flux errors""" 

102 

103 ct = self.colorterms.getColorterm("g", photoCatName="ham") 

104 for s in self.sources: 

105 self.assertEqual(ct.propagateFluxErrors(s["fluxErr_g"], s["fluxErr_r"]), s["true_fluxErr_g"]) 

106 

107 def testPickle(self): 

108 """Ensure color terms can be pickled""" 

109 colorterms = pickle.loads(pickle.dumps(self.colorterms)) 

110 self.assertEqual(colorterms, self.colorterms) 

111 

112 

113def make_fake_refcat(center, flux): 

114 """Make a fake reference catalog.""" 

115 filters = ['f1', 'f2'] 

116 schema = LoadReferenceObjectsTask.makeMinimalSchema(filters) 

117 catalog = lsst.afw.table.SimpleCatalog(schema) 

118 record = catalog.addNew() 

119 record.setCoord(center) 

120 record[filters[0] + '_flux'] = flux 

121 record[filters[0] + '_fluxErr'] = flux*0.1 

122 record[filters[1] + '_flux'] = flux*10 

123 record[filters[1] + '_fluxErr'] = flux*10*0.1 

124 return catalog 

125 

126 

127class ApplyColortermsTestCase(lsst.utils.tests.TestCase): 

128 def setUp(self): 

129 self.colorterm = Colorterm(primary="f1", secondary="f2", c0=2.0, c1=3.0) 

130 

131 def testGetCorrectedMagnitudes(self): 

132 center = lsst.geom.SpherePoint(30, -30, lsst.geom.degrees) 

133 flux = 100 

134 refCat = make_fake_refcat(center, flux) 

135 

136 expectMag = self.colorterm.transformMags((u.nJy*refCat['f1_flux']).to_value(u.ABmag), 

137 (u.nJy*refCat['f2_flux']).to_value(u.ABmag)) 

138 refMag, refMagErr = self.colorterm.getCorrectedMagnitudes(refCat) 

139 self.assertEqual(refMag, expectMag) 

140 # TODO DM-17692: Not testing the returned errors, as I do not trust `propagateFluxErrors()` 

141 # and there is some interesting logic involved in how the errors are propagated. 

142 

143 

144class MemoryTester(lsst.utils.tests.MemoryTestCase): 

145 pass 

146 

147 

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

149 lsst.utils.tests.init() 

150 unittest.main()