Coverage for tests/test_metrics.py : 33%

Hot-keys 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 ip_diffim.
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 unittest
24import astropy.units as u
26from lsst.afw.table import SourceCatalog
27import lsst.utils.tests
28import lsst.pipe.base.testUtils
29from lsst.verify import Name
30from lsst.verify.gen2tasks.testUtils import MetricTaskTestCase
31from lsst.verify.tasks import MetricComputationError
33from lsst.ip.diffim.metrics import \
34 NumberSciSourcesMetricTask, \
35 FractionDiaSourcesToSciSourcesMetricTask
38def _makeDummyCatalog(size):
39 catalog = SourceCatalog(SourceCatalog.Table.makeMinimalSchema())
40 for i in range(size):
41 catalog.addNew()
42 return catalog
45class TestNumSciSources(MetricTaskTestCase):
47 @classmethod
48 def makeTask(cls):
49 return NumberSciSourcesMetricTask()
51 def testValid(self):
52 catalog = _makeDummyCatalog(3)
53 result = self.task.run(catalog)
54 lsst.pipe.base.testUtils.assertValidOutput(self.task, result)
55 meas = result.measurement
57 self.assertEqual(meas.metric_name, Name(metric="ip_diffim.numSciSources"))
58 self.assertEqual(meas.quantity, len(catalog) * u.count)
60 def testEmptyCatalog(self):
61 catalog = _makeDummyCatalog(0)
62 result = self.task.run(catalog)
63 lsst.pipe.base.testUtils.assertValidOutput(self.task, result)
64 meas = result.measurement
66 self.assertEqual(meas.metric_name, Name(metric="ip_diffim.numSciSources"))
67 self.assertEqual(meas.quantity, 0 * u.count)
69 def testMissingData(self):
70 result = self.task.run(None)
71 lsst.pipe.base.testUtils.assertValidOutput(self.task, result)
72 meas = result.measurement
73 self.assertIsNone(meas)
76class TestFractionDiaSources(MetricTaskTestCase):
78 @classmethod
79 def makeTask(cls):
80 return FractionDiaSourcesToSciSourcesMetricTask()
82 def testValid(self):
83 sciCatalog = _makeDummyCatalog(5)
84 diaCatalog = _makeDummyCatalog(3)
85 result = self.task.run(sciCatalog, diaCatalog)
86 lsst.pipe.base.testUtils.assertValidOutput(self.task, result)
87 meas = result.measurement
89 self.assertEqual(meas.metric_name, Name(metric="ip_diffim.fracDiaSourcesToSciSources"))
90 self.assertEqual(meas.quantity, len(diaCatalog) / len(sciCatalog) * u.dimensionless_unscaled)
92 def testEmptyDiaCatalog(self):
93 sciCatalog = _makeDummyCatalog(5)
94 diaCatalog = _makeDummyCatalog(0)
95 result = self.task.run(sciCatalog, diaCatalog)
96 lsst.pipe.base.testUtils.assertValidOutput(self.task, result)
97 meas = result.measurement
99 self.assertEqual(meas.metric_name, Name(metric="ip_diffim.fracDiaSourcesToSciSources"))
100 self.assertEqual(meas.quantity, 0.0 * u.dimensionless_unscaled)
102 def testEmptySciCatalog(self):
103 sciCatalog = _makeDummyCatalog(0)
104 diaCatalog = _makeDummyCatalog(3)
105 with self.assertRaises(MetricComputationError):
106 self.task.run(sciCatalog, diaCatalog)
108 def testEmptyCatalogs(self):
109 sciCatalog = _makeDummyCatalog(0)
110 diaCatalog = _makeDummyCatalog(0)
111 with self.assertRaises(MetricComputationError):
112 self.task.run(sciCatalog, diaCatalog)
114 def testMissingData(self):
115 result = self.task.run(None, None)
116 lsst.pipe.base.testUtils.assertValidOutput(self.task, result)
117 meas = result.measurement
118 self.assertIsNone(meas)
120 def testSemiMissingData(self):
121 result = self.task.run(sciSources=_makeDummyCatalog(3), diaSources=None)
122 lsst.pipe.base.testUtils.assertValidOutput(self.task, result)
123 meas = result.measurement
124 self.assertIsNone(meas)
127# Hack around unittest's hacky test setup system
128del MetricTaskTestCase
131class MemoryTester(lsst.utils.tests.MemoryTestCase):
132 pass
135def setup_module(module):
136 lsst.utils.tests.init()
139if __name__ == "__main__": 139 ↛ 140line 139 didn't jump to line 140, because the condition on line 139 was never true
140 lsst.utils.tests.init()
141 unittest.main()