Coverage for tests/test_plotUtils.py: 20%

52 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-10 14:10 +0000

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 perpDistance, stellarLocusFit 

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 

39 # Define an initial fit box that encompasses the points 

40 testParams = {"xMin": 0, "xMax": 11, "yMin": 0, "yMax": 11, "mHW": 1, "bHW": 0} 

41 paramsOut = stellarLocusFit(xs, ys, testParams) 

42 

43 # stellarLocusFit performs two iterations of fitting and also 

44 # calculates the perpendicular gradient to the fit line and 

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

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

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

48 self.assertFloatsAlmostEqual(paramsOut["mODR2"], 1.0) 

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

50 self.assertFloatsAlmostEqual(paramsOut["bODR2"], 0.0) 

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

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

53 

54 def testPerpDistance(self): 

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

56 

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

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

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

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

61 # then calculates the perpendicular distance of the testPoints 

62 # to this line 

63 dists = perpDistance(p1, p2, testPoints) 

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

65 

66 

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

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

69 

70 def test_shorten_list(self): 

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

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

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

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

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

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

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

78 

79 # Test for different data types 

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

81 self.assertEqual( 

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

83 ) # test for a tuple 

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

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

86 

87 # Test the RC2/DC2 tract list explicitly. 

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

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

90 

91 # Test the keyword-only arguments. 

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

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

94 self.assertEqual( 

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

96 ) 

97 

98 # Test that those are keyword only. 

99 with self.assertRaises(TypeError): 

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

101 with self.assertRaises(TypeError): 

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

103 with self.assertRaises(TypeError): 

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

105 

106 # Test that it sorts the container. 

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

108 

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

110 # nevertheless handle it. 

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

112 

113 

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

115 lsst.utils.tests.init() 

116 unittest.main()