Coverage for tests/test_utils.py: 100%

68 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-20 09:58 +0000

1# This file is part of ip_diffim. 

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 

23import logging 

24import requests 

25from unittest import mock 

26 

27import lsst.afw.cameraGeom.testUtils 

28import lsst.afw.detection 

29import lsst.daf.butler 

30from lsst.images import obs_info_from_legacy 

31from lsst.images.tests import get_dp2_exposure_record 

32from lsst.ip.diffim.utils import ( 

33 checkTemplateIsSufficient, 

34 evaluateMeanPsfFwhm, 

35 populate_sattle_visit_cache, 

36 record_from_obs_info, 

37) 

38from lsst.pex.exceptions import InvalidParameterError 

39import lsst.pipe.base 

40import lsst.utils.tests 

41 

42from test_detectAndMeasure import makeVisitInfo, MockResponse 

43from utils import makeTestImage 

44 

45 

46class ExposureRecordTest(lsst.utils.tests.TestCase): 

47 """Tests of rebuilding an exposure record from an observation info.""" 

48 

49 def setUp(self): 

50 self.universe = lsst.daf.butler.DimensionUniverse() 

51 self.record = get_dp2_exposure_record(self.universe) 

52 # lsst.images needs per-amplifier raw geometry and a field angle 

53 # transform, which the trivial test detectors do not have. 

54 self.detector = list(lsst.afw.cameraGeom.testUtils.CameraWrapper().camera)[0] 

55 self.obsInfo = self._makeObsInfo(self.record) 

56 

57 def _makeObsInfo(self, record): 

58 """Return the observation info that a converted image built from 

59 ``record`` would carry. 

60 """ 

61 # The conversion requires the visit info and the record to agree 

62 # on the exposure id. 

63 return obs_info_from_legacy(makeVisitInfo(id=record.id), record, self.detector, 

64 detector_exposure_id=12345) 

65 

66 def _rebuild(self): 

67 """Return the record rebuilt from the observation info, with additional 

68 identifiers added. 

69 """ 

70 return record_from_obs_info(self.obsInfo, self.record.instrument, self.record.id, self.universe) 

71 

72 def test_fields(self): 

73 """Every field the conversion reads comes back with the value the 

74 original record had. 

75 """ 

76 record = self._rebuild() 

77 for field in ("instrument", "id", "obs_id", "group", "day_obs", "physical_filter", 

78 "exposure_time", "seq_num", "seq_start", "seq_end", "can_see_sky", "timespan"): 

79 self.assertEqual(getattr(record, field), getattr(self.record, field), msg=field) 

80 self.assertFloatsAlmostEqual(record.azimuth, self.record.azimuth, rtol=1e-14) 

81 self.assertFloatsAlmostEqual(record.zenith_angle, self.record.zenith_angle, rtol=1e-14) 

82 

83 def test_conversion_unchanged(self): 

84 """Converting with the rebuilt record gives the same observation 

85 info as converting with the original one. 

86 """ 

87 self.assertEqual(self._makeObsInfo(self._rebuild()), self.obsInfo) 

88 

89 def test_missing_obs_info(self): 

90 with self.assertRaises(ValueError): 

91 record_from_obs_info(None, self.record.instrument, self.record.id, self.universe) 

92 

93 

94class UtilsTest(lsst.utils.tests.TestCase): 

95 

96 def test_populate_sattle(self): 

97 response = MockResponse({}, 200, "success") 

98 visit_info = makeVisitInfo() 

99 with mock.patch('requests.put', return_value=response): 

100 populate_sattle_visit_cache(visit_info) 

101 

102 def test_populate_sattle_raises(self): 

103 response = MockResponse({}, 500, "failure") 

104 visit_info = makeVisitInfo() 

105 with mock.patch('requests.put', return_value=response): 

106 with self.assertRaises(requests.exceptions.HTTPError): 

107 populate_sattle_visit_cache(visit_info) 

108 

109 def test_raise_on_even_kernel(self): 

110 with self.assertRaises(ValueError): 

111 makeTestImage(kernelSize=32) 

112 

113 def test_evaluateMeanPsfFwhm_raises(self): 

114 """A PSF that cannot be evaluated at any grid point is invalid.""" 

115 exposure, _ = makeTestImage(nSrc=1, xSize=64, ySize=64) 

116 error = InvalidParameterError("No inputs at this position.") 

117 with mock.patch("lsst.ip.diffim.utils.getPsfFwhm", side_effect=error): 

118 with self.assertRaisesRegex(lsst.afw.detection.InvalidPsfError, "at any position"): 

119 evaluateMeanPsfFwhm(exposure, fwhmExposureBuffer=0.05, fwhmExposureGrid=3) 

120 

121 def test_checkTemplateIsSufficient(self): 

122 """The coverage is the fraction of pixels with data in the template 

123 and, if supplied, also in the science image. 

124 """ 

125 template, _ = makeTestImage(nSrc=1, xSize=64, ySize=64, clearEdgeMask=True) 

126 science = template.clone() 

127 noData = template.mask.getPlaneBitMask("NO_DATA") 

128 template.mask.array[:32, :] |= noData 

129 science.mask.array[:, :32] |= noData 

130 logger = logging.getLogger(__name__) 

131 

132 self.assertEqual(checkTemplateIsSufficient(template, logger), 0.5) 

133 self.assertEqual(checkTemplateIsSufficient(template, logger, scienceExposure=science), 0.25) 

134 self.assertEqual(checkTemplateIsSufficient(template, logger, requiredTemplateFraction=0.5), 0.5) 

135 with self.assertRaisesRegex(lsst.pipe.base.NoWorkFound, "Insufficient Template Coverage"): 

136 checkTemplateIsSufficient(template, logger, scienceExposure=science, 

137 requiredTemplateFraction=0.5)