LSST Applications g0f08755f38+82efc23009,g12f32b3c4e+e7bdf1200e,g1653933729+a8ce1bb630,g1a0ca8cf93+50eff2b06f,g28da252d5a+52db39f6a5,g2bbee38e9b+37c5a29d61,g2bc492864f+37c5a29d61,g2cdde0e794+c05ff076ad,g3156d2b45e+41e33cbcdc,g347aa1857d+37c5a29d61,g35bb328faa+a8ce1bb630,g3a166c0a6a+37c5a29d61,g3e281a1b8c+fb992f5633,g414038480c+7f03dfc1b0,g41af890bb2+11b950c980,g5fbc88fb19+17cd334064,g6b1c1869cb+12dd639c9a,g781aacb6e4+a8ce1bb630,g80478fca09+72e9651da0,g82479be7b0+04c31367b4,g858d7b2824+82efc23009,g9125e01d80+a8ce1bb630,g9726552aa6+8047e3811d,ga5288a1d22+e532dc0a0b,gae0086650b+a8ce1bb630,gb58c049af0+d64f4d3760,gc28159a63d+37c5a29d61,gcf0d15dbbd+2acd6d4d48,gd7358e8bfb+778a810b6e,gda3e153d99+82efc23009,gda6a2b7d83+2acd6d4d48,gdaeeff99f8+1711a396fd,ge2409df99d+6b12de1076,ge79ae78c31+37c5a29d61,gf0baf85859+d0a5978c5a,gf3967379c6+4954f8c433,gfb92a5be7c+82efc23009,gfec2e1e490+2aaed99252,w.2024.46
LSST Data Management Base Package
Loading...
Searching...
No Matches
match_tract_catalog.py
Go to the documentation of this file.
1# This file is part of pipe_tasks.
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/>.
21
22__all__ = [
23 'MatchTractCatalogSubConfig', 'MatchTractCatalogSubTask',
24 'MatchTractCatalogConfig', 'MatchTractCatalogTask'
25]
26
27import lsst.afw.geom as afwGeom
28import lsst.pex.config as pexConfig
29import lsst.pipe.base as pipeBase
30import lsst.pipe.base.connectionTypes as cT
31from lsst.skymap import BaseSkyMap
32
33from abc import ABC, abstractmethod
34
35import astropy.table
36import pandas as pd
37from typing import Tuple, Set
38
39
40MatchTractCatalogBaseTemplates = {
41 "name_input_cat_ref": "truth_summary",
42 "name_input_cat_target": "objectTable_tract",
43}
44
45
47 pipeBase.PipelineTaskConnections,
48 dimensions=("tract", "skymap"),
49 defaultTemplates=MatchTractCatalogBaseTemplates,
50):
51 cat_ref = cT.Input(
52 doc="Reference object catalog to match from",
53 name="{name_input_cat_ref}",
54 storageClass="ArrowAstropy",
55 dimensions=("tract", "skymap"),
56 deferLoad=True,
57 )
58 cat_target = cT.Input(
59 doc="Target object catalog to match",
60 name="{name_input_cat_target}",
61 storageClass="ArrowAstropy",
62 dimensions=("tract", "skymap"),
63 deferLoad=True,
64 )
65 skymap = cT.Input(
66 doc="Input definition of geometry/bbox and projection/wcs for coadded exposures",
67 name=BaseSkyMap.SKYMAP_DATASET_TYPE_NAME,
68 storageClass="SkyMap",
69 dimensions=("skymap",),
70 )
71 cat_output_ref = cT.Output(
72 doc="Reference matched catalog with indices of target matches",
73 name="match_ref_{name_input_cat_ref}_{name_input_cat_target}",
74 storageClass="ArrowAstropy",
75 dimensions=("tract", "skymap"),
76 )
77 cat_output_target = cT.Output(
78 doc="Target matched catalog with indices of reference matches",
79 name="match_target_{name_input_cat_ref}_{name_input_cat_target}",
80 storageClass="ArrowAstropy",
81 dimensions=("tract", "skymap"),
82 )
83
84 def __init__(self, *, config=None):
85 if config.refcat_sharding_type != "tract":
86 if config.refcat_sharding_type == "none":
87 old = self.cat_ref
88 del self.cat_ref
89 self.cat_ref = cT.Input(
90 doc=old.doc,
91 name=old.name,
92 storageClass=old.storageClass,
93 dimensions=(),
94 deferLoad=old.deferLoad,
95 )
96
97
98class MatchTractCatalogSubConfig(pexConfig.Config):
99 """Config class for the MatchTractCatalogSubTask to define methods returning
100 values that depend on multiple config settings.
101 """
102 @property
103 @abstractmethod
104 def columns_in_ref(self) -> Set[str]:
105 raise NotImplementedError()
106
107 @property
108 @abstractmethod
109 def columns_in_target(self) -> Set[str]:
110 raise NotImplementedError()
111
112
113class MatchTractCatalogSubTask(pipeBase.Task, ABC):
114 """An abstract interface for subtasks of MatchTractCatalogTask to match
115 two tract object catalogs.
116
117 Parameters
118 ----------
119 **kwargs
120 Additional arguments to be passed to the `lsst.pipe.base.Task`
121 constructor.
122 """
123 ConfigClass = MatchTractCatalogSubConfig
124
125 def __init__(self, **kwargs):
126 super().__init__(**kwargs)
127
128 @abstractmethod
129 def run(
130 self,
131 catalog_ref: pd.DataFrame | astropy.table.Table,
132 catalog_target: pd.DataFrame | astropy.table.Table,
133 wcs: afwGeom.SkyWcs = None,
134 ) -> pipeBase.Struct:
135 """Match sources in a reference tract catalog with a target catalog.
136
137 Parameters
138 ----------
139 catalog_ref : `pandas.DataFrame` | `astropy.table.Table`
140 A reference catalog to match objects/sources from.
141 catalog_target : `pandas.DataFrame` | `astropy.table.Table`
142 A target catalog to match reference objects/sources to.
143 wcs : `lsst.afw.image.SkyWcs`
144 A coordinate system to convert catalog positions to sky coordinates.
145
146 Returns
147 -------
148 retStruct : `lsst.pipe.base.Struct`
149 A struct with output_ref and output_target attribute containing the
150 output matched catalogs.
151 """
152 raise NotImplementedError()
153
154
156 pipeBase.PipelineTaskConfig,
157 pipelineConnections=MatchTractCatalogConnections,
158):
159 """Configure a MatchTractCatalogTask, including a configurable matching subtask.
160 """
161 match_tract_catalog = pexConfig.ConfigurableField(
162 target=MatchTractCatalogSubTask,
163 doc="Task to match sources in a reference tract catalog with a target catalog",
164 )
165 refcat_sharding_type = pexConfig.ChoiceField[str](
166 doc="The type of sharding (spatial splitting) for the reference catalog",
167 allowed={"tract": "Tract-based shards", "none": "No sharding at all"},
168 default="tract",
169 )
170
171 def get_columns_in(self) -> Tuple[Set, Set]:
172 """Get the set of input columns required for matching.
173
174 Returns
175 -------
176 columns_ref : `set` [`str`]
177 The set of required input catalog column names.
178 columns_target : `set` [`str`]
179 The set of required target catalog column names.
180 """
181 try:
182 columns_ref, columns_target = (self.match_tract_catalog.columns_in_ref,
183 self.match_tract_catalog.columns_in_target)
184 except AttributeError as err:
185 raise RuntimeError(f'{__class__}.match_tract_catalog must have columns_in_ref and'
186 f' columns_in_target attributes: {err}') from None
187 return set(columns_ref), set(columns_target)
188
189
190class MatchTractCatalogTask(pipeBase.PipelineTask):
191 """Match sources in a reference tract catalog with those in a target catalog.
192 """
193 ConfigClass = MatchTractCatalogConfig
194 _DefaultName = "MatchTractCatalog"
195
196 def __init__(self, initInputs, **kwargs):
197 super().__init__(initInputs=initInputs, **kwargs)
198 self.makeSubtask("match_tract_catalog")
199
200 def runQuantum(self, butlerQC, inputRefs, outputRefs):
201 inputs = butlerQC.get(inputRefs)
202 columns_ref, columns_target = self.config.get_columns_in()
203 skymap = inputs.pop("skymap")
204
205 outputs = self.run(
206 catalog_ref=inputs['cat_ref'].get(parameters={'columns': columns_ref}),
207 catalog_target=inputs['cat_target'].get(parameters={'columns': columns_target}),
208 wcs=skymap[butlerQC.quantum.dataId["tract"]].wcs,
209 )
210 butlerQC.put(outputs, outputRefs)
211
212 def run(
213 self,
214 catalog_ref: pd.DataFrame | astropy.table.Table,
215 catalog_target: pd.DataFrame | astropy.table.Table,
216 wcs: afwGeom.SkyWcs = None,
217 ) -> pipeBase.Struct:
218 """Match sources in a reference tract catalog with a target catalog.
219
220 Parameters
221 ----------
222 catalog_ref : `pandas.DataFrame` | `astropy.table.Table`
223 A reference catalog to match objects/sources from.
224 catalog_target : `pandas.DataFrame` | `astropy.table.Table`
225 A target catalog to match reference objects/sources to.
226 wcs : `lsst.afw.image.SkyWcs`
227 A coordinate system to convert catalog positions to sky coordinates,
228 if necessary.
229
230 Returns
231 -------
232 retStruct : `lsst.pipe.base.Struct`
233 A struct with output_ref and output_target attribute containing the
234 output matched catalogs.
235 """
236 output = self.match_tract_catalog.run(catalog_ref, catalog_target, wcs=wcs)
237 if output.exceptions:
238 self.log.warn('Exceptions: %s', output.exceptions)
239 retStruct = pipeBase.Struct(cat_output_ref=output.cat_output_ref,
240 cat_output_target=output.cat_output_target)
241 return retStruct
A 2-dimensional celestial WCS that transform pixels to ICRS RA/Dec, using the LSST standard for pixel...
Definition SkyWcs.h:117
pipeBase.Struct run(self, pd.DataFrame|astropy.table.Table catalog_ref, pd.DataFrame|astropy.table.Table catalog_target, afwGeom.SkyWcs wcs=None)
pipeBase.Struct run(self, pd.DataFrame|astropy.table.Table catalog_ref, pd.DataFrame|astropy.table.Table catalog_target, afwGeom.SkyWcs wcs=None)