27 import matplotlib.pyplot
as plt
36 from .
import psfexLib
37 from .psfex
import compute_fwhmrange
39 __all__ = [
"PsfexStarSelectorConfig",
"PsfexStarSelectorTask"]
43 fluxName = pexConfig.Field(
45 doc=
"Name of photometric flux key ",
46 default=
"base_PsfFlux",
48 fluxErrName = pexConfig.Field(
50 doc=
"Name of phot. flux err. key",
53 minFwhm = pexConfig.Field(
55 doc=
"Maximum allowed FWHM ",
58 maxFwhm = pexConfig.Field(
60 doc=
"Minimum allowed FWHM ",
63 maxFwhmVariability = pexConfig.Field(
65 doc=
"Allowed FWHM variability (1.0 = 100%)",
68 maxbad = pexConfig.Field(
70 doc=
"Max number of bad pixels ",
72 check=
lambda x: x >= 0,
74 maxbadflag = pexConfig.Field(
76 doc=
"Filter bad pixels? ",
79 maxellip = pexConfig.Field(
81 doc=
"Maximum (A-B)/(A+B) ",
83 check=
lambda x: x >= 0.0,
85 minsn = pexConfig.Field(
87 doc=
"Minimum S/N for candidates",
89 check=
lambda x: x >= 0.0,
93 pexConfig.Config.validate(self)
98 msg = f
"fluxErrName ({self.fluxErrName}) doesn't correspond to fluxName ({self.fluxName})" 99 raise pexConfig.FieldValidationError(PsfexStarSelectorConfig.fluxName, self, msg)
102 raise pexConfig.FieldValidationError(PsfexStarSelectorConfig.minFwhm, self,
103 f
"minFwhm ({self.minFwhm}) > maxFwhm ({self.maxFwhm})")
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",
118 """A class to handle key strokes with matplotlib displays 121 def __init__(self, axes, xs, ys, x, y, frames=[0]):
129 self.
cid = self.
axes.figure.canvas.mpl_connect(
'key_press_event', self)
132 if ev.inaxes != self.
axes:
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
139 which = np.where(dist ==
min(dist))
144 disp = afwDisplay.Display(frame=frame)
151 def plot(mag, width, centers, clusterId, marker="o", markersize=2, markeredgewidth=0, ltype='-',
163 axes = fig.add_axes((0.1, 0.1, 0.85, 0.80))
165 xmin = sorted(mag)[
int(0.05*len(mag))]
166 xmax = sorted(mag)[
int(0.95*len(mag))]
168 axes.set_xlim(-17.5, -13)
169 axes.set_xlim(xmin - 0.1*(xmax - xmin), xmax + 0.1*(xmax - xmin))
172 colors = [
"r", "g", "b", "c", "m", "k", ]
173 for k, mean
in enumerate(centers):
175 axes.plot(axes.get_xlim(), (mean, mean,),
"k%s" % ltype)
178 axes.plot(mag[l], width[l], marker, markersize=markersize, markeredgewidth=markeredgewidth,
179 color=colors[k%len(colors)])
181 l = (clusterId == -1)
182 axes.plot(mag[l], width[l], marker, markersize=markersize, markeredgewidth=markeredgewidth,
186 axes.set_xlabel(
"model")
187 axes.set_ylabel(
r"$\sqrt{I_{xx} + I_{yy}}$")
199 @pexConfig.registerConfigurable(
"psfex", sourceSelectorRegistry)
201 """A star selector whose algorithm is not yet documented. 203 @anchor PsfexStarSelectorTask_ 205 @section meas_extensions_psfex_psfexStarSelectorStarSelector_Contents Contents 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 213 @section meas_extensions_psfex_psfexStarSelectorStarSelector_Purpose Description 215 A star selector whose algorithm is not yet documented 217 @section meas_extensions_psfex_psfexStarSelectorStarSelector_Initialize Task initialisation 219 @copydoc \_\_init\_\_ 221 @section meas_extensions_psfex_psfexStarSelectorStarSelector_IO Invoking the Task 223 Like all star selectors, the main method is `run`. 225 @section meas_extensions_psfex_psfexStarSelectorStarSelector_Config Configuration parameters 227 See @ref PsfexStarSelectorConfig 229 @section meas_extensions_psfex_psfexStarSelectorStarSelector_Debug Debug variables 231 PsfexStarSelectorTask has a debug dictionary with the following keys: 234 <dd>bool; if True display debug information 236 <dd>bool; if True display the exposure and spatial cells 237 <dt>plotFwhmHistogram 238 <dd>bool; if True plot histogram of FWHM 240 <dd>bool: if True plot the sources coloured by their flags 242 <dd>bool; if True plot why sources are rejected 245 For example, put something like: 249 di = lsstDebug.getInfo(name) # N.b. lsstDebug.Info(name) would call us recursively 250 if name.endswith("objectSizeStarSelector"): 252 di.displayExposure = True 253 di.plotFwhmHistogram = True 257 lsstDebug.Info = DebugInfo 259 into your `debug.py` file and run your task with the `--debug` flag. 261 ConfigClass = PsfexStarSelectorConfig
265 """Return a selection of psf-like objects. 269 sourceCat : `lsst.afw.table.SourceCatalog` 270 Catalog of sources to select from. 271 This catalog must be contiguous in memory. 272 matches : `list` of `lsst.afw.table.ReferenceMatch` or None 273 Ignored by this source selector. 274 exposure : `lsst.afw.image.Exposure` or None 275 The exposure the catalog was built from; used for debug display. 279 struct : `lsst.pipe.base.Struct` 280 The struct contains the following data: 282 - selected : `numpy.ndarray` of `bool`` 283 Boolean array of sources that were selected, same length as 289 displayExposure = display
and \
291 plotFwhmHistogram = display
and plt
and \
293 plotFlags = display
and plt
and \
295 plotRejection = display
and plt
and \
297 afwDisplay.setDefaultMaskTransparency(75)
299 fluxName = self.config.fluxName
300 fluxErrName = self.config.fluxErrName
301 minFwhm = self.config.minFwhm
302 maxFwhm = self.config.maxFwhm
303 maxFwhmVariability = self.config.maxFwhmVariability
304 maxbad = self.config.maxbad
305 maxbadflag = self.config.maxbadflag
306 maxellip = self.config.maxellip
307 minsn = self.config.minsn
309 maxelong = (maxellip + 1.0)/(1.0 - maxellip)
if maxellip < 1.0
else 100
312 shape = sourceCat.getShapeDefinition()
313 ixx = sourceCat.get(
"%s.xx" % shape)
314 iyy = sourceCat.get(
"%s.yy" % shape)
316 fwhm = 2*np.sqrt(2*np.log(2))*np.sqrt(0.5*(ixx + iyy))
317 elong = 0.5*(ixx - iyy)/(ixx + iyy)
319 flux = sourceCat.get(fluxName)
320 fluxErr = sourceCat.get(fluxErrName)
321 sn = flux/np.where(fluxErr > 0, fluxErr, 1)
322 sn[fluxErr <= 0] = -psfexLib.BIG
325 for i, f
in enumerate(self.config.badFlags):
326 flags = np.bitwise_or(flags, np.where(sourceCat.get(f), 1 << i, 0))
330 good = np.logical_and(sn > minsn, np.logical_not(flags))
331 good = np.logical_and(good, elong < maxelong)
332 good = np.logical_and(good, fwhm >= minFwhm)
333 good = np.logical_and(good, fwhm < maxFwhm)
335 fwhmMode, fwhmMin, fwhmMax =
compute_fwhmrange(fwhm[good], maxFwhmVariability, minFwhm, maxFwhm,
336 plot=dict(fwhmHistogram=plotFwhmHistogram))
348 selectionVectors = []
349 selectionVectors.append((bad,
"flags %d" % sum(bad)))
353 bad = np.logical_or(bad, dbad)
355 selectionVectors.append((dbad,
"S/N %d" % sum(dbad)))
357 dbad = fwhm < fwhmMin
359 bad = np.logical_or(bad, dbad)
361 selectionVectors.append((dbad,
"fwhmMin %d" % sum(dbad)))
363 dbad = fwhm > fwhmMax
365 bad = np.logical_or(bad, dbad)
367 selectionVectors.append((dbad,
"fwhmMax %d" % sum(dbad)))
369 dbad = elong > maxelong
371 bad = np.logical_or(bad, dbad)
373 selectionVectors.append((dbad,
"elong %d" % sum(dbad)))
377 nbad = np.array([(v <= -psfexLib.BIG).sum()
for v
in vignet])
380 bad = np.logical_or(bad, dbad)
382 selectionVectors.append((dbad,
"badpix %d" % sum(dbad)))
384 good = np.logical_not(bad)
390 mi = exposure.getMaskedImage()
391 disp = afwDisplay.Display(frame=frame)
392 disp.mtv(mi, title=
"PSF candidates")
394 with disp.Buffering():
395 for i, source
in enumerate(sourceCat):
397 ctype = afwDisplay.GREEN
399 ctype = afwDisplay.RED
401 disp.dot(
"+", source.getX() - mi.getX0(), source.getY() - mi.getY0(),
404 if plotFlags
or plotRejection:
405 imag = -2.5*np.log10(flux)
410 isSet = np.where(flags == 0x0)[0]
411 plt.plot(imag[isSet], fwhm[isSet],
'o', alpha=alpha, label=
"good")
413 for i, f
in enumerate(self.config.badFlags):
415 isSet = np.where(np.bitwise_and(flags, mask))[0]
417 if np.isfinite(imag[isSet] + fwhm[isSet]).
any():
418 label = re.sub(
r"\_flag",
"",
419 re.sub(
r"^base\_",
"",
420 re.sub(
r"^.*base\_PixelFlags\_flag\_",
"", f)))
421 plt.plot(imag[isSet], fwhm[isSet],
'o', alpha=alpha, label=label)
423 for bad, label
in selectionVectors:
424 plt.plot(imag[bad], fwhm[bad],
'o', alpha=alpha, label=label)
426 plt.plot(imag[good], fwhm[good],
'o', color=
"black", label=
"selected")
427 [plt.axhline(_, color=
'red')
for _
in [fwhmMin, fwhmMax]]
428 plt.xlim(np.median(imag[good]) + 5*np.array([-1, 1]))
429 plt.ylim(fwhm[np.where(np.isfinite(fwhm + imag))].
min(), 2*fwhmMax)
431 plt.xlabel(
"Instrumental %s Magnitude" % fluxName.split(
".")[-1].title())
433 title =
"PSFEX Star Selection" 434 plt.title(
"%s %d selected" % (title, sum(good)))
438 eventHandler =
EventHandler(plt.axes(), imag, fwhm, sourceCat.getX(), sourceCat.getY(),
441 if plotFlags
or plotRejection:
444 reply = input(
"continue? [y[es] h(elp) p(db) q(uit)] ").
strip()
453 At this prompt, you can continue with almost any key; 'p' enters pdb, 454 'q' returns to the shell, and 460 If you put the cursor on a point in the matplotlib scatter plot and hit 'p' you'll see it in ds9.""")
461 elif reply[0] ==
"p":
464 elif reply[0] ==
'q':
469 return Struct(selected=good)
def selectSources(self, sourceCat, matches=None, exposure=None)
Fit spatial kernel using approximate fluxes for candidates, and solving a linear system of equations...
bool any(CoordinateExpr< N > const &expr) noexcept
Return true if any elements are true.
def compute_fwhmrange(fwhm, maxvar, minin, maxin, plot=dict(fwhmHistogram=False))
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)