LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
LSSTDataManagementBasePackage
genDistortedImage.py
Go to the documentation of this file.
1 from __future__ import print_function
2 from builtins import range
3 #
4 # LSST Data Management System
5 # Copyright 2008, 2009, 2010 LSST Corporation.
6 #
7 # This product includes software developed by the
8 # LSST Project (http://www.lsst.org/).
9 #
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the LSST License Statement and
21 # the GNU General Public License along with this program. If not,
22 # see <http://www.lsstcorp.org/LegalNotices/>.
23 #
24 
25 import math
26 
27 import lsst.afw.image as afwImg
28 import lsst.afw.table as afwTable
29 
30 
31 def noDistort(src):
32  """Do no distortion. Used for sanity checking"""
33 
34  out = src.table.copyRecord(src)
35  return out
36 
37 
38 def linearXDistort(src, frac=.001):
39  """Increase the x value in a Source object by frac. E.g
40  src.x = 1000 --> 1001 if frac=.001
41 
42  Input:
43  src A Source object
44  frac How much to change X by
45 
46  Output:
47  A deep copy of src, with the value of x changed
48  """
49 
50  out = src.table.copyRecord(src)
51  out.set(out.table.getCentroidKey().getX(), out.getX()*(1+frac))
52  return out
53 
54 
55 def quadraticDistortX(src, frac=1e-6):
56  """Distort image by terms with power <=2
57  i.e y, y^2, x, xy, x^2
58  """
59 
60  out = src.table.copyRecord(src)
61  x = out.getX()
62  y = out.getY()
63  val = x**2
64 
65  out.set(out.table.getCentroidKey().getX(), x + val*frac)
66  out.set(out.table.getCentroidKey().getY(), y)
67  return out
68 
69 
70 def cubicDistortX(src, frac=1e-9):
71  """Distort image by terms with power <=2
72  i.e y, y^2, x, xy, x^2
73  """
74 
75  out = src.table.copyRecord(src)
76  x = out.getX()
77  y = out.getY()
78  val = x**3
79 
80  out.set(out.table.getCentroidKey().getX(), x + val*frac)
81  out.set(out.table.getCentroidKey().getY(), y)
82  return out
83 
84 
85 def manyTermX(src, frac=1e-9):
86  out = src.table.copyRecord(src)
87  x = out.getX()
88  y = out.getY()
89  val = x**3 - 2*x**2 + 4*x - 9
90 
91  out.set(out.table.getCentroidKey().getX(), x + val*frac)
92  out.set(out.table.getCentroidKey().getY(), y)
93  return out
94 
95 
96 def linearYDistort(src, frac=.001):
97  """Increase the y value in a Source object by frac. E.g
98  src.x = 1000 --> 1001 if frac=.001
99 
100  Input:
101  src A Source object
102  frac How much to change Y by
103 
104  Output:
105  A deep copy of src, with the value of y changed
106  """
107 
108  out = src.table.copyRecord(src)
109  out.set(out.table.getCentroidKey().getY(), out.getY()*(1+frac))
110  return out
111 
112 
113 def quadraticDistortY(src, frac=1e-6):
114  """Distort image by terms with power <=2
115  i.e y, y^2, x, xy, x^2
116  """
117 
118  out = src.table.copyRecord(src)
119  x = out.getX()
120  y = out.getY()
121  val = y**2
122 
123  out.set(out.table.getCentroidKey().getX(), x)
124  out.set(out.table.getCentroidKey().getY(), y + val*frac)
125  return out
126 
127 
128 def cubicDistortY(src, frac=1e-9):
129  """Distort image by terms with power <=2
130  i.e y, y^2, x, xy, x^2
131  """
132 
133  out = src.table.copyRecord(src)
134  x = out.getX()
135  y = out.getY()
136  val = x**3
137 
138  out.set(out.table.getCentroidKey().getX(), x)
139  out.set(out.table.getCentroidKey().getY(), y + val*frac)
140  return out
141 
142 
143 def manyTermY(src, frac=1e-9):
144  out = src.table.copyRecord(src)
145  x = out.getX()
146  y = out.getY()
147  val = y**3 - 2*y**2 + 4*y - 9
148 
149  out.set(out.table.getCentroidKey().getX(), x)
150  out.set(out.table.getCentroidKey().getY(), y + val*frac)
151  return out
152 
153 
154 def crossTerms1(src, frac=1e-11):
155  out = src.table.copyRecord(src)
156  x = out.getX()
157  y = out.getY()
158  val = x**3 - 2*x**2 # + 4*x - 9
159 
160  out.set(out.table.getCentroidKey().getX(), x)
161  out.set(out.table.getCentroidKey().getY(), y + val*frac)
162  return out
163 
164 
165 def crossTerms2(src, frac=1e-11):
166  out = src.table.copyRecord(src)
167  x = out.getX()
168  y = out.getY()
169  val = y**3 - 2*y**2 + 4*y - 9
170 
171  out.set(out.table.getCentroidKey().getX(), x + val*frac)
172  out.set(out.table.getCentroidKey().getY(), y)
173  return out
174 
175 
176 def crossTerms3(src, frac=1e-9):
177  out = src.table.copyRecord(src)
178  x = out.getX()
179  y = out.getY()
180  valx = x**3 - 2*x**2 + 4*x - 9
181  valy = y**3 - 2*y**2 + 4*y - 9
182 
183  out.set(out.table.getCentroidKey().getX(), x + valy*frac)
184  out.set(out.table.getCentroidKey().getY(), y + valx*frac)
185  return out
186 
187 
188 def quadraticDistort(src, frac=1e-6):
189  """Distort image by terms with power <=2
190  i.e y, y^2, x, xy, x^2
191  """
192 
193  out = src.table.copyRecord(src)
194  x = out.getX()
195  y = out.getY()
196  val = y + 2*y**2
197  val += 3*x + 4*x*y
198  val += x**2
199 
200  out.set(out.table.getCentroidKey().getX(), x + val*frac)
201  out.set(out.table.getCentroidKey().getY(), y)
202  return out
203 
204 
205 def T2DistortX(src, frac=1e-6):
206  """Distort image by a 2nd order Cheby polynomial"""
207 
208  out = src.table.copyRecord(src)
209  x = src.getX()
210  val = 2*(x**2) - 1
211  out.set(out.table.getCentroidKey().getX(), x + frac*val)
212  return out
213 
214 
215 def distortList(srcList, function):
216  """Create a copy of srcList, and apply function to distort the
217  values of x and y.
218 
219  Input:
220  srcList a SourceSet object
221  function: A function that does a deep copy of a single Source
222  """
223 
224  out = afwTable.SourceCatalog(srcList.table)
225 
226  for src in srcList:
227  out.append(function(src))
228 
229  maxDiff = 0
230  for i in range(len(srcList)):
231  s = srcList[i]
232  o = out[i]
233 
234  x1, y1 = s.getX(), s.getY()
235  x2, y2 = o.getX(), o.getY()
236 
237  diff = math.hypot(x1-x2, y1-y2)
238  maxDiff = max(diff, maxDiff)
239 
240  print("Max deviation is %e pixels" % (maxDiff))
241 
242  return out
Custom catalog class for record/table subclasses that are guaranteed to have an ID, and should generally be sorted by that ID.
Definition: fwd.h:55