114 def determinePsf(self, exposure, psfCandidateList, metadata=None, flagKey=None):
115 """Determine a PSFEX PSF model for an exposure given a list of PSF
120 exposure: `lsst.afw.image.Exposure`
121 Exposure containing the PSF candidates.
122 psfCandidateList: iterable of `lsst.meas.algorithms.PsfCandidate`
123 Sequence of PSF candidates typically obtained by detecting sources
124 and then running them through a star selector.
125 metadata: metadata, optional
126 A home for interesting tidbits of information.
127 flagKey: `lsst.afw.table.Key`, optional
128 Schema key used to mark sources actually used in PSF determination.
132 psf: `lsst.meas.extensions.psfex.PsfexPsf`
138 displayExposure = display
and \
140 displayPsfComponents = display
and \
142 showBadCandidates = display
and \
144 displayResiduals = display
and \
146 displayPsfMosaic = display
and \
149 afwDisplay.setDefaultMaskTransparency(75)
152 mi = exposure.getMaskedImage()
154 nCand = len(psfCandidateList)
156 raise RuntimeError(
"No PSF candidates supplied.")
162 bbox = mi.getBBox(afwImage.PARENT)
163 psfCellSet = afwMath.SpatialCellSet(bbox, self.config.sizeCellX, self.config.sizeCellY)
167 sizes = np.empty(nCand)
168 for i, psfCandidate
in enumerate(psfCandidateList):
171 psfCellSet.insertCandidate(psfCandidate)
172 except Exception
as e:
173 self.log.error(
"Skipping PSF candidate %d of %d: %s", i, len(psfCandidateList), e)
176 source = psfCandidate.getSource()
177 quad = afwEll.Quadrupole(source.getIxx(), source.getIyy(), source.getIxy())
178 rmsSize = quad.getTraceRadius()
181 pixKernelSize = self.config.stampSize
182 actualKernelSize = int(2*np.floor(0.5*pixKernelSize/self.config.samplingSize) + 1)
185 rms = np.median(sizes)
186 self.log.debug(
"Median PSF RMS size=%.2f pixels (\"FWHM\"=%.2f)",
187 rms, 2*np.sqrt(2*np.log(2))*rms)
189 self.log.trace(
"Psfex Kernel size=%.2f, Image Kernel Size=%.2f", actualKernelSize, pixKernelSize)
195 defaultsFile = os.path.join(os.environ[
"MEAS_EXTENSIONS_PSFEX_DIR"],
"config",
"default-lsst.psfex")
196 args_md = dafBase.PropertySet()
197 args_md.set(
"BASIS_TYPE", str(self.config.psfexBasis))
198 args_md.set(
"PSFVAR_DEGREES", str(self.config.spatialOrder))
199 args_md.set(
"PSF_SIZE", str(actualKernelSize))
200 args_md.set(
"PSF_SAMPLING", str(self.config.samplingSize))
201 args_md.set(
"PHOTFLUX_KEY", str(self.config.photometricFluxField))
202 args_md.set(
"PHOTFLUXERR_KEY", str(self.config.photometricFluxField) +
"Err")
203 prefs = psfex.Prefs(defaultsFile, args_md)
204 prefs.setCommandLine([])
205 prefs.addCatalog(
"psfexPsfDeterminer")
208 principalComponentExclusionFlag = bool(bool(psfex.Context.REMOVEHIDDEN)
209 if False else psfex.Context.KEEPHIDDEN)
210 context = psfex.Context(prefs.getContextName(), prefs.getContextGroup(),
211 prefs.getGroupDeg(), principalComponentExclusionFlag)
212 psfSet = psfex.Set(context)
213 psfSet.setVigSize(pixKernelSize, pixKernelSize)
214 psfSet.setFwhm(2*np.sqrt(2*np.log(2))*np.median(sizes))
215 psfSet.setRecentroid(self.config.recentroid)
218 backnoise2 = afwMath.makeStatistics(mi.getImage(), afwMath.VARIANCECLIP).getValue()
219 ccd = exposure.getDetector()
221 gain = np.mean(np.array([a.getGain()
for a
in ccd]))
224 self.log.warning(
"Setting gain to %g", gain)
227 for i, key
in enumerate(context.getName()):
230 contextvalp.append(exposure.getMetadata().getScalar(key[1:]))
231 except KeyError
as e:
232 raise RuntimeError(
"%s parameter not found in the header of %s" %
233 (key[1:], prefs.getContextName()))
from e
236 contextvalp.append(np.array([psfCandidateList[_].getSource().get(key)
237 for _
in range(nCand)]))
238 except KeyError
as e:
239 raise RuntimeError(
"%s parameter not found" % (key,))
from e
240 psfSet.setContextname(i, key)
245 disp = afwDisplay.Display(frame=frame)
246 disp.mtv(exposure, title=
"psf determination")
248 badBits = mi.getMask().getPlaneBitMask(self.config.badMaskBits)
249 fluxName = prefs.getPhotfluxRkey()
250 fluxFlagName =
"base_" + fluxName +
"_flag"
253 for i, psfCandidate
in enumerate(psfCandidateList):
254 source = psfCandidate.getSource()
257 xc, yc = source.getX(), source.getY()
258 if not np.isfinite(xc)
or not np.isfinite(yc):
261 if fluxFlagName
in source.schema
and source.get(fluxFlagName):
264 flux = source.get(fluxName)
265 if flux < 0
or not np.isfinite(flux):
269 pstamp = psfCandidate.getMaskedImage(pixKernelSize, pixKernelSize).clone()
270 except pexExcept.LengthError:
271 self.log.warning(
"Could not get stamp image for psfCandidate: %s with kernel size: %s",
272 psfCandidate, pixKernelSize)
280 sample = psfSet.newSample()
281 sample.setCatindex(catindex)
282 sample.setExtindex(ext)
283 sample.setObjindex(i)
285 imArray = pstamp.getImage().getArray()
286 imArray[np.where(np.bitwise_and(pstamp.getMask().getArray(), badBits))] = \
288 sample.setVig(imArray)
291 sample.setBacknoise2(backnoise2)
295 sample.setFluxrad(sizes[i])
297 for j
in range(psfSet.getNcontext()):
298 sample.setContext(j, float(contextvalp[j][i]))
299 except Exception
as e:
300 self.log.error(
"Exception when processing sample at (%f,%f): %s", xc, yc, e)
303 psfSet.finiSample(sample)
309 with disp.Buffering():
310 disp.dot(
"o", xc, yc, ctype=afwDisplay.CYAN, size=4)
312 if psfSet.getNsample() == 0:
313 raise RuntimeError(
"No good PSF candidates to pass to PSFEx")
316 for i
in range(psfSet.getNcontext()):
317 cmin = contextvalp[i].min()
318 cmax = contextvalp[i].max()
319 psfSet.setContextScale(i, cmax - cmin)
320 psfSet.setContextOffset(i, (cmin + cmax)/2.0)
330 field = psfex.Field(
"Unknown")
331 field.addExt(exposure.getWcs(), exposure.getWidth(), exposure.getHeight(), psfSet.getNsample())
339 psfex.makeit(fields, sets)
340 psfs = field.getPsfs()
344 for i
in range(sets[0].getNsample()):
345 index = sets[0].getSample(i).getObjindex()
347 good_indices.append(index)
349 if flagKey
is not None:
350 for i, psfCandidate
in enumerate(psfCandidateList):
351 source = psfCandidate.getSource()
352 if i
in good_indices:
353 source.set(flagKey,
True)
355 xpos = np.array(xpos)
356 ypos = np.array(ypos)
357 numGoodStars = len(good_indices)
358 avgX, avgY = np.mean(xpos), np.mean(ypos)
368 _ = psf.getKernel(psf.getAveragePosition())
369 except pexExcept.InvalidParameterError:
370 raise RuntimeError(
"Failed to determine psfex psf: too few good stars.")
376 assert psfCellSet
is not None
379 maUtils.showPsfSpatialCells(exposure, psfCellSet, showChi2=
True,
380 symb=
"o", ctype=afwDisplay.YELLOW, ctypeBad=afwDisplay.RED,
381 size=8, display=disp)
383 disp4 = afwDisplay.Display(frame=4)
384 maUtils.showPsfCandidates(exposure, psfCellSet, psf=psf, display=disp4,
385 normalize=normalizeResiduals,
386 showBadCandidates=showBadCandidates)
387 if displayPsfComponents:
388 disp6 = afwDisplay.Display(frame=6)
389 maUtils.showPsf(psf, display=disp6)
391 disp7 = afwDisplay.Display(frame=7)
392 maUtils.showPsfMosaic(exposure, psf, display=disp7, showFwhm=
True)
393 disp.scale(
'linear', 0, 1)
399 if metadata
is not None:
400 metadata[
"spatialFitChi2"] = np.nan
401 metadata[
"numAvailStars"] = nCand
402 metadata[
"numGoodStars"] = numGoodStars
403 metadata[
"avgX"] = avgX
404 metadata[
"avgY"] = avgY
406 return psf, psfCellSet