LSSTApplications  20.0.0
LSSTDataManagementBasePackage
metrics.py
Go to the documentation of this file.
1 # This file is part of ip_diffim.
2 #
3 # Developed for the LSST Data Management System.
4 # This product includes software developed by the LSST Project
5 # (http://www.lsst.org).
6 # See the COPYRIGHT file at the top-level directory of this distribution
7 # for details of code ownership.
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 GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #
22 
23 __all__ = [
24  "NumberSciSourcesMetricTask", "NumberSciSourcesMetricConfig",
25  "FractionDiaSourcesToSciSourcesMetricTask", "FractionDiaSourcesToSciSourcesMetricConfig",
26 ]
27 
28 
29 import astropy.units as u
30 
31 from lsst.pipe.base import Struct, connectionTypes
32 from lsst.verify import Measurement
33 from lsst.verify.gen2tasks import register
34 from lsst.verify.tasks import MetricTask, MetricConfig, MetricConnections, \
35  MetricComputationError
36 
37 
39  MetricConnections,
40  defaultTemplates={"package": "ip_diffim",
41  "metric": "numSciSources"},
42  dimensions={"Instrument", "Exposure", "Detector"},
43 ):
44  sources = connectionTypes.Input(
45  doc="The catalog of science sources.",
46  name="src",
47  storageClass="SourceCatalog",
48  dimensions={"Instrument", "Exposure", "Detector"},
49  )
50 
51 
52 class NumberSciSourcesMetricConfig(
53  MetricConfig,
54  pipelineConnections=NumberSciSourcesMetricConnections):
55  pass
56 
57 
58 @register("numSciSources")
59 class NumberSciSourcesMetricTask(MetricTask):
60  """Task that computes the number of cataloged science sources.
61  """
62  _DefaultName = "numSciSources"
63  ConfigClass = NumberSciSourcesMetricConfig
64 
65  def run(self, sources):
66  """Count the number of science sources.
67 
68  Parameters
69  ----------
70  sources : `lsst.afw.table.SourceCatalog` or `None`
71  A science source catalog, which may be empty or `None`.
72 
73  Returns
74  -------
75  result : `lsst.pipe.base.Struct`
76  A `~lsst.pipe.base.Struct` containing the following component:
77 
78  ``measurement``
79  the total number of science sources (`lsst.verify.Measurement`
80  or `None`)
81  """
82  if sources is not None:
83  nSciSources = len(sources)
84  meas = Measurement(self.config.metricName, nSciSources * u.count)
85  else:
86  self.log.info("Nothing to do: no catalogs found.")
87  meas = None
88  return Struct(measurement=meas)
89 
90 
91 class FractionDiaSourcesToSciSourcesMetricConnections(
92  MetricTask.ConfigClass.ConnectionsClass,
93  dimensions={"Instrument", "Exposure", "Detector"},
94  defaultTemplates={"coaddName": "deep",
95  "package": "ip_diffim",
96  "metric": "fracDiaSourcesToSciSources"}):
97  sciSources = connectionTypes.Input(
98  doc="The catalog of science sources.",
99  name="src",
100  storageClass="SourceCatalog",
101  dimensions={"Instrument", "Exposure", "Detector"},
102  )
103  diaSources = connectionTypes.Input(
104  doc="The catalog of DIASources.",
105  name="{coaddName}Diff_diaSrc",
106  storageClass="SourceCatalog",
107  dimensions={"Instrument", "Exposure", "Detector"},
108  )
109 
110 
111 class FractionDiaSourcesToSciSourcesMetricConfig(
112  MetricTask.ConfigClass,
113  pipelineConnections=FractionDiaSourcesToSciSourcesMetricConnections):
114  pass
115 
116 
117 @register("fracDiaSourcesToSciSources")
118 class FractionDiaSourcesToSciSourcesMetricTask(MetricTask):
119  """Task that computes the ratio of difference image sources to science
120  sources in an image, visit, etc.
121  """
122  _DefaultName = "fracDiaSourcesToSciSources"
123  ConfigClass = FractionDiaSourcesToSciSourcesMetricConfig
124 
125  def run(self, sciSources, diaSources):
126  """Compute the ratio of DIASources to science sources.
127 
128  Parameters
129  ----------
130  sciSources : `lsst.afw.table.SourceCatalog` or `None`
131  A science source catalog, which may be empty or `None`.
132  diaSources : `lsst.afw.table.SourceCatalog` or `None`
133  A DIASource catalog for the same unit of processing
134  as ``sciSources``.
135 
136  Returns
137  -------
138  result : `lsst.pipe.base.Struct`
139  A `~lsst.pipe.base.Struct` containing the following component:
140 
141  ``measurement``
142  the ratio (`lsst.verify.Measurement` or `None`)
143  """
144  if diaSources is not None and sciSources is not None:
145  nSciSources = len(sciSources)
146  nDiaSources = len(diaSources)
147  metricName = self.config.metricName
148  if nSciSources <= 0.0:
149  raise MetricComputationError(
150  "No science sources found; ratio of DIASources to science sources ill-defined.")
151  else:
152  meas = Measurement(metricName, nDiaSources / nSciSources * u.dimensionless_unscaled)
153  else:
154  self.log.info("Nothing to do: no catalogs found.")
155  meas = None
156  return Struct(measurement=meas)
lsst::log.log.logContinued.info
def info(fmt, *args)
Definition: logContinued.py:198
lsst::ip::diffim.metrics.NumberSciSourcesMetricConnections
Definition: metrics.py:40
lsst.pipe.tasks.assembleCoadd.run
def run(self, skyInfo, tempExpRefList, imageScalerList, weightList, altMaskList=None, mask=None, supplementaryData=None)
Definition: assembleCoadd.py:712
lsst.pipe.base.struct.Struct
Definition: struct.py:26
lsst.gdb.ip.diffim.printers.register
def register(obj)
Definition: printers.py:20
lsst.pipe.base
Definition: __init__.py:1