lsst.afw  13.0-101-g894215678
transform.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2017 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 """Python helpers for pybind11 wrapping of Transform classes and subclasses
23 
24 .. _pybind11_transform_classes:
25 
26 Transform Classes and Subclasses
27 --------------------------------
28 
29 Transforms are instances of
30 lsst::afw::geom::Transform<FromEndpoint, ToEndpoint>
31 and subclasses, such as lsst::afw::geom::SkyWcs.
32 
33 In Python the templated Transform classes have names such as
34 `lsst.afw.geom.TransformIcrsCoordToPoint2` for
35 `lsst::afw::geom::Transform<IcrsCoordEndpoint, Point2Endpoint>`
36 """
37 
38 from __future__ import absolute_import, division, print_function
39 
40 __all__ = ["addTransformMethods", "transformRegistry"]
41 
42 import lsst.pex.exceptions
43 
44 # registry of transform classes; a dict of class name: transform class
45 transformRegistry = {}
46 
47 
48 def getJacobian(self, x):
49  # Force 2D matrix over numpy's protests
50  matrix = self._getJacobian(x)
51  matrix.shape = (self.toEndpoint.nAxes,
52  self.fromEndpoint.nAxes)
53  return matrix
54 
55 
56 def then(self, next):
57  """Concatenate two transforms
58 
59  The result of A.then(B) is is C(x) = B(A(x))
60  """
61  if self.toEndpoint == next.fromEndpoint:
62  return self._then(next)
63  else:
64  raise lsst.pex.exceptions.InvalidParameterError(
65  "Cannot concatenate %r and %r: endpoints do not match."
66  % (self, next))
67 
68 
70  """Add pure python methods to the specified Transform class, and register
71  the class in `transformRegistry`
72 
73  All :ref:`_pybind11_transform_classes` must call this function.
74 
75  Parameters
76  ----------
77  cls : :ref:`_pybind11_transform_classes`
78  A Transform class or subclass, e.g.
79  `lsst.afw.geom.TransformPoint2ToIcrsCoord`
80  """
81  global transformRegistry
82  className = cls.__name__
83  if className in transformRegistry:
84  raise RuntimeError("Class %r=%s already registered; cannot register class %s" %
85  (className, transformRegistry[className], cls))
86  transformRegistry[cls.__name__] = cls
87  cls.getJacobian = getJacobian
88  cls.then = then