Coverage for tests/test_setValidPolygonIntersect.py: 31%

Shortcuts 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

62 statements  

1# 

2# LSST Data Management System 

3# Copyright 2008-2015 AURA/LSST. 

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 <https://www.lsstcorp.org/LegalNotices/>. 

21# 

22 

23""" 

24Tests setting of valid focal plane polygon intersection with ccd corners 

25 

26Run with: 

27 python testSetValidPolygonIntersect.py 

28or 

29 python 

30 >>> import testSetValidPolygonIntersect; testSetValidPolygonIntersect.run() 

31""" 

32 

33import unittest 

34 

35import numpy as np 

36 

37import lsst.utils.tests 

38import lsst.geom 

39import lsst.afw.geom as afwGeom 

40import lsst.afw.image as afwImage 

41from lsst.ip.isr.vignette import setValidPolygonCcdIntersect 

42from lsst.afw.cameraGeom.testUtils import DetectorWrapper 

43from lsst.afw.cameraGeom import PIXELS, FOCAL_PLANE 

44 

45 

46def makeCircularPolygon(fpCenterX, fpCenterY, fpRadius, numPolygonPoints): 

47 theta = np.linspace(0, 2*np.pi, num=numPolygonPoints, endpoint=False) 

48 xx = fpRadius*np.cos(theta) + fpCenterX 

49 yy = fpRadius*np.sin(theta) + fpCenterY 

50 points = np.array([xx, yy]).transpose() 

51 polygon = afwGeom.Polygon([lsst.geom.Point2D(x, y) for x, y in reversed(points)]) 

52 return polygon 

53 

54 

55def makeSquarePolygon(fpX0, fpY0, fpSize): 

56 xx = [fpX0, fpX0, fpX0 + fpSize - 1, fpX0 + fpSize - 1, fpX0] 

57 yy = [fpY0, fpY0 + fpSize - 1, fpY0 + fpSize - 1, fpY0, fpY0] 

58 points = np.array([xx, yy]).transpose() 

59 polygon = afwGeom.Polygon([lsst.geom.Point2D(x, y) for x, y in points]) 

60 return polygon 

61 

62 

63class SetValidPolygonIntersectTestCase(lsst.utils.tests.TestCase): 

64 """A test case for setting of valid focal plane polygon intersection with ccd corners """ 

65 

66 def testSetPolygonIntersect(self): 

67 # Create a detector 

68 detector = DetectorWrapper().detector 

69 numPolygonPoints = 50 

70 # Create an exposure with bounding box defined by detector 

71 exposure = afwImage.ExposureF(detector.getBBox()) 

72 exposure.setDetector(detector) 

73 

74 pixelSizeMm = exposure.getDetector().getPixelSize()[0] 

75 

76 pixX0 = exposure.getX0() 

77 pixY0 = exposure.getY0() 

78 pixX1 = pixX0 + exposure.getWidth() - 1 

79 pixY1 = pixY0 + exposure.getHeight() - 1 

80 

81 fpCenter = exposure.getDetector().getCenter(FOCAL_PLANE) 

82 fpCenterX = fpCenter[0] 

83 fpCenterY = fpCenter[1] 

84 pixCenter = exposure.getDetector().getCenter(PIXELS) 

85 

86 # Make a polygon that encompases entire ccd (radius of 2*max of width/height) 

87 fpRadius = 2.0*max(exposure.getWidth()*pixelSizeMm, exposure.getHeight()*pixelSizeMm) 

88 fpPolygon = makeCircularPolygon(fpCenterX, fpCenterY, fpRadius, numPolygonPoints) 

89 # Set the polygon that is the intersection of fpPolygon and ccd 

90 setValidPolygonCcdIntersect(exposure, fpPolygon) 

91 # Since the ccd is fully contained in the fpPolygon, the intersection should be the ccdPolygon itself 

92 ccdPolygonPix = afwGeom.Polygon(exposure.getDetector().getCorners(PIXELS)) 

93 self.assertEqual(exposure.getInfo().getValidPolygon(), ccdPolygonPix) 

94 

95 # Make a polygon that is entirely within, but smaller than, the ccd 

96 # (radius of 0.2*min of width/height) 

97 fpRadius = 0.2*min(exposure.getWidth()*pixelSizeMm, exposure.getHeight()*pixelSizeMm) 

98 fpPolygon = makeCircularPolygon(fpCenterX, fpCenterY, fpRadius, numPolygonPoints) 

99 # Set the polygon that is the intersection of fpPolygon and ccd 

100 setValidPolygonCcdIntersect(exposure, fpPolygon) 

101 # all vertices of polygon should be contained within the ccd 

102 for x in exposure.getInfo().getValidPolygon(): 

103 self.assertTrue(ccdPolygonPix.contains(lsst.geom.Point2D(x))) 

104 # intersection is smaller than the ccd 

105 self.assertNotEqual(exposure.getInfo().getValidPolygon(), ccdPolygonPix) 

106 

107 # make a simple square polygon that partly intersects the ccd, centered at ccd center 

108 fpPolygonSize = max(exposure.getWidth()*pixelSizeMm, exposure.getHeight()*pixelSizeMm) 

109 fpPolygon = makeSquarePolygon(fpCenterX, fpCenterY, fpPolygonSize) 

110 setValidPolygonCcdIntersect(exposure, fpPolygon) 

111 # Check that the polygon contains the central pixel (offset by one to actually be "contained") 

112 pixCenterPlusOne = lsst.geom.Point2D(pixCenter[0] + 1, pixCenter[1] + 1) 

113 self.assertTrue(exposure.getInfo().getValidPolygon().contains(lsst.geom.Point2D(pixCenterPlusOne))) 

114 # Check that the polygon contains the upper right ccd edge 

115 self.assertTrue(exposure.getInfo().getValidPolygon().contains(lsst.geom.Point2D(pixX1, pixY1))) 

116 

117 

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

119 pass 

120 

121 

122def setup_module(module): 

123 lsst.utils.tests.init() 

124 

125 

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

127 lsst.utils.tests.init() 

128 unittest.main()