Coverage for tests/test_processStar.py: 23%

73 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-02-18 10:05 +0000

1#!/usr/bin/env python 

2 

3# 

4# LSST Data Management System 

5# 

6# Copyright 2008-2017 AURA/LSST. 

7# 

8# This product includes software developed by the 

9# LSST Project (http://www.lsst.org/). 

10# 

11# This program is free software: you can redistribute it and/or modify 

12# it under the terms of the GNU General Public License as published by 

13# the Free Software Foundation, either version 3 of the License, or 

14# (at your option) any later version. 

15# 

16# This program is distributed in the hope that it will be useful, 

17# but WITHOUT ANY WARRANTY; without even the implied warranty of 

18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

19# GNU General Public License for more details. 

20# 

21# You should have received a copy of the LSST License Statement and 

22# the GNU General Public License along with this program. If not, 

23# see <https://www.lsstcorp.org/LegalNotices/>. 

24# 

25"""Test cases for atmospec.""" 

26 

27import unittest 

28import numpy as np 

29 

30import lsst.utils 

31import lsst.utils.tests 

32import lsst.afw.image as afwImage 

33from lsst.atmospec.extraction import SpectralExtractionTask 

34 

35TRACEBACKS_FOR_ABI_WARNINGS = False 

36 

37if TRACEBACKS_FOR_ABI_WARNINGS: 37 ↛ 38line 37 didn't jump to line 38, because the condition on line 37 was never true

38 import traceback 

39 import warnings 

40 import sys 

41 

42 def warn_with_traceback(message, category, filename, lineno, file=None, line=None): 

43 

44 log = file if hasattr(file, 'write') else sys.stderr 

45 traceback.print_stack(file=log) 

46 log.write(warnings.formatwarning(message, category, filename, lineno, line)) 

47 

48 warnings.showwarning = warn_with_traceback 

49 

50 

51class ProcessStarTestCase(lsst.utils.tests.TestCase): 

52 """A test case for atmospec.""" 

53 

54 def testImport(self): 

55 import lsst.atmospec as atmospec # noqa: F401 

56 

57 def testClassInstantiation(self): 

58 config = SpectralExtractionTask.ConfigClass() 

59 task = SpectralExtractionTask(config=config) 

60 del config, task 

61 

62 def test_calculateBackgroundMasking(self): 

63 task = SpectralExtractionTask() 

64 

65 for nbins in [1, 2, 3]: 

66 

67 mi = afwImage.MaskedImageF(5, 5) 

68 mi.image.array[:] = np.ones((5, 5)) 

69 

70 bgImg = task._calculateBackground(mi, nbins) 

71 self.assertEqual(np.shape(mi.image.array), np.shape(bgImg.array)) 

72 self.assertEqual(np.max(bgImg.array), 1.) 

73 

74 mi.image.array[2, 2] = 100 

75 bgImg = task._calculateBackground(mi, nbins) 

76 self.assertTrue(np.max(bgImg.array) > 1.) 

77 

78 MaskPixel = afwImage.MaskPixel 

79 mi.mask.array[2, 2] = afwImage.Mask[MaskPixel].getPlaneBitMask("DETECTED") 

80 bgImg = task._calculateBackground(mi, nbins) 

81 self.assertEqual(np.max(bgImg.array), 1.) 

82 

83 mi.image.array[3, 3] = 200 

84 mi.mask.array[3, 3] = afwImage.Mask[MaskPixel].getPlaneBitMask("BAD") 

85 self.assertEqual(np.max(mi.image.array), 200) 

86 # don't include "BAD", but it's the default, so exclude explicitly 

87 bgImg = task._calculateBackground(mi, nbins, ignorePlanes="DETECTED") 

88 self.assertTrue(np.max(bgImg.array > 1.)) 

89 

90 # And now check thet explicitly including it gets us back 

91 # to where we were 

92 bgImg = task._calculateBackground(mi, nbins, ignorePlanes=["DETECTED", "BAD"]) 

93 self.assertEqual(np.max(bgImg.array), 1) 

94 

95 for nbins in [5, 15, 50]: 

96 mi = afwImage.MaskedImageF(5, 5) 

97 mi.image.array[:] = np.ones((5, 5)) 

98 

99 bgImg = task._calculateBackground(mi, nbins) 

100 self.assertEqual(np.shape(mi.image.array), np.shape(bgImg.array)) 

101 self.assertEqual(np.max(bgImg.array), 1.) 

102 

103 mi.image.array[2, 2] = 100 

104 bgImg = task._calculateBackground(mi, nbins) 

105 self.assertTrue(np.max(bgImg.array) > 1.) 

106 

107 MaskPixel = afwImage.MaskPixel 

108 mi.mask.array[2, 2] = afwImage.Mask[MaskPixel].getPlaneBitMask("DETECTED") 

109 bgImg = task._calculateBackground(mi, nbins) 

110 self.assertEqual(np.max(bgImg.array), 1.) 

111 

112 mi.image.array[3, 3] = 200 

113 mi.mask.array[3, 3] = afwImage.Mask[MaskPixel].getPlaneBitMask("BAD") 

114 self.assertEqual(np.max(mi.image.array), 200) 

115 

116 # don't include "BAD", but it's the default 

117 # so exclude explicitly 

118 bgImg = task._calculateBackground(mi, nbins, ignorePlanes="DETECTED") 

119 self.assertTrue(np.max(bgImg.array > 1.)) 

120 

121 # And now check that explicitly including it gets us back 

122 # to where we were 

123 bgImg = task._calculateBackground(mi, nbins, ignorePlanes=["DETECTED", "BAD"]) 

124 self.assertEqual(np.max(bgImg.array), 1) 

125 

126 # TODO: 

127 # should make a new test that makes a larger image and actually tests 

128 # interpolation and background calculations 

129 # also, I'm sure this could be tidied up with itertools 

130 

131 

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

133 pass 

134 

135 

136def setup_module(module): 

137 lsst.utils.tests.init() 

138 

139 

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

141 lsst.utils.tests.init() 

142 unittest.main()