lsst.meas.base  16.0-13-gd9b1b71+12
classification.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # LSST Data Management System
4 # Copyright 2008-2016 AURA/LSST.
5 #
6 # This product includes software developed by the
7 # LSST Project (http://www.lsst.org/).
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the LSST License Statement and
20 # the GNU General Public License along with this program. If not,
21 # see <http://www.lsstcorp.org/LegalNotices/>.
22 #
23 """
24 Definition and registration of classification plugins
25 """
26 
27 import numpy as np
28 
29 import lsst.pex.config
30 from .catalogCalculation import CatalogCalculationPluginConfig, CatalogCalculationPlugin
31 from .pluginRegistry import register
32 
33 __all__ = (
34  "CatalogCalculationClassificationConfig", "CatalogCalculationClassificationPlugin",
35 )
36 
37 
39  fluxRatio = lsst.pex.config.Field(dtype=float, default=.925, optional=True,
40  doc="critical ratio of model to psf flux")
41  modelErrFactor = lsst.pex.config.Field(dtype=float, default=0.0, optional=True,
42  doc="correction factor for modelFlux error")
43  psfErrFactor = lsst.pex.config.Field(dtype=float, default=0.0, optional=True,
44  doc="correction factor for psfFlux error")
45 
46 
47 @register("base_ClassificationExtendedness")
49  """
50  A binary measure of the extendedness of a source, based a simple cut on the ratio of the
51  PSF flux to the model flux.
52 
53  Because the fluxes on which this algorithm is based on are slot measurements, they can be provided
54  by different algorithms, and the "fluxRatio" threshold used by this algorithm should generally
55  be set differently for different algorithms. To do this, plot the difference between the PSF
56  magnitude and the model magnitude vs. the PSF magnitude, and look for where the cloud of galaxies
57  begins.
58  """
59 
60  ConfigClass = CatalogCalculationClassificationConfig
61 
62  @classmethod
65 
66  def __init__(self, config, name, schema, metadata):
67  CatalogCalculationPlugin.__init__(self, config, name, schema, metadata)
68  self.keyProbability = schema.addField(name + "_value", type="D",
69  doc="Set to 1 for extended sources, 0 for point sources.")
70  self.keyFlag = schema.addField(name + "_flag", type="Flag", doc="Set to 1 for any fatal failure.")
71 
72  def calculate(self, measRecord):
73  modelFlux = measRecord.getModelInstFlux()
74  psfFlux = measRecord.getPsfInstFlux()
75  modelFluxFlag = (measRecord.getModelFluxFlag()
76  if measRecord.table.getModelFluxSlot().isValid()
77  else False)
78  psfFluxFlag = (measRecord.getPsfFluxFlag()
79  if measRecord.table.getPsfFluxSlot().isValid()
80  else False)
81  flux1 = self.config.fluxRatio*modelFlux
82  if self.config.modelErrFactor != 0:
83  flux1 += self.config.modelErrFactor*measRecord.getModelInstFluxErr()
84  flux2 = psfFlux
85  if not self.config.psfErrFactor == 0:
86  flux2 += self.config.psfErrFactor*measRecord.getPsfInstFluxErr()
87 
88  # A generic failure occurs when either FluxFlag is set to True
89  # A generic failure also occurs if either calculated flux value is NAN:
90  # this can occur if the Flux field itself is NAN,
91  # or the ErrFactor != 0 and the FluxErr is NAN
92  if np.isnan(flux1) or np.isnan(flux2) or modelFluxFlag or psfFluxFlag:
93  self.fail(measRecord)
94  else:
95  measRecord.set(self.keyProbability, 0.0 if flux1 < flux2 else 1.0)
96 
97  def fail(self, measRecord, error=None):
98  measRecord.set(self.keyFlag, True)