lsst.meas.base  16.0-12-g5ad1ebf+7
measurementInvestigationLib.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 #
4 # Copyright 2008-2017 AURA/LSST.
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 <https://www.lsstcorp.org/LegalNotices/>.
22 #
23 
24 from collections import Iterable
25 
26 from lsst.afw.table import Schema, SourceCatalog
27 from lsst.meas.base import NoiseReplacer, NoiseReplacerConfig
28 from lsst.meas.base import SingleFrameMeasurementTask as SFMT # noqa N814
29 
30 
31 def rebuildNoiseReplacer(exposure, measCat):
32  """ Recreate NoiseReplacer used in measurement
33 
34  Given a measurement catalog and the exposure on which the measurements were
35  made, reconstruct the NoiseReplacer object that was used to mask out
36  sources during measurement.
37 
38  Parameters
39  ----------
40  exposure : lsst.awf.exposure.Exposure
41  The exposure on which measurements were made
42 
43  meaCat : lsst.afw.table.SourceCatalog
44  Catalog containing the outputs of measurements on each source
45 
46  Returns
47  -------
48  noiseReplacer : lsst.meas.base.NoiseReplacer
49  Object used to replace and or restore sources in the exposure with
50  deterministic noise
51  """
52 
53  algMetadata = measCat.getMetadata()
54  noiseReplacerConf = NoiseReplacerConfig()
55  noiseReplacerConf.noiseSeedMultiplier = \
56  algMetadata.getScalar(SFMT.NOISE_SEED_MULTIPLIER)
57  noiseReplacerConf.noiseSource = algMetadata.getScalar(SFMT.NOISE_SOURCE)
58  noiseReplacerConf.noiseOffset = algMetadata.getScalar(SFMT.NOISE_OFFSET)
59 
60  footprints = {src.getId(): (src.getParent(), src.getFootprint())
61  for src in measCat}
62 
63  try:
64  exposureId = algMetadata.getScalar(SFMT.NOISE_EXPOSURE_ID)
65  except Exception:
66  exposureId = None
67 
68  noiseReplacer = NoiseReplacer(noiseReplacerConf, exposure, footprints,
69  exposureId=exposureId)
70  return noiseReplacer
71 
72 
73 def makeRerunCatalog(schema, oldCatalog, idList, fields=None,
74  resetParents=True, addParents=False):
75  """ Creates a catalog prepopulated with ids
76 
77  This function is used to generate a SourceCatalog containing blank records
78  with Ids specified in the idList parameter
79 
80  This function is primarily used when rerunning measurements on a footprint.
81  Specifying ids in a new measurement catalog which correspond to ids in an
82  old catalog makes comparing results much easier.
83 
84  The resetParents and addParents options are needed because
85  lsst.meas.base.SingleFrameMeasurementTask.runPlugins() will skip child
86  objects whose parents are not in the catalog.
87 
88  Parameters
89  ----------
90  schema : lsst.afw.table.Schema
91  Schema used to describe the fields in the resulting SourceCatalog
92 
93  oldCatalog : lsst.afw.table.SourceCatalog
94  Catalog containing previous measurements.
95 
96  idList : iterable
97  Python iterable whose values should be numbers corresponding to
98  measurement ids, ids must exist in the oldCatalog
99 
100  fields : iterable
101  Python iterable whose entries should be strings corresponding to schema
102  keys that exist in both the old catalog and input schema. Fields listed
103  will be copied from the old catalog into the new catalog.
104 
105  resetParents: boolean
106  Flag to toggle whether child objects whose parents are not in the
107  idList should have their parents reset to zero.
108 
109  addParents: boolean
110  Flag to toggle whether parents of child objects will be added to the
111  idList (if not already present).
112 
113  Returns
114  -------
115  measCat : lsst.afw.table.SourceCatalog
116  SourceCatalog prepopulated with entries corresponding to the ids
117  specified
118  """
119 
120  if not isinstance(schema, Schema):
121  raise RuntimeError("schema must be an instance of "
122  "lsst.afw.table.Schema")
123 
124  if not isinstance(oldCatalog, SourceCatalog):
125  raise RuntimeError("oldCatalog must be an instance of "
126  "lsst.afw.table.SourceCatalogiterable")
127 
128  if fields is None:
129  fields = []
130  if not isinstance(fields, Iterable):
131  raise RuntimeError("fields list must be an iterable with string"
132  "elements")
133  for entry in fields:
134  if entry not in schema:
135  schema.addField(oldCatalog.schema.find(entry).field)
136 
137  if addParents:
138  newIdList = set()
139  for srcId in idList:
140  newIdList.add(srcId)
141  parentId = oldCatalog.find(srcId).getParent()
142  if parentId:
143  newIdList.add(parentId)
144  idList = newIdList
145  idList = sorted(idList)
146 
147  measCat = SourceCatalog(schema)
148  for srcId in idList:
149  oldSrc = oldCatalog.find(srcId)
150  src = measCat.addNew()
151  src.setId(srcId)
152  src.setFootprint(oldSrc.getFootprint())
153  parent = oldSrc.getParent()
154  if resetParents and parent and parent not in idList:
155  parent = 0
156  src.setParent(parent)
157  src.setCoord(oldSrc.getCoord())
158  for entry in fields:
159  src[entry] = oldSrc[entry]
160  return measCat
def makeRerunCatalog(schema, oldCatalog, idList, fields=None, resetParents=True, addParents=False)
Class that handles replacing sources with noise during measurement.