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

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

from __future__ import print_function 

from __future__ import absolute_import 

from builtins import zip 

import matplotlib 

matplotlib.use('Agg') 

import pylab as plt 

import numpy as np 

import lsst.afw.image as afwImage 

import lsst.afw.detection as afwDet 

import lsst.log 

 

from .utils import * 

from .suprime import * 

 

 

def main(): 

''' 

Runs the deblender and creates plots for the "design document", 

doc/design.tex. See the file NOTES for how to get set up to the 

point where you can actually run this on data. 

''' 

 

from optparse import OptionParser 

parser = OptionParser() 

parser.add_option('--root', dest='root', help='Root directory for Subaru data') 

parser.add_option('--outroot', '-o', dest='outroot', help='Output root directory for Subaru data') 

parser.add_option('--sources', help='Read a FITS table of sources') 

parser.add_option('--calexp', help='Read a FITS calexp') 

parser.add_option('--psf', help='Read a FITS PSF') 

 

parser.add_option('--drill', '-D', dest='drill', action='append', type=str, default=[], 

help='Drill down on individual source IDs') 

parser.add_option('--drillxy', dest='drillxy', action='append', type=str, default=[], 

help='Drill down on individual source positions, eg 132,46;54,67') 

parser.add_option('--visit', dest='visit', type=int, default=108792, help='Suprimecam visit id') 

parser.add_option('--ccd', dest='ccd', type=int, default=5, help='Suprimecam CCD number') 

parser.add_option('--prefix', dest='prefix', default='design-', help='plot filename prefix') 

parser.add_option('--suffix', dest='suffix', default=None, help='plot filename suffix (default: ".png")') 

parser.add_option('--pat', dest='pat', 

help='Plot filename pattern: eg, "design-%(pid)04i-%(name).png"; overrides --prefix and --suffix') 

parser.add_option('--pdf', dest='pdf', action='store_true', default=False, help='save in PDF format?') 

parser.add_option('-v', dest='verbose', action='store_true') 

parser.add_option('--figw', dest='figw', type=float, help='Figure window width (inches)', 

default=4.) 

parser.add_option('--figh', dest='figh', type=float, help='Figure window height (inches)', 

default=4.) 

parser.add_option('--order', dest='order', type=str, help='Child order: eg 3,0,1,2') 

 

parser.add_option('--sdss', dest='sec', action='store_const', const='sdss', 

help='Produce plots for the SDSS section.') 

parser.add_option('--mono', dest='sec', action='store_const', const='mono', 

help='Produce plots for the "monotonic" section.') 

parser.add_option('--median', dest='sec', action='store_const', const='median', 

help='Produce plots for the "median filter" section.') 

parser.add_option('--ramp', dest='sec', action='store_const', const='ramp', 

help='Produce plots for the "ramp edges" section.') 

parser.add_option('--ramp2', dest='sec', action='store_const', const='ramp2', 

help='Produce plots for the "ramp edges + stray flux" section.') 

parser.add_option('--patch', dest='sec', action='store_const', const='patch', 

help='Produce plots for the "patch edges" section.') 

 

opt, args = parser.parse_args() 

 

# Logging 

if opt.verbose: 

lsst.log.setLevel('', lsst.log.DEBUG) 

else: 

lsst.log.setLevel('', lsst.log.INFO) 

 

if opt.sec is None: 

opt.sec = 'sdss' 

if opt.pdf: 

if opt.suffix is None: 

opt.suffix = '' 

opt.suffix += '.pdf' 

if not opt.suffix: 

opt.suffix = '.png' 

 

if opt.pat: 

plotpattern = opt.pat 

else: 

plotpattern = opt.prefix + '%(pid)04i-%(name)s' + opt.suffix 

 

if opt.order is not None: 

opt.order = [int(x) for x in opt.order.split(',')] 

invorder = np.zeros(len(opt.order)) 

invorder[opt.order] = np.arange(len(opt.order)) 

 

def mapchild(i): 

if opt.order is None: 

return i 

return invorder[i] 

 

def savefig(pid, figname): 

fn = plotpattern % dict(pid=pid, name=figname) 

plt.savefig(fn) 

 

# Load data using the butler, if desired 

dr = None 

if opt.sources is None or opt.calexp is None: 

print('Creating DataRef...') 

dr = getSuprimeDataref(opt.visit, opt.ccd, rootdir=opt.root, 

outrootdir=opt.outroot) 

print('Got', dr) 

 

# Which parent ids / deblend families are we going to plot? 

keepids = None 

if len(opt.drill): 

keepids = [] 

for d in opt.drill: 

for dd in d.split(','): 

keepids.append(int(dd)) 

print('Keeping parent ids', keepids) 

 

keepxys = None 

if len(opt.drillxy): 

keepxys = [] 

for d in opt.drillxy: 

for dd in d.split(';'): 

xy = dd.split(',') 

assert(len(xy) == 2) 

keepxys.append((int(xy[0]), int(xy[1]))) 

print('Keeping parents at xy', keepxys) 

 

# Read from butler or local file 

cat = readCatalog(opt.sources, None, dataref=dr, keepids=keepids, 

keepxys=keepxys, patargs=dict(visit=opt.visit, ccd=opt.ccd)) 

print('Got', len(cat), 'sources') 

 

# Load data from butler or local files 

if opt.calexp is not None: 

print('Reading exposure from', opt.calexp) 

exposure = afwImage.ExposureF(opt.calexp) 

else: 

exposure = dr.get('calexp') 

print('Exposure', exposure) 

mi = exposure.getMaskedImage() 

 

if opt.psf is not None: 

print('Reading PSF from', opt.psf) 

psf = afwDet.Psf.readFits(opt.psf) 

print('Got', psf) 

elif dr: 

psf = dr.get('psf') 

else: 

psf = exposure.getPsf() 

 

sigma1 = get_sigma1(mi) 

 

fams = getFamilies(cat) 

print(len(fams), 'deblend families') 

 

if False: 

for j, (parent, children) in enumerate(fams): 

print('parent', parent) 

print('children', children) 

plotDeblendFamily(mi, parent, children, cat, sigma1, ellipses=False) 

fn = '%04i.png' % parent.getId() 

plt.savefig(fn) 

print('wrote', fn) 

 

def nlmap(X): 

return np.arcsinh(X / (3.*sigma1)) 

 

def myimshow(im, **kwargs): 

kwargs = kwargs.copy() 

mn = kwargs.get('vmin', -5*sigma1) 

kwargs['vmin'] = nlmap(mn) 

mx = kwargs.get('vmax', 100*sigma1) 

kwargs['vmax'] = nlmap(mx) 

plt.imshow(nlmap(im), **kwargs) 

plt.figure(figsize=(opt.figw, opt.figh)) 

plt.subplot(1, 1, 1) 

plt.subplots_adjust(left=0.01, right=0.99, bottom=0.01, top=0.99, 

wspace=0.05, hspace=0.1) 

 

# Make plots for each deblend family. 

 

for j, (parent, children) in enumerate(fams): 

print('parent', parent.getId()) 

print('children', [ch.getId() for ch in children]) 

print('parent x,y', parent.getX(), parent.getY()) 

 

pid = parent.getId() 

fp = parent.getFootprint() 

bb = fp.getBBox() 

pim = footprintToImage(parent.getFootprint(), mi).getArray() 

pext = getExtent(bb) 

imargs = dict(interpolation='nearest', origin='lower', vmax=pim.max() * 0.95, vmin=-3.*sigma1) 

pksty = dict(linestyle='None', marker='+', color='r', mew=3, ms=20, alpha=0.6) 

 

plt.clf() 

myimshow(afwImage.ImageF(mi.getImage(), bb).getArray(), **imargs) 

plt.gray() 

plt.xticks([]) 

plt.yticks([]) 

savefig(pid, 'image') 

 

# Parent footprint 

plt.clf() 

myimshow(pim, extent=pext, **imargs) 

plt.gray() 

pks = fp.getPeaks() 

plt.plot([pk.getIx() for pk in pks], [pk.getIy() for pk in pks], **pksty) 

plt.xticks([]) 

plt.yticks([]) 

plt.axis(pext) 

savefig(pid, 'parent') 

 

from lsst.meas.deblender.baseline import deblend 

 

xc = int((bb.getMinX() + bb.getMaxX()) / 2.) 

yc = int((bb.getMinY() + bb.getMaxY()) / 2.) 

if hasattr(psf, 'getFwhm'): 

psf_fwhm = psf.getFwhm(xc, yc) 

else: 

psf_fwhm = psf.computeShape().getDeterminantRadius() * 2.35 

 

# Each section of the design doc runs the deblender with different args. 

 

kwargs = dict(sigma1=sigma1, verbose=opt.verbose, 

getTemplateSum=True) 

 

basic = kwargs.copy() 

basic.update(fit_psfs=False, 

median_smooth_template=False, 

monotonic_template=False, 

lstsq_weight_templates=False, 

assignStrayFlux=False, 

rampFluxAtEdge=False, 

patchEdges=False) 

 

if opt.sec == 'sdss': 

# SDSS intro 

kwargs = basic 

kwargs.update(lstsq_weight_templates=True) 

 

elif opt.sec == 'mono': 

kwargs = basic 

kwargs.update(lstsq_weight_templates=True, 

monotonic_template=True) 

elif opt.sec == 'median': 

kwargs = basic 

kwargs.update(lstsq_weight_templates=True, 

median_smooth_template=True, 

monotonic_template=True) 

elif opt.sec == 'ramp': 

kwargs = basic 

kwargs.update(median_smooth_template=True, 

monotonic_template=True, 

rampFluxAtEdge=True) 

 

elif opt.sec == 'ramp2': 

kwargs = basic 

kwargs.update(median_smooth_template=True, 

monotonic_template=True, 

rampFluxAtEdge=True, 

assignStrayFlux=True) 

 

elif opt.sec == 'patch': 

kwargs = basic 

kwargs.update(median_smooth_template=True, 

monotonic_template=True, 

patchEdges=True) 

 

else: 

raise 'Unknown section: "%s"' % opt.sec 

 

print('Running deblender with kwargs:', kwargs) 

res = deblend(fp, mi, psf, psf_fwhm, **kwargs) 

#print 'got result with', [x for x in dir(res) if not x.startswith('__')] 

#for pk in res.peaks: 

# print 'got peak with', [x for x in dir(pk) if not x.startswith('__')] 

# print ' deblend as psf?', pk.deblend_as_psf 

 

# Find bounding-box of all templates. 

tbb = fp.getBBox() 

for pkres, pk in zip(res.peaks, pks): 

tbb.include(pkres.template_foot.getBBox()) 

print('Bounding-box of all templates:', tbb) 

 

# Sum-of-templates plot 

tsum = np.zeros((tbb.getHeight(), tbb.getWidth())) 

tx0, ty0 = tbb.getMinX(), tbb.getMinY() 

 

# Sum-of-deblended children plot(s) 

# "heavy" bbox == template bbox. 

hsum = np.zeros((tbb.getHeight(), tbb.getWidth())) 

hsum2 = np.zeros((tbb.getHeight(), tbb.getWidth())) 

 

# Sum of templates from the deblender itself 

plt.clf() 

t = res.templateSum 

myimshow(t.getArray(), extent=getExtent(t.getBBox()), **imargs) 

plt.gray() 

plt.xticks([]) 

plt.yticks([]) 

savefig(pid, 'tsum1') 

 

# Make plots for each deblended child (peak) 

 

k = 0 

for pkres, pk in zip(res.peaks, pks): 

 

heavy = pkres.get_flux_portion() 

if heavy is None: 

print('Child has no HeavyFootprint -- skipping') 

continue 

 

kk = mapchild(k) 

 

w = pkres.template_weight 

 

cfp = pkres.template_foot 

cbb = cfp.getBBox() 

cext = getExtent(cbb) 

 

# Template image 

tim = pkres.template_mimg.getImage() 

timext = cext 

tim = tim.getArray() 

 

(x0, x1, y0, y1) = timext 

print('tim ext', timext) 

tsum[y0-ty0:y1-ty0, x0-tx0:x1-tx0] += tim 

 

# "Heavy" image -- flux assigned to child 

him = footprintToImage(heavy).getArray() 

hext = getExtent(heavy.getBBox()) 

 

(x0, x1, y0, y1) = hext 

hsum[y0-ty0:y1-ty0, x0-tx0:x1-tx0] += him 

 

# "Heavy" without stray flux 

h2 = pkres.get_flux_portion(strayFlux=False) 

him2 = footprintToImage(h2).getArray() 

hext2 = getExtent(h2.getBBox()) 

(x0, x1, y0, y1) = hext2 

hsum2[y0-ty0:y1-ty0, x0-tx0:x1-tx0] += him2 

 

if opt.sec == 'median': 

try: 

med = pkres.median_filtered_template 

except: 

med = pkres.orig_template 

 

for im, nm in [(pkres.orig_template, 'symm'), (med, 'med')]: 

#print 'im:', im 

plt.clf() 

myimshow(im.getArray(), extent=cext, **imargs) 

plt.gray() 

plt.xticks([]) 

plt.yticks([]) 

plt.plot([pk.getIx()], [pk.getIy()], **pksty) 

plt.axis(pext) 

savefig(pid, nm + '%i' % (kk)) 

 

# Template 

plt.clf() 

myimshow(pkres.template_mimg.getImage().getArray() / w, extent=cext, **imargs) 

plt.gray() 

plt.xticks([]) 

plt.yticks([]) 

plt.plot([pk.getIx()], [pk.getIy()], **pksty) 

plt.axis(pext) 

savefig(pid, 't%i' % (kk)) 

 

# Weighted template 

plt.clf() 

myimshow(tim, extent=cext, **imargs) 

plt.gray() 

plt.xticks([]) 

plt.yticks([]) 

plt.plot([pk.getIx()], [pk.getIy()], **pksty) 

plt.axis(pext) 

savefig(pid, 'tw%i' % (kk)) 

 

# "Heavy" 

plt.clf() 

myimshow(him, extent=hext, **imargs) 

plt.gray() 

plt.xticks([]) 

plt.yticks([]) 

plt.plot([pk.getIx()], [pk.getIy()], **pksty) 

plt.axis(pext) 

savefig(pid, 'h%i' % (kk)) 

 

# Original symmetric template 

plt.clf() 

t = pkres.orig_template 

foot = pkres.orig_foot 

myimshow(t.getArray(), extent=getExtent(foot.getBBox()), **imargs) 

plt.gray() 

plt.xticks([]) 

plt.yticks([]) 

plt.plot([pk.getIx()], [pk.getIy()], **pksty) 

plt.axis(pext) 

savefig(pid, 'o%i' % (kk)) 

 

if opt.sec == 'patch' and pkres.patched: 

pass 

 

if opt.sec in ['ramp', 'ramp2'] and pkres.has_ramped_template: 

 

# Ramped template 

plt.clf() 

t = pkres.ramped_template 

myimshow(t.getArray(), extent=getExtent(t.getBBox()), 

**imargs) 

plt.gray() 

plt.xticks([]) 

plt.yticks([]) 

plt.plot([pk.getIx()], [pk.getIy()], **pksty) 

plt.axis(pext) 

savefig(pid, 'r%i' % (kk)) 

 

# Median-filtered template 

plt.clf() 

t = pkres.median_filtered_template 

myimshow(t.getArray(), extent=getExtent(t.getBBox()), 

**imargs) 

plt.gray() 

plt.xticks([]) 

plt.yticks([]) 

plt.plot([pk.getIx()], [pk.getIy()], **pksty) 

plt.axis(pext) 

savefig(pid, 'med%i' % (kk)) 

 

# Assigned flux 

plt.clf() 

t = pkres.portion_mimg.getImage() 

myimshow(t.getArray(), extent=getExtent(t.getBBox()), 

**imargs) 

plt.gray() 

plt.xticks([]) 

plt.yticks([]) 

plt.plot([pk.getIx()], [pk.getIy()], **pksty) 

plt.axis(pext) 

savefig(pid, 'p%i' % (kk)) 

 

if opt.sec == 'ramp2': 

# stray flux 

if pkres.stray_flux is not None: 

s = pkres.stray_flux 

strayim = footprintToImage(s).getArray() 

strayext = getExtent(s.getBBox()) 

 

plt.clf() 

myimshow(strayim, extent=strayext, **imargs) 

plt.gray() 

plt.xticks([]) 

plt.yticks([]) 

plt.plot([pk.getIx()], [pk.getIy()], **pksty) 

plt.axis(pext) 

savefig(pid, 's%i' % (kk)) 

 

# Assigned flux, omitting stray flux. 

plt.clf() 

myimshow(him2, extent=hext2, **imargs) 

plt.gray() 

plt.xticks([]) 

plt.yticks([]) 

plt.plot([pk.getIx()], [pk.getIy()], **pksty) 

plt.axis(pext) 

savefig(pid, 'hb%i' % (kk)) 

 

k += 1 

 

# sum of templates 

plt.clf() 

myimshow(tsum, extent=getExtent(tbb), **imargs) 

plt.gray() 

plt.xticks([]) 

plt.yticks([]) 

plt.plot([pk.getIx() for pk in pks], [pk.getIy() for pk in pks], **pksty) 

plt.axis(pext) 

savefig(pid, 'tsum') 

 

# sum of assigned flux 

plt.clf() 

myimshow(hsum, extent=getExtent(tbb), **imargs) 

plt.gray() 

plt.xticks([]) 

plt.yticks([]) 

plt.plot([pk.getIx() for pk in pks], [pk.getIy() for pk in pks], **pksty) 

plt.axis(pext) 

savefig(pid, 'hsum') 

 

plt.clf() 

myimshow(hsum2, extent=getExtent(tbb), **imargs) 

plt.gray() 

plt.xticks([]) 

plt.yticks([]) 

plt.plot([pk.getIx() for pk in pks], [pk.getIy() for pk in pks], **pksty) 

plt.axis(pext) 

savefig(pid, 'hsum2') 

 

k = 0 

for pkres, pk in zip(res.peaks, pks): 

heavy = pkres.get_flux_portion() 

if heavy is None: 

continue 

 

print('Template footprint:', pkres.template_foot.getBBox()) 

print('Template img:', pkres.template_mimg.getBBox()) 

print('Heavy footprint:', heavy.getBBox()) 

 

cfp = pkres.template_foot 

cbb = cfp.getBBox() 

cext = getExtent(cbb) 

tim = pkres.template_mimg.getImage().getArray() 

(x0, x1, y0, y1) = cext 

 

frac = tim / tsum[y0-ty0:y1-ty0, x0-tx0:x1-tx0] 

 

msk = afwImage.ImageF(cbb.getWidth(), cbb.getHeight()) 

msk.setXY0(cbb.getMinX(), cbb.getMinY()) 

afwDet.setImageFromFootprint(msk, cfp, 1.) 

msk = msk.getArray() 

frac[msk == 0.] = np.nan 

 

# Fraction of flux assigned to this child. 

plt.clf() 

plt.imshow(frac, extent=cext, interpolation='nearest', origin='lower', vmin=0, vmax=1) 

#plt.plot([x0,x0,x1,x1,x0], [y0,y1,y1,y0,y0], 'k-') 

plt.gray() 

plt.xticks([]) 

plt.yticks([]) 

plt.plot([pk.getIx()], [pk.getIy()], **pksty) 

plt.gca().set_axis_bgcolor((0.9, 0.9, 0.5)) 

plt.axis(pext) 

savefig(pid, 'f%i' % (mapchild(k))) 

 

k += 1 

 

if __name__ == '__main__': 

main()