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 unittest 

26 

27import lsst.geom as geom 

28import lsst.skymap as skyMap 

29import lsst.utils.tests 

30 

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

32 

33 

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

35 

36 def setUp(self): 

37 """ 

38 """ 

39 simpleMapConfig = skyMap.discreteSkyMap.DiscreteSkyMapConfig() 

40 simpleMapConfig.raList = [10] 

41 simpleMapConfig.decList = [-1] 

42 simpleMapConfig.radiusList = [0.1] 

43 

44 self.simpleMap = skyMap.DiscreteSkyMap(simpleMapConfig) 

45 self.tractId = 0 

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

47 self.nSources = 10 

48 self.sourceDensity = (self.nSources 

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

50 self.fraction = 0.5 

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

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

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

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

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

56 

57 def testRun(self): 

58 """Test the run method. 

59 """ 

60 fakesConfig = CreateRandomApFakesConfig() 

61 fakesConfig.fraction = 0.5 

62 fakesConfig.fakeDensity = self.sourceDensity 

63 fakesTask = CreateRandomApFakesTask(config=fakesConfig) 

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

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

66 fakeCat = result.fakeCat.toDataFrame() 

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

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

69 self.assertTrue( 

70 bCircle.contains( 

71 geom.SpherePoint(row[fakesTask.config.raColName], 

72 row[fakesTask.config.decColName], 

73 geom.radians).getVector())) 

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

75 self.nInVisit) 

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

77 self.nInTemplate) 

78 for f in fakesConfig.filterSet: 

79 filterMags = fakeCat[fakesConfig.magVar % f] 

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

81 self.assertTrue( 

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

83 self.assertTrue( 

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

85 

86 def testCreateRandomPositions(self): 

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

88 contained in the cap bound. 

89 """ 

90 fakesTask = CreateRandomApFakesTask() 

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

92 

93 randData = fakesTask.createRandomPositions( 

94 nFakes=self.nSources, 

95 boundingCircle=bCircle, 

96 rng=self.rng) 

97 self.assertEqual(self.nSources, len(randData[fakesTask.config.raColName])) 

98 self.assertEqual(self.nSources, len(randData[fakesTask.config.decColName])) 

99 for idx in range(self.nSources): 

100 self.assertTrue( 

101 bCircle.contains( 

102 geom.SpherePoint(randData[fakesTask.config.raColName][idx], 

103 randData[fakesTask.config.decColName][idx], 

104 geom.radians).getVector())) 

105 

106 def testCreateRotMatrix(self): 

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

108 a test vector to the expected location. 

109 """ 

110 createFakes = CreateRandomApFakesTask() 

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

112 rotMatrix = createFakes._createRotMatrix(bCircle) 

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

114 expectedVect = bCircle.getCenter() 

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

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

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

118 

119 def testVisitCoaddSubdivision(self): 

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

121 correct. 

122 """ 

123 fakesConfig = CreateRandomApFakesConfig() 

124 fakesConfig.fraction = 0.5 

125 fakesTask = CreateRandomApFakesTask(config=fakesConfig) 

126 subdivision = fakesTask.createVisitCoaddSubdivision(self.nSources) 

127 self.assertEqual( 

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

129 self.nInVisit) 

130 self.assertEqual( 

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

132 self.nInTemplate) 

133 

134 def testRandomMagnitudes(self): 

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

136 produced. 

137 """ 

138 fakesConfig = CreateRandomApFakesConfig() 

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

140 fakesConfig.magVar = "%smag" 

141 fakesConfig.magMin = 20 

142 fakesConfig.magMax = 21 

143 fakesTask = CreateRandomApFakesTask(config=fakesConfig) 

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

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

146 for f in fakesConfig.filterSet: 

147 filterMags = mags[fakesConfig.magVar % f] 

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

149 self.assertTrue( 

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

151 self.assertTrue( 

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

153 

154 

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

156 pass 

157 

158 

159def setup_module(module): 

160 lsst.utils.tests.init() 

161 

162 

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

164 lsst.utils.tests.init() 

165 unittest.main()