-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
59 lines (41 loc) · 1.75 KB
/
utils.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
import matplotlib.pyplot as plt
import numpy as np
import h5py
import seaborn as sns
from sklearn.metrics import confusion_matrix
class Evaluation(object):
def __init__(self):
pass
@staticmethod
def mae_(target, output):
return np.mean(np.abs(target - output))
@staticmethod
def mape_(target, output):
return np.mean(np.abs(target - output) / (target + 0.5))
@staticmethod
def rmse_(target, output):
return np.sqrt(np.mean(np.power(target - output, 2)))
@staticmethod
def total(target, output):
mae = Evaluation.mae_(target, output)
mape = Evaluation.mape_(target, output)
rmse = Evaluation.rmse_(target, output)
return mae, mape, rmse
def visualize_result(h5_file, nodes_id, time_se, visualize_file):
file_obj = h5py.File(h5_file, "r")
prediction = file_obj["predict"][:][:, :, 0] # [N, T]
target = file_obj["target"][:][:, :, 0] # [N, T]
file_obj.close()
plot_prediction = prediction[nodes_id][time_se[0]: time_se[1]] # [T1]
plot_target = target[nodes_id][time_se[0]: time_se[1]] # [T1]
plt.figure()
plt.grid(True, linestyle="-.", linewidth=0.2)
plt.plot(np.array([t for t in range(time_se[1] - time_se[0])]), plot_prediction, ls="-", marker=" ", color="r")
plt.plot(np.array([t for t in range(time_se[1] - time_se[0])]), plot_target, ls="-", marker=" ", color="b")
plt.legend(["prediction", "target"], loc="upper right")
plt.axis([0, time_se[1] - time_se[0],
np.min(np.array([np.min(plot_prediction), np.min(plot_target)])),
np.max(np.array([np.max(plot_prediction), np.max(plot_target)]))])
plt.xlabel('Time')
plt.ylabel('Traffic Flow ')
plt.savefig(visualize_file + ".png")