lsst.meas.astrom  15.0-3-g52118bc
catalogStarSelector.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 
23 __all__ = ["CatalogStarSelectorConfig", "CatalogStarSelectorTask"]
24 
25 
26 from lsst.afw.table import SourceCatalog
27 from lsst.meas.algorithms import BaseStarSelectorTask, starSelectorRegistry
28 from lsst.pipe.base import Struct
29 import lsst.pex.config as pexConfig
30 import lsst.afw.display.ds9 as ds9
31 
32 
33 class CatalogStarSelectorConfig(BaseStarSelectorTask.ConfigClass):
34  fluxLim = pexConfig.RangeField(
35  doc="specify the minimum psfFlux for good Psf Candidates",
36  dtype=float,
37  default=0.0,
38  min=0.0,
39  )
40  fluxMax = pexConfig.RangeField(
41  doc="specify the maximum psfFlux for good Psf Candidates (ignored if == 0)",
42  dtype=float,
43  default=0.0,
44  min=0.0,
45  )
46 
47  def setDefaults(self):
48  BaseStarSelectorTask.ConfigClass.setDefaults(self)
49  self.badFlags = [
50  "base_PixelFlags_flag_edge",
51  "base_PixelFlags_flag_interpolatedCenter",
52  "base_PixelFlags_flag_saturatedCenter",
53  ]
54 
55 
57  """A functor to check whether a source has any flags set that should cause it to be labeled bad."""
58 
59  def __init__(self, table, fluxLim, fluxMax, badFlags):
60  self.keys = [table.getSchema().find(name).key for name in badFlags]
61  self.keys.append(table.getCentroidFlagKey())
62  self.fluxLim = fluxLim
63  self.fluxMax = fluxMax
64 
65  def __call__(self, source):
66  for k in self.keys:
67  if source.get(k):
68  return False
69  if self.fluxLim is not None and source.getPsfFlux() < self.fluxLim: # ignore faint objects
70  return False
71  if self.fluxMax != 0.0 and source.getPsfFlux() > self.fluxMax: # ignore bright objects
72  return False
73  return True
74 
75 # \addtogroup LSST_task_documentation
76 # \{
77 # \page CatalogStarSelectorTask
78 # \ref CatalogStarSelectorTask_ "CatalogStarSelectorTask"
79 # \copybrief CatalogStarSelectorTask
80 # \}
81 
82 
84  """!Select stars based on a reference catalog
85 
86  @anchor CatalogStarSelectorTask_
87 
88  @section meas_astrom_catalogStarSelector_Contents Contents
89 
90  - @ref meas_astrom_catalogStarSelector_Purpose
91  - @ref meas_astrom_catalogStarSelector_Initialize
92  - @ref meas_astrom_catalogStarSelector_IO
93  - @ref meas_astrom_catalogStarSelector_Config
94  - @ref meas_astrom_catalogStarSelector_Debug
95 
96  @section meas_astrom_catalogStarSelector_Purpose Description
97 
98  Select stars using a match list: select sources where the matching reference object is unresolved,
99  plus the source passes the following tests:
100  - no flag from config.badFlags is set
101  - psf flux >= config.fluxLim
102  - psf flux <= config.fluxMax (not checked if fluxMax == 0)
103 
104  @section meas_astrom_catalogStarSelector_Initialize Task initialisation
105 
106  @copydoc \_\_init\_\_
107 
108  @section meas_astrom_catalogStarSelector_IO Invoking the Task
109 
110  Like all star selectors, the main method is `run`. Unlike most star selectors,
111  this one requires the `matches` argument (the `usesMatches` property is true).
112 
113  @section meas_astrom_catalogStarSelector_Config Configuration parameters
114 
115  See @ref CatalogStarSelectorConfig
116 
117  @section meas_astrom_catalogStarSelector_Debug Debug variables
118 
119  CatalogStarSelectorTask has a debug dictionary with the following keys:
120  <dl>
121  <dt>display
122  <dd>bool; if True display debug information
123  <dt>pauseAtEnd
124  <dd>bool; if True wait after displaying everything and wait for user input
125  </dl>
126 
127  For example, put something like:
128  @code{.py}
129  import lsstDebug
130  def DebugInfo(name):
131  di = lsstDebug.getInfo(name) # N.b. lsstDebug.Info(name) would call us recursively
132  if name.endswith("catalogStarSelector"):
133  di.display = True
134 
135  return di
136 
137  lsstDebug.Info = DebugInfo
138  @endcode
139  into your `debug.py` file and run your task with the `--debug` flag.
140  """
141  ConfigClass = CatalogStarSelectorConfig
142  usesMatches = True # `run` and `selectStars` require the `matches` argument
143 
144  def selectStars(self, exposure, sourceCat, matches=None):
145  """!Return a list of PSF candidates that represent likely stars
146 
147  A list of PSF candidates may be used by a PSF fitter to construct a PSF.
148 
149  @param[in] exposure the exposure containing the sources
150  @param[in] sourceCat catalog of sources that may be stars (an lsst.afw.table.SourceCatalog)
151  @param[in] matches a match vector as produced by meas_astrom; required
152  (defaults to None to match the StarSelector API and improve error handling)
153 
154  @return an lsst.pipe.base.Struct containing:
155  - starCat catalog of selected stars (a subset of sourceCat)
156  """
157  import lsstDebug
158  debugInfo = lsstDebug.Info(__name__)
159  display = debugInfo.display
160  pauseAtEnd = debugInfo.pauseAtEnd # pause when done
161 
162  if matches is None:
163  raise RuntimeError("CatalogStarSelectorTask requires matches")
164 
165  mi = exposure.getMaskedImage()
166 
167  if display:
168  frame = 1
169  ds9.mtv(mi, frame=frame, title="PSF candidates")
170 
171  isGoodSource = CheckSource(sourceCat, self.config.fluxLim, self.config.fluxMax, self.config.badFlags)
172 
173  starCat = SourceCatalog(sourceCat.schema)
174  with ds9.Buffering():
175  for ref, source, d in matches:
176  if not ref.get("resolved"):
177  if not isGoodSource(source):
178  symb, ctype = "+", ds9.RED
179  else:
180  starCat.append(source)
181  symb, ctype = "+", ds9.GREEN
182 
183  if display:
184  ds9.dot(symb, source.getX() - mi.getX0(), source.getY() - mi.getY0(),
185  size=4, frame=frame, ctype=ctype)
186 
187  if display and pauseAtEnd:
188  input("Continue? y[es] p[db] ")
189 
190  return Struct(
191  starCat=starCat,
192  )
193 
194 
195 starSelectorRegistry.register("catalog", CatalogStarSelectorTask)
def selectStars(self, exposure, sourceCat, matches=None)
Return a list of PSF candidates that represent likely stars.
def __init__(self, table, fluxLim, fluxMax, badFlags)