LSSTApplications  19.0.0-14-gb0260a2+72efe9b372,20.0.0+7927753e06,20.0.0+8829bf0056,20.0.0+995114c5d2,20.0.0+b6f4b2abd1,20.0.0+bddc4f4cbe,20.0.0-1-g253301a+8829bf0056,20.0.0-1-g2b7511a+0d71a2d77f,20.0.0-1-g5b95a8c+7461dd0434,20.0.0-12-g321c96ea+23efe4bbff,20.0.0-16-gfab17e72e+fdf35455f6,20.0.0-2-g0070d88+ba3ffc8f0b,20.0.0-2-g4dae9ad+ee58a624b3,20.0.0-2-g61b8584+5d3db074ba,20.0.0-2-gb780d76+d529cf1a41,20.0.0-2-ged6426c+226a441f5f,20.0.0-2-gf072044+8829bf0056,20.0.0-2-gf1f7952+ee58a624b3,20.0.0-20-geae50cf+e37fec0aee,20.0.0-25-g3dcad98+544a109665,20.0.0-25-g5eafb0f+ee58a624b3,20.0.0-27-g64178ef+f1f297b00a,20.0.0-3-g4cc78c6+e0676b0dc8,20.0.0-3-g8f21e14+4fd2c12c9a,20.0.0-3-gbd60e8c+187b78b4b8,20.0.0-3-gbecbe05+48431fa087,20.0.0-38-ge4adf513+a12e1f8e37,20.0.0-4-g97dc21a+544a109665,20.0.0-4-gb4befbc+087873070b,20.0.0-4-gf910f65+5d3db074ba,20.0.0-5-gdfe0fee+199202a608,20.0.0-5-gfbfe500+d529cf1a41,20.0.0-6-g64f541c+d529cf1a41,20.0.0-6-g9a5b7a1+a1cd37312e,20.0.0-68-ga3f3dda+5fca18c6a4,20.0.0-9-g4aef684+e18322736b,w.2020.45
LSSTDataManagementBasePackage
setPrimaryFlags.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # LSST Data Management System
4 # Copyright 2008-2016 LSST/AURA
5 #
6 # This product includes software developed by the
7 # LSST Project (http://www.lsst.org/).
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the LSST License Statement and
20 # the GNU General Public License along with this program. If not,
21 # see <http://www.lsstcorp.org/LegalNotices/>.
22 #
23 import numpy
24 from lsst.pex.config import Config, Field, ListField
25 from lsst.pipe.base import Task
26 from lsst.geom import Box2D
27 
28 
30  nChildKeyName = Field(dtype=str, default="deblend_nChild",
31  doc="Name of field in schema with number of deblended children")
32  pseudoFilterList = ListField(dtype=str, default=['sky'],
33  doc="Names of filters which should never be primary")
34 
35 
37  """Add isPrimaryKey to a given schema.
38 
39  Parameters
40  ----------
41  schema : `lsst.afw.table.Schema`
42  The input schema.
43  isSingleFrame : `bool`
44  Flag specifying if task is operating with single frame imaging.
45  kwargs :
46  Keyword arguments passed to the task.
47  """
48 
49  ConfigClass = SetPrimaryFlagsConfig
50 
51  def __init__(self, schema, isSingleFrame=False, **kwargs):
52  Task.__init__(self, **kwargs)
53  self.schema = schema
54  self.isSingleFrame = isSingleFrame
55  if not self.isSingleFrame:
56  primaryDoc = ("true if source has no children and is in the inner region of a coadd patch "
57  "and is in the inner region of a coadd tract "
58  "and is not \"detected\" in a pseudo-filter (see config.pseudoFilterList)")
59  self.isPatchInnerKey = self.schema.addField(
60  "detect_isPatchInner", type="Flag",
61  doc="true if source is in the inner region of a coadd patch",
62  )
63  self.isTractInnerKey = self.schema.addField(
64  "detect_isTractInner", type="Flag",
65  doc="true if source is in the inner region of a coadd tract",
66  )
67  else:
68  primaryDoc = "true if source has no children and is not a sky source"
69  self.isPrimaryKey = self.schema.addField(
70  "detect_isPrimary", type="Flag",
71  doc=primaryDoc,
72  )
73 
74  def run(self, sources, skyMap=None, tractInfo=None, patchInfo=None,
75  includeDeblend=True):
76  """Set is-patch-inner, is-tract-inner and is-primary flags on sources.
77  For coadded imaging, the is-primary flag returns True when an object
78  has no children, is in the inner region of a coadd patch, is in the
79  inner region of a coadd trach, and is not detected in a pseudo-filter
80  (e.g., a sky_object).
81  For single frame imaging, the is-primary flag returns True when a
82  source has no children and is not a sky source.
83 
84  Parameters
85  ----------
86  sources : `lsst.afw.table.SourceCatalog`
87  A sourceTable. Reads in centroid fields and an nChild field.
88  Writes is-patch-inner, is-tract-inner, and is-primary flags.
89  skyMap : `lsst.skymap.BaseSkyMap`
90  Sky tessellation object
91  tractInfo : `lsst.skymap.TractInfo`
92  Tract object
93  patchInfo : `lsst.skymap.PatchInfo`
94  Patch object
95  includeDeblend : `bool`
96  Include deblend information in isPrimary?
97  """
98  nChildKey = None
99  if includeDeblend:
100  nChildKey = self.schema.find(self.config.nChildKeyName).key
101 
102  # coadd case
103  if not self.isSingleFrame:
104  # set inner flags for each source and set primary flags for sources with no children
105  # (or all sources if deblend info not available)
106  innerFloatBBox = Box2D(patchInfo.getInnerBBox())
107 
108  # When the centroider fails, we can still fall back to the peak, but we don't trust
109  # that quite as much - so we use a slightly smaller box for the patch comparison.
110  # That's trickier for the tract comparison, so we just use the peak without extra
111  # care there.
112  shrunkInnerFloatBBox = Box2D(innerFloatBBox)
113  shrunkInnerFloatBBox.grow(-1)
114 
115  pseudoFilterKeys = []
116  for filt in self.config.pseudoFilterList:
117  try:
118  pseudoFilterKeys.append(self.schema.find("merge_peak_%s" % filt).getKey())
119  except Exception:
120  self.log.warn("merge_peak is not set for pseudo-filter %s" % filt)
121 
122  tractId = tractInfo.getId()
123  for source in sources:
124  centroidPos = source.getCentroid()
125  if numpy.any(numpy.isnan(centroidPos)):
126  continue
127  if source.getCentroidFlag():
128  # Use a slightly smaller box to guard against bad centroids (see above)
129  isPatchInner = shrunkInnerFloatBBox.contains(centroidPos)
130  else:
131  isPatchInner = innerFloatBBox.contains(centroidPos)
132  source.setFlag(self.isPatchInnerKey, isPatchInner)
133 
134  skyPos = source.getCoord()
135  sourceInnerTractId = skyMap.findTract(skyPos).getId()
136  isTractInner = sourceInnerTractId == tractId
137  source.setFlag(self.isTractInnerKey, isTractInner)
138 
139  if nChildKey is None or source.get(nChildKey) == 0:
140  for pseudoFilterKey in pseudoFilterKeys:
141  if source.get(pseudoFilterKey):
142  isPseudo = True
143  break
144  else:
145  isPseudo = False
146 
147  source.setFlag(self.isPrimaryKey, isPatchInner and isTractInner and not isPseudo)
148 
149  # single frame case
150  else:
151  hasSkySources = True if "sky_source" in sources.schema else False
152  for source in sources:
153  hasNoChildren = True if nChildKey is None or source.get(nChildKey) == 0 else False
154  isSkySource = False
155  if hasSkySources:
156  if source["sky_source"]:
157  isSkySource = True
158  source.setFlag(self.isPrimaryKey, hasNoChildren and not isSkySource)
lsst::log.log.logContinued.warn
def warn(fmt, *args)
Definition: logContinued.py:205
lsst.pipe.tasks.setPrimaryFlags.SetPrimaryFlagsTask.isPrimaryKey
isPrimaryKey
Definition: setPrimaryFlags.py:69
lsst.pex.config.listField.ListField
Definition: listField.py:216
lsst.pipe.tasks.setPrimaryFlags.SetPrimaryFlagsTask.isPatchInnerKey
isPatchInnerKey
Definition: setPrimaryFlags.py:59
lsst.pipe.tasks.setPrimaryFlags.SetPrimaryFlagsTask.run
def run(self, sources, skyMap=None, tractInfo=None, patchInfo=None, includeDeblend=True)
Definition: setPrimaryFlags.py:74
lsst.pipe.tasks.setPrimaryFlags.SetPrimaryFlagsTask.schema
schema
Definition: setPrimaryFlags.py:53
lsst.pipe.tasks.setPrimaryFlags.SetPrimaryFlagsTask.isTractInnerKey
isTractInnerKey
Definition: setPrimaryFlags.py:63
lsst.pex.config
Definition: __init__.py:1
lsst.pipe.base.task.Task.config
config
Definition: task.py:162
lsst.pipe.base.task.Task.log
log
Definition: task.py:161
lsst.pipe.tasks.setPrimaryFlags.SetPrimaryFlagsTask
Definition: setPrimaryFlags.py:36
lsst.pipe.tasks.setPrimaryFlags.SetPrimaryFlagsTask.isSingleFrame
isSingleFrame
Definition: setPrimaryFlags.py:54
lsst::geom
Definition: AffineTransform.h:36
lsst.pipe.base.task.Task
Definition: task.py:47
lsst.pex.config.config.Config
Definition: config.py:736
lsst.pipe.tasks.setPrimaryFlags.SetPrimaryFlagsTask.__init__
def __init__(self, schema, isSingleFrame=False, **kwargs)
Definition: setPrimaryFlags.py:51
lsst.pipe.tasks.setPrimaryFlags.SetPrimaryFlagsConfig
Definition: setPrimaryFlags.py:29
lsst.pex.config.config.Field
Definition: config.py:247
lsst::geom::Box2D
A floating-point coordinate rectangle geometry.
Definition: Box.h:413
lsst.pipe.base
Definition: __init__.py:1