358 """Return a selection of PSF candidates that represent likely stars.
360 A list of PSF candidates may be used by a PSF fitter to construct a PSF.
364 sourceCat : `lsst.afw.table.SourceCatalog`
365 Catalog of sources to select from.
366 This catalog must be contiguous in memory.
367 matches : `list` of `lsst.afw.table.ReferenceMatch` or None
368 Ignored in this SourceSelector.
369 exposure : `lsst.afw.image.Exposure` or None
370 The exposure the catalog was built from; used to get the detector
371 to transform to TanPix, and for debug display.
375 struct : `lsst.pipe.base.Struct`
376 The struct contains the following data:
379 Boolean array of sources that were selected, same length as
380 sourceCat. (`numpy.ndarray` of `bool`)
382 if len(sourceCat) == 0:
383 raise ObjectSizeNoSourcesError
394 detector = exposure.getDetector()
396 pixToTanPix = detector.getTransform(PIXELS, TAN_PIXELS)
400 flux = sourceCat[self.config.sourceFluxField]
401 fluxErr = sourceCat[self.config.sourceFluxField +
"Err"]
403 xx = numpy.empty(len(sourceCat))
404 xy = numpy.empty_like(xx)
405 yy = numpy.empty_like(xx)
406 for i, source
in enumerate(sourceCat):
407 Ixx, Ixy, Iyy = source.getIxx(), source.getIxy(), source.getIyy()
410 linTransform = afwGeom.linearizeTransform(pixToTanPix, p).getLinear()
411 m = afwGeom.Quadrupole(Ixx, Iyy, Ixy)
412 m.transform(linTransform)
413 Ixx, Iyy, Ixy = m.getIxx(), m.getIyy(), m.getIxy()
415 xx[i], xy[i], yy[i] = Ixx, Ixy, Iyy
417 width = numpy.sqrt(0.5*(xx + yy))
418 with numpy.errstate(invalid=
"ignore"):
419 bad = reduce(
lambda x, y: numpy.logical_or(x, sourceCat[y]), self.config.badFlags,
False)
420 bad = numpy.logical_or(bad, numpy.logical_not(numpy.isfinite(width)))
421 bad = numpy.logical_or(bad, numpy.logical_not(numpy.isfinite(flux)))
422 if self.config.doFluxLimit:
423 bad = numpy.logical_or(bad, flux < self.config.fluxMin)
424 if self.config.fluxMax > 0:
425 bad = numpy.logical_or(bad, flux > self.config.fluxMax)
426 if self.config.doSignalToNoiseLimit:
427 bad = numpy.logical_or(bad, flux/fluxErr < self.config.signalToNoiseMin)
428 if self.config.signalToNoiseMax > 0:
429 bad = numpy.logical_or(bad, flux/fluxErr > self.config.signalToNoiseMax)
430 bad = numpy.logical_or(bad, width < self.config.widthMin)
431 bad = numpy.logical_or(bad, width > self.config.widthMax)
432 good = numpy.logical_not(bad)
434 if not numpy.any(good):
437 mag = -2.5*numpy.log10(flux[good])
445 import pickle
as pickle
448 pickleFile = os.path.expanduser(os.path.join(
"~",
"widths-%d.pkl" % _ii))
449 if not os.path.exists(pickleFile):
453 with open(pickleFile,
"wb")
as fd:
454 pickle.dump(mag, fd, -1)
455 pickle.dump(width, fd, -1)
457 centers, clusterId =
_kcenters(width, nCluster=4, useMedian=
True,
458 widthStdAllowed=self.config.widthStdAllowed)
460 if display
and plotMagSize:
461 fig =
plot(mag, width, centers, clusterId,
462 magType=self.config.sourceFluxField.split(
".")[-1].title(),
463 marker=
"+", markersize=3, markeredgewidth=
None, ltype=
':', clear=
True)
468 nsigma=self.config.nSigmaClip,
469 widthStdAllowed=self.config.widthStdAllowed)
471 if display
and plotMagSize:
472 plot(mag, width, centers, clusterId, marker=
"x", markersize=3, markeredgewidth=
None, clear=
False)
474 stellar = (clusterId == 0)
481 if display
and displayExposure:
482 disp = afwDisplay.Display(frame=frame)
483 disp.mtv(exposure.getMaskedImage(), title=
"PSF candidates")
486 eventHandler =
EventHandler(fig.get_axes()[0], mag, width,
487 sourceCat.getX()[good], sourceCat.getY()[good], frames=[frame])
493 reply = input(
"continue? [c h(elp) q(uit) p(db)] ").strip()
502 We cluster the points; red are the stellar candidates and the other colours are other clusters.
503 Points labelled + are rejects from the cluster (only for cluster 0).
505 At this prompt, you can continue with almost any key; 'p' enters pdb, and 'h' prints this text
507 If displayExposure is true, you can put the cursor on a point and hit 'p' to see it in the
510 elif reply[0] ==
"p":
513 elif reply[0] ==
'q':
518 if display
and displayExposure:
519 mi = exposure.getMaskedImage()
520 with disp.Buffering():
521 for i, source
in enumerate(sourceCat):
523 ctype = afwDisplay.GREEN
525 ctype = afwDisplay.RED
527 disp.dot(
"+", source.getX() - mi.getX0(), source.getY() - mi.getY0(), ctype=ctype)
533 return Struct(selected=good)