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

3# 

4# Developed for the LSST Data Management System. 

5# This product includes software developed by the LSST Project 

6# (http://www.lsst.org). 

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

8# for details of code ownership. 

9# 

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

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

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

13# (at your option) any later version. 

14# 

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

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

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

18# GNU General Public License for more details. 

19# 

20# You should have received a copy of the GNU General Public License 

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

22# 

23 

24import numpy as np 

25import pandas as pd 

26import shutil 

27import tempfile 

28import unittest 

29 

30import lsst.daf.butler.tests as butlerTests 

31import lsst.geom as geom 

32import lsst.meas.base.tests as measTests 

33from lsst.pipe.base import testUtils 

34import lsst.skymap as skyMap 

35import lsst.utils.tests 

36 

37from lsst.ap.pipe.matchApFakes import MatchApFakesTask, MatchApFakesConfig 

38from lsst.ap.pipe.createApFakes import CreateRandomApFakesTask, CreateRandomApFakesConfig 

39 

40 

41class TestMatchApFakes(lsst.utils.tests.TestCase): 

42 

43 def setUp(self): 

44 """Create fake data to use in the tests. 

45 """ 

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

47 geom.Extent2I(1024, 1153)) 

48 dataset = measTests.TestDataset(self.bbox) 

49 self.exposure = dataset.exposure 

50 

51 simpleMapConfig = skyMap.discreteSkyMap.DiscreteSkyMapConfig() 

52 simpleMapConfig.raList = [dataset.exposure.getWcs().getSkyOrigin().getRa().asDegrees()] 

53 simpleMapConfig.decList = [dataset.exposure.getWcs().getSkyOrigin().getDec().asDegrees()] 

54 simpleMapConfig.radiusList = [0.1] 

55 

56 self.simpleMap = skyMap.DiscreteSkyMap(simpleMapConfig) 

57 self.tractId = 0 

58 bCircle = self.simpleMap.generateTract(self.tractId).getInnerSkyPolygon().getBoundingCircle() 

59 targetSources = 10000 

60 self.sourceDensity = (targetSources 

61 / (bCircle.getArea() * (180 / np.pi) ** 2)) 

62 self.rng = np.random.default_rng(1234) 

63 

64 fakesConfig = CreateRandomApFakesConfig() 

65 fakesConfig.fraction = 0.0 

66 fakesConfig.fakeDensity = self.sourceDensity 

67 fakesTask = CreateRandomApFakesTask(config=fakesConfig) 

68 self.fakeCat = fakesTask.run(self.tractId, self.simpleMap).fakeCat 

69 

70 self.inExp = np.zeros(len(self.fakeCat), dtype=bool) 

71 bbox = geom.Box2D(self.exposure.getBBox()) 

72 for idx, row in self.fakeCat.iterrows(): 

73 coord = geom.SpherePoint(row[fakesConfig.raColName], 

74 row[fakesConfig.decColName], 

75 geom.radians) 

76 cent = self.exposure.getWcs().skyToPixel(coord) 

77 self.inExp[idx] = bbox.contains(cent) 

78 

79 tmpCat = self.fakeCat[self.inExp].iloc[:int(self.inExp.sum() / 2)] 

80 extraColumnData = self.rng.integers(0, 100, size=len(tmpCat)) 

81 self.sourceCat = pd.DataFrame( 

82 data={"ra": np.degrees(tmpCat[fakesTask.config.raColName]), 

83 "decl": np.degrees(tmpCat[fakesTask.config.decColName]), 

84 "diaObjectId": np.arange(1, len(tmpCat) + 1, dtype=int), 

85 "filterName": "g", 

86 "diaSourceId": np.arange(1, len(tmpCat) + 1, dtype=int), 

87 "extraColumn": extraColumnData}) 

88 self.sourceCat.set_index(["diaObjectId", "filterName", "extraColumn"], 

89 drop=False, 

90 inplace=True) 

91 

92 def testRunQuantum(self): 

93 """Test the run quantum method with a gen3 butler. 

94 """ 

95 root = tempfile.mkdtemp() 

96 dimensions = {"instrument": ["notACam"], 

97 "skymap": ["deepCoadd_skyMap"], 

98 "tract": [0, 42], 

99 "visit": [1234, 4321], 

100 "detector": [25, 26]} 

101 testRepo = butlerTests.makeTestRepo(root, dimensions) 

102 matchTask = MatchApFakesTask() 

103 connections = matchTask.config.ConnectionsClass( 

104 config=matchTask.config) 

105 

106 fakesDataId = {"skymap": "deepCoadd_skyMap", 

107 "tract": 0} 

108 imgDataId = {"instrument": "notACam", 

109 "visit": 1234, 

110 "detector": 25} 

111 butlerTests.addDatasetType( 

112 testRepo, 

113 connections.fakeCat.name, 

114 connections.fakeCat.dimensions, 

115 connections.fakeCat.storageClass) 

116 butlerTests.addDatasetType( 

117 testRepo, 

118 connections.diffIm.name, 

119 connections.diffIm.dimensions, 

120 connections.diffIm.storageClass) 

121 butlerTests.addDatasetType( 

122 testRepo, 

123 connections.associatedDiaSources.name, 

124 connections.associatedDiaSources.dimensions, 

125 connections.associatedDiaSources.storageClass) 

126 butlerTests.addDatasetType( 

127 testRepo, 

128 connections.matchedDiaSources.name, 

129 connections.matchedDiaSources.dimensions, 

130 connections.matchedDiaSources.storageClass) 

131 butler = butlerTests.makeTestCollection(testRepo) 

132 

133 butler.put(self.fakeCat, 

134 connections.fakeCat.name, 

135 {"tract": fakesDataId["tract"], 

136 "skymap": fakesDataId["skymap"]}) 

137 butler.put(self.exposure, 

138 connections.diffIm.name, 

139 {"instrument": imgDataId["instrument"], 

140 "visit": imgDataId["visit"], 

141 "detector": imgDataId["detector"]}) 

142 butler.put(self.sourceCat, 

143 connections.associatedDiaSources.name, 

144 {"instrument": imgDataId["instrument"], 

145 "visit": imgDataId["visit"], 

146 "detector": imgDataId["detector"]}) 

147 

148 quantumDataId = imgDataId.copy() 

149 quantumDataId.update(fakesDataId) 

150 quantum = testUtils.makeQuantum( 

151 matchTask, butler, quantumDataId, 

152 {"fakeCat": fakesDataId, 

153 "diffIm": imgDataId, 

154 "associatedDiaSources": imgDataId, 

155 "matchedDiaSources": imgDataId}) 

156 run = testUtils.runTestQuantum(matchTask, butler, quantum) 

157 # Actual input dataset omitted for simplicity 

158 run.assert_called_once() 

159 shutil.rmtree(root, ignore_errors=True) 

160 

161 def testRun(self): 

162 """Test the run method. 

163 """ 

164 matchFakesConfig = MatchApFakesConfig() 

165 matchFakesConfig.matchDistanceArcseconds = 0.1 

166 matchFakes = MatchApFakesTask(config=matchFakesConfig) 

167 result = matchFakes.run(self.fakeCat, 

168 self.exposure, 

169 self.sourceCat) 

170 self.assertEqual(self.inExp.sum(), len(result.matchedDiaSources)) 

171 self.assertEqual( 

172 len(self.sourceCat), 

173 np.sum(np.isfinite(result.matchedDiaSources["extraColumn"]))) 

174 

175 def testTrimCat(self): 

176 """Test that the correct number of sources are in the ccd area. 

177 """ 

178 matchTask = MatchApFakesTask() 

179 result = matchTask._trimFakeCat(self.fakeCat, self.exposure) 

180 self.assertEqual(len(result), self.inExp.sum()) 

181 

182 

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

184 pass 

185 

186 

187def setup_module(module): 

188 lsst.utils.tests.init() 

189 

190 

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

192 lsst.utils.tests.init() 

193 unittest.main()