Coverage for tests/test_metrics.py: 30%
112 statements
« prev ^ index » next coverage.py v6.4.4, created at 2022-08-18 12:56 -0700
« prev ^ index » next coverage.py v6.4.4, created at 2022-08-18 12:56 -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 testMissingData(self):
104 """Test the run method with no data.
105 """
106 result = self.task.run(None, None)
107 testUtils.assertValidOutput(self.task, result)
108 meas = result.measurement
109 self.assertIsNone(meas)
111 def testValidEmpty(self):
112 """Test the run method with a valid but zero result.
113 """
114 metricComplete = self.makeTask(self.magCut, self.magCut + 5)
115 result = metricComplete.run(self.fakeCat, self.band)
116 testUtils.assertValidOutput(metricComplete, result)
118 meas = result.measurement
119 self.assertEqual(meas.metric_name, Name(metric="ap_pipe.apFakesCompleteness"))
120 self.assertEqual(meas.quantity, 0 * u.dimensionless_unscaled)
123class TestApCountTask(MetricTaskTestCase):
125 @classmethod
126 def makeTask(cls, magMin=20, magMax=25):
127 """Make the task and allow for modification of the config min and max.
129 Parameters
130 ----------
131 magMin : min magnitude, `float`
132 Minimum magnitude
133 magMax : min magnitude, `float`
134 Maximum magnitude
135 """
136 config = ApFakesCountMetricConfig()
137 config.magMin = magMin
138 config.magMax = magMax
140 return ApFakesCountMetricTask(config=config)
142 def setUp(self):
143 super().setUp()
145 simpleMapConfig = skyMap.discreteSkyMap.DiscreteSkyMapConfig()
146 simpleMapConfig.raList = [45]
147 simpleMapConfig.decList = [45]
148 simpleMapConfig.radiusList = [0.1]
150 self.simpleMap = skyMap.DiscreteSkyMap(simpleMapConfig)
151 self.tractId = 0
152 bCircle = self.simpleMap.generateTract(self.tractId).getInnerSkyPolygon().getBoundingCircle()
153 self.targetSources = 1000
154 self.sourceDensity = (self.targetSources
155 / (bCircle.getArea() * (180 / np.pi) ** 2))
157 fakesConfig = CreateRandomApFakesConfig()
158 fakesConfig.fraction = 0.0
159 fakesConfig.fakeDensity = self.sourceDensity
160 fakesTask = CreateRandomApFakesTask(config=fakesConfig)
161 fakeCat = fakesTask.run(self.tractId, self.simpleMap).fakeCat
163 self.band = 'g'
164 self.magCut = 25
165 magMask = (fakeCat[fakesConfig.mag_col % self.band] < self.magCut)
166 self.expectedAllMatched = magMask.sum()
167 ids = np.where(magMask, np.arange(1, len(fakeCat) + 1, dtype=int), 0)
168 # Add columns to mimic the matched fakes result without running the
169 # full pipeline.
170 self.fakeCat = fakeCat.assign(diaObjectId=ids,
171 filterName=["g"] * len(fakeCat),
172 diaSourceId=ids)
174 def testValid(self):
175 """Test the run method.
176 """
177 result = self.task.run(self.fakeCat, self.band)
178 testUtils.assertValidOutput(self.task, result)
180 meas = result.measurement
181 self.assertEqual(meas.metric_name, Name(metric="ap_pipe.apFakesCompleteness"))
182 # Work around for Mac failing this test.
183 self.assertAlmostEqual(
184 meas.quantity.value,
185 (self.expectedAllMatched * u.count).value,
186 places=2)
188 def testMissingData(self):
189 """Test the run method with no data.
190 """
191 result = self.task.run(None, None)
192 testUtils.assertValidOutput(self.task, result)
193 meas = result.measurement
194 self.assertIsNone(meas)
196 def testValidEmpty(self):
197 """Test the run method with a valid but zero result.
198 """
199 # Make a mag cut that will have no sources. 30 < g < 35.
200 metricComplete = self.makeTask(self.magCut + 5, self.magCut + 10)
201 result = metricComplete.run(self.fakeCat, self.band)
202 testUtils.assertValidOutput(metricComplete, result)
204 meas = result.measurement
205 self.assertEqual(meas.metric_name, Name(metric="ap_pipe.apFakesCompleteness"))
206 self.assertEqual(meas.quantity, 0 * u.count)
209# Hack around unittest's hacky test setup system
210del MetricTaskTestCase
213class MemoryTester(lsst.utils.tests.MemoryTestCase):
214 pass
217def setup_module(module):
218 lsst.utils.tests.init()
221if __name__ == "__main__": 221 ↛ 222line 221 didn't jump to line 222, because the condition on line 221 was never true
222 lsst.utils.tests.init()
223 unittest.main()