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

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

# 

# 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 

 

Parameters 

---------- 

src : `lsst.afw.table.SourceRecord` 

Input record 

 

Returns 

------- 

out : `lsst.afw.table.SourceRecord` 

Copy of input record. 

""" 

 

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 

 

Parameters 

---------- 

src : `lsst.afw.table.SourceRecord` 

A Source object 

frac : `float` 

How much to change X by 

 

Returns 

------- 

out : `lsst.afw.table.SourceRecord` 

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 

 

Parameters 

---------- 

src : `lsst.afw.table.SourceRecord` 

A Source object 

frac : `float` 

How much to change X by 

 

Returns 

------- 

out : `lsst.afw.table.SourceRecord` 

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

""" 

 

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 

 

Parameters 

---------- 

src : `lsst.afw.table.SourceRecord` 

A Source object 

frac : `float` 

How much to change X by 

 

Returns 

------- 

out : `lsst.afw.table.SourceRecord` 

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

""" 

 

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): 

"""Distort image by multiple powers of x, 'x**3 - 2*x**2 + 4*x - 9'. 

 

Parameters 

---------- 

src : `lsst.afw.table.SourceRecord` 

A Source object 

frac : `float` 

How much to change X by 

 

Returns 

------- 

out : `lsst.afw.table.SourceRecord` 

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

""" 

 

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 

 

Parameters 

---------- 

src : `lsst.afw.table.SourceRecord` 

A Source object 

frac : `float` 

How much to change Y by 

 

Returns 

------- 

out : `lsst.afw.table.SourceRecord` 

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 

 

Parameters 

---------- 

src : `lsst.afw.table.SourceRecord` 

A Source object 

frac : `float` 

How much to change Y by 

 

Returns 

------- 

out : `lsst.afw.table.SourceRecord` 

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

""" 

 

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 

 

Parameters 

---------- 

src : `lsst.afw.table.SourceRecord` 

A Source object 

frac : `float` 

How much to change Y by 

 

Returns 

------- 

out : `lsst.afw.table.SourceRecord` 

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

""" 

 

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): 

"""Distort image by multiple terms of Y, 'y**3 - 2*y**2 + 4*y - 9'. 

 

Parameters 

---------- 

src : `lsst.afw.table.SourceRecord` 

A Source object 

frac : `float` 

How much to change Y by 

 

Returns 

------- 

out : `lsst.afw.table.SourceRecord` 

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

""" 

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): 

"""Distort image Y by X, leaving X unchanged, 'x**3 - 2*x**2'. 

 

Parameters 

---------- 

src : `lsst.afw.table.SourceRecord` 

A Source object 

frac : `float` 

How much to change Y by 

 

Returns 

------- 

out : `lsst.afw.table.SourceRecord` 

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

""" 

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): 

"""Distort image X by Y, leaving Y unchanged, 'y**3 - 2*y**2 + 4*y - 9'. 

 

Parameters 

---------- 

src : `lsst.afw.table.SourceRecord` 

A Source object 

frac : `float` 

How much to change X by 

 

Returns 

------- 

out : `lsst.afw.table.SourceRecord` 

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

""" 

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): 

"""Distort image X and Y , 'dx=x**3 - 2*x**2 + 4*x - 9', 

'dy=y**3 - 2*y**2 + 4*y - 9'. 

 

Parameters 

---------- 

src : `lsst.afw.table.SourceRecord` 

A Source object 

frac : `float` 

How much to change X and Y by 

 

Returns 

------- 

out : `lsst.afw.table.SourceRecord` 

A deep copy of src, with the value of X and Y changed 

""" 

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 

 

Parameters 

---------- 

src : `lsst.afw.table.SourceRecord` 

A Source object 

frac : `float` 

How much to change X 

 

Returns 

------- 

out : `lsst.afw.table.SourceRecord` 

A deep copy of src, with the value of X 

""" 

 

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 

 

Parameters 

---------- 

src : `lsst.afw.table.SourceRecord` 

A Source object 

frac : `float` 

How much to change X 

 

Returns 

------- 

out : `lsst.afw.table.SourceRecord` 

A deep copy of src, with the value of X 

""" 

 

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. 

 

Parameters 

---------- 

srcList : `list` of `lsst.afw.table.SourceRecord` 

Input list of source to distort. 

function : `callable` 

A function that does a deep copy of a single Source 

 

Returns 

------- 

out : `lsst.afw.table.SourceCatalog` 

Output catalog with distorted positions. 

""" 

 

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