|
| 1 | +from itertools import product |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +import matplotlib.collections as mcollections |
| 5 | +import matplotlib.pyplot as plt |
| 6 | +import numpy as np |
| 7 | +import pandas as pd |
| 8 | +import pytest |
| 9 | + |
| 10 | +from darts import TimeSeries |
| 11 | +from darts.utils.utils import generate_index |
| 12 | + |
| 13 | + |
| 14 | +class TestTimeSeriesPlot: |
| 15 | + # datetime index, deterministic |
| 16 | + n_comps = 2 |
| 17 | + series_dt_d = TimeSeries.from_times_and_values( |
| 18 | + times=generate_index(start="2000-01-01", length=10, freq="D"), |
| 19 | + values=np.random.random((10, n_comps, 1)), |
| 20 | + ) |
| 21 | + # datetime index, probabilistic |
| 22 | + series_dt_p = TimeSeries.from_times_and_values( |
| 23 | + times=generate_index(start="2000-01-01", length=10, freq="D"), |
| 24 | + values=np.random.random((10, n_comps, 5)), |
| 25 | + ) |
| 26 | + # range index, deterministic |
| 27 | + series_ri_d = TimeSeries.from_times_and_values( |
| 28 | + times=generate_index(start=0, length=10, freq=1), |
| 29 | + values=np.random.random((10, n_comps, 1)), |
| 30 | + ) |
| 31 | + # range index, probabilistic |
| 32 | + series_ri_p = TimeSeries.from_times_and_values( |
| 33 | + times=generate_index(start=0, length=10, freq=1), |
| 34 | + values=np.random.random((10, n_comps, 5)), |
| 35 | + ) |
| 36 | + |
| 37 | + @patch("matplotlib.pyplot.show") |
| 38 | + @pytest.mark.parametrize( |
| 39 | + "config", |
| 40 | + product( |
| 41 | + ["dt", "ri"], |
| 42 | + ["d", "p"], |
| 43 | + [True, False], |
| 44 | + ), |
| 45 | + ) |
| 46 | + def test_plot_single_series(self, mock_show, config): |
| 47 | + index_type, stoch_type, use_ax = config |
| 48 | + series = getattr(self, f"series_{index_type}_{stoch_type}") |
| 49 | + if use_ax: |
| 50 | + _, ax = plt.subplots() |
| 51 | + else: |
| 52 | + ax = None |
| 53 | + series.plot(ax=ax) |
| 54 | + |
| 55 | + # For deterministic series with len > 1: one line per component |
| 56 | + # For probabilistic series with len > 1: one line per component + one area per component |
| 57 | + ax = ax if use_ax else plt.gca() |
| 58 | + |
| 59 | + # Count lines (Line2D objects with multiple data points representing actual lines) |
| 60 | + lines = [line for line in ax.lines if len(line.get_xdata()) > 1] |
| 61 | + assert len(lines) == self.n_comps |
| 62 | + |
| 63 | + # For probabilistic: count filled areas (PolyCollection from fill_between) |
| 64 | + if series.is_stochastic: |
| 65 | + areas = [ |
| 66 | + coll |
| 67 | + for coll in ax.collections |
| 68 | + if isinstance(coll, mcollections.PolyCollection) |
| 69 | + ] |
| 70 | + assert len(areas) == self.n_comps |
| 71 | + |
| 72 | + plt.show() |
| 73 | + plt.close() |
| 74 | + |
| 75 | + @patch("matplotlib.pyplot.show") |
| 76 | + @pytest.mark.parametrize( |
| 77 | + "config", |
| 78 | + product( |
| 79 | + ["dt", "ri"], |
| 80 | + ["d", "p"], |
| 81 | + ), |
| 82 | + ) |
| 83 | + def test_plot_point_series(self, mock_show, config): |
| 84 | + index_type, stoch_type = config |
| 85 | + series = getattr(self, f"series_{index_type}_{stoch_type}") |
| 86 | + series = series[:1] |
| 87 | + series.plot() |
| 88 | + |
| 89 | + # For deterministic series with len == 1: one point per component |
| 90 | + # For probabilistic series with len == 1: one point per component + one vertical line per component |
| 91 | + ax = plt.gca() |
| 92 | + |
| 93 | + # Count points (Line2D objects with markers representing single points) |
| 94 | + points = [ |
| 95 | + line |
| 96 | + for line in ax.lines |
| 97 | + if len(line.get_xdata()) == 1 and line.get_marker() != "None" |
| 98 | + ] |
| 99 | + assert len(points) == self.n_comps |
| 100 | + |
| 101 | + # For probabilistic: count vertical lines for confidence intervals |
| 102 | + if series.is_stochastic: |
| 103 | + # The confidence interval is plotted as a line with "-+" marker |
| 104 | + # It's a vertical line where x-coordinates are the same |
| 105 | + vert_lines = [] |
| 106 | + for line in ax.lines: |
| 107 | + xdata = np.asarray(line.get_xdata()) |
| 108 | + ydata = np.asarray(line.get_ydata()) |
| 109 | + if len(xdata) == 2 and len(ydata) == 2: |
| 110 | + # check if x-coords are the same (vertical line) |
| 111 | + xdiff = xdata[0] - xdata[1] |
| 112 | + |
| 113 | + if isinstance(xdiff, pd.Timedelta): |
| 114 | + xdiff = xdiff.total_seconds() |
| 115 | + |
| 116 | + if abs(xdiff) < 1e-10: |
| 117 | + vert_lines.append(line) |
| 118 | + assert len(vert_lines) == self.n_comps |
| 119 | + |
| 120 | + plt.show() |
| 121 | + plt.close() |
| 122 | + |
| 123 | + @patch("matplotlib.pyplot.show") |
| 124 | + @pytest.mark.parametrize( |
| 125 | + "config", |
| 126 | + product( |
| 127 | + ["dt", "ri"], |
| 128 | + ["d", "p"], |
| 129 | + ), |
| 130 | + ) |
| 131 | + def test_plot_empty_series(self, mock_show, config): |
| 132 | + index_type, stoch_type = config |
| 133 | + series = getattr(self, f"series_{index_type}_{stoch_type}") |
| 134 | + series = series[:0] |
| 135 | + series.plot() |
| 136 | + |
| 137 | + # For len == 0: no points or lines should be plotted |
| 138 | + ax = plt.gca() |
| 139 | + # empty plot creates a line with empty data, but we want to check for actual plotted content |
| 140 | + # no points |
| 141 | + points = [ |
| 142 | + line |
| 143 | + for line in ax.lines |
| 144 | + if len(line.get_xdata()) == 1 and line.get_marker() != "None" |
| 145 | + ] |
| 146 | + assert len(points) == 0 |
| 147 | + |
| 148 | + # no lines |
| 149 | + lines_meaningful = [line for line in ax.lines if len(line.get_xdata()) > 1] |
| 150 | + assert len(lines_meaningful) == 0 |
| 151 | + |
| 152 | + # no areas |
| 153 | + areas = [ |
| 154 | + coll |
| 155 | + for coll in ax.collections |
| 156 | + if isinstance(coll, mcollections.PolyCollection) |
| 157 | + ] |
| 158 | + assert len(areas) == 0 |
| 159 | + |
| 160 | + plt.show() |
| 161 | + plt.close() |
| 162 | + |
| 163 | + @patch("matplotlib.pyplot.show") |
| 164 | + @pytest.mark.parametrize( |
| 165 | + "config", |
| 166 | + product( |
| 167 | + ["dt", "ri"], |
| 168 | + ["d", "p"], |
| 169 | + [ |
| 170 | + {"new_plot": True}, |
| 171 | + {"default_formatting": False}, |
| 172 | + {"title": "my title"}, |
| 173 | + {"label": "comps"}, |
| 174 | + {"label": ["comps_1", "comps_2"]}, |
| 175 | + {"alpha": 0.1, "color": "blue"}, |
| 176 | + {"color": ["blue", "red"]}, |
| 177 | + {"lw": 2}, |
| 178 | + ], |
| 179 | + ), |
| 180 | + ) |
| 181 | + def test_plot_params(self, mock_show, config): |
| 182 | + index_type, stoch_type, kwargs = config |
| 183 | + series = getattr(self, f"series_{index_type}_{stoch_type}") |
| 184 | + series.plot(**kwargs) |
| 185 | + plt.show() |
| 186 | + plt.close() |
| 187 | + |
| 188 | + @patch("matplotlib.pyplot.show") |
| 189 | + @pytest.mark.parametrize( |
| 190 | + "config", |
| 191 | + product( |
| 192 | + ["dt", "ri"], |
| 193 | + [ |
| 194 | + {"central_quantile": "mean"}, |
| 195 | + {"central_quantile": 0.5}, |
| 196 | + { |
| 197 | + "low_quantile": 0.2, |
| 198 | + "central_quantile": 0.6, |
| 199 | + "high_quantile": 0.7, |
| 200 | + "alpha": 0.1, |
| 201 | + }, |
| 202 | + ], |
| 203 | + ), |
| 204 | + ) |
| 205 | + def test_plot_stochastic_params(self, mock_show, config): |
| 206 | + (index_type, kwargs), stoch_type = config, "p" |
| 207 | + series = getattr(self, f"series_{index_type}_{stoch_type}") |
| 208 | + series.plot(**kwargs) |
| 209 | + plt.show() |
| 210 | + plt.close() |
| 211 | + |
| 212 | + @patch("matplotlib.pyplot.show") |
| 213 | + @pytest.mark.parametrize("config", ["dt", "ri"]) |
| 214 | + def test_plot_multiple_series(self, mock_show, config): |
| 215 | + index_type = config |
| 216 | + series1 = getattr(self, f"series_{index_type}_d") |
| 217 | + series2 = getattr(self, f"series_{index_type}_p") |
| 218 | + series1.plot() |
| 219 | + series2.plot() |
| 220 | + plt.show() |
| 221 | + plt.close() |
| 222 | + |
| 223 | + @patch("matplotlib.pyplot.show") |
| 224 | + @pytest.mark.parametrize("config", ["dt", "ri"]) |
| 225 | + def test_plot_deterministic_and_stochastic(self, mock_show, config): |
| 226 | + index_type = config |
| 227 | + series1 = getattr(self, f"series_{index_type}_d") |
| 228 | + series2 = getattr(self, f"series_{index_type}_p") |
| 229 | + series1.plot() |
| 230 | + series2.plot() |
| 231 | + plt.show() |
| 232 | + plt.close() |
| 233 | + |
| 234 | + @patch("matplotlib.pyplot.show") |
| 235 | + @pytest.mark.parametrize("config", ["d", "p"]) |
| 236 | + def test_cannot_plot_different_index_types(self, mock_show, config): |
| 237 | + stoch_type = config |
| 238 | + series1 = getattr(self, f"series_dt_{stoch_type}") |
| 239 | + series2 = getattr(self, f"series_ri_{stoch_type}") |
| 240 | + # datetime index plot changes x-axis to use datetime index |
| 241 | + series1.plot() |
| 242 | + # cannot plot a range index on datetime index |
| 243 | + with pytest.raises(TypeError): |
| 244 | + series2.plot() |
| 245 | + plt.show() |
| 246 | + plt.close() |
0 commit comments