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# 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.log.utils as logUtils 

33 

34verbosity = 3 

35logUtils.traceSetAt("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 afwMath.convolve(self.scienceImage, self.templateImage, self.gaussKernel, False) 

67 

68 def tearDown(self): 

69 del self.subconfig 

70 del self.gaussFunction 

71 del self.gaussKernel 

72 if defDataDir: 

73 del self.templateImage 

74 del self.scienceImage 

75 

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

77 imsize = int(5 * self.kSize) 

78 

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

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

81 bbox = geom.Box2I(p0, p1) 

82 

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

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

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

86 

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

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

89 

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

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

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

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

94 

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

96 imsize = int(5 * self.kSize) 

97 

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

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

100 bbox = geom.Box2I(p0, p1) 

101 

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

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

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

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

106 

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

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

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

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

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

112 

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

114 def testConvolveAndSubtract(self): 

115 self.runConvolveAndSubtract1(bgVal=0) 

116 self.runConvolveAndSubtract1(bgVal=10) 

117 # this one uses a function 

118 self.runConvolveAndSubtract2(bgOrder=0) 

119 self.runConvolveAndSubtract2(bgOrder=2) 

120 

121##### 

122 

123 

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

125 pass 

126 

127 

128def setup_module(module): 

129 lsst.utils.tests.init() 

130 

131 

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

133 lsst.utils.tests.init() 

134 unittest.main()