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

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

# 

# 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/>. 

# 

 

__all__ = ["noDistort", "linearXDistort", "quadraticDistortX", 

"cubicDistortX", "manyTermX", "crossTerms1", 

"crossTerms2", "crossTerms3", "quadraticDistort", 

"T2DistortX", "T2DistortX"] 

 

 

import math 

 

import lsst.afw.table as afwTable 

 

 

def noDistort(src): 

"""Do no distortion. Used for sanity checking""" 

 

out = src.table.copyRecord(src) 

return out 

 

 

def linearXDistort(src, frac=.001): 

"""Increase the x value in a Source object by frac. E.g 

src.x = 1000 --> 1001 if frac=.001 

 

Input: 

src A Source object 

frac How much to change X by 

 

Output: 

A deep copy of src, with the value of x changed 

""" 

 

out = src.table.copyRecord(src) 

out.set(out.table.getCentroidKey().getX(), out.getX()*(1+frac)) 

return out 

 

 

def quadraticDistortX(src, frac=1e-6): 

"""Distort image by terms with power <=2 

i.e y, y^2, x, xy, x^2 

""" 

 

out = src.table.copyRecord(src) 

x = out.getX() 

y = out.getY() 

val = x**2 

 

out.set(out.table.getCentroidKey().getX(), x + val*frac) 

out.set(out.table.getCentroidKey().getY(), y) 

return out 

 

 

def cubicDistortX(src, frac=1e-9): 

"""Distort image by terms with power <=2 

i.e y, y^2, x, xy, x^2 

""" 

 

out = src.table.copyRecord(src) 

x = out.getX() 

y = out.getY() 

val = x**3 

 

out.set(out.table.getCentroidKey().getX(), x + val*frac) 

out.set(out.table.getCentroidKey().getY(), y) 

return out 

 

 

def manyTermX(src, frac=1e-9): 

out = src.table.copyRecord(src) 

x = out.getX() 

y = out.getY() 

val = x**3 - 2*x**2 + 4*x - 9 

 

out.set(out.table.getCentroidKey().getX(), x + val*frac) 

out.set(out.table.getCentroidKey().getY(), y) 

return out 

 

 

def linearYDistort(src, frac=.001): 

"""Increase the y value in a Source object by frac. E.g 

src.x = 1000 --> 1001 if frac=.001 

 

Input: 

src A Source object 

frac How much to change Y by 

 

Output: 

A deep copy of src, with the value of y changed 

""" 

 

out = src.table.copyRecord(src) 

out.set(out.table.getCentroidKey().getY(), out.getY()*(1+frac)) 

return out 

 

 

def quadraticDistortY(src, frac=1e-6): 

"""Distort image by terms with power <=2 

i.e y, y^2, x, xy, x^2 

""" 

 

out = src.table.copyRecord(src) 

x = out.getX() 

y = out.getY() 

val = y**2 

 

out.set(out.table.getCentroidKey().getX(), x) 

out.set(out.table.getCentroidKey().getY(), y + val*frac) 

return out 

 

 

def cubicDistortY(src, frac=1e-9): 

"""Distort image by terms with power <=2 

i.e y, y^2, x, xy, x^2 

""" 

 

out = src.table.copyRecord(src) 

x = out.getX() 

y = out.getY() 

val = x**3 

 

out.set(out.table.getCentroidKey().getX(), x) 

out.set(out.table.getCentroidKey().getY(), y + val*frac) 

return out 

 

 

def manyTermY(src, frac=1e-9): 

out = src.table.copyRecord(src) 

x = out.getX() 

y = out.getY() 

val = y**3 - 2*y**2 + 4*y - 9 

 

out.set(out.table.getCentroidKey().getX(), x) 

out.set(out.table.getCentroidKey().getY(), y + val*frac) 

return out 

 

 

def crossTerms1(src, frac=1e-11): 

out = src.table.copyRecord(src) 

x = out.getX() 

y = out.getY() 

val = x**3 - 2*x**2 # + 4*x - 9 

 

out.set(out.table.getCentroidKey().getX(), x) 

out.set(out.table.getCentroidKey().getY(), y + val*frac) 

return out 

 

 

def crossTerms2(src, frac=1e-11): 

out = src.table.copyRecord(src) 

x = out.getX() 

y = out.getY() 

val = y**3 - 2*y**2 + 4*y - 9 

 

out.set(out.table.getCentroidKey().getX(), x + val*frac) 

out.set(out.table.getCentroidKey().getY(), y) 

return out 

 

 

def crossTerms3(src, frac=1e-9): 

out = src.table.copyRecord(src) 

x = out.getX() 

y = out.getY() 

valx = x**3 - 2*x**2 + 4*x - 9 

valy = y**3 - 2*y**2 + 4*y - 9 

 

out.set(out.table.getCentroidKey().getX(), x + valy*frac) 

out.set(out.table.getCentroidKey().getY(), y + valx*frac) 

return out 

 

 

def quadraticDistort(src, frac=1e-6): 

"""Distort image by terms with power <=2 

i.e y, y^2, x, xy, x^2 

""" 

 

out = src.table.copyRecord(src) 

x = out.getX() 

y = out.getY() 

val = y + 2*y**2 

val += 3*x + 4*x*y 

val += x**2 

 

out.set(out.table.getCentroidKey().getX(), x + val*frac) 

out.set(out.table.getCentroidKey().getY(), y) 

return out 

 

 

def T2DistortX(src, frac=1e-6): 

"""Distort image by a 2nd order Cheby polynomial""" 

 

out = src.table.copyRecord(src) 

x = src.getX() 

val = 2*(x**2) - 1 

out.set(out.table.getCentroidKey().getX(), x + frac*val) 

return out 

 

 

def distortList(srcList, function): 

"""Create a copy of srcList, and apply function to distort the 

values of x and y. 

 

Input: 

srcList a SourceSet object 

function: A function that does a deep copy of a single Source 

""" 

 

out = afwTable.SourceCatalog(srcList.table) 

out.reserve(len(srcList)) 

 

for src in srcList: 

out.append(function(src)) 

 

maxDiff = 0 

for i in range(len(srcList)): 

s = srcList[i] 

o = out[i] 

 

x1, y1 = s.getX(), s.getY() 

x2, y2 = o.getX(), o.getY() 

 

diff = math.hypot(x1-x2, y1-y2) 

maxDiff = max(diff, maxDiff) 

 

print("Max deviation is %e pixels" % (maxDiff)) 

 

return out