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

# 

# 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.afw.geom as afwGeom 

import lsst.afw.image as afwImage 

from lsst.log import Log 

import lsst.meas.algorithms as measAlg 

from lsst.meas.deblender.plugins import _fitPsf 

from lsst.meas.deblender.baseline import DeblendedPeak, CachingPsf 

 

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 

print("doPlot is set -- saving plots to current directory.") 

else: 

print("doPlot is not set -- not saving plots. To enable plots, edit", __file__) 

 

 

class FitPsfTestCase(unittest.TestCase): 

 

def test1(self): 

 

# circle 

spans = afwGeom.SpanSet.fromShape(45, offset=(50, 50)) 

fp = afwDet.Footprint(spans) 

 

# 

psfsig = 1.5 

psffwhm = psfsig * 2.35 

psf1 = measAlg.DoubleGaussianPsf(11, 11, psfsig) 

 

psf2 = measAlg.DoubleGaussianPsf(100, 100, psfsig) 

 

fbb = fp.getBBox() 

print('fbb', fbb.getMinX(), fbb.getMaxX(), fbb.getMinY(), fbb.getMaxY()) 

 

fmask = afwImage.Mask(fbb) 

fmask.setXY0(fbb.getMinX(), fbb.getMinY()) 

fp.spans.setMask(fmask, 1) 

 

sig1 = 10. 

 

img = afwImage.ImageF(fbb) 

A = img.getArray() 

A += np.random.normal(0, sig1, size=(fbb.getHeight(), fbb.getWidth())) 

print('img x0,y0', img.getX0(), img.getY0()) 

print('BBox', img.getBBox()) 

 

peaks = afwDet.PeakCatalog(afwDet.PeakTable.makeMinimalSchema()) 

 

def makePeak(x, y): 

p = peaks.addNew() 

p.setFx(x) 

p.setFy(y) 

p.setIx(int(x)) 

p.setIy(int(y)) 

return p 

pk1 = makePeak(20., 30.) 

pk3 = makePeak(92., 50.) 

 

ibb = img.getBBox() 

iext = [ibb.getMinX(), ibb.getMaxX(), ibb.getMinY(), ibb.getMaxY()] 

ix0, iy0 = iext[0], iext[2] 

 

pbbs = [] 

pxys = [] 

 

fluxes = [10000., 5000., 5000.] 

for pk, f in zip(peaks, fluxes): 

psfim = psf1.computeImage(afwGeom.Point2D(pk.getFx(), pk.getFy())) 

print('psfim x0,y0', psfim.getX0(), psfim.getY0()) 

pbb = psfim.getBBox() 

print('pbb', pbb.getMinX(), pbb.getMaxX(), pbb.getMinY(), pbb.getMaxY()) 

pbb.clip(ibb) 

print('clipped pbb', pbb.getMinX(), pbb.getMaxX(), pbb.getMinY(), pbb.getMaxY()) 

psfim = psfim.Factory(psfim, pbb) 

 

psfa = psfim.getArray() 

psfa /= psfa.sum() 

img.getArray()[pbb.getMinY() - iy0: pbb.getMaxY()+1 - iy0, 

pbb.getMinX() - ix0: pbb.getMaxX()+1 - ix0] += f * psfa 

 

pbbs.append((pbb.getMinX(), pbb.getMaxX(), pbb.getMinY(), pbb.getMaxY())) 

pxys.append((pk.getFx(), pk.getFy())) 

 

112 ↛ 113line 112 didn't jump to line 113, because the condition on line 112 was never true if doPlot: 

plt.clf() 

plt.imshow(img.getArray(), extent=iext, interpolation='nearest', origin='lower') 

ax = plt.axis() 

x0, x1, y0, y1 = fbb.getMinX(), fbb.getMaxX(), fbb.getMinY(), fbb.getMaxY() 

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

for x0, x1, y0, y1 in pbbs: 

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

for x, y in pxys: 

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

plt.axis(ax) 

plt.savefig('img.png') 

 

varimg = afwImage.ImageF(fbb) 

varimg.set(sig1**2) 

 

psf_chisq_cut1 = psf_chisq_cut2 = psf_chisq_cut2b = 1.5 

 

# pkres = PerPeak() 

pkres = DeblendedPeak(pk1, 0, None) 

 

loglvl = Log.INFO 

# if verbose: 

# loglvl = Log.DEBUG 

log = Log.getLogger('tests.fit_psf') 

log.setLevel(loglvl) 

 

cpsf = CachingPsf(psf1) 

 

peaksF = [pk.getF() for pk in peaks] 

pkF = pk1.getF() 

 

_fitPsf(fp, fmask, pk1, pkF, pkres, fbb, peaks, peaksF, log, cpsf, psffwhm, 

img, varimg, 

psf_chisq_cut1, psf_chisq_cut2, psf_chisq_cut2b) 

for k in dir(pkres): 

if k.startswith('__'): 

continue 

print(' ', k, getattr(pkres, k)) 

 

cpsf = CachingPsf(psf2) 

_fitPsf(fp, fmask, pk1, pkF, pkres, fbb, peaks, peaksF, log, cpsf, psffwhm, 

img, varimg, 

psf_chisq_cut1, psf_chisq_cut2, psf_chisq_cut2b) 

for k in dir(pkres): 

if k.startswith('__'): 

continue 

print(' ', k, getattr(pkres, k)) 

 

pkF = pk3.getF() 

_fitPsf(fp, fmask, pk3, pkF, pkres, fbb, peaks, peaksF, log, cpsf, psffwhm, 

img, varimg, 

psf_chisq_cut1, psf_chisq_cut2, psf_chisq_cut2b) 

for k in dir(pkres): 

if k.startswith('__'): 

continue 

print(' ', k, getattr(pkres, k)) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()