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

# 

# LSST Data Management System 

# 

# Copyright 2008-2016 AURA/LSST. 

# 

# 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 <https://www.lsstcorp.org/LegalNotices/>. 

# 

import unittest 

import numpy as np 

from functools import reduce 

 

import lsst.utils.tests 

import lsst.afw.detection as afwDet 

import lsst.afw.geom as afwGeom 

import lsst.afw.image as afwImage 

from lsst.log import Log 

from lsst.meas.deblender.baseline import deblend 

import lsst.meas.algorithms as measAlg 

 

doPlot = False 

36 ↛ 37line 36 didn't jump to line 37, because the condition on line 36 was never trueif doPlot: 

import matplotlib 

matplotlib.use('Agg') 

import pylab as plt 

import os.path 

plotpat = os.path.join(os.path.dirname(__file__), 'stray%i.png') 

print('Writing plots to', plotpat) 

else: 

print('"doPlot" not set -- not making plots. To enable plots, edit', __file__) 

 

# Lower the level to Log.DEBUG to see debug messages 

Log.getLogger('meas.deblender.symmetrizeFootprint').setLevel(Log.INFO) 

 

 

def imExt(img): 

bbox = img.getBBox() 

return [bbox.getMinX(), bbox.getMaxX(), 

bbox.getMinY(), bbox.getMaxY()] 

 

 

def doubleGaussianPsf(W, H, fwhm1, fwhm2, a2): 

return measAlg.DoubleGaussianPsf(W, H, fwhm1, fwhm2, a2) 

 

 

def gaussianPsf(W, H, fwhm): 

return measAlg.DoubleGaussianPsf(W, H, fwhm) 

 

 

class StrayFluxTestCase(lsst.utils.tests.TestCase): 

 

def test1(self): 

''' 

A simple example: three overlapping blobs (detected as 1 

footprint with three peaks). We artificially omit one of the 

peaks, meaning that its flux is "stray". Assert that the 

stray flux assigned to the other two peaks accounts for all 

the flux in the parent. 

''' 

H, W = 100, 100 

 

fpbb = afwGeom.Box2I(afwGeom.Point2I(0, 0), 

afwGeom.Point2I(W-1, H-1)) 

 

afwimg = afwImage.MaskedImageF(fpbb) 

imgbb = afwimg.getBBox() 

img = afwimg.getImage().getArray() 

 

var = afwimg.getVariance().getArray() 

var[:, :] = 1. 

 

blob_fwhm = 10. 

blob_psf = doubleGaussianPsf(99, 99, blob_fwhm, 3.*blob_fwhm, 0.03) 

 

fakepsf_fwhm = 3. 

fakepsf = gaussianPsf(11, 11, fakepsf_fwhm) 

 

blobimgs = [] 

x = 75. 

XY = [(x, 35.), (x, 65.), (50., 50.)] 

flux = 1e6 

for x, y in XY: 

bim = blob_psf.computeImage(afwGeom.Point2D(x, y)) 

bbb = bim.getBBox() 

bbb.clip(imgbb) 

 

bim = bim.Factory(bim, bbb) 

bim2 = bim.getArray() 

 

blobimg = np.zeros_like(img) 

blobimg[bbb.getMinY():bbb.getMaxY()+1, 

bbb.getMinX():bbb.getMaxX()+1] += flux * bim2 

blobimgs.append(blobimg) 

 

img[bbb.getMinY():bbb.getMaxY()+1, 

bbb.getMinX():bbb.getMaxX()+1] += flux * bim2 

 

# Run the detection code to get a ~ realistic footprint 

thresh = afwDet.createThreshold(5., 'value', True) 

fpSet = afwDet.FootprintSet(afwimg, thresh, 'DETECTED', 1) 

fps = fpSet.getFootprints() 

print('found', len(fps), 'footprints') 

pks2 = [] 

for fp in fps: 

print('peaks:', len(fp.getPeaks())) 

for pk in fp.getPeaks(): 

print(' ', pk.getIx(), pk.getIy()) 

pks2.append((pk.getIx(), pk.getIy())) 

 

# The first peak in this list is the one we want to omit. 

fp0 = fps[0] 

fakefp = afwDet.Footprint(fp0.getSpans(), fp0.getBBox()) 

for pk in fp0.getPeaks()[1:]: 

fakefp.getPeaks().append(pk) 

 

ima = dict(interpolation='nearest', origin='lower', cmap='gray', 

vmin=0, vmax=1e3) 

 

if doPlot: 

plt.figure(figsize=(12, 6)) 

 

plt.clf() 

plt.suptitle('strayFlux.py: test1 input') 

plt.subplot(2, 2, 1) 

plt.title('Image') 

plt.imshow(img, **ima) 

ax = plt.axis() 

plt.plot([x for x, y in XY], [y for x, y in XY], 'r.') 

plt.axis(ax) 

for i, (b, (x, y)) in enumerate(zip(blobimgs, XY)): 

plt.subplot(2, 2, 2+i) 

plt.title('Blob %i' % i) 

plt.imshow(b, **ima) 

ax = plt.axis() 

plt.plot(x, y, 'r.') 

plt.axis(ax) 

plt.savefig(plotpat % 1) 

 

# Change verbose to False to quiet down the meas_deblender.baseline logger 

deb = deblend(fakefp, afwimg, fakepsf, fakepsf_fwhm, verbose=True) 

parent_img = afwImage.ImageF(fpbb) 

fakefp.spans.copyImage(afwimg.getImage(), parent_img) 

 

if doPlot: 

def myimshow(*args, **kwargs): 

plt.imshow(*args, **kwargs) 

plt.xticks([]) 

plt.yticks([]) 

plt.axis(imExt(afwimg)) 

 

plt.clf() 

plt.suptitle('strayFlux.py: test1 results') 

# R,C = 3,5 

R, C = 3, 4 

plt.subplot(R, C, (2*C) + 1) 

plt.title('Image') 

myimshow(img, **ima) 

ax = plt.axis() 

plt.plot([x for x, y in XY], [y for x, y in XY], 'r.') 

plt.axis(ax) 

 

plt.subplot(R, C, (2*C) + 2) 

plt.title('Parent footprint') 

myimshow(parent_img.getArray(), **ima) 

ax = plt.axis() 

plt.plot([pk.getIx() for pk in fakefp.getPeaks()], 

[pk.getIy() for pk in fakefp.getPeaks()], 'r.') 

plt.axis(ax) 

 

sumimg = None 

for i, dpk in enumerate(deb.peaks): 

plt.subplot(R, C, i*C + 1) 

plt.title('ch%i symm' % i) 

symm = dpk.templateImage 

myimshow(symm.getArray(), extent=imExt(symm), **ima) 

 

plt.subplot(R, C, i*C + 2) 

plt.title('ch%i portion' % i) 

port = dpk.fluxPortion.getImage() 

myimshow(port.getArray(), extent=imExt(port), **ima) 

 

himg = afwImage.ImageF(fpbb) 

heavy = dpk.getFluxPortion(strayFlux=False) 

heavy.insert(himg) 

 

# plt.subplot(R, C, i*C + 3) 

# plt.title('ch%i heavy' % i) 

# myimshow(himg.getArray(), **ima) 

# ax = plt.axis() 

# plt.plot([x for x,y in XY], [y for x,y in XY], 'r.') 

# plt.axis(ax) 

 

simg = afwImage.ImageF(fpbb) 

dpk.strayFlux.insert(simg) 

 

plt.subplot(R, C, i*C + 3) 

plt.title('ch%i stray' % i) 

myimshow(simg.getArray(), **ima) 

ax = plt.axis() 

plt.plot([x for x, y in XY], [y for x, y in XY], 'r.') 

plt.axis(ax) 

 

himg2 = afwImage.ImageF(fpbb) 

heavy = dpk.getFluxPortion(strayFlux=True) 

heavy.insert(himg2) 

 

if sumimg is None: 

sumimg = himg2.getArray().copy() 

else: 

sumimg += himg2.getArray() 

 

plt.subplot(R, C, i*C + 4) 

myimshow(himg2.getArray(), **ima) 

plt.title('ch%i total' % i) 

ax = plt.axis() 

plt.plot([x for x, y in XY], [y for x, y in XY], 'r.') 

plt.axis(ax) 

 

plt.subplot(R, C, (2*C) + C) 

myimshow(sumimg, **ima) 

ax = plt.axis() 

plt.plot([x for x, y in XY], [y for x, y in XY], 'r.') 

plt.axis(ax) 

plt.title('Sum of deblends') 

 

plt.savefig(plotpat % 2) 

 

# Compute the sum-of-children image 

sumimg = None 

for i, dpk in enumerate(deb.deblendedParents[0].peaks): 

himg2 = afwImage.ImageF(fpbb) 

dpk.getFluxPortion().insert(himg2) 

if sumimg is None: 

sumimg = himg2.getArray().copy() 

else: 

sumimg += himg2.getArray() 

 

# Sum of children ~= Original image inside footprint (parent_img) 

 

absdiff = np.max(np.abs(sumimg - parent_img.getArray())) 

print('Max abs diff:', absdiff) 

imgmax = parent_img.getArray().max() 

print('Img max:', imgmax) 

self.assertLess(absdiff, imgmax*1e-6) 

 

def test2(self): 

''' 

A 1-d example, to test the stray-flux assignment. 

''' 

H, W = 1, 100 

 

fpbb = afwGeom.Box2I(afwGeom.Point2I(0, 0), 

afwGeom.Point2I(W-1, H-1)) 

afwimg = afwImage.MaskedImageF(fpbb) 

img = afwimg.getImage().getArray() 

 

var = afwimg.getVariance().getArray() 

var[:, :] = 1. 

 

y = 0 

img[y, 1:-1] = 10. 

 

img[0, 1] = 20. 

img[0, -2] = 20. 

 

fakepsf_fwhm = 1. 

fakepsf = gaussianPsf(1, 1, fakepsf_fwhm) 

 

# Run the detection code to get a ~ realistic footprint 

thresh = afwDet.createThreshold(5., 'value', True) 

fpSet = afwDet.FootprintSet(afwimg, thresh, 'DETECTED', 1) 

fps = fpSet.getFootprints() 

self.assertEqual(len(fps), 1) 

fp = fps[0] 

 

# WORKAROUND: the detection alg produces ONE peak, at (1,0), 

# rather than two. 

self.assertEqual(len(fp.getPeaks()), 1) 

fp.addPeak(W-2, y, float("NaN")) 

# print 'Added peak; peaks:', len(fp.getPeaks()) 

# for pk in fp.getPeaks(): 

# print ' ', pk.getFx(), pk.getFy() 

 

# Change verbose to False to quiet down the meas_deblender.baseline logger 

deb = deblend(fp, afwimg, fakepsf, fakepsf_fwhm, verbose=True, 

fitPsfs=False, ) 

 

if doPlot: 

XX = np.arange(W+1).repeat(2)[1:-1] 

 

plt.clf() 

p1 = plt.plot(XX, img[y, :].repeat(2), 'g-', lw=3, alpha=0.3) 

 

for i, dpk in enumerate(deb.peaks): 

print(dpk) 

port = dpk.fluxPortion.getImage() 

bb = port.getBBox() 

YY = np.zeros(XX.shape) 

YY[bb.getMinX()*2: (bb.getMaxX()+1)*2] = port.getArray()[0, :].repeat(2) 

p2 = plt.plot(XX, YY, 'r-') 

 

simg = afwImage.ImageF(fpbb) 

dpk.strayFlux.insert(simg) 

p3 = plt.plot(XX, simg.getArray()[y, :].repeat(2), 'b-') 

 

plt.legend((p1[0], p2[0], p3[0]), 

('Parent Flux', 'Child portion', 'Child stray flux')) 

plt.ylim(-2, 22) 

plt.savefig(plotpat % 3) 

 

strays = [] 

for i, dpk in enumerate(deb.deblendedParents[0].peaks): 

simg = afwImage.ImageF(fpbb) 

dpk.strayFlux.insert(simg) 

strays.append(simg.getArray()) 

 

ssum = reduce(np.add, strays) 

 

starget = np.zeros(W) 

starget[2:-2] = 10. 

 

self.assertFloatsEqual(ssum, starget) 

 

X = np.arange(W) 

dx1 = X - 1. 

dx2 = X - (W-2) 

f1 = (1. / (1. + dx1**2)) 

f2 = (1. / (1. + dx2**2)) 

strayclip = 0.001 

fsum = f1 + f2 

f1[f1 < strayclip * fsum] = 0. 

f2[f2 < strayclip * fsum] = 0. 

 

s1 = f1 / (f1+f2) * 10. 

s2 = f2 / (f1+f2) * 10. 

 

s1[:2] = 0. 

s2[-2:] = 0. 

 

if doPlot: 

p4 = plt.plot(XX, s1.repeat(2), 'm-') 

plt.plot(XX, s2.repeat(2), 'm-') 

 

plt.legend((p1[0], p2[0], p3[0], p4[0]), 

('Parent Flux', 'Child portion', 'Child stray flux', 

'Expected stray flux')) 

plt.ylim(-2, 22) 

plt.savefig(plotpat % 4) 

 

# test abs diff 

d = np.max(np.abs(s1 - strays[0])) 

self.assertLess(d, 1e-6) 

d = np.max(np.abs(s2 - strays[1])) 

self.assertLess(d, 1e-6) 

 

# test relative diff 

self.assertLess(np.max(np.abs(s1 - strays[0])/np.maximum(1e-3, s1)), 1e-6) 

self.assertLess(np.max(np.abs(s2 - strays[1])/np.maximum(1e-3, s2)), 1e-6) 

 

 

class TestMemory(lsst.utils.tests.MemoryTestCase): 

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

383 ↛ 384line 383 didn't jump to line 384, because the condition on line 383 was never trueif __name__ == "__main__": 

lsst.utils.tests.init() 

unittest.main()