Coverage for tests/test_suspectMasking.py: 23%

81 statements  

« prev     ^ index     » next       coverage.py v7.2.1, created at 2023-03-12 03:09 -0700

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 default 

82 """ 

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

84 

85 isrConfig = ipIsr.IsrTask.ConfigClass() 

86 isrConfig.suspectMaskName = AltMaskName 

87 isrTask = ipIsr.IsrTask(config=isrConfig) 

88 

89 maxVal = 32000 

90 fracSuspect = 0.3 

91 suspectLevel = maxVal*(1 - fracSuspect) 

92 

93 bbox = self.ampInfo.getRawBBox() 

94 self.ampInfo.setSuspectLevel(suspectLevel) 

95 maskedImage = makeRampMaskedImage(bbox, 0, maxVal) 

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

97 desSetArr = imArr >= suspectLevel 

98 exposure = afwImage.ExposureF(maskedImage) 

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

100 isrTask.suspectDetection(exposure, self.ampInfo) 

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

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

103 measSetArr = maskArr == suspectMask 

104 self.assertImagesEqual(desSetArr, measSetArr) 

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

106 

107 

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

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

110 

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

112 Variance values equal image values + 100 

113 Mask values equal 0 

114 """ 

115 mi = imgClass(bbox) 

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

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

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

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

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

121 imageArr[:] = imData 

122 varianceArr[:] = 100 + imData 

123 maskArr[:] = 0 

124 return mi 

125 

126 

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

128 pass 

129 

130 

131def setup_module(module): 

132 lsst.utils.tests.init() 

133 

134 

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

136 lsst.utils.tests.init() 

137 unittest.main()