-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbasic_feature.py
More file actions
67 lines (56 loc) · 2.38 KB
/
Copy pathbasic_feature.py
File metadata and controls
67 lines (56 loc) · 2.38 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
import time
import os
import traceback
import json
from utility import write_to_file
from conf import STORAGE
def basic_feature(fpath):
feature={}
with open(fpath) as f:
jsondata = json.load(f)
threads=jsondata['threads']
file_id =jsondata['file_id']
label = jsondata.get('lable','-1')
threadnum=len(threads)
totalapicall = 0
maxapicall = 0
minapicall = 0
meanapicallperthread=0.0
for tid in threads:
info = threads[tid]
totalapicall = len(info['api_calls'])
maxapicall=max(maxapicall,len(info['api_calls']))
minapicall=min(minapicall,len(info['api_calls']))
meanapicallperthread = (totalapicall*1.0)/threadnum
print('{} {} {} {} {} {} {}'.format(file_id,label,threadnum,totalapicall,maxapicall,minapicall,meanapicallperthread))
feature['file_id'] = file_id
feature['label'] = label
feature['threadnum'] = threadnum
feature['totalapicall'] = totalapicall
feature['maxapicall'] = maxapicall
feature['minapicall'] = minapicall
feature['meanapicallperthread'] = meanapicallperthread
return feature
def folder_to_basic_feature(folder,feature_save_path):
featureheader=['file_id','label','threadnum','totalapicall','maxapicall','minapicall','meanapicallperthread']
stime=time.time()
count=0
write_to_file(feature_save_path,'',mode='w+')
for subfolder in os.listdir(folder):
fullsubfolder = os.path.join(folder,subfolder)
for fname in os.listdir(fullsubfolder):
fullname = os.path.join(fullsubfolder,fname)
try:
feature = basic_feature(fullname)
attr=[]
for head in featureheader:
attr.append(str(feature.get(head,0)))
txt =','.join(attr)
write_to_file(feature_save_path,txt+'\n',mode='a+')
except Exception as e:
traceback.print_exc()
def main():
folder_to_basic_feature(os.path.join(STORAGE,'train_json'),os.path.join(STORAGE,'train_data','basicfeature.csv'))
folder_to_basic_feature(os.path.join(STORAGE,'test_json'),os.path.join(STORAGE,'test_data','basicfeature.csv'))
if __name__ == '__main__':
main()