Coverage for python/lsst/meas/astrom/match_probabilistic_task.py: 26%
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 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/>.
22import lsst.afw.geom as afwGeom
23import lsst.geom as geom
24import lsst.pipe.base as pipeBase
25from .matcher_probabilistic import MatchProbabilisticConfig, MatcherProbabilistic
27import logging
28import numpy as np
29import pandas as pd
30from typing import Dict, Set, Tuple
32__all__ = ['MatchProbabilisticTask']
35class MatchProbabilisticTask(pipeBase.Task):
36 """Run MatchProbabilistic on a reference and target catalog covering the same tract.
37 """
38 ConfigClass = MatchProbabilisticConfig
39 _DefaultName = "matchProbabilistic"
41 @property
42 def columns_in_ref(self) -> Set[str]:
43 return self.config.columns_in_ref()
45 @property
46 def columns_in_target(self) -> Set[str]:
47 return self.config.columns_in_target()
49 def match(
50 self,
51 catalog_ref: pd.DataFrame,
52 catalog_target: pd.DataFrame,
53 select_ref: np.array = None,
54 select_target: np.array = None,
55 wcs: afwGeom.SkyWcs = None,
56 logger: logging.Logger = None,
57 logging_n_rows: int = None,
58 ) -> Tuple[pd.DataFrame, pd.DataFrame, Dict[int, str]]:
59 """Match sources in a reference tract catalog with a target catalog.
61 Parameters
62 ----------
63 catalog_ref : `pandas.DataFrame`
64 A reference catalog to match objects/sources from.
65 catalog_target : `pandas.DataFrame`
66 A target catalog to match reference objects/sources to.
67 select_ref : `numpy.array`
68 A boolean array of the same length as `catalog_ref` selecting the sources that can be matched.
69 select_target : `numpy.array`
70 A boolean array of the same length as `catalog_target` selecting the sources that can be matched.
71 wcs : `lsst.afw.image.SkyWcs`
72 A coordinate system to convert catalog positions to sky coordinates. Only used if
73 `self.config.coords_ref_to_convert` is set.
74 logger : `logging.Logger`
75 A Logger for logging.
76 logging_n_rows : `int`
77 Number of matches to make before outputting incremental log message.
79 Returns
80 -------
81 catalog_out_ref : `pandas.DataFrame`
82 Reference matched catalog with indices of target matches.
83 catalog_out_target : `pandas.DataFrame`
84 Reference matched catalog with indices of target matches.
85 """
86 if logger is None:
87 logger = self.log
89 config = self.config
91 if config.column_order is None:
92 flux_tot = np.nansum(catalog_ref.loc[:, config.columns_ref_flux].values, axis=1)
93 catalog_ref['flux_total'] = flux_tot
94 if config.mag_brightest_ref != -np.inf or config.mag_faintest_ref != np.inf:
95 mag_tot = -2.5 * np.log10(flux_tot) + config.mag_zeropoint_ref
96 select_mag = (mag_tot >= config.mag_brightest_ref) & (
97 mag_tot <= config.mag_faintest_ref)
98 else:
99 select_mag = np.isfinite(flux_tot)
100 if select_ref is None:
101 select_ref = select_mag
102 else:
103 select_ref &= select_mag
105 if config.coords_ref_to_convert:
106 ra_ref, dec_ref = [catalog_ref[column] for column in config.coords_ref_to_convert.keys()]
107 factor = config.coords_ref_factor
108 radec_true = [geom.SpherePoint(ra*factor, dec*factor, geom.degrees)
109 for ra, dec in zip(ra_ref, dec_ref)]
110 xy_true = wcs.skyToPixel(radec_true)
112 for idx_coord, column_out in enumerate(config.coords_ref_to_convert.values()):
113 catalog_ref[column_out] = np.array([xy[idx_coord] for xy in xy_true])
115 select_additional = (len(config.columns_target_select_true)
116 + len(config.columns_target_select_false)) > 0
117 if select_additional:
118 if select_target is None:
119 select_target = np.ones(len(catalog_target), dtype=bool)
120 for column in config.columns_target_select_true:
121 select_target &= catalog_target[column].values
122 for column in config.columns_target_select_false:
123 select_target &= ~catalog_target[column].values
125 logger.info('Beginning MatcherProbabilistic.match with %d/%d ref sources selected vs %d/%d target',
126 np.sum(select_ref), len(select_ref), np.sum(select_target), len(select_target))
128 catalog_out_ref, catalog_out_target, exceptions = self.matcher.match(
129 catalog_ref,
130 catalog_target,
131 select_ref=select_ref,
132 select_target=select_target,
133 logger=logger,
134 logging_n_rows=logging_n_rows,
135 )
137 return catalog_out_ref, catalog_out_target, exceptions
139 @pipeBase.timeMethod
140 def run(
141 self,
142 catalog_ref: pd.DataFrame,
143 catalog_target: pd.DataFrame,
144 wcs: afwGeom.SkyWcs = None,
145 **kwargs,
146 ) -> pipeBase.Struct:
147 """Match sources in a reference tract catalog with a target catalog.
149 Parameters
150 ----------
151 catalog_ref : `pandas.DataFrame`
152 A reference catalog to match objects/sources from.
153 catalog_target : `pandas.DataFrame`
154 A target catalog to match reference objects/sources to.
155 wcs : `lsst.afw.image.SkyWcs`
156 A coordinate system to convert catalog positions to sky coordinates.
157 Only needed if `config.coords_ref_to_convert` is used to convert
158 reference catalog sky coordinates to pixel positions.
159 kwargs : Additional keyword arguments to pass to `match`.
161 Returns
162 -------
163 retStruct : `lsst.pipe.base.Struct`
164 A struct with output_ref and output_target attribute containing the
165 output matched catalogs, as well as a dict
166 """
167 catalog_ref, catalog_target, exceptions = self.match(catalog_ref, catalog_target, wcs=wcs, **kwargs)
168 return pipeBase.Struct(cat_output_ref=catalog_ref, cat_output_target=catalog_target,
169 exceptions=exceptions)
171 def __init__(self, **kwargs):
172 super().__init__(**kwargs)
173 self.matcher = MatcherProbabilistic(self.config)