332 """Return a selection of PSF candidates that represent likely stars.
334 A list of PSF candidates may be used by a PSF fitter to construct a PSF.
338 sourceCat : `lsst.afw.table.SourceCatalog`
339 Catalog of sources to select from.
340 This catalog must be contiguous in memory.
341 matches : `list` of `lsst.afw.table.ReferenceMatch` or None
342 Ignored in this SourceSelector.
343 exposure : `lsst.afw.image.Exposure` or None
344 The exposure the catalog was built from; used to get the detector
345 to transform to TanPix, and for debug display.
349 struct : `lsst.pipe.base.Struct`
350 The struct contains the following data:
353 Boolean array of sources that were selected, same length as
354 sourceCat. (`numpy.ndarray` of `bool`)
356 if len(sourceCat) == 0:
357 raise RuntimeError(
"Input catalog for source selection is empty.")
368 detector = exposure.getDetector()
370 pixToTanPix = detector.getTransform(PIXELS, TAN_PIXELS)
374 flux = sourceCat[self.config.sourceFluxField]
375 fluxErr = sourceCat[self.config.sourceFluxField +
"Err"]
377 xx = numpy.empty(len(sourceCat))
378 xy = numpy.empty_like(xx)
379 yy = numpy.empty_like(xx)
380 for i, source
in enumerate(sourceCat):
381 Ixx, Ixy, Iyy = source.getIxx(), source.getIxy(), source.getIyy()
386 m.transform(linTransform)
387 Ixx, Iyy, Ixy = m.getIxx(), m.getIyy(), m.getIxy()
389 xx[i], xy[i], yy[i] = Ixx, Ixy, Iyy
391 width = numpy.sqrt(0.5*(xx + yy))
392 with numpy.errstate(invalid=
"ignore"):
393 bad = reduce(
lambda x, y: numpy.logical_or(x, sourceCat[y]), self.config.badFlags,
False)
394 bad = numpy.logical_or(bad, numpy.logical_not(numpy.isfinite(width)))
395 bad = numpy.logical_or(bad, numpy.logical_not(numpy.isfinite(flux)))
396 if self.config.doFluxLimit:
397 bad = numpy.logical_or(bad, flux < self.config.fluxMin)
398 if self.config.fluxMax > 0:
399 bad = numpy.logical_or(bad, flux > self.config.fluxMax)
400 if self.config.doSignalToNoiseLimit:
401 bad = numpy.logical_or(bad, flux/fluxErr < self.config.signalToNoiseMin)
402 if self.config.signalToNoiseMax > 0:
403 bad = numpy.logical_or(bad, flux/fluxErr > self.config.signalToNoiseMax)
404 bad = numpy.logical_or(bad, width < self.config.widthMin)
405 bad = numpy.logical_or(bad, width > self.config.widthMax)
406 good = numpy.logical_not(bad)
408 if not numpy.any(good):
409 raise RuntimeError(
"No objects passed our cuts for consideration as psf stars")
411 mag = -2.5*numpy.log10(flux[good])
419 import pickle
as pickle
422 pickleFile = os.path.expanduser(os.path.join(
"~",
"widths-%d.pkl" % _ii))
423 if not os.path.exists(pickleFile):
427 with open(pickleFile,
"wb")
as fd:
428 pickle.dump(mag, fd, -1)
429 pickle.dump(width, fd, -1)
431 centers, clusterId =
_kcenters(width, nCluster=4, useMedian=
True,
432 widthStdAllowed=self.config.widthStdAllowed)
434 if display
and plotMagSize:
435 fig =
plot(mag, width, centers, clusterId,
436 magType=self.config.sourceFluxField.split(
".")[-1].title(),
437 marker=
"+", markersize=3, markeredgewidth=
None, ltype=
':', clear=
True)
442 nsigma=self.config.nSigmaClip,
443 widthStdAllowed=self.config.widthStdAllowed)
445 if display
and plotMagSize:
446 plot(mag, width, centers, clusterId, marker=
"x", markersize=3, markeredgewidth=
None, clear=
False)
448 stellar = (clusterId == 0)
455 if display
and displayExposure:
456 disp = afwDisplay.Display(frame=frame)
457 disp.mtv(exposure.getMaskedImage(), title=
"PSF candidates")
460 eventHandler =
EventHandler(fig.get_axes()[0], mag, width,
461 sourceCat.getX()[good], sourceCat.getY()[good], frames=[frame])
467 reply = input(
"continue? [c h(elp) q(uit) p(db)] ").strip()
476 We cluster the points; red are the stellar candidates and the other colours are other clusters.
477 Points labelled + are rejects from the cluster (only for cluster 0).
479 At this prompt, you can continue with almost any key; 'p' enters pdb, and 'h' prints this text
481 If displayExposure is true, you can put the cursor on a point and hit 'p' to see it in the
484 elif reply[0] ==
"p":
487 elif reply[0] ==
'q':
492 if display
and displayExposure:
493 mi = exposure.getMaskedImage()
494 with disp.Buffering():
495 for i, source
in enumerate(sourceCat):
497 ctype = afwDisplay.GREEN
499 ctype = afwDisplay.RED
501 disp.dot(
"+", source.getX() - mi.getX0(), source.getY() - mi.getY0(), ctype=ctype)
507 return Struct(selected=good)