Coverage for tests/test_HealpixPixelization.py: 24%

90 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-15 02:30 -0700

1# 

2# LSST Data Management System 

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

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# 

22import pickle 

23import unittest 

24 

25import healpy as hp 

26import numpy as np 

27 

28try: 

29 import yaml 

30except ImportError: 

31 yaml = None 

32 

33from lsst.sphgeom import Angle, Box, Circle, ConvexPolygon, HealpixPixelization, LonLat, UnitVector3d 

34 

35 

36class HealpixPixelizationTestCase(unittest.TestCase): 

37 def test_construction(self): 

38 """Test construction of a HealpixPixelization.""" 

39 with self.assertRaises(ValueError): 

40 HealpixPixelization(-1) 

41 h1 = HealpixPixelization(5) 

42 self.assertEqual(h1.level, 5) 

43 self.assertEqual(h1.getLevel(), 5) 

44 self.assertEqual(h1.nside, 32) 

45 h2 = HealpixPixelization(6) 

46 h3 = HealpixPixelization(h2.level) 

47 self.assertNotEqual(h1, h2) 

48 self.assertEqual(h2, h3) 

49 

50 def test_indexing(self): 

51 """Test indexing of HealpixPixelization.""" 

52 h = HealpixPixelization(5) 

53 vec = UnitVector3d(1, 1, 1) 

54 lonlat = LonLat(vec) 

55 pix = hp.ang2pix( 

56 h.nside, lonlat.getLon().asDegrees(), lonlat.getLat().asDegrees(), lonlat=True, nest=True 

57 ) 

58 self.assertEqual(h.index(UnitVector3d(1, 1, 1)), pix) 

59 

60 def test_pixel(self): 

61 """Test pixel polygon of HealpixPixelization.""" 

62 h = HealpixPixelization(5) 

63 pix_poly = h.pixel(10) 

64 self.assertIsInstance(pix_poly, ConvexPolygon) 

65 

66 def test_envelope(self): 

67 """Test envelope method of HealpixPixelization.""" 

68 # Make the hardest intersection: a region that _just_ 

69 # touches a healpix pixel. 

70 h = HealpixPixelization(5) 

71 pix = hp.ang2pix(h.nside, 50.0, 20.0, lonlat=True, nest=True) 

72 

73 corners = hp.boundaries(h.nside, pix, step=1, nest=True) 

74 corners_ra, corners_dec = hp.vec2ang(corners.T, lonlat=True) 

75 

76 # Take the southernmost corner... 

77 smost = np.argmin(corners_dec) 

78 

79 # Choose challenging comparison box corners: 

80 ra_range = np.array([corners_ra[smost] - 0.5, corners_ra[smost] + 0.5]) 

81 dec_range = np.array([corners_dec[smost] - 0.5, corners_dec[smost] + 1e-8]) 

82 

83 # Test the box region 

84 box = Box( 

85 point1=LonLat.fromDegrees(ra_range[0], dec_range[0]), 

86 point2=LonLat.fromDegrees(ra_range[1], dec_range[1]), 

87 ) 

88 # These pixels have been checked to completely overlap the region 

89 self._check_envelope(h, box, [98, 99, 104, 105]) 

90 

91 # Try a polygon region: 

92 poly = ConvexPolygon( 

93 [ 

94 UnitVector3d(LonLat.fromDegrees(ra_range[0], dec_range[0])), 

95 UnitVector3d(LonLat.fromDegrees(ra_range[1], dec_range[0])), 

96 UnitVector3d(LonLat.fromDegrees(ra_range[1], dec_range[1])), 

97 UnitVector3d(LonLat.fromDegrees(ra_range[0], dec_range[1])), 

98 UnitVector3d(LonLat.fromDegrees(ra_range[0], dec_range[0])), 

99 ] 

100 ) 

101 self._check_envelope(h, poly, [98, 99, 104, 105]) 

102 

103 # Try a circle region 

104 circle = Circle( 

105 center=UnitVector3d( 

106 LonLat.fromDegrees((ra_range[0] + ra_range[1]) / 2.0, (dec_range[0] + dec_range[1]) / 2.0) 

107 ), 

108 angle=Angle.fromDegrees((dec_range[1] - dec_range[0]) / 2.0), 

109 ) 

110 self._check_envelope(h, circle, [98, 99, 104, 105]) 

111 

112 def _check_envelope(self, pixelization, region, check_pixels): 

113 """Check the envelope from a region. 

114 

115 Parameters 

116 ---------- 

117 pixelization : `lsst.sphgeom.HealpixPixelization` 

118 region : `lsst.sphgeom.Region` 

119 check_pixels : `list` [`int`] 

120 """ 

121 pixel_range = pixelization.envelope(region) 

122 

123 pixels = [] 

124 for r in pixel_range.ranges(): 

125 pixels.extend(range(r[0], r[1])) 

126 

127 self.assertEqual(pixels, check_pixels) 

128 

129 def test_interior(self): 

130 """Test interior method of HealpixPixelization.""" 

131 

132 h = HealpixPixelization(5) 

133 pix = hp.ang2pix(h.nside, 50.0, 20.0, lonlat=True, nest=True) 

134 

135 corners = hp.boundaries(h.nside, pix, step=1, nest=True) 

136 corners_ra, corners_dec = hp.vec2ang(corners.T, lonlat=True) 

137 

138 ra_range = np.array([corners_ra.min() - 1.0, corners_ra.max() + 1.0]) 

139 dec_range = np.array([corners_dec.min() - 1.0, corners_dec.max() + 1.0]) 

140 

141 # Test the box region 

142 box = Box( 

143 point1=LonLat.fromDegrees(ra_range[0], dec_range[0]), 

144 point2=LonLat.fromDegrees(ra_range[1], dec_range[1]), 

145 ) 

146 # These pixels have been checked to completely overlap the region 

147 self._check_interior(h, box, [pix]) 

148 

149 # Try a polygon region: 

150 poly = ConvexPolygon( 

151 [ 

152 UnitVector3d(LonLat.fromDegrees(ra_range[0], dec_range[0])), 

153 UnitVector3d(LonLat.fromDegrees(ra_range[1], dec_range[0])), 

154 UnitVector3d(LonLat.fromDegrees(ra_range[1], dec_range[1])), 

155 UnitVector3d(LonLat.fromDegrees(ra_range[0], dec_range[1])), 

156 UnitVector3d(LonLat.fromDegrees(ra_range[0], dec_range[0])), 

157 ] 

158 ) 

159 self._check_interior(h, poly, [pix]) 

160 

161 # Try a circle region 

162 circle = Circle( 

163 center=UnitVector3d( 

164 LonLat.fromDegrees((ra_range[0] + ra_range[1]) / 2.0, (dec_range[0] + dec_range[1]) / 2.0) 

165 ), 

166 angle=Angle.fromDegrees(2.5), 

167 ) 

168 self._check_interior(h, circle, [pix]) 

169 

170 def _check_interior(self, pixelization, region, check_pixels): 

171 """Check the interior from a region. 

172 

173 Parameters 

174 ---------- 

175 pixelization : `lsst.sphgeom.HealpixPixelization` 

176 region : `lsst.sphgeom.Region` 

177 check_pixels : `list` [`int`] 

178 """ 

179 pixel_range = pixelization.interior(region) 

180 

181 pixels = [] 

182 for r in pixel_range.ranges(): 

183 pixels.extend(range(r[0], r[1])) 

184 

185 self.assertEqual(pixels, check_pixels) 

186 

187 def test_index_to_string(self): 

188 """Test converting index to string of HealpixPixelization.""" 

189 h = HealpixPixelization(5) 

190 self.assertEqual(h.toString(0), str(0)) 

191 self.assertEqual(h.toString(100), str(100)) 

192 

193 def test_string(self): 

194 """Test string representation of HealpixPixelization.""" 

195 h = HealpixPixelization(5) 

196 self.assertEqual(str(h), "HealpixPixelization(5)") 

197 self.assertEqual(str(h), repr(h)) 

198 self.assertEqual(h, eval(repr(h), dict(HealpixPixelization=HealpixPixelization))) 

199 

200 def test_pickle(self): 

201 """Test pickling of HealpixPixelization.""" 

202 a = HealpixPixelization(5) 

203 b = pickle.loads(pickle.dumps(a)) 

204 self.assertEqual(a, b) 

205 

206 @unittest.skipIf(not yaml, "YAML module can not be imported") 

207 def test_yaml(self): 

208 """Test yaml representation of HealpixPixelization.""" 

209 a = HealpixPixelization(5) 

210 b = yaml.safe_load(yaml.dump(a)) 

211 self.assertEqual(a, b) 

212 

213 

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

215 unittest.main()