-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
281 lines (242 loc) · 9.9 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
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
import os
import json
import numpy as np
from sklearn.metrics import roc_auc_score, average_precision_score
from torch.utils.data import Dataset
from config import DefaultConfig
class TextDataset(Dataset):
def __init__(self, data_name, model_name="gpt"):
self.data_name = data_name
self.model_name = model_name
(self.X, self.y,
self.normal_label_list, self.anomaly_label_list,
self.origianl_task,
self.normal_desc_dict, self.anomaly_desc_dict) = self._process_dataset()
def __len__(self):
return len(self.X)
def __getitem__(self, idx):
return self.X[idx]
def get_labels(self):
return self.y
def get_normal_label_list(self):
return self.normal_label_list
def get_anomaly_label_list(self):
return self.anomaly_label_list
def get_origianl_task(self):
return self.origianl_task
def get_normal_desc_dict(self):
return self.normal_desc_dict
def get_anomaly_desc_dict(self):
return self.anomaly_desc_dict
def get_X(self):
return self.X
def _process_dataset(self):
cur_dir = os.path.dirname(__file__)
data_dir = os.path.join(cur_dir, 'data')
file_path = os.path.join(data_dir, self.data_name,
f"{self.data_name}_test_data.jsonl")
X, y = [], []
normal_label_list, anomaly_label_list = [], []
normal_desc_dict, anomaly_desc_dict = {}, {}
origianl_task = None
print(f"Start reading dataset {self.data_name}...")
with open(file_path, 'r') as file:
for line in file:
try:
data = json.loads(line)
text, label = data['text'], data['label']
if label != 0 and label != 1:
raise ValueError("Invalid label value.")
X.append(text)
y.append(label)
except json.JSONDecodeError:
print(f"!!! Error reading line: {line} in {file_path}")
continue
data_sum_path = os.path.join(data_dir, "data_summary.jsonl")
with open(data_sum_path, 'r') as file:
for line in file:
try:
data = json.loads(line)
if data['name'] == self.data_name:
normal_label_list = data['normal_label_list']
anomaly_label_list = data['anomaly_label_list']
origianl_task = data['origianl_task']
break
except json.JSONDecodeError:
print(f"!!! Error reading line: {line} in {data_sum_path}")
continue
if not DefaultConfig._use_desc:
print("Complete reading data.")
return X, y, normal_label_list, anomaly_label_list, origianl_task, \
normal_desc_dict, anomaly_desc_dict
data_desc_path = os.path.join(data_dir, self.data_name,
f"{self.data_name}_{self.model_name}_desc.json")
with open(data_desc_path, 'r') as file:
try:
data = json.load(file)
# print(data)
except json.JSONDecodeError:
raise ValueError(f"!!! Error reading {data_desc_path}")
# get normal_desc_dict and anomaly_desc_dict
for key, value in data.items():
if key in normal_label_list:
normal_desc_dict[key] = value
elif key in anomaly_label_list:
anomaly_desc_dict[key] = value
else:
raise ValueError(f"!!! Error: {key} not in normal or anomaly label list")
print("Complete reading data.")
return X, y, normal_label_list, anomaly_label_list, origianl_task, \
normal_desc_dict, anomaly_desc_dict
class SynthDataset(Dataset):
def __init__(self, data_name, mode=0, model_name="gpt"):
self.data_name = data_name
self.part_X = self._process_part_dataset()
self.mode = mode
self.model_name = model_name
# mode 0: part dataset
# mode 1: synth + part dataset
# mode 2: synth dataset
# check the mode
if self.mode == 0:
self.X = self.part_X
elif self.mode == 1:
self.synth_X = self._process_synth_dataset()
self.X = self.part_X + self.synth_X
elif self.mode == 2:
self.X = self._process_synth_dataset()
else:
raise ValueError("Invalid mode value.")
def __len__(self):
return len(self.X)
def __getitem__(self, idx):
return self.X[idx]
def _process_part_dataset(self):
cur_dir = os.path.dirname(__file__)
data_dir = os.path.join(cur_dir, 'data')
file_path = os.path.join(data_dir, self.data_name,
f"{self.data_name}_train_part_data.jsonl")
X = []
with open(file_path, 'r') as file:
for line in file:
try:
data = json.loads(line)
text = data['text']
X.append(text)
except json.JSONDecodeError:
print(f"!!! Error reading line: {line} in {file_path}")
continue
return X
def _process_synth_dataset(self):
cur_dir = os.path.dirname(__file__)
data_dir = os.path.join(cur_dir, 'data')
use_desc = ""
if DefaultConfig._use_desc:
use_desc = "_use_desc"
file_path = os.path.join(data_dir, self.data_name,
f"{self.data_name}_{self.model_name}_synth_data{use_desc}.jsonl")
X = []
with open(file_path, 'r') as file:
for line in file:
try:
data = json.loads(line)
text = data['text']
X.append(text)
except json.JSONDecodeError:
print(f"!!! Error reading line: {line} in {file_path}")
continue
return X
def evaluate(y_true, y_score, threshold=0.5):
sample_count = len(y_true)
if not isinstance(y_score, np.ndarray):
y_score = np.array(y_score)
if not isinstance(y_true, np.ndarray):
y_true = np.array(y_true)
# delete error samples
print(f"Error count: {np.sum(y_score == DefaultConfig.error_symbol)}")
error_indecies = np.where(y_score == DefaultConfig.error_symbol)
y_true = np.delete(y_true, error_indecies)
y_score = np.delete(y_score, error_indecies)
tp = np.sum((y_true == 1) & (y_score >= threshold))
fp = np.sum((y_true == 0) & (y_score >= threshold))
tn = np.sum((y_true == 0) & (y_score < threshold))
fn = np.sum((y_true == 1) & (y_score < threshold))
accuracy = (tp + tn) / (tp + tn + fp + fn)
try:
precision = tp / (tp + fp)
except ZeroDivisionError:
print("ZeroDivisionError: precision")
precision = 0
try:
recall = tp / (tp + fn)
except ZeroDivisionError:
print("ZeroDivisionError: recall")
recall = 0
try:
f1 = 2 * precision * recall / (precision + recall)
except ZeroDivisionError:
print("ZeroDivisionError: f1")
f1 = 0
auroc = roc_auc_score(y_true, y_score)
auprc = average_precision_score(y_true, y_score)
print("Evaluation results:")
print(f"AUROC: {auroc}")
print(f"AUPRC: {auprc}")
print("=====================================")
print(f"The following results are calculated based on threshold={threshold}.")
print(f"TP: {tp}, FP: {fp}, TN: {tn}, FN: {fn}")
print(f"Accuracy: {accuracy}")
print(f"Precision: {precision}")
print(f"Recall: {recall}")
print(f"F1: {f1}")
def save_json(generated_json, data_name, suffix):
cur_dir = os.path.dirname(__file__)
data_dir = os.path.join(cur_dir, 'data')
file_path = os.path.join(data_dir, data_name,
f"{data_name}_{suffix}.json")
with open(file_path, 'w') as file:
json.dump(generated_json, file, indent=4)
print(f"Saved the generated JSON to {file_path}")
def read_data_summary(data_name):
cur_dir = os.path.dirname(__file__)
data_dir = os.path.join(cur_dir, 'data')
file_path = os.path.join(data_dir, "data_summary.jsonl")
size = 0
normal_label_list, anomaly_label_list = [], []
origianl_task = None
with open(file_path, 'r') as file:
for line in file:
try:
data = json.loads(line)
if data['name'] == data_name:
size = data['size']
normal_label_list = data['normal_label_list']
anomaly_label_list = data['anomaly_label_list']
origianl_task = data['origianl_task']
break
except json.JSONDecodeError:
print(f"!!! Error reading line: {line} in {file_path}")
continue
return normal_label_list, anomaly_label_list, origianl_task, size
def read_json(data_name, file_name):
cur_dir = os.path.dirname(__file__)
data_dir = os.path.join(cur_dir, 'data')
file_path = os.path.join(data_dir, data_name, file_name)
with open(file_path, 'r') as file:
try:
data = json.load(file)
except json.JSONDecodeError:
raise ValueError(f"!!! Error reading {file_path}")
return data
def read_normal_desc(data_name, model_name, normal_label_list):
file_name = f"{data_name}_{model_name}_desc.json"
data = read_json(data_name, file_name)
normal_desc_dict = {}
# get normal_desc_dict
for key, value in data.items():
if key in normal_label_list:
normal_desc_dict[key] = value
else:
# abnormal label
continue
return normal_desc_dict