LSST Applications g0265f82a02+c6dfa2ddaf,g1162b98a3f+ffe7eabc7e,g2079a07aa2+1b2e822518,g2bbee38e9b+c6dfa2ddaf,g337abbeb29+c6dfa2ddaf,g36da64cc00+ea84795170,g3ddfee87b4+955a963fd8,g50ff169b8f+2eb0e556e8,g52b1c1532d+90ebb246c7,g555ede804d+955a963fd8,g591dd9f2cf+bac198a2cb,g5ec818987f+420292cfeb,g858d7b2824+d6c9a0a3b8,g876c692160+aabc49a3c3,g8a8a8dda67+90ebb246c7,g8cdfe0ae6a+4fd9e222a8,g99cad8db69+e6cd765486,g9ddcbc5298+a1346535a5,ga1e77700b3+df8f93165b,ga8c6da7877+acd47f83f4,gae46bcf261+c6dfa2ddaf,gb0e22166c9+8634eb87fb,gb3f2274832+12c8382528,gba4ed39666+1ac82b564f,gbb8dafda3b+0574160a1f,gbeb006f7da+dea2fbb49f,gc28159a63d+c6dfa2ddaf,gc86a011abf+d6c9a0a3b8,gcf0d15dbbd+955a963fd8,gdaeeff99f8+1cafcb7cd4,gdc0c513512+d6c9a0a3b8,ge79ae78c31+c6dfa2ddaf,geb67518f79+ba1859f325,gee10cc3b42+90ebb246c7,gf1cff7945b+d6c9a0a3b8,w.2024.13
LSST Data Management Base Package
Loading...
Searching...
No Matches
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
29import numpy as np
30import astropy.units as u
31
32from lsst.pipe.base import Struct, connectionTypes
33from lsst.verify import Measurement
34from lsst.verify.tasks import MetricTask, MetricConfig, MetricConnections, \
35 MetricComputationError
36
37
39 MetricConnections,
40 defaultTemplates={"package": "ip_diffim",
41 "metric": "numSciSources"},
42 dimensions={"instrument", "visit", "detector"},
43):
44 sources = connectionTypes.Input(
45 doc="The catalog of science sources.",
46 name="src",
47 storageClass="SourceCatalog",
48 dimensions={"instrument", "visit", "detector"},
49 )
50
51
52class NumberSciSourcesMetricConfig(
53 MetricConfig,
54 pipelineConnections=NumberSciSourcesMetricConnections):
55 pass
56
57
58class NumberSciSourcesMetricTask(MetricTask):
59 """Task that computes the number of cataloged non-primary science sources.
60
61 Notes
62 -----
63 The task excludes any non-primary sources in the catalog, but it does
64 not require that the catalog include a ``detect_isPrimary`` or
65 ``sky_sources`` column.
66 """
67 _DefaultName = "numSciSources"
68 ConfigClass = NumberSciSourcesMetricConfig
69
70 def run(self, sources):
71 """Count the number of primary science sources.
72
73 Parameters
74 ----------
75 sources : `lsst.afw.table.SourceCatalog`
76 A science source catalog, which may be empty.
77
78 Returns
79 -------
80 result : `lsst.pipe.base.Struct`
81 A `~lsst.pipe.base.Struct` containing the following component:
82
83 ``measurement``
84 the total number of non-primary science sources
85 (`lsst.verify.Measurement` or `None`)
86 """
87 nSciSources = _countRealSources(sources)
88 meas = Measurement(self.config.metricName, nSciSources * u.count)
89 return Struct(measurement=meas)
90
91
92class FractionDiaSourcesToSciSourcesMetricConnections(
93 MetricTask.ConfigClass.ConnectionsClass,
94 dimensions={"instrument", "visit", "detector"},
95 defaultTemplates={"coaddName": "deep",
96 "fakesType": "",
97 "package": "ip_diffim",
98 "metric": "fracDiaSourcesToSciSources"}):
99 sciSources = connectionTypes.Input(
100 doc="The catalog of science sources.",
101 name="src",
102 storageClass="SourceCatalog",
103 dimensions={"instrument", "visit", "detector"},
104 )
105 diaSources = connectionTypes.Input(
106 doc="The catalog of DIASources.",
107 name="{fakesType}{coaddName}Diff_diaSrc",
108 storageClass="SourceCatalog",
109 dimensions={"instrument", "visit", "detector"},
110 )
111
112
113class FractionDiaSourcesToSciSourcesMetricConfig(
114 MetricTask.ConfigClass,
115 pipelineConnections=FractionDiaSourcesToSciSourcesMetricConnections):
116 pass
117
118
119class FractionDiaSourcesToSciSourcesMetricTask(MetricTask):
120 """Task that computes the ratio of difference image sources to science
121 sources in an image, visit, etc.
122
123 Notes
124 -----
125 The task excludes any non-primary sources in the catalog, but it does
126 not require that the catalog include a ``detect_isPrimary`` or
127 ``sky_sources`` column.
128 """
129 _DefaultName = "fracDiaSourcesToSciSources"
130 ConfigClass = FractionDiaSourcesToSciSourcesMetricConfig
131
132 def run(self, sciSources, diaSources):
133 """Compute the ratio of DIASources to non-primary science sources.
134
135 Parameters
136 ----------
137 sciSources : `lsst.afw.table.SourceCatalog`
138 A science source catalog, which may be empty.
139 diaSources : `lsst.afw.table.SourceCatalog`
140 A DIASource catalog for the same unit of processing
141 as ``sciSources``.
142
143 Returns
144 -------
145 result : `lsst.pipe.base.Struct`
146 A `~lsst.pipe.base.Struct` containing the following component:
147
148 ``measurement``
149 the ratio (`lsst.verify.Measurement` or `None`)
150 """
151 nSciSources = _countRealSources(sciSources)
152 nDiaSources = _countRealSources(diaSources)
153 metricName = self.config.metricName
154 if nSciSources <= 0:
155 raise MetricComputationError(
156 "No science sources found; ratio of DIASources to science sources ill-defined.")
157 else:
158 meas = Measurement(metricName, nDiaSources / nSciSources * u.dimensionless_unscaled)
159 return Struct(measurement=meas)
160
161
162def _countRealSources(catalog):
163 """Return the number of valid sources in a catalog.
164
165 At present, this definition includes only primary sources. If a catalog
166 does not have a ``detect_isPrimary`` flag, this function counts non-sky
167 sources. If it does not have a ``sky_source`` flag, either, all sources
168 are counted.
169
170 Parameters
171 ----------
172 `catalog` : `lsst.afw.table.SourceCatalog`
173 The catalog of sources to count.
174
175 Returns
176 -------
177 count : `int`
178 The number of sources that satisfy the criteria.
179 """
180 # E712 is not applicable, because afw.table.SourceRecord.ColumnView
181 # is not a bool.
182 if "detect_isPrimary" in catalog.schema:
183 return np.count_nonzero(catalog["detect_isPrimary"] == True) # noqa: E712
184 elif "sky_source" in catalog.schema:
185 return np.count_nonzero(catalog["sky_source"] == False) # noqa: E712
186 else:
187 return len(catalog)
run(self, coaddExposures, bbox, wcs, dataIds, physical_filter=None, **kwargs)