Coverage for tests/test_negative.py: 23%

84 statements  

« prev     ^ index     » next       coverage.py v6.4.1, created at 2022-07-11 07:09 +0000

1# This file is part of meas_algorithms. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <https://www.gnu.org/licenses/>. 

21 

22import unittest 

23 

24import lsst.geom 

25import lsst.afw.table as afwTable 

26import lsst.daf.base as dafBase 

27from lsst.meas.algorithms import SourceDetectionTask 

28from lsst.meas.base import SingleFrameMeasurementTask as SourceMeasurementTask 

29from lsst.meas.algorithms.testUtils import plantSources 

30import lsst.utils.tests 

31 

32try: 

33 display 

34except NameError: 

35 display = False 

36else: 

37 import lsst.afw.display as afwDisplay 

38 afwDisplay.setDefaultMaskTransparency(75) 

39 

40 

41class NegativeMeasurementTestCase(lsst.utils.tests.TestCase): 

42 """A test case for negative objects. 

43 """ 

44 

45 def testBasics(self): 

46 bbox = lsst.geom.Box2I(lsst.geom.Point2I(256, 100), lsst.geom.Extent2I(128, 127)) 

47 minCounts = 2000 

48 maxCounts = 20000 

49 starSigma = 1.5 

50 numX = 4 

51 numY = 4 

52 coordList = self.makeCoordList( 

53 bbox=bbox, 

54 numX=numX, 

55 numY=numY, 

56 minCounts=minCounts, 

57 maxCounts=maxCounts, 

58 sigma=starSigma, 

59 ) 

60 kwid = 11 

61 sky = 2000 

62 addPoissonNoise = True 

63 exposure = plantSources(bbox=bbox, kwid=kwid, sky=sky, coordList=coordList, 

64 addPoissonNoise=addPoissonNoise) 

65 

66 if display: 

67 disp = afwDisplay.Display(frame=1) 

68 disp.mtv(exposure, title=self._testMethodName + ": image with -ve sources") 

69 

70 schema = afwTable.SourceTable.makeMinimalSchema() 

71 config = SourceDetectionTask.ConfigClass() 

72 config.reEstimateBackground = False 

73 config.thresholdPolarity = 'both' 

74 detection = SourceDetectionTask(config=config, schema=schema) 

75 algMetadata = dafBase.PropertyList() 

76 measurement = SourceMeasurementTask(schema=schema, algMetadata=algMetadata) 

77 

78 table = afwTable.SourceTable.make(schema) 

79 detections = detection.run(table, exposure) 

80 sources = detections.sources 

81 fpSets = detections.fpSets 

82 

83 self.assertEqual(len(sources), numX*numY) 

84 self.assertEqual(fpSets.numPos, numX*numY/2) 

85 self.assertEqual(fpSets.numNeg, numX*numY/2) 

86 

87 measurement.run(sources, exposure) 

88 

89 nGoodCent = 0 

90 nGoodShape = 0 

91 for s in sources: 

92 cent = s.getCentroid() 

93 shape = s.getShape() 

94 

95 if cent[0] == cent[0] and cent[1] == cent[1]: 

96 nGoodCent += 1 

97 

98 if (shape.getIxx() == shape.getIxx() 

99 and shape.getIyy() == shape.getIyy() 

100 and shape.getIxy() == shape.getIxy()): 

101 nGoodShape += 1 

102 

103 if display: 

104 xy = cent[0], cent[1] 

105 disp.dot('+', *xy) 

106 disp.dot(shape, *xy, ctype=afwDisplay.RED) 

107 

108 self.assertEqual(nGoodCent, numX*numY) 

109 self.assertEqual(nGoodShape, numX*numY) 

110 

111 def makeCoordList(self, bbox, numX, numY, minCounts, maxCounts, sigma): 

112 """Make a coordList for makeExposure. 

113 """ 

114 dX = bbox.getWidth()/float(numX) 

115 dY = bbox.getHeight()/float(numY) 

116 minX = bbox.getMinX() + (dX/2.0) 

117 minY = bbox.getMinY() + (dY/2.0) 

118 dCounts = (maxCounts - minCounts)/(numX*numY/2 - 1) 

119 

120 coordList = [] 

121 counts = minCounts 

122 for i in range(numX): 

123 x = minX + (dX*i) 

124 for j in range(numY): 

125 y = minY + (dY*j) 

126 if j%2 == 0: 

127 coordList.append([x, y, counts, sigma]) 

128 else: 

129 coordList.append([x, y, -counts, sigma]) 

130 counts += dCounts 

131 return coordList 

132 

133 

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

135 pass 

136 

137 

138def setup_module(module): 

139 lsst.utils.tests.init() 

140 

141 

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

143 lsst.utils.tests.init() 

144 unittest.main()