Coverage for tests/test_dataset_handle.py: 23%

69 statements  

« prev     ^ index     » next       coverage.py v7.2.1, created at 2023-03-12 20:54 -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# (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 

22import unittest 

23 

24from lsst.daf.butler import DataCoordinate, DimensionUniverse, StorageClassConfig, StorageClassFactory 

25from lsst.daf.butler.tests import MetricsExample 

26from lsst.pipe.base import InMemoryDatasetHandle 

27 

28storageClasses = """ 

29Integer: 

30 pytype: int 

31StructuredDataTestDict: 

32 pytype: dict 

33StructuredDataTestList: 

34 pytype: list 

35 delegate: lsst.daf.butler.tests.ListDelegate 

36 parameters: 

37 - slice 

38 derivedComponents: 

39 counter: Integer 

40StructuredDataTest: 

41 # Data from a simple Python class 

42 pytype: lsst.daf.butler.tests.MetricsExample 

43 delegate: lsst.daf.butler.tests.MetricsDelegate 

44 # Use YAML formatter by default 

45 components: 

46 # Components are those supported by get. 

47 summary: StructuredDataTestDict 

48 output: StructuredDataTestDict 

49 data: StructuredDataTestList 

50 parameters: 

51 - slice 

52 derivedComponents: 

53 counter: Integer 

54MetricsConversion: 

55 # Special storage class to test conversions. 

56 pytype: lsst.daf.butler.tests.MetricsExampleModel 

57 delegate: lsst.daf.butler.tests.MetricsDelegate 

58 converters: 

59 lsst.daf.butler.tests.MetricsExample: lsst.daf.butler.tests.MetricsExampleModel.from_metrics 

60StructuredDataTestListSet: 

61 pytype: set 

62 converters: 

63 list: builtins.set 

64""" 

65 

66 

67class SpecialThing: 

68 """Class known not to have associated StorageClass""" 

69 

70 

71class TestDatasetHandle(unittest.TestCase): 

72 @classmethod 

73 def setUpClass(cls): 

74 config = StorageClassConfig.fromYaml(storageClasses) 

75 cls.factory = StorageClassFactory() 

76 cls.factory.addFromConfig(config) 

77 

78 def test_dataset_handle_basic(self): 

79 inMemoryDataset = 42 

80 hdl = InMemoryDatasetHandle(inMemoryDataset) 

81 

82 self.assertEqual(hdl.get(), inMemoryDataset) 

83 

84 def test_dataset_handle_unknown(self): 

85 inMemoryDataset = SpecialThing() 

86 hdl = InMemoryDatasetHandle(inMemoryDataset) 

87 

88 self.assertEqual(hdl.get(), inMemoryDataset) 

89 

90 with self.assertRaises(KeyError): 

91 # Will not be able to find a matching StorageClass. 

92 hdl.get(parameters={"key": "value"}) 

93 

94 def test_dataset_handle_none(self): 

95 hdl = InMemoryDatasetHandle(None) 

96 self.assertIsNone(hdl.get()) 

97 self.assertIsNone(hdl.get(component="comp")) 

98 self.assertIsNone(hdl.get(parameters={"something": 42})) 

99 

100 def test_dataset_handle_dataid(self): 

101 hdl = InMemoryDatasetHandle(42) 

102 self.assertEqual(dict(hdl.dataId), {}) 

103 

104 dataId = DataCoordinate.makeEmpty(DimensionUniverse()) 

105 hdl = InMemoryDatasetHandle(42, dataId=dataId) 

106 self.assertIs(hdl.dataId, dataId) 

107 

108 def test_dataset_handle_metric(self): 

109 metric = MetricsExample(summary={"a": 1, "b": 2}, output={"c": {"d": 5}}, data=[1, 2, 3, 4]) 

110 

111 # First with explicit storage class. 

112 hdl = InMemoryDatasetHandle(metric, storageClass="StructuredDataTest") 

113 retrieved = hdl.get() 

114 self.assertEqual(retrieved, metric) 

115 

116 data = hdl.get(component="data") 

117 self.assertEqual(data, metric.data) 

118 

119 # Now with implicit storage class. 

120 hdl = InMemoryDatasetHandle(metric) 

121 data = hdl.get(component="data") 

122 self.assertEqual(data, metric.data) 

123 

124 # Parameters. 

125 data = hdl.get(parameters={"slice": slice(2)}) 

126 self.assertEqual(data.summary, metric.summary) 

127 self.assertEqual(data.data, [1, 2]) 

128 

129 data = hdl.get(parameters={"slice": slice(2)}, component="data") 

130 self.assertEqual(data, [1, 2]) 

131 

132 # Use parameters in constructor and also override. 

133 hdl = InMemoryDatasetHandle(metric, storageClass="StructuredDataTest", parameters={"slice": slice(3)}) 

134 self.assertEqual(hdl.get(component="data"), [1, 2, 3]) 

135 self.assertEqual(hdl.get(component="counter"), 3) 

136 self.assertEqual(hdl.get(component="data", parameters={"slice": slice(1, 3)}), [2, 3]) 

137 self.assertEqual(hdl.get(component="counter", parameters={"slice": slice(1, 3)}), 2) 

138 

139 # Ensure the original has not been modified. 

140 self.assertEqual(len(metric.data), 4) 

141 

142 def test_handle_conversion(self): 

143 metric = MetricsExample(summary={"a": 1, "b": 2}, output={"c": {"d": 5}}, data=[1, 2, 3, 4]) 

144 

145 # Test conversion with no components or parameters. 

146 hdl = InMemoryDatasetHandle(metric) 

147 retrieved = hdl.get() # Reset the reference. 

148 converted = hdl.get(storageClass="MetricsConversion") 

149 self.assertIsNot(type(converted), type(retrieved)) 

150 self.assertEqual(retrieved, converted) 

151 

152 # Again with a full storage class. 

153 sc = self.factory.getStorageClass("MetricsConversion") 

154 converted2 = hdl.get(storageClass=sc) 

155 self.assertEqual(converted2, converted) 

156 

157 # Conversion of component. 

158 data = hdl.get(component="data", storageClass="StructuredDataTestListSet") 

159 self.assertIsInstance(data, set) 

160 self.assertEqual(data, set(converted.data)) 

161 

162 

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

164 unittest.main()