lsst.afw  14.0-38-g278b05818
utils.py
Go to the documentation of this file.
1 from __future__ import absolute_import, division, print_function
2 #
3 # LSST Data Management System
4 # Copyright 2015 LSST Corporation.
5 #
6 # This product includes software developed by the
7 # LSST Project (http://www.lsst.org/).
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 LSST License Statement and
20 # the GNU General Public License along with this program. If not,
21 # see <http://www.lsstcorp.org/LegalNotices/>.
22 #
23 """Utilities that should be imported into the lsst.afw.geom namespace when lsst.afw.geom is used
24 
25 In the case of the assert functions, importing them makes them available in lsst.utils.tests.TestCase
26 """
27 __all__ = ["assertAnglesAlmostEqual", "assertPairsAlmostEqual",
28  "assertPairListsAlmostEqual", "assertSpherePointsAlmostEqual",
29  "assertSpherePointListsAlmostEqual", "assertBoxesAlmostEqual",
30  "makeEndpoints", "assertAnglesNearlyEqual", "assertPairsNearlyEqual",
31  "assertBoxesNearlyEqual"]
32 
33 from builtins import range
34 import warnings
35 import math
36 
37 import numpy as np
38 
39 import lsst.utils.tests
40 from .angle import arcseconds
41 from .endpoint import GenericEndpoint, Point2Endpoint, IcrsCoordEndpoint
42 
43 
44 def extraMsg(msg):
45  """Format extra error message, if any
46  """
47  if msg:
48  return ": " + msg
49  return ""
50 
51 
52 @lsst.utils.tests.inTestCase
53 def assertAnglesAlmostEqual(testCase, ang0, ang1, maxDiff=0.001*arcseconds,
54  ignoreWrap=True, msg="Angles differ"):
55  """!Assert that two angles are almost equal, ignoring wrap differences by default
56 
57  @param[in] testCase unittest.TestCase instance the test is part of;
58  an object supporting one method: fail(self, msgStr)
59  @param[in] ang0 angle 0 (an lsst.afw.geom.Angle)
60  @param[in] ang1 angle 1 (an lsst.afw.geom.Angle)
61  @param[in] maxDiff maximum difference between the two angles (an lsst.afw.geom.Angle)
62  @param[in] ignoreWrap ignore wrap when comparing the angles?
63  - if True then wrap is ignored, e.g. 0 and 360 degrees are considered equal
64  - if False then wrap matters, e.g. 0 and 360 degrees are considered different
65  @param[in] msg exception message prefix; details of the error are appended after ": "
66 
67  @throw AssertionError if the difference is greater than maxDiff
68  """
69  measDiff = ang1 - ang0
70  if ignoreWrap:
71  measDiff = measDiff.wrapCtr()
72  if abs(measDiff) > maxDiff:
73  testCase.fail("%s: measured difference %s arcsec > max allowed %s arcsec" %
74  (msg, measDiff.asArcseconds(), maxDiff.asArcseconds()))
75 
76 
77 @lsst.utils.tests.inTestCase
78 def assertPairsAlmostEqual(testCase, pair0, pair1, maxDiff=1e-7, msg="Pairs differ"):
79  """!Assert that two Cartesian points are almost equal.
80 
81  Each point can be any indexable pair of two floats, including
82  Point2D or Extent2D, a list or a tuple.
83 
84  @warning Does not compare types, just compares values.
85 
86  @param[in] testCase unittest.TestCase instance the test is part of;
87  an object supporting one method: fail(self, msgStr)
88  @param[in] pair0 pair 0 (a pair of floats)
89  @param[in] pair1 pair 1 (a pair of floats)
90  @param[in] maxDiff maximum radial separation between the two points
91  @param[in] msg exception message prefix; details of the error are appended after ": "
92 
93  @throw AssertionError if the radial difference is greater than maxDiff
94  """
95  if len(pair0) != 2:
96  raise RuntimeError("len(pair0)=%s != 2" % (len(pair0),))
97  if len(pair1) != 2:
98  raise RuntimeError("len(pair1)=%s != 2" % (len(pair1),))
99 
100  pairDiff = [float(pair1[i] - pair0[i]) for i in range(2)]
101  measDiff = math.hypot(*pairDiff)
102  if measDiff > maxDiff:
103  testCase.fail("%s: measured radial distance = %s > maxDiff = %s" % (
104  msg, measDiff, maxDiff))
105 
106 
107 @lsst.utils.tests.inTestCase
108 def assertPairListsAlmostEqual(testCase, list0, list1, maxDiff=1e-7, msg=None):
109  """!Assert that two lists of Cartesian points are almost equal
110 
111  Each point can be any indexable pair of two floats, including
112  Point2D or Extent2D, a list or a tuple.
113 
114  @warning Does not compare types, just values.
115 
116  @param[in] testCase unittest.TestCase instance the test is part of;
117  an object supporting one method: fail(self, msgStr)
118  @param[in] list0 list of pairs 0 (each element a pair of floats)
119  @param[in] list1 list of pairs 1
120  @param[in] maxDiff maximum radial separation between the two points
121  @param[in] msg additional information for the error message; appended after ": "
122 
123  @throw AssertionError if the radial difference is greater than maxDiff
124  """
125  testCase.assertEqual(len(list0), len(list1))
126  lenList1 = np.array([len(val) for val in list0])
127  lenList2 = np.array([len(val) for val in list1])
128  testCase.assertTrue(np.all(lenList1 == 2))
129  testCase.assertTrue(np.all(lenList2 == 2))
130 
131  diffArr = np.array([(val0[0] - val1[0], val0[1] - val1[1])
132  for val0, val1 in zip(list0, list1)], dtype=float)
133  sepArr = np.hypot(diffArr[:, 0], diffArr[:, 1])
134  badArr = sepArr > maxDiff
135  if np.any(badArr):
136  maxInd = np.argmax(sepArr)
137  testCase.fail("PairLists differ in %s places; max separation is at %s: %s > %s%s" %
138  (np.sum(badArr), maxInd, sepArr[maxInd], maxDiff, extraMsg(msg)))
139 
140 
141 @lsst.utils.tests.inTestCase
142 def assertSpherePointsAlmostEqual(testCase, sp0, sp1, maxSep=0.001*arcseconds, msg=""):
143  """!Assert that two SpherePoints are almost equal
144 
145  @param[in] testCase unittest.TestCase instance the test is part of;
146  an object supporting one method: fail(self, msgStr)
147  @param[in] sp0 SpherePoint 0
148  @param[in] sp1 SpherePoint 1
149  @param[in] maxSep maximum separation, an lsst.afw.geom.Angle
150  @param[in] msg extra information to be printed with any error message
151  """
152  if sp0.separation(sp1) > maxSep:
153  testCase.fail("Angular separation between %s and %s = %s\" > maxSep = %s\"%s" %
154  (sp0, sp1, sp0.separation(sp1).asArcseconds(), maxSep.asArcseconds(), extraMsg(msg)))
155 
156 
157 @lsst.utils.tests.inTestCase
158 def assertSpherePointListsAlmostEqual(testCase, splist0, splist1, maxSep=0.001*arcseconds, msg=None):
159  """!Assert that two lists of SpherePoints are almost equal
160 
161  @param[in] testCase unittest.TestCase instance the test is part of;
162  an object supporting one method: fail(self, msgStr)
163  @param[in] splist0 list of SpherePoints 0
164  @param[in] splist1 list of SpherePoints 1
165  @param[in] maxSep maximum separation, an lsst.afw.geom.Angle
166  @param[in] msg exception message prefix; details of the error are appended after ": "
167  """
168  testCase.assertEqual(len(splist0), len(splist1), msg=msg)
169  sepArr = np.array([sp0.separation(sp1)
170  for sp0, sp1 in zip(splist0, splist1)])
171  badArr = sepArr > maxSep
172  if np.any(badArr):
173  maxInd = np.argmax(sepArr)
174  testCase.fail("SpherePointLists differ in %s places; max separation is at %s: %s\" > %s\"%s" %
175  (np.sum(badArr), maxInd, sepArr[maxInd].asArcseconds(),
176  maxSep.asArcseconds(), extraMsg(msg)))
177 
178 
179 @lsst.utils.tests.inTestCase
180 def assertBoxesAlmostEqual(testCase, box0, box1, maxDiff=1e-7, msg="Boxes differ"):
181  """!Assert that two boxes (Box2D or Box2I) are almost equal
182 
183  @warning Does not compare types, just compares values.
184 
185  @param[in] testCase unittest.TestCase instance the test is part of;
186  an object supporting one method: fail(self, msgStr)
187  @param[in] box0 box 0
188  @param[in] box1 box 1
189  @param[in] maxDiff maximum radial separation between the min points and max points
190  @param[in] msg exception message prefix; details of the error are appended after ": "
191 
192  @throw AssertionError if the radial difference of the min points or max points is greater than maxDiff
193  """
194  assertPairsAlmostEqual(testCase, box0.getMin(),
195  box1.getMin(), maxDiff=maxDiff, msg=msg + ": min")
196  assertPairsAlmostEqual(testCase, box0.getMax(),
197  box1.getMax(), maxDiff=maxDiff, msg=msg + ": max")
198 
199 
200 @lsst.utils.tests.inTestCase
201 def makeEndpoints(testCase):
202  """Generate a representative sample of Endpoints.
203 
204  Returns
205  -------
206  x : `list`
207  List of endpoints with enough diversity to exercise Endpoint-related
208  code. Each invocation of this method shall return independent objects.
209  """
210  return [GenericEndpoint(n) for n in range(1, 6)] + \
211  [Point2Endpoint(), IcrsCoordEndpoint()]
212 
213 
214 @lsst.utils.tests.inTestCase
215 def assertAnglesNearlyEqual(*args, **kwargs):
216  warnings.warn("Deprecated. Use assertAnglesAlmostEqual",
217  DeprecationWarning, 2)
218  assertAnglesAlmostEqual(*args, **kwargs)
219 
220 
221 @lsst.utils.tests.inTestCase
222 def assertPairsNearlyEqual(*args, **kwargs):
223  warnings.warn("Deprecated. Use assertPairsAlmostEqual", DeprecationWarning, 2)
224  assertPairsAlmostEqual(*args, **kwargs)
225 
226 
227 @lsst.utils.tests.inTestCase
228 def assertBoxesNearlyEqual(*args, **kwargs):
229  warnings.warn("Deprecated. Use assertBoxesAlmostEqual", DeprecationWarning, 2)
230  assertBoxesAlmostEqual(*args, **kwargs)
def assertSpherePointsAlmostEqual(testCase, sp0, sp1, maxSep=0.001 *arcseconds, msg="")
Assert that two SpherePoints are almost equal.
Definition: utils.py:142
def makeEndpoints(testCase)
Definition: utils.py:201
def extraMsg(msg)
Definition: utils.py:44
def assertBoxesNearlyEqual(args, kwargs)
Definition: utils.py:228
def assertAnglesNearlyEqual(args, kwargs)
Definition: utils.py:215
def assertPairsAlmostEqual(testCase, pair0, pair1, maxDiff=1e-7, msg="Pairs differ")
Assert that two Cartesian points are almost equal.
Definition: utils.py:78
def assertPairsNearlyEqual(args, kwargs)
Definition: utils.py:222
def assertPairListsAlmostEqual(testCase, list0, list1, maxDiff=1e-7, msg=None)
Assert that two lists of Cartesian points are almost equal.
Definition: utils.py:108
def assertBoxesAlmostEqual(testCase, box0, box1, maxDiff=1e-7, msg="Boxes differ")
Assert that two boxes (Box2D or Box2I) are almost equal.
Definition: utils.py:180
def assertAnglesAlmostEqual(testCase, ang0, ang1, maxDiff=0.001 *arcseconds, ignoreWrap=True, msg="Angles differ")
Assert that two angles are almost equal, ignoring wrap differences by default.
Definition: utils.py:54
def assertSpherePointListsAlmostEqual(testCase, splist0, splist1, maxSep=0.001 *arcseconds, msg=None)
Assert that two lists of SpherePoints are almost equal.
Definition: utils.py:158