LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
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 from __future__ import print_function
35 from __future__ import division
36 from builtins import range
37 
38 import os
39 import sys
40 
41 import lsst.utils
42 import lsst.afw.detection as afwDetect
43 import lsst.afw.image.imageLib as afwImage
44 import lsst.afw.math.mathLib as afwMath
45 import lsst.afw.geom as afwGeom
46 import lsst.afw.display.ds9 as ds9
47 
48 import testSpatialCellLib
49 
50 try:
51  type(display)
52 except NameError:
53  display = False
54 
55 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
56 
57 def readImage(filename=None):
58  """Read an image and background subtract it"""
59  if not filename:
60  try:
61  afwDataDir = lsst.utils.getPackageDir("afwdata")
62  except Exception:
63  raise RuntimeError("You must provide a filename or setup afwdata to run these examples")
64 
65  filename = os.path.join(afwDataDir, "CFHT", "D4", "cal-53535-i-797722_1")
66 
67  bbox = afwGeom.Box2I(afwGeom.Point2I(270, 2530), afwGeom.Extent2I(512, 512))
68  else:
69  bbox = None
70 
71  mi = afwImage.MaskedImageF(filename, 0, None, bbox, afwImage.LOCAL)
72  mi.setXY0(afwGeom.Point2I(0, 0))
73  #
74  # Subtract the background. We'd use a canned procedure, but that's in meas/utils/sourceDetection.py. We
75  # can't fix those pesky cosmic rays either, as that's in a dependent product (meas/algorithms) too
76  #
77  bctrl = afwMath.BackgroundControl(afwMath.Interpolate.NATURAL_SPLINE)
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)
82  sctrl.setNumIter(2)
83 
84  im = mi.getImage()
85  try:
86  backobj = afwMath.makeBackground(im, bctrl)
87  except Exception as e:
88  print(e, end=' ', file=sys.stderr)
89 
90  bctrl.setInterpStyle(afwMath.Interpolate.CONSTANT)
91  backobj = afwMath.makeBackground(im, bctrl)
92 
93  im -= backobj.getImageF()
94  #
95  # Find sources
96  #
97  threshold = afwDetect.Threshold(5, afwDetect.Threshold.STDEV)
98  npixMin = 5 # we didn't smooth
99  fs = afwDetect.FootprintSet(mi, threshold, "DETECTED", npixMin)
100  grow, isotropic = 1, False
101  fs = afwDetect.FootprintSet(fs, grow, isotropic)
102  fs.setMask(mi.getMask(), "DETECTED")
103 
104  return mi, fs
105 
106 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
107 
108 def SpatialCellSetDemo(filename=None):
109  """A demonstration of the use of a SpatialCellSet"""
110 
111  im, fs = readImage(filename)
112 
113  if display:
114  ds9.mtv(im, frame=0, title="Input")
115  #
116  # Create an (empty) SpatialCellSet
117  #
118  cellSet = afwMath.SpatialCellSet(afwGeom.Box2I(afwGeom.Point2I(0, 0), im.getDimensions()),
119  260, 200)
120 
121  if display:
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()),
129  ], frame=0)
130  ds9.dot(cell.getLabel(),
131  (cell.getBBox().getMinX() + cell.getBBox().getMaxX())/2,
132  (cell.getBBox().getMinY() + cell.getBBox().getMaxY())/2)
133  #
134  # Populate cellSet
135  #
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)
142  #
143  # OK, the SpatialCellList is populated. Let's do something with it
144  #
145  visitor = testSpatialCellLib.ExampleCandidateVisitor()
146 
147  cellSet.visitCandidates(visitor)
148  print("There are %d candidates" % (visitor.getN()))
149 
150  ctypes = ["red", "yellow", "cyan", ]
151  for i in range(cellSet.getCellList().size()):
152  cell = cellSet.getCellList()[i]
153  cell.visitCandidates(visitor)
154 
155  j = 0
156  for cand in cell:
157  #
158  # Swig doesn't know that we're a SpatialCellImageCandidate; all it knows is that we have
159  # a SpatialCellCandidate so we need an explicit (dynamic) cast
160  #
161  cand = testSpatialCellLib.cast_ExampleCandidate(cand)
162 
163  w, h = cand.getBBox().getDimensions()
164  if w*h < 75:
165  #print "%d %5.2f %5.2f %d" % (i, cand.getXCenter(), cand.getYCenter(), w*h)
166  cand.setStatus(afwMath.SpatialCellCandidate.BAD)
167 
168  if display:
169  ds9.dot("o", cand.getXCenter(), cand.getYCenter(), size=4, ctype=ctypes[i%len(ctypes)])
170  else:
171  if display:
172  ds9.dot("%s:%d" % (cand.getId(), j),
173  cand.getXCenter(), cand.getYCenter(), size=4, ctype=ctypes[i%len(ctypes)])
174  j += 1
175 
176  im = cand.getMaskedImage()
177  if 0 and display:
178  ds9.mtv(im, title="Candidate", frame=1)
179  #
180  # Now count the good and bad candidates
181  #
182  for i in range(len(cellSet.getCellList())):
183  cell = cellSet.getCellList()[i]
184  cell.visitCandidates(visitor)
185 
186  cell.setIgnoreBad(False) # include BAD in cell.size()
187  print("%s nobj=%d N_good=%d NPix_good=%d" % \
188  (cell.getLabel(), cell.size(), visitor.getN(), visitor.getNPix()))
189 
190 
191  cellSet.setIgnoreBad(True) # don't visit BAD candidates
192  cellSet.visitCandidates(visitor)
193  print("There are %d good candidates" % (visitor.getN()))
194 
195 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
196 
197 def run():
198  """Run the tests"""
199 
200  SpatialCellSetDemo()
201 
202 if __name__ == "__main__":
203  run()