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