26 An example using SpatialCells
29 python SpatialCellExample.py
32 >>> import SpatialCellExample; SpatialCellExample.run()
34 from __future__
import print_function
35 from __future__
import division
36 from builtins
import range
43 import lsst.afw.image.imageLib
as afwImage
44 import lsst.afw.math.mathLib
as afwMath
48 import testSpatialCellLib
57 def readImage(filename=None):
58 """Read an image and background subtract it"""
63 raise RuntimeError(
"You must provide a filename or setup afwdata to run these examples")
65 filename = os.path.join(afwDataDir,
"CFHT",
"D4",
"cal-53535-i-797722_1")
71 mi = afwImage.MaskedImageF(filename, 0,
None, bbox, afwImage.LOCAL)
78 bctrl.setNxSample(int(mi.getWidth()/256) + 1)
79 bctrl.setNySample(int(mi.getHeight()/256) + 1)
80 sctrl = bctrl.getStatisticsControl()
81 sctrl.setNumSigmaClip(3.0)
87 except Exception
as e:
88 print(e, end=
' ', file=sys.stderr)
90 bctrl.setInterpStyle(afwMath.Interpolate.CONSTANT)
93 im -= backobj.getImageF()
100 grow, isotropic = 1,
False
102 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)
161 cand = testSpatialCellLib.cast_ExampleCandidate(cand)
163 w, h = cand.getBBox().getDimensions()
166 cand.setStatus(afwMath.SpatialCellCandidate.BAD)
169 ds9.dot(
"o", cand.getXCenter(), cand.getYCenter(), size=4, ctype=ctypes[i%len(ctypes)])
172 ds9.dot(
"%s:%d" % (cand.getId(), j),
173 cand.getXCenter(), cand.getYCenter(), size=4, ctype=ctypes[i%len(ctypes)])
176 im = cand.getMaskedImage()
178 ds9.mtv(im, title=
"Candidate", frame=1)
182 for i
in range(len(cellSet.getCellList())):
183 cell = cellSet.getCellList()[i]
184 cell.visitCandidates(visitor)
186 cell.setIgnoreBad(
False)
187 print(
"%s nobj=%d N_good=%d NPix_good=%d" % \
188 (cell.getLabel(), cell.size(), visitor.getN(), visitor.getNPix()))
191 cellSet.setIgnoreBad(
True)
192 cellSet.visitCandidates(visitor)
193 print(
"There are %d good candidates" % (visitor.getN()))
202 if __name__ ==
"__main__":