Hide keyboard shortcuts

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/>. 

21 

22import unittest 

23 

24import astropy.units as u 

25 

26from lsst.afw.table import SourceCatalog 

27import lsst.utils.tests 

28from lsst.verify import Name 

29from lsst.verify.gen2tasks.testUtils import MetricTaskTestCase 

30from lsst.verify.tasks import MetricComputationError 

31 

32from lsst.ip.diffim.metrics import \ 

33 NumberSciSourcesMetricTask, \ 

34 FractionDiaSourcesToSciSourcesMetricTask 

35 

36 

37def _makeDummyCatalog(size): 

38 catalog = SourceCatalog(SourceCatalog.Table.makeMinimalSchema()) 

39 for i in range(size): 

40 catalog.addNew() 

41 return catalog 

42 

43 

44class TestNumSciSources(MetricTaskTestCase): 

45 

46 @classmethod 

47 def makeTask(cls): 

48 return NumberSciSourcesMetricTask() 

49 

50 def testValid(self): 

51 catalog = _makeDummyCatalog(3) 

52 result = self.task.run(catalog) 

53 meas = result.measurement 

54 

55 self.assertEqual(meas.metric_name, Name(metric="ip_diffim.numSciSources")) 

56 self.assertEqual(meas.quantity, len(catalog) * u.count) 

57 

58 def testEmptyCatalog(self): 

59 catalog = _makeDummyCatalog(0) 

60 result = self.task.run(catalog) 

61 meas = result.measurement 

62 

63 self.assertEqual(meas.metric_name, Name(metric="ip_diffim.numSciSources")) 

64 self.assertEqual(meas.quantity, 0 * u.count) 

65 

66 def testMissingData(self): 

67 result = self.task.run(None) 

68 meas = result.measurement 

69 self.assertIsNone(meas) 

70 

71 

72class TestFractionDiaSources(MetricTaskTestCase): 

73 

74 @classmethod 

75 def makeTask(cls): 

76 return FractionDiaSourcesToSciSourcesMetricTask() 

77 

78 def testValid(self): 

79 sciCatalog = _makeDummyCatalog(5) 

80 diaCatalog = _makeDummyCatalog(3) 

81 result = self.task.run(sciCatalog, diaCatalog) 

82 meas = result.measurement 

83 

84 self.assertEqual(meas.metric_name, Name(metric="ip_diffim.fracDiaSourcesToSciSources")) 

85 self.assertEqual(meas.quantity, len(diaCatalog) / len(sciCatalog) * u.dimensionless_unscaled) 

86 

87 def testEmptyDiaCatalog(self): 

88 sciCatalog = _makeDummyCatalog(5) 

89 diaCatalog = _makeDummyCatalog(0) 

90 result = self.task.run(sciCatalog, diaCatalog) 

91 meas = result.measurement 

92 

93 self.assertEqual(meas.metric_name, Name(metric="ip_diffim.fracDiaSourcesToSciSources")) 

94 self.assertEqual(meas.quantity, 0.0 * u.dimensionless_unscaled) 

95 

96 def testEmptySciCatalog(self): 

97 sciCatalog = _makeDummyCatalog(0) 

98 diaCatalog = _makeDummyCatalog(3) 

99 with self.assertRaises(MetricComputationError): 

100 self.task.run(sciCatalog, diaCatalog) 

101 

102 def testEmptyCatalogs(self): 

103 sciCatalog = _makeDummyCatalog(0) 

104 diaCatalog = _makeDummyCatalog(0) 

105 with self.assertRaises(MetricComputationError): 

106 self.task.run(sciCatalog, diaCatalog) 

107 

108 def testMissingData(self): 

109 result = self.task.run(None, None) 

110 meas = result.measurement 

111 self.assertIsNone(meas) 

112 

113 def testSemiMissingData(self): 

114 result = self.task.run(sciSources=_makeDummyCatalog(3), diaSources=None) 

115 meas = result.measurement 

116 self.assertIsNone(meas) 

117 

118 

119# Hack around unittest's hacky test setup system 

120del MetricTaskTestCase 

121 

122 

123class MemoryTester(lsst.utils.tests.MemoryTestCase): 

124 pass 

125 

126 

127def setup_module(module): 

128 lsst.utils.tests.init() 

129 

130 

131if __name__ == "__main__": 131 ↛ 132line 131 didn't jump to line 132, because the condition on line 131 was never true

132 lsst.utils.tests.init() 

133 unittest.main()