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