Coverage for tests/test_plotting_limits.py: 31%
112 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-08 09:53 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-08 09:53 +0000
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#
13import unittest
15import numpy as np
16from lsst.utils.plotting.limits import calculate_safe_plotting_limits, make_calculate_safe_plotting_limits
19class PlottingLimitsClosureTestCase(unittest.TestCase):
20 """Tests for `make_calculate_safe_plotting_limits` function."""
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)
27 series2 = np.sin(xs) + 1.2 # min=0.2, max=2.19999
28 series2_min = min(series2)
29 series2_max = max(series2)
31 outliers = series1[:]
32 outliers[1000] = 20
33 outliers[2000] = -1000
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 = make_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)
48 def testMultipleSeries(self):
49 """Test that passing multiple several series in works wrt outliers."""
50 calculate_safe_plotting_limits_accumulator = make_calculate_safe_plotting_limits()
51 _, _ = calculate_safe_plotting_limits_accumulator(self.series1)
52 ymin, ymax = calculate_safe_plotting_limits_accumulator(self.outliers)
54 self.assertLess(ymin, self.series1_min)
55 self.assertGreater(ymin, self.series1_min - 1)
56 self.assertLess(ymax, self.series1_max + 1)
57 self.assertGreater(ymax, self.series1_max)
59 def testMultipleSeriesCommonRange(self):
60 """Test that passing multiple several series in works wrt outliers."""
61 calculate_safe_plotting_limits_accumulator = make_calculate_safe_plotting_limits()
62 _, _ = calculate_safe_plotting_limits_accumulator(self.series1)
63 ymin, ymax = calculate_safe_plotting_limits_accumulator(self.series2)
64 # lower bound less than the lowest of the two
65 self.assertLess(ymin, min(self.series1_min, self.series2_min))
66 # lower bound less than the lowest of the two, but not by much
67 self.assertGreater(ymin, min(self.series1_min, self.series2_min) - 1)
68 # upper bound greater than the highest of the two
69 self.assertGreater(ymax, max(self.series1_max, self.series2_max))
70 # upper bound greater than the highest of the two, but not by much
71 self.assertLess(ymax, max(self.series1_max, self.series2_max) + 1)
73 def testSymmetric(self):
74 """Test that the symmetric option works"""
75 calc = make_calculate_safe_plotting_limits(symmetric_around_zero=True)
76 _, _ = calc(self.series1)
77 ymin, ymax = calc(self.outliers)
79 self.assertEqual(ymin, -ymax)
80 self.assertGreater(ymax, self.series1_max)
81 self.assertLess(ymin, self.series1_min)
83 def testConstantExtra(self):
84 """Test that the constantExtra option works"""
85 calc = make_calculate_safe_plotting_limits(constant_extra=0)
86 _, _ = calc(self.series1)
87 strictMin, strictMax = calc(self.outliers)
89 self.assertAlmostEqual(strictMin, self.series1_min, places=4)
90 self.assertAlmostEqual(strictMax, self.series1_max, places=4)
92 for extra in [-2.123, -1, 0, 1, 1.5, 23]:
93 calc = make_calculate_safe_plotting_limits(constant_extra=extra)
94 _, _ = calc(self.series1)
95 ymin, ymax = calc(self.outliers)
97 self.assertAlmostEqual(ymin, self.series1_min - extra, places=4)
98 self.assertAlmostEqual(ymax, self.series1_max + extra, places=4)
100 def testSeriesOfSeries(self):
101 """Test that we can pass a list of series to the accumulator in one."""
102 calculate_safe_plotting_limits_accumulator = make_calculate_safe_plotting_limits()
103 ymin, ymax = calculate_safe_plotting_limits_accumulator([self.series1, self.outliers])
105 self.assertLess(ymin, self.series1_min)
106 self.assertGreater(ymin, self.series1_min - 1)
107 self.assertLess(ymax, self.series1_max + 1)
108 self.assertGreater(ymax, self.series1_max)
110 def testRaises(self):
111 with self.assertRaises(TypeError):
112 make_calculate_safe_plotting_limits()(1.234)
115class PlottingLimitsTestCase(unittest.TestCase):
116 """Tests for `calculate_safe_plotting_limits` function."""
118 xs = np.linspace(0, 10, 10000)
119 series1 = np.sin(xs + 3.1415 / 2) + 0.75 # min=-0.24999, max=1.74999
120 series1_min = min(series1)
121 series1_max = max(series1)
123 series2 = np.sin(xs) + 1.2 # min=0.2, max=2.19999
124 series2_min = min(series2)
125 series2_max = max(series2)
127 outliers = series1[:]
128 outliers[1000] = 20
129 outliers[2000] = -1000
131 def testSingleSeries(self):
132 """Test that a single series works and the outliers exclusion works."""
133 # Deliberately test the bounds are the same when using the series
134 # itself, and the copy with the outlier values, i.e. using
135 # self.series1_min/max inside the loop despite changing the series we
136 # loop over is the intent here, not a bug.
137 for series in [self.series1, self.outliers]:
138 ymin, ymax = calculate_safe_plotting_limits(series)
139 self.assertLess(ymin, self.series1_min)
140 self.assertGreater(ymin, self.series1_min - 1)
141 self.assertLess(ymax, self.series1_max + 1)
142 self.assertGreater(ymax, self.series1_max)
144 def testMultipleSeries(self):
145 """Test that passing multiple several series in works wrt outliers."""
146 ymin, ymax = calculate_safe_plotting_limits([self.series1, self.outliers])
147 self.assertLess(ymin, self.series1_min)
148 self.assertGreater(ymin, self.series1_min - 1)
149 self.assertLess(ymax, self.series1_max + 1)
150 self.assertGreater(ymax, self.series1_max)
152 def testMultipleSeriesCommonRange(self):
153 """Test that passing multiple several series in works wrt outliers."""
154 ymin, ymax = calculate_safe_plotting_limits([self.series1, self.series2])
155 # lower bound less than the lowest of the two
156 self.assertLess(ymin, min(self.series1_min, self.series2_min))
157 # lower bound less than the lowest of the two, but not by much
158 self.assertGreater(ymin, min(self.series1_min, self.series2_min) - 1)
159 # upper bound greater than the highest of the two
160 self.assertGreater(ymax, max(self.series1_max, self.series2_max))
161 # upper bound greater than the highest of the two, but not by much
162 self.assertLess(ymax, max(self.series1_max, self.series2_max) + 1)
164 def testSymmetric(self):
165 """Test that the symmetric option works"""
166 ymin, ymax = calculate_safe_plotting_limits([self.series1, self.outliers], symmetric_around_zero=True)
167 self.assertEqual(ymin, -ymax)
168 self.assertGreater(ymax, self.series1_max)
169 self.assertLess(ymin, self.series1_min)
171 def testConstantExtra(self):
172 """Test that the constantExtra option works"""
173 strictMin, strictMax = calculate_safe_plotting_limits([self.series1, self.outliers], constant_extra=0)
174 self.assertAlmostEqual(strictMin, self.series1_min, places=4)
175 self.assertAlmostEqual(strictMax, self.series1_max, places=4)
177 for extra in [-2.123, -1, 0, 1, 1.5, 23]:
178 ymin, ymax = calculate_safe_plotting_limits([self.series1, self.outliers], constant_extra=extra)
179 self.assertAlmostEqual(ymin, self.series1_min - extra, places=4)
180 self.assertAlmostEqual(ymax, self.series1_max + extra, places=4)
182 def testRaises(self):
183 with self.assertRaises(TypeError):
184 calculate_safe_plotting_limits(1.234)
187if __name__ == "__main__":
188 unittest.main()