lsst.meas.modelfit  15.0-3-g150fc43+8
cModelDisplay.py
Go to the documentation of this file.
1 from __future__ import division, print_function, absolute_import
2 
3 __all__ = ("displayReconstructedCmodel", "displayReconstrucedCmodelMpl",
4  "buildCModelImages", "reconstructCModel")
5 
6 import numpy as np
7 import matplotlib.pyplot as plt
8 
9 import lsst.afw.image as afwImage
10 import lsst.meas.modelfit as measMod
11 import lsst.shapelet as shapelet
12 
13 
14 def displayReconstructedCmodel(exposure, record, config, display="mpl"):
15  """ Display an image, the Cmodel, and residuals
16 
17  Parameters
18  ----------
19  exposure : `lsst.afw.image.Exposure`
20  Exposure object that contains the source which was modeled
21  record : `lsst.afw.table.SourceRecord`
22  Record object which contains the measurements made on the source
23  config : `lsst.meas.modelfit.CModel(SingleFrame/Forced)Config`
24  Configuration object of the CModel plugin used in the measurement
25  process
26  display : `str`, optional
27  Display in which to render the data, may be mpl for matplotlib or afw
28  for afwDisplay, defaults to mpl
29 
30  Raises
31  ------
32  `NotImplementedError`
33  If the display backend specified is not implemented
34  """
35 
36  if display is "mpl":
37  displayReconstrucedCmodelMpl(exposure, record, config)
38  elif display is "afw":
39  raise NotImplementedError("The afw display backend is not yet "
40  "implemented")
41  else:
42  raise NotImplementedError("The display backend '{}' is not a supported"
43  "backend".format(display))
44 
45 
46 def displayReconstrucedCmodelMpl(exposure, record, config):
47  """ Display an image, the Cmodel, and residuals using matplotlib
48 
49  Parameters
50  ----------
51  exposure : `lsst.afw.image.Exposure`
52  Exposure object that contains the source which was modeled
53  record : `lsst.afw.table.SourceRecord`
54  Record object which contains the measurements made on the source
55  config : `lsst.meas.modelfit.CModel(SingleFrame/Forced)Config`
56  Configuration object of the CModel plugin used in the measurement
57  process
58  """
59 
60  subImage, devIm, expIm, jointIm = buildCModelImages(exposure, record,
61  config)
62  # Get the min and max values for the sub image to use for scaling
63  subImMin = subImage.array.min()
64  subImMax = subImage.array.max()
65 
66  # Calculate the scaling to use for the residual images
67  devResidual = subImage.array-devIm.array
68  expResidual = subImage.array-expIm.array
69  jointResidual = subImage.array-jointIm.array
70  residualList = (devResidual, expResidual, jointResidual)
71 
72  differences = [(x.max()-x.max()) for x in residualList]
73  maxDifferenceIndex = np.argmax(differences)
74  resImMin = residualList[maxDifferenceIndex].min()
75  resImMax = residualList[maxDifferenceIndex].max()
76 
77  # Build the image figure
78  fig, axes = plt.subplots(3, 3, sharex='col', sharey='row', figsize=(8, 5))
79  fig.subplots_adjust(left=0.25, right=0.8)
80  lCBar = fig.add_axes([0.1, 0.15, 0.05, 0.7])
81  rCBar = fig.add_axes([0.85, 0.15, 0.05, 0.7])
82 
83  # Populate just the exposures in the appropriate places
84  for i in range(3):
85  axes[i, 0].imshow(subImage.array, vmin=subImMin, vmax=subImMax)
86 
87  # Populate dev panels
88  axes[0, 1].imshow(devIm.array, vmin=subImMin, vmax=subImMax)
89  axes[0, 2].imshow(devResidual, vmin=resImMin, vmax=resImMax, cmap="BrBG")
90 
91  # Populate exp panels
92  axes[1, 1].imshow(expIm.array, vmin=subImMin, vmax=subImMax)
93  axes[1, 2].imshow(expResidual, vmin=resImMin, vmax=resImMax, cmap="BrBG")
94 
95  # Populate joint panels
96  axes[2, 1].imshow(jointIm.array, vmin=subImMin, vmax=subImMax)
97  axes[2, 2].imshow(jointResidual, vmin=resImMin, vmax=resImMax, cmap="BrBG")
98 
99  axes[0, 0].set_title("Image")
100  axes[0, 1].set_title("Model")
101  axes[0, 2].set_title('Residuals')
102 
103  axes[0, 0].set_ylabel("Dev")
104  axes[1, 0].set_ylabel("Exp")
105  axes[2, 0].set_ylabel("Joint")
106 
107  fig.colorbar(axes[0, 0].get_images()[0], lCBar)
108  lCBar.yaxis.set_ticks_position('left')
109  fig.colorbar(axes[maxDifferenceIndex, 2].get_images()[0], rCBar)
110 
111  plt.show()
112 
113 
114 def buildCModelImages(exposure, record, config):
115  """ Create Images out of the CModel for the given record
116 
117  Parameters
118  ----------
119  exposure : `lsst.afw.image.Exposure`
120  Exposure object that contains the source which was modeled
121  record : `lsst.afw.table.SourceRecord`
122  Record object which contains the measurements made on the source
123  config : `lsst.meas.modelfit.CModel(SingleFrame/Forced)Config`
124  Configuration object of the CModel plugin used in the measurement
125  process
126 
127  Returns
128  -------
129  subImage : `lsst.afw.image.ImageF`
130  Sub image of the original data taken from a region defined by the
131  bounding box of the footprint for the object defined in the given
132  source record
133  devIm : `lsst.afw.image.ImageF`
134  Image created from the dev component of the CModel for the supplied
135  record at the same pixel locations as subImage
136  expIm: `lsst.afw.image.ImageF`
137  Image created from the exp component of the CModel for the supplied
138  record at the same pixel locations as subImage
139  jointIm :
140  Image created from the joint fit of the dev and exp components of the
141  CModel for the supplied record at the same pixel locations as subImage
142  """
143 
144  dev, exp, jointDev, jointExp = reconstructCModel(exposure, record, config)
145  # Get exposure cutout
146  footBBox = record.getFootprint().getBBox()
147  subImage = afwImage.ImageF(exposure.getImage(), footBBox)
148 
149  # Build the psf
150  shapeletPsfKey = shapelet.MultiShapeletFunctionKey(
151  record.schema[config.psfName])
152  psfApprox = record.get(shapeletPsfKey)
153 
154  # Build the dev Image from the shapelet function
155  devIm = afwImage.ImageF(footBBox)
156  dev = dev.convolve(psfApprox)
157  dev.evaluate().addToImage(devIm)
158 
159  # Build the exp image from the shapelet function
160  expIm = afwImage.ImageF(footBBox)
161  exp = exp.convolve(psfApprox)
162  exp.evaluate().addToImage(expIm)
163 
164  # Build the joint image from the shapelet function
165  jointIm = afwImage.ImageF(footBBox)
166  jointDev = jointDev.convolve(psfApprox)
167  jointExp = jointExp.convolve(psfApprox)
168  jointDev.evaluate().addToImage(jointIm)
169  jointExp.evaluate().addToImage(jointIm)
170 
171  return subImage, devIm, expIm, jointIm
172 
173 
174 def reconstructCModel(exposure, record, config):
175  """ Reconstruct the CModel for the given record
176 
177  Parameters
178  ----------
179  exposure : `lsst.afw.image.Exposure`
180  Exposure object that contains the source which was modeled
181  record : `lsst.afw.table.SourceRecord`
182  Record object which contains the measurements made on the source
183  config : `lsst.meas.modelfit.CModel(SingleFrame/Forced)Config`
184  Configuration object of the CModel plugin used in the measurement
185  process
186 
187  Returns
188  -------
189  devShapelet : `lsst.shapelet.MultiShapeletFunction`
190  Multi-component shapelet model of the dev component of CModel
191  expShapelet : `lsst.shapelet.MultiShapeletFunction`
192  Multi-component shapelet model fo the exp component of CModel
193  devJointShapelet : `lsst.shapelet.MultiShapeletFunction`
194  Multi-component shapelet model of the dev component of CModel jointly
195  fit with the exp component
196  expJointShapelet : `lsst.shapelet.MultiShapeletFunction`
197  Multi-component shapelet model of the exp component of Cmodel jointly
198  fit with the dev component
199  """
200 
201  # build a unit system transformation object
202  center = record.getCentroid()
203  position = exposure.getWcs().pixelToSky(center)
204  measSys = measMod.UnitSystem(exposure)
205  approxFlux = record.get("base_PsfFlux_flux")
206  fitSys = measMod.UnitSystem(position, exposure.getCalib(), approxFlux)
207  fitSysToMeasSys = measMod.LocalUnitTransform(center, fitSys, measSys)
208 
209  # Build the Shapelet objects
210  ctrl = config.makeControl()
211  baseName = "modelfit_CModel"
212  nonlinearKeys = ["{}_{{model}}_nonlinear_{p}".format(baseName, p=p)
213  for p in range(3)]
214  fixedKeys = ["{}_{{model}}_fixed_{p}".format(baseName, p=p)
215  for p in range(2)]
216  fluxKey = "{}_{{model}}_flux".format(baseName)
217 
218  # fetch the aperture corrections, if this fails set it to one
219  try:
220  apCorr = record.get("{}_apCorr".format(baseName))
221  except Exception:
222  print("Warning, problem retrieving aperture correction, using a value"
223  " of 1")
224  apCorr = 1
225 
226  # Get parameters for the dev model
227  devNonLinearParams = np.array([record.get(key.format(model="dev"))
228  for key in nonlinearKeys])
229  devFixedParams = np.array([record.get(key.format(model="dev"))
230  for key in fixedKeys])
231  devAmp = np.array([record.get(fluxKey.format(model="dev"))])
232  devAmp /= apCorr
233  devShapelet = ctrl.dev.getModel().makeShapeletFunction(devNonLinearParams,
234  devAmp,
235  devFixedParams)
236  devShapelet.transformInPlace(fitSysToMeasSys.geometric)
237 
238  # Get parameters for the exp model
239  expNonLinearParams = np.array([record.get(key.format(model="exp"))
240  for key in nonlinearKeys])
241  expFixedParams = np.array([record.get(key.format(model="exp"))
242  for key in fixedKeys])
243  expAmp = np.array([record.get(fluxKey.format(model="exp"))])
244  expAmp /= apCorr
245  expShapelet = ctrl.exp.getModel().makeShapeletFunction(expNonLinearParams,
246  expAmp,
247  expFixedParams)
248  expShapelet.transformInPlace(fitSysToMeasSys.geometric)
249 
250  # Get joint shapelet model
251  fracDev = record.get("{}_fracDev".format(baseName))
252  jointFlux = np.array([record.get("{}_flux".format(baseName))])
253  jointFlux /= apCorr
254  devJointShapelet = ctrl.dev.getModel()\
255  .makeShapeletFunction(devNonLinearParams, jointFlux*fracDev,
256  devFixedParams)
257  devJointShapelet.transformInPlace(fitSysToMeasSys.geometric)
258 
259  expJointShapelet = ctrl.exp.getModel()\
260  .makeShapeletFunction(expNonLinearParams, jointFlux*(1-fracDev),
261  expFixedParams)
262  expJointShapelet.transformInPlace(fitSysToMeasSys.geometric)
263 
264  return devShapelet, expShapelet, devJointShapelet, expJointShapelet
def displayReconstructedCmodel(exposure, record, config, display="mpl")
def reconstructCModel(exposure, record, config)
def displayReconstrucedCmodelMpl(exposure, record, config)
def buildCModelImages(exposure, record, config)