Coverage for tests/test_basisLists.py: 14%

166 statements  

« prev     ^ index     » next       coverage.py v7.2.5, created at 2023-05-08 08:40 +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.ImagePsfMatchTask.ConfigClass() 

21 self.configAL.kernel.name = "AL" 

22 self.subconfigAL = self.configAL.kernel.active 

23 

24 self.configDF = ipDiffim.ImagePsfMatchTask.ConfigClass() 

25 self.configDF.kernel.name = "DF" 

26 self.subconfigDF = self.configDF.kernel.active 

27 

28 self.psAL = pexConfig.makePropertySet(self.subconfigAL) 

29 self.psDF = pexConfig.makePropertySet(self.subconfigDF) 

30 

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

32 

33 def tearDown(self): 

34 del self.configAL 

35 del self.psAL 

36 del self.configDF 

37 del self.psDF 

38 

39 def deltaFunctionTest(self, ks): 

40 # right shape 

41 nk = 0 

42 for rowi in range(self.kSize): 

43 for colj in range(self.kSize): 

44 kernel = ks[nk] 

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

46 ksum = kernel.computeImage(kimage, False) 

47 self.assertEqual(ksum, 1.) 

48 

49 for rowk in range(self.kSize): 

50 for coll in range(self.kSize): 

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

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

53 else: 

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

55 nk += 1 

56 

57 def testMakeDeltaFunction(self): 

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

59 

60 # right size 

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

62 

63 # right shape 

64 self.deltaFunctionTest(ks) 

65 

66 def alardLuptonTest(self, ks): 

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

68 nBasis = len(ks) 

69 

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

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

72 self.assertAlmostEqual(num.sum(num.ravel(kim.getArray())), 1.0) 

73 

74 for k in range(1, nBasis): 

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

76 self.assertAlmostEqual(num.sum(num.ravel(kim.getArray())), 0.0) 

77 

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

79 for k in range(1, nBasis): 

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

81 arr = kim.getArray() 

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

83 

84 def testMakeAlardLupton(self): 

85 nGauss = self.psAL["alardNGauss"] 

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

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

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

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

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

91 kHalfWidth = self.kSize // 2 

92 

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

94 

95 # right size 

96 nTot = 0 

97 for deg in degGauss: 

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

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

100 

101 # right orthogonality 

102 self.alardLuptonTest(ks) 

103 

104 def testGenerateAlardLupton(self): 

105 # defaults 

106 ks = ipDiffim.generateAlardLuptonBasisList(self.subconfigAL) 

107 self.alardLuptonTest(ks) 

108 

109 # send FWHM 

110 ks = ipDiffim.generateAlardLuptonBasisList(self.subconfigAL, targetFwhmPix=3.0, referenceFwhmPix=4.0) 

111 self.alardLuptonTest(ks) 

112 

113 def testMakeKernelBasisList(self): 

114 ks = ipDiffim.makeKernelBasisList(self.subconfigAL) 

115 self.alardLuptonTest(ks) 

116 

117 ks = ipDiffim.makeKernelBasisList(self.subconfigDF) 

118 self.deltaFunctionTest(ks) 

119 

120 def testRenormalize(self): 

121 # inputs 

122 gauss1 = afwMath.GaussianFunction2D(2, 2) 

123 gauss2 = afwMath.GaussianFunction2D(3, 4) 

124 gauss3 = afwMath.GaussianFunction2D(0.2, 0.25) 

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

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

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

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

129 ksum1 = gaussKernel1.computeImage(kimage1, False) 

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

131 ksum2 = gaussKernel2.computeImage(kimage2, False) 

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

133 ksum3 = gaussKernel3.computeImage(kimage3, False) 

134 self.assertNotEqual(ksum1, 1.) 

135 self.assertNotEqual(ksum2, 1.) 

136 self.assertNotEqual(ksum3, 1.) 

137 # no constraints on first kernels norm 

138 self.assertNotEqual(num.sum(num.ravel(kimage2.getArray())**2), 1.) 

139 self.assertNotEqual(num.sum(num.ravel(kimage3.getArray())**2), 1.) 

140 basisListIn = [] 

141 basisListIn.append(gaussKernel1) 

142 basisListIn.append(gaussKernel2) 

143 basisListIn.append(gaussKernel3) 

144 

145 # outputs 

146 basisListOut = ipDiffim.renormalizeKernelList(basisListIn) 

147 gaussKernel1 = basisListOut[0] 

148 gaussKernel2 = basisListOut[1] 

149 gaussKernel3 = basisListOut[2] 

150 ksum1 = gaussKernel1.computeImage(kimage1, False) 

151 ksum2 = gaussKernel2.computeImage(kimage2, False) 

152 ksum3 = gaussKernel3.computeImage(kimage3, False) 

153 self.assertAlmostEqual(ksum1, 1.) 

154 self.assertAlmostEqual(ksum2, 0.) 

155 self.assertAlmostEqual(ksum3, 0.) 

156 # no constraints on first kernels norm 

157 self.assertAlmostEqual(num.sum(num.ravel(kimage2.getArray())**2), 1.) 

158 self.assertAlmostEqual(num.sum(num.ravel(kimage3.getArray())**2), 1.) 

159 

160 def testCentralRegularization(self): 

161 # stencil of 1 not allowed 

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

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

164 self.psDF["centralRegularizationStencil"] = 1 

165 ipDiffim.makeRegularizationMatrix(self.psDF) 

166 

167 # stencil of 5 allowed 

168 self.psDF["centralRegularizationStencil"] = 5 

169 try: 

170 ipDiffim.makeRegularizationMatrix(self.psDF) 

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

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

173 

174 # stencil of 9 allowed 

175 self.psDF["centralRegularizationStencil"] = 9 

176 try: 

177 ipDiffim.makeRegularizationMatrix(self.psDF) 

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

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

180 

181 # border penalty < 0 

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

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

184 ipDiffim.makeRegularizationMatrix(self.psDF) 

185 

186 # border penalty > 0 

187 self.psDF["regularizationBorderPenalty"] = 0.0 

188 try: 

189 ipDiffim.makeRegularizationMatrix(self.psDF) 

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

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

192 

193 def testForwardRegularization(self): 

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

195 

196 # order 1..3 allowed 

197 self.psDF["forwardRegularizationOrders"] = 0 

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

199 ipDiffim.makeRegularizationMatrix(self.psDF) 

200 

201 self.psDF["forwardRegularizationOrders"] = 1 

202 try: 

203 ipDiffim.makeRegularizationMatrix(self.psDF) 

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

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

206 

207 self.psDF["forwardRegularizationOrders"] = 4 

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

209 ipDiffim.makeRegularizationMatrix(self.psDF) 

210 

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

212 try: 

213 ipDiffim.makeRegularizationMatrix(self.psDF) 

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

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

216 

217 def testBadRegularization(self): 

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

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

220 ipDiffim.makeRegularizationMatrix(self.psDF) 

221 

222 

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

224 pass 

225 

226 

227def setup_module(module): 

228 lsst.utils.tests.init() 

229 

230 

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

232 lsst.utils.tests.init() 

233 unittest.main()