23 "NumberDeblendedSourcesMetricTask",
"NumberDeblendedSourcesMetricConfig",
24 "NumberDeblendChildSourcesMetricTask",
"NumberDeblendChildSourcesMetricConfig",
29import astropy.units
as u
32from lsst.verify
import Measurement
33from lsst.verify.gen2tasks
import register
34from lsst.verify.tasks
import MetricTask, MetricConfig, MetricConnections, MetricComputationError
39 defaultTemplates={
"package":
"pipe_tasks",
40 "metric":
"numDeblendedSciSources"},
41 dimensions={
"instrument",
"visit",
"detector"},
43 sources = connectionTypes.Input(
44 doc=
"The catalog of science sources.",
46 storageClass=
"SourceCatalog",
47 dimensions={
"instrument",
"visit",
"detector"},
51class NumberDeblendedSourcesMetricConfig(
53 pipelineConnections=NumberDeblendedSourcesMetricConnections):
57@register("numDeblendedSciSources")
58class NumberDeblendedSourcesMetricTask(MetricTask):
59 """Task that computes the number of science sources that have
62 This task only counts sources that existed prior to any deblending;
63 i.e., if deblending was run more than once
or with multiple iterations,
64 only the
"top-level" deblended sources are counted,
and not any
65 intermediate ones. If sky source information
is present, sky sources
70 The task excludes any non-sky sources
in the catalog, but it does
71 not require that the catalog include a ``sky_sources`` column.
73 _DefaultName = "numDeblendedSciSources"
74 ConfigClass = NumberDeblendedSourcesMetricConfig
76 def run(self, sources):
77 """Count the number of deblended science sources.
82 A science source catalog, which may be empty
or `
None`.
86 result : `lsst.pipe.base.Struct`
87 A `~lsst.pipe.base.Struct` containing the following component:
90 the total number of deblended science sources
91 (`lsst.verify.Measurement`). If no deblending information
is
92 available
in ``sources``, this
is `
None`.
96 MetricComputationError
97 Raised
if ``sources``
is missing mandatory keys
for
101 self.log.info(
"Nothing to do: no catalogs found.")
103 elif "deblend_nChild" not in sources.schema:
104 self.log.info(
"Nothing to do: no deblending performed.")
108 deblended = ((sources[
"parent"] == 0)
109 & (sources[
"deblend_nChild"] > 0)
111 deblended = _filterSkySources(sources, deblended)
112 except LookupError
as e:
114 raise MetricComputationError(
"Invalid input catalog")
from e
116 nDeblended = np.count_nonzero(deblended)
117 meas = Measurement(self.config.metricName, nDeblended * u.dimensionless_unscaled)
119 return Struct(measurement=meas)
122class NumberDeblendChildSourcesMetricConnections(
124 defaultTemplates={
"package":
"pipe_tasks",
125 "metric":
"numDeblendChildSciSources"},
126 dimensions={
"instrument",
"visit",
"detector"},
128 sources = connectionTypes.Input(
129 doc=
"The catalog of science sources.",
131 storageClass=
"SourceCatalog",
132 dimensions={
"instrument",
"visit",
"detector"},
136class NumberDeblendChildSourcesMetricConfig(
138 pipelineConnections=NumberDeblendChildSourcesMetricConnections):
142@register("numDeblendChildSciSources")
143class NumberDeblendChildSourcesMetricTask(MetricTask):
144 """Task that computes the number of science sources created
147 This task only counts final deblending products; i.e., if deblending was
148 run more than once
or with multiple iterations, only the final set of
149 deblended sources are counted,
and not any intermediate ones.
150 If sky source information
is present, sky sources are excluded.
154 The task excludes any non-sky sources
in the catalog, but it does
155 not require that the catalog include a ``sky_sources`` column.
157 _DefaultName = "numDeblendChildSciSources"
158 ConfigClass = NumberDeblendChildSourcesMetricConfig
160 def run(self, sources):
161 """Count the number of science sources created by deblending.
166 A science source catalog, which may be empty
or `
None`.
170 result : `lsst.pipe.base.Struct`
171 A `~lsst.pipe.base.Struct` containing the following component:
174 the total number of science sources
from deblending
175 (`lsst.verify.Measurement`). If no deblending information
is
176 available
in ``sources``, this
is `
None`.
180 MetricComputationError
181 Raised
if ``sources``
is missing mandatory keys
for
185 self.log.info(
"Nothing to do: no catalogs found.")
189 elif "deblend_parentNChild" not in sources.schema
or "deblend_nChild" not in sources.schema:
190 self.log.info(
"Nothing to do: no deblending performed.")
194 children = ((sources[
"deblend_parentNChild"] > 1)
195 & (sources[
"deblend_nChild"] == 0)
197 children = _filterSkySources(sources, children)
198 except LookupError
as e:
200 raise MetricComputationError(
"Invalid input catalog")
from e
202 nChildren = np.count_nonzero(children)
203 meas = Measurement(self.config.metricName, nChildren * u.dimensionless_unscaled)
205 return Struct(measurement=meas)
208def _filterSkySources(catalog, selection):
209 """Filter out any sky sources from a vector of selected sources.
211 If no sky source information is available, all sources are assumed to
217 The catalog to filter.
218 selection : `numpy.ndarray` [`bool`], (N,)
219 A vector of existing source selections, of the same length
as
220 ``catalog``, where selected sources are marked `
True`.
224 filtered : `numpy.ndarray` [`bool`], (N,)
225 A version of ``selection``
with any sky sources filtered out
226 (set to `
False`). May be the same vector
as ``selection``
if
227 no changes were made.
229 if "sky_source" in catalog.schema:
232 return selection & (catalog[
"sky_source"] ==
False)