Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

# 

# LSST Data Management System 

# Copyright 2008, 2009, 2010 LSST Corporation. 

# 

# This product includes software developed by the 

# LSST Project (http://www.lsst.org/). 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the LSST License Statement and 

# the GNU General Public License along with this program. If not, 

# see <http://www.lsstcorp.org/LegalNotices/>. 

# 

 

import functools 

import numpy 

 

import lsst.geom 

from lsst.pex.config import Config, ListField, makeRegistry, \ 

ConfigDictField, ConfigurableField 

from .transformFactory import makeTransform, makeIdentityTransform, \ 

makeRadialTransform 

 

__all__ = ["transformRegistry", "OneTransformConfig", "TransformConfig", 

"IdentityTransformConfig", "AffineTransformConfig", "RadialTransformConfig", 

"MultiTransformConfig"] 

 

transformRegistry = makeRegistry( 

""""A registry of ``Transform`` factories 

 

A ``Transform`` factory is a function that obeys these rules: 

- has an attribute ``ConfigClass`` 

- takes one argument, ``config`` (an instance of ``ConfigClass``) by name 

- returns a ``Transform`` 

""" 

) 

 

 

class IdentityTransformConfig(Config): 

"""A Config representing a ``Transform`` that does nothing. 

 

See Also 

-------- 

lsst.afw.geom.makeIdentityTransform 

""" 

pass 

 

 

def identityFactory(config): 

"""Make an identity ``Transform`` 

""" 

return makeIdentityTransform() 

 

 

identityFactory.ConfigClass = IdentityTransformConfig 

transformRegistry.register("identity", identityFactory) 

 

 

class OneTransformConfig(Config): 

"""A Config representing a single ``Transform`` in a compound ``Transform``. 

 

See Also 

-------- 

lsst.afw.geom.MultiTransformConfig 

""" 

transform = ConfigurableField( 

doc="Transform factory", 

target=identityFactory, 

) 

 

 

def invertingFactory(config): 

"""Invert a ``Transform`` specified by config. 

""" 

return config.transform.apply().inverted() 

 

 

invertingFactory.ConfigClass = OneTransformConfig 

transformRegistry.register("inverted", invertingFactory) 

 

 

class AffineTransformConfig(Config): 

"""A Config representing an affine ``Transform``. 

 

See Also 

-------- 

lsst.afw.geom.makeTransform 

""" 

linear = ListField( 

doc="2x2 linear matrix in the usual numpy order; " 

"to rotate a vector by theta use: cos(theta), sin(theta), " 

"-sin(theta), cos(theta)", 

dtype=float, 

length=4, 

default=(1, 0, 0, 1), 

) 

translation = ListField( 

doc="x, y translation vector", 

dtype=float, 

length=2, 

default=(0, 0), 

) 

 

 

def affineFactory(config): 

"""Make an affine ``Transform`` 

""" 

linear = numpy.array(config.linear) 

linear.shape = (2, 2) 

translation = numpy.array(config.translation) 

return makeTransform(lsst.geom.AffineTransform(linear, translation)) 

 

 

affineFactory.ConfigClass = AffineTransformConfig 

transformRegistry.register("affine", affineFactory) 

 

 

class RadialTransformConfig(Config): 

"""A Config representing a radially symmetric ``Transform``. 

 

See Also 

-------- 

lsst.afw.geom.makeRadialTransform 

""" 

coeffs = ListField( 

doc="Coefficients for the radial polynomial; coeff[0] must be 0", 

dtype=float, 

minLength=1, 

optional=False, 

) 

 

def validate(self): 

if len(self.coeffs) == 0: 

return 

if len(self.coeffs) == 1 or self.coeffs[0] != 0 or self.coeffs[1] == 0: 

raise RuntimeError( 

"invalid radial transform coeffs %s: " % (self.coeffs,) + 

"need len(coeffs)=0 or len(coeffs)>1, coeffs[0]==0, " 

"and coeffs[1]!=0") 

 

 

def radialFactory(config): 

"""Make a radial ``Transform`` 

""" 

return makeRadialTransform(config.coeffs._list) 

 

 

radialFactory.ConfigClass = RadialTransformConfig 

transformRegistry.register("radial", radialFactory) 

 

 

class MultiTransformConfig(Config): 

"""A Config representing a chain of consecutive ``Transforms``. 

""" 

transformDict = ConfigDictField( 

doc="Dict of index: OneTransformConfig (a transform wrapper); " 

"key order is transform order", 

keytype=int, 

itemtype=OneTransformConfig, 

) 

 

 

def multiFactory(config): 

"""Concatenate multiple ``Transforms`` 

""" 

transformKeys = sorted(config.transformDict.keys()) 

transformList = [config.transformDict[key].transform.apply() 

for key in transformKeys] 

 

# Can't use then(self, other) directly because no single Transform class 

def concat(transform1, transform2): 

return transform1.then(transform2) 

 

return functools.reduce(concat, transformList) 

 

 

multiFactory.ConfigClass = MultiTransformConfig 

transformRegistry.register("multi", multiFactory) 

 

 

class TransformConfig(Config): 

"""Config that identifies ``Transforms`` by keyword. 

 

Supported configs: 

 

``"identity"`` 

`IdentityTransformConfig` 

``"inverted"`` 

`OneTransformConfig` 

``"affine"`` 

`AffineTransformConfig` 

``"radial"`` 

`RadialTransformConfig` 

``"multi"`` 

`MultiTransformConfig` 

""" 

transform = transformRegistry.makeField( 

doc="a Transform from the registry" 

)