Coverage for tests/test_uniform_grid.py: 18%

99 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-10 04:37 -0700

1# This file is part of cell_coadds. 

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 

22from __future__ import annotations 

23 

24import pickle 

25import unittest 

26 

27import lsst.utils.tests 

28from lsst.cell_coadds import UniformGrid 

29from lsst.geom import Box2I, Extent2I, Point2I 

30from lsst.skymap import Index2D 

31 

32 

33class UniformGridTestCase(unittest.TestCase): 

34 """Tests for UniformGrid and GridIndex/Index2D's C++/Python 

35 translation. 

36 """ 

37 

38 def setUp(self) -> None: # noqa: D102 

39 self.x0 = 1 

40 self.y0 = 2 

41 self.bw = 15 

42 self.bh = 12 

43 self.nx = 5 

44 self.ny = 6 

45 self.cw = 3 

46 self.ch = 2 

47 

48 self.bbox = Box2I(Point2I(x=self.x0, y=self.y0), Extent2I(x=self.bw, y=self.bh)) 

49 self.cell_size = Extent2I(x=self.cw, y=self.ch) 

50 self.shape = Index2D(x=self.nx, y=self.ny) 

51 

52 def test_ctor_bbox_cell_size(self) -> None: 

53 """Test UniformGrid after construction with (bbox, cell_size).""" 

54 grid = UniformGrid.from_bbox_cell_size(self.bbox, self.cell_size) 

55 self._check(grid) 

56 

57 def test_ctor_bbox_shape(self) -> None: 

58 """Test UniformGrid after construction with (bbox, shape).""" 

59 grid = UniformGrid.from_bbox_shape(self.bbox, self.shape) 

60 self._check(grid) 

61 

62 def test_ctor_cell_size_shape_min(self) -> None: 

63 """Test UniformGrid after construction with (cell_size, shape, min).""" 

64 grid = UniformGrid(self.cell_size, self.shape, min=self.bbox.getMin()) 

65 self._check(grid) 

66 

67 def _check(self, grid: UniformGrid) -> None: 

68 self.assertEqual(grid.bbox, self.bbox) 

69 self.assertEqual(grid.cell_size, self.cell_size) 

70 self.assertEqual(grid.shape, self.shape) 

71 self.assertIsInstance(grid.shape, Index2D) 

72 self.assertEqual(grid.bbox_of(Index2D(x=3, y=4)), Box2I(Point2I(x=10, y=10), self.cell_size)) 

73 index = grid.index(Point2I(x=11, y=9)) 

74 self.assertEqual(index, Index2D(x=3, y=3)) 

75 self.assertIsInstance(index, Index2D) 

76 

77 def test_index(self): 

78 """Test various inputs to UniformGrid.index.""" 

79 grid = UniformGrid.from_bbox_cell_size(self.bbox, self.cell_size) 

80 self.assertEqual(grid.index(self.bbox.getMin()), Index2D(x=0, y=0)) 

81 self.assertEqual(grid.index(self.bbox.getMax()), Index2D(x=4, y=5)) 

82 self.assertEqual(grid.index(Point2I(x=9, y=5)), Index2D(x=2, y=1)) 

83 self.assertEqual(grid.index(Point2I(x=9, y=6)), Index2D(x=2, y=2)) 

84 self.assertEqual(grid.index(Point2I(x=10, y=5)), Index2D(x=3, y=1)) 

85 self.assertEqual(grid.index(Point2I(x=10, y=6)), Index2D(x=3, y=2)) 

86 with self.assertRaises(ValueError): 

87 grid.index(self.bbox.getMin() - Extent2I(x=0, y=1)) 

88 with self.assertRaises(ValueError): 

89 grid.index(self.bbox.getMin() - Extent2I(x=1, y=0)) 

90 with self.assertRaises(ValueError): 

91 grid.index(self.bbox.getMin() - Extent2I(x=1, y=1)) 

92 with self.assertRaises(ValueError): 

93 grid.index(self.bbox.getMax() + Extent2I(x=0, y=1)) 

94 with self.assertRaises(ValueError): 

95 grid.index(self.bbox.getMax() + Extent2I(x=1, y=0)) 

96 with self.assertRaises(ValueError): 

97 grid.index(self.bbox.getMax() + Extent2I(x=1, y=1)) 

98 

99 def test_repr(self): 

100 """Test that UniformGrid.__repr__ round-trips through eval.""" 

101 for grid in ( 

102 UniformGrid.from_bbox_cell_size(self.bbox, self.cell_size), 

103 UniformGrid.from_bbox_shape(self.bbox, self.shape, padding=1), 

104 UniformGrid(self.cell_size, self.shape, min=self.bbox.getMin(), padding=3), 

105 ): 

106 self.assertEqual(eval(repr(grid)), grid, msg=repr(grid)) 

107 

108 @lsst.utils.tests.methodParameters(padding=[0, 3]) 

109 def test_pickle(self, padding: int): 

110 """Test that UniformGrid objects are pickleable.""" 

111 grid1 = UniformGrid.from_bbox_cell_size(self.bbox, self.cell_size, padding=padding) 

112 grid2 = pickle.loads(pickle.dumps(grid1, pickle.HIGHEST_PROTOCOL)) 

113 self.assertIsInstance(grid2, UniformGrid) 

114 self.assertEqual(grid1, grid2) 

115 

116 @lsst.utils.tests.methodParameters(padding=(1, 2, 3, 4, 7, 10)) 

117 def test_padding(self, padding: int): 

118 """Test that bbox_of and index methods work with padding > 0.""" 

119 grid = UniformGrid.from_bbox_cell_size(self.bbox, self.cell_size, padding=padding) 

120 

121 # Test all interior cells 

122 for x in range(1, self.nx - 1): 

123 for y in range(1, self.ny - 1): 

124 bbox = grid.bbox_of(Index2D(x=x, y=y)) 

125 self.assertEqual(bbox.getDimensions(), self.cell_size) 

126 self.assertEqual(bbox.getMin(), Point2I(x=x * self.cw + self.x0, y=y * self.ch + self.y0)) 

127 

128 # Test the four corners 

129 for x in (0, self.nx - 1): 

130 for y in (0, self.ny - 1): 

131 bbox = grid.bbox_of(Index2D(x=x, y=y)) 

132 self.assertEqual( 

133 bbox.getDimensions(), 

134 self.cell_size + Extent2I(padding, padding), 

135 ) 

136 self.assertEqual( 

137 bbox.getMin(), 

138 Point2I( 

139 x=x * self.cw + self.x0 - padding * (x == 0), 

140 y=y * self.ch + self.y0 - padding * (y == 0), 

141 ), 

142 ) 

143 

144 # Test along the two horizontal edges 

145 for x in (0, self.nx - 1): 

146 for y in range(1, self.ny - 1): 

147 bbox = grid.bbox_of(Index2D(x=x, y=y)) 

148 self.assertEqual(bbox.getDimensions(), self.cell_size + Extent2I(padding, 0)) 

149 self.assertEqual( 

150 bbox.getMin(), 

151 Point2I(x=x * self.cw + self.x0 - padding * (x == 0), y=y * self.ch + self.y0), 

152 ) 

153 

154 # Test along the two vertical edges 

155 for x in range(1, self.nx - 1): 

156 for y in (0, self.ny - 1): 

157 bbox = grid.bbox_of(Index2D(x=x, y=y)) 

158 self.assertEqual(bbox.getDimensions(), self.cell_size + Extent2I(0, padding)) 

159 self.assertEqual( 

160 bbox.getMin(), 

161 Point2I(x=x * self.cw + self.x0, y=y * self.ch + self.y0 - padding * (y == 0)), 

162 ) 

163 

164 # Check the mapping between positions and indices 

165 positions_index = ( 

166 # Check the four interior corners 

167 (Point2I(x=self.x0, y=self.y0), Index2D(x=0, y=0)), # Lower-left corner, in the x and y buffer 

168 ( 

169 Point2I(x=self.x0 + self.bw - 1, y=self.y0), 

170 Index2D(x=self.nx - 1, y=0), 

171 ), # Lower-right corner, in the x buffer 

172 ( 

173 Point2I(x=self.x0, y=self.y0 + self.bh - 1), 

174 Index2D(x=0, y=self.ny - 1), 

175 ), # Upper-left corner, in the y buffer 

176 ( 

177 Point2I(x=self.x0 + self.bw - 1, y=self.y0 + self.bh - 1), 

178 Index2D(x=self.nx - 1, y=self.ny - 1), 

179 ), # Upper-right corner, in the x and y buffer 

180 # Check for one cell from the origin in either directions 

181 (Point2I(x=self.x0, y=self.y0 + self.ch), Index2D(x=0, y=1)), # Left edge, in the x buffer 

182 (Point2I(x=self.x0 + self.cw, y=self.y0), Index2D(x=1, y=0)), 

183 # Check the four corners of the lower left buffer region 

184 (Point2I(x=self.x0 - padding, y=self.y0), Index2D(0, 0)), 

185 (Point2I(x=self.x0, y=self.y0 - padding), Index2D(0, 0)), 

186 (Point2I(x=self.x0 - padding, y=self.y0), Index2D(0, 0)), 

187 (Point2I(x=self.x0 - padding, y=self.y0 - padding), Index2D(0, 0)), 

188 # Check the four corners of the lower right buffer region 

189 (Point2I(x=self.x0 + self.bw, y=self.y0), Index2D(self.nx - 1, 0)), 

190 (Point2I(x=self.x0 + self.bw + padding - 1, y=self.y0), Index2D(self.nx - 1, 0)), 

191 (Point2I(x=self.x0 + self.bw + padding - 1, y=self.y0 - padding), Index2D(self.nx - 1, 0)), 

192 (Point2I(x=self.x0 + self.bw, y=self.y0 - padding), Index2D(self.nx - 1, 0)), 

193 # Check the four corners of the upper left buffer region 

194 (Point2I(x=self.x0, y=self.y0 + self.bh), Index2D(0, self.ny - 1)), 

195 (Point2I(x=self.x0, y=self.y0 + self.bh + padding - 1), Index2D(0, self.ny - 1)), 

196 (Point2I(x=self.x0 - padding, y=self.y0 + self.bh + padding - 1), Index2D(0, self.ny - 1)), 

197 (Point2I(x=self.x0 - padding, y=self.y0 + self.bh), Index2D(0, self.ny - 1)), 

198 # Check the four corners of the upper right buffer region 

199 (Point2I(x=self.x0 + self.bw, y=self.y0 + self.bh), Index2D(self.nx - 1, self.ny - 1)), 

200 ( 

201 Point2I(x=self.x0 + self.bw, y=self.y0 + self.bh + padding - 1), 

202 Index2D(self.nx - 1, self.ny - 1), 

203 ), 

204 ( 

205 Point2I(x=self.x0 + self.bw + padding - 1, y=self.y0 + self.bh), 

206 Index2D(self.nx - 1, self.ny - 1), 

207 ), 

208 ( 

209 Point2I(x=self.x0 + self.bw + padding - 1, y=self.y0 + self.bh + padding - 1), 

210 Index2D(self.nx - 1, self.ny - 1), 

211 ), 

212 ) 

213 

214 for position, index in positions_index: 

215 self.assertEqual(grid.index(position), index, msg=f"{position} does not map to {index}") 

216 

217 # Check that positions outside the grid raise an exception 

218 self.assertRaises(ValueError, grid.index, Point2I(x=self.x0 - padding - 1, y=self.y0)) 

219 self.assertRaises(ValueError, grid.index, Point2I(x=self.x0 + self.bw + padding, y=self.y0)) 

220 self.assertRaises(ValueError, grid.index, Point2I(x=self.x0, y=self.y0 - padding - 1)) 

221 self.assertRaises(ValueError, grid.index, Point2I(x=self.x0, y=self.y0 + self.bh + padding)) 

222 

223 

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

225 unittest.main()