Coverage for tests/test_metrics.py: 27%
102 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-03-31 04:10 -0700
« prev ^ index » next coverage.py v6.5.0, created at 2023-03-31 04:10 -0700
1# This file is part of ap_pipe.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://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 <http://www.gnu.org/licenses/>.
21#
23import astropy.units as u
24import numpy as np
25import unittest
27from lsst.pipe.base import testUtils
28import lsst.skymap as skyMap
29import lsst.utils.tests
30from lsst.verify import Name
31from lsst.verify.tasks.testUtils import MetricTaskTestCase
33from lsst.ap.pipe.createApFakes import CreateRandomApFakesTask, CreateRandomApFakesConfig
34from lsst.ap.pipe.metrics import (ApFakesCompletenessMetricTask, ApFakesCompletenessMetricConfig,
35 ApFakesCountMetricTask, ApFakesCountMetricConfig)
38class TestApCompletenessTask(MetricTaskTestCase):
40 @classmethod
41 def makeTask(cls, magMin=20, magMax=30):
42 """Make the task and allow for modification of the config min and max.
44 Parameters
45 ----------
46 magMin : min magnitude, `float`
47 Minimum magnitude
48 magMax : min magnitude, `float`
49 Maximum magnitude
50 """
51 config = ApFakesCompletenessMetricConfig()
52 config.magMin = magMin
53 config.magMax = magMax
55 return ApFakesCompletenessMetricTask(config=config)
57 def setUp(self):
58 super().setUp()
60 simpleMapConfig = skyMap.discreteSkyMap.DiscreteSkyMapConfig()
61 simpleMapConfig.raList = [45]
62 simpleMapConfig.decList = [45]
63 simpleMapConfig.radiusList = [0.1]
65 self.simpleMap = skyMap.DiscreteSkyMap(simpleMapConfig)
66 self.tractId = 0
67 bCircle = self.simpleMap.generateTract(self.tractId).getInnerSkyPolygon().getBoundingCircle()
68 self.targetSources = 1000
69 self.sourceDensity = (self.targetSources
70 / (bCircle.getArea() * (180 / np.pi) ** 2))
72 fakesConfig = CreateRandomApFakesConfig()
73 fakesConfig.fraction = 0.0
74 fakesConfig.fakeDensity = self.sourceDensity
75 fakesTask = CreateRandomApFakesTask(config=fakesConfig)
76 fakeCat = fakesTask.run(self.tractId, self.simpleMap).fakeCat
78 self.band = 'g'
79 self.magCut = 25
80 magMask = (fakeCat[fakesConfig.mag_col % self.band] < self.magCut)
81 self.expectedAllMatched = magMask.sum()
82 ids = np.where(magMask, np.arange(1, len(fakeCat) + 1, dtype=int), 0)
83 # Add columns to mimic the matched fakes result without running the
84 # full pipeline.
85 self.fakeCat = fakeCat.assign(diaObjectId=ids,
86 filterName=["g"] * len(fakeCat),
87 diaSourceId=ids)
89 def testValid(self):
90 """Test the run method.
91 """
92 result = self.task.run(self.fakeCat, self.band)
93 testUtils.assertValidOutput(self.task, result)
95 meas = result.measurement
96 self.assertEqual(meas.metric_name, Name(metric="ap_pipe.apFakesCompleteness"))
97 # Work around for Mac failing this test.
98 self.assertAlmostEqual(
99 meas.quantity.value,
100 ((self.expectedAllMatched / self.targetSources) * u.dimensionless_unscaled).value,
101 places=2)
103 def testValidEmpty(self):
104 """Test the run method with a valid but zero result.
105 """
106 metricComplete = self.makeTask(self.magCut, self.magCut + 5)
107 result = metricComplete.run(self.fakeCat, self.band)
108 testUtils.assertValidOutput(metricComplete, result)
110 meas = result.measurement
111 self.assertEqual(meas.metric_name, Name(metric="ap_pipe.apFakesCompleteness"))
112 self.assertEqual(meas.quantity, 0 * u.dimensionless_unscaled)
115class TestApCountTask(MetricTaskTestCase):
117 @classmethod
118 def makeTask(cls, magMin=20, magMax=25):
119 """Make the task and allow for modification of the config min and max.
121 Parameters
122 ----------
123 magMin : min magnitude, `float`
124 Minimum magnitude
125 magMax : min magnitude, `float`
126 Maximum magnitude
127 """
128 config = ApFakesCountMetricConfig()
129 config.magMin = magMin
130 config.magMax = magMax
132 return ApFakesCountMetricTask(config=config)
134 def setUp(self):
135 super().setUp()
137 simpleMapConfig = skyMap.discreteSkyMap.DiscreteSkyMapConfig()
138 simpleMapConfig.raList = [45]
139 simpleMapConfig.decList = [45]
140 simpleMapConfig.radiusList = [0.1]
142 self.simpleMap = skyMap.DiscreteSkyMap(simpleMapConfig)
143 self.tractId = 0
144 bCircle = self.simpleMap.generateTract(self.tractId).getInnerSkyPolygon().getBoundingCircle()
145 self.targetSources = 1000
146 self.sourceDensity = (self.targetSources
147 / (bCircle.getArea() * (180 / np.pi) ** 2))
149 fakesConfig = CreateRandomApFakesConfig()
150 fakesConfig.fraction = 0.0
151 fakesConfig.fakeDensity = self.sourceDensity
152 fakesTask = CreateRandomApFakesTask(config=fakesConfig)
153 fakeCat = fakesTask.run(self.tractId, self.simpleMap).fakeCat
155 self.band = 'g'
156 self.magCut = 25
157 magMask = (fakeCat[fakesConfig.mag_col % self.band] < self.magCut)
158 self.expectedAllMatched = magMask.sum()
159 ids = np.where(magMask, np.arange(1, len(fakeCat) + 1, dtype=int), 0)
160 # Add columns to mimic the matched fakes result without running the
161 # full pipeline.
162 self.fakeCat = fakeCat.assign(diaObjectId=ids,
163 filterName=["g"] * len(fakeCat),
164 diaSourceId=ids)
166 def testValid(self):
167 """Test the run method.
168 """
169 result = self.task.run(self.fakeCat, self.band)
170 testUtils.assertValidOutput(self.task, result)
172 meas = result.measurement
173 self.assertEqual(meas.metric_name, Name(metric="ap_pipe.apFakesCompleteness"))
174 # Work around for Mac failing this test.
175 self.assertAlmostEqual(
176 meas.quantity.value,
177 (self.expectedAllMatched * u.count).value,
178 places=2)
180 def testValidEmpty(self):
181 """Test the run method with a valid but zero result.
182 """
183 # Make a mag cut that will have no sources. 30 < g < 35.
184 metricComplete = self.makeTask(self.magCut + 5, self.magCut + 10)
185 result = metricComplete.run(self.fakeCat, self.band)
186 testUtils.assertValidOutput(metricComplete, result)
188 meas = result.measurement
189 self.assertEqual(meas.metric_name, Name(metric="ap_pipe.apFakesCompleteness"))
190 self.assertEqual(meas.quantity, 0 * u.count)
193# Hack around unittest's hacky test setup system
194del MetricTaskTestCase
197class MemoryTester(lsst.utils.tests.MemoryTestCase):
198 pass
201def setup_module(module):
202 lsst.utils.tests.init()
205if __name__ == "__main__": 205 ↛ 206line 205 didn't jump to line 206, because the condition on line 205 was never true
206 lsst.utils.tests.init()
207 unittest.main()