Coverage for tests/test_diaPipe.py: 28%

Shortcuts 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

74 statements  

1# This file is part of ap_association. 

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 

23import numpy as np 

24import pandas as pd 

25 

26import lsst.afw.image as afwImage 

27import lsst.afw.table as afwTable 

28import lsst.pipe.base as pipeBase 

29import lsst.utils.tests 

30from unittest.mock import patch, Mock, MagicMock, DEFAULT 

31 

32from lsst.ap.association import DiaPipelineTask 

33 

34 

35class TestDiaPipelineTask(unittest.TestCase): 

36 

37 @classmethod 

38 def _makeDefaultConfig(cls, 

39 doPackageAlerts=False, 

40 doSolarSystemAssociation=False): 

41 config = DiaPipelineTask.ConfigClass() 

42 config.apdb.db_url = "sqlite://" 

43 config.doPackageAlerts = doPackageAlerts 

44 config.doSolarSystemAssociation = doSolarSystemAssociation 

45 return config 

46 

47 def setUp(self): 

48 # schemas are persisted in both Gen 2 and Gen 3 butler as prototypical catalogs 

49 srcSchema = afwTable.SourceTable.makeMinimalSchema() 

50 srcSchema.addField("base_PixelFlags_flag", type="Flag") 

51 srcSchema.addField("base_PixelFlags_flag_offimage", type="Flag") 

52 self.srcSchema = afwTable.SourceCatalog(srcSchema) 

53 

54 def tearDown(self): 

55 pass 

56 

57 def testRun(self): 

58 """Test running while creating and packaging alerts. 

59 """ 

60 self._testRun(doPackageAlerts=True, doSolarSystemAssociation=True) 

61 

62 def testRunWithSolarSystemAssociation(self): 

63 """Test running while creating and packaging alerts. 

64 """ 

65 self._testRun(doPackageAlerts=False, doSolarSystemAssociation=True) 

66 

67 def testRunWithAlerts(self): 

68 """Test running while creating and packaging alerts. 

69 """ 

70 self._testRun(doPackageAlerts=True, doSolarSystemAssociation=False) 

71 

72 def testRunWithoutAlertsOrSolarSystem(self): 

73 """Test running without creating and packaging alerts. 

74 """ 

75 self._testRun(doPackageAlerts=False, doSolarSystemAssociation=False) 

76 

77 def _testRun(self, doPackageAlerts=False, doSolarSystemAssociation=False): 

78 """Test the normal workflow of each ap_pipe step. 

79 """ 

80 config = self._makeDefaultConfig( 

81 doPackageAlerts=doPackageAlerts, 

82 doSolarSystemAssociation=doSolarSystemAssociation) 

83 task = DiaPipelineTask(config=config) 

84 # Set DataFrame index testing to always return False. Mocks return 

85 # true for this check otherwise. 

86 task.testDataFrameIndex = lambda x: False 

87 diffIm = Mock(spec=afwImage.ExposureF) 

88 exposure = Mock(spec=afwImage.ExposureF) 

89 template = Mock(spec=afwImage.ExposureF) 

90 diaSrc = MagicMock(spec=pd.DataFrame()) 

91 ssObjects = MagicMock(spec=pd.DataFrame()) 

92 ccdExposureIdBits = 32 

93 

94 # Each of these subtasks should be called once during diaPipe 

95 # execution. We use mocks here to check they are being executed 

96 # appropriately. 

97 subtasksToMock = [ 

98 "diaCatalogLoader", 

99 "associator", 

100 "diaCalculation", 

101 "diaForcedSource", 

102 ] 

103 if doPackageAlerts: 

104 subtasksToMock.append("alertPackager") 

105 else: 

106 self.assertFalse(hasattr(task, "alertPackager")) 

107 

108 if doSolarSystemAssociation: 

109 subtasksToMock.append("solarSystemAssociator") 

110 else: 

111 self.assertFalse(hasattr(task, "solarSystemAssociator")) 

112 

113 # apdb isn't a subtask, but still needs to be mocked out for correct 

114 # execution in the test environment. 

115 def concatMock(data): 

116 return MagicMock(spec=pd.DataFrame) 

117 with patch.multiple( 

118 task, **{task: DEFAULT for task in subtasksToMock + ["apdb"]} 

119 ): 

120 with patch('lsst.ap.association.diaPipe.pd.concat', 

121 new=concatMock): 

122 result = task.run(diaSrc, 

123 ssObjects, 

124 diffIm, 

125 exposure, 

126 template, 

127 ccdExposureIdBits, 

128 "g") 

129 for subtaskName in subtasksToMock: 

130 getattr(task, subtaskName).run.assert_called_once() 

131 pipeBase.testUtils.assertValidOutput(task, result) 

132 self.assertEqual(result.apdbMarker.db_url, "sqlite://") 

133 

134 def test_createDiaObjects(self): 

135 """Test that creating new DiaObjects works as expected. 

136 """ 

137 nSources = 5 

138 diaSources = pd.DataFrame(data=[ 

139 {"ra": 0.04*idx, "decl": 0.04*idx, 

140 "diaSourceId": idx + 1 + nSources, "diaObjectId": 0, 

141 "ssObjectId": 0} 

142 for idx in range(nSources)]) 

143 

144 config = self._makeDefaultConfig(doPackageAlerts=False) 

145 task = DiaPipelineTask(config=config) 

146 result = task.createNewDiaObjects(diaSources) 

147 self.assertEqual(nSources, len(result.newDiaObjects)) 

148 self.assertTrue(np.all(np.equal( 

149 result.diaSources["diaObjectId"].to_numpy(), 

150 result.diaSources["diaSourceId"].to_numpy()))) 

151 self.assertTrue(np.all(np.equal( 

152 result.newDiaObjects["diaObjectId"].to_numpy(), 

153 result.diaSources["diaSourceId"].to_numpy()))) 

154 

155 

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

157 pass 

158 

159 

160def setup_module(module): 

161 lsst.utils.tests.init() 

162 

163 

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

165 lsst.utils.tests.init() 

166 unittest.main()