Coverage for tests/test_transformDiaSourceCatalog.py: 21%

89 statements  

« prev     ^ index     » next       coverage.py v7.2.5, created at 2023-05-02 23:05 +0000

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.info.id = self.expId 

67 self.exposure.setDetector(detector) 

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

69 self.filterName = 'g' 

70 self.exposure.setFilter(afwImage.FilterLabel(band=self.filterName, physical='g.MP9401')) 

71 scale = 2 

72 scaleErr = 1 

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

74 self.exposure.setPhotoCalib(self.photoCalib) 

75 

76 def test_run(self): 

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

78 from the exposure. 

79 """ 

80 transConfig = TransformDiaSourceCatalogConfig() 

81 transConfig.flagMap = os.path.join( 

82 getPackageDir("ap_association"), 

83 "tests", 

84 "data", 

85 "test-flag-map.yaml") 

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

87 "tests", 

88 "data", 

89 "testDiaSource.yaml") 

90 transformTask = TransformDiaSourceCatalogTask(initInputs=self.initInputs, 

91 config=transConfig) 

92 result = transformTask.run(self.inputCatalog, 

93 self.exposure, 

94 self.filterName, 

95 ccdVisitId=self.expId) 

96 

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

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

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

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

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

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

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

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

105 

106 def test_run_dia_source_wrong_flags(self): 

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

108 that are not in the input schema. 

109 """ 

110 with self.assertRaises(KeyError): 

111 TransformDiaSourceCatalogTask(initInputs=self.initInputsBadFlags) 

112 

113 def test_computeBBoxSize(self): 

114 """Test the values created for diaSourceBBox. 

115 """ 

116 transConfig = TransformDiaSourceCatalogConfig() 

117 transConfig.flagMap = os.path.join( 

118 getPackageDir("ap_association"), 

119 "tests", 

120 "data", 

121 "test-flag-map.yaml") 

122 transform = TransformDiaSourceCatalogTask(initInputs=self.initInputs, 

123 config=transConfig) 

124 bboxArray = transform.computeBBoxSizes(self.inputCatalog) 

125 

126 # Default in catalog is 18. 

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

128 

129 def test_bit_unpacker(self): 

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

131 """ 

132 transConfig = TransformDiaSourceCatalogConfig() 

133 transConfig.flagMap = os.path.join( 

134 getPackageDir("ap_association"), 

135 "tests", 

136 "data", 

137 "test-flag-map.yaml") 

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

139 "tests", 

140 "data", 

141 "testDiaSource.yaml") 

142 transform = TransformDiaSourceCatalogTask(initInputs=self.initInputs, 

143 config=transConfig) 

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

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

146 obj.set("base_PixelFlags_flag", 1) 

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

148 obj.set("base_PixelFlags_flag_offimage", 1) 

149 outputCatalog = transform.run(self.inputCatalog, 

150 self.exposure, 

151 self.filterName, 

152 ccdVisitId=self.expId).diaSourceTable 

153 

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

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

156 

157 for idx, flag in enumerate(flag_values): 

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

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

160 else: 

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

162 

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

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

165 else: 

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

167 

168 

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

170 pass 

171 

172 

173def setup_module(module): 

174 lsst.utils.tests.init() 

175 

176 

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

178 lsst.utils.tests.init() 

179 unittest.main()