-
Notifications
You must be signed in to change notification settings - Fork 44
/
result_test.py
134 lines (117 loc) · 5.9 KB
/
result_test.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
import torch
from metrics import All_Metrics
import json
import numpy as np
import os
from sklearn.metrics import f1_score, recall_score, precision_score, accuracy_score, classification_report
def test(mode, mae_thresh=None, mape_thresh=0.0):
len_nums = 0
y_pred_in = []
y_true_in = []
y_pred_out = []
y_true_out = []
y_true_in_regionlist = []
y_pred_in_regionlist = []
y_true_out_regionlist = []
y_pred_out_regionlist = []
index_all = 0
# Retrieve all JSON files from a folder and sort them by filename
file_list = sorted([filename for filename in os.listdir(folder_path) if filename.endswith(".json")])
for idx, filename in enumerate(file_list):
file_path = os.path.join(folder_path, filename)
print(file_path)
with open(file_path, "r") as file:
data_t = json.load(file)
for i in range(len(data_t)):
i_data = data_t[i]
y_in = np.array(i_data["y_in"])
y_out = np.array(i_data["y_out"])
st_pre_infolow = np.array(i_data["st_pre_infolow"])
st_pre_outfolow = np.array(i_data["st_pre_outfolow"])
i4data_all = int(data_t[i]["id"].split('_')[6])
if index_all != i4data_all :
len_nums = len_nums + 1
y_true_in_region = np.stack(y_true_in, axis=-1)
y_pred_in_region = np.stack(y_pred_in, axis=-1)
y_true_out_region = np.stack(y_true_out, axis=-1)
y_pred_out_region = np.stack(y_pred_out, axis=-1)
y_true_in_regionlist.append(y_true_in_region)
y_pred_in_regionlist.append(y_pred_in_region)
y_true_out_regionlist.append(y_true_out_region)
y_pred_out_regionlist.append(y_pred_out_region)
y_pred_in = []
y_true_in = []
y_pred_out = []
y_true_out = []
index_all = i4data_all
y_true_in.append(y_in)
y_pred_in.append(st_pre_infolow)
y_true_out.append(y_out)
y_pred_out.append(st_pre_outfolow)
if (i == len(data_t) - 1 and idx == len(file_list) - 1):
y_true_in_region = np.stack(y_true_in, axis=-1)
y_pred_in_region = np.stack(y_pred_in, axis=-1)
y_true_out_region = np.stack(y_true_out, axis=-1)
y_pred_out_region = np.stack(y_pred_out, axis=-1)
y_true_in_regionlist.append(y_true_in_region)
y_pred_in_regionlist.append(y_pred_in_region)
y_true_out_regionlist.append(y_true_out_region)
y_pred_out_regionlist.append(y_pred_out_region)
y_pred_in = []
y_true_in = []
y_pred_out = []
y_true_out = []
print('len_nums', len_nums)
y_true_in = np.stack(y_true_in_regionlist, axis=0)
y_pred_in = np.stack(y_pred_in_regionlist, axis=0)
y_true_out = np.stack(y_true_out_regionlist, axis=0)
y_pred_out = np.stack(y_pred_out_regionlist, axis=0)
y_pred_in, y_pred_out = np.abs(y_pred_in), np.abs(y_pred_out)
print(y_true_in.shape, y_pred_in.shape, y_true_out.shape, y_pred_out.shape)
if mode == 'classification':
test_classfication(y_true_in, y_pred_in, y_true_out, y_pred_out)
else:
for t in range(y_true_in.shape[1]):
mae, rmse, mape, _, _ = All_Metrics(y_pred_in[:, t, ...], y_true_in[:, t, ...], mae_thresh, mape_thresh, None)
print("Horizon {:02d}, MAE: {:.2f}, RMSE: {:.2f}, MAPE: {:.4f}%".format(t + 1, mae, rmse, mape * 100))
mae, rmse, mape, _, _ = All_Metrics(y_pred_in, y_true_in, mae_thresh, mape_thresh, None)
print("Average Horizon, MAE: {:.2f}, RMSE: {:.2f}, MAPE: {:.4f}%".format(mae, rmse, mape * 100))
for t in range(y_true_in.shape[1]):
mae, rmse, mape, _, _ = All_Metrics(y_pred_out[:, t, ...], y_true_out[:, t, ...], mae_thresh, mape_thresh, None)
print("Horizon {:02d}, MAE: {:.2f}, RMSE: {:.2f}, MAPE: {:.4f}%".format(t + 1, mae, rmse, mape * 100))
mae, rmse, mape, _, _ = All_Metrics(y_pred_out, y_true_out, mae_thresh, mape_thresh, None)
print("Average Horizon, MAE: {:.2f}, RMSE: {:.2f}, MAPE: {:.4f}%".format(mae, rmse, mape * 100))
def test_classfication(y_true_in, y_pred_in, y_true_out, y_pred_out):
for i in range(2):
if i == 0:
y_true = y_true_in
y_pred = y_pred_in
else:
y_true = y_true_out
y_pred = y_pred_out
y_true[y_true > 1] = 1
y_pred[y_pred >= 0.5] = 1
y_pred[y_pred < 0.5] = 0
y_true, y_pred = y_true.reshape(-1), y_pred.reshape(-1)
recall = recall_score(y_true, y_pred)
precision = precision_score(y_true, y_pred)
accuracy = accuracy_score(y_true, y_pred)
micro_f1 = f1_score(y_true, y_pred, average='micro')
macro_f1 = f1_score(y_true, y_pred, average='macro')
f1 = f1_score(y_true, y_pred)
print(f"Accuracy: {accuracy:.2f}")
print(f"Precision: {precision:.2f}")
print(f"Recall: {recall:.2f}")
print(f"MicroF1: {micro_f1:.2f}")
print(f"MacroF1: {macro_f1:.2f}")
print(f"f1 Score: {f1:.2f}")
################################ result path ################################
folder_path = 'result_test_file/tw2t_multi_reg-cla_NYC_taxi_final'
# folder_path = 'result_test_file/tw2t_multi_reg-cla_NYC_bike_final'
# 'BURGLARY': 0, 'GRAND LARCENY': 1, 'ROBBERY': 2, 'FELONY ASSAULT': 3
# folder_path = 'result_test_file/tw2t_multi_reg-cla_NYC_crime1_final'
# folder_path = 'result_test_file/tw2t_multi_reg-cla_NYC_crime2_final'
# mode = 'classification' # regression or classification
mode = 'regression'
# Make sure that the total length of your json file(s) a multiple of 80.
test(mode)