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 

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 = 1000 

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 for idx, row in self.fakeCat.iterrows(): 

72 self.inExp[idx] = geom.Box2D(self.bbox).contains( 

73 self.exposure.getWcs().skyToPixel( 

74 geom.SpherePoint(row[fakesConfig.raColName], 

75 row[fakesConfig.decColName], 

76 geom.radians))) 

77 

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

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

80 self.sourceCat = pd.DataFrame( 

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

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

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

84 "filterName": "g", 

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

86 "extraColumn": extraColumnData}) 

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

88 drop=False, 

89 inplace=True) 

90 

91 def testRunQuantum(self): 

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

93 """ 

94 root = tempfile.mkdtemp() 

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

96 "skymap": ["deepCoadd_skyMap"], 

97 "tract": [0, 42], 

98 "visit": [1234, 4321], 

99 "detector": [25, 26]} 

100 testRepo = butlerTests.makeTestRepo(root, dimensions) 

101 matchTask = MatchApFakesTask() 

102 connections = matchTask.config.ConnectionsClass( 

103 config=matchTask.config) 

104 

105 dataId = {"instrument": "notACam", 

106 "skymap": "deepCoadd_skyMap", 

107 "tract": 0, 

108 "visit": 1234, 

109 "detector": 25} 

110 butlerTests.addDatasetType( 

111 testRepo, 

112 connections.fakeCat.name, 

113 connections.fakeCat.dimensions, 

114 connections.fakeCat.storageClass) 

115 butlerTests.addDatasetType( 

116 testRepo, 

117 connections.diffIm.name, 

118 connections.diffIm.dimensions, 

119 connections.diffIm.storageClass) 

120 butlerTests.addDatasetType( 

121 testRepo, 

122 connections.associatedDiaSources.name, 

123 connections.associatedDiaSources.dimensions, 

124 connections.associatedDiaSources.storageClass) 

125 butlerTests.addDatasetType( 

126 testRepo, 

127 connections.matchedDiaSources.name, 

128 connections.matchedDiaSources.dimensions, 

129 connections.matchedDiaSources.storageClass) 

130 butler = butlerTests.makeTestCollection(testRepo) 

131 

132 butler.put(self.fakeCat, 

133 connections.fakeCat.name, 

134 {"tract": dataId["tract"], 

135 "skymap": dataId["skymap"]}) 

136 butler.put(self.exposure, 

137 connections.diffIm.name, 

138 {"instrument": dataId["instrument"], 

139 "visit": dataId["visit"], 

140 "detector": dataId["detector"]}) 

141 butler.put(self.sourceCat, 

142 connections.associatedDiaSources.name, 

143 {"instrument": dataId["instrument"], 

144 "visit": dataId["visit"], 

145 "detector": dataId["detector"]}) 

146 

147 quantum = testUtils.makeQuantum( 

148 matchTask, butler, dataId, 

149 {key: dataId for key in {"fakeCat", "diffIm", "associatedDiaSources", "matchedDiaSources"}}) 

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

151 # Actual input dataset omitted for simplicity 

152 run.assert_called_once() 

153 shutil.rmtree(root, ignore_errors=True) 

154 

155 def testRun(self): 

156 """Test the run method. 

157 """ 

158 matchFakes = MatchApFakesTask() 

159 result = matchFakes.run(self.fakeCat, 

160 self.exposure, 

161 self.sourceCat) 

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

163 self.assertEqual( 

164 len(self.sourceCat), 

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

166 

167 def testTrimCat(self): 

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

169 """ 

170 matchTask = MatchApFakesTask() 

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

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

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()