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_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, DEFAULT 

31 

32from lsst.ap.association import DiaPipelineTask 

33 

34 

35class TestDiaPipelineTask(unittest.TestCase): 

36 

37 @classmethod 

38 def _makeDefaultConfig(cls, doPackageAlerts=False): 

39 config = DiaPipelineTask.ConfigClass() 

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

41 config.doPackageAlerts = doPackageAlerts 

42 return config 

43 

44 def setUp(self): 

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

46 srcSchema = afwTable.SourceTable.makeMinimalSchema() 

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

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

49 self.srcSchema = afwTable.SourceCatalog(srcSchema) 

50 

51 def tearDown(self): 

52 pass 

53 

54 def testRunWithAlerts(self): 

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

56 """ 

57 self._testRun(True) 

58 

59 def testRunWithoutAlerts(self): 

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

61 """ 

62 self._testRun(False) 

63 

64 def _testRun(self, doPackageAlerts=False): 

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

66 """ 

67 config = self._makeDefaultConfig(doPackageAlerts=doPackageAlerts) 

68 task = DiaPipelineTask(config=config) 

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

70 # true for this check otherwise. 

71 task.testDataFrameIndex = lambda x: False 

72 diffIm = Mock(spec=afwImage.ExposureF) 

73 exposure = Mock(spec=afwImage.ExposureF) 

74 template = Mock(spec=afwImage.ExposureF) 

75 diaSrc = Mock(sepc=pd.DataFrame) 

76 ccdExposureIdBits = 32 

77 

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

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

80 # appropriately. 

81 subtasksToMock = [ 

82 "diaCatalogLoader", 

83 "associator", 

84 "diaCalculation", 

85 "diaForcedSource", 

86 ] 

87 if doPackageAlerts: 

88 subtasksToMock.append("alertPackager") 

89 else: 

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

91 

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

93 # execution in the test environment. 

94 with patch.multiple( 

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

96 ): 

97 result = task.run(diaSrc, 

98 diffIm, 

99 exposure, 

100 template, 

101 ccdExposureIdBits, 

102 "g") 

103 for subtaskName in subtasksToMock: 

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

105 pipeBase.testUtils.assertValidOutput(task, result) 

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

107 

108 def test_createDiaObjects(self): 

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

110 """ 

111 nSources = 5 

112 diaSources = pd.DataFrame(data=[ 

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

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

115 "ssObjectId": 0} 

116 for idx in range(nSources)]) 

117 

118 config = self._makeDefaultConfig(doPackageAlerts=False) 

119 task = DiaPipelineTask(config=config) 

120 result = task.createNewDiaObjects(diaSources) 

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

122 self.assertTrue(np.all(np.equal( 

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

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

125 self.assertTrue(np.all(np.equal( 

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

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

128 

129 

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

131 pass 

132 

133 

134def setup_module(module): 

135 lsst.utils.tests.init() 

136 

137 

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

139 lsst.utils.tests.init() 

140 unittest.main()