Coverage for tests/test_transformDiaSourceCatalog.py: 24%

93 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-23 03:24 -0700

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 

25import numpy as np 

26 

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

28 TransformDiaSourceCatalogTask) 

29from lsst.afw.cameraGeom.testUtils import DetectorWrapper 

30import lsst.daf.base as dafBase 

31import lsst.afw.image as afwImage 

32import lsst.geom as geom 

33import lsst.meas.base.tests as measTests 

34from lsst.pipe.base import Struct 

35from lsst.utils import getPackageDir 

36import lsst.utils.tests 

37 

38from lsst.ap.association.transformDiaSourceCatalog import UnpackApdbFlags 

39 

40 

41class TestTransformDiaSourceCatalogTask(unittest.TestCase): 

42 def setUp(self): 

43 self.nSources = 10 

44 self.bboxSize = 18 

45 self.xyLoc = 100 

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

47 geom.Extent2I(1024, 1153)) 

48 dataset = measTests.TestDataset(self.bbox) 

49 for srcIdx in range(self.nSources-1): 

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

51 # Ensure the last source has no peak `significance` field. 

52 dataset.addSource(100000.0, geom.Point2D(self.xyLoc, self.xyLoc), setPeakSignificance=False) 

53 schema = dataset.makeMinimalSchema() 

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

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

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

57 # Create schemas for use in initializing the TransformDiaSourceCatalog 

58 # task. 

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

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

61 

62 self.expId = 4321 

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

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

65 visit = afwImage.VisitInfo( 

66 exposureId=self.expId, 

67 exposureTime=200., 

68 date=self.date) 

69 self.exposure.info.id = self.expId 

70 self.exposure.setDetector(detector) 

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

72 self.filterName = 'g' 

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

74 scale = 2 

75 scaleErr = 1 

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

77 self.exposure.setPhotoCalib(self.photoCalib) 

78 

79 def test_run(self): 

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

81 from the exposure. 

82 """ 

83 transConfig = TransformDiaSourceCatalogConfig() 

84 transConfig.flagMap = os.path.join( 

85 getPackageDir("ap_association"), 

86 "tests", 

87 "data", 

88 "test-flag-map.yaml") 

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

90 "tests", 

91 "data", 

92 "testDiaSource.yaml") 

93 transformTask = TransformDiaSourceCatalogTask(initInputs=self.initInputs, 

94 config=transConfig) 

95 result = transformTask.run(self.inputCatalog, 

96 self.exposure, 

97 self.filterName, 

98 ccdVisitId=self.expId) 

99 

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

101 np.testing.assert_array_equal(result.diaSourceTable["bboxSize"], [self.bboxSize]*self.nSources) 

102 np.testing.assert_array_equal(result.diaSourceTable["ccdVisitId"], [self.expId]*self.nSources) 

103 np.testing.assert_array_equal(result.diaSourceTable["filterName"], [self.filterName]*self.nSources) 

104 np.testing.assert_array_equal(result.diaSourceTable["midPointTai"], 

105 [self.date.get(system=dafBase.DateTime.MJD)]*self.nSources) 

106 np.testing.assert_array_equal(result.diaSourceTable["diaObjectId"], [0]*self.nSources) 

107 # The final snr value should be NaN because it doesn't have a peak significance field. 

108 expect_snr = [397.887353515625]*9 

109 expect_snr.append(np.nan) 

110 self.assertTrue(np.array_equal(result.diaSourceTable["snr"], expect_snr, equal_nan=True)) 

111 

112 def test_run_dia_source_wrong_flags(self): 

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

114 that are not in the input schema. 

115 """ 

116 with self.assertRaises(KeyError): 

117 TransformDiaSourceCatalogTask(initInputs=self.initInputsBadFlags) 

118 

119 def test_computeBBoxSize(self): 

120 """Test the values created for diaSourceBBox. 

121 """ 

122 transConfig = TransformDiaSourceCatalogConfig() 

123 transConfig.flagMap = os.path.join( 

124 getPackageDir("ap_association"), 

125 "tests", 

126 "data", 

127 "test-flag-map.yaml") 

128 transform = TransformDiaSourceCatalogTask(initInputs=self.initInputs, 

129 config=transConfig) 

130 bboxArray = transform.computeBBoxSizes(self.inputCatalog) 

131 

132 # Default in catalog is 18. 

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

134 

135 def test_bit_unpacker(self): 

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

137 """ 

138 transConfig = TransformDiaSourceCatalogConfig() 

139 transConfig.flagMap = os.path.join( 

140 getPackageDir("ap_association"), 

141 "tests", 

142 "data", 

143 "test-flag-map.yaml") 

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

145 "tests", 

146 "data", 

147 "testDiaSource.yaml") 

148 transform = TransformDiaSourceCatalogTask(initInputs=self.initInputs, 

149 config=transConfig) 

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

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

152 obj.set("base_PixelFlags_flag", 1) 

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

154 obj.set("base_PixelFlags_flag_offimage", 1) 

155 outputCatalog = transform.run(self.inputCatalog, 

156 self.exposure, 

157 self.filterName, 

158 ccdVisitId=self.expId).diaSourceTable 

159 

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

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

162 

163 for idx, flag in enumerate(flag_values): 

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

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

166 else: 

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

168 

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

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

171 else: 

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

173 

174 

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

176 pass 

177 

178 

179def setup_module(module): 

180 lsst.utils.tests.init() 

181 

182 

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

184 lsst.utils.tests.init() 

185 unittest.main()