Coverage for tests/test_plotUtils.py: 20%

51 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-04-24 04:10 -0700

1# 

2# Developed for the LSST Data Management System. 

3# This product includes software developed by the LSST Project 

4# (https://www.lsst.org). 

5# See the COPYRIGHT file at the top-level directory of this distribution 

6# for details of code ownership. 

7# 

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

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

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

11# (at your option) any later version. 

12# 

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

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

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

16# GNU General Public License for more details. 

17# 

18# You should have received a copy of the GNU General Public License 

19# along with this program. If not, see <https://www.gnu.org/licenses/>. 

20 

21import unittest 

22 

23import lsst.utils.tests 

24import numpy as np 

25from lsst.analysis.tools.actions.keyedData.stellarLocusFit import _stellarLocusFit, perpDistance 

26from lsst.analysis.tools.actions.plot.plotUtils import shorten_list 

27 

28 

29class FitTest(lsst.utils.tests.TestCase): 

30 """Test to see if the fitting and distance calculations are working.""" 

31 

32 def testFitLine(self): 

33 """Make a known array of points for x and y and then test that 

34 the derived fit parameters are as expected.""" 

35 

36 xs = np.arange(1, 10) 

37 ys = np.arange(1, 10) 

38 mags = np.arange(17.5, 22, 0.5) 

39 

40 # Define an initial fit box that encompasses the points. 

41 testParams = { 

42 "xMin": 0, 

43 "xMax": 11, 

44 "yMin": 0, 

45 "yMax": 11, 

46 "mFixed": 1, 

47 "bFixed": 0, 

48 "nSigmaToClip1": 3.5, 

49 "nSigmaToClip2": 5.0, 

50 "minObjectForFit": 3, 

51 } 

52 paramsOut = _stellarLocusFit(xs, ys, mags, testParams) 

53 

54 # _stellarLocusFit performs two iterations of fitting and also 

55 # calculates the perpendicular gradient to the fit line and 

56 # the points of intersection between the box and the fit 

57 # line. Test that these are returning what is expected. 

58 self.assertFloatsAlmostEqual(paramsOut["mODR"], 1.0) 

59 self.assertFloatsAlmostEqual(paramsOut["bODR"], 0.0) 

60 self.assertFloatsAlmostEqual(paramsOut["bPerpMin"], 0.0) 

61 self.assertFloatsAlmostEqual(paramsOut["bPerpMax"], 22.0) 

62 

63 def testPerpDistance(self): 

64 """Test the calculation of the perpendicular distance.""" 

65 

66 p1 = np.array([1, 1]) 

67 p2 = np.array([2, 2]) 

68 testPoints = np.array([[1, 2], [1.5, 1.5], [2, 1]]) 

69 # perpDistance uses two points, p1 and p2, to define a line 

70 # then calculates the perpendicular distance of the testPoints 

71 # to this line 

72 dists = perpDistance(p1, p2, testPoints) 

73 self.assertFloatsAlmostEqual(np.array([1.0 / np.sqrt(2), 0.0, -1.0 / np.sqrt(2)]), np.array(dists)) 

74 

75 

76class ShortListTestCase(lsst.utils.tests.TestCase): 

77 """Test to see if the shorten_list function works as it should.""" 

78 

79 def test_shorten_list(self): 

80 self.assertEqual(shorten_list([]), "") # empty container 

81 self.assertEqual(shorten_list([5]), "5") # single element 

82 self.assertEqual(shorten_list([5, 6]), "5-6") # 2 contigous elements 

83 self.assertEqual(shorten_list([5, 7]), "5,7") # 2 non-contiguous elements 

84 self.assertEqual(shorten_list([-7, -6, -5]), "-7--5") # 3 contiguous elements 

85 self.assertEqual(shorten_list([5, 7, 9]), "5,7,9") # 3 non-contiguous elements 

86 self.assertEqual(shorten_list([5, 6, 8]), "5-6,8") # 3 mixed elements 

87 

88 # Test for different data types 

89 self.assertEqual(shorten_list({1, 2, 3, 5, 7, 8, 9, 10, 13}), "1-3,5,7-10,13") # test for a set 

90 self.assertEqual( 

91 shorten_list((1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18)), "1-3,5-13,15-18" 

92 ) # test for a tuple 

93 self.assertEqual(shorten_list(range(100)), "0-99") # test for an itertor 

94 self.assertEqual(shorten_list(np.arange(100, dtype=int)), "0-99") # test for a numpy array 

95 

96 # Test the RC2/DC2 tract list explicitly. 

97 self.assertEqual(shorten_list([9615, 9813, 9697]), "9615,9697,9813") 

98 self.assertEqual(shorten_list([3828, 3829]), "3828-3829") 

99 

100 # Test the keyword-only arguments. 

101 self.assertEqual(shorten_list([1, 2, 3, 4, 7, 8, 9], range_indicator=".."), "1..4,7..9") 

102 self.assertEqual(shorten_list([1, 2, 3, 4, 7, 8, 9], range_separator="^"), "1-4^7-9") 

103 self.assertEqual( 

104 shorten_list([1, 2, 3, 4, 7, 8, 9], range_indicator=":", range_separator=";"), "1:4;7:9" 

105 ) 

106 

107 # Test that those are keyword only. 

108 with self.assertRaises(TypeError): 

109 shorten_list([1, 2, 3, 4, 7, 8, 9], "..", "^") 

110 with self.assertRaises(TypeError): 

111 shorten_list([1, 2, 3, 4, 7, 8, 9], "..", range_separator="^") 

112 with self.assertRaises(TypeError): 

113 shorten_list([1, 2, 3, 4, 7, 8, 9], "!", range_indicator="..") 

114 

115 # Test that it sorts the container. 

116 self.assertEqual(shorten_list([6, 3, 1, 2]), "1-3,6") 

117 

118 # Repeated numbers are not expected. Test that the function can 

119 # nevertheless handle it. 

120 self.assertEqual(shorten_list([1, 2, 2, 3, 3, 3, 5, 7, 8, 7]), "1-3,5,7-8") 

121 

122 

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

124 lsst.utils.tests.init() 

125 unittest.main()