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

# 

# 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 

 

import lsst.utils.tests 

import lsst.afw.detection as afwDet 

import lsst.geom as geom 

import lsst.afw.image as afwImage 

import lsst.meas.algorithms as measAlg 

from lsst.log import Log 

from lsst.meas.deblender.baseline import deblend 

 

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__), 'edge%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) 

Log.getLogger('meas.deblender.symmetricFootprint').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 RampEdgeTestCase(lsst.utils.tests.TestCase): 

 

def test1(self): 

''' 

In this test, we create a test image containing two blobs, one 

of which is truncated by the edge of the image. 

 

We run the detection code to get realistic peaks and 

footprints. 

 

We then test out the different edge treatments and assert that 

they do what they claim. We also make plots, tests/edge*.png 

''' 

 

# Create fake image... 

H, W = 100, 100 

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

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

afwimg = afwImage.MaskedImageF(fpbb) 

imgbb = afwimg.getBBox() 

img = afwimg.getImage().getArray() 

 

var = afwimg.getVariance().getArray() 

var[:, :] = 1. 

 

blob_fwhm = 15. 

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

fakepsf_fwhm = 5. 

S = int(np.ceil(fakepsf_fwhm * 2.)) * 2 + 1 

print('S', S) 

fakepsf = gaussianPsf(S, S, fakepsf_fwhm) 

 

# Create and save blob images, and add to image to deblend. 

blobimgs = [] 

XY = [(50., 50.), (90., 50.)] 

flux = 1e6 

for x, y in XY: 

bim = blob_psf.computeImage(geom.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(10., 'value', True) 

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

fps = fpSet.getFootprints() 

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

 

# set EDGE bit on edge pixels. 

margin = 5 

lo = imgbb.getMin() 

lo.shift(geom.Extent2I(margin, margin)) 

hi = imgbb.getMax() 

hi.shift(geom.Extent2I(-margin, -margin)) 

goodbbox = geom.Box2I(lo, hi) 

print('Good bbox for setting EDGE pixels:', goodbbox) 

print('image bbox:', imgbb) 

edgebit = afwimg.getMask().getPlaneBitMask("EDGE") 

print('edgebit:', edgebit) 

measAlg.SourceDetectionTask.setEdgeBits(afwimg, goodbbox, edgebit) 

 

if False: 

plt.clf() 

plt.imshow(afwimg.getMask().getArray(), 

interpolation='nearest', origin='lower') 

plt.colorbar() 

plt.title('Mask') 

plt.savefig('mask.png') 

 

M = afwimg.getMask().getArray() 

for bit in range(32): 

mbit = (1 << bit) 

if not np.any(M & mbit): 

continue 

plt.clf() 

plt.imshow(M & mbit, 

interpolation='nearest', origin='lower') 

plt.colorbar() 

plt.title('Mask bit %i (0x%x)' % (bit, mbit)) 

plt.savefig('mask-%02i.png' % bit) 

 

for fp in fps: 

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

for pk in fp.getPeaks(): 

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

assert(len(fps) == 1) 

fp = fps[0] 

assert(len(fp.getPeaks()) == 2) 

 

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

cmap='jet', 

vmin=0, vmax=400) 

 

for j, (tt, kwa) in enumerate([ 

('No edge treatment', dict()), 

('Ramp by PSF', dict(rampFluxAtEdge=True)), 

('No clip at edge', dict(patchEdges=True)), 

]): 

# print 'Deblending...' 

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

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

**kwa) 

# print 'Result:', deb 

# print len(deb.peaks), 'deblended peaks' 

 

parent_img = afwImage.ImageF(fpbb) 

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

 

X = [x for x, y in XY] 

Y = [y for x, y in XY] 

PX = [pk.getIx() for pk in fp.getPeaks()] 

PY = [pk.getIy() for pk in fp.getPeaks()] 

 

# Grab 1-d slices to make assertion about. 

symms = [] 

monos = [] 

symm1ds = [] 

mono1ds = [] 

yslice = H//2 

parent1d = img[yslice, :] 

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

symm = dpk.origTemplate 

symms.append(symm) 

 

bbox = symm.getBBox() 

x0, y0 = bbox.getMinX(), bbox.getMinY() 

im = symm.getArray() 

h, w = im.shape 

oned = np.zeros(W) 

oned[x0: x0+w] = im[yslice-y0, :] 

symm1ds.append(oned) 

 

mono = afwImage.ImageF(fpbb) 

dpk.templateFootprint.spans.copyImage(dpk.templateImage, mono) 

monos.append(mono) 

 

im = mono.getArray() 

bbox = mono.getBBox() 

x0, y0 = bbox.getMinX(), bbox.getMinY() 

h, w = im.shape 

oned = np.zeros(W) 

oned[x0: x0+w] = im[yslice-y0, :] 

mono1ds.append(oned) 

 

for i, (symm, mono) in enumerate(zip(symm1ds, mono1ds)): 

# for the first two cases, the basic symmetric 

# template for the second source drops to zero at < 

# ~75 where the symmetric part is outside the 

# footprint. 

if i == 1 and j in [0, 1]: 

self.assertFloatsEqual(symm[:74], 0.0) 

if i == 1 and j == 2: 

# For the third case, the 'symm' template gets 

# "patched" with the parent's value 

self.assertFloatsEqual(symm[:74], parent1d[:74]) 

 

if i == 1 and j == 0: 

# No edge handling: mono template == 0 

self.assertFloatsEqual(mono[:74], 0.0) 

if i == 1 and j == 1: 

# ramp by psf: zero up to ~65, ramps up 

self.assertFloatsEqual(mono[:64], 0.0) 

self.assertTrue(np.any(mono[65:74] > 0)) 

self.assertTrue(np.all(np.diff(mono)[60:80] >= 0.)) 

if i == 1 and j == 2: 

# no edge clipping: profile is monotonic and positive. 

self.assertTrue(np.all(np.diff(mono)[:85] >= 0.)) 

self.assertTrue(np.all(mono[:85] > 0.)) 

 

if not doPlot: 

continue 

 

plt.clf() 

p1 = plt.plot(parent1d, 'b-', lw=3, alpha=0.5) 

for i, (symm, mono) in enumerate(zip(symm1ds, mono1ds)): 

p2 = plt.plot(symm, 'r-', lw=2, alpha=0.7) 

p3 = plt.plot(mono, 'g-') 

plt.legend((p1[0], p2[0], p3[0]), ('Parent', 'Symm template', 'Mono template'), 

loc='upper left') 

plt.title('1-d slice: %s' % tt) 

fn = plotpat % (2*j+0) 

plt.savefig(fn) 

print('Wrote', fn) 

 

def myimshow(*args, **kwargs): 

x0, x1, y0, y1 = imExt(afwimg) 

plt.fill([x0, x0, x1, x1, x0], [y0, y1, y1, y0, y0], color=(1, 1, 0.8), 

zorder=20) 

plt.imshow(*args, zorder=25, **kwargs) 

plt.xticks([]) 

plt.yticks([]) 

plt.axis(imExt(afwimg)) 

 

plt.clf() 

 

pa = dict(color='m', marker='.', linestyle='None', zorder=30) 

 

R, C = 3, 6 

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

myimshow(img, **ima) 

ax = plt.axis() 

plt.plot(X, Y, **pa) 

plt.axis(ax) 

plt.title('Image') 

 

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

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

ax = plt.axis() 

plt.plot(PX, PY, **pa) 

plt.axis(ax) 

plt.title('Footprint') 

 

sumimg = None 

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

 

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

myimshow(blobimgs[i], **ima) 

ax = plt.axis() 

plt.plot(PX[i], PY[i], **pa) 

plt.axis(ax) 

plt.title('true') 

 

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

t = dpk.origTemplate 

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

ax = plt.axis() 

plt.plot(PX[i], PY[i], **pa) 

plt.axis(ax) 

plt.title('symm') 

 

# monotonic template 

mimg = afwImage.ImageF(fpbb) 

afwDet.copyWithinFootprintImage(dpk.templateFootprint, 

dpk.templateImage, mimg) 

 

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

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

ax = plt.axis() 

plt.plot(PX[i], PY[i], **pa) 

plt.axis(ax) 

plt.title('monotonic') 

 

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

port = dpk.fluxPortion.getImage() 

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

plt.title('portion') 

ax = plt.axis() 

plt.plot(PX[i], PY[i], **pa) 

plt.axis(ax) 

 

if dpk.strayFlux is not None: 

simg = afwImage.ImageF(fpbb) 

dpk.strayFlux.insert(simg) 

 

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

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

plt.title('stray') 

ax = plt.axis() 

plt.plot(PX, PY, **pa) 

plt.axis(ax) 

 

himg2 = afwImage.ImageF(fpbb) 

portion = dpk.getFluxPortion() 

portion.insert(himg2) 

 

if sumimg is None: 

sumimg = himg2.getArray().copy() 

else: 

sumimg += himg2.getArray() 

 

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

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

plt.title('portion+stray') 

ax = plt.axis() 

plt.plot(PX, PY, **pa) 

plt.axis(ax) 

 

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

myimshow(sumimg, **ima) 

ax = plt.axis() 

plt.plot(X, Y, **pa) 

plt.axis(ax) 

plt.title('Sum of deblends') 

 

plt.suptitle(tt) 

fn = plotpat % (2*j + 1) 

plt.savefig(fn) 

print('Wrote', fn) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()