Coverage for tests/test_buildSpatialKernelVisitor.py: 17%

109 statements  

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

1import unittest 

2 

3 

4import lsst.utils.tests 

5import lsst.afw.image as afwImage 

6import lsst.geom as geom 

7import lsst.ip.diffim as ipDiffim 

8import lsst.utils.logging as logUtils 

9import lsst.pex.config as pexConfig 

10 

11logUtils.trace_set_at("lsst.ip.diffim", 4) 

12 

13# This tests the basics of the BuildSpatialKernelVisitor. E.g. that 

14# it makes the right size solution. For more complex behaviors such 

15# as reducing a delta function basis set into a Pca basis set, look at 

16# FitSpatialKernelFromCandidates.py 

17 

18 

19class DiffimTestCases(unittest.TestCase): 

20 

21 def setUp(self): 

22 self.config = ipDiffim.PsfMatchConfigAL() 

23 

24 self.ps = pexConfig.makePropertySet(self.config) 

25 self.size = 51 

26 

27 def tearDown(self): 

28 del self.ps 

29 

30 def makeCandidate(self, kSum, x, y): 

31 mi1 = afwImage.MaskedImageF(geom.Extent2I(self.size, self.size)) 

32 mi1.getVariance().set(1.0) # avoid NaNs 

33 mi1[self.size//2, self.size//2, afwImage.LOCAL] = (1, 0x0, 1) 

34 mi2 = afwImage.MaskedImageF(geom.Extent2I(self.size, self.size)) 

35 mi2.getVariance().set(1.0) # avoid NaNs 

36 mi2[self.size//2, self.size//2, afwImage.LOCAL] = (kSum, 0x0, kSum) 

37 kc = ipDiffim.makeKernelCandidate(x, y, mi1, mi2, self.ps) 

38 return kc 

39 

40 def testNoBg(self): 

41 self.runNoBg(0) 

42 self.runNoBg(1) 

43 self.runNoBg(2) 

44 

45 def runNoBg(self, sko): 

46 basisList = ipDiffim.makeKernelBasisList(self.config) 

47 self.ps['spatialKernelOrder'] = sko 

48 self.ps['fitForBackground'] = False 

49 

50 bbox = geom.Box2I(geom.Point2I(0, 0), 

51 geom.Extent2I(self.size*10, self.size*10)) 

52 

53 bsikv = ipDiffim.BuildSingleKernelVisitorF(basisList, self.ps) 

54 bspkv = ipDiffim.BuildSpatialKernelVisitorF(basisList, bbox, self.ps) 

55 

56 for x in range(1, self.size, 10): 

57 for y in range(1, self.size, 10): 

58 cand = self.makeCandidate(1.0, x, y) 

59 bsikv.processCandidate(cand) 

60 bspkv.processCandidate(cand) 

61 

62 bspkv.solveLinearEquation() 

63 sk, sb = bspkv.getSolutionPair() 

64 

65 # Kernel 

66 if sko == 0: 

67 # Specialization for speedup 

68 spatialKernelSolution = sk.getKernelParameters() 

69 

70 # One term for each basis function 

71 self.assertEqual(len(spatialKernelSolution), len(basisList)) 

72 

73 else: 

74 spatialKernelSolution = sk.getSpatialParameters() 

75 

76 nSpatialTerms = int(0.5 * (sko + 1) * (sko + 2)) 

77 # One model for each basis function 

78 self.assertEqual(len(spatialKernelSolution), len(basisList)) 

79 # First basis has no spatial variation 

80 for i in range(1, nSpatialTerms): 

81 self.assertEqual(spatialKernelSolution[0][i], 0.) 

82 # All bases have correct number of terms 

83 for i in range(len(spatialKernelSolution)): 

84 self.assertEqual(len(spatialKernelSolution[i]), nSpatialTerms) 

85 

86 # Background 

87 spatialBgSolution = sb.getParameters() 

88 nBgTerms = 1 

89 self.assertEqual(len(spatialBgSolution), nBgTerms) 

90 

91 def testModelType(self): 

92 bbox = geom.Box2I(geom.Point2I(10, 10), 

93 geom.Extent2I(10, 10)) 

94 basisList = ipDiffim.makeKernelBasisList(self.config) 

95 

96 self.ps["spatialModelType"] = "polynomial" 

97 ipDiffim.BuildSpatialKernelVisitorF(basisList, bbox, self.ps) 

98 

99 self.ps["spatialModelType"] = "chebyshev1" 

100 ipDiffim.BuildSpatialKernelVisitorF(basisList, bbox, self.ps) 

101 

102 try: 

103 self.ps["spatialModelType"] = "foo" 

104 ipDiffim.BuildSpatialKernelVisitorF(basisList, bbox, self.ps) 

105 except Exception: 

106 pass 

107 else: 

108 self.fail() 

109 

110 def testAlSpatialModel(self): 

111 self.runAlSpatialModel(0, 0) 

112 self.runAlSpatialModel(1, 0) 

113 self.runAlSpatialModel(0, 1) 

114 self.runAlSpatialModel(1, 1) 

115 self.runAlSpatialModel(2, 2) 

116 

117 def runAlSpatialModel(self, sko, bgo): 

118 basisList = ipDiffim.makeKernelBasisList(self.config) 

119 self.ps['spatialKernelOrder'] = sko 

120 self.ps['spatialBgOrder'] = bgo 

121 self.ps['fitForBackground'] = True 

122 

123 bbox = geom.Box2I(geom.Point2I(0, 0), 

124 geom.Extent2I(self.size*10, self.size*10)) 

125 

126 bsikv = ipDiffim.BuildSingleKernelVisitorF(basisList, self.ps) 

127 bspkv = ipDiffim.BuildSpatialKernelVisitorF(basisList, bbox, self.ps) 

128 

129 for x in range(1, self.size, 10): 

130 for y in range(1, self.size, 10): 

131 cand = self.makeCandidate(1.0, x, y) 

132 bsikv.processCandidate(cand) 

133 bspkv.processCandidate(cand) 

134 

135 bspkv.solveLinearEquation() 

136 sk, sb = bspkv.getSolutionPair() 

137 

138 # Kernel 

139 if sko == 0: 

140 # Specialization for speedup 

141 spatialKernelSolution = sk.getKernelParameters() 

142 

143 # One term for each basis function 

144 self.assertEqual(len(spatialKernelSolution), len(basisList)) 

145 

146 else: 

147 spatialKernelSolution = sk.getSpatialParameters() 

148 

149 nSpatialTerms = int(0.5 * (sko + 1) * (sko + 2)) 

150 # One model for each basis function 

151 self.assertEqual(len(spatialKernelSolution), len(basisList)) 

152 # First basis has no spatial variation 

153 for i in range(1, nSpatialTerms): 

154 self.assertEqual(spatialKernelSolution[0][i], 0.) 

155 # All bases have correct number of terms 

156 for i in range(len(spatialKernelSolution)): 

157 self.assertEqual(len(spatialKernelSolution[i]), nSpatialTerms) 

158 

159 # Background 

160 spatialBgSolution = sb.getParameters() 

161 nBgTerms = int(0.5 * (bgo + 1) * (bgo + 2)) 

162 self.assertEqual(len(spatialBgSolution), nBgTerms) 

163 

164 

165##### 

166 

167class TestMemory(lsst.utils.tests.MemoryTestCase): 

168 pass 

169 

170 

171def setup_module(module): 

172 lsst.utils.tests.init() 

173 

174 

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

176 lsst.utils.tests.init() 

177 unittest.main()