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