-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathts_plotting.py
More file actions
416 lines (359 loc) · 13.5 KB
/
ts_plotting.py
File metadata and controls
416 lines (359 loc) · 13.5 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
"""
Custom time series plotting functions implemented in plotly.
"""
import numpy as np
import pandas as pd
import plotly.graph_objects as go
from pmdarima import ARIMA
RED_COLOR_DICT = {
"95% PI": "rgba(255, 0, 0, 0.15)", # Lightest red
"90% PI": "rgba(255, 0, 0, 0.18)",
"80% PI": "rgba(255, 0, 0, 0.21)",
"70% PI": "rgba(255, 0, 0, 0.24)",
"60% PI": "rgba(255, 0, 0, 0.27)",
"50% PI": "rgba(255, 0, 0, 0.30)",
"40% PI": "rgba(255, 0, 0, 0.33)",
"30% PI": "rgba(255, 0, 0, 0.36)",
"20% PI": "rgba(255, 0, 0, 0.39)",
"10% PI": "rgba(255, 0, 0, 0.42)", # Darkest red
"forecast": "#660033", # Darker yellow or deep gray
}
BLUE_COLOR_DICT = {
"95% PI": "rgba(0, 0, 255, 0.15)", # Lightest blue
"90% PI": "rgba(0, 0, 255, 0.18)",
"80% PI": "rgba(0, 0, 255, 0.21)",
"70% PI": "rgba(0, 0, 255, 0.24)",
"60% PI": "rgba(0, 0, 255, 0.27)",
"50% PI": "rgba(0, 0, 255, 0.30)",
"40% PI": "rgba(0, 0, 255, 0.33)",
"30% PI": "rgba(0, 0, 255, 0.36)",
"20% PI": "rgba(0, 0, 255, 0.39)",
"10% PI": "rgba(0, 0, 255, 0.42)", # Darkest blue
}
GREEN_COLOR_DICT = {
"95% PI": "rgba(0, 128, 0, 0.15)", # Lightest green
"90% PI": "rgba(0, 128, 0, 0.18)",
"80% PI": "rgba(0, 128, 0, 0.21)",
"70% PI": "rgba(0, 128, 0, 0.24)",
"60% PI": "rgba(0, 128, 0, 0.27)",
"50% PI": "rgba(0, 128, 0, 0.30)",
"40% PI": "rgba(0, 128, 0, 0.33)",
"30% PI": "rgba(0, 128, 0, 0.36)",
"20% PI": "rgba(0, 128, 0, 0.39)",
"10% PI": "rgba(0, 128, 0, 0.42)", # Darkest green
"forecast": "#660033", # Deep purple or dark gray
}
def generate_arima_prediction_intervals(
model: ARIMA,
n_periods: int,
alpha: tuple = (0.05, 0.2)
) -> dict[str, pd.DataFrame]:
"""
For a given ARIMA model generate and return the specified
prediction intervals.
Parameters:
-----------
model: ARIMA
The trained ARIMA model. Note if the model is not trained the
prediction will fail.
n_periods: int
The forecast horizon
alpha: tuple, optional (default=(0.05, 0.2))
Specified the 100(1-alpha)% prediction intervals to generate
Returns
-------
dict{str: pd.DataFrame}
A dictionary containing the prediction intervals. Each set has its own
key e.g. '95%' and '80%'. The prediction intervals are a dataframe
with a datetimeindex and two columns (lower, upper).
"""
intervals_dict = {}
for current_alpha in alpha:
# forecast and prediction intervals
preds, intervals = model.predict(
n_periods=n_periods, return_conf_int=True, alpha=current_alpha
)
# cast to dataframe
intervals = pd.DataFrame(
intervals, index=preds.index, columns=["lower", "upper"]
)
# store in dict
interval_str = f"{round((1 - current_alpha) * 100, 0)}%"
intervals_dict[interval_str] = intervals
return intervals_dict
def plot_time_series(
training_data: pd.DataFrame,
test_data: pd.DataFrame | None = None,
forecast: pd.DataFrame | None = None,
prediction_intervals: dict[str, pd.DataFrame] | None = None,
test_data_mode: str = "markers",
y_axis_label: str = "Value",
color_dict: dict | None = None,
include_title=True
) -> None:
"""
Plots univariate time series data using Plotly. Options for displaying
training, test, and
Parameters:
----------
training_data: pd.DataFrame
A pandas DataFrame with a DatetimeIndex containing the training data
It should have one column representing the time series values.
test_data: pd.DataFrame, optional (default=None)
A pandas DataFrame with a DatetimeIndex containing the test data.
Displayed as black dots or a line based on `test_data_mode`.
forecast: pd.DataFrame, optional (default=None)
A pandas DataFrame with a DatetimeIndex containing the forecasted data.
prediction_intervals: dict[str, pd.DataFrame], Optional (default=None):
A dictionary of pandas dataframes that contain prediction intervals
Each dataframe contains two columns (upper and lower) and a
datetimeindex (over the forecast period). The key for the dictionary
can be set at your discertion. However, it is recommended to use
convention '95% PI', '80% PI' etc.
test_data_mode: str, optional (default='markers')
Mode for displaying test data.
Options are 'markers' (dots) or 'lines' (line).
y_axis_label: str, optional (default='Value')
The quantity measured by the time series to display on the y-axis
color_dict: dict, optional (default=None)
Dictionary with color codes for 'training', 'test', 'forecast',
and prediction intervals.
Returns:
None: Displays an interactive Plotly figure.
"""
# Validate test_data_mode input
if test_data_mode not in ["markers", "lines"]:
raise ValueError(
"Invalid value for test_data_mode. Choose 'markers' or 'lines'."
)
if color_dict is None:
color_dict = {}
# Set default colors for standard elements
color_dict.setdefault("training", "#0072B2")
color_dict.setdefault("test", "#000000")
color_dict.setdefault("forecast", "#FF0000")
# Default prediction interval colors (red shades)
default_pi_colors = [
"rgba(255, 0, 0, 0.15)", # Lightest red
"rgba(255, 0, 0, 0.18)",
"rgba(255, 0, 0, 0.21)",
"rgba(255, 0, 0, 0.24)",
"rgba(255, 0, 0, 0.27)",
"rgba(255, 0, 0, 0.30)",
"rgba(255, 0, 0, 0.33)",
"rgba(255, 0, 0, 0.36)",
"rgba(255, 0, 0, 0.39)",
"rgba(255, 0, 0, 0.42)", # Darkest red
]
# Create a Plotly figure
fig = go.Figure()
# Add training data as a line plot
fig.add_trace(
go.Scatter(
x=training_data.index,
y=training_data.iloc[:, 0],
mode="lines",
name="Training Data",
line=dict(color=color_dict["training"]),
)
)
# Add prediction intervals before forecast
if prediction_intervals is not None:
for idx, (interval_name, interval_df) in enumerate(
prediction_intervals.items()
):
if not {"lower", "upper"}.issubset(interval_df.columns):
err_msg = "DataFrame must contain 'lower' and 'upper' columns"
raise ValueError(
f"{interval_name} {err_msg}"
)
interval_color = color_dict.get(
interval_name, default_pi_colors[idx % len(default_pi_colors)]
)
# Upper bound
fig.add_trace(
go.Scatter(
x=interval_df.index,
y=interval_df["upper"],
mode="lines",
line={"width": 0},
showlegend=False,
name=f"{interval_name}",
)
)
# Lower bound with fill
fig.add_trace(
go.Scatter(
x=interval_df.index,
y=interval_df["lower"],
mode="lines",
line={"width": 0},
fill="tonexty",
fillcolor=interval_color,
name=f"{interval_name}",
showlegend=True,
)
)
# Add forecast line
if forecast is not None:
fig.add_trace(
go.Scatter(
x=forecast.index,
y=forecast.iloc[:, 0],
mode="lines",
name="Point Forecast",
line=dict(color=color_dict["forecast"], dash="dash"),
)
)
# Add test data based on the selected mode
if test_data is not None:
fig.add_trace(
go.Scatter(
x=test_data.index,
y=test_data.iloc[:, 0],
mode=test_data_mode,
name="Test Data",
marker=(
dict(color=color_dict["test"], size=6)
if test_data_mode == "markers"
else None
),
line=(
dict(color=color_dict["test"])
if test_data_mode == "lines"
else None
),
)
)
title = ""
if include_title:
title="Univariate Time Series Visualization"
# Enable vertical spike lines on hover
fig.update_layout(
title=title,
xaxis=dict(
showspikes=True, # Enable spikes
spikemode="across", # Spike spans across all traces
spikesnap="cursor", # Spike snaps to cursor position
spikedash="dot", # Style of spike line
spikethickness=1.5, # Thickness of spike line
spikecolor="gray", # Color of spike line
),
yaxis=dict(showspikes=False), # Disable horizontal spikes
hovermode="x", # Hover closest to x-axis value
template="plotly_white",
xaxis_title="Date",
yaxis_title=y_axis_label,
legend=dict(
orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1
),
)
# Show the figure
fig.show()
# def plot_time_series(
# training_data: pd.DataFrame,
# test_data: pd.DataFrame | None = None,
# forecast: pd.DataFrame | None = None,
# test_data_mode: str = "markers",
# y_axis_label: str = "Value",
# color_dict: dict | None = None,
# ) -> None:
# """
# Plots univariate time series data using Plotly. Options for displaying
# training, test, and
# Parameters:
# ----------
# training_data: pd.DataFrame
# A pandas DataFrame with a DatetimeIndex containing the training data
# It should have one column representing the time series values.
# test_data: pd.DataFrame, optional (default=None)
# A pandas DataFrame with a DatetimeIndex containing the test data.
# Displayed as black dots or a line based on `test_data_mode`.
# forecast: pd.DataFrame, optional (default=None)
# A pandas DataFrame with a DatetimeIndex containing the forecasted data.
# test_data_mode: str, optional (default='markers')
# Mode for displaying test data.
# Options are 'markers' (dots) or 'lines' (line).
# y_axis_label: str, optional (default='Value')
# The quantity measured by the time series to display on the y-axis
# color_dict: dict, optional (default=None)
# Dictionary with color codes for 'training', 'test', and 'forecast'.
# Returns:
# None: Displays an interactive Plotly figure.
# """
# # Validate test_data_mode input
# if test_data_mode not in ["markers", "lines"]:
# raise ValueError(
# "Invalid value for test_data_mode. Choose 'markers' or 'lines'."
# )
# if color_dict is None:
# color_dict = {
# "training": "#0072B2", # Default blue color for training data
# "test": "#000000", # Default black color for test data
# "forecast": "#FF0000", # Default red color for forecast data
# }
# # Create a Plotly figure
# fig = go.Figure()
# # Add training data as a line plot
# fig.add_trace(
# go.Scatter(
# x=training_data.index,
# y=training_data.iloc[:, 0],
# mode="lines",
# name="Training Data",
# line=dict(color=color_dict["training"]),
# )
# )
# # Add test data based on the selected mode
# if test_data is not None:
# fig.add_trace(
# go.Scatter(
# x=test_data.index,
# y=test_data.iloc[:, 0],
# mode=test_data_mode,
# name="Test Data",
# marker=(
# dict(color=color_dict["test"], size=6)
# if test_data_mode == "markers"
# else None
# ),
# line=(
# dict(color=color_dict["test"])
# if test_data_mode == "lines"
# else None
# ),
# )
# )
# # Add forecast data as a line plot if provided
# if forecast is not None:
# fig.add_trace(
# go.Scatter(
# x=forecast.index,
# y=forecast.iloc[:, 0],
# mode="lines",
# name="Point Forecast",
# line=dict(
# color=color_dict["forecast"], dash="dash"
# ), # Dashed red line for forecast
# )
# )
# # Enable vertical spike lines on hover
# fig.update_layout(
# title="Univariate Time Series Visualization",
# xaxis=dict(
# showspikes=True, # Enable spikes
# spikemode="across", # Spike spans across all traces
# spikesnap="cursor", # Spike snaps to cursor position
# spikedash="dot", # Style of spike line
# spikethickness=1.5, # Thickness of spike line
# spikecolor="gray", # Color of spike line
# ),
# yaxis=dict(showspikes=False), # Disable horizontal spikes
# hovermode="x", # Hover closest to x-axis value
# template="plotly_white",
# xaxis_title="Date",
# yaxis_title=y_axis_label,
# legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1),
# )
# # Show the figure
# fig.show()