lsst.meas.extensions.psfex  17.0.1-1-g9deacb5+12
psfexStarSelector.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2008, 2009, 2010 LSST Corporation.
4 #
5 # This product includes software developed by the
6 # LSST Project (http://www.lsst.org/).
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the LSST License Statement and
19 # the GNU General Public License along with this program. If not,
20 # see <http://www.lsstcorp.org/LegalNotices/>.
21 #
22 import re
23 import sys
24 
25 import numpy as np
26 try:
27  import matplotlib.pyplot as plt
28  fig = None
29 except ImportError:
30  plt = None
31 
32 from lsst.pipe.base import Struct
33 import lsst.pex.config as pexConfig
34 import lsst.afw.display as afwDisplay
35 from lsst.meas.algorithms import BaseSourceSelectorTask, sourceSelectorRegistry
36 from . import psfexLib
37 from .psfex import compute_fwhmrange
38 
39 __all__ = ["PsfexStarSelectorConfig", "PsfexStarSelectorTask"]
40 
41 
42 class PsfexStarSelectorConfig(BaseSourceSelectorTask.ConfigClass):
43  fluxName = pexConfig.Field(
44  dtype=str,
45  doc="Name of photometric flux key ",
46  default="base_PsfFlux",
47  )
48  fluxErrName = pexConfig.Field(
49  dtype=str,
50  doc="Name of phot. flux err. key",
51  default="",
52  )
53  minFwhm = pexConfig.Field(
54  dtype=float,
55  doc="Maximum allowed FWHM ",
56  default=2,
57  )
58  maxFwhm = pexConfig.Field(
59  dtype=float,
60  doc="Minimum allowed FWHM ",
61  default=10,
62  )
63  maxFwhmVariability = pexConfig.Field(
64  dtype=float,
65  doc="Allowed FWHM variability (1.0 = 100%)",
66  default=0.2,
67  )
68  maxbad = pexConfig.Field(
69  dtype=int,
70  doc="Max number of bad pixels ",
71  default=0,
72  check=lambda x: x >= 0,
73  )
74  maxbadflag = pexConfig.Field(
75  dtype=bool,
76  doc="Filter bad pixels? ",
77  default=True
78  )
79  maxellip = pexConfig.Field(
80  dtype=float,
81  doc="Maximum (A-B)/(A+B) ",
82  default=0.3,
83  check=lambda x: x >= 0.0,
84  )
85  minsn = pexConfig.Field(
86  dtype=float,
87  doc="Minimum S/N for candidates",
88  default=100,
89  check=lambda x: x >= 0.0,
90  )
91 
92  def validate(self):
93  pexConfig.Config.validate(self)
94 
95  if self.fluxErrName == "":
96  self.fluxErrName = self.fluxName + ".err"
97  elif self.fluxErrName != self.fluxName + ".err":
98  raise pexConfig.FieldValidationError("fluxErrName (%s) doesn't correspond to fluxName (%s)"
99  % (self.fluxErrName, self.fluxName))
100 
101  if self.minFwhm > self.maxFwhm:
102  raise pexConfig.FieldValidationError("minFwhm (%f) > maxFwhm (%f)" % (self.minFwhm, self.maxFwhm))
103 
104  def setDefaults(self):
105  self.badFlags = [
106  "base_PixelFlags_flag_edge",
107  "base_PixelFlags_flag_saturatedCenter",
108  "base_PixelFlags_flag_crCenter",
109  "base_PixelFlags_flag_bad",
110  "base_PixelFlags_flag_suspectCenter",
111  "base_PsfFlux_flag",
112  # "parent", # actually this is a test on deblend_nChild
113  ]
114 
115 
116 class EventHandler():
117  """A class to handle key strokes with matplotlib displays
118  """
119 
120  def __init__(self, axes, xs, ys, x, y, frames=[0]):
121  self.axes = axes
122  self.xs = xs
123  self.ys = ys
124  self.x = x
125  self.y = y
126  self.frames = frames
127 
128  self.cid = self.axes.figure.canvas.mpl_connect('key_press_event', self)
129 
130  def __call__(self, ev):
131  if ev.inaxes != self.axes:
132  return
133 
134  if ev.key and ev.key in ("p"):
135  dist = np.hypot(self.xs - ev.xdata, self.ys - ev.ydata)
136  dist[np.where(np.isnan(dist))] = 1e30
137 
138  which = np.where(dist == min(dist))
139 
140  x = self.x[which][0]
141  y = self.y[which][0]
142  for frame in self.frames:
143  disp = afwDisplay.Display(frame=frame)
144  disp.pan(x, y)
145  disp.flush()
146  else:
147  pass
148 
149 
150 def plot(mag, width, centers, clusterId, marker="o", markersize=2, markeredgewidth=0, ltype='-',
151  clear=True):
152 
153  global fig
154  if not fig:
155  fig = plt.figure()
156  newFig = True
157  else:
158  newFig = False
159  if clear:
160  fig.clf()
161 
162  axes = fig.add_axes((0.1, 0.1, 0.85, 0.80))
163 
164  xmin = sorted(mag)[int(0.05*len(mag))]
165  xmax = sorted(mag)[int(0.95*len(mag))]
166 
167  axes.set_xlim(-17.5, -13)
168  axes.set_xlim(xmin - 0.1*(xmax - xmin), xmax + 0.1*(xmax - xmin))
169  axes.set_ylim(0, 10)
170 
171  colors = ["r", "g", "b", "c", "m", "k", ]
172  for k, mean in enumerate(centers):
173  if k == 0:
174  axes.plot(axes.get_xlim(), (mean, mean,), "k%s" % ltype)
175 
176  l = (clusterId == k)
177  axes.plot(mag[l], width[l], marker, markersize=markersize, markeredgewidth=markeredgewidth,
178  color=colors[k%len(colors)])
179 
180  l = (clusterId == -1)
181  axes.plot(mag[l], width[l], marker, markersize=markersize, markeredgewidth=markeredgewidth,
182  color='k')
183 
184  if newFig:
185  axes.set_xlabel("model")
186  axes.set_ylabel(r"$\sqrt{I_{xx} + I_{yy}}$")
187 
188  return fig
189 
190 
196 
197 
198 @pexConfig.registerConfigurable("psfex", sourceSelectorRegistry)
199 class PsfexStarSelectorTask(BaseSourceSelectorTask):
200  """A star selector whose algorithm is not yet documented.
201 
202  @anchor PsfexStarSelectorTask_
203 
204  @section meas_extensions_psfex_psfexStarSelectorStarSelector_Contents Contents
205 
206  - @ref meas_extensions_psfex_psfexStarSelectorStarSelector_Purpose
207  - @ref meas_extensions_psfex_psfexStarSelectorStarSelector_Initialize
208  - @ref meas_extensions_psfex_psfexStarSelectorStarSelector_IO
209  - @ref meas_extensions_psfex_psfexStarSelectorStarSelector_Config
210  - @ref meas_extensions_psfex_psfexStarSelectorStarSelector_Debug
211 
212  @section meas_extensions_psfex_psfexStarSelectorStarSelector_Purpose Description
213 
214  A star selector whose algorithm is not yet documented
215 
216  @section meas_extensions_psfex_psfexStarSelectorStarSelector_Initialize Task initialisation
217 
218  @copydoc \_\_init\_\_
219 
220  @section meas_extensions_psfex_psfexStarSelectorStarSelector_IO Invoking the Task
221 
222  Like all star selectors, the main method is `run`.
223 
224  @section meas_extensions_psfex_psfexStarSelectorStarSelector_Config Configuration parameters
225 
226  See @ref PsfexStarSelectorConfig
227 
228  @section meas_extensions_psfex_psfexStarSelectorStarSelector_Debug Debug variables
229 
230  PsfexStarSelectorTask has a debug dictionary with the following keys:
231  <dl>
232  <dt>display
233  <dd>bool; if True display debug information
234  <dt>displayExposure
235  <dd>bool; if True display the exposure and spatial cells
236  <dt>plotFwhmHistogram
237  <dd>bool; if True plot histogram of FWHM
238  <dt>plotFlags
239  <dd>bool: if True plot the sources coloured by their flags
240  <dt>plotRejection
241  <dd>bool; if True plot why sources are rejected
242  </dl>
243 
244  For example, put something like:
245  @code{.py}
246  import lsstDebug
247  def DebugInfo(name):
248  di = lsstDebug.getInfo(name) # N.b. lsstDebug.Info(name) would call us recursively
249  if name.endswith("objectSizeStarSelector"):
250  di.display = True
251  di.displayExposure = True
252  di.plotFwhmHistogram = True
253 
254  return di
255 
256  lsstDebug.Info = DebugInfo
257  @endcode
258  into your `debug.py` file and run your task with the `--debug` flag.
259  """
260  ConfigClass = PsfexStarSelectorConfig
261  usesMatches = False # selectStars does not use its matches argument
262 
263  def selectSources(self, sourceCat, matches=None, exposure=None):
264  """Return a selection of psf-like objects.
265 
266  Parameters
267  ----------
268  sourceCat : `lsst.afw.table.SourceCatalog`
269  Catalog of sources to select from.
270  This catalog must be contiguous in memory.
271  matches : `list` of `lsst.afw.table.ReferenceMatch` or None
272  Ignored by this source selector.
273  exposure : `lsst.afw.image.Exposure` or None
274  The exposure the catalog was built from; used for debug display.
275 
276  Return
277  ------
278  struct : `lsst.pipe.base.Struct`
279  The struct contains the following data:
280 
281  - selected : `numpy.ndarray` of `bool``
282  Boolean array of sources that were selected, same length as
283  sourceCat.
284  """
285  import lsstDebug
286  display = lsstDebug.Info(__name__).display
287 
288  displayExposure = display and \
289  lsstDebug.Info(__name__).displayExposure # display the Exposure + spatialCells
290  plotFwhmHistogram = display and plt and \
291  lsstDebug.Info(__name__).plotFwhmHistogram # Plot histogram of FWHM
292  plotFlags = display and plt and \
293  lsstDebug.Info(__name__).plotFlags # Plot the sources coloured by their flags
294  plotRejection = display and plt and \
295  lsstDebug.Info(__name__).plotRejection # Plot why sources are rejected
296  afwDisplay.setDefaultMaskTransparency(75)
297 
298  fluxName = self.config.fluxName
299  fluxErrName = self.config.fluxErrName
300  minFwhm = self.config.minFwhm
301  maxFwhm = self.config.maxFwhm
302  maxFwhmVariability = self.config.maxFwhmVariability
303  maxbad = self.config.maxbad
304  maxbadflag = self.config.maxbadflag
305  maxellip = self.config.maxellip
306  minsn = self.config.minsn
307 
308  maxelong = (maxellip + 1.0)/(1.0 - maxellip) if maxellip < 1.0 else 100
309 
310  # Unpack the catalogue
311  shape = sourceCat.getShapeDefinition()
312  ixx = sourceCat.get("%s.xx" % shape)
313  iyy = sourceCat.get("%s.yy" % shape)
314 
315  fwhm = 2*np.sqrt(2*np.log(2))*np.sqrt(0.5*(ixx + iyy))
316  elong = 0.5*(ixx - iyy)/(ixx + iyy)
317 
318  flux = sourceCat.get(fluxName)
319  fluxErr = sourceCat.get(fluxErrName)
320  sn = flux/np.where(fluxErr > 0, fluxErr, 1)
321  sn[fluxErr <= 0] = -psfexLib.BIG
322 
323  flags = 0x0
324  for i, f in enumerate(self.config.badFlags):
325  flags = np.bitwise_or(flags, np.where(sourceCat.get(f), 1 << i, 0))
326  #
327  # Estimate the acceptable range of source widths
328  #
329  good = np.logical_and(sn > minsn, np.logical_not(flags))
330  good = np.logical_and(good, elong < maxelong)
331  good = np.logical_and(good, fwhm >= minFwhm)
332  good = np.logical_and(good, fwhm < maxFwhm)
333 
334  fwhmMode, fwhmMin, fwhmMax = compute_fwhmrange(fwhm[good], maxFwhmVariability, minFwhm, maxFwhm,
335  plot=dict(fwhmHistogram=plotFwhmHistogram))
336 
337  # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
338  #
339  # Here's select_candidates
340  #
341  # ---- Apply some selection over flags, fluxes...
342 
343  bad = (flags != 0)
344  # set.setBadFlags(int(sum(bad)))
345 
346  if plotRejection:
347  selectionVectors = []
348  selectionVectors.append((bad, "flags %d" % sum(bad)))
349 
350  dbad = sn < minsn
351  # set.setBadSN(int(sum(dbad)))
352  bad = np.logical_or(bad, dbad)
353  if plotRejection:
354  selectionVectors.append((dbad, "S/N %d" % sum(dbad)))
355 
356  dbad = fwhm < fwhmMin
357  # set.setBadFrmin(int(sum(dbad)))
358  bad = np.logical_or(bad, dbad)
359  if plotRejection:
360  selectionVectors.append((dbad, "fwhmMin %d" % sum(dbad)))
361 
362  dbad = fwhm > fwhmMax
363  # set.setBadFrmax(int(sum(dbad)))
364  bad = np.logical_or(bad, dbad)
365  if plotRejection:
366  selectionVectors.append((dbad, "fwhmMax %d" % sum(dbad)))
367 
368  dbad = elong > maxelong
369  # set.setBadElong(int(sum(dbad)))
370  bad = np.logical_or(bad, dbad)
371  if plotRejection:
372  selectionVectors.append((dbad, "elong %d" % sum(dbad)))
373 
374  # -- ... and check the integrity of the sample
375  if maxbadflag:
376  nbad = np.array([(v <= -psfexLib.BIG).sum() for v in vignet])
377  dbad = nbad > maxbad
378  # set.setBadPix(int(sum(dbad)))
379  bad = np.logical_or(bad, dbad)
380  if plotRejection:
381  selectionVectors.append((dbad, "badpix %d" % sum(dbad)))
382 
383  good = np.logical_not(bad)
384  #
385  # We know enough to plot, if so requested
386  #
387  frame = 0
388  if displayExposure:
389  mi = exposure.getMaskedImage()
390  disp = afwDisplay.Display(frame=frame)
391  disp.mtv(mi, title="PSF candidates")
392 
393  with disp.Buffering():
394  for i, source in enumerate(sourceCat):
395  if good[i]:
396  ctype = afwDisplay.GREEN # star candidate
397  else:
398  ctype = afwDisplay.RED # not star
399 
400  disp.dot("+", source.getX() - mi.getX0(), source.getY() - mi.getY0(),
401  ctype=ctype)
402 
403  if plotFlags or plotRejection:
404  imag = -2.5*np.log10(flux)
405  plt.clf()
406 
407  alpha = 0.5
408  if plotFlags:
409  isSet = np.where(flags == 0x0)[0]
410  plt.plot(imag[isSet], fwhm[isSet], 'o', alpha=alpha, label="good")
411 
412  for i, f in enumerate(self.config.badFlags):
413  mask = 1 << i
414  isSet = np.where(np.bitwise_and(flags, mask))[0]
415  if isSet.any():
416  if np.isfinite(imag[isSet] + fwhm[isSet]).any():
417  label = re.sub(r"\_flag", "",
418  re.sub(r"^base\_", "",
419  re.sub(r"^.*base\_PixelFlags\_flag\_", "", f)))
420  plt.plot(imag[isSet], fwhm[isSet], 'o', alpha=alpha, label=label)
421  else:
422  for bad, label in selectionVectors:
423  plt.plot(imag[bad], fwhm[bad], 'o', alpha=alpha, label=label)
424 
425  plt.plot(imag[good], fwhm[good], 'o', color="black", label="selected")
426  [plt.axhline(_, color='red') for _ in [fwhmMin, fwhmMax]]
427  plt.xlim(np.median(imag[good]) + 5*np.array([-1, 1]))
428  plt.ylim(fwhm[np.where(np.isfinite(fwhm + imag))].min(), 2*fwhmMax)
429  plt.legend(loc=2)
430  plt.xlabel("Instrumental %s Magnitude" % fluxName.split(".")[-1].title())
431  plt.ylabel("fwhm")
432  title = "PSFEX Star Selection"
433  plt.title("%s %d selected" % (title, sum(good)))
434 
435  if displayExposure:
436  global eventHandler
437  eventHandler = EventHandler(plt.axes(), imag, fwhm, sourceCat.getX(), sourceCat.getY(),
438  frames=[frame])
439 
440  if plotFlags or plotRejection:
441  while True:
442  try:
443  reply = input("continue? [y[es] h(elp) p(db) q(uit)] ").strip()
444  except EOFError:
445  reply = "y"
446 
447  if not reply:
448  reply = "y"
449 
450  if reply[0] == "h":
451  print("""\
452 At this prompt, you can continue with almost any key; 'p' enters pdb,
453  'q' returns to the shell, and
454  'h' prints this text
455 """, end=' ')
456 
457  if displayExposure:
458  print("""
459 If you put the cursor on a point in the matplotlib scatter plot and hit 'p' you'll see it in ds9.""")
460  elif reply[0] == "p":
461  import pdb
462  pdb.set_trace()
463  elif reply[0] == 'q':
464  sys.exit(1)
465  else:
466  break
467 
468  return Struct(selected=good)
def selectSources(self, sourceCat, matches=None, exposure=None)
def compute_fwhmrange(fwhm, maxvar, minin, maxin, plot=dict(fwhmHistogram=False))
Definition: psfex.py:55
def __init__(self, axes, xs, ys, x, y, frames=[0])
def plot(mag, width, centers, clusterId, marker="o", markersize=2, markeredgewidth=0, ltype='-', clear=True)