Coverage for tests/test_graphBuilder.py: 20%

65 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-15 02:49 -0700

1# This file is part of pipe_base. 

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 

22"""Tests of things related to the GraphBuilder class.""" 

23 

24import io 

25import logging 

26import unittest 

27 

28import lsst.utils.tests 

29from lsst.daf.butler.registry import UserExpressionError 

30from lsst.pipe.base import QuantumGraph 

31from lsst.pipe.base.graphBuilder import DatasetQueryConstraintVariant 

32from lsst.pipe.base.tests import simpleQGraph 

33from lsst.utils.tests import temporaryDirectory 

34 

35_LOG = logging.getLogger(__name__) 

36 

37 

38class GraphBuilderTestCase(unittest.TestCase): 

39 def _assertGraph(self, graph: QuantumGraph) -> None: 

40 """Check basic structure of the graph.""" 

41 for taskDef in graph.iterTaskGraph(): 

42 refs = graph.initOutputRefs(taskDef) 

43 # task has one initOutput, second ref is for config dataset 

44 self.assertEqual(len(refs), 2) 

45 

46 self.assertEqual(len(list(graph.inputQuanta)), 1) 

47 

48 # This includes only "packages" dataset for now. 

49 refs = graph.globalInitOutputRefs() 

50 self.assertEqual(len(refs), 1) 

51 

52 def testDefault(self): 

53 """Simple test to verify makeSimpleQGraph can be used to make a Quantum 

54 Graph.""" 

55 with temporaryDirectory() as root: 

56 # makeSimpleQGraph calls GraphBuilder. 

57 butler, qgraph = simpleQGraph.makeSimpleQGraph(root=root) 

58 # by default makeSimpleQGraph makes a graph with 5 nodes 

59 self.assertEqual(len(qgraph), 5) 

60 self._assertGraph(qgraph) 

61 constraint = DatasetQueryConstraintVariant.OFF 

62 _, qgraph2 = simpleQGraph.makeSimpleQGraph( 

63 butler=butler, datasetQueryConstraint=constraint, callPopulateButler=False 

64 ) 

65 # When all outputs are random resolved refs, direct comparison 

66 # of graphs does not work because IDs are different. Can only 

67 # verify the number of quanta in the graph without doing something 

68 # terribly complicated. 

69 self.assertEqual(len(qgraph2), 5) 

70 constraint = DatasetQueryConstraintVariant.fromExpression("add_dataset0") 

71 _, qgraph3 = simpleQGraph.makeSimpleQGraph( 

72 butler=butler, datasetQueryConstraint=constraint, callPopulateButler=False 

73 ) 

74 self.assertEqual(len(qgraph3), 5) 

75 

76 def testAddInstrumentMismatch(self): 

77 """Verify that a RuntimeError is raised if the instrument in the user 

78 query does not match the instrument in the pipeline.""" 

79 with temporaryDirectory() as root: 

80 pipeline = simpleQGraph.makeSimplePipeline( 

81 nQuanta=5, instrument="lsst.pipe.base.tests.simpleQGraph.SimpleInstrument" 

82 ) 

83 with self.assertRaises(UserExpressionError): 

84 simpleQGraph.makeSimpleQGraph(root=root, pipeline=pipeline, userQuery="instrument = 'foo'") 

85 

86 def testUserQueryBind(self): 

87 """Verify that bind values work for user query.""" 

88 pipeline = simpleQGraph.makeSimplePipeline( 

89 nQuanta=5, instrument="lsst.pipe.base.tests.simpleQGraph.SimpleInstrument" 

90 ) 

91 instr = simpleQGraph.SimpleInstrument.getName() 

92 # With a literal in the user query 

93 with temporaryDirectory() as root: 

94 simpleQGraph.makeSimpleQGraph(root=root, pipeline=pipeline, userQuery=f"instrument = '{instr}'") 

95 # With a bind for the user query 

96 with temporaryDirectory() as root: 

97 simpleQGraph.makeSimpleQGraph( 

98 root=root, pipeline=pipeline, userQuery="instrument = instr", bind={"instr": instr} 

99 ) 

100 

101 def test_datastore_records(self): 

102 """Test for generating datastore records.""" 

103 with temporaryDirectory() as root: 

104 # need FileDatastore for this tests 

105 butler, qgraph1 = simpleQGraph.makeSimpleQGraph( 

106 root=root, inMemory=False, makeDatastoreRecords=True 

107 ) 

108 

109 # save and reload 

110 buffer = io.BytesIO() 

111 qgraph1.save(buffer) 

112 buffer.seek(0) 

113 qgraph2 = QuantumGraph.load(buffer, universe=butler.dimensions) 

114 del buffer 

115 

116 for qgraph in (qgraph1, qgraph2): 

117 self.assertEqual(len(qgraph), 5) 

118 for i, qnode in enumerate(qgraph): 

119 quantum = qnode.quantum 

120 self.assertIsNotNone(quantum.datastore_records) 

121 # only the first quantum has a pre-existing input 

122 if i == 0: 

123 datastore_name = "FileDatastore@<butlerRoot>" 

124 self.assertEqual(set(quantum.datastore_records.keys()), {datastore_name}) 

125 records_data = quantum.datastore_records[datastore_name] 

126 records = dict(records_data.records) 

127 self.assertEqual(len(records), 1) 

128 _, records = records.popitem() 

129 records = records["file_datastore_records"] 

130 self.assertEqual( 

131 [record.path for record in records], 

132 ["test/add_dataset0/add_dataset0_INSTR_det0_test.pickle"], 

133 ) 

134 else: 

135 self.assertEqual(quantum.datastore_records, {}) 

136 

137 

138if __name__ == "__main__": 

139 lsst.utils.tests.init() 

140 unittest.main()