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
diaCatalogSourceSelector.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2008, 2009, 2010 LSST Corporation.
4 #
5 # This product includes software developed by the
6 # LSST Project (http://www.lsst.org/).
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the LSST License Statement and
19 # the GNU General Public License along with this program. If not,
20 # see <http://www.lsstcorp.org/LegalNotices/>.
21 #
22 import numpy as np
23 import lsst.pex.config as pexConfig
24 import lsst.afw.display.ds9 as ds9
25 import lsst.meas.algorithms as measAlg
26 import lsst.pex.logging as pexLog
27 
28 class DiaCatalogSourceSelectorConfig(pexConfig.Config):
29  # Selection cuts on the input source catalog
30  fluxLim = pexConfig.Field(
31  doc = "specify the minimum psfFlux for good Kernel Candidates",
32  dtype = float,
33  default = 0.0,
34  check = lambda x: x >= 0.0,
35  )
36  fluxMax = pexConfig.Field(
37  doc = "specify the maximum psfFlux for good Kernel Candidates (ignored if == 0)",
38  dtype = float,
39  default = 0.0,
40  check = lambda x: x >= 0.0,
41  )
42  badPixelFlags = pexConfig.ListField(
43  doc = "Kernel candidate objects may not have any of these bits set",
44  dtype = str,
45  default = ["base_PixelFlags_edge", "base_PixelFlags_interpolatedCenter",
46  "base_PixelFlags_saturatedCenter", "slot_Centroid_flag"],
47  )
48  # Selection cuts on the reference catalog
49  selectStar = pexConfig.Field(
50  doc = "Select objects that are flagged as stars",
51  dtype = bool,
52  default = True
53  )
54  selectGalaxy = pexConfig.Field(
55  doc = "Select objects that are flagged as galaxies",
56  dtype = bool,
57  default = False
58  )
59  includeVariable = pexConfig.Field(
60  doc = "Include objects that are known to be variable",
61  dtype = bool,
62  default = False
63  )
64  grMin = pexConfig.Field(
65  doc = "Minimum g-r color for selection (inclusive)",
66  dtype = float,
67  default = 0.0
68  )
69  grMax = pexConfig.Field(
70  doc = "Maximum g-r color for selection (inclusive)",
71  dtype = float,
72  default = 3.0
73  )
74 
75 class CheckSource(object):
76  """A functor to check whether a source has any flags set that should cause it to be labeled bad."""
77 
78  def __init__(self, table, fluxLim, fluxMax, badPixelFlags):
79  self.keys = [table.getSchema().find(name).key for name in badPixelFlags]
80  self.fluxLim = fluxLim
81  self.fluxMax = fluxMax
82 
83  def __call__(self, source):
84  for k in self.keys:
85  if source.get(k):
86  return False
87  if self.fluxLim != None and source.getPsfFlux() < self.fluxLim: # ignore faint objects
88  return False
89  if self.fluxMax != 0.0 and source.getPsfFlux() > self.fluxMax: # ignore bright objects
90  return False
91  return True
92 
94  ConfigClass = DiaCatalogSourceSelectorConfig
95 
96  def __init__(self, config=None):
97  """Construct a source selector that uses a reference catalog
98 
99  @param[in] config: An instance of ConfigClass
100  """
101  if not config:
102  config = DiaCatalogSourceSelector.ConfigClass()
103  self.config = config
104  self.log = pexLog.Log(pexLog.Log.getDefaultLog(),
105  'lsst.ip.diffim.DiaCatalogSourceSelector', pexLog.Log.INFO)
106 
107  def selectSources(self, exposure, sources, matches=None):
108  """Return a list of Sources for Kernel candidates
109 
110  @param[in] exposure: the exposure containing the sources
111  @param[in] sources: a source list containing sources that may be candidates
112  @param[in] matches: a match vector as produced by meas_astrom; not optional
113  (passing None just allows us to handle the exception better here
114  than in calling code)
115 
116  @return kernelCandidateSourceList: a list of sources to be used as kernel candidates
117 
118  """
119  import lsstDebug
120  display = lsstDebug.Info(__name__).display
121  displayExposure = lsstDebug.Info(__name__).displayExposure
122  pauseAtEnd = lsstDebug.Info(__name__).pauseAtEnd
123 
124  if matches is None:
125  raise RuntimeError(
126  "Cannot use catalog source selector without running astrometry."
127  )
128 
129  mi = exposure.getMaskedImage()
130 
131  if display:
132  if displayExposure:
133  ds9.mtv(mi, title="Kernel candidates", frame=lsstDebug.frame)
134  #
135  # Look for flags in each Source
136  #
137  isGoodSource = CheckSource(sources, self.config.fluxLim, self.config.fluxMax, self.config.badPixelFlags)
138 
139  #
140  # Go through and find all the acceptable candidates in the catalogue
141  #
142  kernelCandidateSourceList = []
143 
144  doColorCut = True
145  with ds9.Buffering():
146  refSchema = matches[0][0].schema
147  rRefFluxField = measAlg.getRefFluxField(refSchema, "r")
148  gRefFluxField = measAlg.getRefFluxField(refSchema, "g")
149  for ref, source, d in matches:
150  if not isGoodSource(source):
151  symb, ctype = "+", ds9.RED
152  else:
153  isStar = not ref.get("resolved")
154  isVar = not ref.get("photometric")
155  gMag = None
156  rMag = None
157  if doColorCut:
158  try:
159  gMag = -2.5 * np.log10(ref.get(gRefFluxField))
160  rMag = -2.5 * np.log10(ref.get(rRefFluxField))
161  except KeyError:
162  self.log.warn("Cannot cut on color info; fields 'g' and 'r' do not exist")
163  doColorCut = False
164  isRightColor = True
165  else:
166  isRightColor = (gMag-rMag) >= self.config.grMin and (gMag-rMag) <= self.config.grMax
167 
168  isRightType = (self.config.selectStar and isStar) or (self.config.selectGalaxy and not isStar)
169  isRightVar = (self.config.includeVariable) or (self.config.includeVariable is isVar)
170  if isRightType and isRightVar and isRightColor:
171  kernelCandidateSourceList.append(source)
172  symb, ctype = "+", ds9.GREEN
173  else:
174  symb, ctype = "o", ds9.BLUE
175 
176  if display and displayExposure:
177  ds9.dot(symb, source.getX() - mi.getX0(), source.getY() - mi.getY0(),
178  size=4, ctype=ctype, frame=lsstDebug.frame)
179 
180  if display:
181  lsstDebug.frame += 1
182  if pauseAtEnd:
183  raw_input("Continue? y[es] p[db] ")
184 
185  return kernelCandidateSourceList
186 
187 measAlg.starSelectorRegistry.register("diacatalog", DiaCatalogSourceSelector)
188 
a place to record messages and descriptions of the state of processing.
Definition: Log.h:154