-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsimple_task_example.py
149 lines (120 loc) · 4.33 KB
/
simple_task_example.py
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
import matplotlib.pyplot as plt
import pandas as pd
import textwrap
import numpy as np
from cik_benchmark.sensor_maintenance import SensorMaintenanceInPredictionTask
def plot_forecast_univariate(task, filename):
"""
Plot the first variable of a forecast.
Parameters:
-----------
task: a BaseTask
The task associated with the forecast
filename: Pathlike
Where to save the figure
"""
past_timesteps = task.past_time.index
past_values = task.past_time.to_numpy()[:, -1]
future_timesteps = task.future_time.index
future_values = task.future_time.to_numpy()[:, -1]
# The fill_between method is only ok with pd.DatetimeIndex
if isinstance(past_timesteps, pd.PeriodIndex):
past_timesteps = past_timesteps.to_timestamp()
if isinstance(future_timesteps, pd.PeriodIndex):
future_timesteps = future_timesteps.to_timestamp()
plt.figure(figsize=(15, 15))
plt.plot(
past_timesteps,
past_values,
color=(1, 0, 0),
linewidth=2,
zorder=5,
label="history",
)
plt.plot(
future_timesteps,
future_values,
color=(0, 0, 1),
linewidth=2,
zorder=5,
label="prediction",
)
plt.xticks(fontsize=7)
plt.legend()
plt.title(
"\n".join(textwrap.wrap(task.scenario, width=40))
) # Change this to task.background or task.constraint depending on task
plt.savefig(filename)
def plot_forecast_with_covariates(task, filename):
"""
Plot the first variable of a forecast, along with all covariates.
Parameters:
-----------
task: a BaseTask
The task associated with the forecast
filename: Pathlike
Where to save the figure
"""
past_values = task.past_time.to_numpy()[:, -1]
future_values = task.future_time.to_numpy()[:, -1]
past_timesteps = np.arange(len(past_values))
future_timesteps = np.arange(
len(past_values), len(past_values) + len(future_values)
)
if isinstance(past_timesteps, pd.PeriodIndex):
past_timesteps = past_timesteps.to_timestamp()
if isinstance(future_timesteps, pd.PeriodIndex):
future_timesteps = future_timesteps.to_timestamp()
past_covariates = task.past_time.to_numpy()[:, :-1]
future_covariates = task.future_time.to_numpy()[:, :-1]
num_covariates = past_covariates.shape[1]
fig, axes = plt.subplots(
num_covariates + 1, 1, figsize=(18, 12 * num_covariates), sharex=True
)
plt.xticks(fontsize=7)
for k in range(num_covariates + 1):
if k == 0: # Forecast variable
past, future = past_values, future_values
title = "Forecast variable"
else:
past, future = past_covariates[:, k - 1], future_covariates[:, k - 1]
title = f"Covariate {k}"
past_line = axes[k].plot(
past_timesteps,
past,
color=(1, 0, 0),
linewidth=2,
zorder=5,
label="history",
)
future_line = axes[k].plot(
future_timesteps,
future,
color=(0, 0, 1),
linewidth=2,
zorder=5,
label="prediction",
)
axes[k].grid()
axes[k].set_title(title)
if k == 0:
lines = [past_line[0], future_line[0]]
lables = ["history", "prediction"]
# Add x-label to the last subplot
axes[-1].set_xlabel("Time")
fig.legend(lines, lables, loc="upper right")
scenario = "\n".join(textwrap.wrap(task.scenario, width=125))
background = "\n".join(textwrap.wrap(task.background, width=125))
causal_context = "\n".join(textwrap.wrap(task.causal_context, width=125))
# Count number of characters in scenario, background and causal context
background_chars = len(background)
scenario_chars = len(scenario)
causal_context_chars = len(causal_context)
plt.suptitle(
f"Background ({background_chars}): {background}\nCausal context ({causal_context_chars}): {causal_context}\nScenario ({scenario_chars}): {scenario}",
fontsize=16,
) # Change this to task.background or task.constraint depending on task
plt.tight_layout()
plt.savefig(filename)
task = SensorMaintenanceInPredictionTask() # Change this to required task
plot_forecast_univariate(task, f"task_example_plotting.png")