Coverage for tests/test_createApFakes.py: 20%

103 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-10 21:15 +0000

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 shutil 

26import tempfile 

27import unittest 

28 

29import lsst.daf.butler.tests as butlerTests 

30import lsst.geom as geom 

31from lsst.pipe.base import testUtils 

32import lsst.skymap as skyMap 

33import lsst.utils.tests 

34 

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

36 

37 

38class TestCreateApFakes(lsst.utils.tests.TestCase): 

39 

40 def setUp(self): 

41 """ 

42 """ 

43 simpleMapConfig = skyMap.discreteSkyMap.DiscreteSkyMapConfig() 

44 simpleMapConfig.raList = [10] 

45 simpleMapConfig.decList = [-1] 

46 simpleMapConfig.radiusList = [0.1] 

47 

48 self.simpleMap = skyMap.DiscreteSkyMap(simpleMapConfig) 

49 self.tractId = 0 

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

51 self.nSources = 10 

52 self.sourceDensity = (self.nSources 

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

54 self.fraction = 0.5 

55 self.nInVisit = (int(self.nSources * self.fraction) 

56 + int((1 - self.fraction) / 2 * self.nSources)) 

57 self.nInTemplate = (self.nSources - self.nInVisit 

58 + int(self.nSources * self.fraction)) 

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

60 

61 def testRunQuantum(self): 

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

63 """ 

64 root = tempfile.mkdtemp() 

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

66 "skymap": ["skyMap"], 

67 "tract": [0, 42], 

68 } 

69 testRepo = butlerTests.makeTestRepo(root, dimensions) 

70 fakesTask = CreateRandomApFakesTask() 

71 connections = fakesTask.config.ConnectionsClass( 

72 config=fakesTask.config) 

73 butlerTests.addDatasetType( 

74 testRepo, 

75 connections.skyMap.name, 

76 connections.skyMap.dimensions, 

77 connections.skyMap.storageClass) 

78 butlerTests.addDatasetType( 

79 testRepo, 

80 connections.fakeCat.name, 

81 connections.fakeCat.dimensions, 

82 connections.fakeCat.storageClass) 

83 

84 dataId = {"skymap": "skyMap", "tract": 0} 

85 butler = butlerTests.makeTestCollection(testRepo) 

86 butler.put(self.simpleMap, "skyMap", {"skymap": "skyMap"}) 

87 

88 quantum = testUtils.makeQuantum( 

89 fakesTask, butler, dataId, 

90 {"skyMap": {"skymap": dataId["skymap"]}, "fakeCat": dataId}) 

91 run = testUtils.runTestQuantum(fakesTask, butler, quantum, True) 

92 # Actual input dataset omitted for simplicity 

93 run.assert_called_once_with(tractId=dataId["tract"], skyMap=self.simpleMap) 

94 shutil.rmtree(root, ignore_errors=True) 

95 

96 def testRun(self): 

97 """Test the run method. 

98 """ 

99 fakesConfig = CreateRandomApFakesConfig() 

100 fakesConfig.fraction = 0.5 

101 fakesConfig.fakeDensity = self.sourceDensity 

102 fakesTask = CreateRandomApFakesTask(config=fakesConfig) 

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

104 result = fakesTask.run(self.tractId, self.simpleMap) 

105 fakeCat = result.fakeCat 

106 self.assertEqual(len(fakeCat), self.nSources) 

107 for idx, row in fakeCat.iterrows(): 

108 self.assertTrue( 

109 bCircle.contains( 

110 geom.SpherePoint(row[fakesTask.config.ra_col], 

111 row[fakesTask.config.dec_col], 

112 geom.radians).getVector())) 

113 self.assertEqual(fakeCat[fakesConfig.visitSourceFlagCol].sum(), 

114 self.nInVisit) 

115 self.assertEqual(fakeCat[fakesConfig.templateSourceFlagCol].sum(), 

116 self.nInTemplate) 

117 for f in fakesConfig.filterSet: 

118 filterMags = fakeCat[fakesConfig.mag_col % f] 

119 self.assertEqual(self.nSources, len(filterMags)) 

120 self.assertTrue( 

121 np.all(fakesConfig.magMin <= filterMags)) 

122 self.assertTrue( 

123 np.all(fakesConfig.magMax > filterMags)) 

124 

125 def testCreateRandomPositions(self): 

126 """Test that the correct number of sources are produced and are 

127 contained in the cap bound. 

128 """ 

129 fakesTask = CreateRandomApFakesTask() 

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

131 

132 randData = fakesTask.createRandomPositions( 

133 nFakes=self.nSources, 

134 boundingCircle=bCircle, 

135 rng=self.rng) 

136 self.assertEqual(self.nSources, len(randData[fakesTask.config.ra_col])) 

137 self.assertEqual(self.nSources, len(randData[fakesTask.config.dec_col])) 

138 for idx in range(self.nSources): 

139 self.assertTrue( 

140 bCircle.contains( 

141 geom.SpherePoint(randData[fakesTask.config.ra_col][idx], 

142 randData[fakesTask.config.dec_col][idx], 

143 geom.radians).getVector())) 

144 

145 def testCreateRotMatrix(self): 

146 """Test that the rotation matrix is computed correctly and rotates 

147 a test vector to the expected location. 

148 """ 

149 createFakes = CreateRandomApFakesTask() 

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

151 rotMatrix = createFakes._createRotMatrix(bCircle) 

152 rotatedVector = np.dot(rotMatrix, np.array([0, 0, 1])) 

153 expectedVect = bCircle.getCenter() 

154 self.assertAlmostEqual(expectedVect.x(), rotatedVector[0]) 

155 self.assertAlmostEqual(expectedVect.y(), rotatedVector[1]) 

156 self.assertAlmostEqual(expectedVect.z(), rotatedVector[2]) 

157 

158 def testVisitCoaddSubdivision(self): 

159 """Test that the number of assigned visit to template objects is 

160 correct. 

161 """ 

162 fakesConfig = CreateRandomApFakesConfig() 

163 fakesConfig.fraction = 0.5 

164 fakesTask = CreateRandomApFakesTask(config=fakesConfig) 

165 subdivision = fakesTask.createVisitCoaddSubdivision(self.nSources) 

166 self.assertEqual( 

167 subdivision[fakesConfig.visitSourceFlagCol].sum(), 

168 self.nInVisit) 

169 self.assertEqual( 

170 subdivision[fakesConfig.templateSourceFlagCol].sum(), 

171 self.nInTemplate) 

172 

173 def testRandomMagnitudes(self): 

174 """Test that the correct number of filters and magnitudes have been 

175 produced. 

176 """ 

177 fakesConfig = CreateRandomApFakesConfig() 

178 fakesConfig.filterSet = ["u", "g"] 

179 fakesConfig.mag_col = "%s_mag" 

180 fakesConfig.magMin = 20 

181 fakesConfig.magMax = 21 

182 fakesTask = CreateRandomApFakesTask(config=fakesConfig) 

183 mags = fakesTask.createRandomMagnitudes(self.nSources, self.rng) 

184 self.assertEqual(len(fakesConfig.filterSet), len(mags)) 

185 for f in fakesConfig.filterSet: 

186 filterMags = mags[fakesConfig.mag_col % f] 

187 self.assertEqual(self.nSources, len(filterMags)) 

188 self.assertTrue( 

189 np.all(fakesConfig.magMin <= filterMags)) 

190 self.assertTrue( 

191 np.all(fakesConfig.magMax > filterMags)) 

192 

193 

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

195 pass 

196 

197 

198def setup_module(module): 

199 lsst.utils.tests.init() 

200 

201 

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

203 lsst.utils.tests.init() 

204 unittest.main()