forked from DSBA-Lab/RAPID
-
Notifications
You must be signed in to change notification settings - Fork 0
/
split_data.py
167 lines (139 loc) · 7.12 KB
/
split_data.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
import sys
import os
import pandas as pd
import numpy as np
import argparse
from tqdm import tqdm
import json
import re
from collections import defaultdict
import random
from sklearn.model_selection import train_test_split
import parmap
import multiprocessing
num_processors = multiprocessing.cpu_count()
from utils import str2bool, set_seed, get_parsed_log, get_unique_log, label_parsed_log
def save_processed_log(data, path, need_newline=False):
if not need_newline:
with open(path, 'w') as f:
for log in data:
f.write(log)
else:
with open(path, 'w') as f:
for log in data:
f.write(log)
f.write('\n')
if __name__ == '__main__':
set_seed(1234)
parser = argparse.ArgumentParser()
parser.add_argument("--dataset", help=["hdfs", "bgl", "tbird"], default="tbird")
parser.add_argument("--shuffle", help="shuffle data", default=True, type=str2bool)
parser.add_argument("--sample", help=[0.1, 0.05], default=1, type=lambda x: int(x) if x.isdigit() else float(x))
parser.add_argument("--test_size", help="test_size", default=0.2, type=float)
args = parser.parse_args()
current_dir = os.path.dirname(os.path.abspath(__file__))
if args.dataset == "bgl":
data_dir = os.path.join(current_dir, 'dataset', 'bgl')
log_file = "BGL.log"
output_dir = os.path.join(current_dir, 'processed_data', f'bgl')
elif args.dataset == "tbird":
data_dir = os.path.join(current_dir, 'dataset', 'tbird')
log_file = "Thunderbird.log"
if args.sample != 1:
output_dir = os.path.join(current_dir, 'processed_data', f'tbird_sample_{str(args.sample)}')
else:
output_dir = os.path.join(current_dir, 'processed_data', f'tbird')
elif args.dataset == "hdfs":
# we don't split hdfs dataset with this code
data_dir = os.path.join(current_dir, 'dataset', 'hdfs')
output_dir = os.path.join(current_dir, 'processed_data', f'hdfs')
log_file = "HDFS.log"
blk_label_file = os.path.join(data_dir,"anomaly_label.csv")
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# load dataset and get normal & abnormal
if os.path.exists(os.path.join(output_dir, f'train_{args.test_size}')) and os.path.exists(os.path.join(output_dir, f'test_{args.test_size}')):
print("Already split dataset")
sys.exit()
print("Split dataset")
if args.dataset != "hdfs":
#open data_dir + log_file
with open(os.path.join(data_dir, log_file), 'r', errors='ignore') as f:
labels = []
data=[]
normal_data = []
abnormal_data = []
idx = 0
for line in tqdm(f, desc='get data'):
labels.append(line.split()[0] != '-')
if labels[-1]:
abnormal_data.append(line)
else:
normal_data.append(line)
data.append(line)
idx += 1
else:
#hdfs
if os.path.exists(os.path.join(data_dir, 'preprocessed_data_df.csv')):
print("preprocessed hdfs:preprocessed_data_df.csv exists")
import ast
def str_to_list(s):
return ast.literal_eval(s)
data_df=pd.read_csv(os.path.join(data_dir, 'preprocessed_data_df.csv'), converters={'Raw':str_to_list,'labeled_Raw': str_to_list, 'parsed_unique_log': str_to_list})
else:
print("preprocess hdfs:preprocessed_data_df.csv")
with open(os.path.join(data_dir, log_file), 'r', errors='ignore') as f:
data=[]
for line in tqdm(f, total=11175629, desc='get data'):
data.append(line)
#list to dataframe
df = pd.DataFrame(data, columns=['Raw']) #raw data
data_dict = defaultdict(list) #preserve insertion order of items
for idx, row in tqdm(df.iterrows(), total=df.shape[0], desc='find blk_id'):
blkId_list = re.findall(r'(blk_-?\d+)', row['Raw']) #find all block ids in log Content
blkId_set = set(blkId_list)
for blk_Id in blkId_set:
data_dict[blk_Id].append(row["Raw"])
data_df = pd.DataFrame(list(data_dict.items()), columns=['BlockId', 'Raw'])
# make dataframe:blk_df to dict:blk_label_dict
blk_df=pd.read_csv(blk_label_file)
blk_label_dict = dict(zip(blk_df.BlockId, blk_df.Label))
blk_label_dict = {k: 1 if v == 'Anomaly' else 0 for k, v in blk_label_dict.items()}
data_df["Label"] = data_df["BlockId"].apply(lambda x: blk_label_dict.get(x)) #add label to the sequence of each blockid
parsed_unique_log=parmap.map(get_parsed_log, data_df['Raw'], pm_pbar=True, pm_processes=num_processors-2)
parsed_unique_log=parmap.map(get_unique_log, parsed_unique_log, pm_pbar=True, pm_processes=num_processors-2)
data_df['parsed_unique_log']=parsed_unique_log
data_df=label_parsed_log(data_df)
data_df.to_csv(os.path.join(data_dir, 'preprocessed_data_df.csv'), index=False)
normal_data = data_df[data_df['Label'] == 0]['labeled_parsed_unique_concat'].tolist()
abnormal_data = data_df[data_df['Label'] == 1]['labeled_parsed_unique_concat'].tolist()
#split dataset
if args.sample != 1:
# sample == float or int
# sample data with max_num
#get normal, abnormal data ratio and get # of each max
ab_ratio=len(abnormal_data)/(len(normal_data)+len(abnormal_data))
if isinstance(args.sample, float):
normal_data = random.sample(normal_data, int(len(normal_data)*args.sample))
abnormal_data = random.sample(abnormal_data, int(len(abnormal_data)*args.sample))
normal_train_val, normal_test = train_test_split(normal_data, test_size=args.test_size, random_state=1234, shuffle=args.shuffle)
elif isinstance(args.sample, int):
print("sample data with specific integer num")
normal_data = random.sample(normal_data, int(args.sample*(1-ab_ratio)))
abnormal_data = random.sample(abnormal_data, int(args.sample*ab_ratio))
normal_train_val, normal_test = train_test_split(normal_data, test_size=args.test_size, random_state=1234, shuffle=args.shuffle)
else:
normal_train_val, normal_test = train_test_split(normal_data, test_size=args.test_size, random_state=1234, shuffle=args.shuffle)
test = normal_test + abnormal_data
if args.dataset == "hdfs":
need_newline=True
else:
need_newline=False
save_processed_log(normal_train_val, os.path.join(output_dir, f'train_{args.test_size}'), need_newline)
save_processed_log(test, os.path.join(output_dir, f'test_{args.test_size}'),need_newline)
data_size={}
data_size['train_normal']=len(normal_train_val)
data_size['test_normal']=len(normal_test)
data_size['test_abnormal']=len(abnormal_data)
with open(os.path.join(output_dir, f'data_size_dict_{args.test_size}.json'), 'w') as f:
json.dump(data_size, f, indent=4)