Coverage for tests/test_packers.py: 29%

69 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-06 03:01 -0700

1# This file is part of skymap. 

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 

24import lsst.utils.tests 

25 

26try: 

27 from lsst.daf.butler import DimensionUniverse, DimensionGraph, DataCoordinate 

28 HAVE_DAF_BUTLER = True 

29except ImportError: 

30 HAVE_DAF_BUTLER = False 

31 

32from lsst.skymap.packers import SkyMapDimensionPacker 

33 

34 

35@unittest.skipUnless(HAVE_DAF_BUTLER, "daf_butler not setup") 

36class SkyMapDimensionPackerTestCase(lsst.utils.tests.TestCase): 

37 

38 def setUp(self): 

39 self.universe = DimensionUniverse() 

40 self.fixed = DataCoordinate.fromFullValues( 

41 DimensionGraph(universe=self.universe, names=["skymap"]), 

42 values=("unimportant",), 

43 ).expanded( 

44 records={ 

45 "skymap": self.universe["skymap"].RecordClass( 

46 name="unimportant", 

47 tract_max=5, 

48 patch_nx_max=3, 

49 patch_ny_max=3, 

50 ) 

51 } 

52 ) 

53 

54 def testWithoutFilter(self): 

55 dimensions = DimensionGraph(universe=self.universe, names=["tract", "patch"]) 

56 dataId = DataCoordinate.standardize( 

57 skymap=self.fixed["skymap"], 

58 tract=2, 

59 patch=6, 

60 universe=self.universe 

61 ) 

62 packer = SkyMapDimensionPacker(self.fixed, dimensions) 

63 packedId = packer.pack(dataId) 

64 self.assertLessEqual(packedId.bit_length(), packer.maxBits) 

65 self.assertEqual(packer.unpack(packedId), dataId) 

66 

67 def testWithFilter(self): 

68 dimensions = DimensionGraph(universe=self.universe, names=["tract", "patch", "band"]) 

69 dataId = DataCoordinate.standardize( 

70 skymap=self.fixed["skymap"], 

71 tract=2, 

72 patch=6, 

73 band="g", 

74 universe=self.universe 

75 ) 

76 packer = SkyMapDimensionPacker(self.fixed, dimensions) 

77 packedId = packer.pack(dataId) 

78 self.assertLessEqual(packedId.bit_length(), packer.maxBits) 

79 self.assertEqual(packer.unpack(packedId), dataId) 

80 

81 def test_bad_dimensions(self): 

82 with self.assertRaises(ValueError): 

83 SkyMapDimensionPacker( 

84 self.fixed, 

85 DimensionGraph(universe=self.universe, names=["tract", "patch", "visit"]), 

86 ) 

87 with self.assertRaises(ValueError): 

88 SkyMapDimensionPacker( 

89 self.fixed, 

90 DimensionGraph(universe=self.universe, names=["tract", "patch", "detector"]), 

91 ) 

92 

93 def test_from_config(self): 

94 data_id = DataCoordinate.standardize( 

95 skymap=self.fixed["skymap"], 

96 tract=2, 

97 patch=6, 

98 band="g", 

99 universe=self.universe 

100 ) 

101 config = SkyMapDimensionPacker.ConfigClass() 

102 config.n_tracts = 5 

103 config.n_patches = 9 

104 config.n_bands = 3 

105 config.bands = {"r": 0, "g": 1} 

106 packer = SkyMapDimensionPacker.from_config(data_id, config=config) 

107 packed_id = packer.pack(data_id) 

108 self.assertLessEqual(packed_id.bit_length(), packer.maxBits) 

109 self.assertEqual(packer.unpack(packed_id), data_id) 

110 

111 def test_from_config_no_bands(self): 

112 data_id = DataCoordinate.standardize( 

113 skymap=self.fixed["skymap"], 

114 tract=2, 

115 patch=6, 

116 universe=self.universe 

117 ) 

118 config = SkyMapDimensionPacker.ConfigClass() 

119 config.n_tracts = 5 

120 config.n_patches = 9 

121 packer = SkyMapDimensionPacker.from_config(data_id, config=config) 

122 packed_id = packer.pack(data_id) 

123 self.assertLessEqual(packed_id.bit_length(), packer.maxBits) 

124 self.assertEqual(packer.unpack(packed_id), data_id) 

125 

126 def test_from_config_default_bands(self): 

127 data_id = DataCoordinate.standardize( 

128 skymap=self.fixed["skymap"], 

129 tract=2, 

130 patch=6, 

131 band="g", 

132 universe=self.universe 

133 ) 

134 config = SkyMapDimensionPacker.ConfigClass() 

135 config.n_tracts = 5 

136 config.n_patches = 9 

137 config.n_bands = None 

138 packer = SkyMapDimensionPacker.from_config(data_id, config=config) 

139 packed_id = packer.pack(data_id) 

140 self.assertLessEqual(packed_id.bit_length(), packer.maxBits) 

141 self.assertEqual(packer.unpack(packed_id), data_id) 

142 

143 

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

145 pass 

146 

147 

148def setup_module(module): 

149 lsst.utils.tests.init() 

150 

151 

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

153 lsst.utils.tests.init() 

154 unittest.main()