-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualise_aus.py
325 lines (274 loc) · 13.1 KB
/
visualise_aus.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
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
from datetime import timedelta
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib
import matplotlib.pyplot as plt
import torch
from dateutil.relativedelta import relativedelta
from matplotlib.cm import get_cmap
from matplotlib.lines import Line2D
from torch.nn.functional import mse_loss
from tqdm import tqdm
from data_provider.data_factory import data_dict
from utils.tools import MAPELoss, calculate_mse, calculate_mape
save_path = 'figures/demand_aus/model_comparison/'
# predlens = [96, 192, 356]
predlens = [1, 12, 72]
# predlens = [1, 12, 24, 36, 48, 60, 72]
batch_size = 16
using_short_horizon_forecasting = False
value_vars_list = ['moment_zs', 'moment',
'moirai_zs', 'moirai',
'lag_llama', 'lag_llama_zs',
# 'ttm_zs', 'ttm',
'chronos_zs', 'chronos',
'lstm', 'conv_lstm', 'gru', 'gru_att',
'true']
vars_name_map = {
'moment': 'MOMENT',
'moment_zs': 'MOMENT_zeroshot',
'moirai': 'MOIRAI',
'moirai_zs': 'MOIRAI_zeroshot',
'lag_llama': 'Lag-Llama',
'lag_llama_zs': 'Lag-Llama_zeroshot',
'ttm': 'TTMs',
'ttm_zs': 'TTMs_zeroshot',
# 'ttm_5shot': 'TTMs_5shot',
'chronos_zs': 'Chronos_zeroshot',
'chronos': 'Chronos',
'lstm': 'LSTM',
'conv_lstm': 'Conv-LSTM',
'gru': 'GRU',
'gru_att': 'GRU-Attention',
'forecast': 'NEM Forecast'
}
plot_lianlian_tasks = False
compare_lp_vs_base_loss = False
def halve_if_duplicated(data):
# halve the data (data processing accidentally concatenated two copies of the same data)
midpoint = len(data) // 2
if data.iloc[:midpoint].equals(data.iloc[midpoint:].reset_index(drop=True)):
data = data.iloc[:midpoint]
data['date'] = pd.to_datetime(data['date'])
return data
'''
Add in additional dataframes here and rename pred columns to match the added name in value_vars_list
'''
def load_data(_pred_len):
csv_folder = 'results/data_aus/'
# load MOMENT predictions
df = pd.read_csv(csv_folder + f'MOMENT_Demand_pl{_pred_len}_base_predictions.csv', index_col=0)
if _pred_len > 12 or not using_short_horizon_forecasting:
post_lp = pd.read_csv(csv_folder + f'MOMENT_Demand_pl{_pred_len}_post-lp_predictions.csv', index_col=0)
df['moment'] = post_lp['pred']
df.rename(columns={
'pred': 'moment_zs'
}, inplace=True)
df = halve_if_duplicated(df)
# load LSTM predictions into df
lstm = pd.read_csv(csv_folder + f'LSTM_Demand_pl{_pred_len}_dm200_predictions.csv', index_col=0)
lstm = halve_if_duplicated(lstm)
df['lstm'] = lstm['pred']
try:
# load MOIRAI predictions into df
moirai = pd.read_csv(csv_folder + f'MOIRAI_AUS_pl{_pred_len}_zero_shot.csv')
moirai = halve_if_duplicated(moirai)
df['moirai_zs_mean'] = moirai['pred_mean']
df['moirai_zs'] = moirai['pred_median']
moirai = pd.read_csv(csv_folder + f'MOIRAI_AUS_pl{_pred_len}_finetuned.csv')
moirai = halve_if_duplicated(moirai)
df['moirai_mean'] = moirai['pred_mean']
df['moirai'] = moirai['pred_median']
except Exception as e:
print(e)
try:
# load Lag-Llama predictions into df
lag_llama = pd.read_csv(csv_folder + f'Lag-Llama_pl{_pred_len}_zero_shot.csv')
lag_llama = halve_if_duplicated(lag_llama)
df['lag_llama_zs'] = lag_llama['pred'] # TODO CHANGE
lag_llama = pd.read_csv(csv_folder + f'Lag-Llama_pl{_pred_len}_finetuned.csv')
lag_llama = halve_if_duplicated(lag_llama)
df['lag_llama'] = lag_llama['pred'] # TODO CHANGE
except Exception as e:
print(e)
# load ConvLSTM predictions into df
conv_lstm = pd.read_csv(csv_folder + f'ConvLSTM_Demand_pl{_pred_len}_dm200_predictions.csv')
conv_lstm = halve_if_duplicated(conv_lstm)
df['conv_lstm'] = conv_lstm['pred']
# load GRU predictions into df
gru = pd.read_csv(csv_folder + f'GRU_Demand_pl{_pred_len}_dm200_predictions.csv')
gru = halve_if_duplicated(gru)
df['gru'] = gru['pred']
# load GRU_Attention predictions into df
gru_att = pd.read_csv(csv_folder + f'GRUAttention_Demand_pl{_pred_len}_dm200_predictions.csv')
gru_att = halve_if_duplicated(gru_att)
df['gru_att'] = gru_att['pred']
# load Chronos predictions into df
if _pred_len <= 60:
chronos = pd.read_csv(csv_folder + f'Chronos_pl{_pred_len}_zero_shot.csv')
df['chronos_zs'] = chronos['pred']
chronos = pd.read_csv(csv_folder + f'Chronos_pl{_pred_len}_finetuned.csv')
df['chronos'] = chronos['pred']
try:
# load TTMs predictions into df
ttm = pd.read_csv(csv_folder + f'TTMs_pl{_pred_len}_zeroshot.csv')
# TTMs has the correct number of windows; not sure why the rest don't -> shorten df to match
df = df.iloc[1:len(ttm) + 1].reset_index()
df['ttm_zs'] = ttm['actual']
# ttm = pd.read_csv(csv_folder + f'TTMs_pl{_pred_len}_fewshot5.csv')
# df['ttm_5shot'] = ttm['actual']
ttm = pd.read_csv(csv_folder + f'TTMs_pl{_pred_len}_fullshot.csv')
df['ttm'] = ttm['actual']
except Exception as e:
print(e)
return df
def plot_multiple(df, title, path_to_save):
columns_to_plot = [col for col in value_vars_list if col != 'true']
# Create a 4x2 grid of subplots
fig, axes = plt.subplots(2, 4, figsize=(20, 8), sharex=True, sharey=True)
fig.suptitle(title, fontsize=20)
# Flatten the axes array for easy iteration
axes = axes.flatten()
# Plot each column
for i, col in enumerate(columns_to_plot):
sns.lineplot(data=df, x='date', y='true', ax=axes[i], label='true')
sns.lineplot(data=df, x='date', y=col, ax=axes[i], label=col, errorbar='pi')
axes[i].set_title(f'{vars_name_map[col]}', fontsize=16)
axes[i].set_xlabel('Date', fontsize=14)
axes[i].set_ylabel('Demand', fontsize=14)
axes[i].tick_params(axis='x', labelrotation=25)
axes[i].legend()
fig.savefig(path_to_save, bbox_inches='tight')
# Adjust layout and show plot
plt.tight_layout()
plt.show()
for pred_len in tqdm(predlens):
df = load_data(pred_len)
raw_data = pd.read_csv('data/demand_data_all_nsw_numerical.csv')
raw_data['date'] = pd.to_datetime(raw_data['datetime'])
if pred_len > 60:
value_vars_list = [col for col in value_vars_list if col != "chronos_zs"]
if plot_lianlian_tasks:
test = df.melt(id_vars='date', value_vars=value_vars_list, var_name='ts', value_name='demand')
fig = plt.figure(figsize=(50, 15))
sns.lineplot(data=test, x='date', y='demand', hue='ts', errorbar='pi')
plt.title(f'Demand Plot (Multiple iterations, Horizon {pred_len})', fontsize=40)
plt.xlabel('Date', fontsize=36)
plt.ylabel('Demand', fontsize=36)
plt.legend(title='', fontsize=28)
plt.xticks(fontsize=28)
plt.yticks(fontsize=28)
fig.savefig(save_path + f'Demand_pl{pred_len}_predictions_all.png', bbox_inches='tight')
day_indexes = []
for i in range(7):
days = raw_data.iloc[-int(len(raw_data) * 0.2 + pred_len):-pred_len][(raw_data['hour'] == 0) & (raw_data['day_of_week'] == i)].sample(n=1)
day_indexes.extend(list(days.index))
# day_indexes = [58200, 56544, 49848, 54072, 50904, 60168, 50616] # indexes are sampled from a previous run
days_chosen = raw_data.iloc[day_indexes]
for i, (index, day_) in enumerate(days_chosen.iterrows()):
start_date = day_['date']
end_date = start_date + timedelta(days=1)
sampled_day = df.query(f"'{start_date}' <= date <= '{end_date}'")
# print(sampled_day['date'].max())
day_of_week_sampled = day_['day_of_week']
plot_multiple(sampled_day,
f'Day #{i + 1} - {day_of_week_sampled} (Horizon {pred_len})',
save_path + f'Demand_pl{pred_len}_predictions_day{i + 1}.png')
monday_indexes = []
mondays_temp = raw_data.iloc[-int(len(raw_data) * 0.2 + pred_len):-pred_len][(raw_data['hour'] == 0) & (raw_data['day_of_week'] == 0)].sample(n=6)
monday_indexes.extend(list(mondays_temp.index))
# monday_indexes = [55680, 56016, 56520, 52824, 58200, 53496]
monday_indexes = sorted(monday_indexes)
mondays = raw_data.iloc[monday_indexes]
for i, (index, monday) in enumerate(mondays.iterrows()):
start_date = monday['date']
end_date = start_date + timedelta(days=7)
sampled_week = df.query(f"'{start_date}' <= date <= '{end_date}'")
# print(sampled_week['date'].max())
plot_multiple(sampled_week,
f'Week #{i + 1} (Horizon {pred_len})',
save_path + f'Demand_pl{pred_len}_predictions_week{i + 1}.png')
month_indexes = []
months_temp = raw_data.iloc[-int(len(raw_data) * 0.2 + pred_len):-pred_len][(raw_data['hour'] == 0) & (raw_data['day'] == 1)].sample(n=3)
month_indexes.extend(list(months_temp.index))
# month_indexes = [54768, 58440, 59904]
month_indexes = sorted(month_indexes)
months = raw_data.iloc[month_indexes]
for i, (index, monthday) in enumerate(months.iterrows()):
start_date = monthday['date']
end_date = start_date + relativedelta(months=1)
sampled_month = df.query(f"'{start_date}' <= date <= '{end_date}'")
# print(sampled_month['date'].max())
plot_multiple(sampled_month,
f'Month #{i + 1} (Horizon {pred_len})',
save_path + f'Demand_pl{pred_len}_predictions_month{i + 1}.png')
# continue
# if pred_len > 1:
# # single iter of length pred_len (second pred_len horizon)
# single = df[pred_len ** 2:pred_len ** 2 + pred_len]
# min_date = single['date'].min()
# max_date = single['date'].max()
#
# # multiple iters of the second pred_len horizon
# multiple = df.query(f"'{min_date}' <= date <= '{max_date}'")
# multiple = multiple.melt(id_vars='date', value_vars=value_vars_list, var_name='ts', value_name='demand')
# fig = plt.figure(figsize=(12,8))
# sns.lineplot(data=multiple, x='date', y='demand', hue='ts', errorbar='pi') # use 'sd' or 'pi'
# plt.title(f'Demand Plot (Multiple iterations, Horizon {pred_len})')
# plt.xlabel('Date')
# plt.ylabel('Demand')
# plt.legend(title='')
# fig.savefig(save_path + f'MOMENT_Demand_pl{pred_len}_predictions_all-iter_pi.png', bbox_inches='tight')
# plt.show()
# else:
# # replace 'moment_lp_pred' with 'moment_pred' if using short-horizon-forecasting
# if using_short_horizon_forecasting:
# value_vars_list = value_vars_list[1:]
# value_vars_list.insert(0, 'moment_pred')
#
# # when pred_len <= 12, use split-iters with a fixed window size of 12
# df['window'] = df.index // pred_len
# min_date = df.iloc[12]['date']
# max_date = df.iloc[12 * 2 - 1]['date']
# split = df.query(f"'{min_date}' <= date <= '{max_date}'")
# split = split.melt(id_vars=['date', 'window'], value_vars=value_vars_list, var_name='ts', value_name='demand')
# fig = plt.figure(figsize=(12,8))
# sns.lineplot(data=split, x='date', y='demand', hue='ts', markers=True, dashes=False)
# plt.title(f'Demand Plot (Multiple iterations, Horizon {pred_len})')
# plt.xlabel('Date')
# plt.ylabel('Demand')
#
# cmap = get_cmap('tab10')
# custom_lines = []
# for i, val in enumerate(value_vars_list):
# custom_lines.append(Line2D([0], [0], color=cmap(i), lw=1))
#
# plt.legend(custom_lines, value_vars_list)
# fig.savefig(save_path + f'MOMENT_Demand_pl{pred_len}_predictions_split.png', bbox_inches='tight')
# plt.show()
# calculating losses
cols_to_process = [col for col in value_vars_list if col != "true"]
if pred_len <= 12 and using_short_horizon_forecasting:
cols_to_process = [col for col in cols_to_process if col != "moment"]
if compare_lp_vs_base_loss:
cols_to_process = ['moment_zs', 'moment']
save_path = 'figures/demand/lp_vs_base_loss/'
# export average predictions
avg_data_dict = {'mse': {}, 'mape': {}}
for col in cols_to_process:
mse = calculate_mse(df[col].values, df['true'].values)
mape = calculate_mape(df[col].values, df['true'].values)
avg_data_dict['mse'][col] = mse
avg_data_dict['mape'][col] = mape
pd.DataFrame(avg_data_dict).to_csv(save_path + f'pl{pred_len}_average_losses_comparison.csv')
dfs = []
for pred_len in predlens:
dfs.append(pd.read_csv(save_path + f'pl{pred_len}_average_losses_comparison.csv', index_col=0)
.add_prefix(f'pl{pred_len}_'))
combined_losses = pd.concat(dfs, axis=1)
# Reorder the columns
mape_columns = [col for col in combined_losses.columns if 'mape' in col.lower()]
mse_columns = [col for col in combined_losses.columns if 'mse' in col.lower()]
combined_losses = combined_losses[mape_columns + mse_columns]
combined_losses.to_csv(save_path + 'losses_combined.csv')