1 from __future__
import division, print_function, absolute_import
3 __all__ = (
"displayReconstructedCmodel",
"displayReconstrucedCmodelMpl",
4 "buildCModelImages",
"reconstructCModel")
7 import matplotlib.pyplot
as plt
9 import lsst.afw.image
as afwImage
11 import lsst.shapelet
as shapelet
15 """ Display an image, the Cmodel, and residuals
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
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
33 If the display backend specified is not implemented
38 elif display
is "afw":
39 raise NotImplementedError(
"The afw display backend is not yet "
42 raise NotImplementedError(
"The display backend '{}' is not a supported"
43 "backend".format(display))
47 """ Display an image, the Cmodel, and residuals using matplotlib
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
63 subImMin = subImage.array.min()
64 subImMax = subImage.array.max()
67 devResidual = subImage.array-devIm.array
68 expResidual = subImage.array-expIm.array
69 jointResidual = subImage.array-jointIm.array
70 residualList = (devResidual, expResidual, jointResidual)
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()
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])
85 axes[i, 0].imshow(subImage.array, vmin=subImMin, vmax=subImMax)
88 axes[0, 1].imshow(devIm.array, vmin=subImMin, vmax=subImMax)
89 axes[0, 2].imshow(devResidual, vmin=resImMin, vmax=resImMax, cmap=
"BrBG")
92 axes[1, 1].imshow(expIm.array, vmin=subImMin, vmax=subImMax)
93 axes[1, 2].imshow(expResidual, vmin=resImMin, vmax=resImMax, cmap=
"BrBG")
96 axes[2, 1].imshow(jointIm.array, vmin=subImMin, vmax=subImMax)
97 axes[2, 2].imshow(jointResidual, vmin=resImMin, vmax=resImMax, cmap=
"BrBG")
99 axes[0, 0].set_title(
"Image")
100 axes[0, 1].set_title(
"Model")
101 axes[0, 2].set_title(
'Residuals')
103 axes[0, 0].set_ylabel(
"Dev")
104 axes[1, 0].set_ylabel(
"Exp")
105 axes[2, 0].set_ylabel(
"Joint")
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)
115 """ Create Images out of the CModel for the given record
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
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
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
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
146 footBBox = record.getFootprint().getBBox()
147 subImage = afwImage.ImageF(exposure.getImage(), footBBox)
150 shapeletPsfKey = shapelet.MultiShapeletFunctionKey(
151 record.schema[config.psfName])
152 psfApprox = record.get(shapeletPsfKey)
155 devIm = afwImage.ImageF(footBBox)
156 dev = dev.convolve(psfApprox)
157 dev.evaluate().addToImage(devIm)
160 expIm = afwImage.ImageF(footBBox)
161 exp = exp.convolve(psfApprox)
162 exp.evaluate().addToImage(expIm)
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)
171 return subImage, devIm, expIm, jointIm
175 """ Reconstruct the CModel for the given record
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
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
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(position, fitSys, measSys)
210 ctrl = config.makeControl()
211 baseName =
"modelfit_CModel"
212 nonlinearKeys = [
"{}_{{model}}_nonlinear_{p}".format(baseName, p=p)
214 fixedKeys = [
"{}_{{model}}_fixed_{p}".format(baseName, p=p)
216 fluxKey =
"{}_{{model}}_flux".format(baseName)
220 apCorr = record.get(
"{}_apCorr".format(baseName))
222 print(
"Warning, problem retrieving aperture correction, using a value"
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"))])
233 devShapelet = ctrl.dev.getModel().makeShapeletFunction(devNonLinearParams,
236 devShapelet.transformInPlace(fitSysToMeasSys.geometric)
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"))])
245 expShapelet = ctrl.exp.getModel().makeShapeletFunction(expNonLinearParams,
248 expShapelet.transformInPlace(fitSysToMeasSys.geometric)
251 fracDev = record.get(
"{}_fracDev".format(baseName))
252 jointFlux = np.array([record.get(
"{}_flux".format(baseName))])
254 devJointShapelet = ctrl.dev.getModel()\
255 .makeShapeletFunction(devNonLinearParams, jointFlux*fracDev,
257 devJointShapelet.transformInPlace(fitSysToMeasSys.geometric)
259 expJointShapelet = ctrl.exp.getModel()\
260 .makeShapeletFunction(expNonLinearParams, jointFlux*(1-fracDev),
262 expJointShapelet.transformInPlace(fitSysToMeasSys.geometric)
264 return devShapelet, expShapelet, devJointShapelet, expJointShapelet
def displayReconstructedCmodel
def displayReconstrucedCmodelMpl