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 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# 

22 

23import astropy.units as u 

24import numpy as np 

25import unittest 

26 

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 

32 

33from lsst.ap.pipe.createApFakes import CreateRandomApFakesTask, CreateRandomApFakesConfig 

34from lsst.ap.pipe.metrics import ApFakesCompletenessMetricTask, ApFakesCompletenessMetricConfig 

35 

36 

37class TestApCompletenessTask(MetricTaskTestCase): 

38 

39 @classmethod 

40 def makeTask(cls, magMin=20, magMax=30): 

41 """Make the task and allow for modification of the config min and max. 

42 

43 Parameters 

44 ---------- 

45 magMin : min magnitude, `float` 

46 Minimum magnitude 

47 magMax : min magnitude, `float` 

48 Maximum magnitude 

49 """ 

50 config = ApFakesCompletenessMetricConfig() 

51 config.magMin = magMin 

52 config.magMax = magMax 

53 

54 return ApFakesCompletenessMetricTask(config=config) 

55 

56 def setUp(self): 

57 super().setUp() 

58 

59 simpleMapConfig = skyMap.discreteSkyMap.DiscreteSkyMapConfig() 

60 simpleMapConfig.raList = [45] 

61 simpleMapConfig.decList = [45] 

62 simpleMapConfig.radiusList = [0.1] 

63 

64 self.simpleMap = skyMap.DiscreteSkyMap(simpleMapConfig) 

65 self.tractId = 0 

66 bCircle = self.simpleMap.generateTract(self.tractId).getInnerSkyPolygon().getBoundingCircle() 

67 self.targetSources = 1000 

68 self.sourceDensity = (self.targetSources 

69 / (bCircle.getArea() * (180 / np.pi) ** 2)) 

70 

71 fakesConfig = CreateRandomApFakesConfig() 

72 fakesConfig.fraction = 0.0 

73 fakesConfig.fakeDensity = self.sourceDensity 

74 fakesTask = CreateRandomApFakesTask(config=fakesConfig) 

75 fakeCat = fakesTask.run(self.tractId, self.simpleMap).fakeCat 

76 

77 self.band = 'g' 

78 self.magCut = 25 

79 magMask = (fakeCat[fakesConfig.magVar % self.band] < self.magCut) 

80 self.expectedAllMatched = magMask.sum() 

81 ids = np.where(magMask, np.arange(1, len(fakeCat) + 1, dtype=int), 0) 

82 # Add columns to mimic the matched fakes result without running the 

83 # full pipeline. 

84 self.fakeCat = fakeCat.assign(diaObjectId=ids, 

85 filterName=["g"] * len(fakeCat), 

86 diaSourceId=ids) 

87 

88 def testValid(self): 

89 """Test the run method. 

90 """ 

91 result = self.task.run(self.fakeCat, self.band) 

92 testUtils.assertValidOutput(self.task, result) 

93 

94 meas = result.measurement 

95 self.assertEqual(meas.metric_name, Name(metric="ap_pipe.apFakesCompleteness")) 

96 # Work around for Mac failing this test. 

97 self.assertAlmostEqual( 

98 meas.quantity.value, 

99 ((self.expectedAllMatched / self.targetSources) * u.dimensionless_unscaled).value, 

100 places=2) 

101 

102 def testMissingData(self): 

103 """Test the run method with no data. 

104 """ 

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

106 testUtils.assertValidOutput(self.task, result) 

107 meas = result.measurement 

108 self.assertIsNone(meas) 

109 

110 def testValidEmpty(self): 

111 """Test the run method with a valid but zero result. 

112 """ 

113 metricComplete = self.makeTask(self.magCut, self.magCut + 5) 

114 result = metricComplete.run(self.fakeCat, self.band) 

115 testUtils.assertValidOutput(metricComplete, result) 

116 

117 meas = result.measurement 

118 self.assertEqual(meas.metric_name, Name(metric="ap_pipe.apFakesCompleteness")) 

119 self.assertEqual(meas.quantity, 0 * u.dimensionless_unscaled) 

120 

121 

122# Hack around unittest's hacky test setup system 

123del MetricTaskTestCase 

124 

125 

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

127 pass 

128 

129 

130def setup_module(module): 

131 lsst.utils.tests.init() 

132 

133 

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

135 lsst.utils.tests.init() 

136 unittest.main()