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
interpImage.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 <https://www.lsstcorp.org/LegalNotices/>.
21 #
22 import lsst.pex.config as pexConfig
23 import lsst.afw.math as afwMath
24 import lsst.meas.algorithms as measAlg
25 import lsst.pipe.base as pipeBase
26 import lsst.ip.isr as ipIsr
27 
28 __all__ = ["InterpImageConfig", "InterpImageTask"]
29 
30 class InterpImageConfig(pexConfig.Config):
31  """Config for InterpImageTask
32  """
33  modelPsf = measAlg.GaussianPsfFactory.makeField(doc = "Model Psf factory")
34 
35  useFallbackValueAtEdge = pexConfig.Field(
36  dtype = bool,
37  doc = "Smoothly taper to the fallback value at the edge of the image?",
38  default = True,
39  )
40  fallbackValueType = pexConfig.ChoiceField(
41  dtype = str,
42  doc = "Type of statistic to calculate edge fallbackValue for interpolation",
43  allowed = {
44  "MEAN": "mean",
45  "MEDIAN": "median",
46  "MEANCLIP": "clipped mean",
47  "USER": "user value set in fallbackUserValue config",
48  },
49  default = "MEDIAN",
50  )
51  fallbackUserValue = pexConfig.Field(
52  dtype = float,
53  doc = "If fallbackValueType is 'USER' then use this as the fallbackValue; ignored otherwise",
54  default = 0.0,
55  )
56  negativeFallbackAllowed = pexConfig.Field(
57  dtype = bool,
58  doc = ("Allow negative values for egde interpolation fallbackValue? If False, set "
59  "fallbackValue to max(fallbackValue, 0.0)"),
60  default = False,
61  )
62 
63  def validate(self):
64  pexConfig.Config.validate(self)
65  if self.useFallbackValueAtEdge:
66  if (not self.negativeFallbackAllowed and self.fallbackValueType == "USER" and
67  self.fallbackUserValue < 0.0):
68  raise ValueError("User supplied fallbackValue is negative (%.2f) but "
69  "negativeFallbackAllowed is False" % self.fallbackUserValue)
70 
71 class InterpImageTask(pipeBase.Task):
72  """Interpolate over bad image pixels
73  """
74  ConfigClass = InterpImageConfig
75  _DefaultName = "interpImage"
76 
77  def _setFallbackValue(self, mi=None):
78  """Set the edge fallbackValue for interpolation
79 
80  \param[in] mi input maksedImage on which to calculate the statistics
81  Must be provided if fallbackValueType != "USER".
82 
83  \return fallbackValue The value set/computed based on the fallbackValueType
84  and negativeFallbackAllowed config parameters
85  """
86  if self.config.fallbackValueType != 'USER':
87  assert mi, "No maskedImage provided"
88  if self.config.fallbackValueType == 'MEAN':
89  fallbackValue = afwMath.makeStatistics(mi, afwMath.MEAN).getValue()
90  elif self.config.fallbackValueType == 'MEDIAN':
91  fallbackValue = afwMath.makeStatistics(mi, afwMath.MEDIAN).getValue()
92  elif self.config.fallbackValueType == 'MEANCLIP':
93  fallbackValue = afwMath.makeStatistics(mi, afwMath.MEANCLIP).getValue()
94  elif self.config.fallbackValueType == 'USER':
95  fallbackValue = self.config.fallbackUserValue
96  else:
97  raise NotImplementedError("%s : %s not implemented" %
98  ("fallbackValueType", self.config.fallbackValueType))
99 
100  if not self.config.negativeFallbackAllowed and fallbackValue < 0.0:
101  self.log.warn("Negative interpolation edge fallback value computed but "
102  "negativeFallbackAllowed is False: setting fallbackValue to 0.0")
103  fallbackValue = max(fallbackValue, 0.0)
104 
105  self.log.info("fallbackValueType %s has been set to %.4f" %
106  (self.config.fallbackValueType, fallbackValue))
107 
108  return fallbackValue
109 
110  @pipeBase.timeMethod
111  def run(self, image, planeName=None, fwhmPixels=None, defects=None):
112  """!Interpolate in place over pixels in a maskedImage marked as bad
113 
114  Pixels to be interpolated are set by either a mask planeName provided
115  by the caller OR a defects list of type measAlg.DefectListT. If both
116  are provided an exception is raised.
117 
118  Note that the interpolation code in meas_algorithms currently doesn't
119  use the input PSF (though it's a required argument), so it's not
120  important to set the input PSF parameters exactly. This PSF is set
121  here as the psf attached to the "image" (i.e if the image passed in
122  is an Exposure). Otherwise, a psf model is created using
123  measAlg.GaussianPsfFactory with the value of fwhmPixels (the value
124  passed in by the caller, or the default defaultFwhm set in
125  measAlg.GaussianPsfFactory if None).
126 
127  \param[in,out] image MaskedImage OR Exposure to be interpolated
128  \param[in] planeName name of mask plane over which to interpolate
129  If None, must provide a defects list.
130  \param[in] fwhmPixels FWHM of core star (pixels)
131  If None the default is used, where the default
132  is set to the exposure psf if available
133  \param[in] defects List of defects of type measAlg.DefectListT
134  over which to interpolate.
135  """
136  try:
137  maskedImage = image.getMaskedImage()
138  except AttributeError:
139  maskedImage = image
140 
141  # set defectList from defects OR mask planeName provided
142  if planeName is None:
143  if defects is None:
144  raise ValueError("No defects or plane name provided")
145  else:
146  defectList = defects
147  planeName = "defects"
148  else:
149  if defects is not None:
150  raise ValueError("Provide EITHER a planeName OR a list of defects, not both")
151  if not maskedImage.getMask().getMaskPlaneDict().has_key(planeName):
152  raise ValueError("maskedImage does not contain mask plane %s" % planeName)
153  defectList = ipIsr.getDefectListFromMask(maskedImage, planeName, growFootprints=0)
154 
155  # set psf from exposure if provided OR using modelPsf with fwhmPixels provided
156  try:
157  psf = image.getPsf()
158  self.log.info("Setting psf for interpolation from image")
159  except AttributeError:
160  self.log.info("Creating psf model for interpolation from fwhm(pixels) = %s" %
161  (str(fwhmPixels) if fwhmPixels is not None else
162  (str(self.config.modelPsf.defaultFwhm)) + " [default]"))
163  psf = self.config.modelPsf.apply(fwhm=fwhmPixels)
164 
165  fallbackValue = 0.0 # interpolateOverDefects needs this to be a float, regardless if it is used
166  if self.config.useFallbackValueAtEdge:
167  fallbackValue = self._setFallbackValue(maskedImage)
168 
169  measAlg.interpolateOverDefects(maskedImage, psf, defectList, fallbackValue,
170  self.config.useFallbackValueAtEdge)
171 
172  self.log.info("Interpolated over %d %s pixels." % (len(defectList), planeName))
void interpolateOverDefects(MaskedImageT &image, lsst::afw::detection::Psf const &psf, std::vector< Defect::Ptr > &badList, double fallbackValue=0.0, bool useFallbackValueAtEdge=false)
Process a set of known bad pixels in an image.
Definition: Interp.cc:2058
def run
Interpolate in place over pixels in a maskedImage marked as bad.
Definition: interpImage.py:111
Statistics makeStatistics(afwImage::Mask< afwImage::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl)
Specialization to handle Masks.
Definition: Statistics.cc:1107