Coverage for tests/test_diff_matched_tract_catalog.py: 20%
83 statements
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-14 02:17 -0700
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-14 02:17 -0700
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/>.
23import unittest
24import lsst.utils.tests
26import lsst.afw.geom as afwGeom
27from lsst.meas.astrom import ConvertCatalogCoordinatesConfig
28from lsst.pipe.tasks.diff_matched_tract_catalog import (
29 DiffMatchedTractCatalogConfig, DiffMatchedTractCatalogTask, MatchedCatalogFluxesConfig,
30)
32import numpy as np
33import os
34import pandas as pd
37def _error_format(column):
38 return f'{column}Err'
41ROOT = os.path.dirname(__file__)
42filename_diff_matched = os.path.join(ROOT, "data", "test_diff_matched.txt")
45class DiffMatchedTractCatalogTaskTestCase(lsst.utils.tests.TestCase):
46 """DiffMatchedTractCatalogTask test case."""
47 def setUp(self):
48 ra = np.array([-5.1, -2.2, 0., 3.1, -3.2, 2.01, -4.1])
49 dec = np.array([-4.15, 1.15, 0, 2.15, -7.15, -3.05, 5.7])
50 mag_g = np.array([23., 24., 25., 25.5, 26., 24.7, 23.3])
51 mag_r = mag_g + [0.5, -0.2, -0.8, -0.5, -1.5, 0.8, -0.4]
53 coord_format = ConvertCatalogCoordinatesConfig
54 zeropoint = coord_format.mag_zeropoint_ref.default
55 fluxes = tuple(10**(-0.4*(mag - zeropoint)) for mag in (mag_g, mag_r))
56 # Percent error in measurement
57 err_flux = np.array((0.02, 0.015, -0.035, 0.02, -0.04, 0.06, 0.01))
58 # Absolute error
59 eps_coord = np.array((2.3, 0.6, -1.7, 3.6, -2.4, 55.0, -40.8))
60 err_coord = np.full_like(eps_coord, 0.02)
61 eps_coord *= err_coord
62 extended_ref = [True, False, True, False, True, False, True]
63 extended_target = [False, False, True, True, True, False, True]
64 flags = np.ones_like(eps_coord, dtype=bool)
66 bands = ['g', 'r']
68 columns_flux = [f'flux_{band}' for band in bands]
69 columns_flux_err = [_error_format(column) for column in columns_flux]
71 column_ra_ref = coord_format.column_ref_coord1.default
72 column_dec_ref = coord_format.column_ref_coord2.default
73 column_ra_target = coord_format.column_target_coord1.default
74 column_dec_target = coord_format.column_target_coord2.default
76 column_ra_target_err, column_dec_target_err = [
77 _error_format(col) for col in (column_ra_target, column_dec_target)
78 ]
80 n_points = len(ra)
81 n_unmatched = 2
82 n_matched = n_points - n_unmatched
83 # Reorder some indices to make arbitrary differences
84 idx_ref = np.empty(n_points, dtype=int)
85 idx_ref[:n_matched] = np.arange(n_matched)[::-1]
86 idx_ref[n_matched:] = np.arange(n_matched, n_points)
87 data_ref = {
88 column_ra_ref: ra[idx_ref],
89 column_dec_ref: dec[idx_ref],
90 columns_flux[0]: fluxes[0][idx_ref],
91 columns_flux[1]: fluxes[1][idx_ref],
92 DiffMatchedTractCatalogConfig.column_ref_extended.default: extended_ref,
93 }
94 self.catalog_ref = pd.DataFrame(data=data_ref)
96 data_target = {
97 column_ra_target: ra + eps_coord,
98 column_dec_target: dec + eps_coord,
99 column_ra_target_err: err_coord,
100 column_dec_target_err: err_coord,
101 columns_flux[0]: fluxes[0]*(1 + err_flux),
102 columns_flux[1]: fluxes[1]*(1 + err_flux),
103 _error_format(columns_flux[0]): np.sqrt(fluxes[0]),
104 _error_format(columns_flux[1]): np.sqrt(fluxes[1]),
105 DiffMatchedTractCatalogConfig.columns_target_select_true.default[0]: flags,
106 DiffMatchedTractCatalogConfig.columns_target_select_false.default[0]: ~flags,
107 DiffMatchedTractCatalogConfig.column_target_extended.default: extended_target,
108 }
109 self.catalog_target = pd.DataFrame(data=data_target)
111 # Make the last two rows unmatched (we set eps_coord very large)
112 match_row = np.arange(len(ra))[::-1] - n_unmatched
113 self.catalog_match_ref = pd.DataFrame(data={
114 'match_candidate': flags,
115 'match_row': match_row,
116 })
118 self.catalog_match_target = pd.DataFrame(data={
119 'match_candidate': flags,
120 'match_row': match_row,
121 })
123 self.diff_matched = np.loadtxt(filename_diff_matched)
125 columns_flux_configs = {
126 band: MatchedCatalogFluxesConfig(
127 column_ref_flux=columns_flux[idx],
128 columns_target_flux=[columns_flux[idx]],
129 columns_target_flux_err=[columns_flux_err[idx]],
130 )
131 for idx, band in enumerate(bands)
132 }
134 self.task = DiffMatchedTractCatalogTask(config=DiffMatchedTractCatalogConfig(
135 columns_target_coord_err=[column_ra_target_err, column_dec_target_err],
136 columns_flux=columns_flux_configs,
137 mag_num_bins=1,
138 ))
139 self.wcs = afwGeom.makeSkyWcs(crpix=lsst.geom.Point2D(9000, 9000),
140 crval=lsst.geom.SpherePoint(180., 0., lsst.geom.degrees),
141 cdMatrix=afwGeom.makeCdMatrix(scale=0.2*lsst.geom.arcseconds))
143 def tearDown(self):
144 del self.catalog_ref
145 del self.catalog_target
146 del self.catalog_match_ref
147 del self.catalog_match_target
148 del self.diff_matched
149 del self.task
150 del self.wcs
152 def test_DiffMatchedTractCatalogTask(self):
153 # These tables will have columns added to them in run
154 columns_ref, columns_target = (list(x.columns) for x in (self.catalog_ref, self.catalog_target))
155 result = self.task.run(
156 catalog_ref=self.catalog_ref,
157 catalog_target=self.catalog_target,
158 catalog_match_ref=self.catalog_match_ref,
159 catalog_match_target=self.catalog_match_target,
160 wcs=self.wcs,
161 )
162 columns_result = list(result.cat_matched.columns)
163 columns_expect = list(columns_target) + ["match_distance", "match_distanceErr"]
164 prefix = DiffMatchedTractCatalogConfig.column_matched_prefix_ref.default
165 columns_expect.append(f'{prefix}index')
166 columns_expect.extend((f'{prefix}{col}' for col in columns_ref))
167 self.assertEqual(columns_expect, columns_result)
169 row = result.diff_matched.iloc[0].values.astype(float)
170 # Run to re-save reference data. Will be loaded after this test completes.
171 resave = False
172 if resave:
173 np.savetxt(filename_diff_matched, row)
175 self.assertEqual(len(row), len(self.diff_matched))
176 self.assertFloatsAlmostEqual(row, self.diff_matched, rtol=1e-15)
179class MemoryTester(lsst.utils.tests.MemoryTestCase):
180 pass
183def setup_module(module):
184 lsst.utils.tests.init()
187if __name__ == "__main__": 187 ↛ 188line 187 didn't jump to line 188, because the condition on line 187 was never true
188 lsst.utils.tests.init()
189 unittest.main()