Coverage for python / lsst / obs / base / tests.py: 27%

41 statements  

« prev     ^ index     » next       coverage.py v7.13.2, created at 2026-02-03 01:34 -0800

1# This file is part of obs_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""" 

23Test utilities for obs_base and concrete obs* packages. 

24""" 

25 

26from __future__ import annotations 

27 

28__all__ = ( 

29 "ObsTests", 

30 "make_ramp_array", 

31 "make_ramp_exposure_trimmed", 

32 "make_ramp_exposure_untrimmed", 

33) 

34 

35import logging 

36from typing import TYPE_CHECKING, Any 

37 

38import numpy as np 

39 

40from lsst.afw.cameraGeom.utils import calcRawCcdBBox 

41from lsst.afw.image import Exposure 

42 

43from . import butler_tests, camera_tests 

44 

45if TYPE_CHECKING: 

46 import lsst.afw.cameraGeom 

47 import lsst.afw.geom 

48 import lsst.afw.image 

49 import lsst.daf.butler 

50 import lsst.geom 

51 

52 

53class ObsTests(butler_tests.ButlerGetTests, camera_tests.CameraTests): 

54 """Aggregator class for all of the obs_* test classes. 

55 

56 Inherit from this class, then lsst.utils.tests.TestCase, in that order. 

57 

58 Example subclass: 

59 

60 .. code-block:: python 

61 

62 class TestObs( 

63 lsst.obs.base.tests.ObsTests, lsst.utils.tests.TestCase 

64 ): 

65 def setUp(self): 

66 self.setUp_tests(...) 

67 self.setUp_butler_get(...) 

68 self.setUp_camera(...) 

69 

70 Notes 

71 ----- 

72 The intention is for each obs package to have a single test class that 

73 inherits from this collector class, thus "automatically" getting all new 

74 tests. If those tests require setup that isn't defined in a given obs 

75 package, that obs package will be broken until updated. This is 

76 intentional, as a way to prevent obs packages from falling behind out of 

77 neglect. 

78 """ 

79 

80 def setUp_tests(self, butler: lsst.daf.butler.Butler, dataIds: dict[str, Any]) -> Any: 

81 """Set up the necessary shared variables used by multiple tests. 

82 

83 Parameters 

84 ---------- 

85 butler : `lsst.daf.butler.Butler` 

86 A butler object, instantiated on the testdata repository for the 

87 obs package being tested. 

88 dataIds : `dict` 

89 dictionary of (exposure name): (dataId of that exposure in the 

90 testdata repository), with unittest.SkipTest as the value for any 

91 exposures you do not have/do not want to test. It must contain a 

92 valid 'raw' dataId, in addition to 'bias','flat','dark', which may 

93 be set to SkipTest. For example:: 

94 

95 self.dataIds = { 

96 "raw": {"visit": 1, "filter": "g"}, 

97 "bias": {"visit": 1}, 

98 "flat": {"visit": 1}, 

99 "dark": unittest.SkipTest, 

100 } 

101 """ 

102 self.butler = butler 

103 self.dataIds = dataIds 

104 self.log = logging.getLogger(__name__) 

105 

106 def tearDown(self) -> None: 

107 del self.butler 

108 super().tearDown() # type: ignore[misc] 

109 

110 

111def make_ramp_array(bbox: lsst.geom.Box2I, pedestal: int) -> tuple[np.ndarray, int]: 

112 """Make a 2-d ramp array. 

113 

114 Parameters 

115 ---------- 

116 bbox : `lsst.geom.Box2I` 

117 Bounding box for the array. 

118 pedestal : `int` 

119 Minimum value for the ramp. 

120 

121 Returns 

122 ------- 

123 ramp : `numpy.ndarray` 

124 A 2-d array with shape ``(bbox.getHeight(), bbox.getWidth())``. 

125 end : `int` 

126 One past the maximum value in the ramp (for use as the 

127 pedestal for another box). 

128 """ 

129 end = pedestal + bbox.getArea() 

130 return np.arange(pedestal, end).reshape(bbox.getHeight(), bbox.getWidth()), end 

131 

132 

133def make_ramp_exposure_untrimmed( 

134 detector: lsst.afw.cameraGeom.Detector, dtype: np.dtype | None = None 

135) -> Exposure: 

136 """Create an untrimmed, assembled exposure with different ramps for 

137 each sub-amplifier region. 

138 

139 Parameters 

140 ---------- 

141 detector : `lsst.afw.cameraGeom.Detector` 

142 Detector object that the new exposure should match. Must have all amp 

143 flips and offsets set to False/zero (i.e. represent an already- 

144 assembled image). 

145 dtype : `numpy.dtype`, optional 

146 Type of the new exposure. Defaults to ``int32``. 

147 

148 Returns 

149 ------- 

150 exposure : `lsst.afw.image.Exposure` 

151 New exposure with the given detector attached. 

152 """ 

153 if dtype is None: 

154 dtype = np.dtype(np.int32) 

155 ramp_exposure = Exposure(calcRawCcdBBox(detector), dtype=np.dtype(dtype)) 

156 ramp_exposure.setDetector(detector) 

157 pedestal = 0 

158 for amp in detector: 

159 for name in ("HorizontalOverscan", "VerticalOverscan", "Prescan", "Data"): 

160 bbox = getattr(amp, f"getRaw{name}BBox")() 

161 ramp, pedestal = make_ramp_array(bbox, pedestal) 

162 ramp_exposure.image[bbox].array[:, :] = ramp 

163 return ramp_exposure 

164 

165 

166def make_ramp_exposure_trimmed( 

167 detector: lsst.afw.cameraGeom.Detector, dtype: np.dtype | None = None 

168) -> Exposure: 

169 """Create a trimmed, assembled exposure with different ramps for 

170 each amplifier region. 

171 

172 Parameters 

173 ---------- 

174 detector : `lsst.afw.cameraGeom.Detector` 

175 Detector object that the new exposure should match. 

176 dtype : `numpy.dtype`, optional 

177 Type of the new exposure. Defaults to ``int32``. 

178 

179 Returns 

180 ------- 

181 exposure : `lsst.afw.image.Exposure` 

182 New exposure with the given detector attached. 

183 """ 

184 if dtype is None: 

185 dtype = np.dtype(np.int32) 

186 ramp_exposure = Exposure(detector.getBBox(), dtype=np.dtype(dtype)) 

187 ramp_exposure.setDetector(detector) 

188 pedestal = 0 

189 for amp in detector: 

190 ramp, pedestal = make_ramp_array(amp.getBBox(), pedestal) 

191 ramp_exposure.image[amp.getBBox()].array[:, :] = ramp 

192 return ramp_exposure