lsst.afw  13.0-94-ga1c444047
xyTransformFactory.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 2008, 2009, 2010 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 import numpy
24 from lsst.pex.config import Config, ListField, makeRegistry, ConfigDictField, ConfigurableField
25 from .xyTransform import IdentityXYTransform, InvertedXYTransform, RadialXYTransform, \
26  MultiXYTransform, AffineXYTransform
27 from .affineTransform import AffineTransform
28 
29 __all__ = ["xyTransformRegistry", "OneXYTransformConfig"]
30 
31 xyTransformRegistry = makeRegistry(
32  '''A registry of XYTransform factories
33 
34  An XYTransform factory is a function that obeys these rules:
35  - has an attribute ConfigClass
36  - takes one argument, config (an instance of ConfigClass) by name
37  - returns an XYTransform
38  '''
39 )
40 
41 
42 def makeIdentityTransform(config=None):
43  """Make an IdentityXYTransform (which has no config parameters)
44  """
45  return IdentityXYTransform()
46 
47 
48 makeIdentityTransform.ConfigClass = Config
49 xyTransformRegistry.register("identity", makeIdentityTransform)
50 
51 
52 class OneXYTransformConfig(Config):
53  transform = ConfigurableField(
54  doc = "XYTransform factory",
55  target = makeIdentityTransform,
56  )
57 
58 
60  """Make an InvertedXYTransform
61  """
62  return InvertedXYTransform(config.transform.apply())
63 
64 
65 makeInvertedTransform.ConfigClass = OneXYTransformConfig
66 xyTransformRegistry.register("inverted", makeInvertedTransform)
67 
68 
70  linear = ListField(
71  doc = """2x2 linear matrix in the usual numpy order;
72  to rotate a vector by theta use: cos(theta), sin(theta), -sin(theta), cos(theta)""",
73  dtype = float,
74  length = 4,
75  default = (1, 0, 0, 1),
76  )
77  translation = ListField(
78  doc = "x, y translation vector",
79  dtype = float,
80  length = 2,
81  default = (0, 0),
82  )
83 
84 
86  """Make an AffineXYTransform
87  """
88  linear = numpy.array(config.linear)
89  linear.shape = (2, 2)
90  translation = numpy.array(config.translation)
91  return AffineXYTransform(AffineTransform(linear, translation))
92 
93 
94 makeAffineXYTransform.ConfigClass = AffineXYTransformConfig
95 xyTransformRegistry.register("affine", makeAffineXYTransform)
96 
97 
99  coeffs = ListField(
100  doc = "Coefficients for the radial polynomial; coeff[0] must be 0",
101  dtype = float,
102  minLength = 1,
103  optional = False,
104  )
105 
106  def validate(self):
107  if len(self.coeffs) == 0:
108  return
109  if len(self.coeffs) == 1 or self.coeffs[0] != 0 or self.coeffs[1] == 0:
110  raise RuntimeError(
111  "invalid RadialXYTransform coeffs %s: " % (self.coeffs,) +
112  " need len(coeffs)=0 or len(coeffs)>1, coeffs[0]==0, and coeffs[1]!=0")
113 
114 
116  """Make a RadialXYTransform
117  """
118  return RadialXYTransform(config.coeffs._list)
119 
120 
121 makeRadialXYTransform.ConfigClass = RadialXYTransformConfig
122 xyTransformRegistry.register("radial", makeRadialXYTransform)
123 
124 
126  transformDict = ConfigDictField(
127  doc = "Dict of index: OneXYTransformConfig (a transform wrapper); key order is transform order",
128  keytype = int,
129  itemtype = OneXYTransformConfig,
130  )
131 
132 
133 def makeMultiTransform(config):
134  """Make an MultiXYTransform
135  """
136  transformKeys = sorted(config.transformDict.keys())
137  transformList = [config.transformDict[key].transform.apply()
138  for key in transformKeys]
139  return MultiXYTransform(transformList)
140 
141 
142 makeMultiTransform.ConfigClass = MultiXYTransformConfig
143 xyTransformRegistry.register("multi", makeMultiTransform)
Wrap a sequence of multiple XYTransforms.
Definition: XYTransform.h:129
Wrap an AffineTransform.
Definition: XYTransform.h:148