Coverage for tests/test_focusAnalysis.py: 32%

55 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-05 11:26 +0000

1# This file is part of summit_extras. 

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 

22import unittest 

23from typing import Iterable 

24 

25import lsst.utils.tests 

26 

27import matplotlib as mpl 

28mpl.use('Agg') 

29 

30from lsst.summit.extras import SpectralFocusAnalyzer, NonSpectralFocusAnalyzer # noqa: E402 

31import lsst.summit.utils.butlerUtils as butlerUtils # noqa: E402 

32 

33 

34class FocusAnalysisTestCase(lsst.utils.tests.TestCase): 

35 

36 @classmethod 

37 def setUpClass(cls): 

38 try: 

39 cls.butler = butlerUtils.makeDefaultLatissButler() 

40 except FileNotFoundError: 

41 raise unittest.SkipTest("Skipping tests that require the LATISS butler repo.") 

42 

43 # dataIds have been chosen match those of a spectral focus sweep which 

44 # is in the LATISS-test-data collection at the summit, TTS and USDF 

45 cls.dayObs = 20220628 

46 cls.seqNums = range(280, 288+1) 

47 cls.focusAnalyzer = SpectralFocusAnalyzer() 

48 

49 # default is 3 boxes, so setting four tests the generality of the code. 

50 # The values have been picked to land on the pretty small spectrum we 

51 # have in the test data. 

52 cls.focusAnalyzer.setSpectrumBoxOffsets([1600, 1700, 1800, 1900]) 

53 

54 def test_run(self): 

55 # we don't check the plots, but set doDisplay to True to check the 

56 # plots are generated without error 

57 self.focusAnalyzer.getFocusData(self.dayObs, self.seqNums, doDisplay=True) 

58 result = self.focusAnalyzer.fitDataAndPlot() 

59 self.assertIsInstance(result, Iterable) 

60 self.assertEqual(len(result), len(self.focusAnalyzer.getSpectrumBoxOffsets())) 

61 

62 for number in result: 

63 # check they're all numbers, non-nan, and vaguely sensible. These 

64 # are values in mm of the hexapod offset from nominal focus and are 

65 # usually very small (around 0.01mm) so this is just testing that 

66 # something hasn't gone wildly wrong with the fit 

67 self.assertGreater(number, -2.0) 

68 self.assertLess(number, 2.0) 

69 

70 

71class NonSpectralFocusAnalysisTestCase(lsst.utils.tests.TestCase): 

72 

73 @classmethod 

74 def setUpClass(cls): 

75 try: 

76 cls.butler = butlerUtils.makeDefaultLatissButler() 

77 except FileNotFoundError: 

78 raise unittest.SkipTest("Skipping tests that require the LATISS butler repo.") 

79 

80 # dataIds have been chosen match those of a non-spectral focus sweep 

81 # which is in the LATISS-test-data collection at the summit, TTS and 

82 # USDF 

83 cls.dayObs = 20220405 

84 cls.seqNums = range(523, 531+1) 

85 cls.focusAnalyzer = NonSpectralFocusAnalyzer() 

86 

87 def test_run(self): 

88 # we don't check the plots, but set doDisplay to True to check the 

89 # plots are generated without error 

90 self.focusAnalyzer.getFocusData(self.dayObs, self.seqNums, doDisplay=True) 

91 result = self.focusAnalyzer.fitDataAndPlot() 

92 self.assertIsInstance(result, dict) 

93 

94 # result is a dict which looks like this: 

95 # {'fwhmFitMin': 0.029221417454391708, 

96 # 'ee90FitMin': 0.0754762884665358, 

97 # 'ee80FitMin': 0.07188778363981346, 

98 # 'ee50FitMin': 0.13998855716378267} 

99 self.assertEqual(len(result), 4) 

100 

101 for k, v in result.items(): 

102 # check they're all numbers, non-nan, and vaguely sensible. The 

103 # values are the position of the fwhm which should be close to zero 

104 # on the x-axis assuming we were roughly symmetric around focus 

105 # (which is the case for the current test dataset) and encircled 

106 # energy radii, which also should be small as the data in question 

107 # was roughly in focus. This is really just to check that the code 

108 # isn't horribly broken, so not much attention should be paid 

109 # to these numerical values. 

110 self.assertIsInstance(k, str) 

111 self.assertIsInstance(v, float) 

112 

113 if k == 'fwhmFitMin': 

114 # everything else is strictly positive but the fwhm position 

115 # can be negative. 

116 v = abs(v) 

117 

118 self.assertGreater(v, 0.0) 

119 self.assertLess(v, 1.0) 

120 

121 

122class TestMemory(lsst.utils.tests.MemoryTestCase): 

123 pass 

124 

125 

126def setup_module(module): 

127 lsst.utils.tests.init() 

128 

129 

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

131 lsst.utils.tests.init() 

132 unittest.main()