256 """Return a selection of psf-like objects.
260 sourceCat : `lsst.afw.table.SourceCatalog`
261 Catalog of sources to select from.
262 This catalog must be contiguous in memory.
263 matches : `list` of `lsst.afw.table.ReferenceMatch` or None
264 Ignored by this source selector.
265 exposure : `lsst.afw.image.Exposure` or None
266 The exposure the catalog was built from; used for debug display.
270 struct : `lsst.pipe.base.Struct`
271 The struct contains the following data:
273 - selected : `numpy.ndarray` of `bool``
274 Boolean array of sources that were selected, same length as
280 displayExposure = display
and \
282 plotFwhmHistogram = display
and plt
and \
284 plotFlags = display
and plt
and \
286 plotRejection = display
and plt
and \
288 afwDisplay.setDefaultMaskTransparency(75)
290 fluxName = self.config.fluxName
291 fluxErrName = self.config.fluxErrName
292 minFwhm = self.config.minFwhm
293 maxFwhm = self.config.maxFwhm
294 maxFwhmVariability = self.config.maxFwhmVariability
295 maxellip = self.config.maxellip
296 minsn = self.config.minsn
298 maxelong = (maxellip + 1.0)/(1.0 - maxellip)
if maxellip < 1.0
else 100
301 shape = sourceCat.getShapeDefinition()
302 ixx = sourceCat.get(
"%s.xx" % shape)
303 iyy = sourceCat.get(
"%s.yy" % shape)
305 fwhm = 2*np.sqrt(2*np.log(2))*np.sqrt(0.5*(ixx + iyy))
306 elong = 0.5*(ixx - iyy)/(ixx + iyy)
308 flux = sourceCat.get(fluxName)
309 fluxErr = sourceCat.get(fluxErrName)
310 sn = flux/np.where(fluxErr > 0, fluxErr, 1)
311 sn[fluxErr <= 0] = -psfexLib.BIG
314 for i, f
in enumerate(self.config.badFlags):
315 flags = np.bitwise_or(flags, np.where(sourceCat.get(f), 1 << i, 0))
319 good = np.logical_and(sn > minsn, np.logical_not(flags))
320 good = np.logical_and(good, elong < maxelong)
321 good = np.logical_and(good, fwhm >= minFwhm)
322 good = np.logical_and(good, fwhm < maxFwhm)
324 fwhmMode, fwhmMin, fwhmMax = compute_fwhmrange(fwhm[good], maxFwhmVariability, minFwhm, maxFwhm,
325 plot=dict(fwhmHistogram=plotFwhmHistogram))
337 selectionVectors = []
338 selectionVectors.append((bad,
"flags %d" % sum(bad)))
342 bad = np.logical_or(bad, dbad)
344 selectionVectors.append((dbad,
"S/N %d" % sum(dbad)))
346 dbad = fwhm < fwhmMin
348 bad = np.logical_or(bad, dbad)
350 selectionVectors.append((dbad,
"fwhmMin %d" % sum(dbad)))
352 dbad = fwhm > fwhmMax
354 bad = np.logical_or(bad, dbad)
356 selectionVectors.append((dbad,
"fwhmMax %d" % sum(dbad)))
358 dbad = elong > maxelong
360 bad = np.logical_or(bad, dbad)
362 selectionVectors.append((dbad,
"elong %d" % sum(dbad)))
364 good = np.logical_not(bad)
370 mi = exposure.getMaskedImage()
371 disp = afwDisplay.Display(frame=frame)
372 disp.mtv(mi, title=
"PSF candidates")
374 with disp.Buffering():
375 for i, source
in enumerate(sourceCat):
377 ctype = afwDisplay.GREEN
379 ctype = afwDisplay.RED
381 disp.dot(
"+", source.getX() - mi.getX0(), source.getY() - mi.getY0(),
384 if plotFlags
or plotRejection:
385 imag = -2.5*np.log10(flux)
390 isSet = np.where(flags == 0x0)[0]
391 plt.plot(imag[isSet], fwhm[isSet],
'o', alpha=alpha, label=
"good")
393 for i, f
in enumerate(self.config.badFlags):
395 isSet = np.where(np.bitwise_and(flags, mask))[0]
397 if np.isfinite(imag[isSet] + fwhm[isSet]).any():
398 label = re.sub(
r"\_flag",
"",
399 re.sub(
r"^base\_",
"",
400 re.sub(
r"^.*base\_PixelFlags\_flag\_",
"", f)))
401 plt.plot(imag[isSet], fwhm[isSet],
'o', alpha=alpha, label=label)
403 for bad, label
in selectionVectors:
404 plt.plot(imag[bad], fwhm[bad],
'o', alpha=alpha, label=label)
406 plt.plot(imag[good], fwhm[good],
'o', color=
"black", label=
"selected")
407 [plt.axhline(_, color=
'red')
for _
in [fwhmMin, fwhmMax]]
408 plt.xlim(np.median(imag[good]) + 5*np.array([-1, 1]))
409 plt.ylim(fwhm[np.where(np.isfinite(fwhm + imag))].min(), 2*fwhmMax)
411 plt.xlabel(
"Instrumental %s Magnitude" % fluxName.split(
".")[-1].title())
413 title =
"PSFEX Star Selection"
414 plt.title(
"%s %d selected" % (title, sum(good)))
418 eventHandler =
EventHandler(plt.axes(), imag, fwhm, sourceCat.getX(), sourceCat.getY(),
421 if plotFlags
or plotRejection:
424 reply = input(
"continue? [y[es] h(elp) p(db) q(uit)] ").strip()
433At this prompt, you can continue with almost any key; 'p' enters pdb,
434 'q' returns to the shell, and
440If you put the cursor on a point in the matplotlib scatter plot and hit 'p' you'll see it in ds9.""")
441 elif reply[0] ==
"p":
444 elif reply[0] ==
'q':
449 return Struct(selected=good)