lsst.afw  15.0-17-g0b88e3da8
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.TransformSpherePointToPoint2` for
35 `lsst::afw::geom::Transform<SpherePointEndpoint, Point2Endpoint>`
36 """
37 
38 from __future__ import absolute_import, division, print_function
39 
40 __all__ = ["addTransformMethods", "reduceTransform", "transformRegistry"]
41 
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, simplify=True):
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, simplify=simplify)
63  else:
65  "Cannot concatenate %r and %r: endpoints do not match."
66  % (self, next))
67 
68 
69 def unpickleTransform(cls, state):
70  """Unpickle a Transform object
71 
72  Parameters
73  ----------
74  cls : `type`
75  A `Transform` class.
76  state : `str`
77  Pickled state.
78 
79  Returns
80  -------
81  transform : `cls`
82  The unpickled Transform.
83  """
84  return cls.readString(state)
85 
86 
87 def reduceTransform(transform):
88  """Pickle a Transform object
89 
90  This provides the `__reduce__` implementation for a Transform.
91  """
92  return unpickleTransform, (type(transform), transform.writeString())
93 
94 
96  """Add pure python methods to the specified Transform class, and register
97  the class in `transformRegistry`
98 
99  All :ref:`_pybind11_transform_classes` must call this function.
100 
101  Parameters
102  ----------
103  cls : :ref:`_pybind11_transform_classes`
104  A Transform class or subclass, e.g.
105  `lsst.afw.geom.TransformPoint2ToSpherePoint`
106  """
107  global transformRegistry
108  className = cls.__name__
109  if className in transformRegistry:
110  raise RuntimeError("Class %r=%s already registered; cannot register class %s" %
111  (className, transformRegistry[className], cls))
112  transformRegistry[cls.__name__] = cls
113  cls.getJacobian = getJacobian
114  cls.then = then
115  cls.__reduce__ = reduceTransform
def reduceTransform(transform)
Definition: transform.py:87
def unpickleTransform(cls, state)
Definition: transform.py:69