-
Notifications
You must be signed in to change notification settings - Fork 12
/
inputdata.py
177 lines (131 loc) · 4.27 KB
/
inputdata.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ ="Ktian"
import os
import sys
import numpy as np
import io_helper
def readfilesfromAdir(dataset):
#read a list of files
files = os.listdir(dataset)
files_absolute_paths = []
for i in files:
files_absolute_paths.append(dataset+str(i))
return files_absolute_paths
file = "ADFA-LD/Training_Data_Master/UTD-0001.txt"
#this is used to read a char sequence from
def readCharsFromFile(file):
channel_values = open(file).read().split()
#print (len(channel_values))
#channel_values is a list
return channel_values
#print (channel_values[800:819])
def get_attack_subdir(path):
subdirectories = os.listdir(path)
for i in range(0,len(subdirectories)):
subdirectories[i] = path + subdirectories[i]
print (subdirectories)
return (subdirectories)
def get_all_call_sequences(dire):
files = readfilesfromAdir(dire)
allthelist = []
print (len(files))
for eachfile in files:
if not eachfile.endswith("DS_Store"):
allthelist.append(readCharsFromFile(eachfile))
else:
print ("Skip the file "+ str(eachfile))
elements = []
for item in allthelist:
for key in item:
if key not in elements:
elements.append(key)
elements = map(int,elements)
elements = sorted(elements)
print ("The total unique elements:")
print (elements)
print ("The maximum number of elements:")
print (max(elements))
#print ("The length elements:")
#print (len(elements))
print (len(allthelist))
#clean the all list data set
_max = 0
for i in range(0,len(allthelist)):
_max = max(_max,len(allthelist[i]))
allthelist[i] = map(int,allthelist[i])
print ("The maximum length of a sequence is that {}".format(_max))
return (allthelist)
## shift the data for analysis
def shift(seq, n):
n = n % len(seq)
return seq[n:] + seq[:n]
def convertToOneHot(vector, num_classes=None):
"""
Converts an input 1-D vector of integers into an output
2-D array of one-hot vectors, where an i'th input value
of j will set a '1' in the i'th row, j'th column of the
output array.
Example:
v = np.array((1, 0, 4))
one_hot_v = convertToOneHot(v)
print one_hot_v
[[0 1 0 0 0]
[1 0 0 0 0]
[0 0 0 0 1]]
"""
assert isinstance(vector, np.ndarray)
assert len(vector) > 0
if num_classes is None:
num_classes = np.max(vector)+1
else:
assert num_classes > 0
assert num_classes >= np.max(vector)
result = np.zeros(shape=(len(vector), num_classes))
result[np.arange(len(vector)), vector] = 1
return result.astype(int)
"""
The num_class here is set as 341
"""
#one function do one thing
def sequence_n_gram_parsing(alist,n_gram=20,num_class=341):
if len(alist) <= n_gram:
return alist
ans = []
for i in range(0,len(alist)-n_gram+1,1):
tmp = alist[i:i+n_gram]
oneHot = convertToOneHot(np.asarray(tmp), num_class)
ans.append(oneHot)
#transform into nmup arrray
ans = np.array(ans)
return (ans)
def lists_of_list_into_big_matrix(allthelist,n_gram=20):
array = sequence_n_gram_parsing(allthelist[0])
for i in range(1,len(allthelist),1):
tmp = sequence_n_gram_parsing(allthelist[i])
#print ("tmp shape")
#print (tmp.shape)
array = np.concatenate((array, tmp), axis=0)
percent = (i+0.0)/len(allthelist)
io_helper.drawProgressBar(percent)
if (len(array)> 20000):
break
#print ("array shape")
#print (array.shape)
print (array.shape)
print ("done")
io_helper.saveintopickle(array,"array_test.pickle")
if __name__ == "__main__":
dirc = "ADFA-LD/Training_Data_Master/"
dirc_val = "ADFA-LD/Validation_Data_Master/"
dic_attack ="ADFA-LD/Attack_Data_Master/"
#train1 = get_all_call_sequences(dirc)
#test = [i for i in range(0,300)]
#array = sequence_n_gram_parsing(test)
#print (type(array))
#print (array.shape)
#get_attack_subdir(dic_attack)
#print ("XxxxxxxXXXXXXXXXXX")
#val1 = get_all_call_sequences(dirc_val)
att = get_all_call_sequences(dirc)
lists_of_list_into_big_matrix(att)