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"""
57 dataDir = eups.productDir(
"afwdata")
59 raise RuntimeError(
"You must provide a filename or setup afwdata to run these examples")
61 filename = os.path.join(eups.productDir(
"afwdata"),
"CFHT",
"D4",
"cal-53535-i-797722_1")
67 mi = afwImage.MaskedImageF(filename, 0,
None, bbox, afwImage.LOCAL)
74 bctrl.setNxSample(int(mi.getWidth()/256) + 1)
75 bctrl.setNySample(int(mi.getHeight()/256) + 1)
76 sctrl = bctrl.getStatisticsControl()
77 sctrl.setNumSigmaClip(3.0)
84 print >> sys.stderr, e,
86 bctrl.setInterpStyle(afwMath.Interpolate.CONSTANT)
89 im -= backobj.getImageF()
96 grow, isotropic = 1,
False
98 fs.setMask(mi.getMask(),
"DETECTED")
104 def SpatialCellSetDemo(filename=None):
105 """A demonstration of the use of a SpatialCellSet"""
107 im, fs = readImage(filename)
110 ds9.mtv(im, frame=0, title=
"Input")
118 for i
in range(len(cellSet.getCellList())):
119 cell = cellSet.getCellList()[i]
120 ds9.line([(cell.getBBox().getMinX(), cell.getBBox().getMinY()),
121 (cell.getBBox().getMinX(), cell.getBBox().getMaxY()),
122 (cell.getBBox().getMaxX(), cell.getBBox().getMaxY()),
123 (cell.getBBox().getMaxX(), cell.getBBox().getMinY()),
124 (cell.getBBox().getMinX(), cell.getBBox().getMinY()),
126 ds9.dot(cell.getLabel(),
127 (cell.getBBox().getMinX() + cell.getBBox().getMaxX())/2,
128 (cell.getBBox().getMinY() + cell.getBBox().getMaxY())/2)
132 for foot
in fs.getFootprints():
133 bbox = foot.getBBox()
134 xc = (bbox.getMinX() + bbox.getMaxX())/2.0
135 yc = (bbox.getMinY() + bbox.getMaxY())/2.0
136 tc = testSpatialCellLib.ExampleCandidate(xc, yc, im, bbox)
137 cellSet.insertCandidate(tc)
141 visitor = testSpatialCellLib.ExampleCandidateVisitor()
143 cellSet.visitCandidates(visitor)
144 print "There are %d candidates" % (visitor.getN())
146 ctypes = [
"red",
"yellow",
"cyan", ]
147 for i
in range(cellSet.getCellList().size()):
148 cell = cellSet.getCellList()[i]
149 cell.visitCandidates(visitor)
157 cand = testSpatialCellLib.cast_ExampleCandidate(cand)
159 w, h = cand.getBBox().getDimensions()
162 cand.setStatus(afwMath.SpatialCellCandidate.BAD)
165 ds9.dot(
"o", cand.getXCenter(), cand.getYCenter(), size=4, ctype=ctypes[i%len(ctypes)])
168 ds9.dot(
"%s:%d" % (cand.getId(), j),
169 cand.getXCenter(), cand.getYCenter(), size=4, ctype=ctypes[i%len(ctypes)])
172 im = cand.getMaskedImage()
174 ds9.mtv(im, title=
"Candidate", frame=1)
178 for i
in range(len(cellSet.getCellList())):
179 cell = cellSet.getCellList()[i]
180 cell.visitCandidates(visitor)
182 cell.setIgnoreBad(
False)
183 print "%s nobj=%d N_good=%d NPix_good=%d" % \
184 (cell.getLabel(), cell.size(), visitor.getN(), visitor.getNPix())
187 cellSet.setIgnoreBad(
True)
188 cellSet.visitCandidates(visitor)
189 print "There are %d good candidates" % (visitor.getN())
198 if __name__ ==
"__main__":