LSSTApplications  12.1-5-gbdcc3ab+2,15.0+14,15.0+30,15.0-1-g19261fa+21,15.0-1-g417ea41,15.0-1-g60afb23+30,15.0-1-g615e0bb+22,15.0-1-g788a293+30,15.0-1-ga91101e+30,15.0-1-gae1598d+13,15.0-1-gd076f1f+28,15.0-1-gdf18595+5,15.0-12-g3681e7a+8,15.0-12-g7952b551+2,15.0-16-g6f0eb036+3,15.0-17-g076ea75+3,15.0-2-g100d730+23,15.0-2-g8aea5f4+1,15.0-2-gf38729e+25,15.0-2-gf60f3cf,15.0-3-g707930d+3,15.0-3-g9103c06+12,15.0-30-g9378914ca+1,15.0-4-g9ee0f43+3,15.0-4-gf6f1c6c+3,15.0-4-gf906033+2,15.0-5-g23e394c+18,15.0-5-g4be42a9+4,15.0-5-gae1eaf0+3,15.0-6-g09241ba+6,15.0-6-g69628aa+4,15.0-6-g81517ef+3,15.0-6-gc1213af+3,15.0-6-gfa9b38f+8,15.0-7-ged79c87+3,15.0-8-g13fca11+3,15.0-8-g67a62d3+5,15.0-8-gcf05001+5,15.0-9-g1e7c341+2,w.2018.25
LSSTDataManagementBasePackage
testUtils.py
Go to the documentation of this file.
1 #
2 # Developed for the LSST Data Management System.
3 # This product includes software developed by the LSST Project
4 # (https://www.lsst.org).
5 # See the COPYRIGHT file at the top-level directory of this distribution
6 # for details of code ownership.
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 GNU General Public License
19 # along with this program. If not, see <https://www.gnu.org/licenses/>.
20 #
21 
22 """Utilities that should be imported into the lsst.geom namespace when lsst.geom is used
23 
24 In the case of the assert functions, importing them makes them available in lsst.utils.tests.TestCase
25 """
26 __all__ = []
27 
28 import math
29 
30 import numpy as np
31 
32 import lsst.utils.tests
33 from .angle import arcseconds
34 
35 
36 def extraMsg(msg):
37  """Format extra error message, if any
38  """
39  if msg:
40  return ": " + msg
41  return ""
42 
43 
44 @lsst.utils.tests.inTestCase
45 def assertAnglesAlmostEqual(testCase, ang0, ang1, maxDiff=0.001*arcseconds,
46  ignoreWrap=True, msg="Angles differ"):
47  r"""Assert that two `~lsst.afw.geom.Angle`\ s are almost equal, ignoring wrap differences by default
48 
49  Parameters
50  ----------
51  testCase : `unittest.TestCase`
52  test case the test is part of; an object supporting one method: fail(self, msgStr)
53  ang0 : `lsst.afw.geom.Angle`
54  angle 0
55  ang1 : `an lsst.afw.geom.Angle`
56  angle 1
57  maxDiff : `an lsst.afw.geom.Angle`
58  maximum difference between the two angles
59  ignoreWrap : `bool`
60  ignore wrap when comparing the angles?
61  - if True then wrap is ignored, e.g. 0 and 360 degrees are considered equal
62  - if False then wrap matters, e.g. 0 and 360 degrees are considered different
63  msg : `str`
64  exception message prefix; details of the error are appended after ": "
65 
66  Raises
67  ------
68  AssertionError
69  Raised if the difference is greater than ``maxDiff``
70  """
71  measDiff = ang1 - ang0
72  if ignoreWrap:
73  measDiff = measDiff.wrapCtr()
74  if abs(measDiff) > maxDiff:
75  testCase.fail("%s: measured difference %s arcsec > max allowed %s arcsec" %
76  (msg, measDiff.asArcseconds(), maxDiff.asArcseconds()))
77 
78 
79 @lsst.utils.tests.inTestCase
80 def assertPairsAlmostEqual(testCase, pair0, pair1, maxDiff=1e-7, msg="Pairs differ"):
81  """Assert that two Cartesian points are almost equal.
82 
83  Each point can be any indexable pair of two floats, including
84  Point2D or Extent2D, a list or a tuple.
85 
86  Parameters
87  ----------
88  testCase : `unittest.TestCase`
89  test case the test is part of; an object supporting one method: fail(self, msgStr)
90  pair0 : pair of `float`
91  pair 0
92  pair1 : pair of `floats`
93  pair 1
94  maxDiff : `float`
95  maximum radial separation between the two points
96  msg : `str`
97  exception message prefix; details of the error are appended after ": "
98 
99  Raises
100  ------
101  AssertionError
102  Raised if the radial difference is greater than ``maxDiff``
103 
104  Notes
105  -----
106  .. warning::
107 
108  Does not compare types, just compares values.
109  """
110  if len(pair0) != 2:
111  raise RuntimeError("len(pair0)=%s != 2" % (len(pair0),))
112  if len(pair1) != 2:
113  raise RuntimeError("len(pair1)=%s != 2" % (len(pair1),))
114 
115  pairDiff = [float(pair1[i] - pair0[i]) for i in range(2)]
116  measDiff = math.hypot(*pairDiff)
117  if measDiff > maxDiff:
118  testCase.fail("%s: measured radial distance = %s > maxDiff = %s, pair0=(%r, %r), pair1=(%r, %r)" %
119  (msg, measDiff, maxDiff, pair0[0], pair0[1], pair1[0], pair1[1]))
120 
121 
122 @lsst.utils.tests.inTestCase
123 def assertPairListsAlmostEqual(testCase, list0, list1, maxDiff=1e-7, msg=None):
124  """Assert that two lists of Cartesian points are almost equal
125 
126  Each point can be any indexable pair of two floats, including
127  Point2D or Extent2D, a list or a tuple.
128 
129  Parameters
130  ----------
131  testCase : `unittest.TestCase`
132  test case the test is part of; an object supporting one method: fail(self, msgStr)
133  list0 : `list` of pairs of `float`
134  list of pairs 0
135  list1 : `list` of pairs of `float`
136  list of pairs 1
137  maxDiff : `float`
138  maximum radial separation between the two points
139  msg : `str`
140  additional information for the error message; appended after ": "
141 
142  Raises
143  ------
144  AssertionError
145  Raised if the radial difference is greater than ``maxDiff``
146 
147  Notes
148  -----
149  .. warning::
150 
151  Does not compare types, just values.
152  """
153  testCase.assertEqual(len(list0), len(list1))
154  lenList1 = np.array([len(val) for val in list0])
155  lenList2 = np.array([len(val) for val in list1])
156  testCase.assertTrue(np.all(lenList1 == 2))
157  testCase.assertTrue(np.all(lenList2 == 2))
158 
159  diffArr = np.array([(val0[0] - val1[0], val0[1] - val1[1])
160  for val0, val1 in zip(list0, list1)], dtype=float)
161  sepArr = np.hypot(diffArr[:, 0], diffArr[:, 1])
162  badArr = sepArr > maxDiff
163  if np.any(badArr):
164  maxInd = np.argmax(sepArr)
165  testCase.fail("PairLists differ in %s places; max separation is at %s: %s > %s%s" %
166  (np.sum(badArr), maxInd, sepArr[maxInd], maxDiff, extraMsg(msg)))
167 
168 
169 @lsst.utils.tests.inTestCase
170 def assertSpherePointsAlmostEqual(testCase, sp0, sp1, maxSep=0.001*arcseconds, msg=""):
171  r"""Assert that two `~lsst.afw.geom.SpherePoint`\ s are almost equal
172 
173  Parameters
174  ----------
175  testCase : `unittest.TestCase`
176  test case the test is part of; an object supporting one method: fail(self, msgStr)
177  sp0 : `lsst.afw.geom.SpherePoint`
178  SpherePoint 0
179  sp1 : `lsst.afw.geom.SpherePoint`
180  SpherePoint 1
181  maxSep : `lsst.afw.geom.Angle`
182  maximum separation
183  msg : `str`
184  extra information to be printed with any error message
185  """
186  if sp0.separation(sp1) > maxSep:
187  testCase.fail("Angular separation between %s and %s = %s\" > maxSep = %s\"%s" %
188  (sp0, sp1, sp0.separation(sp1).asArcseconds(), maxSep.asArcseconds(), extraMsg(msg)))
189 
190 
191 @lsst.utils.tests.inTestCase
192 def assertSpherePointListsAlmostEqual(testCase, splist0, splist1, maxSep=0.001*arcseconds, msg=None):
193  r"""Assert that two lists of `~lsst.afw.geom.SpherePoint`\ s are almost equal
194 
195  Parameters
196  ----------
197  testCase : `unittest.TestCase`
198  test case the test is part of; an object supporting one method: fail(self, msgStr)
199  splist0 : `list` of `lsst.afw.geom.SpherePoint`
200  list of SpherePoints 0
201  splist1 : `list` of `lsst.afw.geom.SpherePoint`
202  list of SpherePoints 1
203  maxSep : `lsst.afw.geom.Angle`
204  maximum separation
205  msg : `str`
206  exception message prefix; details of the error are appended after ": "
207  """
208  testCase.assertEqual(len(splist0), len(splist1), msg=msg)
209  sepArr = np.array([sp0.separation(sp1)
210  for sp0, sp1 in zip(splist0, splist1)])
211  badArr = sepArr > maxSep
212  if np.any(badArr):
213  maxInd = np.argmax(sepArr)
214  testCase.fail("SpherePointLists differ in %s places; max separation is at %s: %s\" > %s\"%s" %
215  (np.sum(badArr), maxInd, sepArr[maxInd].asArcseconds(),
216  maxSep.asArcseconds(), extraMsg(msg)))
217 
218 
219 @lsst.utils.tests.inTestCase
220 def assertBoxesAlmostEqual(testCase, box0, box1, maxDiff=1e-7, msg="Boxes differ"):
221  """Assert that two boxes (`~lsst.afw.geom.Box2D` or `~lsst.afw.geom.Box2I`) are almost equal
222 
223  Parameters
224  ----------
225  testCase : `unittest.TestCase`
226  test case the test is part of; an object supporting one method: fail(self, msgStr)
227  box0 : `lsst.afw.geom.Box2D` or `lsst.afw.geom.Box2I`
228  box 0
229  box1 : `lsst.afw.geom.Box2D` or `lsst.afw.geom.Box2I`
230  box 1
231  maxDiff : `float`
232  maximum radial separation between the min points and max points
233  msg : `str`
234  exception message prefix; details of the error are appended after ": "
235 
236  Raises
237  ------
238  AssertionError
239  Raised if the radial difference of the min points or max points is greater than maxDiff
240 
241  Notes
242  -----
243  .. warning::
244 
245  Does not compare types, just compares values.
246  """
247  assertPairsAlmostEqual(testCase, box0.getMin(),
248  box1.getMin(), maxDiff=maxDiff, msg=msg + ": min")
249  assertPairsAlmostEqual(testCase, box0.getMax(),
250  box1.getMax(), maxDiff=maxDiff, msg=msg + ": max")
def assertPairsAlmostEqual(testCase, pair0, pair1, maxDiff=1e-7, msg="Pairs differ")
Definition: testUtils.py:80
Angle abs(Angle const &a)
Definition: Angle.h:106
def assertSpherePointsAlmostEqual(testCase, sp0, sp1, maxSep=0.001 *arcseconds, msg="")
Definition: testUtils.py:170
def assertBoxesAlmostEqual(testCase, box0, box1, maxDiff=1e-7, msg="Boxes differ")
Definition: testUtils.py:220
def assertSpherePointListsAlmostEqual(testCase, splist0, splist1, maxSep=0.001 *arcseconds, msg=None)
Definition: testUtils.py:192
def assertAnglesAlmostEqual(testCase, ang0, ang1, maxDiff=0.001 *arcseconds, ignoreWrap=True, msg="Angles differ")
Definition: testUtils.py:46
def assertPairListsAlmostEqual(testCase, list0, list1, maxDiff=1e-7, msg=None)
Definition: testUtils.py:123