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

1import os 

2import unittest 

3 

4 

5import lsst.utils.tests 

6import lsst.utils 

7import lsst.afw.image as afwImage 

8import lsst.afw.math as afwMath 

9import lsst.geom as geom 

10import lsst.ip.diffim as ipDiffim 

11import lsst.ip.diffim.diffimTools as diffimTools 

12import lsst.log.utils as logUtils 

13import lsst.pex.config as pexConfig 

14 

15logUtils.traceSetAt("ip.diffim", 2) 

16 

17# known input images 

18try: 

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

20except Exception: 

21 defDataDir = None 

22 

23 

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

25 

26 def setUp(self): 

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

28 self.subconfig = self.config.kernel.active 

29 self.ps = pexConfig.makePropertySet(self.subconfig) 

30 self.kSize = self.ps['kernelSize'] 

31 

32 # gaussian reference kernel 

33 self.gSize = self.kSize 

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

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

36 

37 if defDataDir: 

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

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

40 self.templateImage = afwImage.MaskedImageF(defImagePath) 

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

42 

43 afwMath.convolve(self.scienceImage, self.templateImage, self.gaussKernel, False) 

44 

45 def tearDown(self): 

46 del self.config 

47 del self.ps 

48 del self.gaussFunction 

49 del self.gaussKernel 

50 if defDataDir: 

51 del self.templateImage 

52 del self.scienceImage 

53 

54 @unittest.skipIf(not defDataDir, 

55 "Warning: afwdata is not set up; not running KernelCandidateDetection.py") 

56 def testGetCollection(self): 

57 # NOTE - you need to subtract off background from the image 

58 # you run detection on. Here it is the template. 

59 bgConfig = self.subconfig.afwBackgroundConfig 

60 diffimTools.backgroundSubtract(bgConfig, [self.templateImage, ]) 

61 

62 detConfig = self.subconfig.detectionConfig 

63 maskPlane = detConfig.badMaskPlanes[0] 

64 maskVal = afwImage.Mask.getPlaneBitMask(maskPlane) 

65 

66 kcDetect = ipDiffim.KernelCandidateDetectionF(pexConfig.makePropertySet(detConfig)) 

67 kcDetect.apply(self.templateImage, self.scienceImage) 

68 fpList1 = kcDetect.getFootprints() 

69 

70 self.assertNotEqual(len(fpList1), 0) 

71 

72 for fp in fpList1: 

73 bbox = fp.getBBox() 

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

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

76 tmask = tmi.getMask() 

77 smask = smi.getMask() 

78 

79 for j in range(tmask.getHeight()): 

80 for i in range(tmask.getWidth()): 

81 # No masked pixels in either image 

82 self.assertEqual(tmask[i, j, afwImage.LOCAL], 0) 

83 self.assertEqual(smask[i, j, afwImage.LOCAL], 0) 

84 

85 # add a masked pixel to the template image and make sure you don't get it 

86 tp = geom.Point2I(tmask.getWidth()//2, tmask.getHeight()//2) 

87 self.templateImage.mask[fpList1[0].getBBox(), afwImage.LOCAL][tp, afwImage.LOCAL] = maskVal 

88 kcDetect.apply(self.templateImage, self.scienceImage) 

89 fpList2 = kcDetect.getFootprints() 

90 self.assertEqual(len(fpList2), (len(fpList1)-1)) 

91 

92 # add a masked pixel to the science image and make sure you don't get it 

93 sp = geom.Point2I(smask.getWidth()//2, smask.getHeight()//2) 

94 self.scienceImage.mask[fpList1[1].getBBox(), afwImage.LOCAL][sp, afwImage.LOCAL] = maskVal 

95 self.scienceImage.mask[fpList1[2].getBBox(), afwImage.LOCAL][sp, afwImage.LOCAL] = maskVal 

96 kcDetect.apply(self.templateImage, self.scienceImage) 

97 fpList3 = kcDetect.getFootprints() 

98 self.assertEqual(len(fpList3), (len(fpList1)-3)) 

99 

100##### 

101 

102 

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

104 pass 

105 

106 

107def setup_module(module): 

108 lsst.utils.tests.init() 

109 

110 

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

112 lsst.utils.tests.init() 

113 unittest.main()