lsst.meas.deblender  16.0-8-g4dec96c+16
deblendAndMeasure.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2008-2013 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 import lsst.pex.config as pexConfig
24 import lsst.pipe.base as pipeBase
25 import lsst.daf.base as dafBase
26 import lsst.afw.table as afwTable
27 from lsst.meas.algorithms import SourceMeasurementTask
28 from lsst.meas.deblender import SourceDeblendTask
29 
30 
31 class DeblendAndMeasureConfig(pexConfig.Config):
32  doDeblend = pexConfig.Field(dtype=bool, default=True, doc="Deblend sources?")
33  doMeasurement = pexConfig.Field(dtype=bool, default=True, doc="Measure sources?")
34  doWriteSources = pexConfig.Field(dtype=bool, default=True, doc="Write sources?")
35  doWriteHeavyFootprintsInSources = pexConfig.Field(dtype=bool, default=False,
36  doc="Include HeavyFootprint data in source table?")
37 
38  sourceOutputFile = pexConfig.Field(
39  dtype=str, default=None, doc="Write sources to given filename (default: use butler)", optional=True)
40 
41  deblend = pexConfig.ConfigurableField(
42  target=SourceDeblendTask,
43  doc="Split blended sources into their components",
44  )
45  measurement = pexConfig.ConfigurableField(
46  target=SourceMeasurementTask,
47  doc="Final source measurement on low-threshold detections",
48  )
49 
50 
51 class DeblendAndMeasureTask(pipeBase.CmdLineTask):
52  ConfigClass = DeblendAndMeasureConfig
53  _DefaultName = "deblendAndMeasure"
54 
55  def writeConfig(self, *args, **kwargs):
56  pass
57 
58  def __init__(self, **kwargs):
59  pipeBase.CmdLineTask.__init__(self, **kwargs)
60 
61  @pipeBase.timeMethod
62  def run(self, dataRef):
63  self.log.info("Processing %s" % (dataRef.dataId))
64  calexp = dataRef.get('calexp')
65  srcs = dataRef.get('src')
66  print('Calexp:', calexp)
67  print('srcs:', srcs)
68 
69  # FIXME -- this whole mapping business is very fragile -- it
70  # seems to fail, eg, if you don't set "-c
71  # doMeasurement=False" when creating the input 'srcs' list.
72 
73  mapper = afwTable.SchemaMapper(srcs.getSchema())
74  # map all the existing fields
75  mapper.addMinimalSchema(srcs.getSchema(), True)
76  schema = mapper.getOutputSchema()
77  self.algMetadata = dafBase.PropertyList()
78  if self.config.doDeblend:
79  self.makeSubtask("deblend", schema=schema)
80  if self.config.doMeasurement:
81  self.makeSubtask("measurement", schema=schema, algMetadata=self.algMetadata)
82  self.schema = schema
83 
84  parents = []
85  for src in srcs:
86  if src.getParent() == 0:
87  parents.append(src)
88 
89  outsources = afwTable.SourceCatalog(schema)
90  outsources.reserve(len(parents))
91  outsources.extend(parents, mapper=mapper)
92  srcs = outsources
93  print(len(srcs), 'sources before deblending')
94 
95  if self.config.doDeblend:
96  self.deblend.run(calexp, srcs)
97 
98  if self.config.doMeasurement:
99  self.measurement.run(calexp, srcs)
100 
101  if srcs is not None and self.config.doWriteSources:
102  sourceWriteFlags = (0 if self.config.doWriteHeavyFootprintsInSources
103  else afwTable.SOURCE_IO_NO_HEAVY_FOOTPRINTS)
104  print('Writing "src" outputs')
105  if self.config.sourceOutputFile:
106  srcs.writeFits(self.config.sourceOutputFile, flags=sourceWriteFlags)
107  else:
108  dataRef.put(srcs, 'src', flags=sourceWriteFlags)
109 
110 
111 if __name__ == '__main__':
112  DeblendAndMeasureTask.parseAndRun()