lsst.pipe.tasks  15.0-7-g6bb3a066+2
selectImages.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2008, 2009, 2010 LSST Corporation.
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 import numpy as np
23 import lsst.pex.config as pexConfig
24 import lsst.pex.exceptions as pexExceptions
25 import lsst.afw.geom as afwGeom
26 import lsst.pipe.base as pipeBase
27 
28 __all__ = ["BaseSelectImagesTask", "BaseExposureInfo", "WcsSelectImagesTask", "PsfWcsSelectImagesTask",
29  "DatabaseSelectImagesConfig"]
30 
31 
32 class DatabaseSelectImagesConfig(pexConfig.Config):
33  """Base configuration for subclasses of BaseSelectImagesTask that use a database"""
34  host = pexConfig.Field(
35  doc="Database server host name",
36  dtype=str,
37  )
38  port = pexConfig.Field(
39  doc="Database server port",
40  dtype=int,
41  )
42  database = pexConfig.Field(
43  doc="Name of database",
44  dtype=str,
45  )
46  maxExposures = pexConfig.Field(
47  doc="maximum exposures to select; intended for debugging; ignored if None",
48  dtype=int,
49  optional=True,
50  )
51 
52 
53 class BaseExposureInfo(pipeBase.Struct):
54  """Data about a selected exposure
55  """
56 
57  def __init__(self, dataId, coordList):
58  """Create exposure information that can be used to generate data references
59 
60  The object has the following fields:
61  - dataId: data ID of exposure (a dict)
62  - coordList: ICRS coordinates of the corners of the exposure (list of lsst.afw.geom.SpherePoint)
63  plus any others items that are desired
64  """
65  super(BaseExposureInfo, self).__init__(dataId=dataId, coordList=coordList)
66 
67 
68 class BaseSelectImagesTask(pipeBase.Task):
69  """Base task for selecting images suitable for coaddition
70  """
71  ConfigClass = pexConfig.Config
72  _DefaultName = "selectImages"
73 
74  @pipeBase.timeMethod
75  def run(self, coordList):
76  """Select images suitable for coaddition in a particular region
77 
78  @param[in] coordList: list of coordinates defining region of interest; if None then select all images
79  subclasses may add additional keyword arguments, as required
80 
81  @return a pipeBase Struct containing:
82  - exposureInfoList: a list of exposure information objects (subclasses of BaseExposureInfo),
83  which have at least the following fields:
84  - dataId: data ID dictionary
85  - coordList: ICRS coordinates of the corners of the exposure (list of lsst.afw.geom.SpherePoint)
86  """
87  raise NotImplementedError()
88 
89  def _runArgDictFromDataId(self, dataId):
90  """Extract keyword arguments for run (other than coordList) from a data ID
91 
92  @return keyword arguments for run (other than coordList), as a dict
93  """
94  raise NotImplementedError()
95 
96  def runDataRef(self, dataRef, coordList, makeDataRefList=True, selectDataList=[]):
97  """Run based on a data reference
98 
99  This delegates to run() and _runArgDictFromDataId() to do the actual
100  selection. In the event that the selectDataList is non-empty, this will
101  be used to further restrict the selection, providing the user with
102  additional control over the selection.
103 
104  @param[in] dataRef: data reference; must contain any extra keys needed by the subclass
105  @param[in] coordList: list of coordinates defining region of interest; if None, search the whole sky
106  @param[in] makeDataRefList: if True, return dataRefList
107  @param[in] selectDataList: List of SelectStruct with dataRefs to consider for selection
108  @return a pipeBase Struct containing:
109  - exposureInfoList: a list of objects derived from ExposureInfo
110  - dataRefList: a list of data references (None if makeDataRefList False)
111  """
112  runArgDict = self._runArgDictFromDataId(dataRef.dataId)
113  exposureInfoList = self.run(coordList, **runArgDict).exposureInfoList
114 
115  if len(selectDataList) > 0 and len(exposureInfoList) > 0:
116  # Restrict the exposure selection further
117  ccdKeys, ccdValues = _extractKeyValue(exposureInfoList)
118  inKeys, inValues = _extractKeyValue([s.dataRef for s in selectDataList], keys=ccdKeys)
119  inValues = set(inValues)
120  newExposureInfoList = []
121  for info, ccdVal in zip(exposureInfoList, ccdValues):
122  if ccdVal in inValues:
123  newExposureInfoList.append(info)
124  else:
125  self.log.info("De-selecting exposure %s: not in selectDataList" % info.dataId)
126  exposureInfoList = newExposureInfoList
127 
128  if makeDataRefList:
129  butler = dataRef.butlerSubset.butler
130  dataRefList = [butler.dataRef(datasetType="calexp",
131  dataId=expInfo.dataId,
132  ) for expInfo in exposureInfoList]
133  else:
134  dataRefList = None
135 
136  return pipeBase.Struct(
137  dataRefList=dataRefList,
138  exposureInfoList=exposureInfoList,
139  )
140 
141 
142 def _extractKeyValue(dataList, keys=None):
143  """Extract the keys and values from a list of dataIds
144 
145  The input dataList is a list of objects that have 'dataId' members.
146  This allows it to be used for both a list of data references and a
147  list of ExposureInfo
148  """
149  assert len(dataList) > 0
150  if keys is None:
151  keys = sorted(dataList[0].dataId.keys())
152  keySet = set(keys)
153  values = list()
154  for data in dataList:
155  thisKeys = set(data.dataId.keys())
156  if thisKeys != keySet:
157  raise RuntimeError("DataId keys inconsistent: %s vs %s" % (keySet, thisKeys))
158  values.append(tuple(data.dataId[k] for k in keys))
159  return keys, values
160 
161 
162 class SelectStruct(pipeBase.Struct):
163  """A container for data to be passed to the WcsSelectImagesTask"""
164 
165  def __init__(self, dataRef, wcs, bbox):
166  super(SelectStruct, self).__init__(dataRef=dataRef, wcs=wcs, bbox=bbox)
167 
168 
170  """Select images using their Wcs"""
171 
172  def runDataRef(self, dataRef, coordList, makeDataRefList=True, selectDataList=[]):
173  """Select images in the selectDataList that overlap the patch
174 
175  We use the "convexHull" function in the geom package to define
176  polygons on the celestial sphere, and test the polygon of the
177  patch for overlap with the polygon of the image.
178 
179  We use "convexHull" instead of generating a SphericalConvexPolygon
180  directly because the standard for the inputs to SphericalConvexPolygon
181  are pretty high and we don't want to be responsible for reaching them.
182  If "convexHull" is found to be too slow, we can revise this.
183 
184  @param dataRef: Data reference for coadd/tempExp (with tract, patch)
185  @param coordList: List of ICRS coordinates (lsst.afw.geom.SpherePoint) specifying boundary of patch
186  @param makeDataRefList: Construct a list of data references?
187  @param selectDataList: List of SelectStruct, to consider for selection
188  """
189  from lsst.geom import convexHull
190 
191  dataRefList = []
192  exposureInfoList = []
193 
194  patchVertices = [coord.getVector() for coord in coordList]
195  patchPoly = convexHull(patchVertices)
196 
197  for data in selectDataList:
198  dataRef = data.dataRef
199  imageWcs = data.wcs
200  imageBox = data.bbox
201 
202  try:
203  imageCorners = [imageWcs.pixelToSky(pix) for pix in afwGeom.Box2D(imageBox).getCorners()]
204  except (pexExceptions.DomainError, pexExceptions.RuntimeError) as e:
205  # Protecting ourselves from awful Wcs solutions in input images
206  self.log.debug("WCS error in testing calexp %s (%s): deselecting", dataRef.dataId, e)
207  continue
208 
209  imagePoly = convexHull([coord.getVector() for coord in imageCorners])
210  if imagePoly is None:
211  self.log.debug("Unable to create polygon from image %s: deselecting", dataRef.dataId)
212  continue
213  if patchPoly.intersects(imagePoly): # "intersects" also covers "contains" or "is contained by"
214  self.log.info("Selecting calexp %s" % dataRef.dataId)
215  dataRefList.append(dataRef)
216  exposureInfoList.append(BaseExposureInfo(dataRef.dataId, imageCorners))
217 
218  return pipeBase.Struct(
219  dataRefList=dataRefList if makeDataRefList else None,
220  exposureInfoList=exposureInfoList,
221  )
222 
223 
224 class PsfWcsSelectImagesConfig(pexConfig.Config):
225  maxEllipResidual = pexConfig.Field(
226  doc="Maximum median ellipticity residual",
227  dtype=float,
228  default=0.007,
229  optional=True,
230  )
231  maxSizeScatter = pexConfig.Field(
232  doc="Maximum scatter in the size residuals",
233  dtype=float,
234  optional=True,
235  )
236  maxScaledSizeScatter = pexConfig.Field(
237  doc="Maximum scatter in the size residuals, scaled by the median size",
238  dtype=float,
239  default=0.009,
240  optional=True,
241  )
242  starSelection = pexConfig.Field(
243  doc="select star with this field",
244  dtype=str,
245  default='calib_psfUsed'
246  )
247  starShape = pexConfig.Field(
248  doc="name of star shape",
249  dtype=str,
250  default='base_SdssShape'
251  )
252  psfShape = pexConfig.Field(
253  doc="name of psf shape",
254  dtype=str,
255  default='base_SdssShape_psf'
256  )
257 
258 
259 def sigmaMad(array):
260  "Return median absolute deviation scaled to normally distributed data"
261  return 1.4826*np.median(np.abs(array - np.median(array)))
262 
263 
265  """Select images using their Wcs and cuts on the PSF properties"""
266 
267  ConfigClass = PsfWcsSelectImagesConfig
268  _DefaultName = "PsfWcsSelectImages"
269 
270  def runDataRef(self, dataRef, coordList, makeDataRefList=True, selectDataList=[]):
271  """Select images in the selectDataList that overlap the patch and satisfy PSF quality critera.
272 
273  The PSF quality criteria are based on the size and ellipticity residuals from the
274  adaptive second moments of the star and the PSF.
275 
276  The criteria are:
277  - the median of the ellipticty residuals
278  - the robust scatter of the size residuals (using the median absolute deviation)
279  - the robust scatter of the size residuals scaled by the square of
280  the median size
281 
282  @param dataRef: Data reference for coadd/tempExp (with tract, patch)
283  @param coordList: List of ICRS coordinates (lsst.afw.geom.SpherePoint) specifying boundary of patch
284  @param makeDataRefList: Construct a list of data references?
285  @param selectDataList: List of SelectStruct, to consider for selection
286  """
287  result = super(PsfWcsSelectImagesTask, self).runDataRef(dataRef, coordList, makeDataRefList,
288  selectDataList)
289 
290  dataRefList = []
291  exposureInfoList = []
292  for dataRef, exposureInfo in zip(result.dataRefList, result.exposureInfoList):
293  butler = dataRef.butlerSubset.butler
294  srcCatalog = butler.get('src', dataRef.dataId)
295  mask = srcCatalog[self.config.starSelection]
296 
297  starXX = srcCatalog[self.config.starShape+'_xx'][mask]
298  starYY = srcCatalog[self.config.starShape+'_yy'][mask]
299  starXY = srcCatalog[self.config.starShape+'_xy'][mask]
300  psfXX = srcCatalog[self.config.psfShape+'_xx'][mask]
301  psfYY = srcCatalog[self.config.psfShape+'_yy'][mask]
302  psfXY = srcCatalog[self.config.psfShape+'_xy'][mask]
303 
304  starSize = np.power(starXX*starYY - starXY**2, 0.25)
305  starE1 = (starXX - starYY)/(starXX + starYY)
306  starE2 = 2*starXY/(starXX + starYY)
307  medianSize = np.median(starSize)
308 
309  psfSize = np.power(psfXX*psfYY - psfXY**2, 0.25)
310  psfE1 = (psfXX - psfYY)/(psfXX + psfYY)
311  psfE2 = 2*psfXY/(psfXX + psfYY)
312 
313  medianE1 = np.abs(np.median(starE1 - psfE1))
314  medianE2 = np.abs(np.median(starE2 - psfE2))
315  medianE = np.sqrt(medianE1**2 + medianE2**2)
316 
317  scatterSize = sigmaMad(starSize - psfSize)
318  scaledScatterSize = scatterSize/medianSize**2
319 
320  valid = True
321  if self.config.maxEllipResidual and medianE > self.config.maxEllipResidual:
322  self.log.info("Removing visit %s because median e residual too large: %f vs %f" %
323  (dataRef.dataId, medianE, self.config.maxEllipResidual))
324  valid = False
325  elif self.config.maxSizeScatter and scatterSize > self.config.maxSizeScatter:
326  self.log.info("Removing visit %s because size scatter is too large: %f vs %f" %
327  (dataRef.dataId, scatterSize, self.config.maxSizeScatter))
328  valid = False
329  elif self.config.maxScaledSizeScatter and scaledScatterSize > self.config.maxScaledSizeScatter:
330  self.log.info("Removing visit %s because scaled size scatter is too large: %f vs %f" %
331  (dataRef.dataId, scaledScatterSize, self.config.maxScaledSizeScatter))
332  valid = False
333 
334  if valid is False:
335  continue
336 
337  dataRefList.append(dataRef)
338  exposureInfoList.append(exposureInfo)
339 
340  return pipeBase.Struct(
341  dataRefList=dataRefList,
342  exposureInfoList=exposureInfoList,
343  )
def runDataRef(self, dataRef, coordList, makeDataRefList=True, selectDataList=[])
Definition: selectImages.py:96
def runDataRef(self, dataRef, coordList, makeDataRefList=True, selectDataList=[])
def runDataRef(self, dataRef, coordList, makeDataRefList=True, selectDataList=[])
def __init__(self, dataRef, wcs, bbox)