26 An example using SpatialCells
29 python SpatialCellExample.py
32 >>> import SpatialCellExample; SpatialCellExample.run()
34 from __future__
import absolute_import, division, print_function
35 from builtins
import range
47 import testSpatialCellLib
55 def readImage(filename=None):
56 """Read an image and background subtract it"""
59 afwDataDir = lsst.utils.getPackageDir(
"afwdata")
62 "You must provide a filename or setup afwdata to run these examples")
64 filename = os.path.join(afwDataDir,
"CFHT",
"D4",
65 "cal-53535-i-797722_1.fits")
72 mi = afwImage.MaskedImageF(filename, bbox=bbox, origin=afwImage.LOCAL)
79 bctrl.setNxSample(int(mi.getWidth()/256) + 1)
80 bctrl.setNySample(int(mi.getHeight()/256) + 1)
81 sctrl = bctrl.getStatisticsControl()
82 sctrl.setNumSigmaClip(3.0)
88 except Exception
as e:
89 print(e, end=
' ', file=sys.stderr)
91 bctrl.setInterpStyle(afwMath.Interpolate.CONSTANT)
94 im -= backobj.getImageF()
98 threshold = afwDetect.Threshold(5, afwDetect.Threshold.STDEV)
100 fs = afwDetect.FootprintSet(mi, threshold,
"DETECTED", npixMin)
101 grow, isotropic = 1,
False
102 fs = afwDetect.FootprintSet(fs, grow, isotropic)
103 fs.setMask(mi.getMask(),
"DETECTED")
108 def SpatialCellSetDemo(filename=None):
109 """A demonstration of the use of a SpatialCellSet"""
111 im, fs = readImage(filename)
114 ds9.mtv(im, frame=0, title=
"Input")
122 for i
in range(len(cellSet.getCellList())):
123 cell = cellSet.getCellList()[i]
124 ds9.line([(cell.getBBox().getMinX(), cell.getBBox().getMinY()),
125 (cell.getBBox().getMinX(), cell.getBBox().getMaxY()),
126 (cell.getBBox().getMaxX(), cell.getBBox().getMaxY()),
127 (cell.getBBox().getMaxX(), cell.getBBox().getMinY()),
128 (cell.getBBox().getMinX(), cell.getBBox().getMinY()),
130 ds9.dot(cell.getLabel(),
131 (cell.getBBox().getMinX() + cell.getBBox().getMaxX())/2,
132 (cell.getBBox().getMinY() + cell.getBBox().getMaxY())/2)
136 for foot
in fs.getFootprints():
137 bbox = foot.getBBox()
138 xc = (bbox.getMinX() + bbox.getMaxX())/2.0
139 yc = (bbox.getMinY() + bbox.getMaxY())/2.0
140 tc = testSpatialCellLib.ExampleCandidate(xc, yc, im, bbox)
141 cellSet.insertCandidate(tc)
145 visitor = testSpatialCellLib.ExampleCandidateVisitor()
147 cellSet.visitCandidates(visitor)
148 print(
"There are %d candidates" % (visitor.getN()))
150 ctypes = [
"red",
"yellow",
"cyan", ]
151 for i
in range(cellSet.getCellList().size()):
152 cell = cellSet.getCellList()[i]
153 cell.visitCandidates(visitor)
159 w, h = cand.getBBox().getDimensions()
163 cand.setStatus(afwMath.SpatialCellCandidate.BAD)
166 ds9.dot(
"o", cand.getXCenter(), cand.getYCenter(),
167 size=4, ctype=ctypes[i%len(ctypes)])
170 ds9.dot(
"%s:%d" % (cand.getId(), j),
171 cand.getXCenter(), cand.getYCenter(), size=4, ctype=ctypes[i%len(ctypes)])
174 im = cand.getMaskedImage()
176 ds9.mtv(im, title=
"Candidate", frame=1)
180 for i
in range(len(cellSet.getCellList())):
181 cell = cellSet.getCellList()[i]
182 cell.visitCandidates(visitor)
184 cell.setIgnoreBad(
False)
185 print(
"%s nobj=%d N_good=%d NPix_good=%d" %
186 (cell.getLabel(), cell.size(), visitor.getN(), visitor.getNPix()))
188 cellSet.setIgnoreBad(
True)
189 cellSet.visitCandidates(visitor)
190 print(
"There are %d good candidates" % (visitor.getN()))
199 if __name__ ==
"__main__":