lsst.afw  13.0-110-ge674a0930
transformConfig.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2008, 2009, 2010 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 from __future__ import absolute_import, division, print_function
23 
24 import functools
25 import numpy
26 from lsst.pex.config import Config, Field, ListField, makeRegistry, \
27  ConfigDictField, ConfigurableField
28 from .transformFactory import makeTransform, makeIdentityTransform, \
29  makeRadialTransform
30 from .affineTransform import AffineTransform
31 
32 __all__ = ["transformRegistry", "OneTransformConfig", "TransformConfig"]
33 
34 transformRegistry = makeRegistry(
35  '''A registry of Transform factories
36 
37  A Transform factory is a function that obeys these rules:
38  - has an attribute ConfigClass
39  - takes one argument, config (an instance of ConfigClass) by name
40  - returns a Transform
41  '''
42 )
43 
44 
46  pass
47 
48 
49 def identityFactory(config):
50  """Make an identity Transform
51  """
52  return makeIdentityTransform()
53 
54 
55 identityFactory.ConfigClass = IdentityTransformConfig
56 transformRegistry.register("identity", identityFactory)
57 
58 
59 class OneTransformConfig(Config):
60  transform = ConfigurableField(
61  doc = "Transform factory",
62  target = identityFactory,
63  )
64 
65 
66 def invertingFactory(config):
67  """Invert a Transform specified by config.
68  """
69  return config.transform.apply().getInverse()
70 
71 
72 invertingFactory.ConfigClass = OneTransformConfig
73 transformRegistry.register("inverted", invertingFactory)
74 
75 
76 class AffineTransformConfig(Config):
77  linear = ListField(
78  doc = """2x2 linear matrix in the usual numpy order;
79  to rotate a vector by theta use: cos(theta), sin(theta),
80  -sin(theta), cos(theta)""",
81  dtype = float,
82  length = 4,
83  default = (1, 0, 0, 1),
84  )
85  translation = ListField(
86  doc = "x, y translation vector",
87  dtype = float,
88  length = 2,
89  default = (0, 0),
90  )
91 
92 
93 def affineFactory(config):
94  """Make an affine Transform
95  """
96  linear = numpy.array(config.linear)
97  linear.shape = (2, 2)
98  translation = numpy.array(config.translation)
99  return makeTransform(AffineTransform(linear, translation))
100 
101 
102 affineFactory.ConfigClass = AffineTransformConfig
103 transformRegistry.register("affine", affineFactory)
104 
105 
106 class RadialTransformConfig(Config):
107  coeffs = ListField(
108  doc = "Coefficients for the radial polynomial; coeff[0] must be 0",
109  dtype = float,
110  minLength = 1,
111  optional = False,
112  )
113 
114  def validate(self):
115  if len(self.coeffs) == 0:
116  return
117  if len(self.coeffs) == 1 or self.coeffs[0] != 0 or self.coeffs[1] == 0:
118  raise RuntimeError(
119  "invalid radial transform coeffs %s: " % (self.coeffs,) +
120  " need len(coeffs)=0 or len(coeffs)>1, coeffs[0]==0, " +
121  "and coeffs[1]!=0")
122 
123 
124 def radialFactory(config):
125  """Make a radial Transform
126  """
127  return makeRadialTransform(config.coeffs._list)
128 
129 
130 radialFactory.ConfigClass = RadialTransformConfig
131 transformRegistry.register("radial", radialFactory)
132 
133 
134 class MultiTransformConfig(Config):
135  transformDict = ConfigDictField(
136  doc = "Dict of index: OneTransformConfig (a transform wrapper); " +
137  "key order is transform order",
138  keytype = int,
139  itemtype = OneTransformConfig,
140  )
141 
142 
143 def multiFactory(config):
144  """Concatenate multiple Transforms
145  """
146  transformKeys = sorted(config.transformDict.keys())
147  transformList = [config.transformDict[key].transform.apply()
148  for key in transformKeys]
149 
150  # Can't use then(self, other) directly because no single Transform class
151  def concat(transform1, transform2):
152  return transform1.then(transform2)
153 
154  return functools.reduce(concat, transformList)
155 
156 
157 multiFactory.ConfigClass = MultiTransformConfig
158 transformRegistry.register("multi", multiFactory)
159 
160 
161 class TransformConfig(Config):
162  transform = transformRegistry.makeField(
163  doc = "a Transform from the registry"
164  )
TransformPoint2ToPoint2 makeTransform(AffineTransform const &affine)
Wrap an AffineTransform as a Transform.
TransformPoint2ToPoint2 makeIdentityTransform()
Trivial Transform x → x.
TransformPoint2ToPoint2 makeRadialTransform(std::vector< double > const &coeffs)
A purely radial polynomial distortion.