363 """Return a selection of PSF candidates that represent likely stars.
365 A list of PSF candidates may be used by a PSF fitter to construct a PSF.
369 sourceCat : `lsst.afw.table.SourceCatalog`
370 Catalog of sources to select from.
371 This catalog must be contiguous in memory.
372 matches : `list` of `lsst.afw.table.ReferenceMatch` or None
373 Ignored in this SourceSelector.
374 exposure : `lsst.afw.image.Exposure` or None
375 The exposure the catalog was built from; used to get the detector
376 to transform to TanPix, and for debug display.
380 struct : `lsst.pipe.base.Struct`
381 The struct contains the following data:
384 Boolean array of sources that were selected, same length as
385 sourceCat. (`numpy.ndarray` of `bool`)
387 if len(sourceCat) == 0:
388 raise ObjectSizeNoSourcesError
399 detector = exposure.getDetector()
401 pixToTanPix = detector.getTransform(PIXELS, TAN_PIXELS)
405 flux = sourceCat[self.config.sourceFluxField]
406 fluxErr = sourceCat[self.config.sourceFluxField +
"Err"]
408 xx = numpy.empty(len(sourceCat))
409 xy = numpy.empty_like(xx)
410 yy = numpy.empty_like(xx)
411 for i, source
in enumerate(sourceCat):
412 Ixx, Ixy, Iyy = source.getIxx(), source.getIxy(), source.getIyy()
417 m.transform(linTransform)
418 Ixx, Iyy, Ixy = m.getIxx(), m.getIyy(), m.getIxy()
420 xx[i], xy[i], yy[i] = Ixx, Ixy, Iyy
422 width = numpy.sqrt(0.5*(xx + yy))
423 with numpy.errstate(invalid=
"ignore"):
424 bad = reduce(
lambda x, y: numpy.logical_or(x, sourceCat[y]), self.config.badFlags,
False)
425 bad = numpy.logical_or(bad, numpy.logical_not(numpy.isfinite(width)))
426 bad = numpy.logical_or(bad, numpy.logical_not(numpy.isfinite(flux)))
427 if self.config.doFluxLimit:
428 bad = numpy.logical_or(bad, flux < self.config.fluxMin)
429 if self.config.fluxMax > 0:
430 bad = numpy.logical_or(bad, flux > self.config.fluxMax)
431 if self.config.doSignalToNoiseLimit:
432 bad = numpy.logical_or(bad, flux/fluxErr < self.config.signalToNoiseMin)
433 if self.config.signalToNoiseMax > 0:
434 bad = numpy.logical_or(bad, flux/fluxErr > self.config.signalToNoiseMax)
435 bad = numpy.logical_or(bad, width < self.config.widthMin)
436 bad = numpy.logical_or(bad, width > self.config.widthMax)
437 good = numpy.logical_not(bad)
439 if not numpy.any(good):
442 mag = -2.5*numpy.log10(flux[good])
450 import pickle
as pickle
453 pickleFile = os.path.expanduser(os.path.join(
"~",
"widths-%d.pkl" % _ii))
454 if not os.path.exists(pickleFile):
458 with open(pickleFile,
"wb")
as fd:
459 pickle.dump(mag, fd, -1)
460 pickle.dump(width, fd, -1)
462 centers, clusterId =
_kcenters(width, nCluster=4, useMedian=
True,
463 widthStdAllowed=self.config.widthStdAllowed)
465 if display
and plotMagSize:
466 fig =
plot(mag, width, centers, clusterId,
467 magType=self.config.sourceFluxField.split(
".")[-1].title(),
468 marker=
"+", markersize=3, markeredgewidth=
None, ltype=
':', clear=
True)
473 nsigma=self.config.nSigmaClip,
474 widthStdAllowed=self.config.widthStdAllowed)
476 if display
and plotMagSize:
477 plot(mag, width, centers, clusterId, marker=
"x", markersize=3, markeredgewidth=
None, clear=
False)
479 stellar = (clusterId == 0)
486 if display
and displayExposure:
487 disp = afwDisplay.Display(frame=frame)
488 disp.mtv(exposure.getMaskedImage(), title=
"PSF candidates")
491 eventHandler =
EventHandler(fig.get_axes()[0], mag, width,
492 sourceCat.getX()[good], sourceCat.getY()[good], frames=[frame])
498 reply = input(
"continue? [c h(elp) q(uit) p(db)] ").strip()
507 We cluster the points; red are the stellar candidates and the other colours are other clusters.
508 Points labelled + are rejects from the cluster (only for cluster 0).
510 At this prompt, you can continue with almost any key; 'p' enters pdb, and 'h' prints this text
512 If displayExposure is true, you can put the cursor on a point and hit 'p' to see it in the
515 elif reply[0] ==
"p":
518 elif reply[0] ==
'q':
523 if display
and displayExposure:
524 mi = exposure.getMaskedImage()
525 with disp.Buffering():
526 for i, source
in enumerate(sourceCat):
528 ctype = afwDisplay.GREEN
530 ctype = afwDisplay.RED
532 disp.dot(
"+", source.getX() - mi.getX0(), source.getY() - mi.getY0(), ctype=ctype)
538 return Struct(selected=good)