Coverage for tests/test_diff_matched_tract_catalog.py: 25%
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/>.
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 pandas as pd
36def _error_format(column):
37 return f'{column}Err'
40class DiffMatchedTractCatalogTaskTestCase(lsst.utils.tests.TestCase):
41 """DiffMatchedTractCatalogTask test case."""
42 def setUp(self):
43 ra = np.array([-5.1, -2.2, 0., 3.1, -3.2])
44 dec = np.array([-4.15, 1.15, 0, 2.15, -7.15])
45 mag_g = np.array([23., 24., 25., 25.5, 26.])
46 mag_r = mag_g + [0.5, -0.2, -0.8, -0.5, -1.5]
48 coord_format = ConvertCatalogCoordinatesConfig
49 zeropoint = coord_format.mag_zeropoint_ref.default
50 fluxes = tuple(10**(-0.4*(mag - zeropoint)) for mag in (mag_g, mag_r))
51 # Percent error in measurement
52 err_flux = np.array((0.02, 0.015, -0.035, 0.02, -0.04))
53 # Absolute error
54 eps_coord = np.array((2.3, 0.6, -1.7, 3.6, -2.4))
55 err_coord = np.full_like(eps_coord, 0.02)
56 eps_coord *= err_coord
57 extended_ref = [True, False, True, False, True]
58 extended_target = [False, False, True, True, True]
59 flags = np.ones_like(eps_coord, dtype=bool)
61 bands = ['g', 'r']
63 columns_flux = [f'flux_{band}' for band in bands]
64 columns_flux_err = [_error_format(column) for column in columns_flux]
66 column_ra_ref = coord_format.column_ref_coord1.default
67 column_dec_ref = coord_format.column_ref_coord2.default
68 column_ra_target = coord_format.column_target_coord1.default
69 column_dec_target = coord_format.column_target_coord2.default
71 column_ra_target_err, column_dec_target_err = [
72 _error_format(col) for col in (column_ra_target, column_dec_target)
73 ]
75 data_ref = {
76 column_ra_ref: ra[::-1],
77 column_dec_ref: dec[::-1],
78 columns_flux[0]: fluxes[0][::-1],
79 columns_flux[1]: fluxes[1][::-1],
80 DiffMatchedTractCatalogConfig.column_ref_extended.default: extended_ref,
81 }
82 self.catalog_ref = pd.DataFrame(data=data_ref)
84 data_target = {
85 column_ra_target: ra + eps_coord,
86 column_dec_target: dec + eps_coord,
87 column_ra_target_err: err_coord,
88 column_dec_target_err: err_coord,
89 columns_flux[0]: fluxes[0]*(1 + err_flux),
90 columns_flux[1]: fluxes[1]*(1 + err_flux),
91 _error_format(columns_flux[0]): np.sqrt(fluxes[0]),
92 _error_format(columns_flux[1]): np.sqrt(fluxes[1]),
93 DiffMatchedTractCatalogConfig.columns_target_select_true.default[0]: flags,
94 DiffMatchedTractCatalogConfig.columns_target_select_false.default[0]: ~flags,
95 DiffMatchedTractCatalogConfig.column_target_extended.default: extended_target,
96 }
97 self.catalog_target = pd.DataFrame(data=data_target)
99 self.catalog_match_ref = pd.DataFrame(data={
100 'match_candidate': flags,
101 'match_row': np.arange(len(ra))[::-1],
102 })
104 self.catalog_match_target = pd.DataFrame(data={
105 'match_candidate': flags,
106 'match_row': np.arange(len(ra))[::-1],
107 })
109 columns_flux_configs = {
110 band: MatchedCatalogFluxesConfig(
111 column_ref_flux=columns_flux[idx],
112 columns_target_flux=[columns_flux[idx]],
113 columns_target_flux_err=[columns_flux_err[idx]],
114 )
115 for idx, band in enumerate(bands)
116 }
118 self.task = DiffMatchedTractCatalogTask(config=DiffMatchedTractCatalogConfig(
119 columns_target_coord_err=[column_ra_target_err, column_dec_target_err],
120 columns_flux=columns_flux_configs,
121 ))
122 self.wcs = afwGeom.makeSkyWcs(crpix=lsst.geom.Point2D(9000, 9000),
123 crval=lsst.geom.SpherePoint(180., 0., lsst.geom.degrees),
124 cdMatrix=afwGeom.makeCdMatrix(scale=0.2*lsst.geom.arcseconds))
126 def tearDown(self):
127 del self.catalog_ref
128 del self.catalog_target
129 del self.catalog_match_ref
130 del self.catalog_match_target
131 del self.task
132 del self.wcs
134 def test_DiffMatchedTractCatalogTask(self):
135 # These tables will have columns added to them in run
136 columns_ref, columns_target = (list(x.columns) for x in (self.catalog_ref, self.catalog_target))
137 result = self.task.run(
138 catalog_ref=self.catalog_ref,
139 catalog_target=self.catalog_target,
140 catalog_match_ref=self.catalog_match_ref,
141 catalog_match_target=self.catalog_match_target,
142 wcs=self.wcs,
143 )
144 columns_expect = columns_target
145 prefix = DiffMatchedTractCatalogConfig.column_matched_prefix_ref.default
146 columns_expect.append(f'{prefix}index')
147 columns_expect.extend((f'{prefix}{col}' for col in columns_ref))
148 self.assertEqual(columns_expect, list(result.cat_matched.columns))
151class MemoryTester(lsst.utils.tests.MemoryTestCase):
152 pass
155def setup_module(module):
156 lsst.utils.tests.init()
159if __name__ == "__main__": 159 ↛ 160line 159 didn't jump to line 160, because the condition on line 159 was never true
160 lsst.utils.tests.init()
161 unittest.main()