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