Coverage for tests/test_fitPsf.py: 16%
112 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-02-22 03:54 -0800
« prev ^ index » next coverage.py v6.5.0, created at 2023-02-22 03:54 -0800
1#
2# LSST Data Management System
3#
4# Copyright 2008-2016 AURA/LSST.
5#
6# This product includes software developed by the
7# LSST Project (http://www.lsst.org/).
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the LSST License Statement and
20# the GNU General Public License along with this program. If not,
21# see <https://www.lsstcorp.org/LegalNotices/>.
22#
23import unittest
24import numpy as np
26import lsst.utils.tests
27import lsst.afw.detection as afwDet
28import lsst.geom as geom
29import lsst.afw.geom as afwGeom
30import lsst.afw.image as afwImage
31from lsst.log import Log
32import lsst.meas.algorithms as measAlg
33from lsst.meas.deblender.plugins import _fitPsf
34from lsst.meas.deblender.baseline import DeblendedPeak, CachingPsf
36doPlot = False
37if doPlot: 37 ↛ 38line 37 didn't jump to line 38, because the condition on line 37 was never true
38 import matplotlib
39 matplotlib.use('Agg')
40 import pylab as plt
41 print("doPlot is set -- saving plots to current directory.")
42else:
43 print("doPlot is not set -- not saving plots. To enable plots, edit", __file__)
46class FitPsfTestCase(unittest.TestCase):
48 def test1(self):
50 # circle
51 spans = afwGeom.SpanSet.fromShape(45, offset=(50, 50))
52 fp = afwDet.Footprint(spans)
54 #
55 psfsig = 1.5
56 psffwhm = psfsig * 2.35
57 psf1 = measAlg.DoubleGaussianPsf(11, 11, psfsig)
59 psf2 = measAlg.DoubleGaussianPsf(100, 100, psfsig)
61 fbb = fp.getBBox()
62 print('fbb', fbb.getMinX(), fbb.getMaxX(), fbb.getMinY(), fbb.getMaxY())
64 fmask = afwImage.Mask(fbb)
65 fmask.setXY0(fbb.getMinX(), fbb.getMinY())
66 fp.spans.setMask(fmask, 1)
68 sig1 = 10.
70 img = afwImage.ImageF(fbb)
71 A = img.getArray()
72 A += np.random.normal(0, sig1, size=(fbb.getHeight(), fbb.getWidth()))
73 print('img x0,y0', img.getX0(), img.getY0())
74 print('BBox', img.getBBox())
76 peaks = afwDet.PeakCatalog(afwDet.PeakTable.makeMinimalSchema())
78 def makePeak(x, y):
79 p = peaks.addNew()
80 p.setFx(x)
81 p.setFy(y)
82 p.setIx(int(x))
83 p.setIy(int(y))
84 return p
85 pk1 = makePeak(20., 30.)
86 pk3 = makePeak(92., 50.)
88 ibb = img.getBBox()
89 iext = [ibb.getMinX(), ibb.getMaxX(), ibb.getMinY(), ibb.getMaxY()]
90 ix0, iy0 = iext[0], iext[2]
92 pbbs = []
93 pxys = []
95 fluxes = [10000., 5000., 5000.]
96 for pk, f in zip(peaks, fluxes):
97 psfim = psf1.computeImage(geom.Point2D(pk.getFx(), pk.getFy()))
98 print('psfim x0,y0', psfim.getX0(), psfim.getY0())
99 pbb = psfim.getBBox()
100 print('pbb', pbb.getMinX(), pbb.getMaxX(), pbb.getMinY(), pbb.getMaxY())
101 pbb.clip(ibb)
102 print('clipped pbb', pbb.getMinX(), pbb.getMaxX(), pbb.getMinY(), pbb.getMaxY())
103 psfim = psfim.Factory(psfim, pbb)
105 psfa = psfim.getArray()
106 psfa /= psfa.sum()
107 img.getArray()[pbb.getMinY() - iy0: pbb.getMaxY()+1 - iy0,
108 pbb.getMinX() - ix0: pbb.getMaxX()+1 - ix0] += f * psfa
110 pbbs.append((pbb.getMinX(), pbb.getMaxX(), pbb.getMinY(), pbb.getMaxY()))
111 pxys.append((pk.getFx(), pk.getFy()))
113 if doPlot:
114 plt.clf()
115 plt.imshow(img.getArray(), extent=iext, interpolation='nearest', origin='lower')
116 ax = plt.axis()
117 x0, x1, y0, y1 = fbb.getMinX(), fbb.getMaxX(), fbb.getMinY(), fbb.getMaxY()
118 plt.plot([x0, x0, x1, x1, x0], [y0, y1, y1, y0, y0], 'k-')
119 for x0, x1, y0, y1 in pbbs:
120 plt.plot([x0, x0, x1, x1, x0], [y0, y1, y1, y0, y0], 'r-')
121 for x, y in pxys:
122 plt.plot(x, y, 'ro')
123 plt.axis(ax)
124 plt.savefig('img.png')
126 varimg = afwImage.ImageF(fbb)
127 varimg.set(sig1**2)
129 psf_chisq_cut1 = psf_chisq_cut2 = psf_chisq_cut2b = 1.5
131 # pkres = PerPeak()
132 pkres = DeblendedPeak(pk1, 0, None)
134 loglvl = Log.INFO
135 # if verbose:
136 # loglvl = Log.DEBUG
137 log = Log.getLogger('tests.fit_psf')
138 log.setLevel(loglvl)
140 cpsf = CachingPsf(psf1)
142 peaksF = [pk.getF() for pk in peaks]
143 pkF = pk1.getF()
145 _fitPsf(fp, fmask, pk1, pkF, pkres, fbb, peaks, peaksF, log, cpsf, psffwhm,
146 img, varimg,
147 psf_chisq_cut1, psf_chisq_cut2, psf_chisq_cut2b)
148 for k in dir(pkres):
149 if k.startswith('__'):
150 continue
151 print(' ', k, getattr(pkres, k))
153 cpsf = CachingPsf(psf2)
154 _fitPsf(fp, fmask, pk1, pkF, pkres, fbb, peaks, peaksF, log, cpsf, psffwhm,
155 img, varimg,
156 psf_chisq_cut1, psf_chisq_cut2, psf_chisq_cut2b)
157 for k in dir(pkres):
158 if k.startswith('__'):
159 continue
160 print(' ', k, getattr(pkres, k))
162 pkF = pk3.getF()
163 _fitPsf(fp, fmask, pk3, pkF, pkres, fbb, peaks, peaksF, log, cpsf, psffwhm,
164 img, varimg,
165 psf_chisq_cut1, psf_chisq_cut2, psf_chisq_cut2b)
166 for k in dir(pkres):
167 if k.startswith('__'):
168 continue
169 print(' ', k, getattr(pkres, k))
172class TestMemory(lsst.utils.tests.MemoryTestCase):
173 pass
176def setup_module(module):
177 lsst.utils.tests.init()
180if __name__ == "__main__": 180 ↛ 181line 180 didn't jump to line 181, because the condition on line 180 was never true
181 lsst.utils.tests.init()
182 unittest.main()