Coverage for tests/test_basisLists.py: 15%

162 statements  

« prev     ^ index     » next       coverage.py v7.3.0, created at 2023-08-31 10:51 +0000

1import unittest 

2 

3import numpy as num 

4 

5import lsst.utils.tests 

6import lsst.afw.image as afwImage 

7import lsst.afw.math as afwMath 

8import lsst.ip.diffim as ipDiffim 

9import lsst.utils.logging as logUtils 

10import lsst.pex.config as pexConfig 

11import lsst.pex.exceptions 

12 

13# Increase the number for more verbose messages 

14logUtils.trace_set_at("lsst.ip.diffim", 0) 

15 

16 

17class DiffimTestCases(unittest.TestCase): 

18 

19 def setUp(self): 

20 self.configAL = ipDiffim.PsfMatchConfigAL() 

21 

22 self.configDF = ipDiffim.PsfMatchConfigDF() 

23 

24 self.psAL = pexConfig.makePropertySet(self.configAL) 

25 self.psDF = pexConfig.makePropertySet(self.configDF) 

26 

27 self.kSize = self.psAL["kernelSize"] 

28 

29 def tearDown(self): 

30 del self.configAL 

31 del self.psAL 

32 del self.configDF 

33 del self.psDF 

34 

35 def deltaFunctionTest(self, ks): 

36 # right shape 

37 nk = 0 

38 for rowi in range(self.kSize): 

39 for colj in range(self.kSize): 

40 kernel = ks[nk] 

41 kimage = afwImage.ImageD(kernel.getDimensions()) 

42 ksum = kernel.computeImage(kimage, False) 

43 self.assertEqual(ksum, 1.) 

44 

45 for rowk in range(self.kSize): 

46 for coll in range(self.kSize): 

47 if (rowi == rowk) and (colj == coll): 

48 self.assertEqual(kimage[coll, rowk, afwImage.LOCAL], 1.) 

49 else: 

50 self.assertEqual(kimage[coll, rowk, afwImage.LOCAL], 0.) 

51 nk += 1 

52 

53 def testMakeDeltaFunction(self): 

54 ks = ipDiffim.makeDeltaFunctionBasisList(self.kSize, self.kSize) 

55 

56 # right size 

57 self.assertEqual(len(ks), self.kSize * self.kSize) 

58 

59 # right shape 

60 self.deltaFunctionTest(ks) 

61 

62 def alardLuptonTest(self, ks): 

63 kim = afwImage.ImageD(ks[0].getDimensions()) 

64 nBasis = len(ks) 

65 

66 # the first one sums to 1; the rest sum to 0 

67 ks[0].computeImage(kim, False) 

68 self.assertAlmostEqual(num.sum(num.ravel(kim.array)), 1.0) 

69 

70 for k in range(1, nBasis): 

71 ks[k].computeImage(kim, False) 

72 self.assertAlmostEqual(num.sum(num.ravel(kim.array)), 0.0) 

73 

74 # the images dotted with themselves is 1, except for the first 

75 for k in range(1, nBasis): 

76 ks[k].computeImage(kim, False) 

77 arr = kim.array 

78 self.assertAlmostEqual(num.sum(arr*arr), 1.0) 

79 

80 def testMakeAlardLupton(self): 

81 nGauss = self.psAL["alardNGauss"] 

82 sigGauss = self.psAL.getArray("alardSigGauss") 

83 degGauss = self.psAL.getArray("alardDegGauss") 

84 self.assertEqual(len(sigGauss), nGauss) 

85 self.assertEqual(len(degGauss), nGauss) 

86 self.assertEqual(self.kSize % 2, 1) # odd sized 

87 kHalfWidth = self.kSize // 2 

88 

89 ks = ipDiffim.makeAlardLuptonBasisList(kHalfWidth, nGauss, sigGauss, degGauss) 

90 

91 # right size 

92 nTot = 0 

93 for deg in degGauss: 

94 nTot += (deg + 1) * (deg + 2) / 2 

95 self.assertEqual(len(ks), nTot) 

96 

97 # right orthogonality 

98 self.alardLuptonTest(ks) 

99 

100 def testGenerateAlardLupton(self): 

101 # defaults 

102 ks = ipDiffim.generateAlardLuptonBasisList(self.configAL) 

103 self.alardLuptonTest(ks) 

104 

105 # send FWHM 

106 ks = ipDiffim.generateAlardLuptonBasisList(self.configAL, targetFwhmPix=3.0, referenceFwhmPix=4.0) 

107 self.alardLuptonTest(ks) 

108 

109 def testMakeKernelBasisList(self): 

110 ks = ipDiffim.makeKernelBasisList(self.configAL) 

111 self.alardLuptonTest(ks) 

112 

113 ks = ipDiffim.makeKernelBasisList(self.configDF) 

114 self.deltaFunctionTest(ks) 

115 

116 def testRenormalize(self): 

117 # inputs 

118 gauss1 = afwMath.GaussianFunction2D(2, 2) 

119 gauss2 = afwMath.GaussianFunction2D(3, 4) 

120 gauss3 = afwMath.GaussianFunction2D(0.2, 0.25) 

121 gaussKernel1 = afwMath.AnalyticKernel(self.kSize, self.kSize, gauss1) 

122 gaussKernel2 = afwMath.AnalyticKernel(self.kSize, self.kSize, gauss2) 

123 gaussKernel3 = afwMath.AnalyticKernel(self.kSize, self.kSize, gauss3) 

124 kimage1 = afwImage.ImageD(gaussKernel1.getDimensions()) 

125 ksum1 = gaussKernel1.computeImage(kimage1, False) 

126 kimage2 = afwImage.ImageD(gaussKernel2.getDimensions()) 

127 ksum2 = gaussKernel2.computeImage(kimage2, False) 

128 kimage3 = afwImage.ImageD(gaussKernel3.getDimensions()) 

129 ksum3 = gaussKernel3.computeImage(kimage3, False) 

130 self.assertNotEqual(ksum1, 1.) 

131 self.assertNotEqual(ksum2, 1.) 

132 self.assertNotEqual(ksum3, 1.) 

133 # no constraints on first kernels norm 

134 self.assertNotEqual(num.sum(num.ravel(kimage2.array)**2), 1.) 

135 self.assertNotEqual(num.sum(num.ravel(kimage3.array)**2), 1.) 

136 basisListIn = [] 

137 basisListIn.append(gaussKernel1) 

138 basisListIn.append(gaussKernel2) 

139 basisListIn.append(gaussKernel3) 

140 

141 # outputs 

142 basisListOut = ipDiffim.renormalizeKernelList(basisListIn) 

143 gaussKernel1 = basisListOut[0] 

144 gaussKernel2 = basisListOut[1] 

145 gaussKernel3 = basisListOut[2] 

146 ksum1 = gaussKernel1.computeImage(kimage1, False) 

147 ksum2 = gaussKernel2.computeImage(kimage2, False) 

148 ksum3 = gaussKernel3.computeImage(kimage3, False) 

149 self.assertAlmostEqual(ksum1, 1.) 

150 self.assertAlmostEqual(ksum2, 0.) 

151 self.assertAlmostEqual(ksum3, 0.) 

152 # no constraints on first kernels norm 

153 self.assertAlmostEqual(num.sum(num.ravel(kimage2.array)**2), 1.) 

154 self.assertAlmostEqual(num.sum(num.ravel(kimage3.array)**2), 1.) 

155 

156 def testCentralRegularization(self): 

157 # stencil of 1 not allowed 

158 self.psDF["regularizationType"] = "centralDifference" 

159 with self.assertRaises(lsst.pex.exceptions.Exception): 

160 self.psDF["centralRegularizationStencil"] = 1 

161 ipDiffim.makeRegularizationMatrix(self.psDF) 

162 

163 # stencil of 5 allowed 

164 self.psDF["centralRegularizationStencil"] = 5 

165 try: 

166 ipDiffim.makeRegularizationMatrix(self.psDF) 

167 except lsst.pex.exceptions.Exception as e: 

168 self.fail("Should not raise %s: stencil of 5 is allowed."%e) 

169 

170 # stencil of 9 allowed 

171 self.psDF["centralRegularizationStencil"] = 9 

172 try: 

173 ipDiffim.makeRegularizationMatrix(self.psDF) 

174 except lsst.pex.exceptions.Exception as e: 

175 self.fail("Should not raise %s: stencil of 9 is allowed"%e) 

176 

177 # border penalty < 0 

178 self.psDF["regularizationBorderPenalty"] = -1.0 

179 with self.assertRaises(lsst.pex.exceptions.Exception): 

180 ipDiffim.makeRegularizationMatrix(self.psDF) 

181 

182 # border penalty > 0 

183 self.psDF["regularizationBorderPenalty"] = 0.0 

184 try: 

185 ipDiffim.makeRegularizationMatrix(self.psDF) 

186 except lsst.pex.exceptions.Exception as e: 

187 self.fail("Should not raise %s: Border penalty > 0"%e) 

188 

189 def testForwardRegularization(self): 

190 self.psDF["regularizationType"] = "forwardDifference" 

191 

192 # order 1..3 allowed 

193 self.psDF["forwardRegularizationOrders"] = 0 

194 with self.assertRaises(lsst.pex.exceptions.Exception): 

195 ipDiffim.makeRegularizationMatrix(self.psDF) 

196 

197 self.psDF["forwardRegularizationOrders"] = 1 

198 try: 

199 ipDiffim.makeRegularizationMatrix(self.psDF) 

200 except lsst.pex.exceptions.Exception as e: 

201 self.fail("Should not raise %s: order 1 allowed"%e) 

202 

203 self.psDF["forwardRegularizationOrders"] = 4 

204 with self.assertRaises(lsst.pex.exceptions.Exception): 

205 ipDiffim.makeRegularizationMatrix(self.psDF) 

206 

207 self.psDF["forwardRegularizationOrders"] = [1, 2] 

208 try: 

209 ipDiffim.makeRegularizationMatrix(self.psDF) 

210 except lsst.pex.exceptions.Exception as e: 

211 self.fail("Should not raise %s: order 1,2 allowed"%e) 

212 

213 def testBadRegularization(self): 

214 with self.assertRaises(lsst.pex.exceptions.Exception): 

215 self.psDF["regularizationType"] = "foo" 

216 ipDiffim.makeRegularizationMatrix(self.psDF) 

217 

218 

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

220 pass 

221 

222 

223def setup_module(module): 

224 lsst.utils.tests.init() 

225 

226 

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

228 lsst.utils.tests.init() 

229 unittest.main()