23 "NumberDeblendedSourcesMetricTask",
"NumberDeblendedSourcesMetricConfig",
24 "NumberDeblendChildSourcesMetricTask",
"NumberDeblendChildSourcesMetricConfig",
29import astropy.units
as u
31from lsst.pipe.base
import NoWorkFound, Struct, connectionTypes
32from lsst.verify
import Measurement
33from lsst.verify.tasks
import MetricTask, MetricConfig, MetricConnections, MetricComputationError
38 defaultTemplates={
"package":
"pipe_tasks",
39 "metric":
"numDeblendedSciSources"},
40 dimensions={
"instrument",
"visit",
"detector"},
42 sources = connectionTypes.Input(
43 doc=
"The catalog of science sources.",
45 storageClass=
"SourceCatalog",
46 dimensions={
"instrument",
"visit",
"detector"},
50class NumberDeblendedSourcesMetricConfig(
52 pipelineConnections=NumberDeblendedSourcesMetricConnections):
56class NumberDeblendedSourcesMetricTask(MetricTask):
57 """Task that computes the number of science sources that have
60 This task only counts sources that existed prior to any deblending;
61 i.e., if deblending was run more than once or with multiple iterations,
62 only the "top-level" deblended sources are counted, and not any
63 intermediate ones. If sky source information is present, sky sources
68 The task excludes any non-sky sources in the catalog, but it does
69 not require that the catalog include a ``sky_sources`` column.
71 _DefaultName =
"numDeblendedSciSources"
72 ConfigClass = NumberDeblendedSourcesMetricConfig
74 def run(self, sources):
75 """Count the number of deblended science sources.
79 sources : `lsst.afw.table.SourceCatalog`
80 A science source catalog, which may be empty.
84 result : `lsst.pipe.base.Struct`
85 A `~lsst.pipe.base.Struct` containing the following component:
88 the total number of deblended science sources
89 (`lsst.verify.Measurement`). If no deblending information is
90 available in ``sources``, this is `None`.
94 MetricComputationError
95 Raised if ``sources`` is missing mandatory keys for
98 if "deblend_nChild" not in sources.schema:
99 raise NoWorkFound(
"Nothing to do: no deblending performed.")
102 deblended = ((sources[
"parent"] == 0)
103 & (sources[
"deblend_nChild"] > 0)
105 deblended = _filterSkySources(sources, deblended)
106 except LookupError
as e:
108 raise MetricComputationError(
"Invalid input catalog")
from e
110 nDeblended = np.count_nonzero(deblended)
111 return Struct(measurement=Measurement(self.config.metricName,
112 nDeblended * u.dimensionless_unscaled))
115class NumberDeblendChildSourcesMetricConnections(
117 defaultTemplates={
"package":
"pipe_tasks",
118 "metric":
"numDeblendChildSciSources"},
119 dimensions={
"instrument",
"visit",
"detector"},
121 sources = connectionTypes.Input(
122 doc=
"The catalog of science sources.",
124 storageClass=
"SourceCatalog",
125 dimensions={
"instrument",
"visit",
"detector"},
129class NumberDeblendChildSourcesMetricConfig(
131 pipelineConnections=NumberDeblendChildSourcesMetricConnections):
135class NumberDeblendChildSourcesMetricTask(MetricTask):
136 """Task that computes the number of science sources created
139 This task only counts final deblending products; i.e., if deblending was
140 run more than once or with multiple iterations, only the final set of
141 deblended sources are counted, and not any intermediate ones.
142 If sky source information is present, sky sources are excluded.
146 The task excludes any non-sky sources in the catalog, but it does
147 not require that the catalog include a ``sky_sources`` column.
149 _DefaultName =
"numDeblendChildSciSources"
150 ConfigClass = NumberDeblendChildSourcesMetricConfig
152 def run(self, sources):
153 """Count the number of science sources created by deblending.
157 sources : `lsst.afw.table.SourceCatalog`
158 A science source catalog, which may be empty.
162 result : `lsst.pipe.base.Struct`
163 A `~lsst.pipe.base.Struct` containing the following component:
166 the total number of science sources from deblending
167 (`lsst.verify.Measurement`). If no deblending information is
168 available in ``sources``, this is `None`.
172 MetricComputationError
173 Raised if ``sources`` is missing mandatory keys for
178 if "deblend_parentNChild" not in sources.schema
or "deblend_nChild" not in sources.schema:
179 raise NoWorkFound(
"Nothing to do: no deblending performed.")
182 children = ((sources[
"deblend_parentNChild"] > 1)
183 & (sources[
"deblend_nChild"] == 0)
185 children = _filterSkySources(sources, children)
186 except LookupError
as e:
188 raise MetricComputationError(
"Invalid input catalog")
from e
190 nChildren = np.count_nonzero(children)
191 return Struct(measurement=Measurement(self.config.metricName,
192 nChildren * u.dimensionless_unscaled))
195def _filterSkySources(catalog, selection):
196 """Filter out any sky sources from a vector of selected sources.
198 If no sky source information is available, all sources are assumed to
203 catalog : `lsst.afw.table.SourceCatalog`
204 The catalog to filter.
205 selection : `numpy.ndarray` [`bool`], (N,)
206 A vector of existing source selections, of the same length as
207 ``catalog``, where selected sources are marked `True`.
211 filtered : `numpy.ndarray` [`bool`], (N,)
212 A version of ``selection`` with any sky sources filtered out
213 (set to `False`). May be the same vector as ``selection`` if
214 no changes were made.
216 if "sky_source" in catalog.schema:
219 return selection & (catalog[
"sky_source"] ==
False)