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
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 
24 import numpy
25 from lsst.pex.config import Config, Field, ListField
26 from lsst.pipe.base import Task
27 from lsst.afw.geom import Box2D
28 
29 
30 class SetPrimaryFlagsConfig(Config):
31  nChildKeyName = Field(dtype=str, default="deblend_nChild",
32  doc="Name of field in schema with number of deblended children")
33  pseudoFilterList = ListField(dtype=str, default=['sky'],
34  doc="Names of filters which should never be primary")
35 
36 
37 class SetPrimaryFlagsTask(Task):
38  ConfigClass = SetPrimaryFlagsConfig
39 
40  def __init__(self, schema, **kwargs):
41  Task.__init__(self, **kwargs)
42  self.schema = schema
43  self.isPatchInnerKey = self.schema.addField(
44  "detect_isPatchInner", type="Flag",
45  doc="true if source is in the inner region of a coadd patch",
46  )
47  self.isTractInnerKey = self.schema.addField(
48  "detect_isTractInner", type="Flag",
49  doc="true if source is in the inner region of a coadd tract",
50  )
51  self.isPrimaryKey = self.schema.addField(
52  "detect_isPrimary", type="Flag",
53  doc="true if source has no children and is in the inner region of a coadd patch "
54  + "and is in the inner region of a coadd tract "
55  "and is not \"detected\" in a pseudo-filter (see config.pseudoFilterList)",
56  )
57 
58  def run(self, sources, skyMap, tractInfo, patchInfo, includeDeblend=True):
59  """Set is-primary and related flags on sources
60 
61  @param[in,out] sources a SourceTable
62  - reads centroid fields and an nChild field
63  - writes is-patch-inner, is-tract-inner and is-primary flags
64  @param[in] skyMap sky tessellation object (subclass of lsst.skymap.BaseSkyMap)
65  @param[in] tractInfo tract object (subclass of lsst.skymap.TractInfo)
66  @param[in] patchInfo patch object (subclass of lsst.skymap.PatchInfo)
67  @param[in] includeDeblend include deblend information in isPrimary?
68  """
69  nChildKey = None
70  if includeDeblend:
71  nChildKey = self.schema.find(self.config.nChildKeyName).key
72 
73  # set inner flags for each source and set primary flags for sources with no children
74  # (or all sources if deblend info not available)
75  innerFloatBBox = Box2D(patchInfo.getInnerBBox())
76 
77  # When the centroider fails, we can still fall back to the peak, but we don't trust
78  # that quite as much - so we use a slightly smaller box for the patch comparison.
79  # That's trickier for the tract comparison, so we just use the peak without extra
80  # care there.
81  shrunkInnerFloatBBox = Box2D(innerFloatBBox)
82  shrunkInnerFloatBBox.grow(-1)
83 
84  pseudoFilterKeys = []
85  for filt in self.config.pseudoFilterList:
86  try:
87  pseudoFilterKeys.append(self.schema.find("merge_peak_%s" % filt).getKey())
88  except Exception:
89  self.log.warn("merge_peak is not set for pseudo-filter %s" % filt)
90 
91  tractId = tractInfo.getId()
92  for source in sources:
93  centroidPos = source.getCentroid()
94  if numpy.any(numpy.isnan(centroidPos)):
95  continue
96  if source.getCentroidFlag():
97  # Use a slightly smaller box to guard against bad centroids (see above)
98  isPatchInner = shrunkInnerFloatBBox.contains(centroidPos)
99  else:
100  isPatchInner = innerFloatBBox.contains(centroidPos)
101  source.setFlag(self.isPatchInnerKey, isPatchInner)
102 
103  skyPos = source.getCoord()
104  sourceInnerTractId = skyMap.findTract(skyPos).getId()
105  isTractInner = sourceInnerTractId == tractId
106  source.setFlag(self.isTractInnerKey, isTractInner)
107 
108  if nChildKey is None or source.get(nChildKey) == 0:
109  for pseudoFilterKey in pseudoFilterKeys:
110  if source.get(pseudoFilterKey):
111  isPseudo = True
112  break
113  else:
114  isPseudo = False
115 
116  source.setFlag(self.isPrimaryKey, isPatchInner and isTractInner and not isPseudo)
A floating-point coordinate rectangle geometry.
Definition: Box.h:271