-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_plot.py
More file actions
115 lines (104 loc) · 4.21 KB
/
test_plot.py
File metadata and controls
115 lines (104 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import unittest
from unittest.mock import patch, MagicMock
import numpy as np
from dte_adj.plot import plot
class TestPlot(unittest.TestCase):
@patch("dte_adj.plot.plt")
def test_plot(self, mock_plt):
# Arrange
x_values = np.array([1, 2, 3, 4, 5])
means = np.array([1, 2, 3, 4, 5])
upper_bands = np.array([2, 3, 4, 5, 6])
lower_bands = np.array([0, 1, 2, 3, 4])
mock_ax = MagicMock()
mock_plt.subplots.return_value = (MagicMock(), mock_ax)
# Act
result_ax = plot(
x_values,
means,
lower_bands,
upper_bands,
title="Test Title",
xlabel="X Axis",
ylabel="Y Axis",
chart_type="line",
)
# Assert
self.assertEqual(result_ax, mock_ax)
mock_plt.subplots.assert_called_once()
plot_call = mock_ax.plot.call_args
fill_between_call = mock_ax.fill_between.call_args
plot_args, plot_kwargs = plot_call
x_values_arg, y_values_arg = plot_args
self.assertTrue(np.array_equal(x_values_arg, x_values))
self.assertTrue(np.array_equal(y_values_arg, means))
fill_between_args, fill_between_kwargs = fill_between_call
x_fill, lower_fill, upper_fill = fill_between_args
self.assertTrue(np.array_equal(x_fill, x_values_arg))
self.assertTrue(np.array_equal(lower_fill, lower_bands))
self.assertTrue(np.array_equal(upper_fill, upper_bands))
self.assertEqual(fill_between_kwargs["color"], "green")
self.assertAlmostEqual(fill_between_kwargs["alpha"], 0.3)
self.assertEqual(fill_between_kwargs["label"], "Confidence Interval")
mock_ax.set_title.assert_called_once_with("Test Title")
mock_ax.set_xlabel.assert_called_once_with("X Axis")
mock_ax.set_ylabel.assert_called_once_with("Y Axis")
def test_plot_fail_unknown_chart_type(self):
# Arrange
x_values = np.array([1, 2, 3, 4, 5])
means = np.array([1, 2, 3, 4, 5])
upper_bands = np.array([2, 3, 4, 5, 6])
lower_bands = np.array([0, 1, 2, 3, 4])
# Act, Assert
with self.assertRaises(ValueError) as cm:
plot(
x_values,
means,
lower_bands,
upper_bands,
title="Test Title",
xlabel="X Axis",
ylabel="Y Axis",
chart_type="other",
)
self.assertEqual(
str(cm.exception),
"Chart type other is not supported",
)
@patch("dte_adj.plot.plt")
def test_plot_weighted(self, mock_plt):
# Arrange
x_values = np.array([1, 2, 3, 4, 5])
means = np.array([0.1, 0.2, 0.3, 0.4, 0.5])
upper_bands = np.array([0.2, 0.3, 0.4, 0.5, 0.6])
lower_bands = np.array([0.0, 0.1, 0.2, 0.3, 0.4])
mock_ax = MagicMock()
mock_plt.subplots.return_value = (MagicMock(), mock_ax)
# Act
result_ax = plot(
x_values,
means,
lower_bands,
upper_bands,
chart_type="line",
weighted=True,
)
# Assert
self.assertEqual(result_ax, mock_ax)
mock_plt.subplots.assert_called_once()
plot_call = mock_ax.plot.call_args
fill_between_call = mock_ax.fill_between.call_args
# Check that values are weighted (multiplied by x_values)
plot_args, plot_kwargs = plot_call
x_values_arg, y_values_arg = plot_args
expected_weighted_means = means * x_values
self.assertTrue(np.array_equal(x_values_arg, x_values))
self.assertTrue(np.array_equal(y_values_arg, expected_weighted_means))
# Check that confidence intervals are also weighted
fill_between_args, fill_between_kwargs = fill_between_call
x_fill, lower_fill, upper_fill = fill_between_args
expected_weighted_lower = lower_bands * x_values
expected_weighted_upper = upper_bands * x_values
self.assertTrue(np.array_equal(x_fill, x_values_arg))
self.assertTrue(np.array_equal(lower_fill, expected_weighted_lower))
self.assertTrue(np.array_equal(upper_fill, expected_weighted_upper))