LSST Applications 27.0.0,g0265f82a02+469cd937ee,g02d81e74bb+21ad69e7e1,g1470d8bcf6+cbe83ee85a,g2079a07aa2+e67c6346a6,g212a7c68fe+04a9158687,g2305ad1205+94392ce272,g295015adf3+81dd352a9d,g2bbee38e9b+469cd937ee,g337abbeb29+469cd937ee,g3939d97d7f+72a9f7b576,g487adcacf7+71499e7cba,g50ff169b8f+5929b3527e,g52b1c1532d+a6fc98d2e7,g591dd9f2cf+df404f777f,g5a732f18d5+be83d3ecdb,g64a986408d+21ad69e7e1,g858d7b2824+21ad69e7e1,g8a8a8dda67+a6fc98d2e7,g99cad8db69+f62e5b0af5,g9ddcbc5298+d4bad12328,ga1e77700b3+9c366c4306,ga8c6da7877+71e4819109,gb0e22166c9+25ba2f69a1,gb6a65358fc+469cd937ee,gbb8dafda3b+69d3c0e320,gc07e1c2157+a98bf949bb,gc120e1dc64+615ec43309,gc28159a63d+469cd937ee,gcf0d15dbbd+72a9f7b576,gdaeeff99f8+a38ce5ea23,ge6526c86ff+3a7c1ac5f1,ge79ae78c31+469cd937ee,gee10cc3b42+a6fc98d2e7,gf1cff7945b+21ad69e7e1,gfbcc870c63+9a11dc8c8f
LSST Data Management Base Package
Loading...
Searching...
No Matches
match_probabilistic_task.py
Go to the documentation of this file.
1# This file is part of meas_astrom.
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
22import lsst.afw.geom as afwGeom
23import lsst.geom as geom
24import lsst.pipe.base as pipeBase
25import lsst.utils as utils
26from .matcher_probabilistic import MatchProbabilisticConfig, MatcherProbabilistic
27
28import logging
29import numpy as np
30import pandas as pd
31from typing import Dict, List, Optional, Set, Tuple
32
33__all__ = ['MatchProbabilisticTask', 'radec_to_xy']
34
35
36def radec_to_xy(ra_vec, dec_vec, factor, wcs: afwGeom.SkyWcs):
37 radec_true = [geom.SpherePoint(ra*factor, dec*factor, geom.degrees)
38 for ra, dec in zip(ra_vec, dec_vec)]
39 return wcs.skyToPixel(radec_true)
40
41
42class MatchProbabilisticTask(pipeBase.Task):
43 """Run MatchProbabilistic on a reference and target catalog covering the same tract.
44 """
45 ConfigClass = MatchProbabilisticConfig
46 _DefaultName = "matchProbabilistic"
47
48 @staticmethod
50 catalog: pd.DataFrame,
51 columns_true: List[str],
52 columns_false: List[str],
53 selection: Optional[np.array],
54 ) -> np.array:
55 """ Apply additional boolean selection columns.
56
57 catalog : `pandas.DataFrame`
58 The catalog to select from.
59 columns_true : `list` [`str`]
60 Columns that must be True for selection.
61 columns_false : `list` [`str`]
62 Columns that must be False for selection.
63 selection : `numpy.array`
64 A prior selection array. Default all true.
65
66 Returns
67 -------
68 selection : `numpy.array`
69 The final selection array.
70
71 """
72 select_additional = (len(columns_true) + len(columns_false)) > 0
73 if select_additional:
74 if selection is None:
75 selection = np.ones(len(catalog), dtype=bool)
76 for column in columns_true:
77 selection &= catalog[column].values
78 for column in columns_false:
79 selection &= ~catalog[column].values
80 return selection
81
82 @property
83 def columns_in_ref(self) -> Set[str]:
84 return self.config.columns_in_ref
85
86 @property
87 def columns_in_target(self) -> Set[str]:
88 return self.config.columns_in_target
89
90 def match(
91 self,
92 catalog_ref: pd.DataFrame,
93 catalog_target: pd.DataFrame,
94 select_ref: np.array = None,
95 select_target: np.array = None,
96 wcs: afwGeom.SkyWcs = None,
97 logger: logging.Logger = None,
98 logging_n_rows: int = None,
99 ) -> Tuple[pd.DataFrame, pd.DataFrame, Dict[int, str]]:
100 """Match sources in a reference tract catalog with a target catalog.
101
102 Parameters
103 ----------
104 catalog_ref : `pandas.DataFrame`
105 A reference catalog to match objects/sources from.
106 catalog_target : `pandas.DataFrame`
107 A target catalog to match reference objects/sources to.
108 select_ref : `numpy.array`
109 A boolean array of the same length as `catalog_ref` selecting the sources that can be matched.
110 select_target : `numpy.array`
111 A boolean array of the same length as `catalog_target` selecting the sources that can be matched.
112 wcs : `lsst.afw.image.SkyWcs`
113 A coordinate system to convert catalog positions to sky coordinates. Only used if
114 `self.config.coords_ref_to_convert` is set.
115 logger : `logging.Logger`
116 A Logger for logging.
117 logging_n_rows : `int`
118 Number of matches to make before outputting incremental log message.
119
120 Returns
121 -------
122 catalog_out_ref : `pandas.DataFrame`
123 Reference matched catalog with indices of target matches.
124 catalog_out_target : `pandas.DataFrame`
125 Reference matched catalog with indices of target matches.
126 """
127 if logger is None:
128 logger = self.log
129
130 config = self.config
131
132 if config.column_ref_order is None:
133 flux_tot = np.nansum(catalog_ref.loc[:, config.columns_ref_flux].values, axis=1)
134 catalog_ref['flux_total'] = flux_tot
135 if config.mag_brightest_ref != -np.inf or config.mag_faintest_ref != np.inf:
136 mag_tot = -2.5*np.log10(flux_tot) + config.coord_format.mag_zeropoint_ref
137 select_mag = (mag_tot >= config.mag_brightest_ref) & (
138 mag_tot <= config.mag_faintest_ref)
139 else:
140 select_mag = np.isfinite(flux_tot)
141 if select_ref is None:
142 select_ref = select_mag
143 else:
144 select_ref &= select_mag
145
146 select_ref = self._apply_select_bool(
147 catalog=catalog_ref,
148 columns_true=config.columns_ref_select_true,
149 columns_false=config.columns_ref_select_false,
150 selection=select_ref
151 )
152 select_target = self._apply_select_bool(
153 catalog=catalog_target,
154 columns_true=config.columns_target_select_true,
155 columns_false=config.columns_target_select_false,
156 selection=select_target
157 )
158
159 logger.info('Beginning MatcherProbabilistic.match with %d/%d ref sources selected vs %d/%d target',
160 np.sum(select_ref), len(select_ref), np.sum(select_target), len(select_target))
161
162 catalog_out_ref, catalog_out_target, exceptions = self.matcher.match(
163 catalog_ref,
164 catalog_target,
165 select_ref=select_ref,
166 select_target=select_target,
167 logger=logger,
168 logging_n_rows=logging_n_rows,
169 wcs=wcs,
170 radec_to_xy_func=radec_to_xy,
171 )
172
173 return catalog_out_ref, catalog_out_target, exceptions
174
175 @utils.timer.timeMethod
176 def run(
177 self,
178 catalog_ref: pd.DataFrame,
179 catalog_target: pd.DataFrame,
180 wcs: afwGeom.SkyWcs = None,
181 **kwargs,
182 ) -> pipeBase.Struct:
183 """Match sources in a reference tract catalog with a target catalog.
184
185 Parameters
186 ----------
187 catalog_ref : `pandas.DataFrame`
188 A reference catalog to match objects/sources from.
189 catalog_target : `pandas.DataFrame`
190 A target catalog to match reference objects/sources to.
191 wcs : `lsst.afw.image.SkyWcs`
192 A coordinate system to convert catalog positions to sky coordinates.
193 Only needed if `config.coords_ref_to_convert` is used to convert
194 reference catalog sky coordinates to pixel positions.
195 kwargs : Additional keyword arguments to pass to `match`.
196
197 Returns
198 -------
199 retStruct : `lsst.pipe.base.Struct`
200 A struct with output_ref and output_target attribute containing the
201 output matched catalogs, as well as a dict
202 """
203 catalog_ref.reset_index(inplace=True)
204 catalog_target.reset_index(inplace=True)
205 catalog_ref, catalog_target, exceptions = self.match(catalog_ref, catalog_target, wcs=wcs, **kwargs)
206 return pipeBase.Struct(cat_output_ref=catalog_ref, cat_output_target=catalog_target,
207 exceptions=exceptions)
208
209 def __init__(self, **kwargs):
210 super().__init__(**kwargs)
211 self.matcher = MatcherProbabilistic(self.config)
A 2-dimensional celestial WCS that transform pixels to ICRS RA/Dec, using the LSST standard for pixel...
Definition SkyWcs.h:117
Point in an unspecified spherical coordinate system.
Definition SpherePoint.h:57
pipeBase.Struct run(self, pd.DataFrame catalog_ref, pd.DataFrame catalog_target, afwGeom.SkyWcs wcs=None, **kwargs)
Tuple[pd.DataFrame, pd.DataFrame, Dict[int, str]] match(self, pd.DataFrame catalog_ref, pd.DataFrame catalog_target, np.array select_ref=None, np.array select_target=None, afwGeom.SkyWcs wcs=None, logging.Logger logger=None, int logging_n_rows=None)
np.array _apply_select_bool(pd.DataFrame catalog, List[str] columns_true, List[str] columns_false, Optional[np.array] selection)
radec_to_xy(ra_vec, dec_vec, factor, afwGeom.SkyWcs wcs)