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