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# This file is part of ap_association 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

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 GNU General Public License 

20# along with this program. If not, see <https://www.gnu.org/licenses/>. 

21 

22import os 

23import unittest 

24 

25from lsst.ap.association.transformDiaSourceCatalog import (TransformDiaSourceCatalogConfig, 

26 TransformDiaSourceCatalogTask) 

27from lsst.afw.cameraGeom.testUtils import DetectorWrapper 

28import lsst.daf.base as dafBase 

29import lsst.afw.image as afwImage 

30import lsst.geom as geom 

31import lsst.meas.base.tests as measTests 

32from lsst.pipe.base import Struct 

33from lsst.utils import getPackageDir 

34import lsst.utils.tests 

35 

36from lsst.ap.association.transformDiaSourceCatalog import UnpackApdbFlags 

37 

38 

39class TestTransformDiaSourceCatalogTask(unittest.TestCase): 

40 

41 def setUp(self): 

42 nSources = 10 

43 self.bboxSize = 18 

44 self.xyLoc = 100 

45 self.bbox = geom.Box2I(geom.Point2I(0, 0), 

46 geom.Extent2I(1024, 1153)) 

47 dataset = measTests.TestDataset(self.bbox) 

48 for srcIdx in range(nSources): 

49 dataset.addSource(100000.0, geom.Point2D(self.xyLoc, self.xyLoc)) 

50 schema = dataset.makeMinimalSchema() 

51 schema.addField("base_PixelFlags_flag", type="Flag") 

52 schema.addField("base_PixelFlags_flag_offimage", type="Flag") 

53 self.exposure, self.inputCatalog = dataset.realize(10.0, schema, randomSeed=1234) 

54 # Create schemas for use in initializing the TransformDiaSourceCatalog 

55 # task. 

56 self.initInputs = {"diaSourceSchema": Struct(schema=schema)} 

57 self.initInputsBadFlags = {"diaSourceSchema": Struct(schema=dataset.makeMinimalSchema())} 

58 

59 self.expId = 4321 

60 self.date = dafBase.DateTime(nsecs=1400000000 * 10**9) 

61 detector = DetectorWrapper(id=23, bbox=self.exposure.getBBox()).detector 

62 visit = afwImage.VisitInfo( 

63 exposureId=self.expId, 

64 exposureTime=200., 

65 date=self.date) 

66 self.exposure.setDetector(detector) 

67 self.exposure.getInfo().setVisitInfo(visit) 

68 self.filterName = 'g' 

69 self.exposure.setFilterLabel(afwImage.FilterLabel(band=self.filterName, physical='g.MP9401')) 

70 scale = 2 

71 scaleErr = 1 

72 self.photoCalib = afwImage.PhotoCalib(scale, scaleErr) 

73 self.exposure.setPhotoCalib(self.photoCalib) 

74 

75 def test_run(self): 

76 """Test output dataFrame is created and values are correctly inserted 

77 from the exposure. 

78 """ 

79 transConfig = TransformDiaSourceCatalogConfig() 

80 transConfig.flagMap = os.path.join( 

81 getPackageDir("ap_association"), 

82 "tests", 

83 "data", 

84 "test-flag-map.yaml") 

85 transConfig.functorFile = os.path.join(getPackageDir("ap_association"), 

86 "tests", 

87 "data", 

88 "testDiaSource.yaml") 

89 transformTask = TransformDiaSourceCatalogTask(initInputs=self.initInputs, 

90 config=transConfig) 

91 result = transformTask.run(self.inputCatalog, 

92 self.exposure, 

93 self.filterName, 

94 ccdVisitId=self.expId) 

95 

96 self.assertEqual(len(result.diaSourceTable), len(self.inputCatalog)) 

97 for idx, src in result.diaSourceTable.iterrows(): 

98 self.assertEqual(src["bboxSize"], self.bboxSize) 

99 self.assertEqual(src["ccdVisitId"], self.expId) 

100 self.assertEqual(src["filterName"], self.filterName) 

101 self.assertEqual(src["midPointTai"], 

102 self.date.get(system=dafBase.DateTime.MJD)) 

103 self.assertEqual(src["diaObjectId"], 0) 

104 

105 def test_run_dia_source_wrong_flags(self): 

106 """Test that the proper errors are thrown when requesting flag columns 

107 that are not in the input schema. 

108 """ 

109 with self.assertRaises(KeyError): 

110 TransformDiaSourceCatalogTask(initInputs=self.initInputsBadFlags) 

111 

112 def test_computeBBoxSize(self): 

113 """Test the values created for diaSourceBBox. 

114 """ 

115 transConfig = TransformDiaSourceCatalogConfig() 

116 transConfig.flagMap = os.path.join( 

117 getPackageDir("ap_association"), 

118 "tests", 

119 "data", 

120 "test-flag-map.yaml") 

121 transform = TransformDiaSourceCatalogTask(initInputs=self.initInputs, 

122 config=transConfig) 

123 bboxArray = transform.computeBBoxSizes(self.inputCatalog) 

124 

125 # Default in catalog is 18. 

126 self.assertEqual(bboxArray[0], self.bboxSize) 

127 

128 def test_bit_unpacker(self): 

129 """Test that the integer bit packer is functioning correctly. 

130 """ 

131 transConfig = TransformDiaSourceCatalogConfig() 

132 transConfig.flagMap = os.path.join( 

133 getPackageDir("ap_association"), 

134 "tests", 

135 "data", 

136 "test-flag-map.yaml") 

137 transConfig.functorFile = os.path.join(getPackageDir("ap_association"), 

138 "tests", 

139 "data", 

140 "testDiaSource.yaml") 

141 transform = TransformDiaSourceCatalogTask(initInputs=self.initInputs, 

142 config=transConfig) 

143 for idx, obj in enumerate(self.inputCatalog): 

144 if idx in [1, 3, 5]: 

145 obj.set("base_PixelFlags_flag", 1) 

146 if idx in [1, 4, 6]: 

147 obj.set("base_PixelFlags_flag_offimage", 1) 

148 outputCatalog = transform.run(self.inputCatalog, 

149 self.exposure, 

150 self.filterName, 

151 ccdVisitId=self.expId).diaSourceTable 

152 

153 unpacker = UnpackApdbFlags(transConfig.flagMap, "DiaSource") 

154 flag_values = unpacker.unpack(outputCatalog["flags"], "flags") 

155 

156 for idx, flag in enumerate(flag_values): 

157 if idx in [1, 3, 5]: 

158 self.assertTrue(flag['base_PixelFlags_flag']) 

159 else: 

160 self.assertFalse(flag['base_PixelFlags_flag']) 

161 

162 if idx in [1, 4, 6]: 

163 self.assertTrue(flag['base_PixelFlags_flag_offimage']) 

164 else: 

165 self.assertFalse(flag['base_PixelFlags_flag_offimage']) 

166 

167 

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

169 pass 

170 

171 

172def setup_module(module): 

173 lsst.utils.tests.init() 

174 

175 

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

177 lsst.utils.tests.init() 

178 unittest.main()