forked from Nixtla/neuralforecast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.py
177 lines (145 loc) · 4.81 KB
/
info.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
import os
import re
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import expon
def load(fn):
return np.load(fn, allow_pickle=True).item()
def get_ys(d):
yt = d["y_true"]
yh = d["y_hat"]
return yt, yh
def calc_mae(d, ax=(0, 1, 2, 3)):
yt, yh = get_ys(d)
mae = np.abs(yt - yh)
if len(ax) > 0:
mae = mae.mean(axis=ax)
return mae
def calc_smape(d, ax=(0, 1, 2, 3)):
yt, yh = get_ys(d)
t = np.prod([yt.shape[i] for i in ax])
smape = (2 / t) * np.sum(np.abs(yt - yh) / (np.abs(yt) + np.abs(yh)), axis=ax)
return smape
def plot_dist(ds):
ax1 = plt.figure().add_subplot()
ax2 = plt.figure().add_subplot()
for d in ds:
err = calc_smape(d, ax=(2, 3))
for s in range(err.shape[0]):
# mu, std = expon.fit(err)
ax1.hist(err[s], bins=100, alpha=0.6, label=d["series"][s])
# xmin, xmax = ax1.get_xlim()
# x = np.linspace(xmin, xmax, 100)
# p = expon.pdf(x, mu, std)
# ax1.plot(x, p, "k", linewidth=2)
ax2.scatter(np.arange(len(err[s])), err[s], s=0.5, label=d["series"][s])
ax1.set_xlabel("sMAPE")
ax1.set_ylabel("# windows")
ax1.legend()
ax2.set_xlabel("window index")
ax2.set_ylabel("sMAPE")
ax2.legend()
ax1.set_title(
", ".join([", ".join(d["series"]) for d in ds]) + "-- error distribution"
)
ax2.set_title("Error by window")
plt.show()
plt.close()
def plot_summary(ds):
names = []
errs = []
for d in ds:
names.append(d["series"][0])
errs.append(calc_smape(d))
plt.bar(names, errs)
plt.xticks(rotation=90)
plt.tight_layout()
plt.show()
plt.close()
def plot_summary_menu(available):
inp = input("plot all or select? [a|s]: ")
if inp == "a":
plot_summary([load(fn) for name, fn in available])
else:
plot_choices_menu(available, plot_summary)
def plot_3d(d, sidx, widx):
ax = plt.figure().add_subplot(projection="3d")
ax.plot(*d["y_true"][sidx, widx].T, label="y_true")
ax.plot(*d["y_hat"][sidx, widx].T, label="y_hat")
plt.show()
plt.close()
def print_hparams(d):
print(d["series"][0])
print(f'\tlearning rate: {d["config"]["learning_rate"]}')
print(f'\tkernel size: {d["config"]["n_pool_kernel_size"]}')
print(f'\tdownsample: {d["config"]["n_freq_downsample"]}')
print(f'\tmlp units: {d["config"]["mlp_units"]}')
def collect_available(pattern, dirname):
available = []
for fn in sorted(os.listdir(dirname)):
m = re.match(pattern, fn)
if m != None:
available.append((m.group(1), f"{dirname}/{fn}"))
return available
def plot_choices_menu(available, plot_fn):
choices = []
while True:
inp = input("idx to add, p to plot, q to quit: ")
if inp == "q":
break
elif inp == "p":
plot_fn([load(available[ch][1]) for ch in choices])
choices = []
else:
try:
choices.append(int(inp))
except Exception as e:
print(e)
def plot_dist_menu(available):
plot_choices_menu(available, plot_dist)
def plot_3d_menu(available):
while True:
try:
sidx = input("series idx to plot, q to quit: ")
if sidx == "q":
break
else:
try:
d = load(available[int(sidx)][1])
nser = d["y_true"].shape[0]
nwin = d["y_true"].shape[1]
while True:
resp = input(
f"series and window to plot [0, {nser-1}].[0, {nwin-1}] (q to quit): "
)
if resp == "q":
break
else:
sidx, widx = resp.split(".")
plot_3d(d, int(sidx), int(widx))
except Exception as e:
print(e)
except Exception as e:
print(e)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument("opt", choices=["dist", "smape", "3d"], help="plot type")
parser.add_argument(
"--pattern", default="(.*).npy", help="re for matching npy files"
)
parser.add_argument(
"--dirname", default="predictions", help="prediction data directory"
)
args = parser.parse_args()
available = collect_available(args.pattern, args.dirname)
for i, (name, fn) in enumerate(available):
print(f"{i}: {name}")
if args.opt == "dist":
plot_dist_menu(available)
elif args.opt == "3d":
plot_3d_menu(available)
elif args.opt == "smape":
plot_summary_menu(available)