LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
spatialCellExample.py
1 #!/usr/bin/env python
2 
3 #
4 # LSST Data Management System
5 # Copyright 2008, 2009, 2010 LSST Corporation.
6 #
7 # This product includes software developed by the
8 # LSST Project (http://www.lsst.org/).
9 #
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the LSST License Statement and
21 # the GNU General Public License along with this program. If not,
22 # see <http://www.lsstcorp.org/LegalNotices/>.
23 #
24 
25 """
26 An example using SpatialCells
27 
28 Run with:
29  python SpatialCellExample.py
30 or
31  python
32  >>> import SpatialCellExample; SpatialCellExample.run()
33 """
34 
35 import os
36 import sys
37 
38 import lsst.utils
39 import lsst.afw.detection as afwDetect
40 import lsst.afw.image.imageLib as afwImage
41 import lsst.afw.math.mathLib as afwMath
42 import lsst.afw.geom as afwGeom
43 import lsst.afw.display.ds9 as ds9
44 
45 import testSpatialCellLib
46 
47 try:
48  type(display)
49 except NameError:
50  display = False
51 
52 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
53 
54 def readImage(filename=None):
55  """Read an image and background subtract it"""
56  if not filename:
57  try:
58  afwDataDir = lsst.utils.getPackageDir("afwdata")
59  except Exception:
60  raise RuntimeError("You must provide a filename or setup afwdata to run these examples")
61 
62  filename = os.path.join(afwDataDir, "CFHT", "D4", "cal-53535-i-797722_1")
63 
64  bbox = afwGeom.Box2I(afwGeom.Point2I(270, 2530), afwGeom.Extent2I(512, 512))
65  else:
66  bbox = None
67 
68  mi = afwImage.MaskedImageF(filename, 0, None, bbox, afwImage.LOCAL)
69  mi.setXY0(afwGeom.Point2I(0, 0))
70  #
71  # Subtract the background. We'd use a canned procedure, but that's in meas/utils/sourceDetection.py. We
72  # can't fix those pesky cosmic rays either, as that's in a dependent product (meas/algorithms) too
73  #
74  bctrl = afwMath.BackgroundControl(afwMath.Interpolate.NATURAL_SPLINE)
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)
79  sctrl.setNumIter(2)
80 
81  im = mi.getImage()
82  try:
83  backobj = afwMath.makeBackground(im, bctrl)
84  except Exception, e:
85  print >> sys.stderr, e,
86 
87  bctrl.setInterpStyle(afwMath.Interpolate.CONSTANT)
88  backobj = afwMath.makeBackground(im, bctrl)
89 
90  im -= backobj.getImageF()
91  #
92  # Find sources
93  #
94  threshold = afwDetect.Threshold(5, afwDetect.Threshold.STDEV)
95  npixMin = 5 # we didn't smooth
96  fs = afwDetect.FootprintSet(mi, threshold, "DETECTED", npixMin)
97  grow, isotropic = 1, False
98  fs = afwDetect.FootprintSet(fs, grow, isotropic)
99  fs.setMask(mi.getMask(), "DETECTED")
100 
101  return mi, fs
102 
103 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
104 
105 def SpatialCellSetDemo(filename=None):
106  """A demonstration of the use of a SpatialCellSet"""
107 
108  im, fs = readImage(filename)
109 
110  if display:
111  ds9.mtv(im, frame=0, title="Input")
112  #
113  # Create an (empty) SpatialCellSet
114  #
115  cellSet = afwMath.SpatialCellSet(afwGeom.Box2I(afwGeom.Point2I(0, 0), im.getDimensions()),
116  260, 200)
117 
118  if display:
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()),
126  ], frame=0)
127  ds9.dot(cell.getLabel(),
128  (cell.getBBox().getMinX() + cell.getBBox().getMaxX())/2,
129  (cell.getBBox().getMinY() + cell.getBBox().getMaxY())/2)
130  #
131  # Populate cellSet
132  #
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)
139  #
140  # OK, the SpatialCellList is populated. Let's do something with it
141  #
142  visitor = testSpatialCellLib.ExampleCandidateVisitor()
143 
144  cellSet.visitCandidates(visitor)
145  print "There are %d candidates" % (visitor.getN())
146 
147  ctypes = ["red", "yellow", "cyan", ]
148  for i in range(cellSet.getCellList().size()):
149  cell = cellSet.getCellList()[i]
150  cell.visitCandidates(visitor)
151 
152  j = 0
153  for cand in cell:
154  #
155  # Swig doesn't know that we're a SpatialCellImageCandidate; all it knows is that we have
156  # a SpatialCellCandidate so we need an explicit (dynamic) cast
157  #
158  cand = testSpatialCellLib.cast_ExampleCandidate(cand)
159 
160  w, h = cand.getBBox().getDimensions()
161  if w*h < 75:
162  #print "%d %5.2f %5.2f %d" % (i, cand.getXCenter(), cand.getYCenter(), w*h)
163  cand.setStatus(afwMath.SpatialCellCandidate.BAD)
164 
165  if display:
166  ds9.dot("o", cand.getXCenter(), cand.getYCenter(), size=4, ctype=ctypes[i%len(ctypes)])
167  else:
168  if display:
169  ds9.dot("%s:%d" % (cand.getId(), j),
170  cand.getXCenter(), cand.getYCenter(), size=4, ctype=ctypes[i%len(ctypes)])
171  j += 1
172 
173  im = cand.getMaskedImage()
174  if 0 and display:
175  ds9.mtv(im, title="Candidate", frame=1)
176  #
177  # Now count the good and bad candidates
178  #
179  for i in range(len(cellSet.getCellList())):
180  cell = cellSet.getCellList()[i]
181  cell.visitCandidates(visitor)
182 
183  cell.setIgnoreBad(False) # include BAD in cell.size()
184  print "%s nobj=%d N_good=%d NPix_good=%d" % \
185  (cell.getLabel(), cell.size(), visitor.getN(), visitor.getNPix())
186 
187 
188  cellSet.setIgnoreBad(True) # don't visit BAD candidates
189  cellSet.visitCandidates(visitor)
190  print "There are %d good candidates" % (visitor.getN())
191 
192 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
193 
194 def run():
195  """Run the tests"""
196 
197  SpatialCellSetDemo()
198 
199 if __name__ == "__main__":
200  run()