lsst.afw  13.0-109-g62eb66483
basicUtils.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2008-2017 LSST/AURA.
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__ = ["makeImageFromArray", "makeMaskFromArray", "makeMaskedImageFromArrays",
25  "wcsAlmostEqualOverBBox", "assertWcsAlmostEqualOverBBox",
26  "wcsNearlyEqualOverBBox", "assertWcsNearlyEqualOverBBox"]
27 
28 import itertools
29 import math
30 import warnings
31 
32 import numpy
33 
34 import lsst.utils.tests
35 import lsst.afw.geom as afwGeom
36 from .image import Image
37 from .mask import Mask
38 from .maskedImage import MaskedImage
39 
40 
41 def makeImageFromArray(array):
42  """Construct an Image from a NumPy array, inferring the Image type from
43  the NumPy type. Return None if input is None.
44  """
45  if array is None:
46  return None
47  return Image(array, dtype=array.dtype.type)
48 
49 
50 def makeMaskFromArray(array):
51  """Construct an Mask from a NumPy array, inferring the Mask type from the
52  NumPy type. Return None if input is None.
53  """
54  if array is None:
55  return None
56  return Mask(array, dtype=array.dtype.type)
57 
58 
59 def makeMaskedImageFromArrays(image, mask=None, variance=None):
60  """Construct a MaskedImage from three NumPy arrays, inferring the
61  MaskedImage types from the NumPy types.
62  """
64  makeImageFromArray(variance), dtype=image.dtype.type)
65 
66 
67 def _compareWcsOverBBox(wcs0, wcs1, bbox, maxDiffSky=0.01*afwGeom.arcseconds,
68  maxDiffPix=0.01, nx=5, ny=5, doShortCircuit=True):
69  """!Compare two WCS over a rectangular grid of pixel positions
70 
71  @param[in] wcs0 WCS 0 (an lsst.afw.image.Wcs)
72  @param[in] wcs1 WCS 1 (an lsst.afw.image.Wcs)
73  @param[in] bbox boundaries of pixel grid over which to compare the WCSs (an lsst.afw.geom.Box2I or Box2D)
74  @param[in] maxDiffSky maximum separation between sky positions computed using Wcs.pixelToSky
75  (an lsst.afw.geom.Angle)
76  @param[in] maxDiffPix maximum separation between pixel positions computed using Wcs.skyToPixel
77  @param[in] nx number of points in x for the grid of pixel positions
78  @param[in] ny number of points in y for the grid of pixel positions
79  @param[in] doShortCircuit if True then stop at the first error, else test all values in the grid
80  and return information about the worst violations found
81 
82  @return return an empty string if the WCS are sufficiently close; else return a string describing
83  the largest error measured in pixel coordinates (if sky to pixel error was excessive) and sky coordinates
84  (if pixel to sky error was excessive). If doShortCircuit is true then the reported error is likely to be
85  much less than the maximum error across the whole pixel grid.
86  """
87  if nx < 1 or ny < 1:
88  raise RuntimeError(
89  "nx = %s and ny = %s must both be positive" % (nx, ny))
90  if maxDiffSky <= 0*afwGeom.arcseconds:
91  raise RuntimeError("maxDiffSky = %s must be positive" % (maxDiffSky,))
92  if maxDiffPix <= 0:
93  raise RuntimeError("maxDiffPix = %s must be positive" % (maxDiffPix,))
94 
95  bboxd = afwGeom.Box2D(bbox)
96  xList = numpy.linspace(bboxd.getMinX(), bboxd.getMaxX(), nx)
97  yList = numpy.linspace(bboxd.getMinY(), bboxd.getMaxY(), ny)
98  # we don't care about measured error unless it is too large, so initialize
99  # to max allowed
100  measDiffSky = (maxDiffSky, "?") # (sky diff, pix pos)
101  measDiffPix = (maxDiffPix, "?") # (pix diff, sky pos)
102  for x, y in itertools.product(xList, yList):
103  fromPixPos = afwGeom.Point2D(x, y)
104  sky0 = wcs0.pixelToSky(fromPixPos)
105  sky1 = wcs1.pixelToSky(fromPixPos)
106  diffSky = sky0.angularSeparation(sky1)
107  if diffSky > measDiffSky[0]:
108  measDiffSky = (diffSky, fromPixPos)
109  if doShortCircuit:
110  break
111 
112  toPixPos0 = wcs0.skyToPixel(sky0)
113  toPixPos1 = wcs1.skyToPixel(sky0)
114  diffPix = math.hypot(*(toPixPos0 - toPixPos1))
115  if diffPix > measDiffPix[0]:
116  measDiffPix = (diffPix, sky0)
117  if doShortCircuit:
118  break
119 
120  msgList = []
121  if measDiffSky[0] > maxDiffSky:
122  msgList.append("%s arcsec max measured sky error > %s arcsec max allowed sky error at pix pos=%s" %
123  (measDiffSky[0].asArcseconds(), maxDiffSky.asArcseconds(), measDiffSky[1]))
124  if measDiffPix[0] > maxDiffPix:
125  msgList.append("%s max measured pix error > %s max allowed pix error at sky pos=%s" %
126  (measDiffPix[0], maxDiffPix, measDiffPix[1]))
127 
128  return "; ".join(msgList)
129 
130 
131 def wcsAlmostEqualOverBBox(wcs0, wcs1, bbox, maxDiffSky=0.01*afwGeom.arcseconds,
132  maxDiffPix=0.01, nx=5, ny=5):
133  """!Return True if two WCS are almost equal over a grid of pixel positions, else False
134 
135  @param[in] wcs0 WCS 0 (an lsst.afw.image.Wcs)
136  @param[in] wcs1 WCS 1 (an lsst.afw.image.Wcs)
137  @param[in] bbox boundaries of pixel grid over which to compare the WCSs (an lsst.afw.geom.Box2I or Box2D)
138  @param[in] maxDiffSky maximum separation between sky positions computed using Wcs.pixelToSky
139  (an lsst.afw.geom.Angle)
140  @param[in] maxDiffPix maximum separation between pixel positions computed using Wcs.skyToPixel
141  @param[in] nx number of points in x for the grid of pixel positions
142  @param[in] ny number of points in y for the grid of pixel positions
143  """
144  return not bool(_compareWcsOverBBox(
145  wcs0 = wcs0,
146  wcs1 = wcs1,
147  bbox = bbox,
148  maxDiffSky = maxDiffSky,
149  maxDiffPix = maxDiffPix,
150  nx = nx,
151  ny = ny,
152  doShortCircuit = True,
153  ))
154 
155 
156 @lsst.utils.tests.inTestCase
157 def assertWcsAlmostEqualOverBBox(testCase, wcs0, wcs1, bbox, maxDiffSky=0.01*afwGeom.arcseconds,
158  maxDiffPix=0.01, nx=5, ny=5, msg="WCSs differ"):
159  """!Assert that two WCS are almost equal over a grid of pixel positions
160 
161  Compare pixelToSky and skyToPixel for two WCS over a rectangular grid of pixel positions.
162  If the WCS are too divergent at any point, call testCase.fail; the message describes
163  the largest error measured in pixel coordinates (if sky to pixel error was excessive)
164  and sky coordinates (if pixel to sky error was excessive) across the entire pixel grid.
165 
166  @param[in] testCase unittest.TestCase instance the test is part of;
167  an object supporting one method: fail(self, msgStr)
168  @param[in] wcs0 WCS 0 (an lsst.afw.image.Wcs)
169  @param[in] wcs1 WCS 1 (an lsst.afw.image.Wcs)
170  @param[in] bbox boundaries of pixel grid over which to compare the WCSs (an lsst.afw.geom.Box2I or Box2D)
171  @param[in] maxDiffSky maximum separation between sky positions computed using Wcs.pixelToSky
172  (an lsst.afw.geom.Angle)
173  @param[in] maxDiffPix maximum separation between pixel positions computed using Wcs.skyToPixel
174  @param[in] nx number of points in x for the grid of pixel positions
175  @param[in] ny number of points in y for the grid of pixel positions
176  @param[in] msg exception message prefix; details of the error are appended after ": "
177  """
178  errMsg = _compareWcsOverBBox(
179  wcs0 = wcs0,
180  wcs1 = wcs1,
181  bbox = bbox,
182  maxDiffSky = maxDiffSky,
183  maxDiffPix = maxDiffPix,
184  nx = nx,
185  ny = ny,
186  doShortCircuit = False,
187  )
188  if errMsg:
189  testCase.fail("%s: %s" % (msg, errMsg))
190 
191 
192 def wcsNearlyEqualOverBBox(*args, **kwargs):
193  warnings.warn("Deprecated. Use wcsAlmostEqualOverBBox", DeprecationWarning, 2)
194  return wcsAlmostEqualOverBBox(*args, **kwargs)
195 
196 
197 @lsst.utils.tests.inTestCase
198 def assertWcsNearlyEqualOverBBox(*args, **kwargs):
199  warnings.warn("Deprecated. Use assertWcsAlmostEqualOverBBox",
200  DeprecationWarning, 2)
201  assertWcsAlmostEqualOverBBox(*args, **kwargs)
def wcsNearlyEqualOverBBox(args, kwargs)
Definition: basicUtils.py:192
def wcsAlmostEqualOverBBox(wcs0, wcs1, bbox, maxDiffSky=0.01 *afwGeom.arcseconds, maxDiffPix=0.01, nx=5, ny=5)
Return True if two WCS are almost equal over a grid of pixel positions, else False.
Definition: basicUtils.py:132
def assertWcsAlmostEqualOverBBox(testCase, wcs0, wcs1, bbox, maxDiffSky=0.01 *afwGeom.arcseconds, maxDiffPix=0.01, nx=5, ny=5, msg="WCSs differ")
Assert that two WCS are almost equal over a grid of pixel positions.
Definition: basicUtils.py:158
def makeMaskedImageFromArrays(image, mask=None, variance=None)
Definition: basicUtils.py:59
A floating-point coordinate rectangle geometry.
Definition: Box.h:266
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:68
def assertWcsNearlyEqualOverBBox(args, kwargs)
Definition: basicUtils.py:198