LSSTApplications  1.1.2+25,10.0+13,10.0+132,10.0+133,10.0+224,10.0+41,10.0+8,10.0-1-g0f53050+14,10.0-1-g4b7b172+19,10.0-1-g61a5bae+98,10.0-1-g7408a83+3,10.0-1-gc1e0f5a+19,10.0-1-gdb4482e+14,10.0-11-g3947115+2,10.0-12-g8719d8b+2,10.0-15-ga3f480f+1,10.0-2-g4f67435,10.0-2-gcb4bc6c+26,10.0-28-gf7f57a9+1,10.0-3-g1bbe32c+14,10.0-3-g5b46d21,10.0-4-g027f45f+5,10.0-4-g86f66b5+2,10.0-4-gc4fccf3+24,10.0-40-g4349866+2,10.0-5-g766159b,10.0-5-gca2295e+25,10.0-6-g462a451+1
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 eups
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  dataDir = eups.productDir("afwdata")
58  if not dataDir:
59  raise RuntimeError("You must provide a filename or setup afwdata to run these examples")
60 
61  filename = os.path.join(eups.productDir("afwdata"), "CFHT", "D4", "cal-53535-i-797722_1")
62 
63  bbox = afwGeom.Box2I(afwGeom.Point2I(270, 2530), afwGeom.Extent2I(512, 512))
64  else:
65  bbox = None
66 
67  mi = afwImage.MaskedImageF(filename, 0, None, bbox, afwImage.LOCAL)
68  mi.setXY0(afwGeom.Point2I(0, 0))
69  #
70  # Subtract the background. We'd use a canned procedure, but that's in meas/utils/sourceDetection.py. We
71  # can't fix those pesky cosmic rays either, as that's in a dependent product (meas/algorithms) too
72  #
73  bctrl = afwMath.BackgroundControl(afwMath.Interpolate.NATURAL_SPLINE)
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)
78  sctrl.setNumIter(2)
79 
80  im = mi.getImage()
81  try:
82  backobj = afwMath.makeBackground(im, bctrl)
83  except Exception, e:
84  print >> sys.stderr, e,
85 
86  bctrl.setInterpStyle(afwMath.Interpolate.CONSTANT)
87  backobj = afwMath.makeBackground(im, bctrl)
88 
89  im -= backobj.getImageF()
90  #
91  # Find sources
92  #
93  threshold = afwDetect.Threshold(5, afwDetect.Threshold.STDEV)
94  npixMin = 5 # we didn't smooth
95  fs = afwDetect.FootprintSet(mi, threshold, "DETECTED", npixMin)
96  grow, isotropic = 1, False
97  fs = afwDetect.FootprintSet(fs, grow, isotropic)
98  fs.setMask(mi.getMask(), "DETECTED")
99 
100  return mi, fs
101 
102 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
103 
104 def SpatialCellSetDemo(filename=None):
105  """A demonstration of the use of a SpatialCellSet"""
106 
107  im, fs = readImage(filename)
108 
109  if display:
110  ds9.mtv(im, frame=0, title="Input")
111  #
112  # Create an (empty) SpatialCellSet
113  #
114  cellSet = afwMath.SpatialCellSet(afwGeom.Box2I(afwGeom.Point2I(0, 0), im.getDimensions()),
115  260, 200)
116 
117  if display:
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()),
125  ], frame=0)
126  ds9.dot(cell.getLabel(),
127  (cell.getBBox().getMinX() + cell.getBBox().getMaxX())/2,
128  (cell.getBBox().getMinY() + cell.getBBox().getMaxY())/2)
129  #
130  # Populate cellSet
131  #
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)
138  #
139  # OK, the SpatialCellList is populated. Let's do something with it
140  #
141  visitor = testSpatialCellLib.ExampleCandidateVisitor()
142 
143  cellSet.visitCandidates(visitor)
144  print "There are %d candidates" % (visitor.getN())
145 
146  ctypes = ["red", "yellow", "cyan", ]
147  for i in range(cellSet.getCellList().size()):
148  cell = cellSet.getCellList()[i]
149  cell.visitCandidates(visitor)
150 
151  j = 0
152  for cand in cell:
153  #
154  # Swig doesn't know that we're a SpatialCellImageCandidate; all it knows is that we have
155  # a SpatialCellCandidate so we need an explicit (dynamic) cast
156  #
157  cand = testSpatialCellLib.cast_ExampleCandidate(cand)
158 
159  w, h = cand.getBBox().getDimensions()
160  if w*h < 75:
161  #print "%d %5.2f %5.2f %d" % (i, cand.getXCenter(), cand.getYCenter(), w*h)
162  cand.setStatus(afwMath.SpatialCellCandidate.BAD)
163 
164  if display:
165  ds9.dot("o", cand.getXCenter(), cand.getYCenter(), size=4, ctype=ctypes[i%len(ctypes)])
166  else:
167  if display:
168  ds9.dot("%s:%d" % (cand.getId(), j),
169  cand.getXCenter(), cand.getYCenter(), size=4, ctype=ctypes[i%len(ctypes)])
170  j += 1
171 
172  im = cand.getMaskedImage()
173  if 0 and display:
174  ds9.mtv(im, title="Candidate", frame=1)
175  #
176  # Now count the good and bad candidates
177  #
178  for i in range(len(cellSet.getCellList())):
179  cell = cellSet.getCellList()[i]
180  cell.visitCandidates(visitor)
181 
182  cell.setIgnoreBad(False) # include BAD in cell.size()
183  print "%s nobj=%d N_good=%d NPix_good=%d" % \
184  (cell.getLabel(), cell.size(), visitor.getN(), visitor.getNPix())
185 
186 
187  cellSet.setIgnoreBad(True) # don't visit BAD candidates
188  cellSet.visitCandidates(visitor)
189  print "There are %d good candidates" % (visitor.getN())
190 
191 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
192 
193 def run():
194  """Run the tests"""
195 
196  SpatialCellSetDemo()
197 
198 if __name__ == "__main__":
199  run()