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

1# 

2# LSST Data Management System 

3# 

4# Copyright 2008-2016 AURA/LSST. 

5# 

6# This product includes software developed by the 

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

8# 

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

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

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

12# (at your option) any later version. 

13# 

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

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

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

17# GNU General Public License for more details. 

18# 

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

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

21# see <https://www.lsstcorp.org/LegalNotices/>. 

22# 

23 

24import itertools 

25import unittest 

26 

27import lsst.afw.table as afwTable 

28import lsst.log 

29from lsst.meas.algorithms import LoadReferenceObjectsTask, getRefFluxField, getRefFluxKeys 

30from lsst.meas.algorithms.loadReferenceObjects import hasNanojanskyFluxUnits, convertToNanojansky 

31import lsst.utils.tests 

32 

33 

34class TrivialLoader(LoadReferenceObjectsTask): 

35 """Minimal subclass of LoadReferenceObjectsTask to allow instantiation 

36 """ 

37 

38 def loadSkyCircle(self, ctrCoord, radius, filterName): 

39 pass 

40 

41 

42class TestLoadReferenceObjects(lsst.utils.tests.TestCase): 

43 """Test case for LoadReferenceObjectsTask abstract base class 

44 

45 Only methods with concrete implementations are tested (hence not loadSkyCircle) 

46 """ 

47 

48 def testMakeMinimalSchema(self): 

49 """Make a schema and check it.""" 

50 for filterNameList in (["r"], ["foo", "_bar"]): 

51 for (addIsPhotometric, addIsResolved, addIsVariable, 

52 coordErrDim, addProperMotion, properMotionErrDim, 

53 addParallax) in itertools.product( 

54 (False, True), (False, True), (False, True), 

55 (-1, 0, 1, 2, 3, 4), (False, True), (-1, 0, 1, 2, 3, 4), 

56 (False, True)): 

57 argDict = dict( 

58 filterNameList=filterNameList, 

59 addIsPhotometric=addIsPhotometric, 

60 addIsResolved=addIsResolved, 

61 addIsVariable=addIsVariable, 

62 coordErrDim=coordErrDim, 

63 addProperMotion=addProperMotion, 

64 properMotionErrDim=properMotionErrDim, 

65 addParallax=addParallax, 

66 ) 

67 if coordErrDim not in (0, 2, 3) or \ 

68 (addProperMotion and properMotionErrDim not in (0, 2, 3)): 

69 with self.assertRaises(ValueError): 

70 LoadReferenceObjectsTask.makeMinimalSchema(**argDict) 

71 else: 

72 refSchema = LoadReferenceObjectsTask.makeMinimalSchema(**argDict) 

73 self.assertTrue("coord_ra" in refSchema) 

74 self.assertTrue("coord_dec" in refSchema) 

75 for filterName in filterNameList: 

76 fluxField = filterName + "_flux" 

77 self.assertIn(fluxField, refSchema) 

78 self.assertNotIn("x" + fluxField, refSchema) 

79 fluxErrField = fluxField + "Err" 

80 self.assertIn(fluxErrField, refSchema) 

81 self.assertEqual(getRefFluxField(refSchema, filterName), filterName + "_flux") 

82 self.assertEqual("resolved" in refSchema, addIsResolved) 

83 self.assertEqual("variable" in refSchema, addIsVariable) 

84 self.assertEqual("photometric" in refSchema, addIsPhotometric) 

85 self.assertEqual("photometric" in refSchema, addIsPhotometric) 

86 self.assertEqual("epoch" in refSchema, addProperMotion or addParallax) 

87 self.assertEqual("coord_raErr" in refSchema, coordErrDim > 0) 

88 self.assertEqual("coord_decErr" in refSchema, coordErrDim > 0) 

89 self.assertEqual("coord_ra_dec_Cov" in refSchema, coordErrDim == 3) 

90 self.assertEqual("pm_ra" in refSchema, addProperMotion) 

91 self.assertEqual("pm_dec" in refSchema, addProperMotion) 

92 self.assertEqual("pm_raErr" in refSchema, addProperMotion and properMotionErrDim > 0) 

93 self.assertEqual("pm_decErr" in refSchema, addProperMotion and properMotionErrDim > 0) 

94 self.assertEqual("pm_flag" in refSchema, addProperMotion) 

95 self.assertEqual("pm_ra_dec_Cov" in refSchema, 

96 addProperMotion and properMotionErrDim == 3) 

97 self.assertEqual("parallax" in refSchema, addParallax) 

98 self.assertEqual("parallaxErr" in refSchema, addParallax) 

99 self.assertEqual("parallax_flag" in refSchema, addParallax) 

100 

101 def testFilterAliasMap(self): 

102 """Make a schema with filter aliases.""" 

103 for defaultFilter in ("", "r", "camr"): 

104 for filterMap in ({}, {"camr": "r"}): 

105 config = TrivialLoader.ConfigClass() 

106 config.defaultFilter = defaultFilter 

107 config.filterMap = filterMap 

108 loader = TrivialLoader(config=config) 

109 refSchema = TrivialLoader.makeMinimalSchema(filterNameList="r") 

110 try: 

111 loader._addFluxAliases(refSchema) 

112 self.assertNotEqual(defaultFilter, "camr") 

113 except Exception: 

114 # only reference filters are allowed as default filters 

115 self.assertEqual(defaultFilter, "camr") 

116 continue 

117 

118 self.assertIn("r_flux", refSchema) 

119 self.assertIn("r_fluxErr", refSchema) 

120 

121 # camera filters aliases are named <filter>_camFlux 

122 if "camr" in filterMap: 

123 self.assertEqual(getRefFluxField(refSchema, "camr"), "camr_camFlux") 

124 else: 

125 with self.assertRaises(RuntimeError): 

126 getRefFluxField(refSchema, "camr") 

127 

128 # if a non-empty default filter is specified then camFlux 

129 # and camFluxErr should be present 

130 hasDefault = bool(defaultFilter) 

131 self.assertEqual("camFlux" in refSchema, hasDefault) 

132 self.assertEqual("camFluxErr" in refSchema, hasDefault) 

133 

134 refCat = afwTable.SimpleCatalog(refSchema) 

135 refObj = refCat.addNew() 

136 refObj["r_flux"] = 1.23 

137 self.assertAlmostEqual(refCat[0].get(getRefFluxField(refSchema, "r")), 1.23) 

138 if "camr" in filterMap: 

139 self.assertAlmostEqual(refCat[0].get(getRefFluxField(refSchema, "camr")), 1.23) 

140 if hasDefault: 

141 self.assertEqual(getRefFluxField(refSchema, ""), "camFlux") 

142 self.assertAlmostEqual(refCat[0].get(getRefFluxField(refSchema, "")), 1.23) 

143 refObj["r_fluxErr"] = 0.111 

144 if "camr" in filterMap: 

145 self.assertEqual(refCat[0].get("camr_camFluxErr"), 0.111) 

146 fluxKey, fluxErrKey = getRefFluxKeys(refSchema, "r") 

147 self.assertEqual(refCat[0].get(fluxKey), 1.23) 

148 self.assertEqual(refCat[0].get(fluxErrKey), 0.111) 

149 if "camr" in filterMap: 

150 fluxKey, fluxErrKey = getRefFluxKeys(refSchema, "camr") 

151 self.assertEqual(refCat[0].get(fluxErrKey), 0.111) 

152 else: 

153 with self.assertRaises(RuntimeError): 

154 getRefFluxKeys(refSchema, "camr") 

155 

156 def testCheckFluxUnits(self): 

157 """Test that we can identify old style fluxes in a schema.""" 

158 schema = LoadReferenceObjectsTask.makeMinimalSchema(['r', 'z']) 

159 # the default schema should pass 

160 self.assertTrue(hasNanojanskyFluxUnits(schema)) 

161 schema.addField('bad_fluxSigma', doc='old flux units', type=float, units='') 

162 self.assertFalse(hasNanojanskyFluxUnits(schema)) 

163 

164 schema = LoadReferenceObjectsTask.makeMinimalSchema(['r', 'z']) 

165 schema.addField('bad_flux', doc='old flux units', type=float, units='') 

166 self.assertFalse(hasNanojanskyFluxUnits(schema)) 

167 

168 schema = LoadReferenceObjectsTask.makeMinimalSchema(['r', 'z']) 

169 schema.addField('bad_flux', doc='old flux units', type=float, units='Jy') 

170 self.assertFalse(hasNanojanskyFluxUnits(schema)) 

171 

172 schema = LoadReferenceObjectsTask.makeMinimalSchema(['r', 'z']) 

173 schema.addField('bad_fluxErr', doc='old flux units', type=float, units='') 

174 self.assertFalse(hasNanojanskyFluxUnits(schema)) 

175 

176 schema = LoadReferenceObjectsTask.makeMinimalSchema(['r', 'z']) 

177 schema.addField('bad_fluxErr', doc='old flux units', type=float, units='Jy') 

178 self.assertFalse(hasNanojanskyFluxUnits(schema)) 

179 

180 schema = LoadReferenceObjectsTask.makeMinimalSchema(['r', 'z']) 

181 schema.addField('bad_fluxSigma', doc='old flux units', type=float, units='') 

182 self.assertFalse(hasNanojanskyFluxUnits(schema)) 

183 

184 def testConvertOldFluxes(self): 

185 """Check that we can convert old style fluxes in a catalog.""" 

186 flux = 1.234 

187 fluxErr = 5.678 

188 log = lsst.log.Log() 

189 

190 def make_catalog(): 

191 schema = LoadReferenceObjectsTask.makeMinimalSchema(['r', 'z']) 

192 schema.addField('bad_flux', doc='old flux units', type=float, units='') 

193 schema.addField('bad_fluxErr', doc='old flux units', type=float, units='Jy') 

194 refCat = afwTable.SimpleCatalog(schema) 

195 refObj = refCat.addNew() 

196 refObj["bad_flux"] = flux 

197 refObj["bad_fluxErr"] = fluxErr 

198 return refCat 

199 

200 oldRefCat = make_catalog() 

201 newRefCat = convertToNanojansky(oldRefCat, log) 

202 self.assertEqual(newRefCat['bad_flux'], [flux*1e9, ]) 

203 self.assertEqual(newRefCat['bad_fluxErr'], [fluxErr*1e9, ]) 

204 self.assertEqual(newRefCat.schema['bad_flux'].asField().getUnits(), 'nJy') 

205 self.assertEqual(newRefCat.schema['bad_fluxErr'].asField().getUnits(), 'nJy') 

206 

207 # check that doConvert=False returns None (it also logs a summary) 

208 oldRefCat = make_catalog() 

209 newRefCat = convertToNanojansky(oldRefCat, log, doConvert=False) 

210 self.assertIsNone(newRefCat) 

211 

212 

213class TestMemory(lsst.utils.tests.MemoryTestCase): 

214 pass 

215 

216 

217def setup_module(module): 

218 lsst.utils.tests.init() 

219 

220 

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

222 lsst.utils.tests.init() 

223 unittest.main()