38 spatialOrder = pexConfig.Field(
39 doc=
"specify spatial order for PSF kernel creation",
42 check=
lambda x: x >= 0,
44 sizeCellX = pexConfig.Field(
45 doc=
"size of cell used to determine PSF (pixels, column direction)",
49 check=
lambda x: x >= 10,
51 sizeCellY = pexConfig.Field(
52 doc=
"size of cell used to determine PSF (pixels, row direction)",
54 default=sizeCellX.default,
56 check=
lambda x: x >= 10,
58 samplingSize = pexConfig.Field(
59 doc=
"Resolution of the internal PSF model relative to the pixel size; "
60 "e.g. 0.5 is equal to 2x oversampling",
64 badMaskBits = pexConfig.ListField(
65 doc=
"List of mask bits which cause a source to be rejected as bad "
66 "N.b. INTRP is used specially in PsfCandidateSet; it means \"Contaminated by neighbour\"",
68 default=[
"INTRP",
"SAT"],
70 psfexBasis = pexConfig.ChoiceField(
71 doc=
"BASIS value given to psfex. PIXEL_AUTO will use the requested samplingSize only if "
72 "the FWHM < 3 pixels. Otherwise, it will use samplingSize=1. PIXEL will always use the "
73 "requested samplingSize",
76 "PIXEL":
"Always use requested samplingSize",
77 "PIXEL_AUTO":
"Only use requested samplingSize when FWHM < 3",
82 tolerance = pexConfig.Field(
83 doc=
"tolerance of spatial fitting",
87 lam = pexConfig.Field(
88 doc=
"floor for variance is lam*data",
92 reducedChi2ForPsfCandidates = pexConfig.Field(
93 doc=
"for psf candidate evaluation",
97 spatialReject = pexConfig.Field(
98 doc=
"Rejection threshold (stdev) for candidates based on spatial fit",
102 recentroid = pexConfig.Field(
103 doc=
"Should PSFEX be permitted to recentroid PSF candidates?",
113 ConfigClass = PsfexPsfDeterminerConfig
115 def determinePsf(self, exposure, psfCandidateList, metadata=None, flagKey=None):
116 """Determine a PSFEX PSF model for an exposure given a list of PSF
121 exposure: `lsst.afw.image.Exposure`
122 Exposure containing the PSF candidates.
123 psfCandidateList: iterable of `lsst.meas.algorithms.PsfCandidate`
124 Sequence of PSF candidates typically obtained by detecting sources
125 and then running them through a star selector.
126 metadata: metadata, optional
127 A home for interesting tidbits of information.
128 flagKey: `lsst.afw.table.Key`, optional
129 Schema key used to mark sources actually used in PSF determination.
133 psf: `lsst.meas.extensions.psfex.PsfexPsf`
139 displayExposure = display
and \
141 displayPsfComponents = display
and \
143 showBadCandidates = display
and \
145 displayResiduals = display
and \
147 displayPsfMosaic = display
and \
150 afwDisplay.setDefaultMaskTransparency(75)
153 mi = exposure.getMaskedImage()
155 nCand = len(psfCandidateList)
157 raise RuntimeError(
"No PSF candidates supplied.")
163 bbox = mi.getBBox(afwImage.PARENT)
164 psfCellSet = afwMath.SpatialCellSet(bbox, self.config.sizeCellX, self.config.sizeCellY)
168 sizes = np.empty(nCand)
169 for i, psfCandidate
in enumerate(psfCandidateList):
172 psfCellSet.insertCandidate(psfCandidate)
173 except Exception
as e:
174 self.log.error(
"Skipping PSF candidate %d of %d: %s", i, len(psfCandidateList), e)
177 source = psfCandidate.getSource()
178 quad = afwEll.Quadrupole(source.getIxx(), source.getIyy(), source.getIxy())
179 rmsSize = quad.getTraceRadius()
182 if self.config.kernelSize >= 15:
183 self.log.warn(
"NOT scaling kernelSize by stellar quadrupole moment, but using absolute value")
184 actualKernelSize = int(self.config.kernelSize)
186 actualKernelSize = 2 * int(self.config.kernelSize * np.sqrt(np.median(sizes)) + 0.5) + 1
187 if actualKernelSize < self.config.kernelSizeMin:
188 actualKernelSize = self.config.kernelSizeMin
189 if actualKernelSize > self.config.kernelSizeMax:
190 actualKernelSize = self.config.kernelSizeMax
192 rms = np.median(sizes)
193 msg =
"Median PSF RMS size=%.2f pixels (\"FWHM\"=%.2f)" % (rms, 2*np.sqrt(2*np.log(2))*rms)
198 pixKernelSize = actualKernelSize
199 if self.config.samplingSize > 0:
200 pixKernelSize = int(actualKernelSize*self.config.samplingSize)
201 if pixKernelSize % 2 == 0:
203 self.log.trace(
"Psfex Kernel size=%.2f, Image Kernel Size=%.2f", actualKernelSize, pixKernelSize)
204 psfCandidateList[0].setHeight(pixKernelSize)
205 psfCandidateList[0].setWidth(pixKernelSize)
211 defaultsFile = os.path.join(os.environ[
"MEAS_EXTENSIONS_PSFEX_DIR"],
"config",
"default-lsst.psfex")
212 args_md = dafBase.PropertySet()
213 args_md.set(
"BASIS_TYPE", str(self.config.psfexBasis))
214 args_md.set(
"PSFVAR_DEGREES", str(self.config.spatialOrder))
215 args_md.set(
"PSF_SIZE", str(actualKernelSize))
216 args_md.set(
"PSF_SAMPLING", str(self.config.samplingSize))
217 prefs = psfex.Prefs(defaultsFile, args_md)
218 prefs.setCommandLine([])
219 prefs.addCatalog(
"psfexPsfDeterminer")
222 principalComponentExclusionFlag = bool(bool(psfex.Context.REMOVEHIDDEN)
223 if False else psfex.Context.KEEPHIDDEN)
224 context = psfex.Context(prefs.getContextName(), prefs.getContextGroup(),
225 prefs.getGroupDeg(), principalComponentExclusionFlag)
226 psfSet = psfex.Set(context)
227 psfSet.setVigSize(pixKernelSize, pixKernelSize)
228 psfSet.setFwhm(2*np.sqrt(2*np.log(2))*np.median(sizes))
229 psfSet.setRecentroid(self.config.recentroid)
232 backnoise2 = afwMath.makeStatistics(mi.getImage(), afwMath.VARIANCECLIP).getValue()
233 ccd = exposure.getDetector()
235 gain = np.mean(np.array([a.getGain()
for a
in ccd]))
238 self.log.warn(
"Setting gain to %g" % (gain,))
241 for i, key
in enumerate(context.getName()):
244 contextvalp.append(exposure.getMetadata().getScalar(key[1:]))
245 except KeyError
as e:
246 raise RuntimeError(
"%s parameter not found in the header of %s" %
247 (key[1:], prefs.getContextName()))
from e
250 contextvalp.append(np.array([psfCandidateList[_].getSource().get(key)
251 for _
in range(nCand)]))
252 except KeyError
as e:
253 raise RuntimeError(
"%s parameter not found" % (key,))
from e
254 psfSet.setContextname(i, key)
259 disp = afwDisplay.Display(frame=frame)
260 disp.mtv(exposure, title=
"psf determination")
262 badBits = mi.getMask().getPlaneBitMask(self.config.badMaskBits)
263 fluxName = prefs.getPhotfluxRkey()
264 fluxFlagName =
"base_" + fluxName +
"_flag"
267 for i, psfCandidate
in enumerate(psfCandidateList):
268 source = psfCandidate.getSource()
271 xc, yc = source.getX(), source.getY()
272 if not np.isfinite(xc)
or not np.isfinite(yc):
275 if fluxFlagName
in source.schema
and source.get(fluxFlagName):
278 flux = source.get(fluxName)
279 if flux < 0
or not np.isfinite(flux):
282 pstamp = psfCandidate.getMaskedImage().clone()
289 sample = psfSet.newSample()
290 sample.setCatindex(catindex)
291 sample.setExtindex(ext)
292 sample.setObjindex(i)
294 imArray = pstamp.getImage().getArray()
295 imArray[np.where(np.bitwise_and(pstamp.getMask().getArray(), badBits))] = \
297 sample.setVig(imArray)
300 sample.setBacknoise2(backnoise2)
304 sample.setFluxrad(sizes[i])
306 for j
in range(psfSet.getNcontext()):
307 sample.setContext(j, float(contextvalp[j][i]))
308 except Exception
as e:
309 self.log.error(
"Exception when processing sample at (%f,%f): %s", xc, yc, e)
312 psfSet.finiSample(sample)
318 with disp.Buffering():
319 disp.dot(
"o", xc, yc, ctype=afwDisplay.CYAN, size=4)
321 if psfSet.getNsample() == 0:
322 raise RuntimeError(
"No good PSF candidates to pass to PSFEx")
325 for i
in range(psfSet.getNcontext()):
326 cmin = contextvalp[i].min()
327 cmax = contextvalp[i].max()
328 psfSet.setContextScale(i, cmax - cmin)
329 psfSet.setContextOffset(i, (cmin + cmax)/2.0)
339 field = psfex.Field(
"Unknown")
340 field.addExt(exposure.getWcs(), exposure.getWidth(), exposure.getHeight(), psfSet.getNsample())
348 psfex.makeit(fields, sets)
349 psfs = field.getPsfs()
353 for i
in range(sets[0].getNsample()):
354 index = sets[0].getSample(i).getObjindex()
356 good_indices.append(index)
358 if flagKey
is not None:
359 for i, psfCandidate
in enumerate(psfCandidateList):
360 source = psfCandidate.getSource()
361 if i
in good_indices:
362 source.set(flagKey,
True)
364 xpos = np.array(xpos)
365 ypos = np.array(ypos)
366 numGoodStars = len(good_indices)
367 avgX, avgY = np.mean(xpos), np.mean(ypos)
375 assert psfCellSet
is not None
378 maUtils.showPsfSpatialCells(exposure, psfCellSet, showChi2=
True,
379 symb=
"o", ctype=afwDisplay.YELLOW, ctypeBad=afwDisplay.RED,
380 size=8, display=disp)
382 disp4 = afwDisplay.Display(frame=4)
383 maUtils.showPsfCandidates(exposure, psfCellSet, psf=psf, display=disp4,
384 normalize=normalizeResiduals,
385 showBadCandidates=showBadCandidates)
386 if displayPsfComponents:
387 disp6 = afwDisplay.Display(frame=6)
388 maUtils.showPsf(psf, display=disp6)
390 disp7 = afwDisplay.Display(frame=7)
391 maUtils.showPsfMosaic(exposure, psf, display=disp7, showFwhm=
True)
392 disp.scale(
'linear', 0, 1)
398 if metadata
is not None:
399 metadata.set(
"spatialFitChi2", np.nan)
400 metadata.set(
"numAvailStars", nCand)
401 metadata.set(
"numGoodStars", numGoodStars)
402 metadata.set(
"avgX", avgX)
403 metadata.set(
"avgY", avgY)
405 return psf, psfCellSet
408 measAlg.psfDeterminerRegistry.register(
"psfex", PsfexPsfDeterminerTask)