lsst.meas.astrom  16.0-16-gd8e3590+8
genDistortedImage.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 
23 __all__ = ["noDistort", "linearXDistort", "quadraticDistortX",
24  "cubicDistortX", "manyTermX", "crossTerms1",
25  "crossTerms2", "crossTerms3", "quadraticDistort",
26  "T2DistortX", "T2DistortX"]
27 
28 
29 import math
30 
31 import lsst.afw.table as afwTable
32 
33 
34 def noDistort(src):
35  """Do no distortion. Used for sanity checking"""
36 
37  out = src.table.copyRecord(src)
38  return out
39 
40 
41 def linearXDistort(src, frac=.001):
42  """Increase the x value in a Source object by frac. E.g
43  src.x = 1000 --> 1001 if frac=.001
44 
45  Input:
46  src A Source object
47  frac How much to change X by
48 
49  Output:
50  A deep copy of src, with the value of x changed
51  """
52 
53  out = src.table.copyRecord(src)
54  out.set(out.table.getCentroidKey().getX(), out.getX()*(1+frac))
55  return out
56 
57 
58 def quadraticDistortX(src, frac=1e-6):
59  """Distort image by terms with power <=2
60  i.e y, y^2, x, xy, x^2
61  """
62 
63  out = src.table.copyRecord(src)
64  x = out.getX()
65  y = out.getY()
66  val = x**2
67 
68  out.set(out.table.getCentroidKey().getX(), x + val*frac)
69  out.set(out.table.getCentroidKey().getY(), y)
70  return out
71 
72 
73 def cubicDistortX(src, frac=1e-9):
74  """Distort image by terms with power <=2
75  i.e y, y^2, x, xy, x^2
76  """
77 
78  out = src.table.copyRecord(src)
79  x = out.getX()
80  y = out.getY()
81  val = x**3
82 
83  out.set(out.table.getCentroidKey().getX(), x + val*frac)
84  out.set(out.table.getCentroidKey().getY(), y)
85  return out
86 
87 
88 def manyTermX(src, frac=1e-9):
89  out = src.table.copyRecord(src)
90  x = out.getX()
91  y = out.getY()
92  val = x**3 - 2*x**2 + 4*x - 9
93 
94  out.set(out.table.getCentroidKey().getX(), x + val*frac)
95  out.set(out.table.getCentroidKey().getY(), y)
96  return out
97 
98 
99 def linearYDistort(src, frac=.001):
100  """Increase the y value in a Source object by frac. E.g
101  src.x = 1000 --> 1001 if frac=.001
102 
103  Input:
104  src A Source object
105  frac How much to change Y by
106 
107  Output:
108  A deep copy of src, with the value of y changed
109  """
110 
111  out = src.table.copyRecord(src)
112  out.set(out.table.getCentroidKey().getY(), out.getY()*(1+frac))
113  return out
114 
115 
116 def quadraticDistortY(src, frac=1e-6):
117  """Distort image by terms with power <=2
118  i.e y, y^2, x, xy, x^2
119  """
120 
121  out = src.table.copyRecord(src)
122  x = out.getX()
123  y = out.getY()
124  val = y**2
125 
126  out.set(out.table.getCentroidKey().getX(), x)
127  out.set(out.table.getCentroidKey().getY(), y + val*frac)
128  return out
129 
130 
131 def cubicDistortY(src, frac=1e-9):
132  """Distort image by terms with power <=2
133  i.e y, y^2, x, xy, x^2
134  """
135 
136  out = src.table.copyRecord(src)
137  x = out.getX()
138  y = out.getY()
139  val = x**3
140 
141  out.set(out.table.getCentroidKey().getX(), x)
142  out.set(out.table.getCentroidKey().getY(), y + val*frac)
143  return out
144 
145 
146 def manyTermY(src, frac=1e-9):
147  out = src.table.copyRecord(src)
148  x = out.getX()
149  y = out.getY()
150  val = y**3 - 2*y**2 + 4*y - 9
151 
152  out.set(out.table.getCentroidKey().getX(), x)
153  out.set(out.table.getCentroidKey().getY(), y + val*frac)
154  return out
155 
156 
157 def crossTerms1(src, frac=1e-11):
158  out = src.table.copyRecord(src)
159  x = out.getX()
160  y = out.getY()
161  val = x**3 - 2*x**2 # + 4*x - 9
162 
163  out.set(out.table.getCentroidKey().getX(), x)
164  out.set(out.table.getCentroidKey().getY(), y + val*frac)
165  return out
166 
167 
168 def crossTerms2(src, frac=1e-11):
169  out = src.table.copyRecord(src)
170  x = out.getX()
171  y = out.getY()
172  val = y**3 - 2*y**2 + 4*y - 9
173 
174  out.set(out.table.getCentroidKey().getX(), x + val*frac)
175  out.set(out.table.getCentroidKey().getY(), y)
176  return out
177 
178 
179 def crossTerms3(src, frac=1e-9):
180  out = src.table.copyRecord(src)
181  x = out.getX()
182  y = out.getY()
183  valx = x**3 - 2*x**2 + 4*x - 9
184  valy = y**3 - 2*y**2 + 4*y - 9
185 
186  out.set(out.table.getCentroidKey().getX(), x + valy*frac)
187  out.set(out.table.getCentroidKey().getY(), y + valx*frac)
188  return out
189 
190 
191 def quadraticDistort(src, frac=1e-6):
192  """Distort image by terms with power <=2
193  i.e y, y^2, x, xy, x^2
194  """
195 
196  out = src.table.copyRecord(src)
197  x = out.getX()
198  y = out.getY()
199  val = y + 2*y**2
200  val += 3*x + 4*x*y
201  val += x**2
202 
203  out.set(out.table.getCentroidKey().getX(), x + val*frac)
204  out.set(out.table.getCentroidKey().getY(), y)
205  return out
206 
207 
208 def T2DistortX(src, frac=1e-6):
209  """Distort image by a 2nd order Cheby polynomial"""
210 
211  out = src.table.copyRecord(src)
212  x = src.getX()
213  val = 2*(x**2) - 1
214  out.set(out.table.getCentroidKey().getX(), x + frac*val)
215  return out
216 
217 
218 def distortList(srcList, function):
219  """Create a copy of srcList, and apply function to distort the
220  values of x and y.
221 
222  Input:
223  srcList a SourceSet object
224  function: A function that does a deep copy of a single Source
225  """
226 
227  out = afwTable.SourceCatalog(srcList.table)
228  out.reserve(len(srcList))
229 
230  for src in srcList:
231  out.append(function(src))
232 
233  maxDiff = 0
234  for i in range(len(srcList)):
235  s = srcList[i]
236  o = out[i]
237 
238  x1, y1 = s.getX(), s.getY()
239  x2, y2 = o.getX(), o.getY()
240 
241  diff = math.hypot(x1-x2, y1-y2)
242  maxDiff = max(diff, maxDiff)
243 
244  print("Max deviation is %e pixels" % (maxDiff))
245 
246  return out