-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresults_plots.py
147 lines (116 loc) · 4.56 KB
/
results_plots.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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import os
import data
from utils import read_json
INPUT_PATH = 'data/'
METRICS = ['mape', 'vape']
def read_target(df, station, aggregation):
data = df[station]
if aggregation == 'day':
forecast_window = 7
elif aggregation == 'hour':
forecast_window = 24
elif aggregation == 'month':
forecast_window = 12
elif aggregation == '15mins':
forecast_window = 8
else:
raise KeyError ('aggregation parameter is one of [day, hour, month, 15 mins]')
shift_list = []
col_list = []
for i in range(forecast_window):
shift_list.append(data.shift(-i))
col_list.append('forecast_period_' + str(i + 1))
target = pd.concat(shift_list, axis = 1)
target.columns = col_list
target = np.array(target.dropna())
assert target.shape[1] == forecast_window
return target
def read_prediction(model, aggregation, station):
path = os.path.join('output', model, 'results', aggregation , station + '.json' )
prediction = read_json(path)
prediction = np.array(prediction['prediction'])
return np.expm1(prediction)
def APE(target, predicted):
return np.abs((target - predicted) / target)
def mape(target, predicted, axis = None):
return np.mean(APE(target, predicted), axis = axis)
def maape(target, predicted, axis = None):
ape = APE(target, predicted)
return np.mean(np.arctan(ape), axis = axis)
def vape(target, predicted, axis = None):
return np.var(APE(target, predicted), axis = axis)
def plot_before_after(pre, post, metric_name):
fig, axs = plt.subplots(nrows=1, ncols=1, figsize = (10,10))
axs.scatter(pre, post, color="g")
axs.plot([0, 1], [0, 1])
axs.set_ylabel('Post COVID-19 \n [Jun 2019 - Feb 2020]')
axs.set_xlabel('Pre COVID-19 \n [Jun 2019 - Feb 2020]')
axs.set_title(metric_name, fontsize = 50)
return None
def plot_evolution(metrics, metric_name):
plt.plot(x = 'Date', y = 'MAPE')
plt.ylabel(metric_name)
plt.title('Evolution of ' + metric_name)
plt.axvline(578, color = 'r')
return None
def run_regression(pre, post, metric_name):
for metric in METRICS:
pre_name = 'pre_' + metric
post_name = 'post_' + metric
x = sm.add_constant(results[pre_name].rename('pre-COVID 19 metric'), prepend=False)
return regression
## FIX ME
def linear_regression(results, metrics):
models_list = []
for metric in metrics:
pre_name = 'pre_' + metric
post_name = 'post_' + metric
x = sm.add_constant(results[pre_name].rename('pre-COVID 19 metric'), prepend=False)
model = sm.OLS(results[post_name], x).fit()
models_list.append(model)
ols_results = summary_col(models_list,stars=True, info_dict = {"N":lambda x:(x.nobs)},
model_names = metrics, float_format='%.3f')
return ols_results
def results(model, aggregation, metric):
#read data
train, test = data.split_data(INPUT_PATH, train_date = (2018, 8, 1), aggreagation = aggregation)
stations = set(train.columns[train.columns.str.contains("\(")])
#For each station
w_metric = []
s_pre_metric = []
s_post_metric = []
for station in stations:
target = read_target(test, station, aggregation)
prediction = read_prediction(station)
## FIX ME: Check 572 is the actual split of the data
pre_target = target[:578,:]
pre_prediction = prediction[:578,:]
post_target = target[578:,:]
post_prediction = prediction[578:,:]
# METRICS
window_metric = metric(target, prediction, axis = 1)
pre_station_metric = metric(pre_target, pre_prediction)
post_station_metric = metric(post_target, post_prediction)
w_metric.append(window_metric)
s_pre_metric.append(pre_station_metric)
s_post_metric.append(post_station_metric)
plot_before_after(s_pre_metric, s_post_metric)
plot_evolution(w_metric)
regression = run_regression(s_pre_metric, s_post_metric)
return regression
## -------------------------- tests ------------------------------------
model = 'arima'
aggregation = 'day'
metric = 'mape'
station = "(02000) cabecera autopista norte"
train, test = data.split_data(INPUT_PATH, train_date = (2018, 8, 1), aggreagation = aggregation)
target = read_target(test, station, aggregation = aggregation)[:-1,:]
print(target.shape)
prediction = read_prediction(model, aggregation, station)[:target.shape[0],:]
print(prediction)
# m = maape(target, prediction)
# print (m)