331 """Return a selection of PSF candidates that represent likely stars.
333 A list of PSF candidates may be used by a PSF fitter to construct a PSF.
337 sourceCat : `lsst.afw.table.SourceCatalog`
338 Catalog of sources to select from.
339 This catalog must be contiguous in memory.
340 matches : `list` of `lsst.afw.table.ReferenceMatch` or None
341 Ignored in this SourceSelector.
342 exposure : `lsst.afw.image.Exposure` or None
343 The exposure the catalog was built from; used to get the detector
344 to transform to TanPix, and for debug display.
348 struct : `lsst.pipe.base.Struct`
349 The struct contains the following data:
352 Boolean array of sources that were selected, same length as
353 sourceCat. (`numpy.ndarray` of `bool`)
364 detector = exposure.getDetector()
366 pixToTanPix = detector.getTransform(PIXELS, TAN_PIXELS)
370 flux = sourceCat[self.config.sourceFluxField]
371 fluxErr = sourceCat[self.config.sourceFluxField +
"Err"]
373 xx = numpy.empty(len(sourceCat))
374 xy = numpy.empty_like(xx)
375 yy = numpy.empty_like(xx)
376 for i, source
in enumerate(sourceCat):
377 Ixx, Ixy, Iyy = source.getIxx(), source.getIxy(), source.getIyy()
382 m.transform(linTransform)
383 Ixx, Iyy, Ixy = m.getIxx(), m.getIyy(), m.getIxy()
385 xx[i], xy[i], yy[i] = Ixx, Ixy, Iyy
387 width = numpy.sqrt(0.5*(xx + yy))
388 with numpy.errstate(invalid=
"ignore"):
389 bad = reduce(
lambda x, y: numpy.logical_or(x, sourceCat[y]), self.config.badFlags,
False)
390 bad = numpy.logical_or(bad, numpy.logical_not(numpy.isfinite(width)))
391 bad = numpy.logical_or(bad, numpy.logical_not(numpy.isfinite(flux)))
392 if self.config.doFluxLimit:
393 bad = numpy.logical_or(bad, flux < self.config.fluxMin)
394 if self.config.fluxMax > 0:
395 bad = numpy.logical_or(bad, flux > self.config.fluxMax)
396 if self.config.doSignalToNoiseLimit:
397 bad = numpy.logical_or(bad, flux/fluxErr < self.config.signalToNoiseMin)
398 if self.config.signalToNoiseMax > 0:
399 bad = numpy.logical_or(bad, flux/fluxErr > self.config.signalToNoiseMax)
400 bad = numpy.logical_or(bad, width < self.config.widthMin)
401 bad = numpy.logical_or(bad, width > self.config.widthMax)
402 good = numpy.logical_not(bad)
404 if not numpy.any(good):
405 raise RuntimeError(
"No objects passed our cuts for consideration as psf stars")
407 mag = -2.5*numpy.log10(flux[good])
415 import pickle
as pickle
418 pickleFile = os.path.expanduser(os.path.join(
"~",
"widths-%d.pkl" % _ii))
419 if not os.path.exists(pickleFile):
423 with open(pickleFile,
"wb")
as fd:
424 pickle.dump(mag, fd, -1)
425 pickle.dump(width, fd, -1)
427 centers, clusterId =
_kcenters(width, nCluster=4, useMedian=
True,
428 widthStdAllowed=self.config.widthStdAllowed)
430 if display
and plotMagSize:
431 fig =
plot(mag, width, centers, clusterId,
432 magType=self.config.sourceFluxField.split(
".")[-1].title(),
433 marker=
"+", markersize=3, markeredgewidth=
None, ltype=
':', clear=
True)
438 nsigma=self.config.nSigmaClip,
439 widthStdAllowed=self.config.widthStdAllowed)
441 if display
and plotMagSize:
442 plot(mag, width, centers, clusterId, marker=
"x", markersize=3, markeredgewidth=
None, clear=
False)
444 stellar = (clusterId == 0)
451 if display
and displayExposure:
452 disp = afwDisplay.Display(frame=frame)
453 disp.mtv(exposure.getMaskedImage(), title=
"PSF candidates")
456 eventHandler =
EventHandler(fig.get_axes()[0], mag, width,
457 sourceCat.getX()[good], sourceCat.getY()[good], frames=[frame])
463 reply = input(
"continue? [c h(elp) q(uit) p(db)] ").
strip()
472 We cluster the points; red are the stellar candidates and the other colours are other clusters.
473 Points labelled + are rejects from the cluster (only for cluster 0).
475 At this prompt, you can continue with almost any key; 'p' enters pdb, and 'h' prints this text
477 If displayExposure is true, you can put the cursor on a point and hit 'p' to see it in the
480 elif reply[0] ==
"p":
483 elif reply[0] ==
'q':
488 if display
and displayExposure:
489 mi = exposure.getMaskedImage()
490 with disp.Buffering():
491 for i, source
in enumerate(sourceCat):
493 ctype = afwDisplay.GREEN
495 ctype = afwDisplay.RED
497 disp.dot(
"+", source.getX() - mi.getX0(), source.getY() - mi.getY0(), ctype=ctype)
503 return Struct(selected=good)