lsst.afw  13.0-39-g3deb52d
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
testUtils.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2016 LSST Corporation.
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 <http://www.lsstcorp.org/LegalNotices/>.
21 #
22 from __future__ import absolute_import, division, print_function
23 
24 __all__ = ["BoxGrid"]
25 
26 from builtins import range
27 from builtins import object
28 
29 import numpy as np
30 
31 from .box import Box2I, Box2D
32 
33 
34 class BoxGrid(object):
35  """!Divide a box into nx by ny sub-boxes that tile the region
36  """
37 
38  def __init__(self, box, numColRow):
39  """!Construct a BoxGrid
40 
41  The sub-boxes will be of the same type as `box` and will exactly tile `box`;
42  they will also all be the same size, to the extent possible (some variation
43  is inevitable for integer boxes that cannot be evenly divided.
44 
45  @param[in] box box (an lsst.afw.geom.Box2I or Box2D);
46  the boxes in the grid will be of the same type
47  @param[in] numColRow number of columns and rows (a pair of ints)
48  """
49  if len(numColRow) != 2:
50  raise RuntimeError(
51  "numColRow=%r; must be a sequence of two integers" % (numColRow,))
52  self._numColRow = tuple(int(val) for val in numColRow)
53 
54  if isinstance(box, Box2I):
55  stopDelta = 1
56  elif isinstance(box, Box2D):
57  stopDelta = 0
58  else:
59  raise RuntimeError("Unknown class %s of box %s" % (type(box), box))
60  self.boxClass = type(box)
61  self.stopDelta = stopDelta
62 
63  minPoint = box.getMin()
64  self.pointClass = type(minPoint)
65  dtype = np.array(minPoint).dtype
66 
67  self._divList = [np.linspace(start=box.getMin()[i],
68  stop=box.getMax()[i] + self.stopDelta,
69  num=self._numColRow[i] + 1,
70  endpoint=True,
71  dtype=dtype) for i in range(2)]
72 
73  @property
74  def numColRow(self):
75  return self._numColRow
76 
77  def __getitem__(self, indXY):
78  """!Return the box at the specified x,y index (a pair of ints)
79  """
80  beg = self.pointClass(*[self._divList[i][indXY[i]] for i in range(2)])
81  end = self.pointClass(
82  *[self._divList[i][indXY[i] + 1] - self.stopDelta for i in range(2)])
83  return self.boxClass(beg, end)
84 
85  def __len__(self):
86  return self.shape[0]*self.shape[1]
87 
88  def __iter__(self):
89  """!Return an iterator over all boxes, where column varies most quickly
90  """
91  for row in range(self.numColRow[1]):
92  for col in range(self.numColRow[0]):
93  yield self[col, row]
def __init__
Construct a BoxGrid.
Definition: testUtils.py:38
def __iter__
Return an iterator over all boxes, where column varies most quickly.
Definition: testUtils.py:88
Divide a box into nx by ny sub-boxes that tile the region.
Definition: testUtils.py:34
def __getitem__
Return the box at the specified x,y index (a pair of ints)
Definition: testUtils.py:77