Coverage for python/lsst/ip/diffim/metrics.py : 57%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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#
23__all__ = [
24 "NumberSciSourcesMetricTask", "NumberSciSourcesMetricConfig",
25 "FractionDiaSourcesToSciSourcesMetricTask", "FractionDiaSourcesToSciSourcesMetricConfig",
26]
29import astropy.units as u
31from lsst.pipe.base import Struct, connectionTypes
32from lsst.verify import Measurement
33from lsst.verify.gen2tasks import register
34from lsst.verify.tasks import MetricTask, MetricConfig, MetricConnections, \
35 MetricComputationError
38class NumberSciSourcesMetricConnections(
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 )
52class NumberSciSourcesMetricConfig(
53 MetricConfig,
54 pipelineConnections=NumberSciSourcesMetricConnections):
55 pass
58@register("numSciSources")
59class NumberSciSourcesMetricTask(MetricTask):
60 """Task that computes the number of cataloged science sources.
61 """
62 _DefaultName = "numSciSources"
63 ConfigClass = NumberSciSourcesMetricConfig
65 def run(self, sources):
66 """Count the number of science sources.
68 Parameters
69 ----------
70 sources : `lsst.afw.table.SourceCatalog` or `None`
71 A science source catalog, which may be empty or `None`.
73 Returns
74 -------
75 result : `lsst.pipe.base.Struct`
76 A `~lsst.pipe.base.Struct` containing the following component:
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)
91class 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 )
111class FractionDiaSourcesToSciSourcesMetricConfig(
112 MetricTask.ConfigClass,
113 pipelineConnections=FractionDiaSourcesToSciSourcesMetricConnections):
114 pass
117@register("fracDiaSourcesToSciSources")
118class 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
125 def run(self, sciSources, diaSources):
126 """Compute the ratio of DIASources to science sources.
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``.
136 Returns
137 -------
138 result : `lsst.pipe.base.Struct`
139 A `~lsst.pipe.base.Struct` containing the following component:
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)