Coverage for tests/test_imageSubtract.py: 29%

78 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-01-04 10:56 +0000

1# 

2# LSST Data Management System 

3# Copyright 2008-2016 LSST Corporation. 

4# 

5# This product includes software developed by the 

6# LSST Project (http://www.lsst.org/). 

7# 

8# This program is free software: you can redistribute it and/or modify 

9# it under the terms of the GNU General Public License as published by 

10# the Free Software Foundation, either version 3 of the License, or 

11# (at your option) any later version. 

12# 

13# This program is distributed in the hope that it will be useful, 

14# but WITHOUT ANY WARRANTY; without even the implied warranty of 

15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

16# GNU General Public License for more details. 

17# 

18# You should have received a copy of the LSST License Statement and 

19# the GNU General Public License along with this program. If not, 

20# see <http://www.lsstcorp.org/LegalNotices/>. 

21# 

22import os 

23import unittest 

24 

25 

26import lsst.utils.tests 

27import lsst.utils 

28import lsst.afw.image as afwImage 

29import lsst.afw.math as afwMath 

30import lsst.geom as geom 

31import lsst.ip.diffim as ipDiffim 

32import lsst.utils.logging as logUtils 

33 

34verbosity = 3 

35logUtils.trace_set_at("lsst.ip.diffim", verbosity) 

36 

37# known input images 

38try: 

39 defDataDir = lsst.utils.getPackageDir('afwdata') 

40except Exception: 

41 defDataDir = None 

42 

43 

44# This one tests convolve and subtract 

45class DiffimTestCases(lsst.utils.tests.TestCase): 

46 

47 # D = I - (K.x.T + bg) 

48 

49 def setUp(self): 

50 self.config = ipDiffim.ImagePsfMatchTask.ConfigClass() 

51 self.config.kernel.name = "AL" 

52 self.subconfig = self.config.kernel.active 

53 self.kSize = self.subconfig.kernelSize 

54 

55 # gaussian reference kernel 

56 self.gSize = self.kSize 

57 self.gaussFunction = afwMath.GaussianFunction2D(2, 3) 

58 self.gaussKernel = afwMath.AnalyticKernel(self.gSize, self.gSize, self.gaussFunction) 

59 

60 if defDataDir: 

61 defImagePath = os.path.join(defDataDir, "DC3a-Sim", "sci", "v5-e0", 

62 "v5-e0-c011-a00.sci.fits") 

63 self.templateImage = afwImage.MaskedImageF(defImagePath) 

64 self.scienceImage = self.templateImage.Factory(self.templateImage.getDimensions()) 

65 

66 convolutionControl = afwMath.ConvolutionControl() 

67 convolutionControl.setDoNormalize(False) 

68 afwMath.convolve(self.scienceImage, self.templateImage, self.gaussKernel, convolutionControl) 

69 

70 def tearDown(self): 

71 del self.subconfig 

72 del self.gaussFunction 

73 del self.gaussKernel 

74 if defDataDir: 

75 del self.templateImage 

76 del self.scienceImage 

77 

78 def runConvolveAndSubtract1(self, bgVal=0, xloc=408, yloc=580): 

79 imsize = int(5 * self.kSize) 

80 

81 p0 = geom.Point2I(xloc - imsize//2, yloc - imsize//2) 

82 p1 = geom.Point2I(xloc + imsize//2, yloc + imsize//2) 

83 bbox = geom.Box2I(p0, p1) 

84 

85 tmi = afwImage.MaskedImageF(self.templateImage, bbox, origin=afwImage.LOCAL) 

86 smi = afwImage.MaskedImageF(self.scienceImage, bbox, origin=afwImage.LOCAL) 

87 diffIm = ipDiffim.convolveAndSubtract(tmi, smi, self.gaussKernel, bgVal) 

88 

89 bbox = self.gaussKernel.shrinkBBox(diffIm.getBBox(origin=afwImage.LOCAL)) 

90 diffIm2 = afwImage.MaskedImageF(diffIm, bbox, origin=afwImage.LOCAL) 

91 

92 # image is empty (or the additional background you subtracted off) 

93 for j in range(diffIm2.getHeight()): 

94 for i in range(diffIm2.getWidth()): 

95 self.assertAlmostEqual(diffIm2.image[i, j, afwImage.LOCAL], -1.*bgVal, 3) 

96 

97 def runConvolveAndSubtract2(self, bgOrder=0, xloc=408, yloc=580): 

98 imsize = int(5 * self.kSize) 

99 

100 p0 = geom.Point2I(xloc - imsize//2, yloc - imsize//2) 

101 p1 = geom.Point2I(xloc + imsize//2, yloc + imsize//2) 

102 bbox = geom.Box2I(p0, p1) 

103 

104 tmi = afwImage.MaskedImageF(self.templateImage, bbox, origin=afwImage.LOCAL) 

105 smi = afwImage.MaskedImageF(self.scienceImage, bbox, origin=afwImage.LOCAL) 

106 bgFunc = afwMath.PolynomialFunction2D(bgOrder) # coeffs are 0. by default 

107 diffIm = ipDiffim.convolveAndSubtract(tmi, smi, self.gaussKernel, bgFunc) 

108 

109 bbox = self.gaussKernel.shrinkBBox(diffIm.getBBox(origin=afwImage.LOCAL)) 

110 diffIm2 = afwImage.MaskedImageF(diffIm, bbox, origin=afwImage.LOCAL) 

111 for j in range(diffIm2.getHeight()): 

112 for i in range(diffIm2.getWidth()): 

113 self.assertAlmostEqual(diffIm2.image[i, j, afwImage.LOCAL], 0., 4) 

114 

115 @unittest.skipIf(not defDataDir, "Warning: afwdata is not set up") 

116 def testConvolveAndSubtract(self): 

117 self.runConvolveAndSubtract1(bgVal=0) 

118 self.runConvolveAndSubtract1(bgVal=10) 

119 # this one uses a function 

120 self.runConvolveAndSubtract2(bgOrder=0) 

121 self.runConvolveAndSubtract2(bgOrder=2) 

122 

123##### 

124 

125 

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

127 pass 

128 

129 

130def setup_module(module): 

131 lsst.utils.tests.init() 

132 

133 

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

135 lsst.utils.tests.init() 

136 unittest.main()