26 An example using SpatialCells 29 python SpatialCellExample.py 32 >>> import SpatialCellExample; SpatialCellExample.run() 45 import testSpatialCellLib
53 def readImage(filename=None):
54 """Read an image and background subtract it""" 60 "You must provide a filename or setup afwdata to run these examples")
62 filename = os.path.join(afwDataDir,
"CFHT",
"D4",
63 "cal-53535-i-797722_1.fits")
70 mi = afwImage.MaskedImageF(filename, bbox=bbox, origin=afwImage.LOCAL)
77 bctrl.setNxSample(int(mi.getWidth()/256) + 1)
78 bctrl.setNySample(int(mi.getHeight()/256) + 1)
79 sctrl = bctrl.getStatisticsControl()
80 sctrl.setNumSigmaClip(3.0)
86 except Exception
as e:
87 print(e, end=
' ', file=sys.stderr)
89 bctrl.setInterpStyle(afwMath.Interpolate.CONSTANT)
92 im -= backobj.getImageF()
96 threshold = afwDetect.Threshold(5, afwDetect.Threshold.STDEV)
98 fs = afwDetect.FootprintSet(mi, threshold,
"DETECTED", npixMin)
99 grow, isotropic = 1,
False 100 fs = afwDetect.FootprintSet(fs, grow, isotropic)
101 fs.setMask(mi.getMask(),
"DETECTED")
106 def SpatialCellSetDemo(filename=None):
107 """A demonstration of the use of a SpatialCellSet""" 109 im, fs = readImage(filename)
112 ds9.mtv(im, frame=0, title=
"Input")
120 for i
in range(len(cellSet.getCellList())):
121 cell = cellSet.getCellList()[i]
122 ds9.line([(cell.getBBox().getMinX(), cell.getBBox().getMinY()),
123 (cell.getBBox().getMinX(), cell.getBBox().getMaxY()),
124 (cell.getBBox().getMaxX(), cell.getBBox().getMaxY()),
125 (cell.getBBox().getMaxX(), cell.getBBox().getMinY()),
126 (cell.getBBox().getMinX(), cell.getBBox().getMinY()),
128 ds9.dot(cell.getLabel(),
129 (cell.getBBox().getMinX() + cell.getBBox().getMaxX())/2,
130 (cell.getBBox().getMinY() + cell.getBBox().getMaxY())/2)
134 for foot
in fs.getFootprints():
135 bbox = foot.getBBox()
136 xc = (bbox.getMinX() + bbox.getMaxX())/2.0
137 yc = (bbox.getMinY() + bbox.getMaxY())/2.0
138 tc = testSpatialCellLib.ExampleCandidate(xc, yc, im, bbox)
139 cellSet.insertCandidate(tc)
143 visitor = testSpatialCellLib.ExampleCandidateVisitor()
145 cellSet.visitCandidates(visitor)
146 print(
"There are %d candidates" % (visitor.getN()))
148 ctypes = [
"red",
"yellow",
"cyan", ]
149 for i
in range(cellSet.getCellList().size()):
150 cell = cellSet.getCellList()[i]
151 cell.visitCandidates(visitor)
157 w, h = cand.getBBox().getDimensions()
161 cand.setStatus(afwMath.SpatialCellCandidate.BAD)
164 ds9.dot(
"o", cand.getXCenter(), cand.getYCenter(),
165 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()))
186 cellSet.setIgnoreBad(
True)
187 cellSet.visitCandidates(visitor)
188 print(
"There are %d good candidates" % (visitor.getN()))
197 if __name__ ==
"__main__":