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
starSelector.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 from __future__ import absolute_import, division, print_function
23 
24 import abc
25 
26 import numpy as np
27 
28 from lsst.afw.table import SourceCatalog, Schema
29 import lsst.afw.math as afwMath
30 import lsst.pex.config as pexConfig
31 import lsst.pipe.base as pipeBase
32 from . import algorithmsLib
33 from future.utils import with_metaclass
34 
35 __all__ = ["BaseStarSelectorConfig", "BaseStarSelectorTask", "starSelectorRegistry"]
36 
37 
38 class BaseStarSelectorConfig(pexConfig.Config):
39  kernelSize = pexConfig.Field(
40  doc="size of the kernel to create",
41  dtype=int,
42  default=21,
43  )
44  borderWidth = pexConfig.Field(
45  doc="number of pixels to ignore around the edge of PSF candidate postage stamps",
46  dtype=int,
47  default=0,
48  )
49  badFlags = pexConfig.ListField(
50  doc="List of flags which cause a source to be rejected as bad",
51  dtype=str,
52  default=[
53  "base_PixelFlags_flag_edge",
54  "base_PixelFlags_flag_interpolatedCenter",
55  "base_PixelFlags_flag_saturatedCenter",
56  "base_PixelFlags_flag_crCenter",
57  "base_PixelFlags_flag_bad",
58  "base_PixelFlags_flag_interpolated",
59  ],
60  )
61 
62 
63 class BaseStarSelectorTask(with_metaclass(abc.ABCMeta, pipeBase.Task)):
64  """!Base class for star selectors
65 
66  Register all star selectors with the starSelectorRegistry using:
67  starSelectorRegistry.register(name, class)
68  """
69 
70  usesMatches = False # Does the star selector use the "matches" argument in the "run method? Few do.
71  ConfigClass = BaseStarSelectorConfig
72  _DefaultName = "starSelector"
73 
74  def __init__(self, schema, **kwds):
75  # catch code that passed config positionally before schema argument was added
76  assert isinstance(schema, Schema)
77  pipeBase.Task.__init__(self, **kwds)
78 
79  def run(self, exposure, sourceCat, matches=None, isStarField=None):
80  """!Select stars, make PSF candidates, and set a flag field True for stars in the input catalog
81 
82  @param[in] exposure the exposure containing the sources
83  @param[in] sourceCat catalog of sources that may be stars (an lsst.afw.table.SourceCatalog)
84  @param[in] matches astrometric matches; ignored by this star selector
85  (an lsst.afw.table.ReferenceMatchVector), or None. Some star selectors
86  will ignore this argument, others may require it. See the usesMatches class variable.
87  @param[in] isStarField name of flag field to set True for stars, or None to not set a field;
88  the field is left unchanged for non-stars
89 
90  @return an lsst.pipe.base.Struct containing:
91  - starCat catalog of stars that were selected as stars and successfuly made into PSF candidates
92  (a subset of sourceCat whose records are shallow copies)
93  - psfCandidates list of PSF candidates (lsst.meas.algorithms.PsfCandidate)
94  """
95  selRes = self.selectStars(exposure=exposure, sourceCat=sourceCat, matches=matches)
96  psfRes = self.makePsfCandidates(exposure=exposure, starCat=selRes.starCat)
97 
98  if isStarField is not None:
99  isStarKey = sourceCat.schema[isStarField].asKey()
100  for star in psfRes.goodStarCat:
101  star.set(isStarKey, True)
102 
103  return pipeBase.Struct(
104  starCat=psfRes.goodStarCat,
105  psfCandidates=psfRes.psfCandidates,
106  )
107 
108  @abc.abstractmethod
109  def selectStars(self, exposure, sourceCat, matches=None):
110  """!Return a catalog of stars: a subset of sourceCat whose records are shallow copies
111 
112  @param[in] exposure the exposure containing the sources
113  @param[in] sourceCat catalog of sources that may be stars (an lsst.afw.table.SourceCatalog)
114  @param[in] matches astrometric matches; ignored by this star selector
115  (an lsst.afw.table.ReferenceMatchVector), or None. Some star selectors
116  will ignore this argument, others may require it. See the usesMatches class variable.
117 
118  @warning The returned catalog must have records that are shallow copies
119  (fortunately this is the default behavior when you add a record from one catalog to another);
120  otherwise the run method cannot set the isStarField flag in the original source catalog.
121 
122  @return a pipeBase.Struct containing:
123  - starCat a catalog of stars (a subset of sourceCat whose records are shallow copies)
124  """
125  raise NotImplementedError("BaseStarSelectorTask is abstract, subclasses must override this method")
126 
127  def makePsfCandidates(self, exposure, starCat):
128  """!Make a list of PSF candidates from a star catalog
129 
130  @param[in] exposure the exposure containing the sources
131  @param[in] starCat catalog of stars (an lsst.afw.table.SourceCatalog),
132  e.g. as returned by the run or selectStars method
133 
134  @return an lsst.pipe.base.Struct with fields:
135  - psfCandidates list of PSF candidates (lsst.meas.algorithms.PsfCandidate)
136  - goodStarCat catalog of stars that were successfully made into PSF candidates (a subset of starCat)
137  """
138  goodStarCat = SourceCatalog(starCat.schema)
139 
140  psfCandidateList = []
141  didSetSize = False
142  for star in starCat:
143  try:
144  psfCandidate = algorithmsLib.makePsfCandidate(star, exposure)
145 
146  # The setXXX methods are class static, but it's convenient to call them on
147  # an instance as we don't know Exposure's pixel type
148  # (and hence psfCandidate's exact type)
149  if not didSetSize:
150  psfCandidate.setBorderWidth(self.config.borderWidth)
151  psfCandidate.setWidth(self.config.kernelSize + 2*self.config.borderWidth)
152  psfCandidate.setHeight(self.config.kernelSize + 2*self.config.borderWidth)
153  didSetSize = True
154 
155  im = psfCandidate.getMaskedImage().getImage()
156  except Exception as err:
157  self.log.debug("Failed to make a psfCandidate from star %d: %s", star.getId(), err)
158  continue
159 
160  vmax = afwMath.makeStatistics(im, afwMath.MAX).getValue()
161  if not np.isfinite(vmax):
162  continue
163  psfCandidateList.append(psfCandidate)
164  goodStarCat.append(star)
165 
166  return pipeBase.Struct(
167  psfCandidates=psfCandidateList,
168  goodStarCat=goodStarCat,
169  )
170 
171 
172 starSelectorRegistry = pexConfig.makeRegistry(
173  doc="A registry of star selectors (subclasses of BaseStarSelectorTask)",
174 )
def run
Select stars, make PSF candidates, and set a flag field True for stars in the input catalog...
Definition: starSelector.py:79
def makePsfCandidates
Make a list of PSF candidates from a star catalog.
def selectStars
Return a catalog of stars: a subset of sourceCat whose records are shallow copies.
Statistics makeStatistics(afwImage::Mask< afwImage::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl)
Specialization to handle Masks.
Definition: Statistics.cc:1107
SortedCatalogT< SourceRecord > SourceCatalog
Definition: fwd.h:75