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

1import unittest 

2import copy 

3 

4import astropy.units as u 

5 

6import lsst.utils.tests 

7import lsst.afw.geom 

8import lsst.pipe.base as pipeBase 

9from lsst.meas.algorithms import LoadReferenceObjectsTask, getRefFluxField, ReferenceObjectLoader 

10from lsst.pipe.tasks.loadReferenceCatalog import LoadReferenceCatalogConfig, LoadReferenceCatalogTask 

11from lsst.pipe.tasks.colorterms import Colorterm, ColortermDict, ColortermLibrary 

12 

13 

14synthTerms = ColortermLibrary(data={ 

15 "synth*": ColortermDict(data={ 

16 "filter1": Colorterm(primary="ref1", secondary="ref2", c0=0.0, c1=0.01), 

17 "filter2": Colorterm(primary="ref2", secondary="ref3", c0=0.0, c1=-0.01), 

18 }) 

19}) 

20 

21 

22_synthFlux = 100.0 

23_synthCenter = lsst.geom.SpherePoint(30, -30, lsst.geom.degrees) 

24 

25 

26def setup_module(module): 

27 lsst.utils.tests.init() 

28 

29 

30class TrivialLoader(ReferenceObjectLoader): 

31 """Minimal subclass of LoadReferenceObjectsTask""" 

32 def make_synthetic_refcat(self, center, flux): 

33 """Make a synthetic reference catalog.""" 

34 filters = ["ref1", "ref2", "ref3"] 

35 schema = LoadReferenceObjectsTask.makeMinimalSchema(filters) 

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

37 record = catalog.addNew() 

38 record.setCoord(center) 

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

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

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

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

43 record[filters[2] + '_flux'] = flux*100 

44 record[filters[2] + '_fluxErr'] = flux*100*0.1 

45 

46 return catalog 

47 

48 def loadSkyCircle(self, ctrCoord, radius, filterName, **kwargs): 

49 refCat = self.make_synthetic_refcat(_synthCenter, _synthFlux) 

50 fluxField = getRefFluxField(schema=refCat.schema, filterName=filterName) 

51 return pipeBase.Struct( 

52 refCat=self.make_synthetic_refcat(_synthCenter, _synthFlux), 

53 fluxField=fluxField 

54 ) 

55 

56 def loadPixelBox(self, bbox, wcs, referenceFilter, **kwargs): 

57 return self.loadSkyCircle(None, None, referenceFilter) 

58 

59 

60class LoadReferenceCatalogTestCase(lsst.utils.tests.TestCase): 

61 @classmethod 

62 def setUpClass(cls): 

63 cls.config = LoadReferenceCatalogConfig() 

64 cls.config.refObjLoader.filterMap = {"filter1": "ref1", 

65 "filter2": "ref2"} 

66 cls.config.refObjLoader.ref_dataset_name = 'synthCam' 

67 cls.config.colorterms = synthTerms 

68 cls.config.referenceSelector.doSignalToNoise = True 

69 cls.config.referenceSelector.signalToNoise.fluxField = 'ref1_flux' 

70 cls.config.referenceSelector.signalToNoise.errField = 'ref1_fluxErr' 

71 cls.config.referenceSelector.signalToNoise.minimum = 20.0 

72 

73 cls.config.doApplyColorTerms = False 

74 cls.config.doReferenceSelection = False 

75 

76 cls.synthMag1 = (_synthFlux*u.nanojansky).to(u.ABmag).value 

77 cls.synthMag2 = ((_synthFlux*10)*u.nanojansky).to(u.ABmag).value 

78 cls.synthMag3 = ((_synthFlux*100)*u.nanojansky).to(u.ABmag).value 

79 

80 cls.synthMag1Corr = cls.synthMag1 + 0.01*(cls.synthMag1 - cls.synthMag2) 

81 cls.synthMag2Corr = cls.synthMag2 - 0.01*(cls.synthMag2 - cls.synthMag3) 

82 

83 cls.trivialLoader = TrivialLoader(dataIds=[], refCats=[], config=cls.config.refObjLoader) 

84 

85 def testGetReferenceCatalogCircle(self): 

86 """Get a reference catalog skycircle.""" 

87 config = copy.copy(self.config) 

88 

89 loaderTask = LoadReferenceCatalogTask(config=config, dataIds=[], refCats=[]) 

90 # Monkey-patch our testing trivial loader to bypass the butler 

91 loaderTask.refObjLoader = self.trivialLoader 

92 

93 cat = loaderTask.getSkyCircleCatalog(_synthCenter, 

94 1.0*lsst.geom.degrees, 

95 ['filter1', 'filter2']) 

96 

97 self.assertAlmostEqual(cat['ra'], _synthCenter.getRa().asDegrees()) 

98 self.assertAlmostEqual(cat['dec'], _synthCenter.getDec().asDegrees()) 

99 self.assertFloatsAlmostEqual(cat['refMag'][0, 0], self.synthMag1, rtol=1e-7) 

100 self.assertFloatsAlmostEqual(cat['refMag'][0, 1], self.synthMag2, rtol=1e-7) 

101 

102 def testGetReferenceCatalogBox(self): 

103 """Get a reference catalog box.""" 

104 config = copy.copy(self.config) 

105 

106 loaderTask = LoadReferenceCatalogTask(config=config, dataIds=[], refCats=[]) 

107 # Monkey-patch our testing trivial loader to bypass the butler 

108 loaderTask.refObjLoader = self.trivialLoader 

109 

110 bbox = lsst.geom.Box2I(corner=lsst.geom.Point2I(0, 0), 

111 dimensions=lsst.geom.Extent2I(100, 100)) 

112 crpix = lsst.geom.Point2D(50, 50) 

113 crval = _synthCenter 

114 cdMatrix = lsst.afw.geom.makeCdMatrix(scale=1.0*lsst.geom.arcseconds) 

115 wcs = lsst.afw.geom.makeSkyWcs(crpix, crval, cdMatrix) 

116 

117 cat = loaderTask.getPixelBoxCatalog(bbox, 

118 wcs, 

119 ['filter1', 'filter2']) 

120 

121 self.assertAlmostEqual(cat['ra'], _synthCenter.getRa().asDegrees()) 

122 self.assertAlmostEqual(cat['dec'], _synthCenter.getDec().asDegrees()) 

123 self.assertFloatsAlmostEqual(cat['refMag'][0, 0], self.synthMag1, rtol=1e-7) 

124 self.assertFloatsAlmostEqual(cat['refMag'][0, 1], self.synthMag2, rtol=1e-7) 

125 

126 def testGetReferenceCatalogCircleColorterms(self): 

127 """Get a reference catalog circle, with color terms applied.""" 

128 config = copy.copy(self.config) 

129 config.doApplyColorTerms = True 

130 

131 loaderTask = LoadReferenceCatalogTask(config=config, dataIds=[], refCats=[]) 

132 # Monkey-patch our testing trivial loader to bypass the butler 

133 loaderTask.refObjLoader = self.trivialLoader 

134 

135 cat = loaderTask.getSkyCircleCatalog(_synthCenter, 

136 1.0*lsst.geom.degrees, 

137 ['filter1', 'filter2']) 

138 

139 self.assertAlmostEqual(cat['ra'], _synthCenter.getRa().asDegrees()) 

140 self.assertAlmostEqual(cat['dec'], _synthCenter.getDec().asDegrees()) 

141 self.assertFloatsAlmostEqual(cat['refMag'][0, 0], self.synthMag1Corr, rtol=1e-7) 

142 self.assertFloatsAlmostEqual(cat['refMag'][0, 1], self.synthMag2Corr, rtol=1e-7) 

143 

144 def testGetReferenceCatalogCircleSelection(self): 

145 """Get a reference catalog circle, apply selection.""" 

146 config = copy.copy(self.config) 

147 config.doReferenceSelection = True 

148 

149 loaderTask = LoadReferenceCatalogTask(config=config, dataIds=[], refCats=[]) 

150 # Monkey-patch our testing trivial loader to bypass the butler 

151 loaderTask.refObjLoader = self.trivialLoader 

152 

153 cat = loaderTask.getSkyCircleCatalog(_synthCenter, 

154 1.0*lsst.geom.degrees, 

155 ['filter1', 'filter2']) 

156 

157 # The selection removed all the objects. 

158 self.assertEqual(len(cat), 0) 

159 

160 def testGetReferenceCatalogCircleSingleFilter(self): 

161 """Get a reference catalog circle, single filter.""" 

162 config = copy.copy(self.config) 

163 

164 loaderTask = LoadReferenceCatalogTask(config=config, dataIds=[], refCats=[]) 

165 # Monkey-patch our testing trivial loader to bypass the butler 

166 loaderTask.refObjLoader = self.trivialLoader 

167 

168 cat = loaderTask.getSkyCircleCatalog(_synthCenter, 

169 1.0*lsst.geom.degrees, 

170 ['filter1']) 

171 

172 self.assertAlmostEqual(cat['ra'], _synthCenter.getRa().asDegrees()) 

173 self.assertAlmostEqual(cat['dec'], _synthCenter.getDec().asDegrees()) 

174 self.assertFloatsAlmostEqual(cat['refMag'][0, 0], self.synthMag1, rtol=1e-7) 

175 

176 def testGetReferenceCatalogAnyFilter(self): 

177 """Get a reference catalog circle, using anyFilterMapsToThis.""" 

178 config = LoadReferenceCatalogConfig() 

179 config.refObjLoader.anyFilterMapsToThis = 'ref1' 

180 config.refObjLoader.ref_dataset_name = 'synthCam' 

181 

182 config.doApplyColorTerms = False 

183 config.doReferenceSelection = False 

184 

185 loaderTask = LoadReferenceCatalogTask(config=config, dataIds=[], refCats=[]) 

186 # Monkey-patch our testing trivial loader to bypass the butler 

187 loaderTask.refObjLoader = self.trivialLoader 

188 

189 cat = loaderTask.getSkyCircleCatalog(_synthCenter, 

190 1.0*lsst.geom.degrees, 

191 ['filter1', 'filter2']) 

192 

193 self.assertFloatsAlmostEqual(cat['refMag'][0, 0], self.synthMag1, rtol=1e-7) 

194 self.assertFloatsAlmostEqual(cat['refMag'][0, 1], self.synthMag1, rtol=1e-7) 

195 

196 

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

198 pass 

199 

200 

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

202 lsst.utils.tests.init() 

203 unittest.main()