Coverage for tests/test_dataset_handle.py: 27%

56 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-10-01 01:55 -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 

54""" 

55 

56 

57class SpecialThing: 

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

59 

60 

61class TestDatasetHandle(unittest.TestCase): 

62 @classmethod 

63 def setUpClass(cls): 

64 config = StorageClassConfig.fromYaml(storageClasses) 

65 factory = StorageClassFactory() 

66 factory.addFromConfig(config) 

67 

68 def test_dataset_handle_basic(self): 

69 inMemoryDataset = 42 

70 hdl = InMemoryDatasetHandle(inMemoryDataset) 

71 

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

73 

74 def test_dataset_handle_unknown(self): 

75 inMemoryDataset = SpecialThing() 

76 hdl = InMemoryDatasetHandle(inMemoryDataset) 

77 

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

79 

80 with self.assertRaises(KeyError): 

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

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

83 

84 def test_dataset_handle_none(self): 

85 hdl = InMemoryDatasetHandle(None) 

86 self.assertIsNone(hdl.get()) 

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

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

89 

90 def test_dataset_handle_dataid(self): 

91 hdl = InMemoryDatasetHandle(42) 

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

93 

94 dataId = DataCoordinate.makeEmpty(DimensionUniverse()) 

95 hdl = InMemoryDatasetHandle(42, dataId=dataId) 

96 self.assertIs(hdl.dataId, dataId) 

97 

98 def test_dataset_handle_metric(self): 

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

100 

101 # First with explicit storage class. 

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

103 retrieved = hdl.get() 

104 self.assertEqual(retrieved, metric) 

105 

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

107 self.assertEqual(data, metric.data) 

108 

109 # Now with implicit storage class. 

110 hdl = InMemoryDatasetHandle(metric) 

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

112 self.assertEqual(data, metric.data) 

113 

114 # Parameters. 

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

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

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

118 

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

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

121 

122 # Use parameters in constructor and also override. 

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

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

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

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

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

128 

129 # Ensure the original has not been modified. 

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

131 

132 

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

134 unittest.main()