Coverage for tests/test_plotting_limits.py: 36%

49 statements  

« prev     ^ index     » next       coverage.py v7.2.3, created at 2023-04-19 03:35 -0700

1# This file is part of utils. 

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# Use of this source code is governed by a 3-clause BSD-style 

10# license that can be found in the LICENSE file. 

11# 

12 

13import unittest 

14 

15import numpy as np 

16from lsst.utils.plotting.limits import calculate_safe_plotting_limits 

17 

18 

19class PlottingLimitsTestCase(unittest.TestCase): 

20 """Tests for `calculate_safe_plotting_limits` function.""" 

21 

22 xs = np.linspace(0, 10, 10000) 

23 series1 = np.sin(xs + 3.1415 / 2) + 0.75 # min=-0.24999, max=1.74999 

24 series1_min = min(series1) 

25 series1_max = max(series1) 

26 

27 series2 = np.sin(xs) + 1.2 # min=0.2, max=2.19999 

28 series2_min = min(series2) 

29 series2_max = max(series2) 

30 

31 outliers = series1[:] 

32 outliers[1000] = 20 

33 outliers[2000] = -1000 

34 

35 def testSingleSeries(self): 

36 """Test that a single series works and the outliers exclusion works.""" 

37 # Deliberately test the bounds are the same when using the series 

38 # itself, and the copy with the outlier values, i.e. using 

39 # self.series1_min/max inside the loop despite changing the series we 

40 # loop over is the intent here, not a bug. 

41 for series in [self.series1, self.outliers]: 

42 ymin, ymax = calculate_safe_plotting_limits(series) 

43 self.assertLess(ymin, self.series1_min) 

44 self.assertGreater(ymin, self.series1_min - 1) 

45 self.assertLess(ymax, self.series1_max + 1) 

46 self.assertGreater(ymax, self.series1_max) 

47 

48 def testMultipleSeries(self): 

49 """Test that passing multiple several series in works wrt outliers.""" 

50 ymin, ymax = calculate_safe_plotting_limits([self.series1, self.outliers]) 

51 self.assertLess(ymin, self.series1_min) 

52 self.assertGreater(ymin, self.series1_min - 1) 

53 self.assertLess(ymax, self.series1_max + 1) 

54 self.assertGreater(ymax, self.series1_max) 

55 

56 def testMultipleSeriesCommonRange(self): 

57 """Test that passing multiple several series in works wrt outliers.""" 

58 ymin, ymax = calculate_safe_plotting_limits([self.series1, self.series2]) 

59 # lower bound less than the lowest of the two 

60 self.assertLess(ymin, min(self.series1_min, self.series2_min)) 

61 # lower bound less than the lowest of the two, but not by much 

62 self.assertGreater(ymin, min(self.series1_min, self.series2_min) - 1) 

63 # upper bound greater than the highest of the two 

64 self.assertGreater(ymax, max(self.series1_max, self.series2_max)) 

65 # upper bound greater than the highest of the two, but not by much 

66 self.assertLess(ymax, max(self.series1_max, self.series2_max) + 1) 

67 

68 def testSymmetric(self): 

69 """Test that the symmetric option works""" 

70 ymin, ymax = calculate_safe_plotting_limits([self.series1, self.outliers], symmetric_around_zero=True) 

71 self.assertEqual(ymin, -ymax) 

72 self.assertGreater(ymax, self.series1_max) 

73 self.assertLess(ymin, self.series1_min) 

74 

75 def testConstantExtra(self): 

76 """Test that the constantExtra option works""" 

77 strictMin, strictMax = calculate_safe_plotting_limits([self.series1, self.outliers], constant_extra=0) 

78 self.assertAlmostEqual(strictMin, self.series1_min, places=4) 

79 self.assertAlmostEqual(strictMax, self.series1_max, places=4) 

80 

81 for extra in [-2.123, -1, 0, 1, 1.5, 23]: 

82 ymin, ymax = calculate_safe_plotting_limits([self.series1, self.outliers], constant_extra=extra) 

83 self.assertAlmostEqual(ymin, self.series1_min - extra, places=4) 

84 self.assertAlmostEqual(ymax, self.series1_max + extra, places=4) 

85 

86 def testRaises(self): 

87 with self.assertRaises(TypeError): 

88 calculate_safe_plotting_limits(1.234) 

89 

90 

91if __name__ == "__main__": 

92 unittest.main()