LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
transformMeasurement.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # LSST Data Management System
4 # Copyright 2008-2015 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 <http://www.lsstcorp.org/LegalNotices/>.
22 #
23 """
24 Tasks for transforming raw measurement outputs to calibrated quantities.
25 """
26 import lsst.afw.table as afwTable
27 import lsst.meas.base as measBase
28 import lsst.pex.config as pexConfig
29 import lsst.pipe.base as pipeBase
30 
31 def makeContiguous(catalog):
32  """!Return a version of the input catalog which is contiguous in memory."""
33  if not catalog.isContiguous():
34  return catalog.copy(deep=True)
35  else:
36  return catalog
37 
38 
39 class TransformConfig(pexConfig.Config):
40  """!Configuration for TransformTask."""
41  copyFields = pexConfig.ListField(
42  dtype=str,
43  doc="Fields to copy from input to output catalog without transformation",
44  default=('id', 'coord_ra', 'coord_dec')
45  )
46 
47 ## \addtogroup LSST_task_documentation
48 ## \{
49 ## \page TransformTask
50 ## \ref TransformTask_ "TransformTask"
51 ## \copybrief TransformTask
52 ## \}
53 
54 class TransformTask(pipeBase.Task):
55  """!
56  \anchor TransformTask_
57 
58  \brief Transform a SourceCatalog containing raw measurements to calibrated form.
59 
60  \section pipe_tasks_transform_Contents Contents
61 
62  - \ref pipe_tasks_transform_purpose
63  - \ref pipe_tasks_transform_initialize
64  - \ref pipe_tasks_transform_invoke
65 
66  \section pipe_tasks_transform_purpose Description
67 
68  Given a set of measurement algorithms with their associated configuration,
69  the table of source measurements they have produced, and information about
70  an associated WCS and calibration, transform the raw measurement output to
71  a calibrated form.
72 
73  Transformations are defined on a per-measurement-plugin basis. In
74  addition, a configurable set of fields may be simply copied from the input
75  to the output catalog.
76 
77  This task operates on an input SourceCatalog and returns a BaseCatalog
78  containing the transformed results. It requires that the caller supply
79  information on the configuration of the measurement task which produced
80  the input data as well as the world coordinate system and calibration
81  under which the transformation will take place. It provides no
82  functionality for reading or writing data from a Butler: rather,
83  per-dataset-type command line tasks are provided to obtain the appropriate
84  information from a Butler (or elsewhere) and then delegate to this task.
85 
86  \section pipe_tasks_transform_initialize Task initialization
87 
88  \copydoc \_\_init\_\_
89 
90  \section pipe_tasks_transform_invoke Task invocation
91 
92  \copydoc run
93  """
94  ConfigClass = TransformConfig
95  _DefaultName = "transform"
96 
97  def __init__(self, measConfig, inputSchema, outputDataset, *args, **kwargs):
98  """!Initialize TransformTask.
99 
100  @param[in] measConfig Configuration for the measurement task which
101  produced the measurments being transformed.
102  @param[in] inputSchema The schema of the input catalog.
103  @param[in] outputDataset The butler dataset type of the output catalog.
104  @param[in] *args Passed through to pipeBase.Task.__init__()
105  @param[in] *kwargs Passed through to pipeBase.Task.__init__()
106  """
107  pipeBase.Task.__init__(self, *args, **kwargs)
108 
109  # This task can be used to generate multiple different output dataset types. We
110  # need to be able to specify the output type together with its schema.
111  self.outputDataset = outputDataset
112 
113  # Define a mapper and add the basic fields to be copied.
114  self.mapper = afwTable.SchemaMapper(inputSchema)
115  for field in self.config.copyFields:
116  self.mapper.addMapping(inputSchema.find(field).key)
117 
118  # Build a list of all transforms that will be applied to the input. We
119  # will iterate over this in run().
120  self.transforms = []
121  for name in measConfig.plugins.names:
122  config = measConfig.plugins.get(name)
123  transformClass = measConfig.plugins.registry.get(name).PluginClass.getTransformClass()
124  self.transforms.append(transformClass(config, name, self.mapper))
125 
126  def getSchemaCatalogs(self):
127  """!Return a dict containing an empty catalog representative of this task's output."""
128  transformedSrc = afwTable.BaseCatalog(self.mapper.getOutputSchema())
129  return {self.outputDataset: transformedSrc}
130 
131  def run(self, inputCat, wcs, calib):
132  """!Transform raw source measurements to calibrated quantities.
133 
134  @param[in] inputCat SourceCatalog of sources to transform.
135  @param[in] wcs The world coordinate system under which transformations will take place.
136  @param[in] calib The calibration under which transformations will take place.
137 
138  @return A BaseCatalog containing the transformed measurements.
139  """
140  outputCat = afwTable.BaseCatalog(self.mapper.getOutputSchema())
141  outputCat.extend(inputCat, mapper=self.mapper)
142 
143  # Transforms may use a ColumnView on the input and output catalogs,
144  # which requires that the data be contiguous in memory.
145  inputCat = makeContiguous(inputCat)
146  outputCat = makeContiguous(outputCat)
147 
148  for transform in self.transforms:
149  transform(inputCat, outputCat, wcs, calib)
150  return outputCat
151 
152 
153 class RunTransformConfig(pexConfig.Config):
154  """!Configuration for RunTransformTaskBase derivatives."""
155  transform = pexConfig.ConfigurableField(
156  doc="Subtask which performs transformations",
157  target=TransformTask
158  )
159  inputConfigType = pexConfig.Field(
160  dtype=str,
161  doc="Dataset type of measurement operation configuration",
162  )
163 
164 
165 class RunTransformTaskBase(pipeBase.CmdLineTask):
166  """!
167  \anchor RunTransformTaskBase_
168 
169  \brief Command line interface for TransformTask.
170 
171  \section pipe_tasks_transform_Contents Contents
172 
173  - \ref pipe_tasks_runtransform_purpose
174  - \ref pipe_tasks_runtransform_invoke
175 
176  \section pipe_tasks_runtransform_purpose Description
177 
178  Provides a command-line task which can be used to run TransformTask.
179 
180  - Loads a plugin registry based on configuration;
181  - Loads configuration for the measurement task which was applied from a repository;
182  - Loads the SourceCatalog input schema from a repository;
183  - For each input dataRef, reads the SourceCatalog, WCS and calibration from the
184  repository and executes TransformTask.
185 
186  This is not a fully-fledged command line task: it requires specialization to a particular
187  source type by defining the variables indicated below.
188 
189  \section pipe_tasks_runtransform_invoke Task invocation
190 
191  \copydoc run
192  """
193  RunnerClass = pipeBase.ButlerInitializedTaskRunner
194  ConfigClass = RunTransformConfig
195 
196  # Subclasses should provide definitions for the attributes named below.
197  # Properties can be used if appropriate.
198  #
199  # Standard CmdLineTask attributes:
200  _DefaultName = None
201 
202  # Butler dataset type of the source type to be transformed ("src", "forced_src", etc):
203  sourceType = None
204 
205  # Butler dataset type of the calibration exposure to use when transforming ("calexp", etc):
206  calexpType = None
207 
208  @property
209  def inputSchemaType(self):
210  """!
211  The Butler dataset type for the schema of the input source catalog.
212 
213  By default, we append `_schema` to the input source type. Subclasses may customize
214  if required.
215  """
216  return self.sourceType + "_schema"
217 
218  @property
219  def outputDataset(self):
220  """!
221  The Butler dataset type for the schema of the output catalog.
222 
223  By default, we prepend `transformed_` to the input source type. Subclasses may
224  customize if required.
225  """
226  return 'transformed_' + self.sourceType
227 
228  @property
229  def measurementConfig(self):
230  """!
231  The configuration of the measurement operation used to generate the input catalog.
232 
233  By default we look for `measurement` under the root configuration of the
234  generating task. Subclasses may customize this (e.g. to `calibration.measurement`)
235  if required.
236  """
237  return self.butler.get(self.config.inputConfigType).measurement.value
238 
239  def __init__(self, *args, **kwargs):
240  pipeBase.CmdLineTask.__init__(self, *args, config=kwargs['config'], log=kwargs['log'])
241  self.butler = kwargs['butler']
242  self.makeSubtask('transform', measConfig=self.measurementConfig,
243  inputSchema=self.butler.get(self.inputSchemaType).schema,
244  outputDataset=self.outputDataset)
245 
246  @pipeBase.timeMethod
247  def run(self, dataRef):
248  """!Transform the source catalog referred to by dataRef.
249 
250  The result is both returned and written as dataset type "transformed_" + the input
251  source dataset type to the provided dataRef.
252 
253  @param[in] dataRef Data reference for source catalog & calibrated exposure.
254 
255  @returns A BaseCatalog containing the transformed measurements.
256  """
257  inputCat = dataRef.get(self.sourceType)
258  wcs = dataRef.get(self.calexpType).getWcs()
259  calib = dataRef.get(self.calexpType).getCalib()
260  outputCat = self.transform.run(inputCat, wcs, calib)
261  dataRef.put(outputCat, self.outputDataset)
262  return outputCat
263 
264 
265 ## \addtogroup LSST_task_documentation
266 ## \{
267 ## \page SrcTransformTask
268 ## \ref SrcTransformTask_ "SrcTransformTask"
269 ## \copybrief SrcTransformTask
270 ## \}
271 
273  """!
274  \anchor SrcTransformTask_
275 
276  \brief Transform ``src`` measuremenents to calibrated form.
277 
278  This is a specialization of \ref RunTransformTaskBase_ "RunTransformTaskBase" which
279  operates on ``src`` measurements. Refer to the parent documentation for details.
280  """
281  _DefaultName = "transformSrcMeasurement"
282  sourceType = 'src'
283  calexpType = 'calexp'
284 
285 
286 ## \addtogroup LSST_task_documentation
287 ## \{
288 ## \page ForcedSrcTransformTask
289 ## \ref ForcedSrcTransformTask_ "ForcedSrcTransformTask"
290 ## \copybrief ForcedSrcTransformTask
291 ## \}
292 
294  """!
295  \anchor ForcedSrcTransformTask_
296 
297  \brief Transform ``forced_src`` measuremenents to calibrated form.
298 
299  This is a specialization of \ref RunTransformTaskBase_ "RunTransformTaskBase" which
300  operates on ``forced_src`` measurements. Refer to the parent documentation for details.
301  """
302  _DefaultName = "transformForcedSrcMeasurement"
303  sourceType = 'forced_src'
304  calexpType = 'calexp'
305 
306 
307 ## \addtogroup LSST_task_documentation
308 ## \{
309 ## \page CoaddSrcTransformTask
310 ## \ref CoaddSrcTransformTask_ "CoaddSrcTransformTask"
311 ## \copybrief CoaddSrcTransformTask
312 ## \}
313 
315  """!
316  \anchor CoaddSrcTransformTask_
317 
318  \brief Transform measuremenents made on coadds to calibrated form.
319 
320  This is a specialization of \ref RunTransformTaskBase_ "RunTransformTaskBase" which
321  operates on measurements made on coadds. Refer to the parent documentation for details.
322  """
323  _DefaultName = "transformCoaddSrcMeasurement"
324 
325  @property
326  def coaddName(self):
327  return self.self.butler.get(self.config.inputConfigType).coaddName
328 
329  @property
330  def sourceType(self):
331  return self.coaddName + "_src"
332 
333  @property
334  def calexpType(self):
335  return self.coaddName + "_calexp"
336 
337  def _getConfigName(self):
338  return "%s_transformCoaddSrcMeasurement_config" % (self.coaddName,)
339 
340  def _getMetaDataName(self):
341  return "%s_transformCoaddSrcMeasurement_metadata" % (self.coaddName,)
Transform a SourceCatalog containing raw measurements to calibrated form.
def measurementConfig
The configuration of the measurement operation used to generate the input catalog.
A custom container class for records, based on std::vector.
Definition: Catalog.h:94
A mapping between the keys of two Schemas, used to copy data between them.
Definition: SchemaMapper.h:19
Transform src measuremenents to calibrated form.
def inputSchemaType
The Butler dataset type for the schema of the input source catalog.
def run
Transform the source catalog referred to by dataRef.
def outputDataset
The Butler dataset type for the schema of the output catalog.
Transform forced_src measuremenents to calibrated form.
Configuration for RunTransformTaskBase derivatives.
def makeContiguous
Return a version of the input catalog which is contiguous in memory.
def getSchemaCatalogs
Return a dict containing an empty catalog representative of this task&#39;s output.
Transform measuremenents made on coadds to calibrated form.
def run
Transform raw source measurements to calibrated quantities.