330 def selectSources(self, sourceCat, matches=None, exposure=None):
331 """Return a selection of PSF candidates that represent likely stars.
332
333 A list of PSF candidates may be used by a PSF fitter to construct a PSF.
334
335 Parameters
336 ----------
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.
345
346 Returns
347 -------
348 struct : `lsst.pipe.base.Struct`
349 The struct contains the following data:
350
351 ``selected``
352 Boolean array of sources that were selected, same length as
353 sourceCat. (`numpy.ndarray` of `bool`)
354 """
355 import lsstDebug
360
361 detector = None
362 pixToTanPix = None
363 if exposure:
364 detector = exposure.getDetector()
365 if detector:
366 pixToTanPix = detector.getTransform(PIXELS, TAN_PIXELS)
367
368
369
370 flux = sourceCat[self.config.sourceFluxField]
371 fluxErr = sourceCat[self.config.sourceFluxField + "Err"]
372
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()
378 if pixToTanPix:
382 m.transform(linTransform)
383 Ixx, Iyy, Ixy = m.getIxx(), m.getIyy(), m.getIxy()
384
385 xx[i], xy[i], yy[i] = Ixx, Ixy, Iyy
386
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)
403
404 if not numpy.any(good):
405 raise RuntimeError("No objects passed our cuts for consideration as psf stars")
406
407 mag = -2.5*numpy.log10(flux[good])
408 width = width[good]
409
410
411
412
413 if dumpData:
414 import os
415 import pickle as pickle
416 _ii = 0
417 while True:
418 pickleFile = os.path.expanduser(os.path.join("~", "widths-%d.pkl" % _ii))
419 if not os.path.exists(pickleFile):
420 break
421 _ii += 1
422
423 with open(pickleFile, "wb") as fd:
424 pickle.dump(mag, fd, -1)
425 pickle.dump(width, fd, -1)
426
427 centers, clusterId = _kcenters(width, nCluster=4, useMedian=True,
428 widthStdAllowed=self.config.widthStdAllowed)
429
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)
434 else:
435 fig = None
436
437 clusterId = _improveCluster(width, centers, clusterId,
438 nsigma=self.config.nSigmaClip,
439 widthStdAllowed=self.config.widthStdAllowed)
440
441 if display and plotMagSize:
442 plot(mag, width, centers, clusterId, marker="x", markersize=3, markeredgewidth=None, clear=False)
443
444 stellar = (clusterId == 0)
445
446
447
448 frame = 0
449
450 if fig:
451 if display and displayExposure:
452 disp = afwDisplay.Display(frame=frame)
453 disp.mtv(exposure.getMaskedImage(), title="PSF candidates")
454
455 global eventHandler
456 eventHandler = EventHandler(fig.get_axes()[0], mag, width,
457 sourceCat.getX()[good], sourceCat.getY()[good], frames=[frame])
458
459 fig.show()
460
461 while True:
462 try:
463 reply = input(
"continue? [c h(elp) q(uit) p(db)] ").
strip()
464 except EOFError:
465 reply = None
466 if not reply:
467 reply = "c"
468
469 if reply:
470 if reply[0] == "h":
471 print("""\
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).
474
475 At this prompt, you can continue with almost any key; 'p' enters pdb, and 'h' prints this text
476
477 If displayExposure is true, you can put the cursor on a point and hit 'p' to see it in the
478 image display.
479 """)
480 elif reply[0] == "p":
481 import pdb
482 pdb.set_trace()
483 elif reply[0] == 'q':
484 sys.exit(1)
485 else:
486 break
487
488 if display and displayExposure:
489 mi = exposure.getMaskedImage()
490 with disp.Buffering():
491 for i, source in enumerate(sourceCat):
492 if good[i]:
493 ctype = afwDisplay.GREEN
494 else:
495 ctype = afwDisplay.RED
496
497 disp.dot("+", source.getX() - mi.getX0(), source.getY() - mi.getY0(), ctype=ctype)
498
499
500 mask = good == True
501 good[mask] = stellar
502
503 return Struct(selected=good)
An ellipse core with quadrupole moments as parameters.
lsst::geom::AffineTransform linearizeTransform(TransformPoint2ToPoint2 const &original, lsst::geom::Point2D const &inPoint)
Approximate a Transform by its local linearization.