26 An example using SpatialCells
29 python SpatialCellExample.py
32 >>> import SpatialCellExample; SpatialCellExample.run()
40 import lsst.afw.image.imageLib
as afwImage
41 import lsst.afw.math.mathLib
as afwMath
45 import testSpatialCellLib
54 def readImage(filename=None):
55 """Read an image and background subtract it"""
60 raise RuntimeError(
"You must provide a filename or setup afwdata to run these examples")
62 filename = os.path.join(afwDataDir,
"CFHT",
"D4",
"cal-53535-i-797722_1")
68 mi = afwImage.MaskedImageF(filename, 0,
None, bbox, afwImage.LOCAL)
75 bctrl.setNxSample(int(mi.getWidth()/256) + 1)
76 bctrl.setNySample(int(mi.getHeight()/256) + 1)
77 sctrl = bctrl.getStatisticsControl()
78 sctrl.setNumSigmaClip(3.0)
85 print >> sys.stderr, e,
87 bctrl.setInterpStyle(afwMath.Interpolate.CONSTANT)
90 im -= backobj.getImageF()
97 grow, isotropic = 1,
False
99 fs.setMask(mi.getMask(),
"DETECTED")
105 def SpatialCellSetDemo(filename=None):
106 """A demonstration of the use of a SpatialCellSet"""
108 im, fs = readImage(filename)
111 ds9.mtv(im, frame=0, title=
"Input")
119 for i
in range(len(cellSet.getCellList())):
120 cell = cellSet.getCellList()[i]
121 ds9.line([(cell.getBBox().getMinX(), cell.getBBox().getMinY()),
122 (cell.getBBox().getMinX(), cell.getBBox().getMaxY()),
123 (cell.getBBox().getMaxX(), cell.getBBox().getMaxY()),
124 (cell.getBBox().getMaxX(), cell.getBBox().getMinY()),
125 (cell.getBBox().getMinX(), cell.getBBox().getMinY()),
127 ds9.dot(cell.getLabel(),
128 (cell.getBBox().getMinX() + cell.getBBox().getMaxX())/2,
129 (cell.getBBox().getMinY() + cell.getBBox().getMaxY())/2)
133 for foot
in fs.getFootprints():
134 bbox = foot.getBBox()
135 xc = (bbox.getMinX() + bbox.getMaxX())/2.0
136 yc = (bbox.getMinY() + bbox.getMaxY())/2.0
137 tc = testSpatialCellLib.ExampleCandidate(xc, yc, im, bbox)
138 cellSet.insertCandidate(tc)
142 visitor = testSpatialCellLib.ExampleCandidateVisitor()
144 cellSet.visitCandidates(visitor)
145 print "There are %d candidates" % (visitor.getN())
147 ctypes = [
"red",
"yellow",
"cyan", ]
148 for i
in range(cellSet.getCellList().size()):
149 cell = cellSet.getCellList()[i]
150 cell.visitCandidates(visitor)
158 cand = testSpatialCellLib.cast_ExampleCandidate(cand)
160 w, h = cand.getBBox().getDimensions()
163 cand.setStatus(afwMath.SpatialCellCandidate.BAD)
166 ds9.dot(
"o", cand.getXCenter(), cand.getYCenter(), size=4, ctype=ctypes[i%len(ctypes)])
169 ds9.dot(
"%s:%d" % (cand.getId(), j),
170 cand.getXCenter(), cand.getYCenter(), size=4, ctype=ctypes[i%len(ctypes)])
173 im = cand.getMaskedImage()
175 ds9.mtv(im, title=
"Candidate", frame=1)
179 for i
in range(len(cellSet.getCellList())):
180 cell = cellSet.getCellList()[i]
181 cell.visitCandidates(visitor)
183 cell.setIgnoreBad(
False)
184 print "%s nobj=%d N_good=%d NPix_good=%d" % \
185 (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__":