-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_process.py
More file actions
339 lines (268 loc) · 16.8 KB
/
data_process.py
File metadata and controls
339 lines (268 loc) · 16.8 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
from typing import Tuple
import pandas as pd
import pickle
from sklearn.model_selection import train_test_split
import os
# for classification approach
def data_process_class(train_path: str, test_path: str, save_path: str, seed: int = 42, val_ratio: float = 0.1, abnormal_ratio: float = 0.1, train_size = None, test_size = None, normal_only: bool = False) -> Tuple[str, str, str]:
with open(train_path, "rb") as f:
train_data = pickle.load(f)
train_df = pd.DataFrame(train_data)
# Check if 'label' column contains lists
if train_df['Label'].apply(isinstance, args=(list,)).all():
# Find the max of each list in the 'label' column
train_df['Label'] = train_df['Label'].apply(max)
# Print label
print(f"Labels: {train_df['Label'].unique()}")
if normal_only:
train_df = train_df[train_df['Label'] == 0].reset_index(drop=True)
# Filter out only EventTemplate and Label columns
train_df = train_df[['EventTemplate', 'Label']]
# Rename Label to label
train_df.rename(columns={'Label': 'label'}, inplace=True)
# Make EventTemplate from list to string and convert using vocab
# train_df['EventTemplate'] = train_df['EventTemplate'].apply(lambda x: " ".join([str(vocab.get_event(i)) for i in x]))
train_df['EventTemplate'] = train_df['EventTemplate'].apply(lambda x: " ".join(x))
if train_size is not None:
# Collect all abnomal data
abnormal_data = train_df[train_df['label'] != 0]
# Print
print(f"Abnormal data: {abnormal_data.shape[0]}")
# Collect all normal data
normal_data = train_df[train_df['label'] == 0]
# Print
print(f"Normal data: {normal_data.shape[0]}")
# Check if number of abnormal data is more than 10% of the train_size, if is, then sample only % of the train_size
abnormal_size = int(train_size*val_ratio*abnormal_ratio + train_size*(1-val_ratio)*abnormal_ratio)
if abnormal_data.shape[0] > abnormal_size:
abnormal_data = abnormal_data.sample(n=abnormal_size, random_state=seed)
abnormal_data_sampled = abnormal_data.shape[0]
print(f"Abnormal data sampled: {abnormal_data_sampled}")
# Calculate the number of train_normal_data and train_abnormal_data, val_normal_data and val_abnormal_data and print
train_abnormal_data = int(train_size*(1-val_ratio)*abnormal_ratio)
train_normal_data = int(train_size * (1-val_ratio) - train_abnormal_data)
val_abnormal_data = abnormal_data_sampled - train_abnormal_data
val_normal_data = int(train_size * val_ratio - val_abnormal_data)
print(f"Train normal data: {train_normal_data}")
print(f"Train abnormal data: {train_abnormal_data}")
print(f"Val normal data: {val_normal_data}")
print(f"Val abnormal data: {val_abnormal_data}")
split_normal_ratio = val_normal_data / (train_normal_data + val_normal_data)
split_abnormal_ratio = val_abnormal_data / (val_abnormal_data + train_abnormal_data)
normal_data = normal_data.sample(n=train_normal_data + val_normal_data, random_state=seed)
# Split train and val for normal and abnormal data
normal_train_df, normal_val_df = train_test_split(normal_data, test_size=split_normal_ratio, random_state=seed)
abnormal_train_df, abnormal_val_df = train_test_split(abnormal_data, test_size=split_abnormal_ratio, random_state=seed)
# Concat normal and abnormal data for train and val
for_train_df = pd.concat([normal_train_df, abnormal_train_df], ignore_index=True)
val_df = pd.concat([normal_val_df, abnormal_val_df], ignore_index=True)
else:
# Normal
normal_data = train_df[train_df['label'] == 0]
# Abnormal
abnormal_data = train_df[train_df['label'] != 0]
# Print
print(f"Train Normal data: {normal_data.shape[0]}")
print(f"Train Abnormal data: {abnormal_data.shape[0]}")
print(f"Total Train data: {train_df.shape[0]}")
# Split
normal_train_df, normal_val_df = train_test_split(normal_data, test_size=val_ratio, random_state=seed)
abnormal_train_df, abnormal_val_df = train_test_split(abnormal_data, test_size=val_ratio, random_state=seed)
# Concat normal and abnormal data for train and val
for_train_df = pd.concat([normal_train_df, abnormal_train_df], ignore_index=True)
val_df = pd.concat([normal_val_df, abnormal_val_df], ignore_index=True)
# Calculate ratio of val_df to total df
calculated_val_ratio = val_df.shape[0] / (for_train_df.shape[0] + val_df.shape[0])
print(f"Calculated val ratio: {calculated_val_ratio}")
# Make label from int to list
for_train_df['label'] = for_train_df['label'].apply(lambda x: [x])
val_df['label'] = val_df['label'].apply(lambda x: [x])
# Print size
print(f"Train data: {for_train_df.shape[0]}")
print(f"Val data: {val_df.shape[0]}")
# Save as .json file
final_train_save_path = save_path + '/' + train_path.split('/')[-2] + '_train.json'
final_val_save_path = save_path + '/' + train_path.split('/')[-2] + '_val.json'
# Make dir if none
if not os.path.exists(save_path):
os.makedirs(save_path)
for_train_df.to_json(final_train_save_path, orient='records', lines=True)
val_df.to_json(final_val_save_path, orient='records', lines=True)
with open(test_path, "rb") as f:
test_data = pickle.load(f)
test_df = pd.DataFrame(test_data)
# Check if 'label' column contains lists
if test_df['Label'].apply(isinstance, args=(list,)).all():
# Find the max of each list in the 'label' column
test_df['Label'] = test_df['Label'].apply(max)
# Filter out only EventTemplate and Label columns
test_df = test_df[['EventTemplate', 'Label']]
# Rename Label to label
test_df.rename(columns={'Label': 'label'}, inplace=True)
# Make EventTemplate from list to string and convert using vocab
# test_df['EventTemplate'] = test_df['EventTemplate'].apply(lambda x: " ".join([str(vocab.get_event(i)) for i in x]))
test_df['EventTemplate'] = test_df['EventTemplate'].apply(lambda x: " ".join(x))
if test_size is not None:
# Collect all abnomal data
abnormal_data = test_df[test_df['label'] != 0]
# Print
print(f"Abnormal data: {abnormal_data.shape[0]}")
# Collect all normal data
normal_data = test_df[test_df['label'] == 0]
# Print
print(f"Normal data: {normal_data.shape[0]}")
# Sample normal data
normal_data = normal_data.sample(n=test_size, random_state=seed)
# Check if number of abnormal data is more than 10% of the test_size, if is, then sample only 10% of the test_size
if abnormal_data.shape[0] > test_size * abnormal_ratio:
abnormal_data = abnormal_data.sample(n=int(test_size * abnormal_ratio), random_state=seed)
abnormal_data_sampled = abnormal_data.shape[0]
print(f"Abnormal data sampled: {abnormal_data_sampled}")
# Calculate the number of test_normal_data and test_abnormal_data and print
test_abnormal_data = int(abnormal_data_sampled)
test_normal_data = int(test_size - test_abnormal_data)
print(f"Test normal data: {test_normal_data}")
print(f"Test abnormal data: {test_abnormal_data}")
# Sample and concat for test
test_df = pd.concat([normal_data.sample(n=int(test_normal_data), random_state=seed), abnormal_data.sample(n=int(test_abnormal_data), random_state=seed)], ignore_index=True)
# Print normal and abnormal len
print(f"Test Normal data: {test_df[test_df['label'] == 0].shape[0]}")
print(f"Test Abnormal data: {test_df[test_df['label'] != 0].shape[0]}")
# Make label from int to list
test_df['label'] = test_df['label'].apply(lambda x: [x])
# Print size
print(f"Test data: {test_df.shape[0]}")
# Make dir if none
if not os.path.exists(save_path):
os.makedirs(save_path)
# Save as .json file
final_test_save_path = save_path + '/' + test_path.split('/')[-2] + '_test.json'
test_df.to_json(final_test_save_path, orient='records', lines=True)
return final_train_save_path, final_val_save_path, final_test_save_path
# for generative approach
def data_process_gen(train_path: str, test_path: str, save_path: str, seed: int = 42, val_ratio: float = 0.1, abnormal_ratio: float = 0.5, train_size = None, test_size = None, normal_only: bool = False) -> Tuple[str, str, str]:
with open(train_path, "rb") as f:
train_data = pickle.load(f)
train_df = pd.DataFrame(train_data)
# Check if 'label' column contains lists
if train_df['Label'].apply(isinstance, args=(list,)).all():
# Find the max of each list in the 'label' column
train_df['Label'] = train_df['Label'].apply(max)
if normal_only:
train_df = train_df[train_df['Label'] == 0].reset_index(drop=True)
# Filter out only EventTemplate and Label columns
train_df = train_df[['EventTemplate', 'Label']]
# Rename Label to label
train_df.rename(columns={'Label': 'answer'}, inplace=True)
# Make EventTemplate from list to string
train_df['EventTemplate'] = train_df['EventTemplate'].apply(lambda x: ' '.join(["[L] " + i for i in x]))
if train_size is not None:
# Collect all abnomal data
abnormal_data = train_df[train_df['answer'] != 0]
# Print
print(f"Abnormal data: {abnormal_data.shape[0]}")
# Collect all normal data
normal_data = train_df[train_df['answer'] == 0]
# Print
print(f"Normal data: {normal_data.shape[0]}")
# Check if number of abnormal data is more than 10% of the train_size, if is, then sample only 10% of the train_size
abnormal_size = int( train_size*val_ratio*abnormal_ratio + train_size*(1-val_ratio)*abnormal_ratio)
if abnormal_data.shape[0] > abnormal_size:
abnormal_data = abnormal_data.sample(n=abnormal_size, random_state=seed)
abnormal_data_sampled = abnormal_data.shape[0]
print(f"Abnormal data sampled: {abnormal_data_sampled}")
# Calculate the number of train_normal_data and train_abnormal_data, val_normal_data and val_abnormal_data and print
train_abnormal_data = int(train_size*(1-val_ratio)*abnormal_ratio)
train_normal_data = int(train_size * (1-val_ratio) - train_abnormal_data)
val_abnormal_data = 1 - train_abnormal_data
val_normal_data = int(train_size * val_ratio - val_abnormal_data)
print(f"Train normal data: {train_normal_data}")
print(f"Train abnormal data: {train_abnormal_data}")
print(f"Val normal data: {val_normal_data}")
print(f"Val abnormal data: {val_abnormal_data}")
split_normal_ratio = val_normal_data / (train_normal_data + val_normal_data)
split_abnormal_ratio = val_abnormal_data / (val_abnormal_data + train_abnormal_data)
normal_data = normal_data.sample(n=train_normal_data + val_normal_data, random_state=seed)
# Split train and val for normal and abnormal data
normal_train_df, normal_val_df = train_test_split(normal_data, test_size=split_normal_ratio, random_state=seed)
abnormal_train_df, abnormal_val_df = train_test_split(abnormal_data, test_size=split_abnormal_ratio, random_state=seed)
# Concat normal and abnormal data for train and val
for_train_df = pd.concat([normal_train_df, abnormal_train_df], ignore_index=True)
val_df = pd.concat([normal_val_df, abnormal_val_df], ignore_index=True)
else:
# Normal
normal_data = train_df[train_df['answer'] == 0]
# Abnormal
abnormal_data = train_df[train_df['answer'] != 0]
# Print
print(f"Train Normal data: {normal_data.shape[0]}")
print(f"Train Abnormal data: {abnormal_data.shape[0]}")
print(f"Total Train data: {train_df.shape[0]}")
# Split
normal_train_df, normal_val_df = train_test_split(normal_data, test_size=val_ratio, random_state=seed)
abnormal_train_df, abnormal_val_df = train_test_split(abnormal_data, test_size=val_ratio, random_state=seed)
# Concat normal and abnormal data for train and val
for_train_df = pd.concat([normal_train_df, abnormal_train_df], ignore_index=True)
val_df = pd.concat([normal_val_df, abnormal_val_df], ignore_index=True)
# Calculate ratio of val_df to total df
calculated_val_ratio = val_df.shape[0] / (for_train_df.shape[0] + val_df.shape[0])
print(f"Calculated val ratio: {calculated_val_ratio}")
# Make label from int to list
for_train_df['answer'] = for_train_df['answer'].apply(lambda x: "A1" if x == 0 else "B1")
val_df['answer'] = val_df['answer'].apply(lambda x: "A1" if x == 0 else "B1")
# Make dir if none
if not os.path.exists(save_path):
os.makedirs(save_path)
# Save as .json file
final_train_save_path = save_path + '/' + train_path.split('/')[-2] + '_train.json'
final_val_save_path = save_path + '/' + train_path.split('/')[-2] + '_val.json'
for_train_df.to_json(final_train_save_path, orient='records', lines=True)
val_df.to_json(final_val_save_path, orient='records', lines=True)
with open(test_path, "rb") as f:
test_data = pickle.load(f)
test_df = pd.DataFrame(test_data)
# Check if 'label' column contains lists
if test_df['Label'].apply(isinstance, args=(list,)).all():
# Find the max of each list in the 'label' column
test_df['Label'] = test_df['Label'].apply(max)
# Filter out only EventTemplate and Label columns
test_df = test_df[['EventTemplate', 'Label']]
# Rename Label to label
test_df.rename(columns={'Label': 'answer'}, inplace=True)
# Make EventTemplate from list to string
test_df['EventTemplate'] = test_df['EventTemplate'].apply(lambda x: ' '.join(["[L] " + i for i in x]))
if test_size is not None:
# Collect all abnomal data
abnormal_data = test_df[test_df['answer'] != 0]
# Print
print(f"Abnormal data: {abnormal_data.shape[0]}")
# Collect all normal data
normal_data = test_df[test_df['answer'] == 0]
# Print
print(f"Normal data: {normal_data.shape[0]}")
# Sample normal data
normal_data = normal_data.sample(n=test_size, random_state=seed)
# Check if number of abnormal data is more than 10% of the test_size, if is, then sample only 10% of the test_size
if abnormal_data.shape[0] > test_size * abnormal_ratio:
abnormal_data = abnormal_data.sample(n=int(test_size * abnormal_ratio), random_state=seed)
abnormal_data_sampled = abnormal_data.shape[0]
print(f"Abnormal data sampled: {abnormal_data_sampled}")
# Calculate the number of test_normal_data and test_abnormal_data and print
test_abnormal_data = int(abnormal_data_sampled)
test_normal_data = int(test_size - test_abnormal_data)
print(f"Test normal data: {test_normal_data}")
print(f"Test abnormal data: {test_abnormal_data}")
# Sample and concat for test
test_df = pd.concat([normal_data.sample(n=int(test_normal_data), random_state=seed), abnormal_data.sample(n=int(test_abnormal_data), random_state=seed)], ignore_index=True)
# Print normal and abnormal len
print(f"Test Normal data: {test_df[test_df['answer'] == 0].shape[0]}")
print(f"Test Abnormal data: {test_df[test_df['answer'] != 0].shape[0]}")
# Make label from int to list
test_df['answer'] = test_df['answer'].apply(lambda x: "A1" if x == 0 else "B1")
# Make dir if none
if not os.path.exists(save_path):
os.makedirs(save_path)
# Save as .json file
final_test_save_path = save_path + '/' + test_path.split('/')[-2] + '_test.json'
test_df.to_json(final_test_save_path, orient='records', lines=True)
return final_train_save_path, final_val_save_path, final_test_save_path