Coverage for tests/test_suspectMasking.py: 23%

81 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-08 11:41 +0000

1# 

2# LSST Data Management System 

3# Copyright 2008, 2009, 2010 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# 

22 

23import unittest 

24 

25import numpy as np 

26 

27import lsst.utils.tests 

28import lsst.geom 

29import lsst.afw.image as afwImage 

30import lsst.afw.cameraGeom as cameraGeom 

31import lsst.ip.isr as ipIsr 

32 

33 

34class IsrTestCases(lsst.utils.tests.TestCase): 

35 

36 def setUp(self): 

37 ampInfo = cameraGeom.Amplifier.Builder() 

38 

39 ampInfo.setRawBBox(lsst.geom.Box2I(lsst.geom.Point2I(-5, 7), lsst.geom.Extent2I(53, 104))) 

40 ampInfo.setSuspectLevel(25000) 

41 self.ampInfo = ampInfo 

42 self.isrTask = ipIsr.IsrTask() 

43 

44 def tearDown(self): 

45 self.ampInfo = None 

46 self.isrTask = None 

47 

48 def testBasicMasking(self): 

49 """Test that masking works 

50 """ 

51 maxVal = 32000 

52 fracSuspect = 0.3 

53 suspectLevel = maxVal*(1 - fracSuspect) 

54 

55 bbox = self.ampInfo.getRawBBox() 

56 self.ampInfo.setSuspectLevel(suspectLevel) 

57 maskedImage = makeRampMaskedImage(bbox, 0, maxVal) 

58 imArr = maskedImage.getImage().getArray() 

59 desSetArr = imArr >= suspectLevel 

60 exposure = afwImage.ExposureF(maskedImage) 

61 inMaskedImage = maskedImage.Factory(maskedImage, True) # deep copy 

62 self.isrTask.suspectDetection(exposure, self.ampInfo) 

63 maskArr = maskedImage.getMask().getArray() 

64 suspectMask = maskedImage.getMask().getPlaneBitMask("SUSPECT") 

65 measSetArr = maskArr == suspectMask 

66 self.assertImagesEqual(desSetArr, measSetArr) 

67 self.assertMaskedImagesAlmostEqual(inMaskedImage, maskedImage, doMask=False) 

68 

69 def testNanLevel(self): 

70 """Test that setting the suspect level to nan disables masking 

71 """ 

72 bbox = self.ampInfo.getRawBBox() 

73 self.ampInfo.setSuspectLevel(float("nan")) 

74 maskedImage = makeRampMaskedImage(bbox, 0, 32000) 

75 exposure = afwImage.ExposureF(maskedImage) 

76 inMaskedImage = maskedImage.Factory(maskedImage, True) # deep copy 

77 self.isrTask.suspectDetection(exposure, self.ampInfo) 

78 self.assertMaskedImagesAlmostEqual(inMaskedImage, maskedImage) 

79 

80 def testRenamedMasking(self): 

81 """Test that masking works using some other mask name instead of the 

82 default. 

83 """ 

84 AltMaskName = "BAD" # pick something that exists for simplicity 

85 

86 isrConfig = ipIsr.IsrTask.ConfigClass() 

87 isrConfig.suspectMaskName = AltMaskName 

88 isrTask = ipIsr.IsrTask(config=isrConfig) 

89 

90 maxVal = 32000 

91 fracSuspect = 0.3 

92 suspectLevel = maxVal*(1 - fracSuspect) 

93 

94 bbox = self.ampInfo.getRawBBox() 

95 self.ampInfo.setSuspectLevel(suspectLevel) 

96 maskedImage = makeRampMaskedImage(bbox, 0, maxVal) 

97 imArr = maskedImage.getImage().getArray() 

98 desSetArr = imArr >= suspectLevel 

99 exposure = afwImage.ExposureF(maskedImage) 

100 inMaskedImage = maskedImage.Factory(maskedImage, True) # deep copy 

101 isrTask.suspectDetection(exposure, self.ampInfo) 

102 maskArr = maskedImage.getMask().getArray() 

103 suspectMask = maskedImage.getMask().getPlaneBitMask(AltMaskName) 

104 measSetArr = maskArr == suspectMask 

105 self.assertImagesEqual(desSetArr, measSetArr) 

106 self.assertMaskedImagesAlmostEqual(inMaskedImage, maskedImage, doMask=False) 

107 

108 

109def makeRampMaskedImage(bbox, minVal, maxVal, imgClass=afwImage.MaskedImageF): 

110 """Make a ramp image of the specified size and image class 

111 

112 Image values start from 0 at the lower left corner and increase by 1 along 

113 rows. 

114 Variance values equal image values + 100 

115 Mask values equal 0 

116 """ 

117 mi = imgClass(bbox) 

118 imageArr = mi.getImage().getArray() 

119 varianceArr = mi.getVariance().getArray() 

120 maskArr = mi.getMask().getArray() 

121 imData = np.linspace(minVal, maxVal, imageArr.size) 

122 imData.shape = (bbox.getHeight(), bbox.getWidth()) 

123 imageArr[:] = imData 

124 varianceArr[:] = 100 + imData 

125 maskArr[:] = 0 

126 return mi 

127 

128 

129class MemoryTester(lsst.utils.tests.MemoryTestCase): 

130 pass 

131 

132 

133def setup_module(module): 

134 lsst.utils.tests.init() 

135 

136 

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

138 lsst.utils.tests.init() 

139 unittest.main()