Coverage for tests/test_endpoint.py: 16%

111 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2022-09-20 02:20 -0700

1""" 

2LSST Data Management System 

3See COPYRIGHT file at the top of the source tree. 

4 

5This product includes software developed by the 

6LSST Project (http://www.lsst.org/). 

7 

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

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

10the Free Software Foundation, either version 3 of the License, or 

11(at your option) any later version. 

12 

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

14but WITHOUT ANY WARRANTY; without even the implied warranty of 

15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

16GNU General Public License for more details. 

17 

18You should have received a copy of the LSST License Statement and 

19the GNU General Public License along with this program. If not, 

20see <http://www.lsstcorp.org/LegalNotices/>. 

21""" 

22import unittest 

23 

24from numpy.testing import assert_allclose, assert_equal 

25import astshim 

26 

27import lsst.utils.tests 

28import lsst.geom 

29import lsst.afw.geom as afwGeom 

30from lsst.pex.exceptions import InvalidParameterError 

31from lsst.afw.geom.testUtils import TransformTestBaseClass 

32 

33 

34class EndpointTestCase(TransformTestBaseClass): 

35 

36 def setUp(self): 

37 self.longMessage = True 

38 

39 def testSpherePointEndpoint(self): 

40 self.assertEqual("SpherePoint", afwGeom.SpherePointEndpoint.getClassPrefix()) 

41 endpoint = afwGeom.SpherePointEndpoint() 

42 self.checkEndpointBasics( 

43 endpoint=endpoint, pointType=lsst.geom.SpherePoint, nAxes=2) 

44 self.assertEqual(repr(endpoint), "lsst.afw.geom.SpherePointEndpoint()") 

45 self.assertEqual("{}".format(endpoint), "SpherePointEndpoint()") 

46 

47 for doPermute in (False, True): 

48 frame = astshim.SkyFrame() 

49 if doPermute: 

50 frame.permAxes([2, 1]) 

51 self.assertEqual(frame.lonAxis, 2) 

52 self.assertEqual(frame.latAxis, 1) 

53 else: 

54 self.assertEqual(frame.lonAxis, 1) 

55 self.assertEqual(frame.latAxis, 2) 

56 endpoint.normalizeFrame(frame) 

57 # the normalized frame always has axis in order Lon, Lat 

58 self.assertEqual(frame.lonAxis, 1) 

59 self.assertEqual(frame.latAxis, 2) 

60 

61 badFrame = astshim.Frame(2) 

62 with self.assertRaises(InvalidParameterError): 

63 endpoint.normalizeFrame(badFrame) 

64 

65 newFrame = endpoint.makeFrame() 

66 self.assertEqual(type(newFrame), astshim.SkyFrame) 

67 self.assertEqual(newFrame.lonAxis, 1) 

68 self.assertEqual(newFrame.latAxis, 2) 

69 

70 def testPoint2Endpoint(self): 

71 self.assertEqual("Point2", afwGeom.Point2Endpoint.getClassPrefix()) 

72 endpoint = afwGeom.Point2Endpoint() 

73 self.checkEndpointBasics( 

74 endpoint=endpoint, pointType=lsst.geom.Point2D, nAxes=2) 

75 self.assertEqual(repr(endpoint), "lsst.afw.geom.Point2Endpoint()") 

76 self.assertEqual("{}".format(endpoint), "Point2Endpoint()") 

77 

78 # normalize does not check the # of axes 

79 for n in range(4): 

80 frame1 = astshim.Frame(n) 

81 try: 

82 endpoint.normalizeFrame(frame1) 

83 except Exception as e: 

84 self.fail( 

85 "endpoint.normalizeFrame(Frame({})) failed with error = {}".format(n, e)) 

86 badFrame = astshim.SkyFrame() 

87 with self.assertRaises(InvalidParameterError): 

88 endpoint.normalizeFrame(badFrame) 

89 

90 def testGenericEndpoint(self): 

91 self.assertEqual("Generic", afwGeom.GenericEndpoint.getClassPrefix()) 

92 for nAxes in (1, 2, 3, 4, 5): 

93 endpoint = afwGeom.GenericEndpoint(nAxes) 

94 self.checkEndpointBasics( 

95 endpoint=endpoint, pointType=list, nAxes=nAxes) 

96 self.assertEqual( 

97 repr(endpoint), "lsst.afw.geom.GenericEndpoint({})".format(nAxes)) 

98 self.assertEqual("{}".format(endpoint), 

99 "GenericEndpoint({})".format(nAxes)) 

100 

101 newFrame = endpoint.makeFrame() 

102 self.assertEqual(type(newFrame), astshim.Frame) 

103 self.assertEqual(newFrame.nAxes, nAxes) 

104 

105 for nAxes in (-1, 0): 

106 with self.assertRaises(InvalidParameterError): 

107 afwGeom.GenericEndpoint(nAxes) 

108 

109 def checkEndpointBasics(self, endpoint, pointType, nAxes): 

110 isAngle = pointType == lsst.geom.SpherePoint # point components are Angles 

111 

112 baseMsg = "endpoint={}, pointType={}, nAxes={}".format( 

113 endpoint, pointType, nAxes) 

114 

115 self.assertEqual(endpoint.nAxes, nAxes, msg=baseMsg) 

116 

117 # generate enough points to be interesting, but no need to overdo it 

118 nPoints = 4 

119 

120 rawData = self.makeRawArrayData(nPoints=nPoints, nAxes=nAxes) 

121 pointList = endpoint.arrayFromData(rawData) 

122 self.assertEqual(endpoint.getNPoints(pointList), nPoints, msg=baseMsg) 

123 if isinstance(endpoint, afwGeom.GenericEndpoint): 

124 self.assertEqual(len(pointList[0]), nPoints, msg=baseMsg) 

125 assert_equal(rawData, pointList) 

126 else: 

127 self.assertEqual(len(pointList), nPoints, msg=baseMsg) 

128 for i, point in enumerate(pointList): 

129 for axis in range(nAxes): 

130 msg = "{}, endpoint={}, i={}, point={}".format( 

131 baseMsg, endpoint, i, point) 

132 if isAngle: 

133 desAngle = rawData[axis, i] * lsst.geom.radians 

134 self.assertAnglesAlmostEqual( 

135 point[axis], desAngle, msg=msg) 

136 else: 

137 self.assertAlmostEqual( 

138 point[axis], rawData[axis, i], msg=msg) 

139 

140 rawDataRoundTrip = endpoint.dataFromArray(pointList) 

141 self.assertEqual(rawData.shape, rawDataRoundTrip.shape, msg=baseMsg) 

142 self.assertFloatsAlmostEqual(rawData, rawDataRoundTrip, msg=baseMsg) 

143 

144 pointData = self.makeRawPointData(nAxes=nAxes) 

145 point = endpoint.pointFromData(pointData) 

146 self.assertEqual(type(point), pointType, msg=baseMsg) 

147 for axis in range(nAxes): 

148 msg = "{}, axis={}".format(baseMsg, axis) 

149 if isAngle: 

150 desAngle = pointData[axis] * lsst.geom.radians 

151 self.assertAnglesAlmostEqual(point[axis], desAngle, msg=msg) 

152 else: 

153 self.assertAlmostEqual(point[axis], pointData[axis], msg=msg) 

154 

155 pointDataRoundTrip = endpoint.dataFromPoint(point) 

156 assert_allclose(pointData, pointDataRoundTrip, err_msg=baseMsg) 

157 

158 def testEndpointEquals(self): 

159 """Test Endpoint == Endpoint 

160 """ 

161 for i1, point1 in enumerate(self.makeEndpoints()): 

162 for i2, point2 in enumerate(self.makeEndpoints()): 

163 if i1 == i2: 

164 self.assertTrue(point1 == point2) 

165 self.assertFalse(point1 != point2) 

166 else: 

167 self.assertFalse(point1 == point2) 

168 self.assertTrue(point1 != point2) 

169 

170 

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

172 pass 

173 

174 

175def setup_module(module): 

176 lsst.utils.tests.init() 

177 

178 

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

180 lsst.utils.tests.init() 

181 unittest.main()