Coverage for python/lsst/faro/base/CatalogMeasurementBase.py: 41%
Shortcuts 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
Shortcuts 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 faro.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://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 <https://www.gnu.org/licenses/>.
21from astropy.table import Table, hstack
22import astropy.units as u
24import lsst.pipe.base as pipeBase
25import lsst.pex.config as pexConfig
26from lsst.verify.tasks import MetricTask, MetricConfig, MetricConnections
27from lsst.pipe.tasks.loadReferenceCatalog import LoadReferenceCatalogTask
28import lsst.geom
29from .BaseSubTasks import NumSourcesTask
31__all__ = (
32 "CatalogMeasurementBaseConnections",
33 "CatalogMeasurementBaseConfig",
34 "CatalogMeasurementBaseTask",
35)
38class CatalogMeasurementBaseConnections(
39 MetricConnections, defaultTemplates={"refDataset": ""}
40):
42 refCat = pipeBase.connectionTypes.PrerequisiteInput(
43 doc="Reference catalog",
44 name="{refDataset}",
45 storageClass="SimpleCatalog",
46 dimensions=("skypix",),
47 deferLoad=True,
48 multiple=True,
49 )
51 def __init__(self, *, config=None):
52 super().__init__(config=config)
53 if config.connections.refDataset == "":
54 self.prerequisiteInputs.remove("refCat")
57class CatalogMeasurementBaseConfig(
58 MetricConfig, pipelineConnections=CatalogMeasurementBaseConnections
59):
60 """Configuration for CatalogMeasurementBaseTask."""
62 measure = pexConfig.ConfigurableField(
63 # This task is meant to make measurements of various types.
64 # The default task is, therefore, a bit of a place holder.
65 # It is expected that this will be overridden in the pipeline
66 # definition in most cases.
67 target=NumSourcesTask,
68 doc="Measure task",
69 )
71 referenceCatalogLoader = pexConfig.ConfigurableField(
72 target=LoadReferenceCatalogTask, doc="Reference catalog loader",
73 )
75 def setDefaults(self):
76 self.referenceCatalogLoader.refObjLoader.ref_dataset_name = ""
77 self.referenceCatalogLoader.doApplyColorTerms = False
79 def validate(self):
80 super().validate()
81 if (
82 self.connections.refDataset
83 != self.referenceCatalogLoader.refObjLoader.ref_dataset_name
84 ):
85 msg = "The reference datasets specified in connections and reference catalog loader must match."
86 raise pexConfig.FieldValidationError(
87 CatalogMeasurementBaseConfig.referenceCatalogLoader, self, msg
88 )
91class CatalogMeasurementBaseTask(MetricTask):
92 """Base class for science performance metrics measured from source/object catalogs."""
94 ConfigClass = CatalogMeasurementBaseConfig
95 _DefaultName = "catalogMeasurementBaseTask"
97 def __init__(self, config, *args, **kwargs):
98 super().__init__(*args, config=config, **kwargs)
99 self.makeSubtask("measure")
101 def run(self, **kwargs):
102 return self.measure.run(self.config.connections.metric, **kwargs)
104 def _getTableColumnsSelectors(self, columns, currentBands=None):
105 """given a list of selectors return columns required to apply these
106 selectors.
107 Parameters
108 ----------
109 columns: `list` [`str`]
110 a list of columns required to calculate a metric. This list
111 is appended with any addditional columns required for the selectorActions.
113 currentBands: `list` [`str`]
114 The filter band(s) associated with the observations.
116 Returns
117 -------
118 columnNames: `list` [`str`] the set of columns required to compute a
119 metric with any addditional columns required for the selectorActions
120 appended to the set.
122 """
123 columnNames = set(columns)
124 for actionStruct in [self.config.measure.selectorActions]:
125 for action in actionStruct:
126 for col in action.columns(currentBands):
127 columnNames.add(col)
129 return columnNames
131 def _getReferenceCatalog(self, butlerQC, dataIds, refCats, filterList, epoch=None):
132 """Load reference catalog in sky region of interest and optionally applies proper
133 motion correction and color terms.
135 Loads the `lsst.afw.table.SimpleCatalog` reference catalog, computes ra and dec
136 (optionally) applying a proper motion correction. Also, color terms
137 are (optionally) applied to the reference magnitudes in order to transform
138 them to the data's photometric system.
140 returns a refCat with both the original loaded reference catalog and
141 the coorected coordinates (ra,dec) and transformed reference magnitudes
142 (refMag-/refMagErr-)
144 Parameters
145 ----------
146 butlerQC : `lsst.pipe.base.butlerQuantumContext.ButlerQuantumContext`
147 Butler quantum context for a Gen3 repository.
148 dataIds: interable of `lsst.daf.butler.dataId`
149 An iterable object of dataIds that point to reference catalogs
150 in a Gen3 repository.
151 refCats : iterable of `lsst.daf.butler.DeferredDatasetHandle`
152 An iterable object of dataset refs for reference catalogs in
153 a Gen3 repository.
154 filterList : `list` [`str`]
155 List of camera physicalFilter names to apply color terms.
156 epoch : `astropy.time.Time`, optional
157 Epoch to which to correct proper motion and parallax
158 (if available), or `None` to not apply such corrections.
160 Returns
161 -------
162 refCat: pandas.dataframe
163 a reference catalog with original columns and corrected
164 coordinates (ra,dec) and reference magnitudes (refMag-/refMagErr-)
165 """
166 center = lsst.geom.SpherePoint(
167 butlerQC.quantum.dataId.region.getBoundingCircle().getCenter()
168 )
169 radius = butlerQC.quantum.dataId.region.getBoundingCircle().getOpeningAngle()
171 loaderTask = LoadReferenceCatalogTask(
172 config=self.config.referenceCatalogLoader, dataIds=dataIds, refCats=refCats
173 )
175 # Get catalog with proper motion and color terms applied
176 refCatCorrected = loaderTask.getSkyCircleCatalog(
177 center, radius, filterList, epoch=epoch
178 )
180 # Get unformatted catalog w/ all columns
181 skyCircle = loaderTask.refObjLoader.loadSkyCircle(
182 center, radius, loaderTask._referenceFilter, epoch=epoch
183 )
184 refCat = skyCircle.refCat
186 refCatTable = Table()
187 refCatTable['ra'] = refCatCorrected['ra']*u.deg
188 refCatTable['dec'] = refCatCorrected['dec']*u.deg
189 for n, filterName in enumerate(filterList):
190 refCatTable['refMag-' + filterName] = refCatCorrected["refMag"][:, n]*u.ABmag
191 refCatTable['refMagErr-' + filterName] = refCatCorrected["refMagErr"][:, n]*u.ABmag
192 refCatFrame = hstack([refCatTable, refCat.asAstropy()]).to_pandas()
194 return refCatFrame