Coverage for tests/test_plotUtils.py: 19%

51 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-01-25 12:23 +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.plot.plotUtils import perpDistance, shorten_list, stellarLocusFit 

26 

27 

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

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

30 

31 def testFitLine(self): 

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

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

34 

35 xs = np.arange(1, 10) 

36 ys = np.arange(1, 10) 

37 

38 # Define an initial fit box that encompasses the points 

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

40 paramsOut = stellarLocusFit(xs, ys, testParams) 

41 

42 # stellarLocusFit performs two iterations of fitting and also 

43 # calculates the perpendicular gradient to the fit line and 

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

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

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

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

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

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

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

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

52 

53 def testPerpDistance(self): 

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

55 

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

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

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

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

60 # then calculates the perpendicular distance of the testPoints 

61 # to this line 

62 dists = perpDistance(p1, p2, testPoints) 

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

64 

65 

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

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

68 

69 def test_shorten_list(self): 

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

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

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

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

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

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

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

77 

78 # Test for different data types 

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

80 self.assertEqual( 

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

82 ) # test for a tuple 

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

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

85 

86 # Test the RC2/DC2 tract list explicitly. 

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

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

89 

90 # Test the keyword-only arguments. 

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

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

93 self.assertEqual( 

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

95 ) 

96 

97 # Test that those are keyword only. 

98 with self.assertRaises(TypeError): 

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

100 with self.assertRaises(TypeError): 

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

102 with self.assertRaises(TypeError): 

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

104 

105 # Test that it sorts the container. 

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

107 

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

109 # nevertheless handle it. 

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

111 

112 

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

114 lsst.utils.tests.init() 

115 unittest.main()