-
Notifications
You must be signed in to change notification settings - Fork 1
/
preprocess_script_3part.py
185 lines (167 loc) · 6.18 KB
/
preprocess_script_3part.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
"""
Ming-Chang Chiu
Last Modified: 10/01/2017
Purpose: TO get filetype detected by Tika, file path and byte frequencies of TREC-DD Polar Dataset
Acknowledgement: convertToByteTable, compandBFD, and computeOnlyFingerPrint functions are
modified from Rahul (https://github.com/USCDataScience/NN-fileTypeDetection)
"""
import numpy as np
import h5py
import os
from os import path
import preprocessor
import tika
from tika import detector
import sys
import pandas as pd
from cbor2 import load,loads
import traceback
class preprocess():
def __init__(self, beta=1.5):
#self.path = os.getcwd()
self.path = '/data/polar'
self.beta = beta
self.dict = {}
self.output = []
def convertToByteTable(self, filename):
"""
Converts the contents of the file to a 256 byte array
input: filename
output: byte table consisting of frequency distribution
"""
try:
table1 = [0] * 256
table2 = [0] * 256
table3 = [0] * 256
#print filename
#with open(filename, 'rb') as fp:
data = open(filename, 'rb')
#print 'ssssss open ok'
pt1 = data.read(256)
for c in pt1:
table1[ord(c)] += 1
pt2 = data.read()
pt2, pt3 = pt2[:-256], pt2[-256:]
for c in pt2:
table2[ord(c)] += 1
for c in pt3:
table3[ord(c)] += 1
'''
buff = data.read(2 ** 20)
while buff:
for c in buff:
table2[ord(c)] += 1
buff = data.read(2 ** 20)
'''
data.close()
return table1, table2, table3
except:
print 'Usage: %s <filename>' % os.path.basename(sys.argv[0])
#self.logger('Usage: %s <filename>' % os.path.basename(sys.argv[0]))
def compandBFD(self, table):
"""
performs beta companding with beta value default as 1.5
input: byte frequency table
output: normalizes the values and compands to return a byte array.
"""
table = [x * 1.0 / max(table) for x in table]
table = [(x ** (1. / self.beta)) for x in table]
return table
def computeOnlyFingerPrint(self, filename):
table1, table2, table3 = self.convertToByteTable(filename)
if max(table1) > 0:
table1 = self.compandBFD(table1)
else:
print 'weird--table1 are 0s'
if max(table2) > 0:
table2 = self.compandBFD(table2)
if max(table3) > 0:
table3 = self.compandBFD(table3)
return table1 + table2 + table3
def searchfile(path,postfix):
for root, dirs, files in os.walk(path):
for name in files:
#print name
if name.endswith(postfix):
#print root,name
return os.path.join(root,name)
if __name__ == '__main__':
chunk = 1
pp = preprocess()
pp_nobeta = preprocess(1)
temp_path = ''
try:
#if True:
cnt = 0
for root, dirs, files in os.walk(pp.path):
for name in files:
temp_path = os.path.join(root, name)
if os.path.isfile(temp_path) and os.path.getsize(temp_path) > 0: #and temp_path[-4:] != 'json' and temp_path[-2:] != 'py' and temp_path[-2:] != 'sh' and temp_path[-3:] != 'txt' and temp_path[-5:] != 'Store' and temp_path[-3:] != 'pyc' and temp_path[-3:] != 'csv':
filetype = detector.from_file(temp_path)
#print filetype
table = pp.computeOnlyFingerPrint(temp_path)
table.insert(0, filetype)
pp.output.append(table)
table_nobeta = pp_nobeta.computeOnlyFingerPrint(temp_path)
table_nobeta.insert(0, filetype)
pp_nobeta.output.append(table_nobeta)
cnt+=1
if cnt % 100 == 0:
print cnt
if cnt>0 and cnt % 10000 == 0:
df = pd.DataFrame(pp.output)
df.to_csv('temp_3p_data.csv',sep=',', index=False)
df = pd.DataFrame(pp_nobeta.output)
df.to_csv('temp_3p_nobeta_data.csv',sep=',', index=False)
del df
if cnt>0 and cnt % 100000 == 0: #save chunk after prcess 100 thousand valid files
df = pd.DataFrame(pp.output)
df.to_csv('chunk_3p_data'+str(chunk)+'.csv',sep=',', index=False)
df = pd.DataFrame(pp_nobeta.output)
df.to_csv('chunk_3p_nobeta_data'+str(chunk)+'.csv',sep=',', index=False)
pp.output = []
pp_nobeta.output = []
del df
chunk += 1
for name in dirs:
temp_path = os.path.join(root, name)
if os.path.isfile(temp_path) and os.path.getsize(temp_path) > 0:
filetype = detector.from_file(temp_path)
table = pp.computeOnlyFingerPrint(temp_path)
table.insert(0, filetype)
pp.output.append(table)
table_nobeta = pp_nobeta.computeOnlyFingerPrint(temp_path)
table_nobeta.insert(0, filetype)
pp_nobeta.output.append(table_nobeta)
cnt+=1
if cnt % 100 == 0:
print cnt
if cnt>0 and cnt % 10000 == 0:
#np.savetxt('temp_data.csv', np.asarray(pp.output), delimiter= ',', fmt = '%s')
df = pd.DataFrame(pp.output)
df.to_csv('temp_3p_data.csv',sep=',', index=False)
df = pd.DataFrame(pp_nobeta.output)
df.to_csv('temp_3p_nobeta_data.csv',sep=',', index=False)
del df
if cnt>0 and cnt % 100000 == 0: #save chunk after prcess 100 thousand valid files
df = pd.DataFrame(pp.output)
df.to_csv('chunk_3p_data'+str(chunk)+'.csv',sep=',', index=False)
df = pd.DataFrame(pp_nobeta.output)
df.to_csv('chunk_3p_nobeta_data'+str(chunk)+'.csv',sep=',', index=False)
pp.output = []
pp_nobeta.output = []
del df
chunk += 1
except Exception as e:
print 'exception on FILE PATH: ', temp_path
print 'NUM FILE: ', cnt
df = pd.DataFrame(pp.output)
df.to_csv('temp_3p_data.csv',sep=',', index=False)
df = pd.DataFrame(pp_nobeta.output)
df.to_csv('temp_3p_nobeta_data.csv',sep=',', index=False)
print traceback.format_exc()
df = pd.DataFrame(pp.output)
df.to_csv('chunk_3p_data'+str(chunk)+'.csv',sep=',', index=False)
df = pd.DataFrame(pp_nobeta.output)
df.to_csv('chunk_3p_nobeta_data'+str(chunk)+'.csv',sep=',', index=False)
#np.savetxt('all_data.csv', np.asarray(pp.output), delimiter= ',')