Coverage for tests/test_modelPsfMatch.py: 28%

91 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2022-09-16 01:36 -0700

1import unittest 

2 

3import lsst.utils.tests 

4import lsst.daf.base as dafBase 

5from lsst.afw.coord import Observatory, Weather 

6import lsst.afw.image as afwImage 

7import lsst.geom as geom 

8import lsst.ip.diffim as ipDiffim 

9import lsst.utils.logging as logUtils 

10import lsst.meas.algorithms as measAlg 

11from lsst.ip.diffim.modelPsfMatch import nextOddInteger 

12 

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

14 

15 

16class PsfMatchTestCases(lsst.utils.tests.TestCase): 

17 

18 def setUp(self): 

19 self.config = ipDiffim.ModelPsfMatchTask.ConfigClass() 

20 self.subconfig = self.config.kernel.active 

21 self.subconfig.scaleByFwhm = False 

22 self.subconfig.kernelSize = 9 

23 self.subconfig.kernelSizeMin = 9 

24 

25 self.imsize = 2 * self.subconfig.sizeCellX 

26 self.ksize = 11 

27 self.sigma1 = 2.0 

28 self.sigma2 = 3.7 

29 self.exp = afwImage.ExposureF(geom.Extent2I(self.imsize, self.imsize)) 

30 self.exp.setPsf(measAlg.DoubleGaussianPsf(self.ksize, self.ksize, self.sigma1)) 

31 

32 def testTooBig(self): 

33 """Test that modelPsfMatchTask raises if kernel size is too big and 

34 and automatic padding disabled 

35 """ 

36 self.config.doAutoPadPsf = False 

37 self.subconfig.kernelSize = self.ksize 

38 psf = measAlg.DoubleGaussianPsf(self.ksize, self.ksize, self.sigma2) 

39 psfMatch = ipDiffim.ModelPsfMatchTask(config=self.config) 

40 try: 

41 psfMatch.run(self.exp, psf) 

42 except Exception: 

43 pass 

44 else: 

45 self.fail() 

46 

47 def testMatch(self): 

48 for order in (0, 1): 

49 for ksum in (0.5, 1.0, 2.7): 

50 self.runMatch(kOrder=order, kSumIn=ksum) 

51 

52 def runMatch(self, kOrder=0, kSumIn=3.7): 

53 self.subconfig.spatialKernelOrder = kOrder 

54 

55 psf = measAlg.DoubleGaussianPsf(self.ksize, self.ksize, self.sigma2) 

56 psfMatch = ipDiffim.ModelPsfMatchTask(config=self.config) 

57 results = psfMatch.run(self.exp, psf, kernelSum=kSumIn) 

58 

59 matchingKernel = results.psfMatchingKernel 

60 

61 kImage = afwImage.ImageD(matchingKernel.getDimensions()) 

62 kSumOut = matchingKernel.computeImage(kImage, False) 

63 

64 self.assertAlmostEqual(kSumIn, kSumOut) 

65 

66 def testAdjustModelSize(self): 

67 """Test that modelPsfMatch correctly adjusts the model PSF dimensions to 

68 match those of the science PSF. 

69 """ 

70 self.config.doAutoPadPsf = False 

71 psfModel = measAlg.DoubleGaussianPsf(self.ksize + 2, self.ksize + 2, self.sigma2) 

72 psfMatch = ipDiffim.ModelPsfMatchTask(config=self.config) 

73 results = psfMatch.run(self.exp, psfModel) 

74 self.assertEqual(results.psfMatchedExposure.getPsf().computeImage().getDimensions(), 

75 self.exp.getPsf().computeImage().getDimensions()) 

76 self.assertEqual(results.psfMatchedExposure.getPsf().getSigma1(), self.sigma2) 

77 

78 def testPadPsf(self): 

79 """Test automatic and manual PSF padding 

80 

81 Compare expected Psf size, after padding, to the reference Psf size. 

82 The reference Psf Size is proxy for the Sciencee Psf size here. 

83 """ 

84 psfModel = measAlg.DoubleGaussianPsf(self.ksize, self.ksize, self.sigma2) 

85 

86 # Test automatic padding (doAutoPadPsf is True by default) 

87 autoPaddedKernel = nextOddInteger(self.subconfig.kernelSize*self.config.autoPadPsfTo) 

88 psfMatch = ipDiffim.ModelPsfMatchTask(config=self.config) 

89 results = psfMatch.run(self.exp, psfModel) 

90 self.assertEqual(results.psfMatchedExposure.getPsf().computeImage().getWidth(), autoPaddedKernel) 

91 

92 # Test manual padding 

93 self.config.doAutoPadPsf = False 

94 PAD_EVEN_VALUES = [0, 2, 4] 

95 for padPix in PAD_EVEN_VALUES: 

96 self.config.padPsfBy = padPix 

97 psfMatch = ipDiffim.ModelPsfMatchTask(config=self.config) 

98 results = psfMatch.run(self.exp, psfModel) 

99 self.assertEqual(results.psfMatchedExposure.getPsf().computeImage().getWidth(), 

100 self.ksize + padPix) 

101 

102 PAD_ODD_VALUES = [1, 3, 5] 

103 for padPix in PAD_ODD_VALUES: 

104 self.config.padPsfBy = padPix 

105 psfMatch = ipDiffim.ModelPsfMatchTask(config=self.config) 

106 with self.assertRaises(ValueError): 

107 results = psfMatch.run(self.exp, psfModel) 

108 

109 def testPropagateVisitInfo(self): 

110 """Test that a PSF-matched exposure preserves the original VisitInfo. 

111 """ 

112 self.exp.getInfo().setVisitInfo(makeVisitInfo()) 

113 psfModel = measAlg.DoubleGaussianPsf(self.ksize + 2, self.ksize + 2, self.sigma2) 

114 psfMatch = ipDiffim.ModelPsfMatchTask(config=self.config) 

115 psfMatchedExposure = psfMatch.run(self.exp, psfModel).psfMatchedExposure 

116 self.assertEqual(psfMatchedExposure.getInfo().getVisitInfo(), 

117 self.exp.getInfo().getVisitInfo()) 

118 

119 def tearDown(self): 

120 del self.exp 

121 del self.subconfig 

122 

123 

124def makeVisitInfo(): 

125 """Return a non-NaN visitInfo.""" 

126 return afwImage.VisitInfo(exposureId=10313423, 

127 exposureTime=10.01, 

128 darkTime=11.02, 

129 date=dafBase.DateTime(65321.1, dafBase.DateTime.MJD, dafBase.DateTime.TAI), 

130 ut1=12345.1, 

131 era=45.1*geom.degrees, 

132 boresightRaDec=geom.SpherePoint(23.1, 73.2, geom.degrees), 

133 boresightAzAlt=geom.SpherePoint(134.5, 33.3, geom.degrees), 

134 boresightAirmass=1.73, 

135 boresightRotAngle=73.2*geom.degrees, 

136 rotType=afwImage.RotType.SKY, 

137 observatory=Observatory( 

138 11.1*geom.degrees, 22.2*geom.degrees, 0.333), 

139 weather=Weather(1.1, 2.2, 34.5), 

140 ) 

141 

142 

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

144 pass 

145 

146 

147def setup_module(module): 

148 lsst.utils.tests.init() 

149 

150 

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

152 lsst.utils.tests.init() 

153 unittest.main()