LSSTApplications  16.0-10-g0ee56ad+5,16.0-11-ga33d1f2+5,16.0-12-g3ef5c14+3,16.0-12-g71e5ef5+18,16.0-12-gbdf3636+3,16.0-13-g118c103+3,16.0-13-g8f68b0a+3,16.0-15-gbf5c1cb+4,16.0-16-gfd17674+3,16.0-17-g7c01f5c+3,16.0-18-g0a50484+1,16.0-20-ga20f992+8,16.0-21-g0e05fd4+6,16.0-21-g15e2d33+4,16.0-22-g62d8060+4,16.0-22-g847a80f+4,16.0-25-gf00d9b8+1,16.0-28-g3990c221+4,16.0-3-gf928089+3,16.0-32-g88a4f23+5,16.0-34-gd7987ad+3,16.0-37-gc7333cb+2,16.0-4-g10fc685+2,16.0-4-g18f3627+26,16.0-4-g5f3a788+26,16.0-5-gaf5c3d7+4,16.0-5-gcc1f4bb+1,16.0-6-g3b92700+4,16.0-6-g4412fcd+3,16.0-6-g7235603+4,16.0-69-g2562ce1b+2,16.0-8-g14ebd58+4,16.0-8-g2df868b+1,16.0-8-g4cec79c+6,16.0-8-gadf6c7a+1,16.0-8-gfc7ad86,16.0-82-g59ec2a54a+1,16.0-9-g5400cdc+2,16.0-9-ge6233d7+5,master-g2880f2d8cf+3,v17.0.rc1
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 
31 class InterpImageConfig(pexConfig.Config):
32  """Config for InterpImageTask
33  """
34  modelPsf = measAlg.GaussianPsfFactory.makeField(doc="Model Psf factory")
35 
36  useFallbackValueAtEdge = pexConfig.Field(
37  dtype=bool,
38  doc="Smoothly taper to the fallback value at the edge of the image?",
39  default=True,
40  )
41  fallbackValueType = pexConfig.ChoiceField(
42  dtype=str,
43  doc="Type of statistic to calculate edge fallbackValue for interpolation",
44  allowed={
45  "MEAN": "mean",
46  "MEDIAN": "median",
47  "MEANCLIP": "clipped mean",
48  "USER": "user value set in fallbackUserValue config",
49  },
50  default="MEDIAN",
51  )
52  fallbackUserValue = pexConfig.Field(
53  dtype=float,
54  doc="If fallbackValueType is 'USER' then use this as the fallbackValue; ignored otherwise",
55  default=0.0,
56  )
57  negativeFallbackAllowed = pexConfig.Field(
58  dtype=bool,
59  doc=("Allow negative values for egde interpolation fallbackValue? If False, set "
60  "fallbackValue to max(fallbackValue, 0.0)"),
61  default=False,
62  )
63 
64  def validate(self):
65  pexConfig.Config.validate(self)
66  if self.useFallbackValueAtEdge:
67  if (not self.negativeFallbackAllowed and self.fallbackValueType == "USER" and
68  self.fallbackUserValue < 0.0):
69  raise ValueError("User supplied fallbackValue is negative (%.2f) but "
70  "negativeFallbackAllowed is False" % self.fallbackUserValue)
71 
72 
73 class InterpImageTask(pipeBase.Task):
74  """Interpolate over bad image pixels
75  """
76  ConfigClass = InterpImageConfig
77  _DefaultName = "interpImage"
78 
79  def _setFallbackValue(self, mi=None):
80  """Set the edge fallbackValue for interpolation
81 
82  @param[in] mi input maksedImage on which to calculate the statistics
83  Must be provided if fallbackValueType != "USER".
84 
85  @return fallbackValue The value set/computed based on the fallbackValueType
86  and negativeFallbackAllowed config parameters
87  """
88  if self.config.fallbackValueType != 'USER':
89  assert mi, "No maskedImage provided"
90  if self.config.fallbackValueType == 'MEAN':
91  fallbackValue = afwMath.makeStatistics(mi, afwMath.MEAN).getValue()
92  elif self.config.fallbackValueType == 'MEDIAN':
93  fallbackValue = afwMath.makeStatistics(mi, afwMath.MEDIAN).getValue()
94  elif self.config.fallbackValueType == 'MEANCLIP':
95  fallbackValue = afwMath.makeStatistics(mi, afwMath.MEANCLIP).getValue()
96  elif self.config.fallbackValueType == 'USER':
97  fallbackValue = self.config.fallbackUserValue
98  else:
99  raise NotImplementedError("%s : %s not implemented" %
100  ("fallbackValueType", self.config.fallbackValueType))
101 
102  if not self.config.negativeFallbackAllowed and fallbackValue < 0.0:
103  self.log.warn("Negative interpolation edge fallback value computed but "
104  "negativeFallbackAllowed is False: setting fallbackValue to 0.0")
105  fallbackValue = max(fallbackValue, 0.0)
106 
107  self.log.info("fallbackValueType %s has been set to %.4f" %
108  (self.config.fallbackValueType, fallbackValue))
109 
110  return fallbackValue
111 
112  @pipeBase.timeMethod
113  def run(self, image, planeName=None, fwhmPixels=None, defects=None):
114  """!Interpolate in place over pixels in a maskedImage marked as bad
115 
116  Pixels to be interpolated are set by either a mask planeName provided
117  by the caller OR a defects list of type measAlg.DefectListT. If both
118  are provided an exception is raised.
119 
120  Note that the interpolation code in meas_algorithms currently doesn't
121  use the input PSF (though it's a required argument), so it's not
122  important to set the input PSF parameters exactly. This PSF is set
123  here as the psf attached to the "image" (i.e if the image passed in
124  is an Exposure). Otherwise, a psf model is created using
125  measAlg.GaussianPsfFactory with the value of fwhmPixels (the value
126  passed in by the caller, or the default defaultFwhm set in
127  measAlg.GaussianPsfFactory if None).
128 
129  @param[in,out] image MaskedImage OR Exposure to be interpolated
130  @param[in] planeName name of mask plane over which to interpolate
131  If None, must provide a defects list.
132  @param[in] fwhmPixels FWHM of core star (pixels)
133  If None the default is used, where the default
134  is set to the exposure psf if available
135  @param[in] defects List of defects of type measAlg.DefectListT
136  over which to interpolate.
137  """
138  try:
139  maskedImage = image.getMaskedImage()
140  except AttributeError:
141  maskedImage = image
142 
143  # set defectList from defects OR mask planeName provided
144  if planeName is None:
145  if defects is None:
146  raise ValueError("No defects or plane name provided")
147  else:
148  defectList = defects
149  planeName = "defects"
150  else:
151  if defects is not None:
152  raise ValueError("Provide EITHER a planeName OR a list of defects, not both")
153  if planeName not in maskedImage.getMask().getMaskPlaneDict():
154  raise ValueError("maskedImage does not contain mask plane %s" % planeName)
155  defectList = ipIsr.getDefectListFromMask(maskedImage, planeName)
156 
157  # set psf from exposure if provided OR using modelPsf with fwhmPixels provided
158  try:
159  psf = image.getPsf()
160  self.log.info("Setting psf for interpolation from image")
161  except AttributeError:
162  self.log.info("Creating psf model for interpolation from fwhm(pixels) = %s" %
163  (str(fwhmPixels) if fwhmPixels is not None else
164  (str(self.config.modelPsf.defaultFwhm)) + " [default]"))
165  psf = self.config.modelPsf.apply(fwhm=fwhmPixels)
166 
167  fallbackValue = 0.0 # interpolateOverDefects needs this to be a float, regardless if it is used
168  if self.config.useFallbackValueAtEdge:
169  fallbackValue = self._setFallbackValue(maskedImage)
170 
171  measAlg.interpolateOverDefects(maskedImage, psf, defectList, fallbackValue,
172  self.config.useFallbackValueAtEdge)
173 
174  self.log.info("Interpolated over %d %s pixels." % (len(defectList), planeName))
Fit spatial kernel using approximate fluxes for candidates, and solving a linear system of equations...
Statistics makeStatistics(lsst::afw::math::MaskedVector< EntryT > const &mv, std::vector< WeightPixel > const &vweights, int const flags, StatisticsControl const &sctrl=StatisticsControl())
The makeStatistics() overload to handle lsst::afw::math::MaskedVector<>
Definition: Statistics.h:520
int max
def run(self, image, planeName=None, fwhmPixels=None, defects=None)
Interpolate in place over pixels in a maskedImage marked as bad.
Definition: interpImage.py:113